subr_prf.c revision 1.76.2.5 1 /* $NetBSD: subr_prf.c,v 1.76.2.5 2001/11/14 19:16:40 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 1986, 1988, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: subr_prf.c,v 1.76.2.5 2001/11/14 19:16:40 nathanw 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/lwp.h>
58 #include <sys/proc.h>
59 #include <sys/ioctl.h>
60 #include <sys/vnode.h>
61 #include <sys/file.h>
62 #include <sys/tty.h>
63 #include <sys/tprintf.h>
64 #include <sys/syslog.h>
65 #include <sys/malloc.h>
66 #include <sys/lock.h>
67
68 #include <dev/cons.h>
69
70 #ifdef DDB
71 #include <ddb/ddbvar.h>
72 #include <machine/db_machdep.h>
73 #include <ddb/db_command.h>
74 #include <ddb/db_interface.h>
75 #endif
76
77 #ifdef IPKDB
78 #include <ipkdb/ipkdb.h>
79 #endif
80
81 #if defined(MULTIPROCESSOR)
82 struct simplelock kprintf_slock = SIMPLELOCK_INITIALIZER;
83
84 /*
85 * Use cpu_simple_lock() and cpu_simple_unlock(). These are the actual
86 * atomic locking operations, and never attempt to print debugging
87 * information.
88 */
89 #define KPRINTF_MUTEX_ENTER(s) \
90 do { \
91 (s) = splhigh(); \
92 __cpu_simple_lock(&kprintf_slock.lock_data); \
93 } while (0)
94
95 #define KPRINTF_MUTEX_EXIT(s) \
96 do { \
97 __cpu_simple_unlock(&kprintf_slock.lock_data); \
98 splx((s)); \
99 } while (0)
100 #else /* ! MULTIPROCESSOR */
101 #define KPRINTF_MUTEX_ENTER(s) (s) = splhigh()
102 #define KPRINTF_MUTEX_EXIT(s) splx((s))
103 #endif /* MULTIPROCESSOR */
104
105 /*
106 * note that stdarg.h and the ansi style va_start macro is used for both
107 * ansi and traditional c complers.
108 * XXX: this requires that stdarg.h define: va_alist and va_dcl
109 */
110 #include <machine/stdarg.h>
111
112
113 #ifdef KGDB
114 #include <sys/kgdb.h>
115 #include <machine/cpu.h>
116 #endif
117 #ifdef DDB
118 #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */
119 #endif
120
121
122 /*
123 * defines
124 */
125
126 /* flags for kprintf */
127 #define TOCONS 0x01 /* to the console */
128 #define TOTTY 0x02 /* to the process' tty */
129 #define TOLOG 0x04 /* to the kernel message buffer */
130 #define TOBUFONLY 0x08 /* to the buffer (only) [for snprintf] */
131 #define TODDB 0x10 /* to ddb console */
132
133 /* max size buffer kprintf needs to print quad_t [size in base 8 + \0] */
134 #define KPRINTF_BUFSIZE (sizeof(quad_t) * NBBY / 3 + 2)
135
136
137 /*
138 * local prototypes
139 */
140
141 static int kprintf __P((const char *, int, void *,
142 char *, va_list));
143 static void putchar __P((int, int, struct tty *));
144 static void klogpri __P((int));
145
146
147 /*
148 * globals
149 */
150
151 extern struct tty *constty; /* pointer to console "window" tty */
152 extern int log_open; /* subr_log: is /dev/klog open? */
153 const char *panicstr; /* arg to first call to panic (used as a flag
154 to indicate that panic has already been called). */
155 long panicstart, panicend; /* position in the msgbuf of the start and
156 end of the formatted panicstr. */
157 int doing_shutdown; /* set to indicate shutdown in progress */
158
159 /*
160 * v_putc: routine to putc on virtual console
161 *
162 * the v_putc pointer can be used to redirect the console cnputc elsewhere
163 * [e.g. to a "virtual console"].
164 */
165
166 void (*v_putc) __P((int)) = cnputc; /* start with cnputc (normal cons) */
167
168
169 /*
170 * functions
171 */
172
173 /*
174 * tablefull: warn that a system table is full
175 */
176
177 void
178 tablefull(tab, hint)
179 const char *tab, *hint;
180 {
181 if (hint)
182 log(LOG_ERR, "%s: table is full - %s\n", tab, hint);
183 else
184 log(LOG_ERR, "%s: table is full\n", tab);
185 }
186
187 /*
188 * panic: handle an unresolvable fatal error
189 *
190 * prints "panic: <message>" and reboots. if called twice (i.e. recursive
191 * call) we avoid trying to sync the disk and just reboot (to avoid
192 * recursive panics).
193 */
194
195 void
196 #ifdef __STDC__
197 panic(const char *fmt, ...)
198 #else
199 panic(fmt, va_alist)
200 char *fmt;
201 va_dcl
202 #endif
203 {
204 int bootopt;
205 va_list ap;
206
207 bootopt = RB_AUTOBOOT | RB_DUMP;
208 if (doing_shutdown)
209 bootopt |= RB_NOSYNC;
210 if (!panicstr)
211 panicstr = fmt;
212 doing_shutdown = 1;
213
214 if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC)
215 panicstart = msgbufp->msg_bufx;
216
217 va_start(ap, fmt);
218 printf("panic: ");
219 vprintf(fmt, ap);
220 printf("\n");
221 va_end(ap);
222
223 if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC)
224 panicend = msgbufp->msg_bufx;
225
226 #ifdef IPKDB
227 ipkdb_panic();
228 #endif
229 #ifdef KGDB
230 kgdb_panic();
231 #endif
232 #ifdef KADB
233 if (boothowto & RB_KDB)
234 kdbpanic();
235 #endif
236 #ifdef DDB
237 if (db_onpanic)
238 Debugger();
239 else {
240 static int intrace = 0;
241
242 if (intrace==0) {
243 intrace=1;
244 printf("Begin traceback...\n");
245 db_stack_trace_print(
246 (db_expr_t)__builtin_frame_address(0),
247 TRUE, 65535, "", printf);
248 printf("End traceback...\n");
249 intrace=0;
250 } else
251 printf("Faulted in mid-traceback; aborting...");
252 }
253 #endif
254 cpu_reboot(bootopt, NULL);
255 }
256
257 /*
258 * kernel logging functions: log, logpri, addlog
259 */
260
261 /*
262 * log: write to the log buffer
263 *
264 * => will not sleep [so safe to call from interrupt]
265 * => will log to console if /dev/klog isn't open
266 */
267
268 void
269 #ifdef __STDC__
270 log(int level, const char *fmt, ...)
271 #else
272 log(level, fmt, va_alist)
273 int level;
274 char *fmt;
275 va_dcl
276 #endif
277 {
278 int s;
279 va_list ap;
280
281 KPRINTF_MUTEX_ENTER(s);
282
283 klogpri(level); /* log the level first */
284 va_start(ap, fmt);
285 kprintf(fmt, TOLOG, NULL, NULL, ap);
286 va_end(ap);
287 if (!log_open) {
288 va_start(ap, fmt);
289 kprintf(fmt, TOCONS, NULL, NULL, ap);
290 va_end(ap);
291 }
292
293 KPRINTF_MUTEX_EXIT(s);
294
295 logwakeup(); /* wake up anyone waiting for log msgs */
296 }
297
298 /*
299 * vlog: write to the log buffer [already have va_alist]
300 */
301
302 void
303 vlog(level, fmt, ap)
304 int level;
305 const char *fmt;
306 va_list ap;
307 {
308 int s;
309
310 KPRINTF_MUTEX_ENTER(s);
311
312 klogpri(level); /* log the level first */
313 kprintf(fmt, TOLOG, NULL, NULL, ap);
314 if (!log_open)
315 kprintf(fmt, TOCONS, NULL, NULL, ap);
316
317 KPRINTF_MUTEX_EXIT(s);
318
319 logwakeup(); /* wake up anyone waiting for log msgs */
320 }
321
322 /*
323 * logpri: log the priority level to the klog
324 */
325
326 void
327 logpri(level)
328 int level;
329 {
330 int s;
331
332 KPRINTF_MUTEX_ENTER(s);
333 klogpri(level);
334 KPRINTF_MUTEX_EXIT(s);
335 }
336
337 /*
338 * Note: we must be in the mutex here!
339 */
340 static void
341 klogpri(level)
342 int level;
343 {
344 char *p;
345 char snbuf[KPRINTF_BUFSIZE];
346
347 putchar('<', TOLOG, NULL);
348 snprintf(snbuf, sizeof(snbuf), "%d", level);
349 for (p = snbuf ; *p ; p++)
350 putchar(*p, TOLOG, NULL);
351 putchar('>', TOLOG, NULL);
352 }
353
354 /*
355 * addlog: add info to previous log message
356 */
357
358 void
359 #ifdef __STDC__
360 addlog(const char *fmt, ...)
361 #else
362 addlog(fmt, va_alist)
363 char *fmt;
364 va_dcl
365 #endif
366 {
367 int s;
368 va_list ap;
369
370 KPRINTF_MUTEX_ENTER(s);
371
372 va_start(ap, fmt);
373 kprintf(fmt, TOLOG, NULL, NULL, ap);
374 va_end(ap);
375 if (!log_open) {
376 va_start(ap, fmt);
377 kprintf(fmt, TOCONS, NULL, NULL, ap);
378 va_end(ap);
379 }
380
381 KPRINTF_MUTEX_EXIT(s);
382
383 logwakeup();
384 }
385
386
387 /*
388 * putchar: print a single character on console or user terminal.
389 *
390 * => if console, then the last MSGBUFS chars are saved in msgbuf
391 * for inspection later (e.g. dmesg/syslog)
392 * => we must already be in the mutex!
393 */
394 static void
395 putchar(c, flags, tp)
396 int c;
397 int flags;
398 struct tty *tp;
399 {
400 struct kern_msgbuf *mbp;
401
402 if (panicstr)
403 constty = NULL;
404 if ((flags & TOCONS) && tp == NULL && constty) {
405 tp = constty;
406 flags |= TOTTY;
407 }
408 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
409 (flags & TOCONS) && tp == constty)
410 constty = NULL;
411 if ((flags & TOLOG) &&
412 c != '\0' && c != '\r' && c != 0177 && msgbufenabled) {
413 mbp = msgbufp;
414 if (mbp->msg_magic != MSG_MAGIC) {
415 /*
416 * Arguably should panic or somehow notify the
417 * user... but how? Panic may be too drastic,
418 * and would obliterate the message being kicked
419 * out (maybe a panic itself), and printf
420 * would invoke us recursively. Silently punt
421 * for now. If syslog is running, it should
422 * notice.
423 */
424 msgbufenabled = 0;
425 } else {
426 mbp->msg_bufc[mbp->msg_bufx++] = c;
427 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
428 mbp->msg_bufx = 0;
429 /* If the buffer is full, keep the most recent data. */
430 if (mbp->msg_bufr == mbp->msg_bufx) {
431 if (++mbp->msg_bufr >= mbp->msg_bufs)
432 mbp->msg_bufr = 0;
433 }
434 }
435 }
436 if ((flags & TOCONS) && constty == NULL && c != '\0')
437 (*v_putc)(c);
438 #ifdef DDB
439 if (flags & TODDB)
440 db_putchar(c);
441 #endif
442 }
443
444
445 /*
446 * uprintf: print to the controlling tty of the current process
447 *
448 * => we may block if the tty queue is full
449 * => no message is printed if the queue doesn't clear in a reasonable
450 * time
451 */
452
453 void
454 #ifdef __STDC__
455 uprintf(const char *fmt, ...)
456 #else
457 uprintf(fmt, va_alist)
458 char *fmt;
459 va_dcl
460 #endif
461 {
462 struct proc *p = curproc->l_proc;
463 va_list ap;
464
465 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
466 /* No mutex needed; going to process TTY. */
467 va_start(ap, fmt);
468 kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
469 va_end(ap);
470 }
471 }
472
473 /*
474 * tprintf functions: used to send messages to a specific process
475 *
476 * usage:
477 * get a tpr_t handle on a process "p" by using "tprintf_open(p)"
478 * use the handle when calling "tprintf"
479 * when done, do a "tprintf_close" to drop the handle
480 */
481
482 /*
483 * tprintf_open: get a tprintf handle on a process "p"
484 *
485 * => returns NULL if process can't be printed to
486 */
487
488 tpr_t
489 tprintf_open(p)
490 struct proc *p;
491 {
492
493 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
494 SESSHOLD(p->p_session);
495 return ((tpr_t) p->p_session);
496 }
497 return ((tpr_t) NULL);
498 }
499
500 /*
501 * tprintf_close: dispose of a tprintf handle obtained with tprintf_open
502 */
503
504 void
505 tprintf_close(sess)
506 tpr_t sess;
507 {
508
509 if (sess)
510 SESSRELE((struct session *) sess);
511 }
512
513 /*
514 * tprintf: given tprintf handle to a process [obtained with tprintf_open],
515 * send a message to the controlling tty for that process.
516 *
517 * => also sends message to /dev/klog
518 */
519 void
520 #ifdef __STDC__
521 tprintf(tpr_t tpr, const char *fmt, ...)
522 #else
523 tprintf(tpr, fmt, va_alist)
524 tpr_t tpr;
525 char *fmt;
526 va_dcl
527 #endif
528 {
529 struct session *sess = (struct session *)tpr;
530 struct tty *tp = NULL;
531 int s, flags = TOLOG;
532 va_list ap;
533
534 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
535 flags |= TOTTY;
536 tp = sess->s_ttyp;
537 }
538
539 KPRINTF_MUTEX_ENTER(s);
540
541 klogpri(LOG_INFO);
542 va_start(ap, fmt);
543 kprintf(fmt, flags, tp, NULL, ap);
544 va_end(ap);
545
546 KPRINTF_MUTEX_EXIT(s);
547
548 logwakeup();
549 }
550
551
552 /*
553 * ttyprintf: send a message to a specific tty
554 *
555 * => should be used only by tty driver or anything that knows the
556 * underlying tty will not be revoked(2)'d away. [otherwise,
557 * use tprintf]
558 */
559 void
560 #ifdef __STDC__
561 ttyprintf(struct tty *tp, const char *fmt, ...)
562 #else
563 ttyprintf(tp, fmt, va_alist)
564 struct tty *tp;
565 char *fmt;
566 va_dcl
567 #endif
568 {
569 va_list ap;
570
571 /* No mutex needed; going to process TTY. */
572 va_start(ap, fmt);
573 kprintf(fmt, TOTTY, tp, NULL, ap);
574 va_end(ap);
575 }
576
577 #ifdef DDB
578
579 /*
580 * db_printf: printf for DDB (via db_putchar)
581 */
582
583 void
584 #ifdef __STDC__
585 db_printf(const char *fmt, ...)
586 #else
587 db_printf(fmt, va_alist)
588 char *fmt;
589 va_dcl
590 #endif
591 {
592 va_list ap;
593
594 /* No mutex needed; DDB pauses all processors. */
595 va_start(ap, fmt);
596 kprintf(fmt, TODDB, NULL, NULL, ap);
597 va_end(ap);
598 }
599
600 #endif /* DDB */
601
602
603 /*
604 * normal kernel printf functions: printf, vprintf, snprintf, vsnprintf
605 */
606
607 /*
608 * printf: print a message to the console and the log
609 */
610 void
611 #ifdef __STDC__
612 printf(const char *fmt, ...)
613 #else
614 printf(fmt, va_alist)
615 char *fmt;
616 va_dcl
617 #endif
618 {
619 va_list ap;
620 int s;
621
622 KPRINTF_MUTEX_ENTER(s);
623
624 va_start(ap, fmt);
625 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
626 va_end(ap);
627
628 KPRINTF_MUTEX_EXIT(s);
629
630 if (!panicstr)
631 logwakeup();
632 }
633
634 /*
635 * vprintf: print a message to the console and the log [already have
636 * va_alist]
637 */
638
639 void
640 vprintf(fmt, ap)
641 const char *fmt;
642 va_list ap;
643 {
644 int s;
645
646 KPRINTF_MUTEX_ENTER(s);
647
648 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
649
650 KPRINTF_MUTEX_EXIT(s);
651
652 if (!panicstr)
653 logwakeup();
654 }
655
656 /*
657 * sprintf: print a message to a buffer
658 */
659 int
660 #ifdef __STDC__
661 sprintf(char *buf, const char *fmt, ...)
662 #else
663 sprintf(buf, fmt, va_alist)
664 char *buf;
665 const char *cfmt;
666 va_dcl
667 #endif
668 {
669 int retval;
670 va_list ap;
671
672 va_start(ap, fmt);
673 retval = kprintf(fmt, TOBUFONLY, NULL, buf, ap);
674 va_end(ap);
675 *(buf + retval) = 0; /* null terminate */
676 return(retval);
677 }
678
679 /*
680 * vsprintf: print a message to a buffer [already have va_alist]
681 */
682
683 int
684 vsprintf(buf, fmt, ap)
685 char *buf;
686 const char *fmt;
687 va_list ap;
688 {
689 int retval;
690
691 retval = kprintf(fmt, TOBUFONLY, NULL, buf, ap);
692 *(buf + retval) = 0; /* null terminate */
693 return (retval);
694 }
695
696 /*
697 * snprintf: print a message to a buffer
698 */
699 int
700 #ifdef __STDC__
701 snprintf(char *buf, size_t size, const char *fmt, ...)
702 #else
703 snprintf(buf, size, fmt, va_alist)
704 char *buf;
705 size_t size;
706 const char *cfmt;
707 va_dcl
708 #endif
709 {
710 int retval;
711 va_list ap;
712 char *p;
713
714 if (size < 1)
715 return (-1);
716 p = buf + size - 1;
717 va_start(ap, fmt);
718 retval = kprintf(fmt, TOBUFONLY, &p, buf, ap);
719 va_end(ap);
720 *(p) = 0; /* null terminate */
721 return(retval);
722 }
723
724 /*
725 * vsnprintf: print a message to a buffer [already have va_alist]
726 */
727 int
728 vsnprintf(buf, size, fmt, ap)
729 char *buf;
730 size_t size;
731 const char *fmt;
732 va_list ap;
733 {
734 int retval;
735 char *p;
736
737 if (size < 1)
738 return (-1);
739 p = buf + size - 1;
740 retval = kprintf(fmt, TOBUFONLY, &p, buf, ap);
741 *(p) = 0; /* null terminate */
742 return(retval);
743 }
744
745 /*
746 * bitmask_snprintf: print an interpreted bitmask to a buffer
747 *
748 * => returns pointer to the buffer
749 */
750 char *
751 bitmask_snprintf(val, p, buf, buflen)
752 u_quad_t val;
753 const char *p;
754 char *buf;
755 size_t buflen;
756 {
757 char *bp, *q;
758 size_t left;
759 char *sbase, snbuf[KPRINTF_BUFSIZE];
760 int base, bit, ch, len, sep;
761 u_quad_t field;
762
763 bp = buf;
764 memset(buf, 0, buflen);
765
766 /*
767 * Always leave room for the trailing NULL.
768 */
769 left = buflen - 1;
770
771 /*
772 * Print the value into the buffer. Abort if there's not
773 * enough room.
774 */
775 if (buflen < KPRINTF_BUFSIZE)
776 return (buf);
777
778 ch = *p++;
779 base = ch != '\177' ? ch : *p++;
780 sbase = base == 8 ? "%qo" : base == 10 ? "%qd" : base == 16 ? "%qx" : 0;
781 if (sbase == 0)
782 return (buf); /* punt if not oct, dec, or hex */
783
784 snprintf(snbuf, sizeof(snbuf), sbase, val);
785 for (q = snbuf ; *q ; q++) {
786 *bp++ = *q;
787 left--;
788 }
789
790 /*
791 * If the value we printed was 0 and we're using the old-style format,
792 * or if we don't have room for "<x>", we're done.
793 */
794 if (((val == 0) && (ch != '\177')) || left < 3)
795 return (buf);
796
797 #define PUTBYTE(b, c, l) \
798 *(b)++ = (c); \
799 if (--(l) == 0) \
800 goto out;
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