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