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