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>
58 #include <sys/ctype.h>
65 * Note that stdarg.h and the ANSI style va_start macro is used for both
66 * ANSI and traditional C compilers.
68 #include <machine/stdarg.h>
74 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
75 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
90 static void msglogchar(int c, int pri);
91 static void putchar(int ch, void *arg);
92 static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len, int upper);
93 static void snprintf_func(int ch, void *arg);
95 static int consintr = 1; /* Ok to handle console interrupts? */
96 static int msgbufmapped; /* Set when safe to use msgbuf */
99 static int log_console_output = 1;
100 TUNABLE_INT("kern.log_console_output", &log_console_output);
101 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
102 &log_console_output, 0, "Duplicate console output to the syslog.");
104 static int always_console_output = 0;
105 TUNABLE_INT("kern.always_console_output", &always_console_output);
106 SYSCTL_INT(_kern, OID_AUTO, always_console_output, CTLFLAG_RW,
107 &always_console_output, 0, "Always output to console despite TIOCCONS.");
110 * Warn that a system table is full.
113 tablefull(const char *tab)
116 log(LOG_ERR, "%s: table is full\n", tab);
120 * Uprintf prints to the controlling terminal for the current process.
123 uprintf(const char *fmt, ...)
125 struct thread *td = curthread;
126 struct proc *p = td->td_proc;
128 struct putchar_arg pca;
131 if (td == NULL || td == PCPU_GET(idlethread))
137 if ((p->p_flag & P_CONTROLT) == 0) {
142 SESS_LOCK(p->p_session);
143 pca.tty = p->p_session->s_ttyp;
144 SESS_UNLOCK(p->p_session);
146 if (pca.tty == NULL) {
152 retval = kvprintf(fmt, putchar, &pca, 10, ap);
160 * tprintf prints on the controlling terminal associated with the given
161 * session, possibly to the log as well.
164 tprintf(struct proc *p, int pri, const char *fmt, ...)
166 struct tty *tp = NULL;
169 struct putchar_arg pca;
170 struct session *sess = NULL;
177 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
184 if (ttycheckoutq(tp, 0))
195 kvprintf(fmt, putchar, &pca, 10, ap);
204 * Ttyprintf displays a message on a tty; it should be used only by
205 * the tty driver, or anything that knows the underlying tty will not
206 * be revoke(2)'d away. Other callers should use tprintf.
209 ttyprintf(struct tty *tp, const char *fmt, ...)
212 struct putchar_arg pca;
218 retval = kvprintf(fmt, putchar, &pca, 10, ap);
224 * Log writes to the log buffer, and guarantees not to sleep (so can be
225 * called by interrupt routines). If there is no process reading the
226 * log yet, it writes to the console also.
229 log(int level, const char *fmt, ...)
232 struct putchar_arg pca;
236 pca.flags = log_open ? TOLOG : TOCONS;
239 kvprintf(fmt, putchar, &pca, 10, ap);
245 #define CONSCHUNK 128
248 log_console(struct uio *uio)
254 if (!log_console_output)
257 pri = LOG_INFO | LOG_CONSOLE;
259 consbuffer = malloc(CONSCHUNK, M_TEMP, M_WAITOK);
262 while (uio->uio_resid > 0) {
263 c = imin(uio->uio_resid, CONSCHUNK);
264 error = uiomove(consbuffer, c, uio);
267 for (i = 0; i < c; i++) {
268 msglogchar(consbuffer[i], pri);
269 if (consbuffer[i] == '\n')
276 msglogchar('\n', pri);
279 free(consbuffer, M_TEMP);
284 printf(const char *fmt, ...)
288 struct putchar_arg pca;
291 savintr = consintr; /* disable interrupts */
295 pca.flags = TOCONS | TOLOG;
297 retval = kvprintf(fmt, putchar, &pca, 10, ap);
301 consintr = savintr; /* reenable interrupts */
306 vprintf(const char *fmt, va_list ap)
309 struct putchar_arg pca;
312 savintr = consintr; /* disable interrupts */
315 pca.flags = TOCONS | TOLOG;
317 retval = kvprintf(fmt, putchar, &pca, 10, ap);
320 consintr = savintr; /* reenable interrupts */
325 * Print a character on console or users terminal. If destination is
326 * the console then the last bunch of characters are saved in msgbuf for
330 putchar(int c, void *arg)
332 struct putchar_arg *ap = (struct putchar_arg*) arg;
333 struct tty *tp = ap->tty;
334 int consdirect, flags = ap->flags;
336 consdirect = ((flags & TOCONS) && constty == NULL);
337 /* Don't use the tty code after a panic or while in ddb. */
346 if ((flags & TOTTY) && tp != NULL)
348 if (flags & TOCONS) {
350 msgbuf_addchar(&consmsgbuf, c);
351 if (always_console_output && c != '\0')
356 msglogchar(c, ap->pri);
360 * Scaled down version of sprintf(3).
363 sprintf(char *buf, const char *cfmt, ...)
369 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
376 * Scaled down version of vsprintf(3).
379 vsprintf(char *buf, const char *cfmt, va_list ap)
383 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
389 * Scaled down version of snprintf(3).
392 snprintf(char *str, size_t size, const char *format, ...)
397 va_start(ap, format);
398 retval = vsnprintf(str, size, format, ap);
404 * Scaled down version of vsnprintf(3).
407 vsnprintf(char *str, size_t size, const char *format, va_list ap)
409 struct snprintf_arg info;
414 retval = kvprintf(format, snprintf_func, &info, 10, ap);
415 if (info.remain >= 1)
421 * Kernel version which takes radix argument vsnprintf(3).
424 vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
426 struct snprintf_arg info;
431 retval = kvprintf(format, snprintf_func, &info, radix, ap);
432 if (info.remain >= 1)
438 snprintf_func(int ch, void *arg)
440 struct snprintf_arg *const info = arg;
442 if (info->remain >= 2) {
449 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
450 * order; return an optional length and a pointer to the last character
451 * written in the buffer (i.e., the first character of the string).
452 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
455 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
462 c = hex2ascii(num % base);
463 *++p = upper ? toupper(c) : c;
464 } while (num /= base);
471 * Scaled down version of printf(3).
473 * Two additional formats:
475 * The format %b is supported to decode error registers.
478 * printf("reg=%b\n", regval, "<base><arg>*");
480 * where <base> is the output base expressed as a control character, e.g.
481 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
482 * the first of which gives the bit number to be inspected (origin 1), and
483 * the next characters (up to a control character, i.e. a character <= 32),
484 * give the name of the register. Thus:
486 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
488 * would produce output:
490 * reg=3<BITTWO,BITONE>
492 * XXX: %D -- Hexdump, takes pointer and separator string:
493 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
494 * ("%*D", len, ptr, " " -> XX XX XX XX ...
497 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
499 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
502 const char *p, *percent, *q;
506 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
507 int cflag, hflag, jflag, tflag, zflag;
510 int stop = 0, retval = 0;
519 fmt = "(fmt null)\n";
521 if (radix < 2 || radix > 36)
527 while ((ch = (u_char)*fmt++) != '%' || stop) {
533 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
534 sign = 0; dot = 0; dwidth = 0; upper = 0;
535 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
536 reswitch: switch (ch = (u_char)*fmt++) {
554 width = va_arg(ap, int);
560 dwidth = va_arg(ap, int);
568 case '1': case '2': case '3': case '4':
569 case '5': case '6': case '7': case '8': case '9':
570 for (n = 0;; ++fmt) {
571 n = n * 10 + ch - '0';
573 if (ch < '0' || ch > '9')
582 num = (u_int)va_arg(ap, int);
583 p = va_arg(ap, char *);
584 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
592 if (num & (1 << (n - 1))) {
593 PCHAR(tmp ? ',' : '<');
594 for (; (n = *p) > ' '; ++p)
598 for (; *p > ' '; ++p)
605 PCHAR(va_arg(ap, int));
608 up = va_arg(ap, u_char *);
609 p = va_arg(ap, char *);
613 PCHAR(hex2ascii(*up >> 4));
614 PCHAR(hex2ascii(*up & 0x0f));
645 *(va_arg(ap, intmax_t *)) = retval;
647 *(va_arg(ap, quad_t *)) = retval;
649 *(va_arg(ap, long *)) = retval;
651 *(va_arg(ap, size_t *)) = retval;
653 *(va_arg(ap, short *)) = retval;
655 *(va_arg(ap, char *)) = retval;
657 *(va_arg(ap, int *)) = retval;
664 sharpflag = (width == 0);
666 num = (uintptr_t)va_arg(ap, void *);
677 p = va_arg(ap, char *);
683 for (n = 0; n < dwidth && p[n]; n++)
688 if (!ladjust && width > 0)
693 if (ladjust && width > 0)
718 num = va_arg(ap, uintmax_t);
720 num = va_arg(ap, u_quad_t);
722 num = va_arg(ap, ptrdiff_t);
724 num = va_arg(ap, u_long);
726 num = va_arg(ap, size_t);
728 num = (u_short)va_arg(ap, int);
730 num = (u_char)va_arg(ap, int);
732 num = va_arg(ap, u_int);
736 num = va_arg(ap, intmax_t);
738 num = va_arg(ap, quad_t);
740 num = va_arg(ap, ptrdiff_t);
742 num = va_arg(ap, long);
744 num = va_arg(ap, size_t);
746 num = (short)va_arg(ap, int);
748 num = (char)va_arg(ap, int);
750 num = va_arg(ap, int);
752 if (sign && (intmax_t)num < 0) {
754 num = -(intmax_t)num;
756 p = ksprintn(nbuf, num, base, &tmp, upper);
757 if (sharpflag && num != 0) {
766 if (!ladjust && padc != '0' && width
767 && (width -= tmp) > 0)
772 if (sharpflag && num != 0) {
775 } else if (base == 16) {
780 if (!ladjust && width && (width -= tmp) > 0)
787 if (ladjust && width && (width -= tmp) > 0)
793 while (percent < fmt)
796 * Since we ignore an formatting argument it is no
797 * longer safe to obey the remaining formatting
798 * arguments as the arguments will no longer match
809 * Put character in log buffer with a particular priority.
812 msglogchar(int c, int pri)
814 static int lastpri = -1;
821 if (c == '\0' || c == '\r')
823 if (pri != -1 && pri != lastpri) {
825 msgbuf_addchar(msgbufp, '\n');
828 msgbuf_addchar(msgbufp, '<');
829 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;)
830 msgbuf_addchar(msgbufp, *p--);
831 msgbuf_addchar(msgbufp, '>');
834 msgbuf_addchar(msgbufp, c);
844 msgbufinit(void *ptr, int size)
847 static struct msgbuf *oldp = NULL;
849 size -= sizeof(*msgbufp);
851 msgbufp = (struct msgbuf *)(cp + size);
852 msgbuf_reinit(msgbufp, cp, size);
853 if (msgbufmapped && oldp != msgbufp)
854 msgbuf_copy(oldp, msgbufp);
859 static int unprivileged_read_msgbuf = 1;
860 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf,
861 CTLFLAG_RW, &unprivileged_read_msgbuf, 0,
862 "Unprivileged processes may read the kernel message buffer");
864 /* Sysctls for accessing/clearing the msgbuf */
866 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
872 if (!unprivileged_read_msgbuf) {
873 error = suser(req->td);
878 /* Read the whole buffer, one chunk at a time. */
879 msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
880 while ((len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq)) > 0) {
881 error = sysctl_handle_opaque(oidp, buf, len, req);
888 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
889 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
891 static int msgbuf_clearflag;
894 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
897 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
898 if (!error && req->newptr) {
899 msgbuf_clear(msgbufp);
900 msgbuf_clearflag = 0;
905 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
906 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clearflag, 0,
907 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
911 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
916 db_printf("msgbuf not mapped yet\n");
919 db_printf("msgbufp = %p\n", msgbufp);
920 db_printf("magic = %x, size = %d, r= %u, w = %u, ptr = %p, cksum= %u\n",
921 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_rseq,
922 msgbufp->msg_wseq, msgbufp->msg_ptr, msgbufp->msg_cksum);
923 for (i = 0; i < msgbufp->msg_size && !db_pager_quit; i++) {
924 j = MSGBUF_SEQ_TO_POS(msgbufp, i + msgbufp->msg_rseq);
925 db_printf("%c", msgbufp->msg_ptr[j]);
933 hexdump(const void *ptr, int length, const char *hdr, int flags)
937 const unsigned char *cp;
940 if ((flags & HD_DELIM_MASK) != 0)
941 delim = (flags & HD_DELIM_MASK) >> 8;
945 if ((flags & HD_COLUMN_MASK) != 0)
946 cols = flags & HD_COLUMN_MASK;
951 for (i = 0; i < length; i+= cols) {
955 if ((flags & HD_OMIT_COUNT) == 0)
958 if ((flags & HD_OMIT_HEX) == 0) {
959 for (j = 0; j < cols; j++) {
962 printf("%c%02x", delim, cp[k]);
968 if ((flags & HD_OMIT_CHARS) == 0) {
970 for (j = 0; j < cols; j++) {
974 else if (cp[k] >= ' ' && cp[k] <= '~')