subr_prf.c revision 1.55 1 /* $NetBSD: subr_prf.c,v 1.55 1998/09/29 21:03:02 thorpej 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, struct tty *,
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 * bitmask_snprintf: print a kernel-printf "%b" message to a buffer
638 *
639 * => returns pointer to the buffer
640 * => XXX: useful vs. kernel %b?
641 */
642 char *
643 bitmask_snprintf(val, p, buf, buflen)
644 u_quad_t val;
645 const char *p;
646 char *buf;
647 size_t buflen;
648 {
649 char *bp, *q;
650 size_t left;
651 char *sbase, snbuf[KPRINTF_BUFSIZE];
652 int base, bit, ch, len, sep;
653 u_quad_t field;
654
655 bp = buf;
656 memset(buf, 0, buflen);
657
658 /*
659 * Always leave room for the trailing NULL.
660 */
661 left = buflen - 1;
662
663 /*
664 * Print the value into the buffer. Abort if there's not
665 * enough room.
666 */
667 if (buflen < KPRINTF_BUFSIZE)
668 return (buf);
669
670 ch = *p++;
671 base = ch != '\177' ? ch : *p++;
672 sbase = base == 8 ? "%qo" : base == 10 ? "%qd" : base == 16 ? "%qx" : 0;
673 if (sbase == 0)
674 return (buf); /* punt if not oct, dec, or hex */
675
676 sprintf(snbuf, sbase, val);
677 for (q = snbuf ; *q ; q++) {
678 *bp++ = *q;
679 left--;
680 }
681
682 /*
683 * If the value we printed was 0, or if we don't have room for
684 * "<x>", we're done.
685 */
686 if (val == 0 || left < 3)
687 return (buf);
688
689 #define PUTBYTE(b, c, l) \
690 *(b)++ = (c); \
691 if (--(l) == 0) \
692 goto out;
693 #define PUTSTR(b, p, l) do { \
694 int c; \
695 while ((c = *(p)++) != 0) { \
696 *(b)++ = c; \
697 if (--(l) == 0) \
698 goto out; \
699 } \
700 } while (0)
701
702 /*
703 * Chris Torek's new style %b format is identified by a leading \177
704 */
705 sep = '<';
706 if (ch != '\177') {
707 /* old (standard) %b format. */
708 for (;(bit = *p++) != 0;) {
709 if (val & (1 << (bit - 1))) {
710 PUTBYTE(bp, sep, left);
711 for (; (ch = *p) > ' '; ++p) {
712 PUTBYTE(bp, ch, left);
713 }
714 sep = ',';
715 } else
716 for (; *p > ' '; ++p)
717 continue;
718 }
719 } else {
720 /* new quad-capable %b format; also does fields. */
721 field = val;
722 while ((ch = *p++) != '\0') {
723 bit = *p++; /* now 0-origin */
724 switch (ch) {
725 case 'b':
726 if (((u_int)(val >> bit) & 1) == 0)
727 goto skip;
728 PUTBYTE(bp, sep, left);
729 PUTSTR(bp, p, left);
730 sep = ',';
731 break;
732 case 'f':
733 case 'F':
734 len = *p++; /* field length */
735 field = (val >> bit) & ((1ULL << len) - 1);
736 if (ch == 'F') /* just extract */
737 break;
738 PUTBYTE(bp, sep, left);
739 sep = ',';
740 PUTSTR(bp, p, left);
741 PUTBYTE(bp, '=', left);
742 sprintf(snbuf, sbase, field);
743 q = snbuf; PUTSTR(bp, q, left);
744 break;
745 case '=':
746 case ':':
747 /*
748 * Here "bit" is actually a value instead,
749 * to be compared against the last field.
750 * This only works for values in [0..255],
751 * of course.
752 */
753 if ((int)field != bit)
754 goto skip;
755 if (ch == '=')
756 PUTBYTE(bp, '=', left);
757 PUTSTR(bp, p, left);
758 break;
759 default:
760 skip:
761 while (*p++ != '\0')
762 continue;
763 break;
764 }
765 }
766 }
767 if (sep != '<')
768 PUTBYTE(bp, '>', left);
769
770 out:
771 return (buf);
772
773 #undef PUTBYTE
774 #undef PUTSTR
775 }
776
777 /*
778 * kprintf: scaled down version of printf(3).
779 *
780 * this version based on vfprintf() from libc which was derived from
781 * software contributed to Berkeley by Chris Torek.
782 *
783 * Two additional formats:
784 *
785 * The format %b is supported to decode error registers.
786 * Its usage is:
787 *
788 * printf("reg=%b\n", regval, "<base><arg>*");
789 *
790 * where <base> is the output base expressed as a control character, e.g.
791 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
792 * the first of which gives the bit number to be inspected (origin 1), and
793 * the next characters (up to a control character, i.e. a character <= 32),
794 * give the name of the register. Thus:
795 *
796 * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
797 *
798 * would produce output:
799 *
800 * reg=3<BITTWO,BITONE>
801 *
802 * The format %: passes an additional format string and argument list
803 * recursively. Its usage is:
804 *
805 * fn(char *fmt, ...)
806 * {
807 * va_list ap;
808 * va_start(ap, fmt);
809 * printf("prefix: %: suffix\n", fmt, ap);
810 * va_end(ap);
811 * }
812 *
813 * this is the actual printf innards
814 *
815 * This code is large and complicated...
816 *
817 * NOTE: The kprintf mutex must be held of we're going TOBUF or TOCONS!
818 */
819
820 /*
821 * macros for converting digits to letters and vice versa
822 */
823 #define to_digit(c) ((c) - '0')
824 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
825 #define to_char(n) ((n) + '0')
826
827 /*
828 * flags used during conversion.
829 */
830 #define ALT 0x001 /* alternate form */
831 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
832 #define LADJUST 0x004 /* left adjustment */
833 #define LONGDBL 0x008 /* long double; unimplemented */
834 #define LONGINT 0x010 /* long integer */
835 #define QUADINT 0x020 /* quad integer */
836 #define SHORTINT 0x040 /* short integer */
837 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
838 #define FPT 0x100 /* Floating point number */
839
840 /*
841 * To extend shorts properly, we need both signed and unsigned
842 * argument extraction methods.
843 */
844 #define SARG() \
845 (flags&QUADINT ? va_arg(ap, quad_t) : \
846 flags&LONGINT ? va_arg(ap, long) : \
847 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
848 (long)va_arg(ap, int))
849 #define UARG() \
850 (flags&QUADINT ? va_arg(ap, u_quad_t) : \
851 flags&LONGINT ? va_arg(ap, u_long) : \
852 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
853 (u_long)va_arg(ap, u_int))
854
855 #define KPRINTF_PUTCHAR(C) \
856 (oflags == TOBUFONLY) ? *sbuf++ = (C) : putchar((C), oflags, tp);
857
858 /*
859 * Guts of kernel printf. Note, we already expect to be in a mutex!
860 */
861 static int
862 kprintf(fmt0, oflags, tp, sbuf, ap)
863 const char *fmt0;
864 int oflags;
865 struct tty *tp;
866 char *sbuf;
867 va_list ap;
868 {
869 char *fmt; /* format string */
870 int ch; /* character from fmt */
871 int n; /* handy integer (short term usage) */
872 char *cp; /* handy char pointer (short term usage) */
873 int flags; /* flags as above */
874 int ret; /* return value accumulator */
875 int width; /* width from format (%8d), or 0 */
876 int prec; /* precision from format (%.3d), or -1 */
877 char sign; /* sign prefix (' ', '+', '-', or \0) */
878
879 u_quad_t _uquad; /* integer arguments %[diouxX] */
880 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
881 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
882 int realsz; /* field size expanded by dprec */
883 int size; /* size of converted field or string */
884 char *xdigs; /* digits for [xX] conversion */
885 char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
886
887 cp = NULL; /* XXX: shutup gcc */
888 size = 0; /* XXX: shutup gcc */
889
890 fmt = (char *)fmt0;
891 ret = 0;
892
893 xdigs = NULL; /* XXX: shut up gcc warning */
894
895 /*
896 * Scan the format for conversions (`%' character).
897 */
898 for (;;) {
899 while (*fmt != '%' && *fmt) {
900 KPRINTF_PUTCHAR(*fmt++);
901 ret++;
902 }
903 if (*fmt == 0)
904 goto done;
905
906 fmt++; /* skip over '%' */
907
908 flags = 0;
909 dprec = 0;
910 width = 0;
911 prec = -1;
912 sign = '\0';
913
914 rflag: ch = *fmt++;
915 reswitch: switch (ch) {
916 /* XXX: non-standard '%:' format */
917 #ifndef __powerpc__
918 case ':':
919 if (oflags != TOBUFONLY) {
920 cp = va_arg(ap, char *);
921 kprintf(cp, oflags, tp,
922 NULL, va_arg(ap, va_list));
923 }
924 continue; /* no output */
925 #endif
926 /* XXX: non-standard '%b' format */
927 case 'b': {
928 char *b, *z;
929 int tmp;
930 _uquad = va_arg(ap, int);
931 b = va_arg(ap, char *);
932 if (*b == 8)
933 sprintf(buf, "%qo", _uquad);
934 else if (*b == 10)
935 sprintf(buf, "%qd", _uquad);
936 else if (*b == 16)
937 sprintf(buf, "%qx", _uquad);
938 else
939 break;
940 b++;
941
942 z = buf;
943 while (*z) {
944 KPRINTF_PUTCHAR(*z++);
945 ret++;
946 }
947
948 if (_uquad) {
949 tmp = 0;
950 while ((n = *b++) != 0) {
951 if (_uquad & (1 << (n - 1))) {
952 KPRINTF_PUTCHAR(tmp ? ',':'<');
953 ret++;
954 while ((n = *b) > ' ') {
955 KPRINTF_PUTCHAR(n);
956 ret++;
957 b++;
958 }
959 tmp = 1;
960 } else {
961 while(*b > ' ')
962 b++;
963 }
964 }
965 if (tmp) {
966 KPRINTF_PUTCHAR('>');
967 ret++;
968 }
969 }
970 continue; /* no output */
971 }
972
973 #ifdef DDB
974 /* XXX: non-standard '%r' format (print int in db_radix) */
975 case 'r':
976 if ((oflags & TODDB) == 0)
977 goto default_case;
978
979 if (db_radix == 16)
980 goto case_z; /* signed hex */
981 _uquad = SARG();
982 if ((quad_t)_uquad < 0) {
983 _uquad = -_uquad;
984 sign = '-';
985 }
986 base = (db_radix == 8) ? OCT : DEC;
987 goto number;
988
989
990 /* XXX: non-standard '%z' format ("signed hex", a "hex %i")*/
991 case 'z':
992 case_z:
993 if ((oflags & TODDB) == 0)
994 goto default_case;
995
996 xdigs = "0123456789abcdef";
997 ch = 'x'; /* the 'x' in '0x' (below) */
998 _uquad = SARG();
999 base = HEX;
1000 /* leading 0x/X only if non-zero */
1001 if (flags & ALT && _uquad != 0)
1002 flags |= HEXPREFIX;
1003 if ((quad_t)_uquad < 0) {
1004 _uquad = -_uquad;
1005 sign = '-';
1006 }
1007 goto number;
1008 #endif
1009
1010 case ' ':
1011 /*
1012 * ``If the space and + flags both appear, the space
1013 * flag will be ignored.''
1014 * -- ANSI X3J11
1015 */
1016 if (!sign)
1017 sign = ' ';
1018 goto rflag;
1019 case '#':
1020 flags |= ALT;
1021 goto rflag;
1022 case '*':
1023 /*
1024 * ``A negative field width argument is taken as a
1025 * - flag followed by a positive field width.''
1026 * -- ANSI X3J11
1027 * They don't exclude field widths read from args.
1028 */
1029 if ((width = va_arg(ap, int)) >= 0)
1030 goto rflag;
1031 width = -width;
1032 /* FALLTHROUGH */
1033 case '-':
1034 flags |= LADJUST;
1035 goto rflag;
1036 case '+':
1037 sign = '+';
1038 goto rflag;
1039 case '.':
1040 if ((ch = *fmt++) == '*') {
1041 n = va_arg(ap, int);
1042 prec = n < 0 ? -1 : n;
1043 goto rflag;
1044 }
1045 n = 0;
1046 while (is_digit(ch)) {
1047 n = 10 * n + to_digit(ch);
1048 ch = *fmt++;
1049 }
1050 prec = n < 0 ? -1 : n;
1051 goto reswitch;
1052 case '0':
1053 /*
1054 * ``Note that 0 is taken as a flag, not as the
1055 * beginning of a field width.''
1056 * -- ANSI X3J11
1057 */
1058 flags |= ZEROPAD;
1059 goto rflag;
1060 case '1': case '2': case '3': case '4':
1061 case '5': case '6': case '7': case '8': case '9':
1062 n = 0;
1063 do {
1064 n = 10 * n + to_digit(ch);
1065 ch = *fmt++;
1066 } while (is_digit(ch));
1067 width = n;
1068 goto reswitch;
1069 case 'h':
1070 flags |= SHORTINT;
1071 goto rflag;
1072 case 'l':
1073 if (*fmt == 'l') {
1074 fmt++;
1075 flags |= QUADINT;
1076 } else {
1077 flags |= LONGINT;
1078 }
1079 goto rflag;
1080 case 'q':
1081 flags |= QUADINT;
1082 goto rflag;
1083 case 'c':
1084 *(cp = buf) = va_arg(ap, int);
1085 size = 1;
1086 sign = '\0';
1087 break;
1088 case 'D':
1089 flags |= LONGINT;
1090 /*FALLTHROUGH*/
1091 case 'd':
1092 case 'i':
1093 _uquad = SARG();
1094 if ((quad_t)_uquad < 0) {
1095 _uquad = -_uquad;
1096 sign = '-';
1097 }
1098 base = DEC;
1099 goto number;
1100 case 'n':
1101 #ifdef DDB
1102 /* XXX: non-standard '%n' format */
1103 /*
1104 * XXX: HACK! DDB wants '%n' to be a '%u' printed
1105 * in db_radix format. this should die since '%n'
1106 * is already defined in standard printf to write
1107 * the number of chars printed so far to the arg (which
1108 * should be a pointer.
1109 */
1110 if (oflags & TODDB) {
1111 if (db_radix == 16)
1112 ch = 'x'; /* convert to %x */
1113 else if (db_radix == 8)
1114 ch = 'o'; /* convert to %o */
1115 else
1116 ch = 'u'; /* convert to %u */
1117
1118 /* ... and start again */
1119 goto reswitch;
1120 }
1121
1122 #endif
1123 if (flags & QUADINT)
1124 *va_arg(ap, quad_t *) = ret;
1125 else if (flags & LONGINT)
1126 *va_arg(ap, long *) = ret;
1127 else if (flags & SHORTINT)
1128 *va_arg(ap, short *) = ret;
1129 else
1130 *va_arg(ap, int *) = ret;
1131 continue; /* no output */
1132 case 'O':
1133 flags |= LONGINT;
1134 /*FALLTHROUGH*/
1135 case 'o':
1136 _uquad = UARG();
1137 base = OCT;
1138 goto nosign;
1139 case 'p':
1140 /*
1141 * ``The argument shall be a pointer to void. The
1142 * value of the pointer is converted to a sequence
1143 * of printable characters, in an implementation-
1144 * defined manner.''
1145 * -- ANSI X3J11
1146 */
1147 /* NOSTRICT */
1148 _uquad = (u_long)va_arg(ap, void *);
1149 base = HEX;
1150 xdigs = "0123456789abcdef";
1151 flags |= HEXPREFIX;
1152 ch = 'x';
1153 goto nosign;
1154 case 's':
1155 if ((cp = va_arg(ap, char *)) == NULL)
1156 cp = "(null)";
1157 if (prec >= 0) {
1158 /*
1159 * can't use strlen; can only look for the
1160 * NUL in the first `prec' characters, and
1161 * strlen() will go further.
1162 */
1163 char *p = memchr(cp, 0, prec);
1164
1165 if (p != NULL) {
1166 size = p - cp;
1167 if (size > prec)
1168 size = prec;
1169 } else
1170 size = prec;
1171 } else
1172 size = strlen(cp);
1173 sign = '\0';
1174 break;
1175 case 'U':
1176 flags |= LONGINT;
1177 /*FALLTHROUGH*/
1178 case 'u':
1179 _uquad = UARG();
1180 base = DEC;
1181 goto nosign;
1182 case 'X':
1183 xdigs = "0123456789ABCDEF";
1184 goto hex;
1185 case 'x':
1186 xdigs = "0123456789abcdef";
1187 hex: _uquad = UARG();
1188 base = HEX;
1189 /* leading 0x/X only if non-zero */
1190 if (flags & ALT && _uquad != 0)
1191 flags |= HEXPREFIX;
1192
1193 /* unsigned conversions */
1194 nosign: sign = '\0';
1195 /*
1196 * ``... diouXx conversions ... if a precision is
1197 * specified, the 0 flag will be ignored.''
1198 * -- ANSI X3J11
1199 */
1200 number: if ((dprec = prec) >= 0)
1201 flags &= ~ZEROPAD;
1202
1203 /*
1204 * ``The result of converting a zero value with an
1205 * explicit precision of zero is no characters.''
1206 * -- ANSI X3J11
1207 */
1208 cp = buf + KPRINTF_BUFSIZE;
1209 if (_uquad != 0 || prec != 0) {
1210 /*
1211 * Unsigned mod is hard, and unsigned mod
1212 * by a constant is easier than that by
1213 * a variable; hence this switch.
1214 */
1215 switch (base) {
1216 case OCT:
1217 do {
1218 *--cp = to_char(_uquad & 7);
1219 _uquad >>= 3;
1220 } while (_uquad);
1221 /* handle octal leading 0 */
1222 if (flags & ALT && *cp != '0')
1223 *--cp = '0';
1224 break;
1225
1226 case DEC:
1227 /* many numbers are 1 digit */
1228 while (_uquad >= 10) {
1229 *--cp = to_char(_uquad % 10);
1230 _uquad /= 10;
1231 }
1232 *--cp = to_char(_uquad);
1233 break;
1234
1235 case HEX:
1236 do {
1237 *--cp = xdigs[_uquad & 15];
1238 _uquad >>= 4;
1239 } while (_uquad);
1240 break;
1241
1242 default:
1243 cp = "bug in kprintf: bad base";
1244 size = strlen(cp);
1245 goto skipsize;
1246 }
1247 }
1248 size = buf + KPRINTF_BUFSIZE - cp;
1249 skipsize:
1250 break;
1251 default: /* "%?" prints ?, unless ? is NUL */
1252 #ifdef DDB
1253 default_case: /* DDB */
1254 #endif
1255 if (ch == '\0')
1256 goto done;
1257 /* pretend it was %c with argument ch */
1258 cp = buf;
1259 *cp = ch;
1260 size = 1;
1261 sign = '\0';
1262 break;
1263 }
1264
1265 /*
1266 * All reasonable formats wind up here. At this point, `cp'
1267 * points to a string which (if not flags&LADJUST) should be
1268 * padded out to `width' places. If flags&ZEROPAD, it should
1269 * first be prefixed by any sign or other prefix; otherwise,
1270 * it should be blank padded before the prefix is emitted.
1271 * After any left-hand padding and prefixing, emit zeroes
1272 * required by a decimal [diouxX] precision, then print the
1273 * string proper, then emit zeroes required by any leftover
1274 * floating precision; finally, if LADJUST, pad with blanks.
1275 *
1276 * Compute actual size, so we know how much to pad.
1277 * size excludes decimal prec; realsz includes it.
1278 */
1279 realsz = dprec > size ? dprec : size;
1280 if (sign)
1281 realsz++;
1282 else if (flags & HEXPREFIX)
1283 realsz+= 2;
1284
1285 /* right-adjusting blank padding */
1286 if ((flags & (LADJUST|ZEROPAD)) == 0) {
1287 n = width - realsz;
1288 while (n-- > 0)
1289 KPRINTF_PUTCHAR(' ');
1290 }
1291
1292 /* prefix */
1293 if (sign) {
1294 KPRINTF_PUTCHAR(sign);
1295 } else if (flags & HEXPREFIX) {
1296 KPRINTF_PUTCHAR('0');
1297 KPRINTF_PUTCHAR(ch);
1298 }
1299
1300 /* right-adjusting zero padding */
1301 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1302 n = width - realsz;
1303 while (n-- > 0)
1304 KPRINTF_PUTCHAR('0');
1305 }
1306
1307 /* leading zeroes from decimal precision */
1308 n = dprec - size;
1309 while (n-- > 0)
1310 KPRINTF_PUTCHAR('0');
1311
1312 /* the string or number proper */
1313 while (size--)
1314 KPRINTF_PUTCHAR(*cp++);
1315 /* left-adjusting padding (always blank) */
1316 if (flags & LADJUST) {
1317 n = width - realsz;
1318 while (n-- > 0)
1319 KPRINTF_PUTCHAR(' ');
1320 }
1321
1322 /* finally, adjust ret */
1323 ret += width > realsz ? width : realsz;
1324
1325 }
1326 done:
1327 return (ret);
1328 /* NOTREACHED */
1329 }
1330