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