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