subr_prf.c revision 1.122 1 /* $NetBSD: subr_prf.c,v 1.122 2008/05/19 17:06:02 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1986, 1988, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: subr_prf.c,v 1.122 2008/05/19 17:06:02 ad 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 /*
884 * printf_nolog: Like printf(), but does not send message to the log.
885 */
886
887 void
888 printf_nolog(const char *fmt, ...)
889 {
890 va_list ap;
891 int s;
892
893 KPRINTF_MUTEX_ENTER(s);
894
895 va_start(ap, fmt);
896 kprintf(fmt, TOCONS, NULL, NULL, ap);
897 va_end(ap);
898
899 KPRINTF_MUTEX_EXIT(s);
900 }
901
902 /*
903 * normal kernel printf functions: printf, vprintf, snprintf, vsnprintf
904 */
905
906 /*
907 * printf: print a message to the console and the log
908 */
909 void
910 printf(const char *fmt, ...)
911 {
912 va_list ap;
913 int s;
914
915 KPRINTF_MUTEX_ENTER(s);
916
917 va_start(ap, fmt);
918 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
919 va_end(ap);
920
921 KPRINTF_MUTEX_EXIT(s);
922
923 if (!panicstr)
924 logwakeup();
925 }
926
927 /*
928 * vprintf: print a message to the console and the log [already have
929 * va_alist]
930 */
931
932 void
933 vprintf(const char *fmt, va_list ap)
934 {
935 int s;
936
937 KPRINTF_MUTEX_ENTER(s);
938
939 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
940
941 KPRINTF_MUTEX_EXIT(s);
942
943 if (!panicstr)
944 logwakeup();
945 }
946
947 /*
948 * sprintf: print a message to a buffer
949 */
950 int
951 sprintf(char *bf, const char *fmt, ...)
952 {
953 int retval;
954 va_list ap;
955
956 va_start(ap, fmt);
957 retval = kprintf(fmt, TOBUFONLY, NULL, bf, ap);
958 va_end(ap);
959 *(bf + retval) = 0; /* null terminate */
960 return(retval);
961 }
962
963 /*
964 * vsprintf: print a message to a buffer [already have va_alist]
965 */
966
967 int
968 vsprintf(char *bf, const char *fmt, va_list ap)
969 {
970 int retval;
971
972 retval = kprintf(fmt, TOBUFONLY, NULL, bf, ap);
973 *(bf + retval) = 0; /* null terminate */
974 return (retval);
975 }
976
977 /*
978 * snprintf: print a message to a buffer
979 */
980 int
981 snprintf(char *bf, size_t size, const char *fmt, ...)
982 {
983 int retval;
984 va_list ap;
985 char *p;
986
987 if (size < 1)
988 return (-1);
989 p = bf + size - 1;
990 va_start(ap, fmt);
991 retval = kprintf(fmt, TOBUFONLY, &p, bf, ap);
992 va_end(ap);
993 *(p) = 0; /* null terminate */
994 return(retval);
995 }
996
997 /*
998 * vsnprintf: print a message to a buffer [already have va_alist]
999 */
1000 int
1001 vsnprintf(char *bf, size_t size, const char *fmt, va_list ap)
1002 {
1003 int retval;
1004 char *p;
1005
1006 if (size < 1)
1007 return (-1);
1008 p = bf + size - 1;
1009 retval = kprintf(fmt, TOBUFONLY, &p, bf, ap);
1010 *(p) = 0; /* null terminate */
1011 return(retval);
1012 }
1013
1014 /*
1015 * kprintf: scaled down version of printf(3).
1016 *
1017 * this version based on vfprintf() from libc which was derived from
1018 * software contributed to Berkeley by Chris Torek.
1019 *
1020 * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS!
1021 */
1022
1023 /*
1024 * macros for converting digits to letters and vice versa
1025 */
1026 #define to_digit(c) ((c) - '0')
1027 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
1028 #define to_char(n) ((n) + '0')
1029
1030 /*
1031 * flags used during conversion.
1032 */
1033 #define ALT 0x001 /* alternate form */
1034 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
1035 #define LADJUST 0x004 /* left adjustment */
1036 #define LONGDBL 0x008 /* long double; unimplemented */
1037 #define LONGINT 0x010 /* long integer */
1038 #define QUADINT 0x020 /* quad integer */
1039 #define SHORTINT 0x040 /* short integer */
1040 #define MAXINT 0x080 /* intmax_t */
1041 #define PTRINT 0x100 /* intptr_t */
1042 #define SIZEINT 0x200 /* size_t */
1043 #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */
1044 #define FPT 0x800 /* Floating point number */
1045
1046 /*
1047 * To extend shorts properly, we need both signed and unsigned
1048 * argument extraction methods.
1049 */
1050 #define SARG() \
1051 (flags&MAXINT ? va_arg(ap, intmax_t) : \
1052 flags&PTRINT ? va_arg(ap, intptr_t) : \
1053 flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
1054 flags&QUADINT ? va_arg(ap, quad_t) : \
1055 flags&LONGINT ? va_arg(ap, long) : \
1056 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
1057 (long)va_arg(ap, int))
1058 #define UARG() \
1059 (flags&MAXINT ? va_arg(ap, uintmax_t) : \
1060 flags&PTRINT ? va_arg(ap, uintptr_t) : \
1061 flags&SIZEINT ? va_arg(ap, size_t) : \
1062 flags&QUADINT ? va_arg(ap, u_quad_t) : \
1063 flags&LONGINT ? va_arg(ap, u_long) : \
1064 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
1065 (u_long)va_arg(ap, u_int))
1066
1067 #define KPRINTF_PUTCHAR(C) { \
1068 if (oflags == TOBUFONLY) { \
1069 if ((vp != NULL) && (sbuf == tailp)) { \
1070 ret += 1; /* indicate error */ \
1071 goto overflow; \
1072 } \
1073 *sbuf++ = (C); \
1074 } else { \
1075 putchar((C), oflags, (struct tty *)vp); \
1076 } \
1077 }
1078
1079 /*
1080 * Guts of kernel printf. Note, we already expect to be in a mutex!
1081 */
1082 int
1083 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap)
1084 {
1085 const char *fmt; /* format string */
1086 int ch; /* character from fmt */
1087 int n; /* handy integer (short term usage) */
1088 char *cp; /* handy char pointer (short term usage) */
1089 int flags; /* flags as above */
1090 int ret; /* return value accumulator */
1091 int width; /* width from format (%8d), or 0 */
1092 int prec; /* precision from format (%.3d), or -1 */
1093 char sign; /* sign prefix (' ', '+', '-', or \0) */
1094
1095 u_quad_t _uquad; /* integer arguments %[diouxX] */
1096 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
1097 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
1098 int realsz; /* field size expanded by dprec */
1099 int size; /* size of converted field or string */
1100 const char *xdigs; /* digits for [xX] conversion */
1101 char bf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
1102 char *tailp; /* tail pointer for snprintf */
1103
1104 tailp = NULL; /* XXX: shutup gcc */
1105 if (oflags == TOBUFONLY && (vp != NULL))
1106 tailp = *(char **)vp;
1107
1108 cp = NULL; /* XXX: shutup gcc */
1109 size = 0; /* XXX: shutup gcc */
1110
1111 fmt = fmt0;
1112 ret = 0;
1113
1114 xdigs = NULL; /* XXX: shut up gcc warning */
1115
1116 /*
1117 * Scan the format for conversions (`%' character).
1118 */
1119 for (;;) {
1120 while (*fmt != '%' && *fmt) {
1121 ret++;
1122 KPRINTF_PUTCHAR(*fmt++);
1123 }
1124 if (*fmt == 0)
1125 goto done;
1126
1127 fmt++; /* skip over '%' */
1128
1129 flags = 0;
1130 dprec = 0;
1131 width = 0;
1132 prec = -1;
1133 sign = '\0';
1134
1135 rflag: ch = *fmt++;
1136 reswitch: switch (ch) {
1137 case ' ':
1138 /*
1139 * ``If the space and + flags both appear, the space
1140 * flag will be ignored.''
1141 * -- ANSI X3J11
1142 */
1143 if (!sign)
1144 sign = ' ';
1145 goto rflag;
1146 case '#':
1147 flags |= ALT;
1148 goto rflag;
1149 case '*':
1150 /*
1151 * ``A negative field width argument is taken as a
1152 * - flag followed by a positive field width.''
1153 * -- ANSI X3J11
1154 * They don't exclude field widths read from args.
1155 */
1156 if ((width = va_arg(ap, int)) >= 0)
1157 goto rflag;
1158 width = -width;
1159 /* FALLTHROUGH */
1160 case '-':
1161 flags |= LADJUST;
1162 goto rflag;
1163 case '+':
1164 sign = '+';
1165 goto rflag;
1166 case '.':
1167 if ((ch = *fmt++) == '*') {
1168 n = va_arg(ap, int);
1169 prec = n < 0 ? -1 : n;
1170 goto rflag;
1171 }
1172 n = 0;
1173 while (is_digit(ch)) {
1174 n = 10 * n + to_digit(ch);
1175 ch = *fmt++;
1176 }
1177 prec = n < 0 ? -1 : n;
1178 goto reswitch;
1179 case '0':
1180 /*
1181 * ``Note that 0 is taken as a flag, not as the
1182 * beginning of a field width.''
1183 * -- ANSI X3J11
1184 */
1185 flags |= ZEROPAD;
1186 goto rflag;
1187 case '1': case '2': case '3': case '4':
1188 case '5': case '6': case '7': case '8': case '9':
1189 n = 0;
1190 do {
1191 n = 10 * n + to_digit(ch);
1192 ch = *fmt++;
1193 } while (is_digit(ch));
1194 width = n;
1195 goto reswitch;
1196 case 'h':
1197 flags |= SHORTINT;
1198 goto rflag;
1199 case 'j':
1200 flags |= MAXINT;
1201 goto rflag;
1202 case 'l':
1203 if (*fmt == 'l') {
1204 fmt++;
1205 flags |= QUADINT;
1206 } else {
1207 flags |= LONGINT;
1208 }
1209 goto rflag;
1210 case 'q':
1211 flags |= QUADINT;
1212 goto rflag;
1213 case 't':
1214 flags |= PTRINT;
1215 goto rflag;
1216 case 'z':
1217 flags |= SIZEINT;
1218 goto rflag;
1219 case 'c':
1220 *(cp = bf) = va_arg(ap, int);
1221 size = 1;
1222 sign = '\0';
1223 break;
1224 case 'D':
1225 flags |= LONGINT;
1226 /*FALLTHROUGH*/
1227 case 'd':
1228 case 'i':
1229 _uquad = SARG();
1230 if ((quad_t)_uquad < 0) {
1231 _uquad = -_uquad;
1232 sign = '-';
1233 }
1234 base = DEC;
1235 goto number;
1236 case 'n':
1237 if (flags & MAXINT)
1238 *va_arg(ap, intmax_t *) = ret;
1239 else if (flags & PTRINT)
1240 *va_arg(ap, intptr_t *) = ret;
1241 else if (flags & SIZEINT)
1242 *va_arg(ap, ssize_t *) = ret;
1243 else if (flags & QUADINT)
1244 *va_arg(ap, quad_t *) = ret;
1245 else if (flags & LONGINT)
1246 *va_arg(ap, long *) = ret;
1247 else if (flags & SHORTINT)
1248 *va_arg(ap, short *) = ret;
1249 else
1250 *va_arg(ap, int *) = ret;
1251 continue; /* no output */
1252 case 'O':
1253 flags |= LONGINT;
1254 /*FALLTHROUGH*/
1255 case 'o':
1256 _uquad = UARG();
1257 base = OCT;
1258 goto nosign;
1259 case 'p':
1260 /*
1261 * ``The argument shall be a pointer to void. The
1262 * value of the pointer is converted to a sequence
1263 * of printable characters, in an implementation-
1264 * defined manner.''
1265 * -- ANSI X3J11
1266 */
1267 /* NOSTRICT */
1268 _uquad = (u_long)va_arg(ap, void *);
1269 base = HEX;
1270 xdigs = hexdigits;
1271 flags |= HEXPREFIX;
1272 ch = 'x';
1273 goto nosign;
1274 case 's':
1275 if ((cp = va_arg(ap, char *)) == NULL)
1276 /*XXXUNCONST*/
1277 cp = __UNCONST("(null)");
1278 if (prec >= 0) {
1279 /*
1280 * can't use strlen; can only look for the
1281 * NUL in the first `prec' characters, and
1282 * strlen() will go further.
1283 */
1284 char *p = memchr(cp, 0, prec);
1285
1286 if (p != NULL) {
1287 size = p - cp;
1288 if (size > prec)
1289 size = prec;
1290 } else
1291 size = prec;
1292 } else
1293 size = strlen(cp);
1294 sign = '\0';
1295 break;
1296 case 'U':
1297 flags |= LONGINT;
1298 /*FALLTHROUGH*/
1299 case 'u':
1300 _uquad = UARG();
1301 base = DEC;
1302 goto nosign;
1303 case 'X':
1304 xdigs = HEXDIGITS;
1305 goto hex;
1306 case 'x':
1307 xdigs = hexdigits;
1308 hex: _uquad = UARG();
1309 base = HEX;
1310 /* leading 0x/X only if non-zero */
1311 if (flags & ALT && _uquad != 0)
1312 flags |= HEXPREFIX;
1313
1314 /* unsigned conversions */
1315 nosign: sign = '\0';
1316 /*
1317 * ``... diouXx conversions ... if a precision is
1318 * specified, the 0 flag will be ignored.''
1319 * -- ANSI X3J11
1320 */
1321 number: if ((dprec = prec) >= 0)
1322 flags &= ~ZEROPAD;
1323
1324 /*
1325 * ``The result of converting a zero value with an
1326 * explicit precision of zero is no characters.''
1327 * -- ANSI X3J11
1328 */
1329 cp = bf + KPRINTF_BUFSIZE;
1330 if (_uquad != 0 || prec != 0) {
1331 /*
1332 * Unsigned mod is hard, and unsigned mod
1333 * by a constant is easier than that by
1334 * a variable; hence this switch.
1335 */
1336 switch (base) {
1337 case OCT:
1338 do {
1339 *--cp = to_char(_uquad & 7);
1340 _uquad >>= 3;
1341 } while (_uquad);
1342 /* handle octal leading 0 */
1343 if (flags & ALT && *cp != '0')
1344 *--cp = '0';
1345 break;
1346
1347 case DEC:
1348 /* many numbers are 1 digit */
1349 while (_uquad >= 10) {
1350 *--cp = to_char(_uquad % 10);
1351 _uquad /= 10;
1352 }
1353 *--cp = to_char(_uquad);
1354 break;
1355
1356 case HEX:
1357 do {
1358 *--cp = xdigs[_uquad & 15];
1359 _uquad >>= 4;
1360 } while (_uquad);
1361 break;
1362
1363 default:
1364 /*XXXUNCONST*/
1365 cp = __UNCONST("bug in kprintf: bad base");
1366 size = strlen(cp);
1367 goto skipsize;
1368 }
1369 }
1370 size = bf + KPRINTF_BUFSIZE - cp;
1371 skipsize:
1372 break;
1373 default: /* "%?" prints ?, unless ? is NUL */
1374 if (ch == '\0')
1375 goto done;
1376 /* pretend it was %c with argument ch */
1377 cp = bf;
1378 *cp = ch;
1379 size = 1;
1380 sign = '\0';
1381 break;
1382 }
1383
1384 /*
1385 * All reasonable formats wind up here. At this point, `cp'
1386 * points to a string which (if not flags&LADJUST) should be
1387 * padded out to `width' places. If flags&ZEROPAD, it should
1388 * first be prefixed by any sign or other prefix; otherwise,
1389 * it should be blank padded before the prefix is emitted.
1390 * After any left-hand padding and prefixing, emit zeroes
1391 * required by a decimal [diouxX] precision, then print the
1392 * string proper, then emit zeroes required by any leftover
1393 * floating precision; finally, if LADJUST, pad with blanks.
1394 *
1395 * Compute actual size, so we know how much to pad.
1396 * size excludes decimal prec; realsz includes it.
1397 */
1398 realsz = dprec > size ? dprec : size;
1399 if (sign)
1400 realsz++;
1401 else if (flags & HEXPREFIX)
1402 realsz+= 2;
1403
1404 /* adjust ret */
1405 ret += width > realsz ? width : realsz;
1406
1407 /* right-adjusting blank padding */
1408 if ((flags & (LADJUST|ZEROPAD)) == 0) {
1409 n = width - realsz;
1410 while (n-- > 0)
1411 KPRINTF_PUTCHAR(' ');
1412 }
1413
1414 /* prefix */
1415 if (sign) {
1416 KPRINTF_PUTCHAR(sign);
1417 } else if (flags & HEXPREFIX) {
1418 KPRINTF_PUTCHAR('0');
1419 KPRINTF_PUTCHAR(ch);
1420 }
1421
1422 /* right-adjusting zero padding */
1423 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1424 n = width - realsz;
1425 while (n-- > 0)
1426 KPRINTF_PUTCHAR('0');
1427 }
1428
1429 /* leading zeroes from decimal precision */
1430 n = dprec - size;
1431 while (n-- > 0)
1432 KPRINTF_PUTCHAR('0');
1433
1434 /* the string or number proper */
1435 while (size--)
1436 KPRINTF_PUTCHAR(*cp++);
1437 /* left-adjusting padding (always blank) */
1438 if (flags & LADJUST) {
1439 n = width - realsz;
1440 while (n-- > 0)
1441 KPRINTF_PUTCHAR(' ');
1442 }
1443 }
1444
1445 done:
1446 if ((oflags == TOBUFONLY) && (vp != NULL))
1447 *(char **)vp = sbuf;
1448 (*v_flush)();
1449 overflow:
1450 return (ret);
1451 /* NOTREACHED */
1452 }
1453