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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/subr_prf.c,v 1.61.2.5 2002/08/31 18:22:08 dwmalone Exp $
40 * $DragonFly: src/sys/kern/subr_prf.c,v 1.6 2003/08/26 21:09:02 rob Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/msgbuf.h>
47 #include <sys/malloc.h>
50 #include <sys/tprintf.h>
51 #include <sys/syslog.h>
54 #include <sys/sysctl.h>
58 * Note that stdarg.h and the ANSI style va_start macro is used for both
59 * ANSI and traditional C compilers.
61 #include <machine/stdarg.h>
67 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
68 #define MAXNBUF (sizeof(quad_t) * NBBY + 1)
83 struct tty *constty; /* pointer to console "window" tty */
85 static void (*v_putc)(int) = cnputc; /* routine to putc on virtual console */
86 static void msglogchar(int c, int pri);
87 static void msgaddchar(int c, void *dummy);
88 static void putchar (int ch, void *arg);
89 static char *ksprintn (char *nbuf, u_long num, int base, int *len);
90 static char *ksprintqn (char *nbuf, u_quad_t num, int base, int *len);
91 static void snprintf_func (int ch, void *arg);
93 static int consintr = 1; /* Ok to handle console interrupts? */
94 static int msgbufmapped; /* Set when safe to use msgbuf */
97 static int log_console_output = 1;
98 TUNABLE_INT("kern.log_console_output", &log_console_output);
99 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
100 &log_console_output, 0, "");
103 * Warn that a system table is full.
106 tablefull(const char *tab)
109 log(LOG_ERR, "%s: table is full\n", tab);
113 * Uprintf prints to the controlling terminal for the current process.
114 * It may block if the tty queue is overfull. No message is printed if
115 * the queue does not clear in a reasonable time.
118 uprintf(const char *fmt, ...)
120 struct proc *p = curproc;
122 struct putchar_arg pca;
125 if (p && p->p_flag & P_CONTROLT &&
126 p->p_session->s_ttyvp) {
128 pca.tty = p->p_session->s_ttyp;
131 retval = kvprintf(fmt, putchar, &pca, 10, ap);
138 tprintf_open(struct proc *p)
141 if ((p->p_flag & P_CONTROLT) && p->p_session->s_ttyvp) {
142 SESSHOLD(p->p_session);
143 return ((tpr_t) p->p_session);
145 return ((tpr_t) NULL);
149 tprintf_close(tpr_t sess)
152 SESSRELE((struct session *) sess);
156 * tprintf prints on the controlling terminal associated
157 * with the given session.
160 tprintf(tpr_t tpr, const char *fmt, ...)
162 struct session *sess = (struct session *)tpr;
163 struct tty *tp = NULL;
166 struct putchar_arg pca;
169 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
177 retval = kvprintf(fmt, putchar, &pca, 10, ap);
184 * Ttyprintf displays a message on a tty; it should be used only by
185 * the tty driver, or anything that knows the underlying tty will not
186 * be revoke(2)'d away. Other callers should use tprintf.
189 ttyprintf(struct tty *tp, const char *fmt, ...)
192 struct putchar_arg pca;
198 retval = kvprintf(fmt, putchar, &pca, 10, ap);
204 * Log writes to the log buffer, and guarantees not to sleep (so can be
205 * called by interrupt routines). If there is no process reading the
206 * log yet, it writes to the console also.
209 log(int level, const char *fmt, ...)
213 struct putchar_arg pca;
217 pca.flags = log_open ? TOLOG : TOCONS;
220 retval = kvprintf(fmt, putchar, &pca, 10, ap);
228 addlog(const char *fmt, ...)
232 struct putchar_arg pca;
236 pca.flags = log_open ? TOLOG : TOCONS;
239 retval = kvprintf(fmt, putchar, &pca, 10, ap);
246 #define CONSCHUNK 128
249 log_console(struct uio *uio)
251 int c, i, error, iovlen, nl;
253 struct iovec *miov = NULL;
257 if (!log_console_output)
260 pri = LOG_INFO | LOG_CONSOLE;
262 iovlen = uio->uio_iovcnt * sizeof (struct iovec);
263 MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
264 MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK);
265 bcopy((caddr_t)muio.uio_iov, (caddr_t)miov, iovlen);
270 while (uio->uio_resid > 0) {
271 c = imin(uio->uio_resid, CONSCHUNK);
272 error = uiomove(consbuffer, c, uio);
275 for (i = 0; i < c; i++) {
276 msglogchar(consbuffer[i], pri);
277 if (consbuffer[i] == '\n')
284 msglogchar('\n', pri);
287 FREE(consbuffer, M_TEMP);
292 printf(const char *fmt, ...)
296 struct putchar_arg pca;
299 savintr = consintr; /* disable interrupts */
303 pca.flags = TOCONS | TOLOG;
306 retval = kvprintf(fmt, putchar, &pca, 10, ap);
311 consintr = savintr; /* reenable interrupts */
316 vprintf(const char *fmt, va_list ap)
319 struct putchar_arg pca;
322 savintr = consintr; /* disable interrupts */
325 pca.flags = TOCONS | TOLOG;
328 retval = kvprintf(fmt, putchar, &pca, 10, ap);
332 consintr = savintr; /* reenable interrupts */
337 * Print a character on console or users terminal. If destination is
338 * the console then the last bunch of characters are saved in msgbuf for
342 putchar(int c, void *arg)
344 struct putchar_arg *ap = (struct putchar_arg*) arg;
345 int flags = ap->flags;
346 struct tty *tp = ap->tty;
349 if ((flags & TOCONS) && tp == NULL && constty) {
353 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
354 (flags & TOCONS) && tp == constty)
357 msglogchar(c, ap->pri);
358 if ((flags & TOCONS) && constty == NULL && c != '\0')
363 * Scaled down version of sprintf(3).
366 sprintf(char *buf, const char *cfmt, ...)
372 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
379 * Scaled down version of vsprintf(3).
382 vsprintf(char *buf, const char *cfmt, va_list ap)
386 retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
392 * Scaled down version of snprintf(3).
395 snprintf(char *str, size_t size, const char *format, ...)
400 va_start(ap, format);
401 retval = vsnprintf(str, size, format, ap);
407 * Scaled down version of vsnprintf(3).
410 vsnprintf(char *str, size_t size, const char *format, va_list ap)
412 struct snprintf_arg info;
417 retval = kvprintf(format, snprintf_func, &info, 10, ap);
418 if (info.remain >= 1)
424 snprintf_func(int ch, void *arg)
426 struct snprintf_arg *const info = arg;
428 if (info->remain >= 2) {
435 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
436 * order; return an optional length and a pointer to the last character
437 * written in the buffer (i.e., the first character of the string).
438 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
441 ksprintn(nbuf, ul, base, lenp)
451 *++p = hex2ascii(ul % base);
452 } while (ul /= base);
457 /* ksprintn, but for a quad_t. */
459 ksprintqn(nbuf, uq, base, lenp)
469 *++p = hex2ascii(uq % base);
470 } while (uq /= base);
477 * Scaled down version of printf(3).
479 * Two additional formats:
481 * The format %b is supported to decode error registers.
484 * printf("reg=%b\n", regval, "<base><arg>*");
486 * where <base> is the output base expressed as a control character, e.g.
487 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
488 * the first of which gives the bit number to be inspected (origin 1), and
489 * the next characters (up to a control character, i.e. a character <= 32),
490 * give the name of the register. Thus:
492 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
494 * would produce output:
496 * reg=3<BITTWO,BITONE>
498 * XXX: %D -- Hexdump, takes pointer and separator string:
499 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
500 * ("%*D", len, ptr, " " -> XX XX XX XX ...
503 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
505 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
512 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
525 fmt = "(fmt null)\n";
527 if (radix < 2 || radix > 36)
533 while ((ch = (u_char)*fmt++) != '%') {
538 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
539 sign = 0; dot = 0; dwidth = 0;
540 reswitch: switch (ch = (u_char)*fmt++) {
558 width = va_arg(ap, int);
564 dwidth = va_arg(ap, int);
572 case '1': case '2': case '3': case '4':
573 case '5': case '6': case '7': case '8': case '9':
574 for (n = 0;; ++fmt) {
575 n = n * 10 + ch - '0';
577 if (ch < '0' || ch > '9')
586 ul = va_arg(ap, int);
587 p = va_arg(ap, char *);
588 for (q = ksprintn(nbuf, ul, *p++, NULL); *q;)
596 if (ul & (1 << (n - 1))) {
597 PCHAR(tmp ? ',' : '<');
598 for (; (n = *p) > ' '; ++p)
602 for (; *p > ' '; ++p)
609 PCHAR(va_arg(ap, int));
612 up = va_arg(ap, u_char *);
613 p = va_arg(ap, char *);
617 PCHAR(hex2ascii(*up >> 4));
618 PCHAR(hex2ascii(*up & 0x0f));
627 uq = va_arg(ap, quad_t);
629 ul = va_arg(ap, long);
631 ul = va_arg(ap, int);
644 uq = va_arg(ap, u_quad_t);
646 ul = va_arg(ap, u_long);
648 ul = va_arg(ap, u_int);
652 ul = (uintptr_t)va_arg(ap, void *);
654 sharpflag = (width == 0);
662 uq = va_arg(ap, u_quad_t);
664 ul = va_arg(ap, u_long);
667 (u_long)va_arg(ap, int) : va_arg(ap, u_int);
671 p = va_arg(ap, char *);
677 for (n = 0; n < dwidth && p[n]; n++)
682 if (!ladjust && width > 0)
687 if (ladjust && width > 0)
693 uq = va_arg(ap, u_quad_t);
695 ul = va_arg(ap, u_long);
697 ul = va_arg(ap, u_int);
703 uq = va_arg(ap, u_quad_t);
705 ul = va_arg(ap, u_long);
707 ul = va_arg(ap, u_int);
712 uq = va_arg(ap, u_quad_t);
714 ul = va_arg(ap, u_long);
717 (u_long)va_arg(ap, int) : va_arg(ap, u_int);
723 if (sign && (quad_t)uq < 0) {
727 p = ksprintqn(nbuf, uq, base, &tmp);
729 if (sign && (long)ul < 0) {
733 p = ksprintn(nbuf, ul, base, &tmp);
735 if (sharpflag && (qflag ? uq != 0 : ul != 0)) {
744 if (!ladjust && width && (width -= tmp) > 0)
749 if (sharpflag && (qflag ? uq != 0 : ul != 0)) {
752 } else if (base == 16) {
761 if (ladjust && width && (width -= tmp) > 0)
778 * Put character in log buffer with a particular priority.
781 msglogchar(int c, int pri)
783 static int lastpri = -1;
790 if (c == '\0' || c == '\r')
792 if (pri != -1 && pri != lastpri) {
794 msgaddchar('\n', NULL);
797 msgaddchar('<', NULL);
798 for (p = ksprintn(nbuf, (u_long)pri, 10, NULL); *p;)
799 msgaddchar(*p--, NULL);
800 msgaddchar('>', NULL);
813 * Put char in log buffer
816 msgaddchar(int c, void *dummy)
823 mbp->msg_ptr[mbp->msg_bufx++] = c;
824 if (mbp->msg_bufx >= mbp->msg_size)
826 /* If the buffer is full, keep the most recent data. */
827 if (mbp->msg_bufr == mbp->msg_bufx) {
828 if (++mbp->msg_bufr >= mbp->msg_size)
834 msgbufcopy(struct msgbuf *oldp)
838 pos = oldp->msg_bufr;
839 while (pos != oldp->msg_bufx) {
840 msglogchar(oldp->msg_ptr[pos], -1);
841 if (++pos >= oldp->msg_size)
847 msgbufinit(void *ptr, size_t size)
850 static struct msgbuf *oldp = NULL;
852 size -= sizeof(*msgbufp);
854 msgbufp = (struct msgbuf *) (cp + size);
855 if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size ||
856 msgbufp->msg_bufx >= size || msgbufp->msg_bufr >= size) {
858 bzero(msgbufp, sizeof(*msgbufp));
859 msgbufp->msg_magic = MSG_MAGIC;
860 msgbufp->msg_size = (char *)msgbufp - cp;
862 msgbufp->msg_ptr = cp;
863 if (msgbufmapped && oldp != msgbufp)
869 /* Sysctls for accessing/clearing the msgbuf */
871 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
876 * Unwind the buffer, so that it's linear (possibly starting with
877 * some initial nulls).
879 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx,
880 msgbufp->msg_size - msgbufp->msg_bufx, req);
883 if (msgbufp->msg_bufx > 0) {
884 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr,
885 msgbufp->msg_bufx, req);
890 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
891 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
893 static int msgbuf_clear;
896 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
899 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
900 if (!error && req->newptr) {
901 /* Clear the buffer and reset write pointer */
902 bzero(msgbufp->msg_ptr, msgbufp->msg_size);
903 msgbufp->msg_bufr = msgbufp->msg_bufx = 0;
909 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
910 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0,
911 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
917 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
922 db_printf("msgbuf not mapped yet\n");
925 db_printf("msgbufp = %p\n", msgbufp);
926 db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
927 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr,
928 msgbufp->msg_bufx, msgbufp->msg_ptr);
929 for (i = 0; i < msgbufp->msg_size; i++) {
930 j = (i + msgbufp->msg_bufr) % msgbufp->msg_size;
931 db_printf("%c", msgbufp->msg_ptr[j]);