Home | History | Annotate | Line # | Download | only in kern
subr_prf.c revision 1.60
      1 /*	$NetBSD: subr_prf.c,v 1.60 1999/02/06 11:57:35 explorer 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. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)subr_prf.c	8.4 (Berkeley) 5/4/95
     41  */
     42 
     43 #include "opt_ddb.h"
     44 #include "opt_multiprocessor.h"
     45 #include "ipkdb.h"
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/buf.h>
     50 #include <sys/conf.h>
     51 #include <sys/reboot.h>
     52 #include <sys/msgbuf.h>
     53 #include <sys/proc.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/vnode.h>
     56 #include <sys/file.h>
     57 #include <sys/tty.h>
     58 #include <sys/tprintf.h>
     59 #include <sys/syslog.h>
     60 #include <sys/malloc.h>
     61 #include <sys/lock.h>
     62 
     63 #include <dev/cons.h>
     64 
     65 #ifdef DDB
     66 #include <ddb/ddbvar.h>
     67 #endif
     68 
     69 #if defined(MULTIPROCESSOR)
     70 struct simplelock kprintf_slock;
     71 
     72 #define	KPRINTF_MUTEX_ENTER(s)						\
     73 do {									\
     74 	(s) = splhigh();						\
     75 	simple_lock(&kprintf_slock);					\
     76 } while (0)
     77 
     78 #define	KPRINTF_MUTEX_EXIT(s)						\
     79 do {									\
     80 	simple_unlock(&kprintf_slock);					\
     81 	splx((s));							\
     82 } while (0)
     83 #else /* ! MULTIPROCESSOR */
     84 #define	KPRINTF_MUTEX_ENTER(s)	(s) = splhigh()
     85 #define	KPRINTF_MUTEX_EXIT(s)	splx((s))
     86 #endif /* MULTIPROCESSOR */
     87 
     88 /*
     89  * note that stdarg.h and the ansi style va_start macro is used for both
     90  * ansi and traditional c complers.
     91  * XXX: this requires that stdarg.h define: va_alist and va_dcl
     92  */
     93 #include <machine/stdarg.h>
     94 
     95 
     96 #ifdef KGDB
     97 #include <sys/kgdb.h>
     98 #include <machine/cpu.h>
     99 #endif
    100 #ifdef DDB
    101 #include <ddb/db_output.h>	/* db_printf, db_putchar prototypes */
    102 extern	int db_radix;		/* XXX: for non-standard '%r' format */
    103 #endif
    104 
    105 
    106 /*
    107  * defines
    108  */
    109 
    110 /* flags for kprintf */
    111 #define TOCONS		0x01	/* to the console */
    112 #define TOTTY		0x02	/* to the process' tty */
    113 #define TOLOG		0x04	/* to the kernel message buffer */
    114 #define TOBUFONLY	0x08	/* to the buffer (only) [for sprintf] */
    115 #define TODDB		0x10	/* to ddb console */
    116 
    117 /* max size buffer kprintf needs to print quad_t [size in base 8 + \0] */
    118 #define KPRINTF_BUFSIZE		(sizeof(quad_t) * NBBY / 3 + 2)
    119 
    120 
    121 /*
    122  * local prototypes
    123  */
    124 
    125 static int	 kprintf __P((const char *, int, void *,
    126 				char *, va_list));
    127 static void	 putchar __P((int, int, struct tty *));
    128 static void	 klogpri __P((int));
    129 
    130 
    131 /*
    132  * globals
    133  */
    134 
    135 struct	tty *constty;	/* pointer to console "window" tty */
    136 int	consintr = 1;	/* ok to handle console interrupts? */
    137 extern	int log_open;	/* subr_log: is /dev/klog open? */
    138 const	char *panicstr; /* arg to first call to panic (used as a flag
    139 			   to indicate that panic has already been called). */
    140 
    141 /*
    142  * v_putc: routine to putc on virtual console
    143  *
    144  * the v_putc pointer can be used to redirect the console cnputc elsewhere
    145  * [e.g. to a "virtual console"].
    146  */
    147 
    148 void (*v_putc) __P((int)) = cnputc;	/* start with cnputc (normal cons) */
    149 
    150 
    151 /*
    152  * functions
    153  */
    154 
    155 /*
    156  * tablefull: warn that a system table is full
    157  */
    158 
    159 void
    160 tablefull(tab)
    161 	const char *tab;
    162 {
    163 	log(LOG_ERR, "%s: table is full\n", tab);
    164 }
    165 
    166 /*
    167  * panic: handle an unresolvable fatal error
    168  *
    169  * prints "panic: <message>" and reboots.   if called twice (i.e. recursive
    170  * call) we avoid trying to sync the disk and just reboot (to avoid
    171  * recursive panics).
    172  */
    173 
    174 void
    175 #ifdef __STDC__
    176 panic(const char *fmt, ...)
    177 #else
    178 panic(fmt, va_alist)
    179 	char *fmt;
    180 	va_dcl
    181 #endif
    182 {
    183 	int bootopt;
    184 	va_list ap;
    185 
    186 	bootopt = RB_AUTOBOOT | RB_DUMP;
    187 	if (panicstr)
    188 		bootopt |= RB_NOSYNC;
    189 	else
    190 		panicstr = fmt;
    191 
    192 	va_start(ap, fmt);
    193 	printf("panic: ");
    194 	vprintf(fmt, ap);
    195 	printf("\n");
    196 	va_end(ap);
    197 
    198 #if NIPKDB > 0
    199 	ipkdb_panic();
    200 #endif
    201 #ifdef KGDB
    202 	kgdb_panic();
    203 #endif
    204 #ifdef KADB
    205 	if (boothowto & RB_KDB)
    206 		kdbpanic();
    207 #endif
    208 #ifdef DDB
    209 	if (db_onpanic)
    210 		Debugger();
    211 #endif
    212 	cpu_reboot(bootopt, NULL);
    213 }
    214 
    215 /*
    216  * kernel logging functions: log, logpri, addlog
    217  */
    218 
    219 /*
    220  * log: write to the log buffer
    221  *
    222  * => will not sleep [so safe to call from interrupt]
    223  * => will log to console if /dev/klog isn't open
    224  */
    225 
    226 void
    227 #ifdef __STDC__
    228 log(int level, const char *fmt, ...)
    229 #else
    230 log(level, fmt, va_alist)
    231 	int level;
    232 	char *fmt;
    233 	va_dcl
    234 #endif
    235 {
    236 	int s;
    237 	va_list ap;
    238 
    239 	KPRINTF_MUTEX_ENTER(s);
    240 
    241 	klogpri(level);		/* log the level first */
    242 	va_start(ap, fmt);
    243 	kprintf(fmt, TOLOG, NULL, NULL, ap);
    244 	va_end(ap);
    245 	if (!log_open) {
    246 		va_start(ap, fmt);
    247 		kprintf(fmt, TOCONS, NULL, NULL, ap);
    248 		va_end(ap);
    249 	}
    250 
    251 	KPRINTF_MUTEX_EXIT(s);
    252 
    253 	logwakeup();		/* wake up anyone waiting for log msgs */
    254 }
    255 
    256 /*
    257  * logpri: log the priority level to the klog
    258  */
    259 
    260 void
    261 logpri(level)
    262 	int level;
    263 {
    264 	int s;
    265 
    266 	KPRINTF_MUTEX_ENTER(s);
    267 	klogpri(level);
    268 	KPRINTF_MUTEX_EXIT(s);
    269 }
    270 
    271 /*
    272  * Note: we must be in the mutex here!
    273  */
    274 static void
    275 klogpri(level)
    276 	int level;
    277 {
    278 	char *p;
    279 	char snbuf[KPRINTF_BUFSIZE];
    280 
    281 	putchar('<', TOLOG, NULL);
    282 	sprintf(snbuf, "%d", level);
    283 	for (p = snbuf ; *p ; p++)
    284 		putchar(*p, TOLOG, NULL);
    285 	putchar('>', TOLOG, NULL);
    286 }
    287 
    288 /*
    289  * addlog: add info to previous log message
    290  */
    291 
    292 void
    293 #ifdef __STDC__
    294 addlog(const char *fmt, ...)
    295 #else
    296 addlog(fmt, va_alist)
    297 	char *fmt;
    298 	va_dcl
    299 #endif
    300 {
    301 	int s;
    302 	va_list ap;
    303 
    304 	KPRINTF_MUTEX_ENTER(s);
    305 
    306 	va_start(ap, fmt);
    307 	kprintf(fmt, TOLOG, NULL, NULL, ap);
    308 	va_end(ap);
    309 	if (!log_open) {
    310 		va_start(ap, fmt);
    311 		kprintf(fmt, TOCONS, NULL, NULL, ap);
    312 		va_end(ap);
    313 	}
    314 
    315 	KPRINTF_MUTEX_EXIT(s);
    316 
    317 	logwakeup();
    318 }
    319 
    320 
    321 /*
    322  * putchar: print a single character on console or user terminal.
    323  *
    324  * => if console, then the last MSGBUFS chars are saved in msgbuf
    325  *	for inspection later (e.g. dmesg/syslog)
    326  * => we must already be in the mutex!
    327  */
    328 static void
    329 putchar(c, flags, tp)
    330 	register int c;
    331 	int flags;
    332 	struct tty *tp;
    333 {
    334 	register struct kern_msgbuf *mbp;
    335 
    336 	if (panicstr)
    337 		constty = NULL;
    338 	if ((flags & TOCONS) && tp == NULL && constty) {
    339 		tp = constty;
    340 		flags |= TOTTY;
    341 	}
    342 	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
    343 	    (flags & TOCONS) && tp == constty)
    344 		constty = NULL;
    345 	if ((flags & TOLOG) &&
    346 	    c != '\0' && c != '\r' && c != 0177 && msgbufenabled) {
    347 		mbp = msgbufp;
    348 		if (mbp->msg_magic != MSG_MAGIC) {
    349 			/*
    350 			 * Arguably should panic or somehow notify the
    351 			 * user...  but how?  Panic may be too drastic,
    352 			 * and would obliterate the message being kicked
    353 			 * out (maybe a panic itself), and printf
    354 			 * would invoke us recursively.  Silently punt
    355 			 * for now.  If syslog is running, it should
    356 			 * notice.
    357 			 */
    358 			msgbufenabled = 0;
    359 		} else {
    360 			mbp->msg_bufc[mbp->msg_bufx++] = c;
    361 			if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
    362 				mbp->msg_bufx = 0;
    363 			/* If the buffer is full, keep the most recent data. */
    364 			if (mbp->msg_bufr == mbp->msg_bufx) {
    365 				 if (++mbp->msg_bufr >= mbp->msg_bufs)
    366 					mbp->msg_bufr = 0;
    367 			}
    368 		}
    369 	}
    370 	if ((flags & TOCONS) && constty == NULL && c != '\0')
    371 		(*v_putc)(c);
    372 #ifdef DDB
    373 	if (flags & TODDB)
    374 		db_putchar(c);
    375 #endif
    376 }
    377 
    378 
    379 /*
    380  * uprintf: print to the controlling tty of the current process
    381  *
    382  * => we may block if the tty queue is full
    383  * => no message is printed if the queue doesn't clear in a reasonable
    384  *	time
    385  */
    386 
    387 void
    388 #ifdef __STDC__
    389 uprintf(const char *fmt, ...)
    390 #else
    391 uprintf(fmt, va_alist)
    392 	char *fmt;
    393 	va_dcl
    394 #endif
    395 {
    396 	register struct proc *p = curproc;
    397 	va_list ap;
    398 
    399 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
    400 		/* No mutex needed; going to process TTY. */
    401 		va_start(ap, fmt);
    402 		kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
    403 		va_end(ap);
    404 	}
    405 }
    406 
    407 /*
    408  * tprintf functions: used to send messages to a specific process
    409  *
    410  * usage:
    411  *   get a tpr_t handle on a process "p" by using "tprintf_open(p)"
    412  *   use the handle when calling "tprintf"
    413  *   when done, do a "tprintf_close" to drop the handle
    414  */
    415 
    416 /*
    417  * tprintf_open: get a tprintf handle on a process "p"
    418  *
    419  * => returns NULL if process can't be printed to
    420  */
    421 
    422 tpr_t
    423 tprintf_open(p)
    424 	register struct proc *p;
    425 {
    426 
    427 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
    428 		SESSHOLD(p->p_session);
    429 		return ((tpr_t) p->p_session);
    430 	}
    431 	return ((tpr_t) NULL);
    432 }
    433 
    434 /*
    435  * tprintf_close: dispose of a tprintf handle obtained with tprintf_open
    436  */
    437 
    438 void
    439 tprintf_close(sess)
    440 	tpr_t sess;
    441 {
    442 
    443 	if (sess)
    444 		SESSRELE((struct session *) sess);
    445 }
    446 
    447 /*
    448  * tprintf: given tprintf handle to a process [obtained with tprintf_open],
    449  * send a message to the controlling tty for that process.
    450  *
    451  * => also sends message to /dev/klog
    452  */
    453 void
    454 #ifdef __STDC__
    455 tprintf(tpr_t tpr, const char *fmt, ...)
    456 #else
    457 tprintf(tpr, fmt, va_alist)
    458 	tpr_t tpr;
    459 	char *fmt;
    460 	va_dcl
    461 #endif
    462 {
    463 	register struct session *sess = (struct session *)tpr;
    464 	struct tty *tp = NULL;
    465 	int s, flags = TOLOG;
    466 	va_list ap;
    467 
    468 	if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
    469 		flags |= TOTTY;
    470 		tp = sess->s_ttyp;
    471 	}
    472 
    473 	KPRINTF_MUTEX_ENTER(s);
    474 
    475 	klogpri(LOG_INFO);
    476 	va_start(ap, fmt);
    477 	kprintf(fmt, flags, tp, NULL, ap);
    478 	va_end(ap);
    479 
    480 	KPRINTF_MUTEX_EXIT(s);
    481 
    482 	logwakeup();
    483 }
    484 
    485 
    486 /*
    487  * ttyprintf: send a message to a specific tty
    488  *
    489  * => should be used only by tty driver or anything that knows the
    490  *    underlying tty will not be revoked(2)'d away.  [otherwise,
    491  *    use tprintf]
    492  */
    493 void
    494 #ifdef __STDC__
    495 ttyprintf(struct tty *tp, const char *fmt, ...)
    496 #else
    497 ttyprintf(tp, fmt, va_alist)
    498 	struct tty *tp;
    499 	char *fmt;
    500 	va_dcl
    501 #endif
    502 {
    503 	va_list ap;
    504 
    505 	/* No mutex needed; going to process TTY. */
    506 	va_start(ap, fmt);
    507 	kprintf(fmt, TOTTY, tp, NULL, ap);
    508 	va_end(ap);
    509 }
    510 
    511 #ifdef DDB
    512 
    513 /*
    514  * db_printf: printf for DDB (via db_putchar)
    515  */
    516 
    517 void
    518 #ifdef __STDC__
    519 db_printf(const char *fmt, ...)
    520 #else
    521 db_printf(fmt, va_alist)
    522 	char *fmt;
    523 	va_dcl
    524 #endif
    525 {
    526 	va_list ap;
    527 
    528 	/* No mutex needed; DDB pauses all processors. */
    529 	va_start(ap, fmt);
    530 	kprintf(fmt, TODDB, NULL, NULL, ap);
    531 	va_end(ap);
    532 }
    533 
    534 #endif /* DDB */
    535 
    536 
    537 /*
    538  * normal kernel printf functions: printf, vprintf, sprintf
    539  */
    540 
    541 /*
    542  * printf: print a message to the console and the log
    543  */
    544 void
    545 #ifdef __STDC__
    546 printf(const char *fmt, ...)
    547 #else
    548 printf(fmt, va_alist)
    549 	char *fmt;
    550 	va_dcl
    551 #endif
    552 {
    553 	va_list ap;
    554 	int s, savintr;
    555 
    556 	KPRINTF_MUTEX_ENTER(s);
    557 
    558 	savintr = consintr;		/* disable interrupts */
    559 	consintr = 0;
    560 	va_start(ap, fmt);
    561 	kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
    562 	va_end(ap);
    563 
    564 	KPRINTF_MUTEX_EXIT(s);
    565 
    566 	if (!panicstr)
    567 		logwakeup();
    568 	consintr = savintr;		/* reenable interrupts */
    569 }
    570 
    571 /*
    572  * vprintf: print a message to the console and the log [already have
    573  *	va_alist]
    574  */
    575 
    576 void
    577 vprintf(fmt, ap)
    578 	const char *fmt;
    579 	va_list ap;
    580 {
    581 	int s, savintr;
    582 
    583 	KPRINTF_MUTEX_ENTER(s);
    584 
    585 	savintr = consintr;		/* disable interrupts */
    586 	consintr = 0;
    587 	kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
    588 
    589 	KPRINTF_MUTEX_EXIT(s);
    590 
    591 	if (!panicstr)
    592 		logwakeup();
    593 	consintr = savintr;		/* reenable interrupts */
    594 }
    595 
    596 /*
    597  * sprintf: print a message to a buffer
    598  */
    599 int
    600 #ifdef __STDC__
    601 sprintf(char *buf, const char *fmt, ...)
    602 #else
    603 sprintf(buf, fmt, va_alist)
    604         char *buf;
    605         const char *cfmt;
    606         va_dcl
    607 #endif
    608 {
    609 	int retval;
    610 	va_list ap;
    611 
    612 	va_start(ap, fmt);
    613 	retval = kprintf(fmt, TOBUFONLY, NULL, buf, ap);
    614 	va_end(ap);
    615 	*(buf + retval) = 0;	/* null terminate */
    616 	return(retval);
    617 }
    618 
    619 /*
    620  * vsprintf: print a message to a buffer [already have va_alist]
    621  */
    622 
    623 int
    624 vsprintf(buf, fmt, ap)
    625 	char *buf;
    626 	const char *fmt;
    627 	va_list ap;
    628 {
    629 	int retval;
    630 
    631 	retval = kprintf(fmt, TOBUFONLY, NULL, buf, ap);
    632 	*(buf + retval) = 0;	/* null terminate */
    633 	return (retval);
    634 }
    635 
    636 /*
    637  * snprintf: print a message to a buffer
    638  */
    639 int
    640 #ifdef __STDC__
    641 snprintf(char *buf, size_t size, const char *fmt, ...)
    642 #else
    643 snprintf(buf, size, fmt, va_alist)
    644         char *buf;
    645         size_t size;
    646         const char *cfmt;
    647         va_dcl
    648 #endif
    649 {
    650 	int retval;
    651 	va_list ap;
    652 	char *p;
    653 
    654 	if (size < 1)
    655 		return (-1);
    656 	p = buf + size - 1;
    657 	va_start(ap, fmt);
    658 	retval = kprintf(fmt, TOBUFONLY, &p, buf, ap);
    659 	va_end(ap);
    660 	*(p) = 0;	/* null terminate */
    661 	return(retval);
    662 }
    663 
    664 /*
    665  * vsnprintf: print a message to a buffer [already have va_alist]
    666  */
    667 int
    668 vsnprintf(buf, size, fmt, ap)
    669         char *buf;
    670         size_t size;
    671         const char *fmt;
    672         va_list ap;
    673 {
    674 	int retval;
    675 	char *p;
    676 
    677 	if (size < 1)
    678 		return (-1);
    679 	p = buf + size - 1;
    680 	retval = kprintf(fmt, TOBUFONLY, &p, buf, ap);
    681 	*(p) = 0;	/* null terminate */
    682 	return(retval);
    683 }
    684 
    685 /*
    686  * bitmask_snprintf: print a kernel-printf "%b" message to a buffer
    687  *
    688  * => returns pointer to the buffer
    689  * => XXX: useful vs. kernel %b?
    690  */
    691 char *
    692 bitmask_snprintf(val, p, buf, buflen)
    693 	u_quad_t val;
    694 	const char *p;
    695 	char *buf;
    696 	size_t buflen;
    697 {
    698 	char *bp, *q;
    699 	size_t left;
    700 	char *sbase, snbuf[KPRINTF_BUFSIZE];
    701 	int base, bit, ch, len, sep;
    702 	u_quad_t field;
    703 
    704 	bp = buf;
    705 	memset(buf, 0, buflen);
    706 
    707 	/*
    708 	 * Always leave room for the trailing NULL.
    709 	 */
    710 	left = buflen - 1;
    711 
    712 	/*
    713 	 * Print the value into the buffer.  Abort if there's not
    714 	 * enough room.
    715 	 */
    716 	if (buflen < KPRINTF_BUFSIZE)
    717 		return (buf);
    718 
    719 	ch = *p++;
    720 	base = ch != '\177' ? ch : *p++;
    721 	sbase = base == 8 ? "%qo" : base == 10 ? "%qd" : base == 16 ? "%qx" : 0;
    722 	if (sbase == 0)
    723 		return (buf);	/* punt if not oct, dec, or hex */
    724 
    725 	sprintf(snbuf, sbase, val);
    726 	for (q = snbuf ; *q ; q++) {
    727 		*bp++ = *q;
    728 		left--;
    729 	}
    730 
    731 	/*
    732 	 * If the value we printed was 0, or if we don't have room for
    733 	 * "<x>", we're done.
    734 	 */
    735 	if (val == 0 || left < 3)
    736 		return (buf);
    737 
    738 #define PUTBYTE(b, c, l)	\
    739 	*(b)++ = (c);		\
    740 	if (--(l) == 0)		\
    741 		goto out;
    742 #define PUTSTR(b, p, l) do {		\
    743 	int c;				\
    744 	while ((c = *(p)++) != 0) {	\
    745 		*(b)++ = c;		\
    746 		if (--(l) == 0)		\
    747 			goto out;	\
    748 	}				\
    749 } while (0)
    750 
    751 	/*
    752 	 * Chris Torek's new style %b format is identified by a leading \177
    753 	 */
    754 	sep = '<';
    755 	if (ch != '\177') {
    756 		/* old (standard) %b format. */
    757 		for (;(bit = *p++) != 0;) {
    758 			if (val & (1 << (bit - 1))) {
    759 				PUTBYTE(bp, sep, left);
    760 				for (; (ch = *p) > ' '; ++p) {
    761 					PUTBYTE(bp, ch, left);
    762 				}
    763 				sep = ',';
    764 			} else
    765 				for (; *p > ' '; ++p)
    766 					continue;
    767 		}
    768 	} else {
    769 		/* new quad-capable %b format; also does fields. */
    770 		field = val;
    771 		while ((ch = *p++) != '\0') {
    772 			bit = *p++;	/* now 0-origin */
    773 			switch (ch) {
    774 			case 'b':
    775 				if (((u_int)(val >> bit) & 1) == 0)
    776 					goto skip;
    777 				PUTBYTE(bp, sep, left);
    778 				PUTSTR(bp, p, left);
    779 				sep = ',';
    780 				break;
    781 			case 'f':
    782 			case 'F':
    783 				len = *p++;	/* field length */
    784 				field = (val >> bit) & ((1ULL << len) - 1);
    785 				if (ch == 'F')	/* just extract */
    786 					break;
    787 				PUTBYTE(bp, sep, left);
    788 				sep = ',';
    789 				PUTSTR(bp, p, left);
    790 				PUTBYTE(bp, '=', left);
    791 				sprintf(snbuf, sbase, field);
    792 				q = snbuf; PUTSTR(bp, q, left);
    793 				break;
    794 			case '=':
    795 			case ':':
    796 				/*
    797 				 * Here "bit" is actually a value instead,
    798 				 * to be compared against the last field.
    799 				 * This only works for values in [0..255],
    800 				 * of course.
    801 				 */
    802 				if ((int)field != bit)
    803 					goto skip;
    804 				if (ch == '=')
    805 					PUTBYTE(bp, '=', left);
    806 				PUTSTR(bp, p, left);
    807 				break;
    808 			default:
    809 			skip:
    810 				while (*p++ != '\0')
    811 					continue;
    812 				break;
    813 			}
    814 		}
    815 	}
    816 	if (sep != '<')
    817 		PUTBYTE(bp, '>', left);
    818 
    819 out:
    820 	return (buf);
    821 
    822 #undef PUTBYTE
    823 #undef PUTSTR
    824 }
    825 
    826 /*
    827  * kprintf: scaled down version of printf(3).
    828  *
    829  * this version based on vfprintf() from libc which was derived from
    830  * software contributed to Berkeley by Chris Torek.
    831  *
    832  * Two additional formats:
    833  *
    834  * The format %b is supported to decode error registers.
    835  * Its usage is:
    836  *
    837  *	printf("reg=%b\n", regval, "<base><arg>*");
    838  *
    839  * where <base> is the output base expressed as a control character, e.g.
    840  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
    841  * the first of which gives the bit number to be inspected (origin 1), and
    842  * the next characters (up to a control character, i.e. a character <= 32),
    843  * give the name of the register.  Thus:
    844  *
    845  *	kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
    846  *
    847  * would produce output:
    848  *
    849  *	reg=3<BITTWO,BITONE>
    850  *
    851  * The format %: passes an additional format string and argument list
    852  * recursively.  Its usage is:
    853  *
    854  * fn(char *fmt, ...)
    855  * {
    856  *	va_list ap;
    857  *	va_start(ap, fmt);
    858  *	printf("prefix: %: suffix\n", fmt, ap);
    859  *	va_end(ap);
    860  * }
    861  *
    862  * this is the actual printf innards
    863  *
    864  * This code is large and complicated...
    865  *
    866  * NOTE: The kprintf mutex must be held of we're going TOBUF or TOCONS!
    867  */
    868 
    869 /*
    870  * macros for converting digits to letters and vice versa
    871  */
    872 #define	to_digit(c)	((c) - '0')
    873 #define is_digit(c)	((unsigned)to_digit(c) <= 9)
    874 #define	to_char(n)	((n) + '0')
    875 
    876 /*
    877  * flags used during conversion.
    878  */
    879 #define	ALT		0x001		/* alternate form */
    880 #define	HEXPREFIX	0x002		/* add 0x or 0X prefix */
    881 #define	LADJUST		0x004		/* left adjustment */
    882 #define	LONGDBL		0x008		/* long double; unimplemented */
    883 #define	LONGINT		0x010		/* long integer */
    884 #define	QUADINT		0x020		/* quad integer */
    885 #define	SHORTINT	0x040		/* short integer */
    886 #define	ZEROPAD		0x080		/* zero (as opposed to blank) pad */
    887 #define FPT		0x100		/* Floating point number */
    888 
    889 	/*
    890 	 * To extend shorts properly, we need both signed and unsigned
    891 	 * argument extraction methods.
    892 	 */
    893 #define	SARG() \
    894 	(flags&QUADINT ? va_arg(ap, quad_t) : \
    895 	    flags&LONGINT ? va_arg(ap, long) : \
    896 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
    897 	    (long)va_arg(ap, int))
    898 #define	UARG() \
    899 	(flags&QUADINT ? va_arg(ap, u_quad_t) : \
    900 	    flags&LONGINT ? va_arg(ap, u_long) : \
    901 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
    902 	    (u_long)va_arg(ap, u_int))
    903 
    904 #define KPRINTF_PUTCHAR(C) {						\
    905 	if (oflags == TOBUFONLY) {					\
    906 		if ((vp != NULL) && (sbuf == tailp)) {			\
    907 			ret += 1;		/* indicate error */	\
    908 			goto overflow;					\
    909 		}							\
    910 		*sbuf++ = (C);						\
    911 	} else {							\
    912 		putchar((C), oflags, (struct tty *)vp);			\
    913 	}								\
    914 }
    915 
    916 /*
    917  * Guts of kernel printf.  Note, we already expect to be in a mutex!
    918  */
    919 static int
    920 kprintf(fmt0, oflags, vp, sbuf, ap)
    921 	const char *fmt0;
    922 	int oflags;
    923 	void *vp;
    924 	char *sbuf;
    925 	va_list ap;
    926 {
    927 	char *fmt;		/* format string */
    928 	int ch;			/* character from fmt */
    929 	int n;			/* handy integer (short term usage) */
    930 	char *cp;		/* handy char pointer (short term usage) */
    931 	int flags;		/* flags as above */
    932 	int ret;		/* return value accumulator */
    933 	int width;		/* width from format (%8d), or 0 */
    934 	int prec;		/* precision from format (%.3d), or -1 */
    935 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
    936 
    937 	u_quad_t _uquad;	/* integer arguments %[diouxX] */
    938 	enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
    939 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
    940 	int realsz;		/* field size expanded by dprec */
    941 	int size;		/* size of converted field or string */
    942 	char *xdigs;		/* digits for [xX] conversion */
    943 	char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
    944 	char *tailp = NULL;	/* tail pointer for snprintf */
    945 
    946 	tailp = NULL;
    947 	if (oflags == TOBUFONLY && (vp != NULL))
    948 		tailp = *(char **)vp;
    949 
    950 	cp = NULL;	/* XXX: shutup gcc */
    951 	size = 0;	/* XXX: shutup gcc */
    952 
    953 	fmt = (char *)fmt0;
    954 	ret = 0;
    955 
    956 	xdigs = NULL;		/* XXX: shut up gcc warning */
    957 
    958 	/*
    959 	 * Scan the format for conversions (`%' character).
    960 	 */
    961 	for (;;) {
    962 		while (*fmt != '%' && *fmt) {
    963 			ret++;
    964 			KPRINTF_PUTCHAR(*fmt++);
    965 		}
    966 		if (*fmt == 0)
    967 			goto done;
    968 
    969 		fmt++;		/* skip over '%' */
    970 
    971 		flags = 0;
    972 		dprec = 0;
    973 		width = 0;
    974 		prec = -1;
    975 		sign = '\0';
    976 
    977 rflag:		ch = *fmt++;
    978 reswitch:	switch (ch) {
    979 		/* XXX: non-standard '%:' format */
    980 #ifndef __powerpc__
    981 		case ':':
    982 			if (oflags != TOBUFONLY) {
    983 				cp = va_arg(ap, char *);
    984 				kprintf(cp, oflags, vp,
    985 					NULL, va_arg(ap, va_list));
    986 			}
    987 			continue;	/* no output */
    988 #endif
    989 		/* XXX: non-standard '%b' format */
    990 		case 'b': {
    991 			char *b, *z;
    992 			int tmp;
    993 			_uquad = va_arg(ap, int);
    994 			b = va_arg(ap, char *);
    995 			if (*b == 8)
    996 				sprintf(buf, "%qo", _uquad);
    997 			else if (*b == 10)
    998 				sprintf(buf, "%qd", _uquad);
    999 			else if (*b == 16)
   1000 				sprintf(buf, "%qx", _uquad);
   1001 			else
   1002 				break;
   1003 			b++;
   1004 
   1005 			z = buf;
   1006 			while (*z) {
   1007 				ret++;
   1008 				KPRINTF_PUTCHAR(*z++);
   1009 			}
   1010 
   1011 			if (_uquad) {
   1012 				tmp = 0;
   1013 				while ((n = *b++) != 0) {
   1014 					if (_uquad & (1 << (n - 1))) {
   1015 						ret++;
   1016 						KPRINTF_PUTCHAR(tmp ? ',':'<');
   1017 						while ((n = *b) > ' ') {
   1018 							ret++;
   1019 							KPRINTF_PUTCHAR(n);
   1020 							b++;
   1021 						}
   1022 						tmp = 1;
   1023 					} else {
   1024 						while(*b > ' ')
   1025 							b++;
   1026 					}
   1027 				}
   1028 				if (tmp) {
   1029 					ret++;
   1030 					KPRINTF_PUTCHAR('>');
   1031 				}
   1032 			}
   1033 			continue;	/* no output */
   1034 		}
   1035 
   1036 #ifdef DDB
   1037 		/* XXX: non-standard '%r' format (print int in db_radix) */
   1038 		case 'r':
   1039 			if ((oflags & TODDB) == 0)
   1040 				goto default_case;
   1041 
   1042 			if (db_radix == 16)
   1043 				goto case_z;	/* signed hex */
   1044 			_uquad = SARG();
   1045 			if ((quad_t)_uquad < 0) {
   1046 				_uquad = -_uquad;
   1047 				sign = '-';
   1048 			}
   1049 			base = (db_radix == 8) ? OCT : DEC;
   1050 			goto number;
   1051 
   1052 
   1053 		/* XXX: non-standard '%z' format ("signed hex", a "hex %i")*/
   1054 		case 'z':
   1055 		case_z:
   1056 			if ((oflags & TODDB) == 0)
   1057 				goto default_case;
   1058 
   1059 			xdigs = "0123456789abcdef";
   1060 			ch = 'x';	/* the 'x' in '0x' (below) */
   1061 			_uquad = SARG();
   1062 			base = HEX;
   1063 			/* leading 0x/X only if non-zero */
   1064 			if (flags & ALT && _uquad != 0)
   1065 				flags |= HEXPREFIX;
   1066 			if ((quad_t)_uquad < 0) {
   1067 				_uquad = -_uquad;
   1068 				sign = '-';
   1069 			}
   1070 			goto number;
   1071 #endif
   1072 
   1073 		case ' ':
   1074 			/*
   1075 			 * ``If the space and + flags both appear, the space
   1076 			 * flag will be ignored.''
   1077 			 *	-- ANSI X3J11
   1078 			 */
   1079 			if (!sign)
   1080 				sign = ' ';
   1081 			goto rflag;
   1082 		case '#':
   1083 			flags |= ALT;
   1084 			goto rflag;
   1085 		case '*':
   1086 			/*
   1087 			 * ``A negative field width argument is taken as a
   1088 			 * - flag followed by a positive field width.''
   1089 			 *	-- ANSI X3J11
   1090 			 * They don't exclude field widths read from args.
   1091 			 */
   1092 			if ((width = va_arg(ap, int)) >= 0)
   1093 				goto rflag;
   1094 			width = -width;
   1095 			/* FALLTHROUGH */
   1096 		case '-':
   1097 			flags |= LADJUST;
   1098 			goto rflag;
   1099 		case '+':
   1100 			sign = '+';
   1101 			goto rflag;
   1102 		case '.':
   1103 			if ((ch = *fmt++) == '*') {
   1104 				n = va_arg(ap, int);
   1105 				prec = n < 0 ? -1 : n;
   1106 				goto rflag;
   1107 			}
   1108 			n = 0;
   1109 			while (is_digit(ch)) {
   1110 				n = 10 * n + to_digit(ch);
   1111 				ch = *fmt++;
   1112 			}
   1113 			prec = n < 0 ? -1 : n;
   1114 			goto reswitch;
   1115 		case '0':
   1116 			/*
   1117 			 * ``Note that 0 is taken as a flag, not as the
   1118 			 * beginning of a field width.''
   1119 			 *	-- ANSI X3J11
   1120 			 */
   1121 			flags |= ZEROPAD;
   1122 			goto rflag;
   1123 		case '1': case '2': case '3': case '4':
   1124 		case '5': case '6': case '7': case '8': case '9':
   1125 			n = 0;
   1126 			do {
   1127 				n = 10 * n + to_digit(ch);
   1128 				ch = *fmt++;
   1129 			} while (is_digit(ch));
   1130 			width = n;
   1131 			goto reswitch;
   1132 		case 'h':
   1133 			flags |= SHORTINT;
   1134 			goto rflag;
   1135 		case 'l':
   1136 			if (*fmt == 'l') {
   1137 				fmt++;
   1138 				flags |= QUADINT;
   1139 			} else {
   1140 				flags |= LONGINT;
   1141 			}
   1142 			goto rflag;
   1143 		case 'q':
   1144 			flags |= QUADINT;
   1145 			goto rflag;
   1146 		case 'c':
   1147 			*(cp = buf) = va_arg(ap, int);
   1148 			size = 1;
   1149 			sign = '\0';
   1150 			break;
   1151 		case 'D':
   1152 			flags |= LONGINT;
   1153 			/*FALLTHROUGH*/
   1154 		case 'd':
   1155 		case 'i':
   1156 			_uquad = SARG();
   1157 			if ((quad_t)_uquad < 0) {
   1158 				_uquad = -_uquad;
   1159 				sign = '-';
   1160 			}
   1161 			base = DEC;
   1162 			goto number;
   1163 		case 'n':
   1164 #ifdef DDB
   1165 		/* XXX: non-standard '%n' format */
   1166 		/*
   1167 		 * XXX: HACK!   DDB wants '%n' to be a '%u' printed
   1168 		 * in db_radix format.   this should die since '%n'
   1169 		 * is already defined in standard printf to write
   1170 		 * the number of chars printed so far to the arg (which
   1171 		 * should be a pointer.
   1172 		 */
   1173 			if (oflags & TODDB) {
   1174 				if (db_radix == 16)
   1175 					ch = 'x';	/* convert to %x */
   1176 				else if (db_radix == 8)
   1177 					ch = 'o';	/* convert to %o */
   1178 				else
   1179 					ch = 'u';	/* convert to %u */
   1180 
   1181 				/* ... and start again */
   1182 				goto reswitch;
   1183 			}
   1184 
   1185 #endif
   1186 			if (flags & QUADINT)
   1187 				*va_arg(ap, quad_t *) = ret;
   1188 			else if (flags & LONGINT)
   1189 				*va_arg(ap, long *) = ret;
   1190 			else if (flags & SHORTINT)
   1191 				*va_arg(ap, short *) = ret;
   1192 			else
   1193 				*va_arg(ap, int *) = ret;
   1194 			continue;	/* no output */
   1195 		case 'O':
   1196 			flags |= LONGINT;
   1197 			/*FALLTHROUGH*/
   1198 		case 'o':
   1199 			_uquad = UARG();
   1200 			base = OCT;
   1201 			goto nosign;
   1202 		case 'p':
   1203 			/*
   1204 			 * ``The argument shall be a pointer to void.  The
   1205 			 * value of the pointer is converted to a sequence
   1206 			 * of printable characters, in an implementation-
   1207 			 * defined manner.''
   1208 			 *	-- ANSI X3J11
   1209 			 */
   1210 			/* NOSTRICT */
   1211 			_uquad = (u_long)va_arg(ap, void *);
   1212 			base = HEX;
   1213 			xdigs = "0123456789abcdef";
   1214 			flags |= HEXPREFIX;
   1215 			ch = 'x';
   1216 			goto nosign;
   1217 		case 's':
   1218 			if ((cp = va_arg(ap, char *)) == NULL)
   1219 				cp = "(null)";
   1220 			if (prec >= 0) {
   1221 				/*
   1222 				 * can't use strlen; can only look for the
   1223 				 * NUL in the first `prec' characters, and
   1224 				 * strlen() will go further.
   1225 				 */
   1226 				char *p = memchr(cp, 0, prec);
   1227 
   1228 				if (p != NULL) {
   1229 					size = p - cp;
   1230 					if (size > prec)
   1231 						size = prec;
   1232 				} else
   1233 					size = prec;
   1234 			} else
   1235 				size = strlen(cp);
   1236 			sign = '\0';
   1237 			break;
   1238 		case 'U':
   1239 			flags |= LONGINT;
   1240 			/*FALLTHROUGH*/
   1241 		case 'u':
   1242 			_uquad = UARG();
   1243 			base = DEC;
   1244 			goto nosign;
   1245 		case 'X':
   1246 			xdigs = "0123456789ABCDEF";
   1247 			goto hex;
   1248 		case 'x':
   1249 			xdigs = "0123456789abcdef";
   1250 hex:			_uquad = UARG();
   1251 			base = HEX;
   1252 			/* leading 0x/X only if non-zero */
   1253 			if (flags & ALT && _uquad != 0)
   1254 				flags |= HEXPREFIX;
   1255 
   1256 			/* unsigned conversions */
   1257 nosign:			sign = '\0';
   1258 			/*
   1259 			 * ``... diouXx conversions ... if a precision is
   1260 			 * specified, the 0 flag will be ignored.''
   1261 			 *	-- ANSI X3J11
   1262 			 */
   1263 number:			if ((dprec = prec) >= 0)
   1264 				flags &= ~ZEROPAD;
   1265 
   1266 			/*
   1267 			 * ``The result of converting a zero value with an
   1268 			 * explicit precision of zero is no characters.''
   1269 			 *	-- ANSI X3J11
   1270 			 */
   1271 			cp = buf + KPRINTF_BUFSIZE;
   1272 			if (_uquad != 0 || prec != 0) {
   1273 				/*
   1274 				 * Unsigned mod is hard, and unsigned mod
   1275 				 * by a constant is easier than that by
   1276 				 * a variable; hence this switch.
   1277 				 */
   1278 				switch (base) {
   1279 				case OCT:
   1280 					do {
   1281 						*--cp = to_char(_uquad & 7);
   1282 						_uquad >>= 3;
   1283 					} while (_uquad);
   1284 					/* handle octal leading 0 */
   1285 					if (flags & ALT && *cp != '0')
   1286 						*--cp = '0';
   1287 					break;
   1288 
   1289 				case DEC:
   1290 					/* many numbers are 1 digit */
   1291 					while (_uquad >= 10) {
   1292 						*--cp = to_char(_uquad % 10);
   1293 						_uquad /= 10;
   1294 					}
   1295 					*--cp = to_char(_uquad);
   1296 					break;
   1297 
   1298 				case HEX:
   1299 					do {
   1300 						*--cp = xdigs[_uquad & 15];
   1301 						_uquad >>= 4;
   1302 					} while (_uquad);
   1303 					break;
   1304 
   1305 				default:
   1306 					cp = "bug in kprintf: bad base";
   1307 					size = strlen(cp);
   1308 					goto skipsize;
   1309 				}
   1310 			}
   1311 			size = buf + KPRINTF_BUFSIZE - cp;
   1312 		skipsize:
   1313 			break;
   1314 		default:	/* "%?" prints ?, unless ? is NUL */
   1315 #ifdef DDB
   1316 		default_case:	/* DDB */
   1317 #endif
   1318 			if (ch == '\0')
   1319 				goto done;
   1320 			/* pretend it was %c with argument ch */
   1321 			cp = buf;
   1322 			*cp = ch;
   1323 			size = 1;
   1324 			sign = '\0';
   1325 			break;
   1326 		}
   1327 
   1328 		/*
   1329 		 * All reasonable formats wind up here.  At this point, `cp'
   1330 		 * points to a string which (if not flags&LADJUST) should be
   1331 		 * padded out to `width' places.  If flags&ZEROPAD, it should
   1332 		 * first be prefixed by any sign or other prefix; otherwise,
   1333 		 * it should be blank padded before the prefix is emitted.
   1334 		 * After any left-hand padding and prefixing, emit zeroes
   1335 		 * required by a decimal [diouxX] precision, then print the
   1336 		 * string proper, then emit zeroes required by any leftover
   1337 		 * floating precision; finally, if LADJUST, pad with blanks.
   1338 		 *
   1339 		 * Compute actual size, so we know how much to pad.
   1340 		 * size excludes decimal prec; realsz includes it.
   1341 		 */
   1342 		realsz = dprec > size ? dprec : size;
   1343 		if (sign)
   1344 			realsz++;
   1345 		else if (flags & HEXPREFIX)
   1346 			realsz+= 2;
   1347 
   1348 		/* adjust ret */
   1349 		ret += width > realsz ? width : realsz;
   1350 
   1351 		/* right-adjusting blank padding */
   1352 		if ((flags & (LADJUST|ZEROPAD)) == 0) {
   1353 			n = width - realsz;
   1354 			while (n-- > 0)
   1355 				KPRINTF_PUTCHAR(' ');
   1356 		}
   1357 
   1358 		/* prefix */
   1359 		if (sign) {
   1360 			KPRINTF_PUTCHAR(sign);
   1361 		} else if (flags & HEXPREFIX) {
   1362 			KPRINTF_PUTCHAR('0');
   1363 			KPRINTF_PUTCHAR(ch);
   1364 		}
   1365 
   1366 		/* right-adjusting zero padding */
   1367 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
   1368 			n = width - realsz;
   1369 			while (n-- > 0)
   1370 				KPRINTF_PUTCHAR('0');
   1371 		}
   1372 
   1373 		/* leading zeroes from decimal precision */
   1374 		n = dprec - size;
   1375 		while (n-- > 0)
   1376 			KPRINTF_PUTCHAR('0');
   1377 
   1378 		/* the string or number proper */
   1379 		while (size--)
   1380 			KPRINTF_PUTCHAR(*cp++);
   1381 		/* left-adjusting padding (always blank) */
   1382 		if (flags & LADJUST) {
   1383 			n = width - realsz;
   1384 			while (n-- > 0)
   1385 				KPRINTF_PUTCHAR(' ');
   1386 		}
   1387 	}
   1388 
   1389 done:
   1390 	if ((oflags == TOBUFONLY) && (vp != NULL))
   1391 		*(char **)vp = sbuf;
   1392 overflow:
   1393 	return (ret);
   1394 	/* NOTREACHED */
   1395 }
   1396