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