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