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