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