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