subr_prf.c revision 1.185 1 /* $NetBSD: subr_prf.c,v 1.185 2020/07/11 07:14:53 maxv Exp $ */
2
3 /*-
4 * Copyright (c) 1986, 1988, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: subr_prf.c,v 1.185 2020/07/11 07:14:53 maxv Exp $");
41
42 #ifdef _KERNEL_OPT
43 #include "opt_ddb.h"
44 #include "opt_kgdb.h"
45 #include "opt_dump.h"
46 #include "opt_rnd_printf.h"
47 #endif
48
49 #include <sys/param.h>
50 #include <sys/stdint.h>
51 #include <sys/systm.h>
52 #include <sys/buf.h>
53 #include <sys/device.h>
54 #include <sys/reboot.h>
55 #include <sys/msgbuf.h>
56 #include <sys/proc.h>
57 #include <sys/ioctl.h>
58 #include <sys/vnode.h>
59 #include <sys/file.h>
60 #include <sys/tty.h>
61 #include <sys/tprintf.h>
62 #include <sys/spldebug.h>
63 #include <sys/syslog.h>
64 #include <sys/kprintf.h>
65 #include <sys/atomic.h>
66 #include <sys/kernel.h>
67 #include <sys/cpu.h>
68 #include <sys/rndsource.h>
69 #include <sys/kmem.h>
70
71 #include <dev/cons.h>
72
73 #include <net/if.h>
74
75 static kmutex_t kprintf_mtx;
76 static bool kprintf_inited = false;
77
78 #ifdef KGDB
79 #include <sys/kgdb.h>
80 #endif
81
82 #ifdef DDB
83 #include <ddb/ddbvar.h> /* db_panic */
84 #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */
85 #endif
86
87
88 /*
89 * defines
90 */
91 #define KLOG_PRI 0x80000000
92
93
94 /*
95 * local prototypes
96 */
97
98 static void putchar(int, int, struct tty *);
99
100
101 /*
102 * globals
103 */
104
105 extern struct tty *constty; /* pointer to console "window" tty */
106 extern int log_open; /* subr_log: is /dev/klog open? */
107 const char *panicstr; /* arg to first call to panic (used as a flag
108 to indicate that panic has already been called). */
109 struct cpu_info *paniccpu; /* cpu that first paniced */
110 long panicstart, panicend; /* position in the msgbuf of the start and
111 end of the formatted panicstr. */
112 int doing_shutdown; /* set to indicate shutdown in progress */
113
114 #ifdef RND_PRINTF
115 static krndsource_t rnd_printf_source;
116 #endif
117
118 #ifndef DUMP_ON_PANIC
119 #define DUMP_ON_PANIC 1
120 #endif
121 int dumponpanic = DUMP_ON_PANIC;
122
123 /*
124 * v_putc: routine to putc on virtual console
125 *
126 * the v_putc pointer can be used to redirect the console cnputc elsewhere
127 * [e.g. to a "virtual console"].
128 */
129
130 void (*v_putc)(int) = cnputc; /* start with cnputc (normal cons) */
131 void (*v_flush)(void) = cnflush; /* start with cnflush (normal cons) */
132
133 const char hexdigits[] = "0123456789abcdef";
134 const char HEXDIGITS[] = "0123456789ABCDEF";
135
136
137 /*
138 * functions
139 */
140
141 /*
142 * Locking is inited fairly early in MI bootstrap. Before that
143 * prints are done unlocked. But that doesn't really matter,
144 * since nothing can preempt us before interrupts are enabled.
145 */
146 void
147 kprintf_init(void)
148 {
149
150 KASSERT(!kprintf_inited && cold); /* not foolproof, but ... */
151 mutex_init(&kprintf_mtx, MUTEX_DEFAULT, IPL_HIGH);
152 #ifdef RND_PRINTF
153 rnd_attach_source(&rnd_printf_source, "printf", RND_TYPE_UNKNOWN,
154 RND_FLAG_COLLECT_TIME|RND_FLAG_COLLECT_VALUE);
155 #endif
156 kprintf_inited = true;
157 }
158
159 void
160 kprintf_lock(void)
161 {
162
163 if (__predict_true(kprintf_inited))
164 mutex_enter(&kprintf_mtx);
165 }
166
167 void
168 kprintf_unlock(void)
169 {
170
171 if (__predict_true(kprintf_inited)) {
172 /* assert kprintf wasn't somehow inited while we were in */
173 KASSERT(mutex_owned(&kprintf_mtx));
174 mutex_exit(&kprintf_mtx);
175 }
176 }
177
178 /*
179 * twiddle: spin a little propellor on the console.
180 */
181
182 void
183 twiddle(void)
184 {
185 static const char twiddle_chars[] = "|/-\\";
186 static int pos;
187
188 kprintf_lock();
189
190 putchar(twiddle_chars[pos++ & 3], TOCONS|NOTSTAMP, NULL);
191 putchar('\b', TOCONS|NOTSTAMP, NULL);
192
193 kprintf_unlock();
194 }
195
196 /*
197 * panic: handle an unresolvable fatal error
198 *
199 * prints "panic: <message>" and reboots. if called twice (i.e. recursive
200 * call) we avoid trying to dump and just reboot (to avoid recursive panics).
201 */
202
203 void
204 panic(const char *fmt, ...)
205 {
206 va_list ap;
207
208 va_start(ap, fmt);
209 vpanic(fmt, ap);
210 va_end(ap);
211 }
212
213 void
214 vpanic(const char *fmt, va_list ap)
215 {
216 CPU_INFO_ITERATOR cii;
217 struct cpu_info *ci, *oci;
218 int bootopt;
219 static char scratchstr[384]; /* stores panic message */
220
221 spldebug_stop();
222
223 if (lwp0.l_cpu && curlwp) {
224 /*
225 * Disable preemption. If already panicing on another CPU, sit
226 * here and spin until the system is rebooted. Allow the CPU that
227 * first paniced to panic again.
228 */
229 kpreempt_disable();
230 ci = curcpu();
231 oci = atomic_cas_ptr((void *)&paniccpu, NULL, ci);
232 if (oci != NULL && oci != ci) {
233 /* Give interrupts a chance to try and prevent deadlock. */
234 for (;;) {
235 #ifndef _RUMPKERNEL /* XXXpooka: temporary build fix, see kern/40505 */
236 DELAY(10);
237 #endif /* _RUMPKERNEL */
238 }
239 }
240
241 /*
242 * Convert the current thread to a bound thread and prevent all
243 * CPUs from scheduling unbound jobs. Do so without taking any
244 * locks.
245 */
246 curlwp->l_pflag |= LP_BOUND;
247 for (CPU_INFO_FOREACH(cii, ci)) {
248 ci->ci_schedstate.spc_flags |= SPCF_OFFLINE;
249 }
250 }
251
252 bootopt = RB_AUTOBOOT | RB_NOSYNC;
253 if (!doing_shutdown) {
254 if (dumponpanic)
255 bootopt |= RB_DUMP;
256 } else
257 printf("Skipping crash dump on recursive panic\n");
258
259 doing_shutdown = 1;
260
261 if (logenabled(msgbufp))
262 panicstart = msgbufp->msg_bufx;
263
264 printf("panic: ");
265 if (panicstr == NULL) {
266 /* first time in panic - store fmt first for precaution */
267 panicstr = fmt;
268
269 vsnprintf(scratchstr, sizeof(scratchstr), fmt, ap);
270 printf("%s", scratchstr);
271 panicstr = scratchstr;
272 } else {
273 vprintf(fmt, ap);
274 }
275 printf("\n");
276
277 if (logenabled(msgbufp))
278 panicend = msgbufp->msg_bufx;
279
280 #ifdef KGDB
281 kgdb_panic();
282 #endif
283 #ifdef KADB
284 if (boothowto & RB_KDB)
285 kdbpanic();
286 #endif
287 #ifdef DDB
288 db_panic();
289 #endif
290 kern_reboot(bootopt, NULL);
291 }
292
293 /*
294 * kernel logging functions: log, logpri, addlog
295 */
296
297 /*
298 * log: write to the log buffer
299 *
300 * => will not sleep [so safe to call from interrupt]
301 * => will log to console if /dev/klog isn't open
302 */
303
304 void
305 log(int level, const char *fmt, ...)
306 {
307 va_list ap;
308
309 kprintf_lock();
310
311 klogpri(level); /* log the level first */
312 va_start(ap, fmt);
313 kprintf(fmt, TOLOG, NULL, NULL, ap);
314 va_end(ap);
315 if (!log_open) {
316 va_start(ap, fmt);
317 kprintf(fmt, TOCONS, NULL, NULL, ap);
318 va_end(ap);
319 }
320
321 kprintf_unlock();
322
323 logwakeup(); /* wake up anyone waiting for log msgs */
324 }
325
326 /*
327 * vlog: write to the log buffer [already have va_list]
328 */
329
330 void
331 vlog(int level, const char *fmt, va_list ap)
332 {
333 va_list cap;
334
335 va_copy(cap, ap);
336 kprintf_lock();
337
338 klogpri(level); /* log the level first */
339 kprintf(fmt, TOLOG, NULL, NULL, ap);
340 if (!log_open)
341 kprintf(fmt, TOCONS, NULL, NULL, cap);
342
343 kprintf_unlock();
344 va_end(cap);
345
346 logwakeup(); /* wake up anyone waiting for log msgs */
347 }
348
349 /*
350 * logpri: log the priority level to the klog
351 */
352
353 void
354 logpri(int level)
355 {
356
357 kprintf_lock();
358 klogpri(level);
359 kprintf_unlock();
360 }
361
362 /*
363 * Note: we must be in the mutex here!
364 */
365 void
366 klogpri(int level)
367 {
368 KASSERT((level & KLOG_PRI) == 0);
369
370 putchar(level | KLOG_PRI, TOLOG, NULL);
371 }
372
373 /*
374 * addlog: add info to previous log message
375 */
376
377 void
378 addlog(const char *fmt, ...)
379 {
380 va_list ap;
381
382 kprintf_lock();
383
384 va_start(ap, fmt);
385 kprintf(fmt, TOLOG, NULL, NULL, ap);
386 va_end(ap);
387 if (!log_open) {
388 va_start(ap, fmt);
389 kprintf(fmt, TOCONS, NULL, NULL, ap);
390 va_end(ap);
391 }
392
393 kprintf_unlock();
394
395 logwakeup();
396 }
397
398 static void
399 putone(int c, int flags, struct tty *tp)
400 {
401 if (panicstr)
402 constty = NULL;
403
404 if ((flags & TOCONS) && tp == NULL && constty) {
405 tp = constty;
406 flags |= TOTTY;
407 }
408 if ((flags & TOTTY) && tp &&
409 tputchar(c, flags, tp) < 0 &&
410 (flags & TOCONS) && tp == constty)
411 constty = NULL;
412 if ((flags & TOLOG) &&
413 c != '\0' && c != '\r' && c != 0177)
414 logputchar(c);
415 if ((flags & TOCONS) && constty == NULL && c != '\0')
416 (*v_putc)(c);
417 }
418
419 static void
420 putlogpri(int level)
421 {
422 char *p;
423 char snbuf[KPRINTF_BUFSIZE];
424
425 putone('<', TOLOG, NULL);
426 snprintf(snbuf, sizeof(snbuf), "%d", level);
427 for (p = snbuf ; *p ; p++)
428 putone(*p, TOLOG, NULL);
429 putone('>', TOLOG, NULL);
430 }
431
432 #ifndef KLOG_NOTIMESTAMP
433 static int needtstamp = 1;
434 int log_ts_prec = 7;
435
436 static void
437 addtstamp(int flags, struct tty *tp)
438 {
439 char buf[64];
440 struct timespec ts;
441 int n, prec;
442 long fsec;
443
444 prec = log_ts_prec;
445 if (prec < 0) {
446 prec = 0;
447 log_ts_prec = prec;
448 } else if (prec > 9) {
449 prec = 9;
450 log_ts_prec = prec;
451 }
452
453 getnanouptime(&ts);
454
455 for (n = prec, fsec = ts.tv_nsec; n < 8; n++)
456 fsec /= 10;
457 if (n < 9)
458 fsec = (fsec / 10) + ((fsec % 10) >= 5);
459
460 n = snprintf(buf, sizeof(buf), "[% 4jd.%.*ld] ",
461 (intmax_t)ts.tv_sec, prec, fsec);
462
463 for (int i = 0; i < n; i++)
464 putone(buf[i], flags, tp);
465 }
466 #endif
467
468 /*
469 * putchar: print a single character on console or user terminal.
470 *
471 * => if console, then the last MSGBUFS chars are saved in msgbuf
472 * for inspection later (e.g. dmesg/syslog)
473 * => we must already be in the mutex!
474 */
475 static void
476 putchar(int c, int flags, struct tty *tp)
477 {
478 if (c & KLOG_PRI) {
479 putlogpri(c & ~KLOG_PRI);
480 return;
481 }
482
483 #ifndef KLOG_NOTIMESTAMP
484 if (c != '\0' && c != '\n' && needtstamp && (flags & NOTSTAMP) == 0) {
485 addtstamp(flags, tp);
486 needtstamp = 0;
487 }
488
489 if (c == '\n')
490 needtstamp = 1;
491 #endif
492 putone(c, flags, tp);
493
494 #ifdef DDB
495 if (flags & TODDB) {
496 db_putchar(c);
497 return;
498 }
499 #endif
500
501 #ifdef RND_PRINTF
502 if (__predict_true(kprintf_inited)) {
503 unsigned char ch = c;
504 rnd_add_data(&rnd_printf_source, &ch, 1, 0);
505 }
506 #endif
507 }
508
509 /*
510 * tablefull: warn that a system table is full
511 */
512
513 void
514 tablefull(const char *tab, const char *hint)
515 {
516 if (hint)
517 log(LOG_ERR, "%s: table is full - %s\n", tab, hint);
518 else
519 log(LOG_ERR, "%s: table is full\n", tab);
520 }
521
522
523 /*
524 * uprintf: print to the controlling tty of the current process
525 *
526 * => we may block if the tty queue is full
527 * => no message is printed if the queue doesn't clear in a reasonable
528 * time
529 */
530
531 void
532 uprintf(const char *fmt, ...)
533 {
534 struct proc *p = curproc;
535 va_list ap;
536
537 /* mutex_enter(&proc_lock); XXXSMP */
538
539 if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) {
540 /* No mutex needed; going to process TTY. */
541 va_start(ap, fmt);
542 kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
543 va_end(ap);
544 }
545
546 /* mutex_exit(&proc_lock); XXXSMP */
547 }
548
549 void
550 uprintf_locked(const char *fmt, ...)
551 {
552 struct proc *p = curproc;
553 va_list ap;
554
555 if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) {
556 /* No mutex needed; going to process TTY. */
557 va_start(ap, fmt);
558 kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
559 va_end(ap);
560 }
561 }
562
563 /*
564 * tprintf functions: used to send messages to a specific process
565 *
566 * usage:
567 * get a tpr_t handle on a process "p" by using "tprintf_open(p)"
568 * use the handle when calling "tprintf"
569 * when done, do a "tprintf_close" to drop the handle
570 */
571
572 /*
573 * tprintf_open: get a tprintf handle on a process "p"
574 *
575 * => returns NULL if process can't be printed to
576 */
577
578 tpr_t
579 tprintf_open(struct proc *p)
580 {
581 tpr_t cookie;
582
583 cookie = NULL;
584
585 mutex_enter(&proc_lock);
586 if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) {
587 proc_sesshold(p->p_session);
588 cookie = (tpr_t)p->p_session;
589 }
590 mutex_exit(&proc_lock);
591
592 return cookie;
593 }
594
595 /*
596 * tprintf_close: dispose of a tprintf handle obtained with tprintf_open
597 */
598
599 void
600 tprintf_close(tpr_t sess)
601 {
602
603 if (sess) {
604 mutex_enter(&proc_lock);
605 /* Releases proc_lock. */
606 proc_sessrele((struct session *)sess);
607 }
608 }
609
610 /*
611 * tprintf: given tprintf handle to a process [obtained with tprintf_open],
612 * send a message to the controlling tty for that process.
613 *
614 * => also sends message to /dev/klog
615 */
616 void
617 tprintf(tpr_t tpr, const char *fmt, ...)
618 {
619 struct session *sess = (struct session *)tpr;
620 struct tty *tp = NULL;
621 int flags = TOLOG;
622 va_list ap;
623
624 /* mutex_enter(&proc_lock); XXXSMP */
625 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
626 flags |= TOTTY;
627 tp = sess->s_ttyp;
628 }
629
630 kprintf_lock();
631
632 klogpri(LOG_INFO);
633 va_start(ap, fmt);
634 kprintf(fmt, flags, tp, NULL, ap);
635 va_end(ap);
636
637 kprintf_unlock();
638 /* mutex_exit(&proc_lock); XXXSMP */
639
640 logwakeup();
641 }
642
643
644 /*
645 * ttyprintf: send a message to a specific tty
646 *
647 * => should be used only by tty driver or anything that knows the
648 * underlying tty will not be revoked(2)'d away. [otherwise,
649 * use tprintf]
650 */
651 void
652 ttyprintf(struct tty *tp, const char *fmt, ...)
653 {
654 va_list ap;
655
656 /* No mutex needed; going to process TTY. */
657 va_start(ap, fmt);
658 kprintf(fmt, TOTTY, tp, NULL, ap);
659 va_end(ap);
660 }
661
662 #ifdef DDB
663
664 /*
665 * db_printf: printf for DDB (via db_putchar)
666 */
667
668 void
669 db_printf(const char *fmt, ...)
670 {
671 va_list ap;
672
673 /* No mutex needed; DDB pauses all processors. */
674 va_start(ap, fmt);
675 kprintf(fmt, TODDB, NULL, NULL, ap);
676 va_end(ap);
677
678 if (db_tee_msgbuf) {
679 va_start(ap, fmt);
680 kprintf(fmt, TOLOG, NULL, NULL, ap);
681 va_end(ap);
682 }
683 }
684
685 void
686 db_vprintf(const char *fmt, va_list ap)
687 {
688 va_list cap;
689
690 va_copy(cap, ap);
691 /* No mutex needed; DDB pauses all processors. */
692 kprintf(fmt, TODDB, NULL, NULL, ap);
693 if (db_tee_msgbuf)
694 kprintf(fmt, TOLOG, NULL, NULL, cap);
695 va_end(cap);
696 }
697
698 #endif /* DDB */
699
700 static void
701 kprintf_internal(const char *fmt, int oflags, void *vp, char *sbuf, ...)
702 {
703 va_list ap;
704
705 va_start(ap, sbuf);
706 (void)kprintf(fmt, oflags, vp, sbuf, ap);
707 va_end(ap);
708 }
709
710 /*
711 * Device autoconfiguration printf routines. These change their
712 * behavior based on the AB_* flags in boothowto. If AB_SILENT
713 * is set, messages never go to the console (but they still always
714 * go to the log). AB_VERBOSE overrides AB_SILENT.
715 */
716
717 /*
718 * aprint_normal: Send to console unless AB_QUIET. Always goes
719 * to the log.
720 */
721 static void
722 aprint_normal_internal(const char *prefix, const char *fmt, va_list ap)
723 {
724 int flags = TOLOG;
725
726 if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 ||
727 (boothowto & AB_VERBOSE) != 0)
728 flags |= TOCONS;
729
730 kprintf_lock();
731
732 if (prefix)
733 kprintf_internal("%s: ", flags, NULL, NULL, prefix);
734 kprintf(fmt, flags, NULL, NULL, ap);
735
736 kprintf_unlock();
737
738 if (!panicstr)
739 logwakeup();
740 }
741
742 void
743 aprint_normal(const char *fmt, ...)
744 {
745 va_list ap;
746
747 va_start(ap, fmt);
748 aprint_normal_internal(NULL, fmt, ap);
749 va_end(ap);
750 }
751
752 void
753 aprint_normal_dev(device_t dv, const char *fmt, ...)
754 {
755 va_list ap;
756
757 KASSERT(dv != NULL);
758
759 va_start(ap, fmt);
760 aprint_normal_internal(device_xname(dv), fmt, ap);
761 va_end(ap);
762 }
763
764 void
765 aprint_normal_ifnet(struct ifnet *ifp, const char *fmt, ...)
766 {
767 va_list ap;
768
769 KASSERT(ifp != NULL);
770
771 va_start(ap, fmt);
772 aprint_normal_internal(ifp->if_xname, fmt, ap);
773 va_end(ap);
774 }
775
776 /*
777 * aprint_error: Send to console unless AB_QUIET. Always goes
778 * to the log. Also counts the number of times called so other
779 * parts of the kernel can report the number of errors during a
780 * given phase of system startup.
781 */
782 static int aprint_error_count;
783
784 int
785 aprint_get_error_count(void)
786 {
787 int count;
788
789 kprintf_lock();
790
791 count = aprint_error_count;
792 aprint_error_count = 0;
793
794 kprintf_unlock();
795
796 return (count);
797 }
798
799 static void
800 aprint_error_internal(const char *prefix, const char *fmt, va_list ap)
801 {
802 int flags = TOLOG;
803
804 if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 ||
805 (boothowto & AB_VERBOSE) != 0)
806 flags |= TOCONS;
807
808 kprintf_lock();
809
810 aprint_error_count++;
811
812 if (prefix)
813 kprintf_internal("%s: ", flags, NULL, NULL, prefix);
814 kprintf_internal("autoconfiguration error: ", TOLOG, NULL, NULL);
815 kprintf(fmt, flags, NULL, NULL, ap);
816
817 kprintf_unlock();
818
819 if (!panicstr)
820 logwakeup();
821 }
822
823 void
824 aprint_error(const char *fmt, ...)
825 {
826 va_list ap;
827
828 va_start(ap, fmt);
829 aprint_error_internal(NULL, fmt, ap);
830 va_end(ap);
831 }
832
833 void
834 aprint_error_dev(device_t dv, const char *fmt, ...)
835 {
836 va_list ap;
837
838 KASSERT(dv != NULL);
839
840 va_start(ap, fmt);
841 aprint_error_internal(device_xname(dv), fmt, ap);
842 va_end(ap);
843 }
844
845 void
846 aprint_error_ifnet(struct ifnet *ifp, const char *fmt, ...)
847 {
848 va_list ap;
849
850 KASSERT(ifp != NULL);
851
852 va_start(ap, fmt);
853 aprint_error_internal(ifp->if_xname, fmt, ap);
854 va_end(ap);
855 }
856
857 /*
858 * aprint_naive: Send to console only if AB_QUIET. Never goes
859 * to the log.
860 */
861 static void
862 aprint_naive_internal(const char *prefix, const char *fmt, va_list ap)
863 {
864 if ((boothowto & (AB_QUIET|AB_SILENT|AB_VERBOSE)) != AB_QUIET)
865 return;
866
867 kprintf_lock();
868
869 if (prefix)
870 kprintf_internal("%s: ", TOCONS, NULL, NULL, prefix);
871 kprintf(fmt, TOCONS, NULL, NULL, ap);
872
873 kprintf_unlock();
874 }
875
876 void
877 aprint_naive(const char *fmt, ...)
878 {
879 va_list ap;
880
881 va_start(ap, fmt);
882 aprint_naive_internal(NULL, fmt, ap);
883 va_end(ap);
884 }
885
886 void
887 aprint_naive_dev(device_t dv, const char *fmt, ...)
888 {
889 va_list ap;
890
891 KASSERT(dv != NULL);
892
893 va_start(ap, fmt);
894 aprint_naive_internal(device_xname(dv), fmt, ap);
895 va_end(ap);
896 }
897
898 void
899 aprint_naive_ifnet(struct ifnet *ifp, const char *fmt, ...)
900 {
901 va_list ap;
902
903 KASSERT(ifp != NULL);
904
905 va_start(ap, fmt);
906 aprint_naive_internal(ifp->if_xname, fmt, ap);
907 va_end(ap);
908 }
909
910 /*
911 * aprint_verbose: Send to console only if AB_VERBOSE. Always
912 * goes to the log.
913 */
914 static void
915 aprint_verbose_internal(const char *prefix, const char *fmt, va_list ap)
916 {
917 int flags = TOLOG;
918
919 if (boothowto & AB_VERBOSE)
920 flags |= TOCONS;
921
922 kprintf_lock();
923
924 if (prefix)
925 kprintf_internal("%s: ", flags, NULL, NULL, prefix);
926 kprintf(fmt, flags, NULL, NULL, ap);
927
928 kprintf_unlock();
929
930 if (!panicstr)
931 logwakeup();
932 }
933
934 void
935 aprint_verbose(const char *fmt, ...)
936 {
937 va_list ap;
938
939 va_start(ap, fmt);
940 aprint_verbose_internal(NULL, fmt, ap);
941 va_end(ap);
942 }
943
944 void
945 aprint_verbose_dev(device_t dv, const char *fmt, ...)
946 {
947 va_list ap;
948
949 KASSERT(dv != NULL);
950
951 va_start(ap, fmt);
952 aprint_verbose_internal(device_xname(dv), fmt, ap);
953 va_end(ap);
954 }
955
956 void
957 aprint_verbose_ifnet(struct ifnet *ifp, const char *fmt, ...)
958 {
959 va_list ap;
960
961 KASSERT(ifp != NULL);
962
963 va_start(ap, fmt);
964 aprint_verbose_internal(ifp->if_xname, fmt, ap);
965 va_end(ap);
966 }
967
968 /*
969 * aprint_debug: Send to console and log only if AB_DEBUG.
970 */
971 static void
972 aprint_debug_internal(const char *prefix, const char *fmt, va_list ap)
973 {
974 if ((boothowto & AB_DEBUG) == 0)
975 return;
976
977 kprintf_lock();
978
979 if (prefix)
980 kprintf_internal("%s: ", TOCONS | TOLOG, NULL, NULL, prefix);
981 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
982
983 kprintf_unlock();
984 }
985
986 void
987 aprint_debug(const char *fmt, ...)
988 {
989 va_list ap;
990
991 va_start(ap, fmt);
992 aprint_debug_internal(NULL, fmt, ap);
993 va_end(ap);
994 }
995
996 void
997 aprint_debug_dev(device_t dv, const char *fmt, ...)
998 {
999 va_list ap;
1000
1001 KASSERT(dv != NULL);
1002
1003 va_start(ap, fmt);
1004 aprint_debug_internal(device_xname(dv), fmt, ap);
1005 va_end(ap);
1006 }
1007
1008 void
1009 aprint_debug_ifnet(struct ifnet *ifp, const char *fmt, ...)
1010 {
1011 va_list ap;
1012
1013 KASSERT(ifp != NULL);
1014
1015 va_start(ap, fmt);
1016 aprint_debug_internal(ifp->if_xname, fmt, ap);
1017 va_end(ap);
1018 }
1019
1020 void
1021 vprintf_flags(int flags, const char *fmt, va_list ap)
1022 {
1023 kprintf_lock();
1024 kprintf(fmt, flags, NULL, NULL, ap);
1025 kprintf_unlock();
1026 }
1027
1028 void
1029 printf_flags(int flags, const char *fmt, ...)
1030 {
1031 va_list ap;
1032
1033 va_start(ap, fmt);
1034 vprintf_flags(flags, fmt, ap);
1035 va_end(ap);
1036 }
1037
1038 void
1039 printf_tolog(const char *fmt, ...)
1040 {
1041 va_list ap;
1042
1043 va_start(ap, fmt);
1044 vprintf_flags(TOLOG, fmt, ap);
1045 va_end(ap);
1046 }
1047
1048 /*
1049 * printf_nolog: Like printf(), but does not send message to the log.
1050 */
1051
1052 void
1053 printf_nolog(const char *fmt, ...)
1054 {
1055 va_list ap;
1056
1057 va_start(ap, fmt);
1058 vprintf_flags(TOCONS, fmt, ap);
1059 va_end(ap);
1060 }
1061
1062 /*
1063 * normal kernel printf functions: printf, vprintf, snprintf, vsnprintf
1064 */
1065
1066 /*
1067 * printf: print a message to the console and the log
1068 */
1069 void
1070 printf(const char *fmt, ...)
1071 {
1072 va_list ap;
1073
1074 va_start(ap, fmt);
1075 vprintf_flags(TOCONS | TOLOG, fmt, ap);
1076 va_end(ap);
1077 }
1078
1079 /*
1080 * vprintf: print a message to the console and the log [already have
1081 * va_list]
1082 */
1083
1084 void
1085 vprintf(const char *fmt, va_list ap)
1086 {
1087 vprintf_flags(TOCONS | TOLOG, fmt, ap);
1088
1089 if (!panicstr)
1090 logwakeup();
1091 }
1092
1093 /*
1094 * snprintf: print a message to a buffer
1095 */
1096 int
1097 snprintf(char *bf, size_t size, const char *fmt, ...)
1098 {
1099 int retval;
1100 va_list ap;
1101
1102 va_start(ap, fmt);
1103 retval = vsnprintf(bf, size, fmt, ap);
1104 va_end(ap);
1105
1106 return retval;
1107 }
1108
1109 /*
1110 * vsnprintf: print a message to a buffer [already have va_list]
1111 */
1112 int
1113 vsnprintf(char *bf, size_t size, const char *fmt, va_list ap)
1114 {
1115 int retval;
1116 char *p;
1117
1118 p = bf + size;
1119 retval = kprintf(fmt, TOBUFONLY, &p, bf, ap);
1120 if (bf && size > 0) {
1121 /* nul terminate */
1122 if (size <= (size_t)retval)
1123 bf[size - 1] = '\0';
1124 else
1125 bf[retval] = '\0';
1126 }
1127 return retval;
1128 }
1129
1130 int
1131 vasprintf(char **bf, const char *fmt, va_list ap)
1132 {
1133 int retval;
1134 va_list cap;
1135
1136 va_copy(cap, ap);
1137 retval = kprintf(fmt, TOBUFONLY, NULL, NULL, cap) + 1;
1138 va_end(cap);
1139 *bf = kmem_alloc(retval, KM_SLEEP);
1140 return vsnprintf(*bf, retval, fmt, ap);
1141 }
1142
1143 /*
1144 * kprintf: scaled down version of printf(3).
1145 *
1146 * this version based on vfprintf() from libc which was derived from
1147 * software contributed to Berkeley by Chris Torek.
1148 *
1149 * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS!
1150 */
1151
1152 /*
1153 * macros for converting digits to letters and vice versa
1154 */
1155 #define to_digit(c) ((c) - '0')
1156 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
1157 #define to_char(n) ((n) + '0')
1158
1159 /*
1160 * flags used during conversion.
1161 */
1162 #define ALT 0x001 /* alternate form */
1163 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
1164 #define LADJUST 0x004 /* left adjustment */
1165 #define LONGDBL 0x008 /* long double; unimplemented */
1166 #define LONGINT 0x010 /* long integer */
1167 #define QUADINT 0x020 /* quad integer */
1168 #define SHORTINT 0x040 /* short integer */
1169 #define MAXINT 0x080 /* intmax_t */
1170 #define PTRINT 0x100 /* intptr_t */
1171 #define SIZEINT 0x200 /* size_t */
1172 #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */
1173 #define FPT 0x800 /* Floating point number */
1174
1175 /*
1176 * To extend shorts properly, we need both signed and unsigned
1177 * argument extraction methods.
1178 */
1179 #define SARG() \
1180 (flags&MAXINT ? va_arg(ap, intmax_t) : \
1181 flags&PTRINT ? va_arg(ap, intptr_t) : \
1182 flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
1183 flags&QUADINT ? va_arg(ap, quad_t) : \
1184 flags&LONGINT ? va_arg(ap, long) : \
1185 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
1186 (long)va_arg(ap, int))
1187 #define UARG() \
1188 (flags&MAXINT ? va_arg(ap, uintmax_t) : \
1189 flags&PTRINT ? va_arg(ap, uintptr_t) : \
1190 flags&SIZEINT ? va_arg(ap, size_t) : \
1191 flags&QUADINT ? va_arg(ap, u_quad_t) : \
1192 flags&LONGINT ? va_arg(ap, u_long) : \
1193 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
1194 (u_long)va_arg(ap, u_int))
1195
1196 #define KPRINTF_PUTCHAR(C) { \
1197 if (oflags == TOBUFONLY) { \
1198 if (sbuf && ((vp == NULL) || (sbuf < tailp))) \
1199 *sbuf++ = (C); \
1200 } else { \
1201 putchar((C), oflags, vp); \
1202 } \
1203 }
1204
1205 void
1206 device_printf(device_t dev, const char *fmt, ...)
1207 {
1208 va_list ap;
1209
1210 va_start(ap, fmt);
1211 printf("%s: ", device_xname(dev));
1212 vprintf(fmt, ap);
1213 va_end(ap);
1214 return;
1215 }
1216
1217 /*
1218 * Guts of kernel printf. Note, we already expect to be in a mutex!
1219 */
1220 int
1221 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap)
1222 {
1223 const char *fmt; /* format string */
1224 int ch; /* character from fmt */
1225 int n; /* handy integer (short term usage) */
1226 char *cp; /* handy char pointer (short term usage) */
1227 int flags; /* flags as above */
1228 int ret; /* return value accumulator */
1229 int width; /* width from format (%8d), or 0 */
1230 int prec; /* precision from format (%.3d), or -1 */
1231 char sign; /* sign prefix (' ', '+', '-', or \0) */
1232
1233 u_quad_t _uquad; /* integer arguments %[diouxX] */
1234 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
1235 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
1236 int realsz; /* field size expanded by dprec */
1237 int size; /* size of converted field or string */
1238 const char *xdigs; /* digits for [xX] conversion */
1239 char bf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
1240 char *tailp; /* tail pointer for snprintf */
1241
1242 if (oflags == TOBUFONLY && (vp != NULL))
1243 tailp = *(char **)vp;
1244 else
1245 tailp = NULL;
1246
1247 cp = NULL; /* XXX: shutup gcc */
1248 size = 0; /* XXX: shutup gcc */
1249
1250 fmt = fmt0;
1251 ret = 0;
1252
1253 xdigs = NULL; /* XXX: shut up gcc warning */
1254
1255 /*
1256 * Scan the format for conversions (`%' character).
1257 */
1258 for (;;) {
1259 for (; *fmt != '%' && *fmt; fmt++) {
1260 ret++;
1261 KPRINTF_PUTCHAR(*fmt);
1262 }
1263 if (*fmt == 0)
1264 goto done;
1265
1266 fmt++; /* skip over '%' */
1267
1268 flags = 0;
1269 dprec = 0;
1270 width = 0;
1271 prec = -1;
1272 sign = '\0';
1273
1274 rflag: ch = *fmt++;
1275 reswitch: switch (ch) {
1276 case ' ':
1277 /*
1278 * ``If the space and + flags both appear, the space
1279 * flag will be ignored.''
1280 * -- ANSI X3J11
1281 */
1282 if (!sign)
1283 sign = ' ';
1284 goto rflag;
1285 case '#':
1286 flags |= ALT;
1287 goto rflag;
1288 case '*':
1289 /*
1290 * ``A negative field width argument is taken as a
1291 * - flag followed by a positive field width.''
1292 * -- ANSI X3J11
1293 * They don't exclude field widths read from args.
1294 */
1295 if ((width = va_arg(ap, int)) >= 0)
1296 goto rflag;
1297 width = -width;
1298 /* FALLTHROUGH */
1299 case '-':
1300 flags |= LADJUST;
1301 goto rflag;
1302 case '+':
1303 sign = '+';
1304 goto rflag;
1305 case '.':
1306 if ((ch = *fmt++) == '*') {
1307 n = va_arg(ap, int);
1308 prec = n < 0 ? -1 : n;
1309 goto rflag;
1310 }
1311 n = 0;
1312 while (is_digit(ch)) {
1313 n = 10 * n + to_digit(ch);
1314 ch = *fmt++;
1315 }
1316 prec = n < 0 ? -1 : n;
1317 goto reswitch;
1318 case '0':
1319 /*
1320 * ``Note that 0 is taken as a flag, not as the
1321 * beginning of a field width.''
1322 * -- ANSI X3J11
1323 */
1324 flags |= ZEROPAD;
1325 goto rflag;
1326 case '1': case '2': case '3': case '4':
1327 case '5': case '6': case '7': case '8': case '9':
1328 n = 0;
1329 do {
1330 n = 10 * n + to_digit(ch);
1331 ch = *fmt++;
1332 } while (is_digit(ch));
1333 width = n;
1334 goto reswitch;
1335 case 'h':
1336 flags |= SHORTINT;
1337 goto rflag;
1338 case 'j':
1339 flags |= MAXINT;
1340 goto rflag;
1341 case 'l':
1342 if (*fmt == 'l') {
1343 fmt++;
1344 flags |= QUADINT;
1345 } else {
1346 flags |= LONGINT;
1347 }
1348 goto rflag;
1349 case 'q':
1350 flags |= QUADINT;
1351 goto rflag;
1352 case 't':
1353 flags |= PTRINT;
1354 goto rflag;
1355 case 'z':
1356 flags |= SIZEINT;
1357 goto rflag;
1358 case 'c':
1359 *(cp = bf) = va_arg(ap, int);
1360 size = 1;
1361 sign = '\0';
1362 break;
1363 case 'D':
1364 flags |= LONGINT;
1365 /*FALLTHROUGH*/
1366 case 'd':
1367 case 'i':
1368 _uquad = SARG();
1369 if ((quad_t)_uquad < 0) {
1370 _uquad = -_uquad;
1371 sign = '-';
1372 }
1373 base = DEC;
1374 goto number;
1375 case 'n':
1376 /* no %n support in the kernel, consume and skip */
1377 if (flags & MAXINT)
1378 (void)va_arg(ap, intmax_t *);
1379 else if (flags & PTRINT)
1380 (void)va_arg(ap, intptr_t *);
1381 else if (flags & SIZEINT)
1382 (void)va_arg(ap, ssize_t *);
1383 else if (flags & QUADINT)
1384 (void)va_arg(ap, quad_t *);
1385 else if (flags & LONGINT)
1386 (void)va_arg(ap, long *);
1387 else if (flags & SHORTINT)
1388 (void)va_arg(ap, short *);
1389 else
1390 (void)va_arg(ap, int *);
1391 continue; /* no output */
1392 case 'O':
1393 flags |= LONGINT;
1394 /*FALLTHROUGH*/
1395 case 'o':
1396 _uquad = UARG();
1397 base = OCT;
1398 goto nosign;
1399 case 'p':
1400 /*
1401 * ``The argument shall be a pointer to void. The
1402 * value of the pointer is converted to a sequence
1403 * of printable characters, in an implementation-
1404 * defined manner.''
1405 * -- ANSI X3J11
1406 */
1407 /* NOSTRICT */
1408 _uquad = (u_long)va_arg(ap, void *);
1409 base = HEX;
1410 xdigs = hexdigits;
1411 flags |= HEXPREFIX;
1412 ch = 'x';
1413 goto nosign;
1414 case 's':
1415 if ((cp = va_arg(ap, char *)) == NULL)
1416 /*XXXUNCONST*/
1417 cp = __UNCONST("(null)");
1418 if (prec >= 0) {
1419 /*
1420 * can't use strlen; can only look for the
1421 * NUL in the first `prec' characters, and
1422 * strlen() will go further.
1423 */
1424 char *p = memchr(cp, 0, prec);
1425
1426 if (p != NULL) {
1427 size = p - cp;
1428 if (size > prec)
1429 size = prec;
1430 } else
1431 size = prec;
1432 } else
1433 size = strlen(cp);
1434 sign = '\0';
1435 break;
1436 case 'U':
1437 flags |= LONGINT;
1438 /*FALLTHROUGH*/
1439 case 'u':
1440 _uquad = UARG();
1441 base = DEC;
1442 goto nosign;
1443 case 'X':
1444 xdigs = HEXDIGITS;
1445 goto hex;
1446 case 'x':
1447 xdigs = hexdigits;
1448 hex: _uquad = UARG();
1449 base = HEX;
1450 /* leading 0x/X only if non-zero */
1451 if (flags & ALT && _uquad != 0)
1452 flags |= HEXPREFIX;
1453
1454 /* unsigned conversions */
1455 nosign: sign = '\0';
1456 /*
1457 * ``... diouXx conversions ... if a precision is
1458 * specified, the 0 flag will be ignored.''
1459 * -- ANSI X3J11
1460 */
1461 number: if ((dprec = prec) >= 0)
1462 flags &= ~ZEROPAD;
1463
1464 /*
1465 * ``The result of converting a zero value with an
1466 * explicit precision of zero is no characters.''
1467 * -- ANSI X3J11
1468 */
1469 cp = bf + KPRINTF_BUFSIZE;
1470 if (_uquad != 0 || prec != 0) {
1471 /*
1472 * Unsigned mod is hard, and unsigned mod
1473 * by a constant is easier than that by
1474 * a variable; hence this switch.
1475 */
1476 switch (base) {
1477 case OCT:
1478 do {
1479 *--cp = to_char(_uquad & 7);
1480 _uquad >>= 3;
1481 } while (_uquad);
1482 /* handle octal leading 0 */
1483 if (flags & ALT && *cp != '0')
1484 *--cp = '0';
1485 break;
1486
1487 case DEC:
1488 /* many numbers are 1 digit */
1489 while (_uquad >= 10) {
1490 *--cp = to_char(_uquad % 10);
1491 _uquad /= 10;
1492 }
1493 *--cp = to_char(_uquad);
1494 break;
1495
1496 case HEX:
1497 do {
1498 *--cp = xdigs[_uquad & 15];
1499 _uquad >>= 4;
1500 } while (_uquad);
1501 break;
1502
1503 default:
1504 /*XXXUNCONST*/
1505 cp = __UNCONST("bug in kprintf: bad base");
1506 size = strlen(cp);
1507 goto skipsize;
1508 }
1509 }
1510 size = bf + KPRINTF_BUFSIZE - cp;
1511 skipsize:
1512 break;
1513 default: /* "%?" prints ?, unless ? is NUL */
1514 if (ch == '\0')
1515 goto done;
1516 /* pretend it was %c with argument ch */
1517 cp = bf;
1518 *cp = ch;
1519 size = 1;
1520 sign = '\0';
1521 break;
1522 }
1523
1524 /*
1525 * All reasonable formats wind up here. At this point, `cp'
1526 * points to a string which (if not flags&LADJUST) should be
1527 * padded out to `width' places. If flags&ZEROPAD, it should
1528 * first be prefixed by any sign or other prefix; otherwise,
1529 * it should be blank padded before the prefix is emitted.
1530 * After any left-hand padding and prefixing, emit zeroes
1531 * required by a decimal [diouxX] precision, then print the
1532 * string proper, then emit zeroes required by any leftover
1533 * floating precision; finally, if LADJUST, pad with blanks.
1534 *
1535 * Compute actual size, so we know how much to pad.
1536 * size excludes decimal prec; realsz includes it.
1537 */
1538 realsz = dprec > size ? dprec : size;
1539 if (sign)
1540 realsz++;
1541 else if (flags & HEXPREFIX)
1542 realsz+= 2;
1543
1544 /* adjust ret */
1545 ret += width > realsz ? width : realsz;
1546
1547 /* right-adjusting blank padding */
1548 if ((flags & (LADJUST|ZEROPAD)) == 0) {
1549 n = width - realsz;
1550 while (n-- > 0)
1551 KPRINTF_PUTCHAR(' ');
1552 }
1553
1554 /* prefix */
1555 if (sign) {
1556 KPRINTF_PUTCHAR(sign);
1557 } else if (flags & HEXPREFIX) {
1558 KPRINTF_PUTCHAR('0');
1559 KPRINTF_PUTCHAR(ch);
1560 }
1561
1562 /* right-adjusting zero padding */
1563 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1564 n = width - realsz;
1565 while (n-- > 0)
1566 KPRINTF_PUTCHAR('0');
1567 }
1568
1569 /* leading zeroes from decimal precision */
1570 n = dprec - size;
1571 while (n-- > 0)
1572 KPRINTF_PUTCHAR('0');
1573
1574 /* the string or number proper */
1575 for (; size--; cp++)
1576 KPRINTF_PUTCHAR(*cp);
1577 /* left-adjusting padding (always blank) */
1578 if (flags & LADJUST) {
1579 n = width - realsz;
1580 while (n-- > 0)
1581 KPRINTF_PUTCHAR(' ');
1582 }
1583 }
1584
1585 done:
1586 if ((oflags == TOBUFONLY) && (vp != NULL))
1587 *(char **)vp = sbuf;
1588 (*v_flush)();
1589
1590 #ifdef RND_PRINTF
1591 if (__predict_true(kprintf_inited))
1592 rnd_add_data(&rnd_printf_source, NULL, 0, 0);
1593 #endif
1594 return ret;
1595 }
1596