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