Sync usage with man page.
[netbsd-mini2440.git] / sys / kern / subr_prf.c
blob4fd3c87c9d8373a1c60e73a6cd8dc37679440cb5
1 /* $NetBSD: subr_prf.c,v 1.136 2009/06/28 15:30:30 rmind Exp $ */
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.
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.
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.
36 * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: subr_prf.c,v 1.136 2009/06/28 15:30:30 rmind Exp $");
42 #include "opt_ddb.h"
43 #include "opt_ipkdb.h"
44 #include "opt_kgdb.h"
45 #include "opt_dump.h"
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/spldebug.h>
61 #include <sys/syslog.h>
62 #include <sys/malloc.h>
63 #include <sys/kprintf.h>
64 #include <sys/atomic.h>
65 #include <sys/kernel.h>
66 #include <sys/cpu.h>
68 #include <dev/cons.h>
70 #include <net/if.h>
72 #ifdef DDB
73 #include <ddb/ddbvar.h>
74 #include <machine/db_machdep.h>
75 #include <ddb/db_command.h>
76 #include <ddb/db_interface.h>
77 #endif
79 #ifdef IPKDB
80 #include <ipkdb/ipkdb.h>
81 #endif
83 static kmutex_t kprintf_mtx;
84 static bool kprintf_inited = false;
87 * note that stdarg.h and the ansi style va_start macro is used for both
88 * ansi and traditional c complers.
89 * XXX: this requires that stdarg.h define: va_alist and va_dcl
91 #include <machine/stdarg.h>
94 #ifdef KGDB
95 #include <sys/kgdb.h>
96 #endif
97 #ifdef DDB
98 #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */
99 #endif
103 * defines
108 * local prototypes
111 static void putchar(int, int, struct tty *);
115 * globals
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 struct cpu_info *paniccpu; /* cpu that first paniced */
123 long panicstart, panicend; /* position in the msgbuf of the start and
124 end of the formatted panicstr. */
125 int doing_shutdown; /* set to indicate shutdown in progress */
127 #ifndef DUMP_ON_PANIC
128 #define DUMP_ON_PANIC 1
129 #endif
130 int dumponpanic = DUMP_ON_PANIC;
133 * v_putc: routine to putc on virtual console
135 * the v_putc pointer can be used to redirect the console cnputc elsewhere
136 * [e.g. to a "virtual console"].
139 void (*v_putc)(int) = cnputc; /* start with cnputc (normal cons) */
140 void (*v_flush)(void) = cnflush; /* start with cnflush (normal cons) */
142 const char hexdigits[] = "0123456789abcdef";
143 const char HEXDIGITS[] = "0123456789ABCDEF";
147 * functions
151 * Locking is inited fairly early in MI bootstrap. Before that
152 * prints are done unlocked. But that doesn't really matter,
153 * since nothing can preempt us before interrupts are enabled.
155 void
156 kprintf_init(void)
159 KASSERT(!kprintf_inited && cold); /* not foolproof, but ... */
160 mutex_init(&kprintf_mtx, MUTEX_DEFAULT, IPL_HIGH);
161 kprintf_inited = true;
164 void
165 kprintf_lock(void)
168 if (__predict_true(kprintf_inited))
169 mutex_enter(&kprintf_mtx);
172 void
173 kprintf_unlock(void)
176 if (__predict_true(kprintf_inited)) {
177 /* assert kprintf wasn't somehow inited while we were in */
178 KASSERT(mutex_owned(&kprintf_mtx));
179 mutex_exit(&kprintf_mtx);
184 * twiddle: spin a little propellor on the console.
187 void
188 twiddle(void)
190 static const char twiddle_chars[] = "|/-\\";
191 static int pos;
193 kprintf_lock();
195 putchar(twiddle_chars[pos++ & 3], TOCONS, NULL);
196 putchar('\b', TOCONS, NULL);
198 kprintf_unlock();
202 * panic: handle an unresolvable fatal error
204 * prints "panic: <message>" and reboots. if called twice (i.e. recursive
205 * call) we avoid trying to sync the disk and just reboot (to avoid
206 * recursive panics).
209 void
210 panic(const char *fmt, ...)
212 CPU_INFO_ITERATOR cii;
213 struct cpu_info *ci, *oci;
214 int bootopt;
215 va_list ap;
217 spldebug_stop();
219 if (lwp0.l_cpu && curlwp) {
221 * Disable preemption. If already panicing on another CPU, sit
222 * here and spin until the system is rebooted. Allow the CPU that
223 * first paniced to panic again.
225 kpreempt_disable();
226 ci = curcpu();
227 oci = atomic_cas_ptr((void *)&paniccpu, NULL, ci);
228 if (oci != NULL && oci != ci) {
229 /* Give interrupts a chance to try and prevent deadlock. */
230 for (;;) {
231 #ifndef _RUMPKERNEL /* XXXpooka: temporary build fix, see kern/40505 */
232 DELAY(10);
233 #endif /* _RUMPKERNEL */
238 * Convert the current thread to a bound thread and prevent all
239 * CPUs from scheduling unbound jobs. Do so without taking any
240 * locks.
242 curlwp->l_pflag |= LP_BOUND;
243 for (CPU_INFO_FOREACH(cii, ci)) {
244 ci->ci_schedstate.spc_flags |= SPCF_OFFLINE;
248 bootopt = RB_AUTOBOOT | RB_NOSYNC;
249 if (dumponpanic)
250 bootopt |= RB_DUMP;
251 if (!panicstr)
252 panicstr = fmt;
253 doing_shutdown = 1;
255 if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC)
256 panicstart = msgbufp->msg_bufx;
258 va_start(ap, fmt);
259 printf("panic: ");
260 vprintf(fmt, ap);
261 printf("\n");
262 va_end(ap);
264 if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC)
265 panicend = msgbufp->msg_bufx;
267 #ifdef IPKDB
268 ipkdb_panic();
269 #endif
270 #ifdef KGDB
271 kgdb_panic();
272 #endif
273 #ifdef KADB
274 if (boothowto & RB_KDB)
275 kdbpanic();
276 #endif
277 #ifdef DDB
278 if (db_onpanic == 1)
279 Debugger();
280 else if (db_onpanic >= 0) {
281 static int intrace = 0;
283 if (intrace == 0) {
284 intrace = 1;
285 printf("cpu%u: Begin traceback...\n",
286 cpu_index(curcpu()));
287 db_stack_trace_print(
288 (db_expr_t)(intptr_t)__builtin_frame_address(0),
289 true, 65535, "", printf);
290 printf("cpu%u: End traceback...\n",
291 cpu_index(curcpu()));
292 intrace = 0;
293 } else
294 printf("Faulted in mid-traceback; aborting...");
295 if (db_onpanic == 2)
296 Debugger();
298 #endif
299 cpu_reboot(bootopt, NULL);
303 * kernel logging functions: log, logpri, addlog
307 * log: write to the log buffer
309 * => will not sleep [so safe to call from interrupt]
310 * => will log to console if /dev/klog isn't open
313 void
314 log(int level, const char *fmt, ...)
316 va_list ap;
318 kprintf_lock();
320 klogpri(level); /* log the level first */
321 va_start(ap, fmt);
322 kprintf(fmt, TOLOG, NULL, NULL, ap);
323 va_end(ap);
324 if (!log_open) {
325 va_start(ap, fmt);
326 kprintf(fmt, TOCONS, NULL, NULL, ap);
327 va_end(ap);
330 kprintf_unlock();
332 logwakeup(); /* wake up anyone waiting for log msgs */
336 * vlog: write to the log buffer [already have va_alist]
339 void
340 vlog(int level, const char *fmt, va_list ap)
343 kprintf_lock();
345 klogpri(level); /* log the level first */
346 kprintf(fmt, TOLOG, NULL, NULL, ap);
347 if (!log_open)
348 kprintf(fmt, TOCONS, NULL, NULL, ap);
350 kprintf_unlock();
352 logwakeup(); /* wake up anyone waiting for log msgs */
356 * logpri: log the priority level to the klog
359 void
360 logpri(int level)
363 kprintf_lock();
364 klogpri(level);
365 kprintf_unlock();
369 * Note: we must be in the mutex here!
371 void
372 klogpri(int level)
374 char *p;
375 char snbuf[KPRINTF_BUFSIZE];
377 putchar('<', TOLOG, NULL);
378 snprintf(snbuf, sizeof(snbuf), "%d", level);
379 for (p = snbuf ; *p ; p++)
380 putchar(*p, TOLOG, NULL);
381 putchar('>', TOLOG, NULL);
385 * addlog: add info to previous log message
388 void
389 addlog(const char *fmt, ...)
391 va_list ap;
393 kprintf_lock();
395 va_start(ap, fmt);
396 kprintf(fmt, TOLOG, NULL, NULL, ap);
397 va_end(ap);
398 if (!log_open) {
399 va_start(ap, fmt);
400 kprintf(fmt, TOCONS, NULL, NULL, ap);
401 va_end(ap);
404 kprintf_unlock();
406 logwakeup();
411 * putchar: print a single character on console or user terminal.
413 * => if console, then the last MSGBUFS chars are saved in msgbuf
414 * for inspection later (e.g. dmesg/syslog)
415 * => we must already be in the mutex!
417 static void
418 putchar(int c, int flags, struct tty *tp)
421 if (panicstr)
422 constty = NULL;
423 if ((flags & TOCONS) && tp == NULL && constty) {
424 tp = constty;
425 flags |= TOTTY;
427 if ((flags & TOTTY) && tp &&
428 tputchar(c, flags, tp) < 0 &&
429 (flags & TOCONS) && tp == constty)
430 constty = NULL;
431 if ((flags & TOLOG) &&
432 c != '\0' && c != '\r' && c != 0177)
433 logputchar(c);
434 if ((flags & TOCONS) && constty == NULL && c != '\0')
435 (*v_putc)(c);
436 #ifdef DDB
437 if (flags & TODDB)
438 db_putchar(c);
439 #endif
443 * tablefull: warn that a system table is full
446 void
447 tablefull(const char *tab, const char *hint)
449 if (hint)
450 log(LOG_ERR, "%s: table is full - %s\n", tab, hint);
451 else
452 log(LOG_ERR, "%s: table is full\n", tab);
457 * uprintf: print to the controlling tty of the current process
459 * => we may block if the tty queue is full
460 * => no message is printed if the queue doesn't clear in a reasonable
461 * time
464 void
465 uprintf(const char *fmt, ...)
467 struct proc *p = curproc;
468 va_list ap;
470 /* mutex_enter(proc_lock); XXXSMP */
472 if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) {
473 /* No mutex needed; going to process TTY. */
474 va_start(ap, fmt);
475 kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
476 va_end(ap);
479 /* mutex_exit(proc_lock); XXXSMP */
482 void
483 uprintf_locked(const char *fmt, ...)
485 struct proc *p = curproc;
486 va_list ap;
488 if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) {
489 /* No mutex needed; going to process TTY. */
490 va_start(ap, fmt);
491 kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
492 va_end(ap);
497 * tprintf functions: used to send messages to a specific process
499 * usage:
500 * get a tpr_t handle on a process "p" by using "tprintf_open(p)"
501 * use the handle when calling "tprintf"
502 * when done, do a "tprintf_close" to drop the handle
506 * tprintf_open: get a tprintf handle on a process "p"
508 * => returns NULL if process can't be printed to
511 tpr_t
512 tprintf_open(struct proc *p)
514 tpr_t cookie;
516 cookie = NULL;
518 mutex_enter(proc_lock);
519 if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) {
520 proc_sesshold(p->p_session);
521 cookie = (tpr_t)p->p_session;
523 mutex_exit(proc_lock);
525 return cookie;
529 * tprintf_close: dispose of a tprintf handle obtained with tprintf_open
532 void
533 tprintf_close(tpr_t sess)
536 if (sess) {
537 mutex_enter(proc_lock);
538 /* Releases proc_lock. */
539 proc_sessrele((struct session *)sess);
544 * tprintf: given tprintf handle to a process [obtained with tprintf_open],
545 * send a message to the controlling tty for that process.
547 * => also sends message to /dev/klog
549 void
550 tprintf(tpr_t tpr, const char *fmt, ...)
552 struct session *sess = (struct session *)tpr;
553 struct tty *tp = NULL;
554 int flags = TOLOG;
555 va_list ap;
557 /* mutex_enter(proc_lock); XXXSMP */
558 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
559 flags |= TOTTY;
560 tp = sess->s_ttyp;
563 kprintf_lock();
565 klogpri(LOG_INFO);
566 va_start(ap, fmt);
567 kprintf(fmt, flags, tp, NULL, ap);
568 va_end(ap);
570 kprintf_unlock();
571 /* mutex_exit(proc_lock); XXXSMP */
573 logwakeup();
578 * ttyprintf: send a message to a specific tty
580 * => should be used only by tty driver or anything that knows the
581 * underlying tty will not be revoked(2)'d away. [otherwise,
582 * use tprintf]
584 void
585 ttyprintf(struct tty *tp, const char *fmt, ...)
587 va_list ap;
589 /* No mutex needed; going to process TTY. */
590 va_start(ap, fmt);
591 kprintf(fmt, TOTTY, tp, NULL, ap);
592 va_end(ap);
595 #ifdef DDB
598 * db_printf: printf for DDB (via db_putchar)
601 void
602 db_printf(const char *fmt, ...)
604 va_list ap;
606 /* No mutex needed; DDB pauses all processors. */
607 va_start(ap, fmt);
608 kprintf(fmt, TODDB, NULL, NULL, ap);
609 va_end(ap);
611 if (db_tee_msgbuf) {
612 va_start(ap, fmt);
613 kprintf(fmt, TOLOG, NULL, NULL, ap);
614 va_end(ap);
618 void
619 db_vprintf(const char *fmt, va_list ap)
622 /* No mutex needed; DDB pauses all processors. */
623 kprintf(fmt, TODDB, NULL, NULL, ap);
624 if (db_tee_msgbuf)
625 kprintf(fmt, TOLOG, NULL, NULL, ap);
628 #endif /* DDB */
630 static void
631 kprintf_internal(const char *fmt, int oflags, void *vp, char *sbuf, ...)
633 va_list ap;
635 va_start(ap, sbuf);
636 (void)kprintf(fmt, oflags, vp, sbuf, ap);
637 va_end(ap);
641 * Device autoconfiguration printf routines. These change their
642 * behavior based on the AB_* flags in boothowto. If AB_SILENT
643 * is set, messages never go to the console (but they still always
644 * go to the log). AB_VERBOSE overrides AB_SILENT.
648 * aprint_normal: Send to console unless AB_QUIET. Always goes
649 * to the log.
651 static void
652 aprint_normal_internal(const char *prefix, const char *fmt, va_list ap)
654 int flags = TOLOG;
656 if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 ||
657 (boothowto & AB_VERBOSE) != 0)
658 flags |= TOCONS;
660 kprintf_lock();
662 if (prefix)
663 kprintf_internal("%s: ", flags, NULL, NULL, prefix);
664 kprintf(fmt, flags, NULL, NULL, ap);
666 kprintf_unlock();
668 if (!panicstr)
669 logwakeup();
672 void
673 aprint_normal(const char *fmt, ...)
675 va_list ap;
677 va_start(ap, fmt);
678 aprint_normal_internal(NULL, fmt, ap);
679 va_end(ap);
682 void
683 aprint_normal_dev(device_t dv, const char *fmt, ...)
685 va_list ap;
687 va_start(ap, fmt);
688 aprint_normal_internal(device_xname(dv), fmt, ap);
689 va_end(ap);
692 void
693 aprint_normal_ifnet(struct ifnet *ifp, const char *fmt, ...)
695 va_list ap;
697 va_start(ap, fmt);
698 aprint_normal_internal(ifp->if_xname, fmt, ap);
699 va_end(ap);
703 * aprint_error: Send to console unless AB_QUIET. Always goes
704 * to the log. Also counts the number of times called so other
705 * parts of the kernel can report the number of errors during a
706 * given phase of system startup.
708 static int aprint_error_count;
711 aprint_get_error_count(void)
713 int count;
715 kprintf_lock();
717 count = aprint_error_count;
718 aprint_error_count = 0;
720 kprintf_unlock();
722 return (count);
725 static void
726 aprint_error_internal(const char *prefix, const char *fmt, va_list ap)
728 int flags = TOLOG;
730 if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 ||
731 (boothowto & AB_VERBOSE) != 0)
732 flags |= TOCONS;
734 kprintf_lock();
736 aprint_error_count++;
738 if (prefix)
739 kprintf_internal("%s: ", flags, NULL, NULL, prefix);
740 kprintf(fmt, flags, NULL, NULL, ap);
742 kprintf_unlock();
744 if (!panicstr)
745 logwakeup();
748 void
749 aprint_error(const char *fmt, ...)
751 va_list ap;
753 va_start(ap, fmt);
754 aprint_error_internal(NULL, fmt, ap);
755 va_end(ap);
758 void
759 aprint_error_dev(device_t dv, const char *fmt, ...)
761 va_list ap;
763 va_start(ap, fmt);
764 aprint_error_internal(device_xname(dv), fmt, ap);
765 va_end(ap);
768 void
769 aprint_error_ifnet(struct ifnet *ifp, const char *fmt, ...)
771 va_list ap;
773 va_start(ap, fmt);
774 aprint_error_internal(ifp->if_xname, fmt, ap);
775 va_end(ap);
779 * aprint_naive: Send to console only if AB_QUIET. Never goes
780 * to the log.
782 static void
783 aprint_naive_internal(const char *prefix, const char *fmt, va_list ap)
786 if ((boothowto & (AB_QUIET|AB_SILENT|AB_VERBOSE)) != AB_QUIET)
787 return;
789 kprintf_lock();
791 if (prefix)
792 kprintf_internal("%s: ", TOCONS, NULL, NULL, prefix);
793 kprintf(fmt, TOCONS, NULL, NULL, ap);
795 kprintf_unlock();
798 void
799 aprint_naive(const char *fmt, ...)
801 va_list ap;
803 va_start(ap, fmt);
804 aprint_naive_internal(NULL, fmt, ap);
805 va_end(ap);
808 void
809 aprint_naive_dev(device_t dv, const char *fmt, ...)
811 va_list ap;
813 va_start(ap, fmt);
814 aprint_naive_internal(device_xname(dv), fmt, ap);
815 va_end(ap);
818 void
819 aprint_naive_ifnet(struct ifnet *ifp, const char *fmt, ...)
821 va_list ap;
823 va_start(ap, fmt);
824 aprint_naive_internal(ifp->if_xname, fmt, ap);
825 va_end(ap);
829 * aprint_verbose: Send to console only if AB_VERBOSE. Always
830 * goes to the log.
832 static void
833 aprint_verbose_internal(const char *prefix, const char *fmt, va_list ap)
835 int flags = TOLOG;
837 if (boothowto & AB_VERBOSE)
838 flags |= TOCONS;
840 kprintf_lock();
842 if (prefix)
843 kprintf_internal("%s: ", flags, NULL, NULL, prefix);
844 kprintf(fmt, flags, NULL, NULL, ap);
846 kprintf_unlock();
848 if (!panicstr)
849 logwakeup();
852 void
853 aprint_verbose(const char *fmt, ...)
855 va_list ap;
857 va_start(ap, fmt);
858 aprint_verbose_internal(NULL, fmt, ap);
859 va_end(ap);
862 void
863 aprint_verbose_dev(device_t dv, const char *fmt, ...)
865 va_list ap;
867 va_start(ap, fmt);
868 aprint_verbose_internal(device_xname(dv), fmt, ap);
869 va_end(ap);
872 void
873 aprint_verbose_ifnet(struct ifnet *ifp, const char *fmt, ...)
875 va_list ap;
877 va_start(ap, fmt);
878 aprint_verbose_internal(ifp->if_xname, fmt, ap);
879 va_end(ap);
883 * aprint_debug: Send to console and log only if AB_DEBUG.
885 static void
886 aprint_debug_internal(const char *prefix, const char *fmt, va_list ap)
889 if ((boothowto & AB_DEBUG) == 0)
890 return;
892 kprintf_lock();
894 if (prefix)
895 kprintf_internal("%s: ", TOCONS | TOLOG, NULL, NULL, prefix);
896 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
898 kprintf_unlock();
901 void
902 aprint_debug(const char *fmt, ...)
904 va_list ap;
906 va_start(ap, fmt);
907 aprint_debug_internal(NULL, fmt, ap);
908 va_end(ap);
911 void
912 aprint_debug_dev(device_t dv, const char *fmt, ...)
914 va_list ap;
916 va_start(ap, fmt);
917 aprint_debug_internal(device_xname(dv), fmt, ap);
918 va_end(ap);
921 void
922 aprint_debug_ifnet(struct ifnet *ifp, const char *fmt, ...)
924 va_list ap;
926 va_start(ap, fmt);
927 aprint_debug_internal(ifp->if_xname, fmt, ap);
928 va_end(ap);
931 void
932 printf_tolog(const char *fmt, ...)
934 va_list ap;
936 kprintf_lock();
938 va_start(ap, fmt);
939 (void)kprintf(fmt, TOLOG, NULL, NULL, ap);
940 va_end(ap);
942 kprintf_unlock();
946 * printf_nolog: Like printf(), but does not send message to the log.
949 void
950 printf_nolog(const char *fmt, ...)
952 va_list ap;
954 kprintf_lock();
956 va_start(ap, fmt);
957 kprintf(fmt, TOCONS, NULL, NULL, ap);
958 va_end(ap);
960 kprintf_unlock();
964 * normal kernel printf functions: printf, vprintf, snprintf, vsnprintf
968 * printf: print a message to the console and the log
970 void
971 printf(const char *fmt, ...)
973 va_list ap;
975 kprintf_lock();
977 va_start(ap, fmt);
978 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
979 va_end(ap);
981 kprintf_unlock();
983 if (!panicstr)
984 logwakeup();
988 * vprintf: print a message to the console and the log [already have
989 * va_alist]
992 void
993 vprintf(const char *fmt, va_list ap)
996 kprintf_lock();
998 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
1000 kprintf_unlock();
1002 if (!panicstr)
1003 logwakeup();
1007 * sprintf: print a message to a buffer
1010 sprintf(char *bf, const char *fmt, ...)
1012 int retval;
1013 va_list ap;
1015 va_start(ap, fmt);
1016 retval = kprintf(fmt, TOBUFONLY, NULL, bf, ap);
1017 va_end(ap);
1018 *(bf + retval) = 0; /* null terminate */
1019 return(retval);
1023 * vsprintf: print a message to a buffer [already have va_alist]
1027 vsprintf(char *bf, const char *fmt, va_list ap)
1029 int retval;
1031 retval = kprintf(fmt, TOBUFONLY, NULL, bf, ap);
1032 *(bf + retval) = 0; /* null terminate */
1033 return (retval);
1037 * snprintf: print a message to a buffer
1040 snprintf(char *bf, size_t size, const char *fmt, ...)
1042 int retval;
1043 va_list ap;
1044 char *p;
1046 if (size < 1)
1047 return (-1);
1048 p = bf + size - 1;
1049 va_start(ap, fmt);
1050 retval = kprintf(fmt, TOBUFONLY, &p, bf, ap);
1051 va_end(ap);
1052 *(p) = 0; /* null terminate */
1053 return(retval);
1057 * vsnprintf: print a message to a buffer [already have va_alist]
1060 vsnprintf(char *bf, size_t size, const char *fmt, va_list ap)
1062 int retval;
1063 char *p;
1065 if (size < 1)
1066 return (-1);
1067 p = bf + size - 1;
1068 retval = kprintf(fmt, TOBUFONLY, &p, bf, ap);
1069 *(p) = 0; /* null terminate */
1070 return(retval);
1074 * kprintf: scaled down version of printf(3).
1076 * this version based on vfprintf() from libc which was derived from
1077 * software contributed to Berkeley by Chris Torek.
1079 * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS!
1083 * macros for converting digits to letters and vice versa
1085 #define to_digit(c) ((c) - '0')
1086 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
1087 #define to_char(n) ((n) + '0')
1090 * flags used during conversion.
1092 #define ALT 0x001 /* alternate form */
1093 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
1094 #define LADJUST 0x004 /* left adjustment */
1095 #define LONGDBL 0x008 /* long double; unimplemented */
1096 #define LONGINT 0x010 /* long integer */
1097 #define QUADINT 0x020 /* quad integer */
1098 #define SHORTINT 0x040 /* short integer */
1099 #define MAXINT 0x080 /* intmax_t */
1100 #define PTRINT 0x100 /* intptr_t */
1101 #define SIZEINT 0x200 /* size_t */
1102 #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */
1103 #define FPT 0x800 /* Floating point number */
1106 * To extend shorts properly, we need both signed and unsigned
1107 * argument extraction methods.
1109 #define SARG() \
1110 (flags&MAXINT ? va_arg(ap, intmax_t) : \
1111 flags&PTRINT ? va_arg(ap, intptr_t) : \
1112 flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
1113 flags&QUADINT ? va_arg(ap, quad_t) : \
1114 flags&LONGINT ? va_arg(ap, long) : \
1115 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
1116 (long)va_arg(ap, int))
1117 #define UARG() \
1118 (flags&MAXINT ? va_arg(ap, uintmax_t) : \
1119 flags&PTRINT ? va_arg(ap, uintptr_t) : \
1120 flags&SIZEINT ? va_arg(ap, size_t) : \
1121 flags&QUADINT ? va_arg(ap, u_quad_t) : \
1122 flags&LONGINT ? va_arg(ap, u_long) : \
1123 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
1124 (u_long)va_arg(ap, u_int))
1126 #define KPRINTF_PUTCHAR(C) { \
1127 if (oflags == TOBUFONLY) { \
1128 if ((vp != NULL) && (sbuf == tailp)) { \
1129 ret += 1; /* indicate error */ \
1130 goto overflow; \
1132 *sbuf++ = (C); \
1133 } else { \
1134 putchar((C), oflags, (struct tty *)vp); \
1139 * Guts of kernel printf. Note, we already expect to be in a mutex!
1142 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap)
1144 const char *fmt; /* format string */
1145 int ch; /* character from fmt */
1146 int n; /* handy integer (short term usage) */
1147 char *cp; /* handy char pointer (short term usage) */
1148 int flags; /* flags as above */
1149 int ret; /* return value accumulator */
1150 int width; /* width from format (%8d), or 0 */
1151 int prec; /* precision from format (%.3d), or -1 */
1152 char sign; /* sign prefix (' ', '+', '-', or \0) */
1154 u_quad_t _uquad; /* integer arguments %[diouxX] */
1155 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
1156 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
1157 int realsz; /* field size expanded by dprec */
1158 int size; /* size of converted field or string */
1159 const char *xdigs; /* digits for [xX] conversion */
1160 char bf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
1161 char *tailp; /* tail pointer for snprintf */
1163 tailp = NULL; /* XXX: shutup gcc */
1164 if (oflags == TOBUFONLY && (vp != NULL))
1165 tailp = *(char **)vp;
1167 cp = NULL; /* XXX: shutup gcc */
1168 size = 0; /* XXX: shutup gcc */
1170 fmt = fmt0;
1171 ret = 0;
1173 xdigs = NULL; /* XXX: shut up gcc warning */
1176 * Scan the format for conversions (`%' character).
1178 for (;;) {
1179 while (*fmt != '%' && *fmt) {
1180 ret++;
1181 KPRINTF_PUTCHAR(*fmt++);
1183 if (*fmt == 0)
1184 goto done;
1186 fmt++; /* skip over '%' */
1188 flags = 0;
1189 dprec = 0;
1190 width = 0;
1191 prec = -1;
1192 sign = '\0';
1194 rflag: ch = *fmt++;
1195 reswitch: switch (ch) {
1196 case ' ':
1198 * ``If the space and + flags both appear, the space
1199 * flag will be ignored.''
1200 * -- ANSI X3J11
1202 if (!sign)
1203 sign = ' ';
1204 goto rflag;
1205 case '#':
1206 flags |= ALT;
1207 goto rflag;
1208 case '*':
1210 * ``A negative field width argument is taken as a
1211 * - flag followed by a positive field width.''
1212 * -- ANSI X3J11
1213 * They don't exclude field widths read from args.
1215 if ((width = va_arg(ap, int)) >= 0)
1216 goto rflag;
1217 width = -width;
1218 /* FALLTHROUGH */
1219 case '-':
1220 flags |= LADJUST;
1221 goto rflag;
1222 case '+':
1223 sign = '+';
1224 goto rflag;
1225 case '.':
1226 if ((ch = *fmt++) == '*') {
1227 n = va_arg(ap, int);
1228 prec = n < 0 ? -1 : n;
1229 goto rflag;
1231 n = 0;
1232 while (is_digit(ch)) {
1233 n = 10 * n + to_digit(ch);
1234 ch = *fmt++;
1236 prec = n < 0 ? -1 : n;
1237 goto reswitch;
1238 case '0':
1240 * ``Note that 0 is taken as a flag, not as the
1241 * beginning of a field width.''
1242 * -- ANSI X3J11
1244 flags |= ZEROPAD;
1245 goto rflag;
1246 case '1': case '2': case '3': case '4':
1247 case '5': case '6': case '7': case '8': case '9':
1248 n = 0;
1249 do {
1250 n = 10 * n + to_digit(ch);
1251 ch = *fmt++;
1252 } while (is_digit(ch));
1253 width = n;
1254 goto reswitch;
1255 case 'h':
1256 flags |= SHORTINT;
1257 goto rflag;
1258 case 'j':
1259 flags |= MAXINT;
1260 goto rflag;
1261 case 'l':
1262 if (*fmt == 'l') {
1263 fmt++;
1264 flags |= QUADINT;
1265 } else {
1266 flags |= LONGINT;
1268 goto rflag;
1269 case 'q':
1270 flags |= QUADINT;
1271 goto rflag;
1272 case 't':
1273 flags |= PTRINT;
1274 goto rflag;
1275 case 'z':
1276 flags |= SIZEINT;
1277 goto rflag;
1278 case 'c':
1279 *(cp = bf) = va_arg(ap, int);
1280 size = 1;
1281 sign = '\0';
1282 break;
1283 case 'D':
1284 flags |= LONGINT;
1285 /*FALLTHROUGH*/
1286 case 'd':
1287 case 'i':
1288 _uquad = SARG();
1289 if ((quad_t)_uquad < 0) {
1290 _uquad = -_uquad;
1291 sign = '-';
1293 base = DEC;
1294 goto number;
1295 case 'n':
1296 if (flags & MAXINT)
1297 *va_arg(ap, intmax_t *) = ret;
1298 else if (flags & PTRINT)
1299 *va_arg(ap, intptr_t *) = ret;
1300 else if (flags & SIZEINT)
1301 *va_arg(ap, ssize_t *) = ret;
1302 else if (flags & QUADINT)
1303 *va_arg(ap, quad_t *) = ret;
1304 else if (flags & LONGINT)
1305 *va_arg(ap, long *) = ret;
1306 else if (flags & SHORTINT)
1307 *va_arg(ap, short *) = ret;
1308 else
1309 *va_arg(ap, int *) = ret;
1310 continue; /* no output */
1311 case 'O':
1312 flags |= LONGINT;
1313 /*FALLTHROUGH*/
1314 case 'o':
1315 _uquad = UARG();
1316 base = OCT;
1317 goto nosign;
1318 case 'p':
1320 * ``The argument shall be a pointer to void. The
1321 * value of the pointer is converted to a sequence
1322 * of printable characters, in an implementation-
1323 * defined manner.''
1324 * -- ANSI X3J11
1326 /* NOSTRICT */
1327 _uquad = (u_long)va_arg(ap, void *);
1328 base = HEX;
1329 xdigs = hexdigits;
1330 flags |= HEXPREFIX;
1331 ch = 'x';
1332 goto nosign;
1333 case 's':
1334 if ((cp = va_arg(ap, char *)) == NULL)
1335 /*XXXUNCONST*/
1336 cp = __UNCONST("(null)");
1337 if (prec >= 0) {
1339 * can't use strlen; can only look for the
1340 * NUL in the first `prec' characters, and
1341 * strlen() will go further.
1343 char *p = memchr(cp, 0, prec);
1345 if (p != NULL) {
1346 size = p - cp;
1347 if (size > prec)
1348 size = prec;
1349 } else
1350 size = prec;
1351 } else
1352 size = strlen(cp);
1353 sign = '\0';
1354 break;
1355 case 'U':
1356 flags |= LONGINT;
1357 /*FALLTHROUGH*/
1358 case 'u':
1359 _uquad = UARG();
1360 base = DEC;
1361 goto nosign;
1362 case 'X':
1363 xdigs = HEXDIGITS;
1364 goto hex;
1365 case 'x':
1366 xdigs = hexdigits;
1367 hex: _uquad = UARG();
1368 base = HEX;
1369 /* leading 0x/X only if non-zero */
1370 if (flags & ALT && _uquad != 0)
1371 flags |= HEXPREFIX;
1373 /* unsigned conversions */
1374 nosign: sign = '\0';
1376 * ``... diouXx conversions ... if a precision is
1377 * specified, the 0 flag will be ignored.''
1378 * -- ANSI X3J11
1380 number: if ((dprec = prec) >= 0)
1381 flags &= ~ZEROPAD;
1384 * ``The result of converting a zero value with an
1385 * explicit precision of zero is no characters.''
1386 * -- ANSI X3J11
1388 cp = bf + KPRINTF_BUFSIZE;
1389 if (_uquad != 0 || prec != 0) {
1391 * Unsigned mod is hard, and unsigned mod
1392 * by a constant is easier than that by
1393 * a variable; hence this switch.
1395 switch (base) {
1396 case OCT:
1397 do {
1398 *--cp = to_char(_uquad & 7);
1399 _uquad >>= 3;
1400 } while (_uquad);
1401 /* handle octal leading 0 */
1402 if (flags & ALT && *cp != '0')
1403 *--cp = '0';
1404 break;
1406 case DEC:
1407 /* many numbers are 1 digit */
1408 while (_uquad >= 10) {
1409 *--cp = to_char(_uquad % 10);
1410 _uquad /= 10;
1412 *--cp = to_char(_uquad);
1413 break;
1415 case HEX:
1416 do {
1417 *--cp = xdigs[_uquad & 15];
1418 _uquad >>= 4;
1419 } while (_uquad);
1420 break;
1422 default:
1423 /*XXXUNCONST*/
1424 cp = __UNCONST("bug in kprintf: bad base");
1425 size = strlen(cp);
1426 goto skipsize;
1429 size = bf + KPRINTF_BUFSIZE - cp;
1430 skipsize:
1431 break;
1432 default: /* "%?" prints ?, unless ? is NUL */
1433 if (ch == '\0')
1434 goto done;
1435 /* pretend it was %c with argument ch */
1436 cp = bf;
1437 *cp = ch;
1438 size = 1;
1439 sign = '\0';
1440 break;
1444 * All reasonable formats wind up here. At this point, `cp'
1445 * points to a string which (if not flags&LADJUST) should be
1446 * padded out to `width' places. If flags&ZEROPAD, it should
1447 * first be prefixed by any sign or other prefix; otherwise,
1448 * it should be blank padded before the prefix is emitted.
1449 * After any left-hand padding and prefixing, emit zeroes
1450 * required by a decimal [diouxX] precision, then print the
1451 * string proper, then emit zeroes required by any leftover
1452 * floating precision; finally, if LADJUST, pad with blanks.
1454 * Compute actual size, so we know how much to pad.
1455 * size excludes decimal prec; realsz includes it.
1457 realsz = dprec > size ? dprec : size;
1458 if (sign)
1459 realsz++;
1460 else if (flags & HEXPREFIX)
1461 realsz+= 2;
1463 /* adjust ret */
1464 ret += width > realsz ? width : realsz;
1466 /* right-adjusting blank padding */
1467 if ((flags & (LADJUST|ZEROPAD)) == 0) {
1468 n = width - realsz;
1469 while (n-- > 0)
1470 KPRINTF_PUTCHAR(' ');
1473 /* prefix */
1474 if (sign) {
1475 KPRINTF_PUTCHAR(sign);
1476 } else if (flags & HEXPREFIX) {
1477 KPRINTF_PUTCHAR('0');
1478 KPRINTF_PUTCHAR(ch);
1481 /* right-adjusting zero padding */
1482 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1483 n = width - realsz;
1484 while (n-- > 0)
1485 KPRINTF_PUTCHAR('0');
1488 /* leading zeroes from decimal precision */
1489 n = dprec - size;
1490 while (n-- > 0)
1491 KPRINTF_PUTCHAR('0');
1493 /* the string or number proper */
1494 while (size--)
1495 KPRINTF_PUTCHAR(*cp++);
1496 /* left-adjusting padding (always blank) */
1497 if (flags & LADJUST) {
1498 n = width - realsz;
1499 while (n-- > 0)
1500 KPRINTF_PUTCHAR(' ');
1504 done:
1505 if ((oflags == TOBUFONLY) && (vp != NULL))
1506 *(char **)vp = sbuf;
1507 (*v_flush)();
1508 overflow:
1509 return (ret);
1510 /* NOTREACHED */