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$");
41 #include "opt_printf.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/mutex.h>
49 #include <sys/kernel.h>
50 #include <sys/msgbuf.h>
51 #include <sys/malloc.h>
54 #include <sys/stddef.h>
55 #include <sys/sysctl.h>
57 #include <sys/syslog.h>
60 #include <sys/ctype.h>
67 * Note that stdarg.h and the ANSI style va_start macro is used for both
68 * ANSI and traditional C compilers.
70 #include <machine/stdarg.h>
76 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
77 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
96 static void msglogchar(int c, int pri);
97 static void putchar(int ch, void *arg);
98 static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len, int upper);
99 static void snprintf_func(int ch, void *arg);
101 static int msgbufmapped; /* Set when safe to use msgbuf */
104 static int log_console_output = 1;
105 TUNABLE_INT("kern.log_console_output", &log_console_output);
106 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
107 &log_console_output, 0, "Duplicate console output to the syslog.");
109 static int always_console_output = 0;
110 TUNABLE_INT("kern.always_console_output", &always_console_output);
111 SYSCTL_INT(_kern, OID_AUTO, always_console_output, CTLFLAG_RW,
112 &always_console_output, 0, "Always output to console despite TIOCCONS.");
115 * Warn that a system table is full.
118 tablefull(const char *tab)
121 log(LOG_ERR, "%s: table is full\n", tab);
125 * Uprintf prints to the controlling terminal for the current process.
128 uprintf(const char *fmt, ...)
130 struct thread *td = curthread;
131 struct proc *p = td->td_proc;
133 struct putchar_arg pca;
136 if (td == NULL || TD_IS_IDLETHREAD(td))
142 if ((p->p_flag & P_CONTROLT) == 0) {
147 SESS_LOCK(p->p_session);
148 pca.tty = p->p_session->s_ttyp;
149 SESS_UNLOCK(p->p_session);
151 if (pca.tty == NULL) {
157 retval = kvprintf(fmt, putchar, &pca, 10, ap);
165 * tprintf prints on the controlling terminal associated with the given
166 * session, possibly to the log as well.
169 tprintf(struct proc *p, int pri, const char *fmt, ...)
171 struct tty *tp = NULL;
174 struct putchar_arg pca;
175 struct session *sess = NULL;
182 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
189 if (ttycheckoutq(tp, 0))
200 kvprintf(fmt, putchar, &pca, 10, ap);
209 * Ttyprintf displays a message on a tty; it should be used only by
210 * the tty driver, or anything that knows the underlying tty will not
211 * be revoke(2)'d away. Other callers should use tprintf.
214 ttyprintf(struct tty *tp, const char *fmt, ...)
217 struct putchar_arg pca;
223 retval = kvprintf(fmt, putchar, &pca, 10, ap);
229 * Log writes to the log buffer, and guarantees not to sleep (so can be
230 * called by interrupt routines). If there is no process reading the
231 * log yet, it writes to the console also.
234 log(int level, const char *fmt, ...)
237 struct putchar_arg pca;
241 pca.flags = log_open ? TOLOG : TOCONS;
245 kvprintf(fmt, putchar, &pca, 10, ap);
251 #define CONSCHUNK 128
254 log_console(struct uio *uio)
260 if (!log_console_output)
263 pri = LOG_INFO | LOG_CONSOLE;
265 consbuffer = malloc(CONSCHUNK, M_TEMP, M_WAITOK);
268 while (uio->uio_resid > 0) {
269 c = imin(uio->uio_resid, CONSCHUNK);
270 error = uiomove(consbuffer, c, uio);
273 for (i = 0; i < c; i++) {
274 msglogchar(consbuffer[i], pri);
275 if (consbuffer[i] == '\n')
282 msglogchar('\n', pri);
285 free(consbuffer, M_TEMP);
290 printf(const char *fmt, ...)
293 struct putchar_arg pca;
295 #ifdef PRINTF_BUFR_SIZE
296 char bufr[PRINTF_BUFR_SIZE];
301 pca.flags = TOCONS | TOLOG;
303 #ifdef PRINTF_BUFR_SIZE
305 pca.p_next = pca.p_bufr;
306 pca.n_bufr = sizeof(bufr);
307 pca.remain = sizeof(bufr);
310 /* Don't buffer console output. */
314 retval = kvprintf(fmt, putchar, &pca, 10, ap);
317 #ifdef PRINTF_BUFR_SIZE
318 /* Write any buffered console output: */
319 if (*pca.p_bufr != '\0')
330 vprintf(const char *fmt, va_list ap)
332 struct putchar_arg pca;
334 #ifdef PRINTF_BUFR_SIZE
335 char bufr[PRINTF_BUFR_SIZE];
339 pca.flags = TOCONS | TOLOG;
341 #ifdef PRINTF_BUFR_SIZE
343 pca.p_next = pca.p_bufr;
344 pca.n_bufr = sizeof(bufr);
345 pca.remain = sizeof(bufr);
348 /* Don't buffer console output. */
352 retval = kvprintf(fmt, putchar, &pca, 10, ap);
354 #ifdef PRINTF_BUFR_SIZE
355 /* Write any buffered console output: */
356 if (*pca.p_bufr != '\0')
367 putcons(int c, struct putchar_arg *ap)
369 /* Check if no console output buffer was provided. */
370 if (ap->p_bufr == NULL)
371 /* Output direct to the console. */
374 /* Buffer the character: */
376 *ap->p_next++ = '\r';
382 /* Always leave the buffer zero terminated. */
385 /* Check if the buffer needs to be flushed. */
386 if (ap->remain < 3 || c == '\n') {
388 ap->p_next = ap->p_bufr;
389 ap->remain = ap->n_bufr;
396 * Print a character on console or users terminal. If destination is
397 * the console then the last bunch of characters are saved in msgbuf for
401 putchar(int c, void *arg)
403 struct putchar_arg *ap = (struct putchar_arg*) arg;
404 struct tty *tp = ap->tty;
405 int flags = ap->flags;
407 /* Don't use the tty code after a panic or while in ddb. */
411 } else if (panicstr || ((flags & TOCONS) && constty == NULL)) {
415 if ((flags & TOTTY) && tp != NULL)
417 if (flags & TOCONS) {
419 msgbuf_addchar(&consmsgbuf, c);
420 if (always_console_output && c != '\0')
425 msglogchar(c, ap->pri);
429 * Scaled down version of sprintf(3).
432 sprintf(char *buf, const char *cfmt, ...)
438 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
445 * Scaled down version of vsprintf(3).
448 vsprintf(char *buf, const char *cfmt, va_list ap)
452 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
458 * Scaled down version of snprintf(3).
461 snprintf(char *str, size_t size, const char *format, ...)
466 va_start(ap, format);
467 retval = vsnprintf(str, size, format, ap);
473 * Scaled down version of vsnprintf(3).
476 vsnprintf(char *str, size_t size, const char *format, va_list ap)
478 struct snprintf_arg info;
483 retval = kvprintf(format, snprintf_func, &info, 10, ap);
484 if (info.remain >= 1)
490 * Kernel version which takes radix argument vsnprintf(3).
493 vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
495 struct snprintf_arg info;
500 retval = kvprintf(format, snprintf_func, &info, radix, ap);
501 if (info.remain >= 1)
507 snprintf_func(int ch, void *arg)
509 struct snprintf_arg *const info = arg;
511 if (info->remain >= 2) {
518 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
519 * order; return an optional length and a pointer to the last character
520 * written in the buffer (i.e., the first character of the string).
521 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
524 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
531 c = hex2ascii(num % base);
532 *++p = upper ? toupper(c) : c;
533 } while (num /= base);
540 * Scaled down version of printf(3).
542 * Two additional formats:
544 * The format %b is supported to decode error registers.
547 * printf("reg=%b\n", regval, "<base><arg>*");
549 * where <base> is the output base expressed as a control character, e.g.
550 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
551 * the first of which gives the bit number to be inspected (origin 1), and
552 * the next characters (up to a control character, i.e. a character <= 32),
553 * give the name of the register. Thus:
555 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
557 * would produce output:
559 * reg=3<BITTWO,BITONE>
561 * XXX: %D -- Hexdump, takes pointer and separator string:
562 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
563 * ("%*D", len, ptr, " " -> XX XX XX XX ...
566 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
568 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
571 const char *p, *percent, *q;
575 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
576 int cflag, hflag, jflag, tflag, zflag;
579 int stop = 0, retval = 0;
588 fmt = "(fmt null)\n";
590 if (radix < 2 || radix > 36)
596 while ((ch = (u_char)*fmt++) != '%' || stop) {
602 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
603 sign = 0; dot = 0; dwidth = 0; upper = 0;
604 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
605 reswitch: switch (ch = (u_char)*fmt++) {
623 width = va_arg(ap, int);
629 dwidth = va_arg(ap, int);
637 case '1': case '2': case '3': case '4':
638 case '5': case '6': case '7': case '8': case '9':
639 for (n = 0;; ++fmt) {
640 n = n * 10 + ch - '0';
642 if (ch < '0' || ch > '9')
651 num = (u_int)va_arg(ap, int);
652 p = va_arg(ap, char *);
653 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
661 if (num & (1 << (n - 1))) {
662 PCHAR(tmp ? ',' : '<');
663 for (; (n = *p) > ' '; ++p)
667 for (; *p > ' '; ++p)
674 PCHAR(va_arg(ap, int));
677 up = va_arg(ap, u_char *);
678 p = va_arg(ap, char *);
682 PCHAR(hex2ascii(*up >> 4));
683 PCHAR(hex2ascii(*up & 0x0f));
714 *(va_arg(ap, intmax_t *)) = retval;
716 *(va_arg(ap, quad_t *)) = retval;
718 *(va_arg(ap, long *)) = retval;
720 *(va_arg(ap, size_t *)) = retval;
722 *(va_arg(ap, short *)) = retval;
724 *(va_arg(ap, char *)) = retval;
726 *(va_arg(ap, int *)) = retval;
733 sharpflag = (width == 0);
735 num = (uintptr_t)va_arg(ap, void *);
746 p = va_arg(ap, char *);
752 for (n = 0; n < dwidth && p[n]; n++)
757 if (!ladjust && width > 0)
762 if (ladjust && width > 0)
787 num = va_arg(ap, uintmax_t);
789 num = va_arg(ap, u_quad_t);
791 num = va_arg(ap, ptrdiff_t);
793 num = va_arg(ap, u_long);
795 num = va_arg(ap, size_t);
797 num = (u_short)va_arg(ap, int);
799 num = (u_char)va_arg(ap, int);
801 num = va_arg(ap, u_int);
805 num = va_arg(ap, intmax_t);
807 num = va_arg(ap, quad_t);
809 num = va_arg(ap, ptrdiff_t);
811 num = va_arg(ap, long);
813 num = va_arg(ap, ssize_t);
815 num = (short)va_arg(ap, int);
817 num = (char)va_arg(ap, int);
819 num = va_arg(ap, int);
821 if (sign && (intmax_t)num < 0) {
823 num = -(intmax_t)num;
825 p = ksprintn(nbuf, num, base, &tmp, upper);
826 if (sharpflag && num != 0) {
835 if (!ladjust && padc != '0' && width
836 && (width -= tmp) > 0)
841 if (sharpflag && num != 0) {
844 } else if (base == 16) {
849 if (!ladjust && width && (width -= tmp) > 0)
856 if (ladjust && width && (width -= tmp) > 0)
862 while (percent < fmt)
865 * Since we ignore an formatting argument it is no
866 * longer safe to obey the remaining formatting
867 * arguments as the arguments will no longer match
878 * Put character in log buffer with a particular priority.
881 msglogchar(int c, int pri)
883 static int lastpri = -1;
890 if (c == '\0' || c == '\r')
892 if (pri != -1 && pri != lastpri) {
894 msgbuf_addchar(msgbufp, '\n');
897 msgbuf_addchar(msgbufp, '<');
898 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;)
899 msgbuf_addchar(msgbufp, *p--);
900 msgbuf_addchar(msgbufp, '>');
903 msgbuf_addchar(msgbufp, c);
913 msgbufinit(void *ptr, int size)
916 static struct msgbuf *oldp = NULL;
918 size -= sizeof(*msgbufp);
920 msgbufp = (struct msgbuf *)(cp + size);
921 msgbuf_reinit(msgbufp, cp, size);
922 if (msgbufmapped && oldp != msgbufp)
923 msgbuf_copy(oldp, msgbufp);
928 static int unprivileged_read_msgbuf = 1;
929 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf,
930 CTLFLAG_RW, &unprivileged_read_msgbuf, 0,
931 "Unprivileged processes may read the kernel message buffer");
933 /* Sysctls for accessing/clearing the msgbuf */
935 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
941 if (!unprivileged_read_msgbuf) {
942 error = priv_check(req->td, PRIV_MSGBUF);
947 /* Read the whole buffer, one chunk at a time. */
948 msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
949 while ((len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq)) > 0) {
950 error = sysctl_handle_opaque(oidp, buf, len, req);
957 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
958 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
960 static int msgbuf_clearflag;
963 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
966 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
967 if (!error && req->newptr) {
968 msgbuf_clear(msgbufp);
969 msgbuf_clearflag = 0;
974 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
975 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clearflag, 0,
976 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
980 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
985 db_printf("msgbuf not mapped yet\n");
988 db_printf("msgbufp = %p\n", msgbufp);
989 db_printf("magic = %x, size = %d, r= %u, w = %u, ptr = %p, cksum= %u\n",
990 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_rseq,
991 msgbufp->msg_wseq, msgbufp->msg_ptr, msgbufp->msg_cksum);
992 for (i = 0; i < msgbufp->msg_size && !db_pager_quit; i++) {
993 j = MSGBUF_SEQ_TO_POS(msgbufp, i + msgbufp->msg_rseq);
994 db_printf("%c", msgbufp->msg_ptr[j]);
1002 hexdump(const void *ptr, int length, const char *hdr, int flags)
1006 const unsigned char *cp;
1009 if ((flags & HD_DELIM_MASK) != 0)
1010 delim = (flags & HD_DELIM_MASK) >> 8;
1014 if ((flags & HD_COLUMN_MASK) != 0)
1015 cols = flags & HD_COLUMN_MASK;
1020 for (i = 0; i < length; i+= cols) {
1024 if ((flags & HD_OMIT_COUNT) == 0)
1027 if ((flags & HD_OMIT_HEX) == 0) {
1028 for (j = 0; j < cols; j++) {
1031 printf("%c%02x", delim, cp[k]);
1037 if ((flags & HD_OMIT_CHARS) == 0) {
1039 for (j = 0; j < cols; j++) {
1043 else if (cp[k] >= ' ' && cp[k] <= '~')
1044 printf("%c", cp[k]);