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