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