xref: /netbsd-src/sys/kern/subr_prf.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: subr_prf.c,v 1.83 2001/11/21 00:55:39 enami 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.83 2001/11/21 00:55:39 enami 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) do {	\
797 	*(b)++ = (c);		\
798 	if (--(l) == 0)		\
799 		goto out;	\
800 } while (0)
801 #define PUTSTR(b, p, l) do {		\
802 	int c;				\
803 	while ((c = *(p)++) != 0) {	\
804 		*(b)++ = c;		\
805 		if (--(l) == 0)		\
806 			goto out;	\
807 	}				\
808 } while (0)
809 
810 	/*
811 	 * Chris Torek's new bitmask format is identified by a leading \177
812 	 */
813 	sep = '<';
814 	if (ch != '\177') {
815 		/* old (standard) format. */
816 		for (;(bit = *p++) != 0;) {
817 			if (val & (1 << (bit - 1))) {
818 				PUTBYTE(bp, sep, left);
819 				for (; (ch = *p) > ' '; ++p) {
820 					PUTBYTE(bp, ch, left);
821 				}
822 				sep = ',';
823 			} else
824 				for (; *p > ' '; ++p)
825 					continue;
826 		}
827 	} else {
828 		/* new quad-capable format; also does fields. */
829 		field = val;
830 		while ((ch = *p++) != '\0') {
831 			bit = *p++;	/* now 0-origin */
832 			switch (ch) {
833 			case 'b':
834 				if (((u_int)(val >> bit) & 1) == 0)
835 					goto skip;
836 				PUTBYTE(bp, sep, left);
837 				PUTSTR(bp, p, left);
838 				sep = ',';
839 				break;
840 			case 'f':
841 			case 'F':
842 				len = *p++;	/* field length */
843 				field = (val >> bit) & ((1ULL << len) - 1);
844 				if (ch == 'F')	/* just extract */
845 					break;
846 				PUTBYTE(bp, sep, left);
847 				sep = ',';
848 				PUTSTR(bp, p, left);
849 				PUTBYTE(bp, '=', left);
850 				sprintf(snbuf, sbase, field);
851 				q = snbuf; PUTSTR(bp, q, left);
852 				break;
853 			case '=':
854 			case ':':
855 				/*
856 				 * Here "bit" is actually a value instead,
857 				 * to be compared against the last field.
858 				 * This only works for values in [0..255],
859 				 * of course.
860 				 */
861 				if ((int)field != bit)
862 					goto skip;
863 				if (ch == '=')
864 					PUTBYTE(bp, '=', left);
865 				PUTSTR(bp, p, left);
866 				break;
867 			default:
868 			skip:
869 				while (*p++ != '\0')
870 					continue;
871 				break;
872 			}
873 		}
874 	}
875 	if (sep != '<')
876 		PUTBYTE(bp, '>', left);
877 
878 out:
879 	return (buf);
880 
881 #undef PUTBYTE
882 #undef PUTSTR
883 }
884 
885 /*
886  * kprintf: scaled down version of printf(3).
887  *
888  * this version based on vfprintf() from libc which was derived from
889  * software contributed to Berkeley by Chris Torek.
890  *
891  * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS!
892  */
893 
894 /*
895  * macros for converting digits to letters and vice versa
896  */
897 #define	to_digit(c)	((c) - '0')
898 #define is_digit(c)	((unsigned)to_digit(c) <= 9)
899 #define	to_char(n)	((n) + '0')
900 
901 /*
902  * flags used during conversion.
903  */
904 #define	ALT		0x001		/* alternate form */
905 #define	HEXPREFIX	0x002		/* add 0x or 0X prefix */
906 #define	LADJUST		0x004		/* left adjustment */
907 #define	LONGDBL		0x008		/* long double; unimplemented */
908 #define	LONGINT		0x010		/* long integer */
909 #define	QUADINT		0x020		/* quad integer */
910 #define	SHORTINT	0x040		/* short integer */
911 #define	MAXINT		0x080		/* intmax_t */
912 #define	PTRINT		0x100		/* intptr_t */
913 #define	SIZEINT		0x200		/* size_t */
914 #define	ZEROPAD		0x400		/* zero (as opposed to blank) pad */
915 #define FPT		0x800		/* Floating point number */
916 
917 	/*
918 	 * To extend shorts properly, we need both signed and unsigned
919 	 * argument extraction methods.
920 	 */
921 #define	SARG() \
922 	(flags&MAXINT ? va_arg(ap, intmax_t) : \
923 	    flags&PTRINT ? va_arg(ap, intptr_t) : \
924 	    flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
925 	    flags&QUADINT ? va_arg(ap, quad_t) : \
926 	    flags&LONGINT ? va_arg(ap, long) : \
927 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
928 	    (long)va_arg(ap, int))
929 #define	UARG() \
930 	(flags&MAXINT ? va_arg(ap, uintmax_t) : \
931 	    flags&PTRINT ? va_arg(ap, uintptr_t) : \
932 	    flags&SIZEINT ? va_arg(ap, size_t) : \
933 	    flags&QUADINT ? va_arg(ap, u_quad_t) : \
934 	    flags&LONGINT ? va_arg(ap, u_long) : \
935 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
936 	    (u_long)va_arg(ap, u_int))
937 
938 #define KPRINTF_PUTCHAR(C) {						\
939 	if (oflags == TOBUFONLY) {					\
940 		if ((vp != NULL) && (sbuf == tailp)) {			\
941 			ret += 1;		/* indicate error */	\
942 			goto overflow;					\
943 		}							\
944 		*sbuf++ = (C);						\
945 	} else {							\
946 		putchar((C), oflags, (struct tty *)vp);			\
947 	}								\
948 }
949 
950 /*
951  * Guts of kernel printf.  Note, we already expect to be in a mutex!
952  */
953 static int
954 kprintf(fmt0, oflags, vp, sbuf, ap)
955 	const char *fmt0;
956 	int oflags;
957 	void *vp;
958 	char *sbuf;
959 	va_list ap;
960 {
961 	char *fmt;		/* format string */
962 	int ch;			/* character from fmt */
963 	int n;			/* handy integer (short term usage) */
964 	char *cp;		/* handy char pointer (short term usage) */
965 	int flags;		/* flags as above */
966 	int ret;		/* return value accumulator */
967 	int width;		/* width from format (%8d), or 0 */
968 	int prec;		/* precision from format (%.3d), or -1 */
969 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
970 
971 	u_quad_t _uquad;	/* integer arguments %[diouxX] */
972 	enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
973 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
974 	int realsz;		/* field size expanded by dprec */
975 	int size;		/* size of converted field or string */
976 	char *xdigs;		/* digits for [xX] conversion */
977 	char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
978 	char *tailp;		/* tail pointer for snprintf */
979 
980 	tailp = NULL;	/* XXX: shutup gcc */
981 	if (oflags == TOBUFONLY && (vp != NULL))
982 		tailp = *(char **)vp;
983 
984 	cp = NULL;	/* XXX: shutup gcc */
985 	size = 0;	/* XXX: shutup gcc */
986 
987 	fmt = (char *)fmt0;
988 	ret = 0;
989 
990 	xdigs = NULL;		/* XXX: shut up gcc warning */
991 
992 	/*
993 	 * Scan the format for conversions (`%' character).
994 	 */
995 	for (;;) {
996 		while (*fmt != '%' && *fmt) {
997 			ret++;
998 			KPRINTF_PUTCHAR(*fmt++);
999 		}
1000 		if (*fmt == 0)
1001 			goto done;
1002 
1003 		fmt++;		/* skip over '%' */
1004 
1005 		flags = 0;
1006 		dprec = 0;
1007 		width = 0;
1008 		prec = -1;
1009 		sign = '\0';
1010 
1011 rflag:		ch = *fmt++;
1012 reswitch:	switch (ch) {
1013 		case ' ':
1014 			/*
1015 			 * ``If the space and + flags both appear, the space
1016 			 * flag will be ignored.''
1017 			 *	-- ANSI X3J11
1018 			 */
1019 			if (!sign)
1020 				sign = ' ';
1021 			goto rflag;
1022 		case '#':
1023 			flags |= ALT;
1024 			goto rflag;
1025 		case '*':
1026 			/*
1027 			 * ``A negative field width argument is taken as a
1028 			 * - flag followed by a positive field width.''
1029 			 *	-- ANSI X3J11
1030 			 * They don't exclude field widths read from args.
1031 			 */
1032 			if ((width = va_arg(ap, int)) >= 0)
1033 				goto rflag;
1034 			width = -width;
1035 			/* FALLTHROUGH */
1036 		case '-':
1037 			flags |= LADJUST;
1038 			goto rflag;
1039 		case '+':
1040 			sign = '+';
1041 			goto rflag;
1042 		case '.':
1043 			if ((ch = *fmt++) == '*') {
1044 				n = va_arg(ap, int);
1045 				prec = n < 0 ? -1 : n;
1046 				goto rflag;
1047 			}
1048 			n = 0;
1049 			while (is_digit(ch)) {
1050 				n = 10 * n + to_digit(ch);
1051 				ch = *fmt++;
1052 			}
1053 			prec = n < 0 ? -1 : n;
1054 			goto reswitch;
1055 		case '0':
1056 			/*
1057 			 * ``Note that 0 is taken as a flag, not as the
1058 			 * beginning of a field width.''
1059 			 *	-- ANSI X3J11
1060 			 */
1061 			flags |= ZEROPAD;
1062 			goto rflag;
1063 		case '1': case '2': case '3': case '4':
1064 		case '5': case '6': case '7': case '8': case '9':
1065 			n = 0;
1066 			do {
1067 				n = 10 * n + to_digit(ch);
1068 				ch = *fmt++;
1069 			} while (is_digit(ch));
1070 			width = n;
1071 			goto reswitch;
1072 		case 'h':
1073 			flags |= SHORTINT;
1074 			goto rflag;
1075 		case 'j':
1076 			flags |= MAXINT;
1077 			goto rflag;
1078 		case 'l':
1079 			if (*fmt == 'l') {
1080 				fmt++;
1081 				flags |= QUADINT;
1082 			} else {
1083 				flags |= LONGINT;
1084 			}
1085 			goto rflag;
1086 		case 'q':
1087 			flags |= QUADINT;
1088 			goto rflag;
1089 		case 't':
1090 			flags |= PTRINT;
1091 			goto rflag;
1092 		case 'z':
1093 			flags |= SIZEINT;
1094 			goto rflag;
1095 		case 'c':
1096 			*(cp = buf) = va_arg(ap, int);
1097 			size = 1;
1098 			sign = '\0';
1099 			break;
1100 		case 'D':
1101 			flags |= LONGINT;
1102 			/*FALLTHROUGH*/
1103 		case 'd':
1104 		case 'i':
1105 			_uquad = SARG();
1106 			if ((quad_t)_uquad < 0) {
1107 				_uquad = -_uquad;
1108 				sign = '-';
1109 			}
1110 			base = DEC;
1111 			goto number;
1112 		case 'n':
1113 			if (flags & MAXINT)
1114 				*va_arg(ap, intmax_t *) = ret;
1115 			else if (flags & PTRINT)
1116 				*va_arg(ap, intptr_t *) = ret;
1117 			else if (flags & SIZEINT)
1118 				*va_arg(ap, ssize_t *) = ret;
1119 			else if (flags & QUADINT)
1120 				*va_arg(ap, quad_t *) = ret;
1121 			else if (flags & LONGINT)
1122 				*va_arg(ap, long *) = ret;
1123 			else if (flags & SHORTINT)
1124 				*va_arg(ap, short *) = ret;
1125 			else
1126 				*va_arg(ap, int *) = ret;
1127 			continue;	/* no output */
1128 		case 'O':
1129 			flags |= LONGINT;
1130 			/*FALLTHROUGH*/
1131 		case 'o':
1132 			_uquad = UARG();
1133 			base = OCT;
1134 			goto nosign;
1135 		case 'p':
1136 			/*
1137 			 * ``The argument shall be a pointer to void.  The
1138 			 * value of the pointer is converted to a sequence
1139 			 * of printable characters, in an implementation-
1140 			 * defined manner.''
1141 			 *	-- ANSI X3J11
1142 			 */
1143 			/* NOSTRICT */
1144 			_uquad = (u_long)va_arg(ap, void *);
1145 			base = HEX;
1146 			xdigs = "0123456789abcdef";
1147 			flags |= HEXPREFIX;
1148 			ch = 'x';
1149 			goto nosign;
1150 		case 's':
1151 			if ((cp = va_arg(ap, char *)) == NULL)
1152 				cp = "(null)";
1153 			if (prec >= 0) {
1154 				/*
1155 				 * can't use strlen; can only look for the
1156 				 * NUL in the first `prec' characters, and
1157 				 * strlen() will go further.
1158 				 */
1159 				char *p = memchr(cp, 0, prec);
1160 
1161 				if (p != NULL) {
1162 					size = p - cp;
1163 					if (size > prec)
1164 						size = prec;
1165 				} else
1166 					size = prec;
1167 			} else
1168 				size = strlen(cp);
1169 			sign = '\0';
1170 			break;
1171 		case 'U':
1172 			flags |= LONGINT;
1173 			/*FALLTHROUGH*/
1174 		case 'u':
1175 			_uquad = UARG();
1176 			base = DEC;
1177 			goto nosign;
1178 		case 'X':
1179 			xdigs = "0123456789ABCDEF";
1180 			goto hex;
1181 		case 'x':
1182 			xdigs = "0123456789abcdef";
1183 hex:			_uquad = UARG();
1184 			base = HEX;
1185 			/* leading 0x/X only if non-zero */
1186 			if (flags & ALT && _uquad != 0)
1187 				flags |= HEXPREFIX;
1188 
1189 			/* unsigned conversions */
1190 nosign:			sign = '\0';
1191 			/*
1192 			 * ``... diouXx conversions ... if a precision is
1193 			 * specified, the 0 flag will be ignored.''
1194 			 *	-- ANSI X3J11
1195 			 */
1196 number:			if ((dprec = prec) >= 0)
1197 				flags &= ~ZEROPAD;
1198 
1199 			/*
1200 			 * ``The result of converting a zero value with an
1201 			 * explicit precision of zero is no characters.''
1202 			 *	-- ANSI X3J11
1203 			 */
1204 			cp = buf + KPRINTF_BUFSIZE;
1205 			if (_uquad != 0 || prec != 0) {
1206 				/*
1207 				 * Unsigned mod is hard, and unsigned mod
1208 				 * by a constant is easier than that by
1209 				 * a variable; hence this switch.
1210 				 */
1211 				switch (base) {
1212 				case OCT:
1213 					do {
1214 						*--cp = to_char(_uquad & 7);
1215 						_uquad >>= 3;
1216 					} while (_uquad);
1217 					/* handle octal leading 0 */
1218 					if (flags & ALT && *cp != '0')
1219 						*--cp = '0';
1220 					break;
1221 
1222 				case DEC:
1223 					/* many numbers are 1 digit */
1224 					while (_uquad >= 10) {
1225 						*--cp = to_char(_uquad % 10);
1226 						_uquad /= 10;
1227 					}
1228 					*--cp = to_char(_uquad);
1229 					break;
1230 
1231 				case HEX:
1232 					do {
1233 						*--cp = xdigs[_uquad & 15];
1234 						_uquad >>= 4;
1235 					} while (_uquad);
1236 					break;
1237 
1238 				default:
1239 					cp = "bug in kprintf: bad base";
1240 					size = strlen(cp);
1241 					goto skipsize;
1242 				}
1243 			}
1244 			size = buf + KPRINTF_BUFSIZE - cp;
1245 		skipsize:
1246 			break;
1247 		default:	/* "%?" prints ?, unless ? is NUL */
1248 			if (ch == '\0')
1249 				goto done;
1250 			/* pretend it was %c with argument ch */
1251 			cp = buf;
1252 			*cp = ch;
1253 			size = 1;
1254 			sign = '\0';
1255 			break;
1256 		}
1257 
1258 		/*
1259 		 * All reasonable formats wind up here.  At this point, `cp'
1260 		 * points to a string which (if not flags&LADJUST) should be
1261 		 * padded out to `width' places.  If flags&ZEROPAD, it should
1262 		 * first be prefixed by any sign or other prefix; otherwise,
1263 		 * it should be blank padded before the prefix is emitted.
1264 		 * After any left-hand padding and prefixing, emit zeroes
1265 		 * required by a decimal [diouxX] precision, then print the
1266 		 * string proper, then emit zeroes required by any leftover
1267 		 * floating precision; finally, if LADJUST, pad with blanks.
1268 		 *
1269 		 * Compute actual size, so we know how much to pad.
1270 		 * size excludes decimal prec; realsz includes it.
1271 		 */
1272 		realsz = dprec > size ? dprec : size;
1273 		if (sign)
1274 			realsz++;
1275 		else if (flags & HEXPREFIX)
1276 			realsz+= 2;
1277 
1278 		/* adjust ret */
1279 		ret += width > realsz ? width : realsz;
1280 
1281 		/* right-adjusting blank padding */
1282 		if ((flags & (LADJUST|ZEROPAD)) == 0) {
1283 			n = width - realsz;
1284 			while (n-- > 0)
1285 				KPRINTF_PUTCHAR(' ');
1286 		}
1287 
1288 		/* prefix */
1289 		if (sign) {
1290 			KPRINTF_PUTCHAR(sign);
1291 		} else if (flags & HEXPREFIX) {
1292 			KPRINTF_PUTCHAR('0');
1293 			KPRINTF_PUTCHAR(ch);
1294 		}
1295 
1296 		/* right-adjusting zero padding */
1297 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1298 			n = width - realsz;
1299 			while (n-- > 0)
1300 				KPRINTF_PUTCHAR('0');
1301 		}
1302 
1303 		/* leading zeroes from decimal precision */
1304 		n = dprec - size;
1305 		while (n-- > 0)
1306 			KPRINTF_PUTCHAR('0');
1307 
1308 		/* the string or number proper */
1309 		while (size--)
1310 			KPRINTF_PUTCHAR(*cp++);
1311 		/* left-adjusting padding (always blank) */
1312 		if (flags & LADJUST) {
1313 			n = width - realsz;
1314 			while (n-- > 0)
1315 				KPRINTF_PUTCHAR(' ');
1316 		}
1317 	}
1318 
1319 done:
1320 	if ((oflags == TOBUFONLY) && (vp != NULL))
1321 		*(char **)vp = sbuf;
1322 overflow:
1323 	return (ret);
1324 	/* NOTREACHED */
1325 }
1326