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