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