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