2 * Copyright (c) 1986, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
46 #include <sys/mutex.h>
48 #include <sys/kernel.h>
49 #include <sys/msgbuf.h>
50 #include <sys/malloc.h>
52 #include <sys/stddef.h>
53 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
64 * Note that stdarg.h and the ANSI style va_start macro is used for both
65 * ANSI and traditional C compilers.
67 #include <machine/stdarg.h>
73 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
74 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
89 static void msglogchar(int c, int pri);
90 static void putchar(int ch, void *arg);
91 static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len);
92 static void snprintf_func(int ch, void *arg);
94 static int consintr = 1; /* Ok to handle console interrupts? */
95 static int msgbufmapped; /* Set when safe to use msgbuf */
98 static int log_console_output = 1;
99 TUNABLE_INT("kern.log_console_output", &log_console_output);
100 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
101 &log_console_output, 0, "Duplicate console output to the syslog.");
103 static int always_console_output = 0;
104 TUNABLE_INT("kern.always_console_output", &always_console_output);
105 SYSCTL_INT(_kern, OID_AUTO, always_console_output, CTLFLAG_RW,
106 &always_console_output, 0, "Always output to console despite TIOCCONS.");
109 * Warn that a system table is full.
112 tablefull(const char *tab)
115 log(LOG_ERR, "%s: table is full\n", tab);
119 * Uprintf prints to the controlling terminal for the current process.
122 uprintf(const char *fmt, ...)
124 struct thread *td = curthread;
125 struct proc *p = td->td_proc;
127 struct putchar_arg pca;
130 if (td == NULL || td == PCPU_GET(idlethread))
136 if ((p->p_flag & P_CONTROLT) == 0) {
141 SESS_LOCK(p->p_session);
142 pca.tty = p->p_session->s_ttyp;
143 SESS_UNLOCK(p->p_session);
145 if (pca.tty == NULL) {
151 retval = kvprintf(fmt, putchar, &pca, 10, ap);
159 * tprintf prints on the controlling terminal associated with the given
160 * session, possibly to the log as well.
163 tprintf(struct proc *p, int pri, const char *fmt, ...)
165 struct tty *tp = NULL;
168 struct putchar_arg pca;
169 struct session *sess = NULL;
176 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
183 if (ttycheckoutq(tp, 0))
194 kvprintf(fmt, putchar, &pca, 10, ap);
203 * Ttyprintf displays a message on a tty; it should be used only by
204 * the tty driver, or anything that knows the underlying tty will not
205 * be revoke(2)'d away. Other callers should use tprintf.
208 ttyprintf(struct tty *tp, const char *fmt, ...)
211 struct putchar_arg pca;
217 retval = kvprintf(fmt, putchar, &pca, 10, ap);
223 * Log writes to the log buffer, and guarantees not to sleep (so can be
224 * called by interrupt routines). If there is no process reading the
225 * log yet, it writes to the console also.
228 log(int level, const char *fmt, ...)
231 struct putchar_arg pca;
235 pca.flags = log_open ? TOLOG : TOCONS;
238 kvprintf(fmt, putchar, &pca, 10, ap);
244 #define CONSCHUNK 128
247 log_console(struct uio *uio)
253 if (!log_console_output)
256 pri = LOG_INFO | LOG_CONSOLE;
258 consbuffer = malloc(CONSCHUNK, M_TEMP, M_WAITOK);
261 while (uio->uio_resid > 0) {
262 c = imin(uio->uio_resid, CONSCHUNK);
263 error = uiomove(consbuffer, c, uio);
266 for (i = 0; i < c; i++) {
267 msglogchar(consbuffer[i], pri);
268 if (consbuffer[i] == '\n')
275 msglogchar('\n', pri);
278 free(consbuffer, M_TEMP);
283 printf(const char *fmt, ...)
287 struct putchar_arg pca;
290 savintr = consintr; /* disable interrupts */
294 pca.flags = TOCONS | TOLOG;
296 retval = kvprintf(fmt, putchar, &pca, 10, ap);
300 consintr = savintr; /* reenable interrupts */
305 vprintf(const char *fmt, va_list ap)
308 struct putchar_arg pca;
311 savintr = consintr; /* disable interrupts */
314 pca.flags = TOCONS | TOLOG;
316 retval = kvprintf(fmt, putchar, &pca, 10, ap);
319 consintr = savintr; /* reenable interrupts */
324 * Print a character on console or users terminal. If destination is
325 * the console then the last bunch of characters are saved in msgbuf for
329 putchar(int c, void *arg)
331 struct putchar_arg *ap = (struct putchar_arg*) arg;
332 struct tty *tp = ap->tty;
333 int consdirect, flags = ap->flags;
335 consdirect = ((flags & TOCONS) && constty == NULL);
336 /* Don't use the tty code after a panic or while in ddb. */
345 if ((flags & TOTTY) && tp != NULL)
347 if (flags & TOCONS) {
349 msgbuf_addchar(&consmsgbuf, c);
350 if (always_console_output && c != '\0')
355 msglogchar(c, ap->pri);
359 * Scaled down version of sprintf(3).
362 sprintf(char *buf, const char *cfmt, ...)
368 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
375 * Scaled down version of vsprintf(3).
378 vsprintf(char *buf, const char *cfmt, va_list ap)
382 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
388 * Scaled down version of snprintf(3).
391 snprintf(char *str, size_t size, const char *format, ...)
396 va_start(ap, format);
397 retval = vsnprintf(str, size, format, ap);
403 * Scaled down version of vsnprintf(3).
406 vsnprintf(char *str, size_t size, const char *format, va_list ap)
408 struct snprintf_arg info;
413 retval = kvprintf(format, snprintf_func, &info, 10, ap);
414 if (info.remain >= 1)
420 * Kernel version which takes radix argument vsnprintf(3).
423 vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
425 struct snprintf_arg info;
430 retval = kvprintf(format, snprintf_func, &info, radix, ap);
431 if (info.remain >= 1)
437 snprintf_func(int ch, void *arg)
439 struct snprintf_arg *const info = arg;
441 if (info->remain >= 2) {
448 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
449 * order; return an optional length and a pointer to the last character
450 * written in the buffer (i.e., the first character of the string).
451 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
454 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp)
461 *++p = hex2ascii(num % base);
462 } while (num /= base);
469 * Scaled down version of printf(3).
471 * Two additional formats:
473 * The format %b is supported to decode error registers.
476 * printf("reg=%b\n", regval, "<base><arg>*");
478 * where <base> is the output base expressed as a control character, e.g.
479 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
480 * the first of which gives the bit number to be inspected (origin 1), and
481 * the next characters (up to a control character, i.e. a character <= 32),
482 * give the name of the register. Thus:
484 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
486 * would produce output:
488 * reg=3<BITTWO,BITONE>
490 * XXX: %D -- Hexdump, takes pointer and separator string:
491 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
492 * ("%*D", len, ptr, " " -> XX XX XX XX ...
495 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
497 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
500 const char *p, *percent, *q;
504 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
505 int cflag, hflag, jflag, tflag, zflag;
508 int stop = 0, retval = 0;
517 fmt = "(fmt null)\n";
519 if (radix < 2 || radix > 36)
525 while ((ch = (u_char)*fmt++) != '%' || stop) {
531 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
532 sign = 0; dot = 0; dwidth = 0;
533 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
534 reswitch: switch (ch = (u_char)*fmt++) {
552 width = va_arg(ap, int);
558 dwidth = va_arg(ap, int);
566 case '1': case '2': case '3': case '4':
567 case '5': case '6': case '7': case '8': case '9':
568 for (n = 0;; ++fmt) {
569 n = n * 10 + ch - '0';
571 if (ch < '0' || ch > '9')
580 num = (u_int)va_arg(ap, int);
581 p = va_arg(ap, char *);
582 for (q = ksprintn(nbuf, num, *p++, NULL); *q;)
590 if (num & (1 << (n - 1))) {
591 PCHAR(tmp ? ',' : '<');
592 for (; (n = *p) > ' '; ++p)
596 for (; *p > ' '; ++p)
603 PCHAR(va_arg(ap, int));
606 up = va_arg(ap, u_char *);
607 p = va_arg(ap, char *);
611 PCHAR(hex2ascii(*up >> 4));
612 PCHAR(hex2ascii(*up & 0x0f));
643 *(va_arg(ap, intmax_t *)) = retval;
645 *(va_arg(ap, quad_t *)) = retval;
647 *(va_arg(ap, long *)) = retval;
649 *(va_arg(ap, size_t *)) = retval;
651 *(va_arg(ap, short *)) = retval;
653 *(va_arg(ap, char *)) = retval;
655 *(va_arg(ap, int *)) = retval;
662 sharpflag = (width == 0);
664 num = (uintptr_t)va_arg(ap, void *);
675 p = va_arg(ap, char *);
681 for (n = 0; n < dwidth && p[n]; n++)
686 if (!ladjust && width > 0)
691 if (ladjust && width > 0)
715 num = va_arg(ap, uintmax_t);
717 num = va_arg(ap, u_quad_t);
719 num = va_arg(ap, ptrdiff_t);
721 num = va_arg(ap, u_long);
723 num = va_arg(ap, size_t);
725 num = (u_short)va_arg(ap, int);
727 num = (u_char)va_arg(ap, int);
729 num = va_arg(ap, u_int);
733 num = va_arg(ap, intmax_t);
735 num = va_arg(ap, quad_t);
737 num = va_arg(ap, ptrdiff_t);
739 num = va_arg(ap, long);
741 num = va_arg(ap, size_t);
743 num = (short)va_arg(ap, int);
745 num = (char)va_arg(ap, int);
747 num = va_arg(ap, int);
749 if (sign && (intmax_t)num < 0) {
751 num = -(intmax_t)num;
753 p = ksprintn(nbuf, num, base, &tmp);
754 if (sharpflag && num != 0) {
763 if (!ladjust && padc != '0' && width
764 && (width -= tmp) > 0)
769 if (sharpflag && num != 0) {
772 } else if (base == 16) {
777 if (!ladjust && width && (width -= tmp) > 0)
784 if (ladjust && width && (width -= tmp) > 0)
790 while (percent < fmt)
793 * Since we ignore an formatting argument it is no
794 * longer safe to obey the remaining formatting
795 * arguments as the arguments will no longer match
806 * Put character in log buffer with a particular priority.
809 msglogchar(int c, int pri)
811 static int lastpri = -1;
818 if (c == '\0' || c == '\r')
820 if (pri != -1 && pri != lastpri) {
822 msgbuf_addchar(msgbufp, '\n');
825 msgbuf_addchar(msgbufp, '<');
826 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL); *p;)
827 msgbuf_addchar(msgbufp, *p--);
828 msgbuf_addchar(msgbufp, '>');
831 msgbuf_addchar(msgbufp, c);
841 msgbufinit(void *ptr, int size)
844 static struct msgbuf *oldp = NULL;
846 size -= sizeof(*msgbufp);
848 msgbufp = (struct msgbuf *)(cp + size);
849 msgbuf_reinit(msgbufp, cp, size);
850 if (msgbufmapped && oldp != msgbufp)
851 msgbuf_copy(oldp, msgbufp);
856 SYSCTL_DECL(_security_bsd);
858 static int unprivileged_read_msgbuf = 1;
859 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf,
860 CTLFLAG_RW, &unprivileged_read_msgbuf, 0,
861 "Unprivileged processes may read the kernel message buffer");
863 /* Sysctls for accessing/clearing the msgbuf */
865 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
871 if (!unprivileged_read_msgbuf) {
872 error = suser(req->td);
877 /* Read the whole buffer, one chunk at a time. */
878 msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
879 while ((len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq)) > 0) {
880 error = sysctl_handle_opaque(oidp, buf, len, req);
887 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
888 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
890 static int msgbuf_clearflag;
893 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
896 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
897 if (!error && req->newptr) {
898 msgbuf_clear(msgbufp);
899 msgbuf_clearflag = 0;
904 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
905 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clearflag, 0,
906 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
910 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
917 db_printf("msgbuf not mapped yet\n");
920 db_setup_paging(db_simple_pager, &quit, db_lines_per_page);
921 db_printf("msgbufp = %p\n", msgbufp);
922 db_printf("magic = %x, size = %d, r= %u, w = %u, ptr = %p, cksum= %u\n",
923 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_rseq,
924 msgbufp->msg_wseq, msgbufp->msg_ptr, msgbufp->msg_cksum);
925 for (i = 0; i < msgbufp->msg_size && !quit; i++) {
926 j = MSGBUF_SEQ_TO_POS(msgbufp, i + msgbufp->msg_rseq);
927 db_printf("%c", msgbufp->msg_ptr[j]);
935 hexdump(const void *ptr, int length, const char *hdr, int flags)
939 const unsigned char *cp;
942 if ((flags & HD_DELIM_MASK) != 0)
943 delim = (flags & HD_DELIM_MASK) >> 8;
947 if ((flags & HD_COLUMN_MASK) != 0)
948 cols = flags & HD_COLUMN_MASK;
953 for (i = 0; i < length; i+= cols) {
957 if ((flags & HD_OMIT_COUNT) == 0)
960 if ((flags & HD_OMIT_HEX) == 0) {
961 for (j = 0; j < cols; j++) {
964 printf("%c%02x", delim, cp[k]);
970 if ((flags & HD_OMIT_CHARS) == 0) {
972 for (j = 0; j < cols; j++) {
976 else if (cp[k] >= ' ' && cp[k] <= '~')