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