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