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