kernel: Nuke legacy FreeBSD "%r" format support.
[dragonfly.git] / sys / kern / subr_prf.c
blob21b2dc3cc39e6c664ff0fb40cde3aa3b4e5ee658
1 /*-
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
12 * are met:
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. 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
32 * SUCH DAMAGE.
34 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
35 * $FreeBSD: src/sys/kern/subr_prf.c,v 1.61.2.5 2002/08/31 18:22:08 dwmalone Exp $
38 #include "opt_ddb.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/msgbuf.h>
44 #include <sys/malloc.h>
45 #include <sys/proc.h>
46 #include <sys/priv.h>
47 #include <sys/tty.h>
48 #include <sys/tprintf.h>
49 #include <sys/stdint.h>
50 #include <sys/syslog.h>
51 #include <sys/cons.h>
52 #include <sys/uio.h>
53 #include <sys/sysctl.h>
54 #include <sys/lock.h>
55 #include <sys/ctype.h>
56 #include <sys/eventhandler.h>
57 #include <sys/kthread.h>
58 #include <sys/cpu_topology.h>
60 #include <sys/thread2.h>
61 #include <sys/spinlock2.h>
63 #ifdef DDB
64 #include <ddb/ddb.h>
65 #endif
68 * Note that stdarg.h and the ANSI style va_start macro is used for both
69 * ANSI and traditional C compilers. We use the __ machine version to stay
70 * within the kernel header file set.
72 #include <machine/stdarg.h>
74 #define TOCONS 0x01
75 #define TOTTY 0x02
76 #define TOLOG 0x04
77 #define TOWAKEUP 0x08
78 #define TONOSPIN 0x10 /* avoid serialization */
80 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
81 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
83 struct putchar_arg {
84 int flags;
85 int pri;
86 struct tty *tty;
89 struct snprintf_arg {
90 char *str;
91 size_t remain;
94 extern int log_open;
96 struct tty *constty; /* pointer to console "window" tty */
98 static void msglogchar(int c, int pri);
99 static void msgaddchar(int c, void *dummy);
100 static void kputchar (int ch, void *arg);
101 static char *ksprintn (char *nbuf, uintmax_t num, int base, int *lenp,
102 int upper);
103 static void snprintf_func (int ch, void *arg);
105 static int consintr = 1; /* Ok to handle console interrupts? */
106 static int msgbufmapped; /* Set when safe to use msgbuf */
107 static struct spinlock cons_spin = SPINLOCK_INITIALIZER(cons_spin, "cons_spin");
108 static thread_t constty_td = NULL;
110 int msgbuftrigger;
112 static int log_console_output = 1;
113 TUNABLE_INT("kern.log_console_output", &log_console_output);
114 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
115 &log_console_output, 0, "");
116 static int kprintf_logging = TOLOG | TOCONS;
117 SYSCTL_INT(_kern, OID_AUTO, kprintf_logging, CTLFLAG_RW,
118 &kprintf_logging, 0, "");
120 static int unprivileged_read_msgbuf = 1;
121 SYSCTL_INT(_security, OID_AUTO, unprivileged_read_msgbuf, CTLFLAG_RW,
122 &unprivileged_read_msgbuf, 0,
123 "Unprivileged processes may read the kernel message buffer");
126 * Warn that a system table is full.
128 void
129 tablefull(const char *tab)
132 log(LOG_ERR, "%s: table is full\n", tab);
136 * Uprintf prints to the controlling terminal for the current process.
139 uprintf(const char *fmt, ...)
141 struct proc *p = curproc;
142 __va_list ap;
143 struct putchar_arg pca;
144 int retval = 0;
146 if (p && (p->p_flags & P_CONTROLT) && p->p_session->s_ttyvp) {
147 __va_start(ap, fmt);
148 pca.tty = p->p_session->s_ttyp;
149 pca.flags = TOTTY;
151 retval = kvcprintf(fmt, kputchar, &pca, ap);
152 __va_end(ap);
154 return (retval);
157 tpr_t
158 tprintf_open(struct proc *p)
160 if ((p->p_flags & P_CONTROLT) && p->p_session->s_ttyvp) {
161 sess_hold(p->p_session);
162 return ((tpr_t) p->p_session);
164 return (NULL);
167 void
168 tprintf_close(tpr_t sess)
170 if (sess)
171 sess_rele((struct session *) sess);
175 * tprintf prints on the controlling terminal associated
176 * with the given session.
179 tprintf(tpr_t tpr, const char *fmt, ...)
181 struct session *sess = (struct session *)tpr;
182 struct tty *tp = NULL;
183 int flags = TOLOG;
184 __va_list ap;
185 struct putchar_arg pca;
186 int retval;
188 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
189 flags |= TOTTY;
190 tp = sess->s_ttyp;
192 __va_start(ap, fmt);
193 pca.tty = tp;
194 pca.flags = flags;
195 pca.pri = LOG_INFO;
196 retval = kvcprintf(fmt, kputchar, &pca, ap);
197 __va_end(ap);
198 msgbuftrigger = 1;
199 return (retval);
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, ...)
210 __va_list ap;
211 struct putchar_arg pca;
212 int retval;
214 __va_start(ap, fmt);
215 pca.tty = tp;
216 pca.flags = TOTTY;
217 retval = kvcprintf(fmt, kputchar, &pca, ap);
218 __va_end(ap);
219 return (retval);
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, ...)
230 __va_list ap;
231 int retval;
232 struct putchar_arg pca;
234 pca.tty = NULL;
235 pca.pri = level;
236 if ((kprintf_logging & TOCONS) == 0 || log_open)
237 pca.flags = TOLOG;
238 else
239 pca.flags = TOCONS;
241 __va_start(ap, fmt);
242 retval = kvcprintf(fmt, kputchar, &pca, ap);
243 __va_end(ap);
245 msgbuftrigger = 1;
246 return (retval);
249 #define CONSCHUNK 128
251 void
252 log_console(struct uio *uio)
254 int c, i, error, iovlen, nl;
255 struct uio muio;
256 struct iovec *miov = NULL;
257 char *consbuffer;
258 int pri;
260 if (!log_console_output)
261 return;
263 pri = LOG_INFO | LOG_CONSOLE;
264 muio = *uio;
265 iovlen = uio->uio_iovcnt * sizeof (struct iovec);
266 miov = kmalloc(iovlen, M_TEMP, M_WAITOK);
267 consbuffer = kmalloc(CONSCHUNK, M_TEMP, M_WAITOK);
268 bcopy((caddr_t)muio.uio_iov, (caddr_t)miov, iovlen);
269 muio.uio_iov = miov;
270 uio = &muio;
272 nl = 0;
273 while (uio->uio_resid > 0) {
274 c = (int)szmin(uio->uio_resid, CONSCHUNK);
275 error = uiomove(consbuffer, (size_t)c, uio);
276 if (error != 0)
277 break;
278 for (i = 0; i < c; i++) {
279 msglogchar(consbuffer[i], pri);
280 if (consbuffer[i] == '\n')
281 nl = 1;
282 else
283 nl = 0;
286 if (!nl)
287 msglogchar('\n', pri);
288 msgbuftrigger = 1;
289 kfree(miov, M_TEMP);
290 kfree(consbuffer, M_TEMP);
291 return;
295 * Output to the console.
298 kprintf(const char *fmt, ...)
300 __va_list ap;
301 int savintr;
302 struct putchar_arg pca;
303 int retval;
305 savintr = consintr; /* disable interrupts */
306 consintr = 0;
307 __va_start(ap, fmt);
308 pca.tty = NULL;
309 pca.flags = kprintf_logging & ~TOTTY;
310 pca.pri = -1;
311 retval = kvcprintf(fmt, kputchar, &pca, ap);
312 __va_end(ap);
313 if (!panicstr)
314 msgbuftrigger = 1;
315 consintr = savintr; /* reenable interrupts */
316 return (retval);
320 kvprintf(const char *fmt, __va_list ap)
322 int savintr;
323 struct putchar_arg pca;
324 int retval;
326 savintr = consintr; /* disable interrupts */
327 consintr = 0;
328 pca.tty = NULL;
329 pca.flags = kprintf_logging & ~TOTTY;
330 pca.pri = -1;
331 retval = kvcprintf(fmt, kputchar, &pca, ap);
332 if (!panicstr)
333 msgbuftrigger = 1;
334 consintr = savintr; /* reenable interrupts */
335 return (retval);
339 * Limited rate kprintf. The passed rate structure must be initialized
340 * with the desired reporting frequency. A frequency of 0 will result in
341 * no output.
343 * count may be initialized to a negative number to allow an initial
344 * burst.
346 void
347 krateprintf(struct krate *rate, const char *fmt, ...)
349 __va_list ap;
351 if (rate->ticks != (int)time_uptime) {
352 rate->ticks = (int)time_uptime;
353 if (rate->count > 0)
354 rate->count = 0;
356 if (rate->count < rate->freq) {
357 ++rate->count;
358 __va_start(ap, fmt);
359 kvprintf(fmt, ap);
360 __va_end(ap);
365 * Print a character to the dmesg log, the console, and/or the user's
366 * terminal.
368 * NOTE: TOTTY does not require nonblocking operation, but TOCONS
369 * and TOLOG do. When we have a constty we still output to
370 * the real console but we have a monitoring thread which
371 * we wakeup which tracks the log.
373 static void
374 kputchar(int c, void *arg)
376 struct putchar_arg *ap = (struct putchar_arg*) arg;
377 int flags = ap->flags;
378 struct tty *tp = ap->tty;
380 if (panicstr)
381 constty = NULL;
382 if ((flags & TOCONS) && tp == NULL && constty)
383 flags |= TOLOG | TOWAKEUP;
384 if ((flags & TOTTY) && tputchar(c, tp) < 0)
385 ap->flags &= ~TOTTY;
386 if ((flags & TOLOG))
387 msglogchar(c, ap->pri);
388 if ((flags & TOCONS) && c)
389 cnputc(c);
390 if (flags & TOWAKEUP)
391 wakeup(constty_td);
395 * Scaled down version of sprintf(3).
398 ksprintf(char *buf, const char *cfmt, ...)
400 int retval;
401 __va_list ap;
403 __va_start(ap, cfmt);
404 retval = kvcprintf(cfmt, NULL, buf, ap);
405 buf[retval] = '\0';
406 __va_end(ap);
407 return (retval);
411 * Scaled down version of vsprintf(3).
414 kvsprintf(char *buf, const char *cfmt, __va_list ap)
416 int retval;
418 retval = kvcprintf(cfmt, NULL, buf, ap);
419 buf[retval] = '\0';
420 return (retval);
424 * Scaled down version of snprintf(3).
427 ksnprintf(char *str, size_t size, const char *format, ...)
429 int retval;
430 __va_list ap;
432 __va_start(ap, format);
433 retval = kvsnprintf(str, size, format, ap);
434 __va_end(ap);
435 return(retval);
439 * Scaled down version of vsnprintf(3).
442 kvsnprintf(char *str, size_t size, const char *format, __va_list ap)
444 struct snprintf_arg info;
445 int retval;
447 info.str = str;
448 info.remain = size;
449 retval = kvcprintf(format, snprintf_func, &info, ap);
450 if (info.remain >= 1)
451 *info.str++ = '\0';
452 return (retval);
456 kvasnprintf(char **strp, size_t size, const char *format, __va_list ap)
458 struct snprintf_arg info;
459 int retval;
461 *strp = kmalloc(size, M_TEMP, M_WAITOK);
462 info.str = *strp;
463 info.remain = size;
464 retval = kvcprintf(format, snprintf_func, &info, ap);
465 if (info.remain >= 1)
466 *info.str++ = '\0';
467 return (retval);
470 void
471 kvasfree(char **strp)
473 if (*strp) {
474 kfree(*strp, M_TEMP);
475 *strp = NULL;
479 static void
480 snprintf_func(int ch, void *arg)
482 struct snprintf_arg *const info = arg;
484 if (info->remain >= 2) {
485 *info->str++ = ch;
486 info->remain--;
491 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
492 * order; return an optional length and a pointer to the last character
493 * written in the buffer (i.e., the first character of the string).
494 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
496 static char *
497 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
499 char *p, c;
501 p = nbuf;
502 *p = '\0';
503 do {
504 c = hex2ascii(num % base);
505 *++p = upper ? toupper(c) : c;
506 } while (num /= base);
507 if (lenp)
508 *lenp = p - nbuf;
509 return (p);
513 * Scaled down version of printf(3).
515 * Two additional formats:
517 * The format %b is supported to decode error registers.
518 * Its usage is:
520 * kprintf("reg=%b\n", regval, "<base><arg>*");
522 * where <base> is the output base expressed as a control character, e.g.
523 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
524 * the first of which gives the bit number to be inspected (origin 1), and
525 * the next characters (up to a control character, i.e. a character <= 32),
526 * give the name of the register. Thus:
528 * kvcprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
530 * would produce output:
532 * reg=3<BITTWO,BITONE>
535 #define PCHAR(c) {int cc=(c); if(func) (*func)(cc,arg); else *d++=cc; retval++;}
538 kvcprintf(char const *fmt, void (*func)(int, void*), void *arg, __va_list ap)
540 char nbuf[MAXNBUF];
541 char *d;
542 const char *p, *percent, *q;
543 int ch, n;
544 uintmax_t num;
545 int base, tmp, width, ladjust, sharpflag, spaceflag, neg, sign, dot;
546 int cflag, hflag, jflag, lflag, qflag, tflag, zflag;
547 int dwidth, upper;
548 char padc;
549 int retval = 0, stop = 0;
550 int usespin;
553 * Make a supreme effort to avoid reentrant panics or deadlocks.
555 * NOTE! Do nothing that would access mycpu/gd/fs unless the
556 * function is the normal kputchar(), which allows us to
557 * use this function for very early debugging with a special
558 * function.
560 if (func == kputchar) {
561 if (mycpu->gd_flags & GDF_KPRINTF)
562 return(0);
563 atomic_set_long(&mycpu->gd_flags, GDF_KPRINTF);
566 num = 0;
567 if (!func)
568 d = (char *) arg;
569 else
570 d = NULL;
572 if (fmt == NULL)
573 fmt = "(fmt null)\n";
575 usespin = (func == kputchar &&
576 (kprintf_logging & TONOSPIN) == 0 &&
577 panic_cpu_gd != mycpu &&
578 (((struct putchar_arg *)arg)->flags & TOTTY) == 0);
579 if (usespin) {
580 crit_enter_hard();
581 spin_lock(&cons_spin);
584 for (;;) {
585 padc = ' ';
586 width = 0;
587 while ((ch = (u_char)*fmt++) != '%' || stop) {
588 if (ch == '\0')
589 goto done;
590 PCHAR(ch);
592 percent = fmt - 1;
593 dot = dwidth = ladjust = neg = sharpflag = sign = upper = 0;
594 spaceflag = 0;
595 cflag = hflag = jflag = lflag = qflag = tflag = zflag = 0;
597 reswitch:
598 switch (ch = (u_char)*fmt++) {
599 case ' ':
600 spaceflag = 1;
601 goto reswitch;
602 case '.':
603 dot = 1;
604 goto reswitch;
605 case '#':
606 sharpflag = 1;
607 goto reswitch;
608 case '+':
609 sign = 1;
610 goto reswitch;
611 case '-':
612 ladjust = 1;
613 goto reswitch;
614 case '%':
615 PCHAR(ch);
616 break;
617 case '*':
618 if (!dot) {
619 width = __va_arg(ap, int);
620 if (width < 0) {
621 ladjust = !ladjust;
622 width = -width;
624 } else {
625 dwidth = __va_arg(ap, int);
627 goto reswitch;
628 case '0':
629 if (!dot) {
630 padc = '0';
631 goto reswitch;
633 case '1': case '2': case '3': case '4':
634 case '5': case '6': case '7': case '8': case '9':
635 for (n = 0;; ++fmt) {
636 n = n * 10 + ch - '0';
637 ch = *fmt;
638 if (ch < '0' || ch > '9')
639 break;
641 if (dot)
642 dwidth = n;
643 else
644 width = n;
645 goto reswitch;
646 case 'b':
647 num = (u_int)__va_arg(ap, int);
648 p = __va_arg(ap, char *);
649 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
650 PCHAR(*q--);
652 if (num == 0)
653 break;
655 for (tmp = 0; *p;) {
656 n = *p++;
657 if (num & (1 << (n - 1))) {
658 PCHAR(tmp ? ',' : '<');
659 for (; (n = *p) > ' '; ++p)
660 PCHAR(n);
661 tmp = 1;
662 } else
663 for (; *p > ' '; ++p)
664 continue;
666 if (tmp)
667 PCHAR('>');
668 break;
669 case 'c':
670 PCHAR(__va_arg(ap, int));
671 break;
672 case 'd':
673 case 'i':
674 base = 10;
675 sign = 1;
676 goto handle_sign;
677 case 'h':
678 if (hflag) {
679 hflag = 0;
680 cflag = 1;
681 } else
682 hflag = 1;
683 goto reswitch;
684 case 'j':
685 jflag = 1;
686 goto reswitch;
687 case 'l':
688 if (lflag) {
689 lflag = 0;
690 qflag = 1;
691 } else
692 lflag = 1;
693 goto reswitch;
694 case 'n':
695 if (cflag)
696 *(__va_arg(ap, char *)) = retval;
697 else if (hflag)
698 *(__va_arg(ap, short *)) = retval;
699 else if (jflag)
700 *(__va_arg(ap, intmax_t *)) = retval;
701 else if (lflag)
702 *(__va_arg(ap, long *)) = retval;
703 else if (qflag)
704 *(__va_arg(ap, quad_t *)) = retval;
705 else
706 *(__va_arg(ap, int *)) = retval;
707 break;
708 case 'o':
709 base = 8;
710 goto handle_nosign;
711 case 'p':
712 base = 16;
713 sharpflag = (width == 0);
714 sign = 0;
715 num = (uintptr_t)__va_arg(ap, void *);
716 goto number;
717 case 'q':
718 qflag = 1;
719 goto reswitch;
720 case 's':
721 p = __va_arg(ap, char *);
722 if (p == NULL)
723 p = "(null)";
724 if (!dot)
725 n = strlen (p);
726 else
727 for (n = 0; n < dwidth && p[n]; n++)
728 continue;
730 width -= n;
732 if (!ladjust && width > 0)
733 while (width--)
734 PCHAR(padc);
735 while (n--)
736 PCHAR(*p++);
737 if (ladjust && width > 0)
738 while (width--)
739 PCHAR(padc);
740 break;
741 case 't':
742 tflag = 1;
743 goto reswitch;
744 case 'u':
745 base = 10;
746 goto handle_nosign;
747 case 'X':
748 upper = 1;
749 /* FALLTHROUGH */
750 case 'x':
751 base = 16;
752 goto handle_nosign;
753 case 'z':
754 zflag = 1;
755 goto reswitch;
756 handle_nosign:
757 sign = 0;
758 if (cflag)
759 num = (u_char)__va_arg(ap, int);
760 else if (hflag)
761 num = (u_short)__va_arg(ap, int);
762 else if (jflag)
763 num = __va_arg(ap, uintmax_t);
764 else if (lflag)
765 num = __va_arg(ap, u_long);
766 else if (qflag)
767 num = __va_arg(ap, u_quad_t);
768 else if (tflag)
769 num = __va_arg(ap, ptrdiff_t);
770 else if (zflag)
771 num = __va_arg(ap, size_t);
772 else
773 num = __va_arg(ap, u_int);
774 goto number;
775 handle_sign:
776 if (cflag)
777 num = (char)__va_arg(ap, int);
778 else if (hflag)
779 num = (short)__va_arg(ap, int);
780 else if (jflag)
781 num = __va_arg(ap, intmax_t);
782 else if (lflag)
783 num = __va_arg(ap, long);
784 else if (qflag)
785 num = __va_arg(ap, quad_t);
786 else if (tflag)
787 num = __va_arg(ap, ptrdiff_t);
788 else if (zflag)
789 num = __va_arg(ap, ssize_t);
790 else
791 num = __va_arg(ap, int);
792 number:
793 if (sign && (intmax_t)num < 0) {
794 neg = 1;
795 num = -(intmax_t)num;
797 p = ksprintn(nbuf, num, base, &n, upper);
798 tmp = 0;
799 if (sharpflag && num != 0) {
800 if (base == 8)
801 tmp++;
802 else if (base == 16)
803 tmp += 2;
805 if (neg || (sign && spaceflag))
806 tmp++;
808 if (!ladjust && padc == '0')
809 dwidth = width - tmp;
810 width -= tmp + imax(dwidth, n);
811 dwidth -= n;
812 if (!ladjust)
813 while (width-- > 0)
814 PCHAR(' ');
815 if (neg) {
816 PCHAR('-');
817 } else if (sign && spaceflag) {
818 PCHAR(' ');
820 if (sharpflag && num != 0) {
821 if (base == 8) {
822 PCHAR('0');
823 } else if (base == 16) {
824 PCHAR('0');
825 PCHAR('x');
828 while (dwidth-- > 0)
829 PCHAR('0');
831 while (*p)
832 PCHAR(*p--);
834 if (ladjust)
835 while (width-- > 0)
836 PCHAR(' ');
838 break;
839 default:
840 while (percent < fmt)
841 PCHAR(*percent++);
843 * Since we ignore an formatting argument it is no
844 * longer safe to obey the remaining formatting
845 * arguments as the arguments will no longer match
846 * the format specs.
848 stop = 1;
849 break;
852 done:
854 * Cleanup reentrancy issues.
856 if (func == kputchar)
857 atomic_clear_long(&mycpu->gd_flags, GDF_KPRINTF);
858 if (usespin) {
859 spin_unlock(&cons_spin);
860 crit_exit_hard();
862 return (retval);
865 #undef PCHAR
868 * Called from the panic code to try to get the console working
869 * again in case we paniced inside a kprintf().
871 void
872 kvcreinitspin(void)
874 spin_init(&cons_spin, "kvcre");
875 atomic_clear_long(&mycpu->gd_flags, GDF_KPRINTF);
879 * Console support thread for constty intercepts. This is needed because
880 * console tty intercepts can block. Instead of having kputchar() attempt
881 * to directly write to the console intercept we just force it to log
882 * and wakeup this baby to track and dump the log to constty.
884 static void
885 constty_daemon(void)
887 u_int rindex;
888 u_int xindex;
889 u_int n;
890 struct msgbuf *mbp;
891 struct tty *tp;
893 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc,
894 constty_td, SHUTDOWN_PRI_FIRST);
895 constty_td->td_flags |= TDF_SYSTHREAD;
897 mbp = msgbufp;
898 rindex = mbp->msg_bufr; /* persistent loop variable */
899 xindex = mbp->msg_bufx - 1; /* anything different than bufx */
900 cpu_ccfence();
902 for (;;) {
903 kproc_suspend_loop();
905 crit_enter();
906 if (mbp != msgbufp)
907 mbp = msgbufp;
908 if (xindex == mbp->msg_bufx ||
909 mbp == NULL ||
910 msgbufmapped == 0) {
911 tsleep(constty_td, 0, "waiting", hz*60);
912 crit_exit();
913 continue;
915 crit_exit();
918 * Get message buf FIFO indices. rindex is tracking.
920 xindex = mbp->msg_bufx;
921 cpu_ccfence();
922 if ((tp = constty) == NULL) {
923 rindex = xindex;
924 continue;
928 * Check if the calculated bytes has rolled the whole
929 * message buffer.
931 n = xindex - rindex;
932 if (n > mbp->msg_size - 1024) {
933 rindex = xindex - mbp->msg_size + 2048;
934 n = xindex - rindex;
938 * And dump it. If constty gets stuck will give up.
940 while (rindex != xindex) {
941 u_int ri = rindex % mbp->msg_size;
942 if (tputchar((uint8_t)mbp->msg_ptr[ri], tp) < 0) {
943 constty = NULL;
944 rindex = xindex;
945 break;
947 if (tp->t_outq.c_cc >= tp->t_ohiwat) {
948 tsleep(constty_daemon, 0, "blocked", hz / 10);
949 if (tp->t_outq.c_cc >= tp->t_ohiwat) {
950 rindex = xindex;
951 break;
954 ++rindex;
959 static struct kproc_desc constty_kp = {
960 "consttyd",
961 constty_daemon,
962 &constty_td
964 SYSINIT(bufdaemon, SI_SUB_KTHREAD_UPDATE, SI_ORDER_ANY,
965 kproc_start, &constty_kp);
968 * Put character in log buffer with a particular priority.
970 * MPSAFE
972 static void
973 msglogchar(int c, int pri)
975 static int lastpri = -1;
976 static int dangling;
977 char nbuf[MAXNBUF];
978 char *p;
980 if (!msgbufmapped)
981 return;
982 if (c == '\0' || c == '\r')
983 return;
984 if (pri != -1 && pri != lastpri) {
985 if (dangling) {
986 msgaddchar('\n', NULL);
987 dangling = 0;
989 msgaddchar('<', NULL);
990 for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;)
991 msgaddchar(*p--, NULL);
992 msgaddchar('>', NULL);
993 lastpri = pri;
995 msgaddchar(c, NULL);
996 if (c == '\n') {
997 dangling = 0;
998 lastpri = -1;
999 } else {
1000 dangling = 1;
1005 * Put char in log buffer. Make sure nothing blows up beyond repair if
1006 * we have an MP race.
1008 * MPSAFE.
1010 static void
1011 msgaddchar(int c, void *dummy)
1013 struct msgbuf *mbp;
1014 u_int lindex;
1015 u_int rindex;
1016 u_int xindex;
1017 u_int n;
1019 if (!msgbufmapped)
1020 return;
1021 mbp = msgbufp;
1022 lindex = mbp->msg_bufl;
1023 rindex = mbp->msg_bufr;
1024 xindex = mbp->msg_bufx++; /* Allow SMP race */
1025 cpu_ccfence();
1027 mbp->msg_ptr[xindex % mbp->msg_size] = c;
1028 n = xindex - lindex;
1029 if (n > mbp->msg_size - 1024) {
1030 lindex = xindex - mbp->msg_size + 2048;
1031 cpu_ccfence();
1032 mbp->msg_bufl = lindex;
1034 n = xindex - rindex;
1035 if (n > mbp->msg_size - 1024) {
1036 rindex = xindex - mbp->msg_size + 2048;
1037 cpu_ccfence();
1038 mbp->msg_bufr = rindex;
1042 static void
1043 msgbufcopy(struct msgbuf *oldp)
1045 u_int rindex;
1046 u_int xindex;
1047 u_int n;
1049 rindex = oldp->msg_bufr;
1050 xindex = oldp->msg_bufx;
1051 cpu_ccfence();
1053 n = xindex - rindex;
1054 if (n > oldp->msg_size - 1024)
1055 rindex = xindex - oldp->msg_size + 2048;
1056 while (rindex != xindex) {
1057 msglogchar(oldp->msg_ptr[rindex % oldp->msg_size], -1);
1058 ++rindex;
1062 void
1063 msgbufinit(void *ptr, size_t size)
1065 char *cp;
1066 static struct msgbuf *oldp = NULL;
1068 size -= sizeof(*msgbufp);
1069 cp = (char *)ptr;
1070 msgbufp = (struct msgbuf *) (cp + size);
1071 if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_size != size) {
1072 bzero(cp, size);
1073 bzero(msgbufp, sizeof(*msgbufp));
1074 msgbufp->msg_magic = MSG_MAGIC;
1075 msgbufp->msg_size = (char *)msgbufp - cp;
1077 msgbufp->msg_ptr = cp;
1078 if (msgbufmapped && oldp != msgbufp)
1079 msgbufcopy(oldp);
1080 cpu_mfence();
1081 msgbufmapped = 1;
1082 oldp = msgbufp;
1085 /* Sysctls for accessing/clearing the msgbuf */
1087 static int
1088 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
1090 struct msgbuf *mbp;
1091 struct ucred *cred;
1092 int error;
1093 u_int rindex_modulo;
1094 u_int xindex_modulo;
1095 u_int rindex;
1096 u_int xindex;
1097 u_int n;
1100 * Only wheel or root can access the message log.
1102 if (unprivileged_read_msgbuf == 0) {
1103 KKASSERT(req->td->td_proc);
1104 cred = req->td->td_proc->p_ucred;
1106 if ((cred->cr_prison || groupmember(0, cred) == 0) &&
1107 priv_check(req->td, PRIV_ROOT) != 0
1109 return (EPERM);
1114 * Unwind the buffer, so that it's linear (possibly starting with
1115 * some initial nulls).
1117 * We don't push the entire buffer like we did before because
1118 * bufr (and bufl) now advance in chunks when the fifo is full,
1119 * rather than one character.
1121 mbp = msgbufp;
1122 rindex = mbp->msg_bufr;
1123 xindex = mbp->msg_bufx;
1124 n = xindex - rindex;
1125 if (n > mbp->msg_size - 1024) {
1126 rindex = xindex - mbp->msg_size + 2048;
1127 n = xindex - rindex;
1129 rindex_modulo = rindex % mbp->msg_size;
1130 xindex_modulo = xindex % mbp->msg_size;
1132 if (rindex_modulo < xindex_modulo) {
1134 * Can handle in one linear section.
1136 error = sysctl_handle_opaque(oidp,
1137 mbp->msg_ptr + rindex_modulo,
1138 xindex_modulo - rindex_modulo,
1139 req);
1140 } else if (rindex_modulo == xindex_modulo) {
1142 * Empty buffer, just return a single newline
1144 error = sysctl_handle_opaque(oidp, "\n", 1, req);
1145 } else if (n <= mbp->msg_size - rindex_modulo) {
1147 * Can handle in one linear section.
1149 error = sysctl_handle_opaque(oidp,
1150 mbp->msg_ptr + rindex_modulo,
1151 n - rindex_modulo,
1152 req);
1153 } else {
1155 * Glue together two linear sections into one contiguous
1156 * output.
1158 error = sysctl_handle_opaque(oidp,
1159 mbp->msg_ptr + rindex_modulo,
1160 mbp->msg_size - rindex_modulo,
1161 req);
1162 n -= mbp->msg_size - rindex_modulo;
1163 if (error == 0)
1164 error = sysctl_handle_opaque(oidp, mbp->msg_ptr,
1165 n, req);
1167 return (error);
1170 SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
1171 0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
1173 static int msgbuf_clear;
1175 static int
1176 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
1178 int error;
1179 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
1180 if (!error && req->newptr) {
1181 /* Clear the buffer and reset write pointer */
1182 msgbufp->msg_bufr = msgbufp->msg_bufx;
1183 msgbufp->msg_bufl = msgbufp->msg_bufx;
1184 bzero(msgbufp->msg_ptr, msgbufp->msg_size);
1185 msgbuf_clear = 0;
1187 return (error);
1190 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
1191 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clear, 0,
1192 sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
1194 #ifdef DDB
1196 DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
1198 u_int rindex;
1199 u_int i;
1200 u_int j;
1202 if (!msgbufmapped) {
1203 db_printf("msgbuf not mapped yet\n");
1204 return;
1206 db_printf("msgbufp = %p\n", msgbufp);
1207 db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n",
1208 msgbufp->msg_magic, msgbufp->msg_size,
1209 msgbufp->msg_bufr % msgbufp->msg_size,
1210 msgbufp->msg_bufx % msgbufp->msg_size,
1211 msgbufp->msg_ptr);
1213 rindex = msgbufp->msg_bufr;
1214 for (i = 0; i < msgbufp->msg_size; i++) {
1215 j = (i + rindex) % msgbufp->msg_size;
1216 db_printf("%c", msgbufp->msg_ptr[j]);
1218 db_printf("\n");
1221 #endif /* DDB */
1224 void
1225 hexdump(const void *ptr, int length, const char *hdr, int flags)
1227 int i, j, k;
1228 int cols;
1229 const unsigned char *cp;
1230 char delim;
1232 if ((flags & HD_DELIM_MASK) != 0)
1233 delim = (flags & HD_DELIM_MASK) >> 8;
1234 else
1235 delim = ' ';
1237 if ((flags & HD_COLUMN_MASK) != 0)
1238 cols = flags & HD_COLUMN_MASK;
1239 else
1240 cols = 16;
1242 cp = ptr;
1243 for (i = 0; i < length; i+= cols) {
1244 if (hdr != NULL)
1245 kprintf("%s", hdr);
1247 if ((flags & HD_OMIT_COUNT) == 0)
1248 kprintf("%04x ", i);
1250 if ((flags & HD_OMIT_HEX) == 0) {
1251 for (j = 0; j < cols; j++) {
1252 k = i + j;
1253 if (k < length)
1254 kprintf("%c%02x", delim, cp[k]);
1255 else
1256 kprintf(" ");
1260 if ((flags & HD_OMIT_CHARS) == 0) {
1261 kprintf(" |");
1262 for (j = 0; j < cols; j++) {
1263 k = i + j;
1264 if (k >= length)
1265 kprintf(" ");
1266 else if (cp[k] >= ' ' && cp[k] <= '~')
1267 kprintf("%c", cp[k]);
1268 else
1269 kprintf(".");
1271 kprintf("|");
1273 kprintf("\n");
1277 void
1278 kprint_cpuset(cpumask_t *mask)
1280 int i;
1281 int b = -1;
1282 int e = -1;
1283 int more = 0;
1285 kprintf("cpus(");
1286 CPUSET_FOREACH(i, *mask) {
1287 if (b < 0) {
1288 b = i;
1289 e = b + 1;
1290 continue;
1292 if (e == i) {
1293 ++e;
1294 continue;
1296 if (more)
1297 kprintf(", ");
1298 if (b == e - 1) {
1299 kprintf("%d", b);
1300 } else {
1301 kprintf("%d-%d", b, e - 1);
1303 more = 1;
1304 b = i;
1305 e = b + 1;
1307 if (more)
1308 kprintf(", ");
1309 if (b >= 0) {
1310 if (b == e - 1) {
1311 kprintf("%d", b);
1312 } else {
1313 kprintf("%d-%d", b, e - 1);
1316 kprintf(") ");