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.2 2003/06/17 04:28:41 dillon 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>
57 * Note that stdarg.h and the ANSI style va_start macro is used for both
58 * ANSI and traditional C compilers.
60 #include <machine/stdarg.h>
66 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
67 #define MAXNBUF (sizeof(quad_t) * NBBY + 1)
82 struct tty *constty; /* pointer to console "window" tty */
84 static void (*v_putc)(int) = cnputc; /* routine to putc on virtual console */
85 static void msglogchar(int c, int pri);
86 static void msgaddchar(int c, void *dummy);
87 static void putchar __P((int ch, void *arg));
88 static char *ksprintn __P((char *nbuf, u_long num, int base, int *len));
89 static char *ksprintqn __P((char *nbuf, u_quad_t num, int base, int *len));
90 static void snprintf_func __P((int ch, void *arg));
92 static int consintr = 1; /* Ok to handle console interrupts? */
93 static int msgbufmapped; /* Set when safe to use msgbuf */
96 static int log_console_output = 1;
97 TUNABLE_INT("kern.log_console_output", &log_console_output);
98 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
99 &log_console_output, 0, "");
102 * Warn that a system table is full.
105 tablefull(const char *tab)
108 log(LOG_ERR, "%s: table is full\n", tab);
112 * Uprintf prints to the controlling terminal for the current process.
113 * It may block if the tty queue is overfull. No message is printed if
114 * the queue does not clear in a reasonable time.
117 uprintf(const char *fmt, ...)
119 struct proc *p = curproc;
121 struct putchar_arg pca;
124 if (p && p->p_flag & P_CONTROLT &&
125 p->p_session->s_ttyvp) {
127 pca.tty = p->p_session->s_ttyp;
129 retval = kvprintf(fmt, putchar, &pca, 10, ap);
137 register struct proc *p;
140 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
141 SESSHOLD(p->p_session);
142 return ((tpr_t) p->p_session);
144 return ((tpr_t) NULL);
153 SESSRELE((struct session *) sess);
157 * tprintf prints on the controlling terminal associated
158 * with the given session.
161 tprintf(tpr_t tpr, const char *fmt, ...)
163 register struct session *sess = (struct session *)tpr;
164 struct tty *tp = NULL;
167 struct putchar_arg pca;
170 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
178 retval = kvprintf(fmt, putchar, &pca, 10, ap);
185 * Ttyprintf displays a message on a tty; it should be used only by
186 * the tty driver, or anything that knows the underlying tty will not
187 * be revoke(2)'d away. Other callers should use tprintf.
190 ttyprintf(struct tty *tp, const char *fmt, ...)
193 struct putchar_arg pca;
199 retval = kvprintf(fmt, putchar, &pca, 10, ap);
205 * Log writes to the log buffer, and guarantees not to sleep (so can be
206 * called by interrupt routines). If there is no process reading the
207 * log yet, it writes to the console also.
210 log(int level, const char *fmt, ...)
214 struct putchar_arg pca;
218 pca.flags = log_open ? TOLOG : TOCONS;
221 retval = kvprintf(fmt, putchar, &pca, 10, ap);
229 addlog(const char *fmt, ...)
233 struct putchar_arg pca;
237 pca.flags = log_open ? TOLOG : TOCONS;
240 retval = kvprintf(fmt, putchar, &pca, 10, ap);
247 #define CONSCHUNK 128
250 log_console(struct uio *uio)
252 int c, i, error, iovlen, nl;
254 struct iovec *miov = NULL;
258 if (!log_console_output)
261 pri = LOG_INFO | LOG_CONSOLE;
263 iovlen = uio->uio_iovcnt * sizeof (struct iovec);
264 MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
265 MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK);
266 bcopy((caddr_t)muio.uio_iov, (caddr_t)miov, iovlen);
271 while (uio->uio_resid > 0) {
272 c = imin(uio->uio_resid, CONSCHUNK);
273 error = uiomove(consbuffer, c, uio);
276 for (i = 0; i < c; i++) {
277 msglogchar(consbuffer[i], pri);
278 if (consbuffer[i] == '\n')
285 msglogchar('\n', pri);
288 FREE(consbuffer, M_TEMP);
293 printf(const char *fmt, ...)
297 struct putchar_arg pca;
300 savintr = consintr; /* disable interrupts */
304 pca.flags = TOCONS | TOLOG;
306 retval = kvprintf(fmt, putchar, &pca, 10, ap);
310 consintr = savintr; /* reenable interrupts */
315 vprintf(const char *fmt, va_list ap)
318 struct putchar_arg pca;
321 savintr = consintr; /* disable interrupts */
324 pca.flags = TOCONS | TOLOG;
326 retval = kvprintf(fmt, putchar, &pca, 10, ap);
329 consintr = savintr; /* reenable interrupts */
334 * Print a character on console or users terminal. If destination is
335 * the console then the last bunch of characters are saved in msgbuf for
339 putchar(int c, void *arg)
341 struct putchar_arg *ap = (struct putchar_arg*) arg;
342 int flags = ap->flags;
343 struct tty *tp = ap->tty;
346 if ((flags & TOCONS) && tp == NULL && constty) {
350 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
351 (flags & TOCONS) && tp == constty)
354 msglogchar(c, ap->pri);
355 if ((flags & TOCONS) && constty == NULL && c != '\0')
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 snprintf_func(int ch, void *arg)
423 struct snprintf_arg *const info = arg;
425 if (info->remain >= 2) {
432 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
433 * order; return an optional length and a pointer to the last character
434 * written in the buffer (i.e., the first character of the string).
435 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
438 ksprintn(nbuf, ul, base, lenp)
448 *++p = hex2ascii(ul % base);
449 } while (ul /= base);
454 /* ksprintn, but for a quad_t. */
456 ksprintqn(nbuf, uq, base, lenp)
466 *++p = hex2ascii(uq % base);
467 } while (uq /= base);
474 * Scaled down version of printf(3).
476 * Two additional formats:
478 * The format %b is supported to decode error registers.
481 * printf("reg=%b\n", regval, "<base><arg>*");
483 * where <base> is the output base expressed as a control character, e.g.
484 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
485 * the first of which gives the bit number to be inspected (origin 1), and
486 * the next characters (up to a control character, i.e. a character <= 32),
487 * give the name of the register. Thus:
489 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
491 * would produce output:
493 * reg=3<BITTWO,BITONE>
495 * XXX: %D -- Hexdump, takes pointer and separator string:
496 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
497 * ("%*D", len, ptr, " " -> XX XX XX XX ...
500 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
502 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
509 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
522 fmt = "(fmt null)\n";
524 if (radix < 2 || radix > 36)
530 while ((ch = (u_char)*fmt++) != '%') {
535 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
536 sign = 0; dot = 0; dwidth = 0;
537 reswitch: switch (ch = (u_char)*fmt++) {
555 width = va_arg(ap, int);
561 dwidth = va_arg(ap, int);
569 case '1': case '2': case '3': case '4':
570 case '5': case '6': case '7': case '8': case '9':
571 for (n = 0;; ++fmt) {
572 n = n * 10 + ch - '0';
574 if (ch < '0' || ch > '9')
583 ul = va_arg(ap, int);
584 p = va_arg(ap, char *);
585 for (q = ksprintn(nbuf, ul, *p++, NULL); *q;)
593 if (ul & (1 << (n - 1))) {
594 PCHAR(tmp ? ',' : '<');
595 for (; (n = *p) > ' '; ++p)
599 for (; *p > ' '; ++p)
606 PCHAR(va_arg(ap, int));
609 up = va_arg(ap, u_char *);
610 p = va_arg(ap, char *);
614 PCHAR(hex2ascii(*up >> 4));
615 PCHAR(hex2ascii(*up & 0x0f));
624 uq = va_arg(ap, quad_t);
626 ul = va_arg(ap, long);
628 ul = va_arg(ap, int);
641 uq = va_arg(ap, u_quad_t);
643 ul = va_arg(ap, u_long);
645 ul = va_arg(ap, u_int);
649 ul = (uintptr_t)va_arg(ap, void *);
651 sharpflag = (width == 0);
659 uq = va_arg(ap, u_quad_t);
661 ul = va_arg(ap, u_long);
664 (u_long)va_arg(ap, int) : va_arg(ap, u_int);
668 p = va_arg(ap, char *);
674 for (n = 0; n < dwidth && p[n]; n++)
679 if (!ladjust && width > 0)
684 if (ladjust && width > 0)
690 uq = va_arg(ap, u_quad_t);
692 ul = va_arg(ap, u_long);
694 ul = va_arg(ap, u_int);
700 uq = va_arg(ap, u_quad_t);
702 ul = va_arg(ap, u_long);
704 ul = va_arg(ap, u_int);
709 uq = va_arg(ap, u_quad_t);
711 ul = va_arg(ap, u_long);
714 (u_long)va_arg(ap, int) : va_arg(ap, u_int);
720 if (sign && (quad_t)uq < 0) {
724 p = ksprintqn(nbuf, uq, base, &tmp);
726 if (sign && (long)ul < 0) {
730 p = ksprintn(nbuf, ul, base, &tmp);
732 if (sharpflag && (qflag ? uq != 0 : ul != 0)) {
741 if (!ladjust && width && (width -= tmp) > 0)
746 if (sharpflag && (qflag ? uq != 0 : ul != 0)) {
749 } else if (base == 16) {
758 if (ladjust && width && (width -= tmp) > 0)
775 * Put character in log buffer with a particular priority.
778 msglogchar(int c, int pri)
780 static int lastpri = -1;
787 if (c == '\0' || c == '\r')
789 if (pri != -1 && pri != lastpri) {
791 msgaddchar('\n', NULL);
794 msgaddchar('<', NULL);
795 for (p = ksprintn(nbuf, (u_long)pri, 10, NULL); *p;)
796 msgaddchar(*p--, NULL);
797 msgaddchar('>', NULL);
810 * Put char in log buffer
813 msgaddchar(int c, void *dummy)
820 mbp->msg_ptr[mbp->msg_bufx++] = c;
821 if (mbp->msg_bufx >= mbp->msg_size)
823 /* If the buffer is full, keep the most recent data. */
824 if (mbp->msg_bufr == mbp->msg_bufx) {
825 if (++mbp->msg_bufr >= mbp->msg_size)
831 msgbufcopy(struct msgbuf *oldp)
835 pos = oldp->msg_bufr;
836 while (pos != oldp->msg_bufx) {
837 msglogchar(oldp->msg_ptr[pos], -1);
838 if (++pos >= oldp->msg_size)
844 msgbufinit(void *ptr, size_t size)
847 static struct msgbuf *oldp = NULL;
849 size -= sizeof(*msgbufp);
851 msgbufp = (struct msgbuf *) (cp + size);
852 if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size ||
853 msgbufp->msg_bufx >= size || msgbufp->msg_bufr >= size) {
855 bzero(msgbufp, sizeof(*msgbufp));
856 msgbufp->msg_magic = MSG_MAGIC;
857 msgbufp->msg_size = (char *)msgbufp - cp;
859 msgbufp->msg_ptr = cp;
860 if (msgbufmapped && oldp != msgbufp)
866 /* Sysctls for accessing/clearing the msgbuf */
868 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
873 * Unwind the buffer, so that it's linear (possibly starting with
874 * some initial nulls).
876 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr + msgbufp->msg_bufx,
877 msgbufp->msg_size - msgbufp->msg_bufx, req);
880 if (msgbufp->msg_bufx > 0) {
881 error = sysctl_handle_opaque(oidp, msgbufp->msg_ptr,
882 msgbufp->msg_bufx, 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_clear;
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 /* Clear the buffer and reset write pointer */
899 bzero(msgbufp->msg_ptr, msgbufp->msg_size);
900 msgbufp->msg_bufr = msgbufp->msg_bufx = 0;
906 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
907 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0,
908 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
914 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
919 db_printf("msgbuf not mapped yet\n");
922 db_printf("msgbufp = %p\n", msgbufp);
923 db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
924 msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr,
925 msgbufp->msg_bufx, msgbufp->msg_ptr);
926 for (i = 0; i < msgbufp->msg_size; i++) {
927 j = (i + msgbufp->msg_bufr) % msgbufp->msg_size;
928 db_printf("%c", msgbufp->msg_ptr[j]);