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