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