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