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