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