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