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