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