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