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