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