xref: /freebsd/sys/kern/subr_prf.c (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1986, 1988, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #ifdef _KERNEL
43 #include "opt_ddb.h"
44 #include "opt_printf.h"
45 #endif  /* _KERNEL */
46 
47 #include <sys/param.h>
48 #ifdef _KERNEL
49 #include <sys/systm.h>
50 #include <sys/lock.h>
51 #include <sys/kdb.h>
52 #include <sys/mutex.h>
53 #include <sys/sx.h>
54 #include <sys/kernel.h>
55 #include <sys/msgbuf.h>
56 #include <sys/malloc.h>
57 #include <sys/priv.h>
58 #include <sys/proc.h>
59 #include <sys/stddef.h>
60 #include <sys/sysctl.h>
61 #include <sys/tty.h>
62 #include <sys/syslog.h>
63 #include <sys/cons.h>
64 #include <sys/uio.h>
65 #else /* !_KERNEL */
66 #include <errno.h>
67 #endif
68 #include <sys/ctype.h>
69 #include <sys/sbuf.h>
70 
71 #ifdef DDB
72 #include <ddb/ddb.h>
73 #endif
74 
75 /*
76  * Note that stdarg.h and the ANSI style va_start macro is used for both
77  * ANSI and traditional C compilers.
78  */
79 #ifdef _KERNEL
80 #include <machine/stdarg.h>
81 #else
82 #include <stdarg.h>
83 #endif
84 
85 /*
86  * This is needed for sbuf_putbuf() when compiled into userland.  Due to the
87  * shared nature of this file, it's the only place to put it.
88  */
89 #ifndef _KERNEL
90 #include <stdio.h>
91 #endif
92 
93 #ifdef _KERNEL
94 
95 #define TOCONS	0x01
96 #define TOTTY	0x02
97 #define TOLOG	0x04
98 
99 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
100 #define MAXNBUF	(sizeof(intmax_t) * NBBY + 1)
101 
102 struct putchar_arg {
103 	int	flags;
104 	int	pri;
105 	struct	tty *tty;
106 	char	*p_bufr;
107 	size_t	n_bufr;
108 	char	*p_next;
109 	size_t	remain;
110 };
111 
112 struct snprintf_arg {
113 	char	*str;
114 	size_t	remain;
115 };
116 
117 extern	int log_open;
118 
119 static void  msglogchar(int c, int pri);
120 static void  msglogstr(char *str, int pri, int filter_cr);
121 static void  prf_putbuf(char *bufr, int flags, int pri);
122 static void  putchar(int ch, void *arg);
123 static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len, int upper);
124 static void  snprintf_func(int ch, void *arg);
125 
126 static bool msgbufmapped;		/* Set when safe to use msgbuf */
127 int msgbuftrigger;
128 struct msgbuf *msgbufp;
129 
130 #ifndef BOOT_TAG_SZ
131 #define	BOOT_TAG_SZ	32
132 #endif
133 #ifndef BOOT_TAG
134 /* Tag used to mark the start of a boot in dmesg */
135 #define	BOOT_TAG	"---<<BOOT>>---"
136 #endif
137 
138 static char current_boot_tag[BOOT_TAG_SZ + 1] = BOOT_TAG;
139 SYSCTL_STRING(_kern, OID_AUTO, boot_tag, CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
140     current_boot_tag, 0, "Tag added to dmesg at start of boot");
141 
142 static int log_console_output = 1;
143 SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RWTUN,
144     &log_console_output, 0, "Duplicate console output to the syslog");
145 
146 /*
147  * See the comment in log_console() below for more explanation of this.
148  */
149 static int log_console_add_linefeed;
150 SYSCTL_INT(_kern, OID_AUTO, log_console_add_linefeed, CTLFLAG_RWTUN,
151     &log_console_add_linefeed, 0, "log_console() adds extra newlines");
152 
153 static int always_console_output;
154 SYSCTL_INT(_kern, OID_AUTO, always_console_output, CTLFLAG_RWTUN,
155     &always_console_output, 0, "Always output to console despite TIOCCONS");
156 
157 /*
158  * Warn that a system table is full.
159  */
160 void
161 tablefull(const char *tab)
162 {
163 
164 	log(LOG_ERR, "%s: table is full\n", tab);
165 }
166 
167 /*
168  * Uprintf prints to the controlling terminal for the current process.
169  */
170 int
171 uprintf(const char *fmt, ...)
172 {
173 	va_list ap;
174 	struct putchar_arg pca;
175 	struct proc *p;
176 	struct thread *td;
177 	int retval;
178 
179 	td = curthread;
180 	if (TD_IS_IDLETHREAD(td))
181 		return (0);
182 
183 	if (td->td_proc == initproc) {
184 		/* Produce output when we fail to load /sbin/init: */
185 		va_start(ap, fmt);
186 		retval = vprintf(fmt, ap);
187 		va_end(ap);
188 		return (retval);
189 	}
190 
191 	sx_slock(&proctree_lock);
192 	p = td->td_proc;
193 	PROC_LOCK(p);
194 	if ((p->p_flag & P_CONTROLT) == 0) {
195 		PROC_UNLOCK(p);
196 		sx_sunlock(&proctree_lock);
197 		return (0);
198 	}
199 	SESS_LOCK(p->p_session);
200 	pca.tty = p->p_session->s_ttyp;
201 	SESS_UNLOCK(p->p_session);
202 	PROC_UNLOCK(p);
203 	if (pca.tty == NULL) {
204 		sx_sunlock(&proctree_lock);
205 		return (0);
206 	}
207 	pca.flags = TOTTY;
208 	pca.p_bufr = NULL;
209 	va_start(ap, fmt);
210 	tty_lock(pca.tty);
211 	sx_sunlock(&proctree_lock);
212 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
213 	tty_unlock(pca.tty);
214 	va_end(ap);
215 	return (retval);
216 }
217 
218 /*
219  * tprintf and vtprintf print on the controlling terminal associated with the
220  * given session, possibly to the log as well.
221  */
222 void
223 tprintf(struct proc *p, int pri, const char *fmt, ...)
224 {
225 	va_list ap;
226 
227 	va_start(ap, fmt);
228 	vtprintf(p, pri, fmt, ap);
229 	va_end(ap);
230 }
231 
232 void
233 vtprintf(struct proc *p, int pri, const char *fmt, va_list ap)
234 {
235 	struct tty *tp = NULL;
236 	int flags = 0;
237 	struct putchar_arg pca;
238 	struct session *sess = NULL;
239 
240 	sx_slock(&proctree_lock);
241 	if (pri != -1)
242 		flags |= TOLOG;
243 	if (p != NULL) {
244 		PROC_LOCK(p);
245 		if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
246 			sess = p->p_session;
247 			sess_hold(sess);
248 			PROC_UNLOCK(p);
249 			tp = sess->s_ttyp;
250 			if (tp != NULL && tty_checkoutq(tp))
251 				flags |= TOTTY;
252 			else
253 				tp = NULL;
254 		} else
255 			PROC_UNLOCK(p);
256 	}
257 	pca.pri = pri;
258 	pca.tty = tp;
259 	pca.flags = flags;
260 	pca.p_bufr = NULL;
261 	if (pca.tty != NULL)
262 		tty_lock(pca.tty);
263 	sx_sunlock(&proctree_lock);
264 	kvprintf(fmt, putchar, &pca, 10, ap);
265 	if (pca.tty != NULL)
266 		tty_unlock(pca.tty);
267 	if (sess != NULL)
268 		sess_release(sess);
269 	msgbuftrigger = 1;
270 }
271 
272 static int
273 _vprintf(int level, int flags, const char *fmt, va_list ap)
274 {
275 	struct putchar_arg pca;
276 	int retval;
277 #ifdef PRINTF_BUFR_SIZE
278 	char bufr[PRINTF_BUFR_SIZE];
279 #endif
280 
281 	TSENTER();
282 	pca.tty = NULL;
283 	pca.pri = level;
284 	pca.flags = flags;
285 #ifdef PRINTF_BUFR_SIZE
286 	pca.p_bufr = bufr;
287 	pca.p_next = pca.p_bufr;
288 	pca.n_bufr = sizeof(bufr);
289 	pca.remain = sizeof(bufr);
290 	*pca.p_next = '\0';
291 #else
292 	/* Don't buffer console output. */
293 	pca.p_bufr = NULL;
294 #endif
295 
296 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
297 
298 #ifdef PRINTF_BUFR_SIZE
299 	/* Write any buffered console/log output: */
300 	if (*pca.p_bufr != '\0')
301 		prf_putbuf(pca.p_bufr, flags, level);
302 #endif
303 
304 	TSEXIT();
305 	return (retval);
306 }
307 
308 /*
309  * Log writes to the log buffer, and guarantees not to sleep (so can be
310  * called by interrupt routines).  If there is no process reading the
311  * log yet, it writes to the console also.
312  */
313 void
314 log(int level, const char *fmt, ...)
315 {
316 	va_list ap;
317 
318 	va_start(ap, fmt);
319 	vlog(level, fmt, ap);
320 	va_end(ap);
321 }
322 
323 void
324 vlog(int level, const char *fmt, va_list ap)
325 {
326 
327 	(void)_vprintf(level, log_open ? TOLOG : TOCONS | TOLOG, fmt, ap);
328 	msgbuftrigger = 1;
329 }
330 
331 #define CONSCHUNK 128
332 
333 void
334 log_console(struct uio *uio)
335 {
336 	int c, error, nl;
337 	char *consbuffer;
338 	int pri;
339 
340 	if (!log_console_output)
341 		return;
342 
343 	pri = LOG_INFO | LOG_CONSOLE;
344 	uio = cloneuio(uio);
345 	consbuffer = malloc(CONSCHUNK, M_TEMP, M_WAITOK);
346 
347 	nl = 0;
348 	while (uio->uio_resid > 0) {
349 		c = imin(uio->uio_resid, CONSCHUNK - 1);
350 		error = uiomove(consbuffer, c, uio);
351 		if (error != 0)
352 			break;
353 		/* Make sure we're NUL-terminated */
354 		consbuffer[c] = '\0';
355 		if (consbuffer[c - 1] == '\n')
356 			nl = 1;
357 		else
358 			nl = 0;
359 		msglogstr(consbuffer, pri, /*filter_cr*/ 1);
360 	}
361 	/*
362 	 * The previous behavior in log_console() is preserved when
363 	 * log_console_add_linefeed is non-zero.  For that behavior, if an
364 	 * individual console write came in that was not terminated with a
365 	 * line feed, it would add a line feed.
366 	 *
367 	 * This results in different data in the message buffer than
368 	 * appears on the system console (which doesn't add extra line feed
369 	 * characters).
370 	 *
371 	 * A number of programs and rc scripts write a line feed, or a period
372 	 * and a line feed when they have completed their operation.  On
373 	 * the console, this looks seamless, but when displayed with
374 	 * 'dmesg -a', you wind up with output that looks like this:
375 	 *
376 	 * Updating motd:
377 	 * .
378 	 *
379 	 * On the console, it looks like this:
380 	 * Updating motd:.
381 	 *
382 	 * We could add logic to detect that situation, or just not insert
383 	 * the extra newlines.  Set the kern.log_console_add_linefeed
384 	 * sysctl/tunable variable to get the old behavior.
385 	 */
386 	if (!nl && log_console_add_linefeed) {
387 		consbuffer[0] = '\n';
388 		consbuffer[1] = '\0';
389 		msglogstr(consbuffer, pri, /*filter_cr*/ 1);
390 	}
391 	msgbuftrigger = 1;
392 	free(uio, M_IOV);
393 	free(consbuffer, M_TEMP);
394 }
395 
396 int
397 printf(const char *fmt, ...)
398 {
399 	va_list ap;
400 	int retval;
401 
402 	va_start(ap, fmt);
403 	retval = vprintf(fmt, ap);
404 	va_end(ap);
405 
406 	return (retval);
407 }
408 
409 int
410 vprintf(const char *fmt, va_list ap)
411 {
412 	int retval;
413 
414 	retval = _vprintf(-1, TOCONS | TOLOG, fmt, ap);
415 
416 	if (!KERNEL_PANICKED())
417 		msgbuftrigger = 1;
418 
419 	return (retval);
420 }
421 
422 static void
423 prf_putchar(int c, int flags, int pri)
424 {
425 
426 	if (flags & TOLOG)
427 		msglogchar(c, pri);
428 
429 	if (flags & TOCONS) {
430 		if ((!KERNEL_PANICKED()) && (constty != NULL))
431 			msgbuf_addchar(&consmsgbuf, c);
432 
433 		if ((constty == NULL) || always_console_output)
434 			cnputc(c);
435 	}
436 }
437 
438 static void
439 prf_putbuf(char *bufr, int flags, int pri)
440 {
441 
442 	if (flags & TOLOG)
443 		msglogstr(bufr, pri, /*filter_cr*/1);
444 
445 	if (flags & TOCONS) {
446 		if ((!KERNEL_PANICKED()) && (constty != NULL))
447 			msgbuf_addstr(&consmsgbuf, -1,
448 			    bufr, /*filter_cr*/ 0);
449 
450 		if ((constty == NULL) || always_console_output)
451 			cnputs(bufr);
452 	}
453 }
454 
455 static void
456 putbuf(int c, struct putchar_arg *ap)
457 {
458 	/* Check if no console output buffer was provided. */
459 	if (ap->p_bufr == NULL) {
460 		prf_putchar(c, ap->flags, ap->pri);
461 	} else {
462 		/* Buffer the character: */
463 		*ap->p_next++ = c;
464 		ap->remain--;
465 
466 		/* Always leave the buffer zero terminated. */
467 		*ap->p_next = '\0';
468 
469 		/* Check if the buffer needs to be flushed. */
470 		if (ap->remain == 2 || c == '\n') {
471 			prf_putbuf(ap->p_bufr, ap->flags, ap->pri);
472 
473 			ap->p_next = ap->p_bufr;
474 			ap->remain = ap->n_bufr;
475 			*ap->p_next = '\0';
476 		}
477 
478 		/*
479 		 * Since we fill the buffer up one character at a time,
480 		 * this should not happen.  We should always catch it when
481 		 * ap->remain == 2 (if not sooner due to a newline), flush
482 		 * the buffer and move on.  One way this could happen is
483 		 * if someone sets PRINTF_BUFR_SIZE to 1 or something
484 		 * similarly silly.
485 		 */
486 		KASSERT(ap->remain > 2, ("Bad buffer logic, remain = %zd",
487 		    ap->remain));
488 	}
489 }
490 
491 /*
492  * Print a character on console or users terminal.  If destination is
493  * the console then the last bunch of characters are saved in msgbuf for
494  * inspection later.
495  */
496 static void
497 putchar(int c, void *arg)
498 {
499 	struct putchar_arg *ap = (struct putchar_arg*) arg;
500 	struct tty *tp = ap->tty;
501 	int flags = ap->flags;
502 
503 	/* Don't use the tty code after a panic or while in ddb. */
504 	if (kdb_active) {
505 		if (c != '\0')
506 			cnputc(c);
507 		return;
508 	}
509 
510 	if ((flags & TOTTY) && tp != NULL && !KERNEL_PANICKED())
511 		tty_putchar(tp, c);
512 
513 	if ((flags & (TOCONS | TOLOG)) && c != '\0')
514 		putbuf(c, ap);
515 }
516 
517 /*
518  * Scaled down version of sprintf(3).
519  */
520 int
521 sprintf(char *buf, const char *cfmt, ...)
522 {
523 	int retval;
524 	va_list ap;
525 
526 	va_start(ap, cfmt);
527 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
528 	buf[retval] = '\0';
529 	va_end(ap);
530 	return (retval);
531 }
532 
533 /*
534  * Scaled down version of vsprintf(3).
535  */
536 int
537 vsprintf(char *buf, const char *cfmt, va_list ap)
538 {
539 	int retval;
540 
541 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
542 	buf[retval] = '\0';
543 	return (retval);
544 }
545 
546 /*
547  * Scaled down version of snprintf(3).
548  */
549 int
550 snprintf(char *str, size_t size, const char *format, ...)
551 {
552 	int retval;
553 	va_list ap;
554 
555 	va_start(ap, format);
556 	retval = vsnprintf(str, size, format, ap);
557 	va_end(ap);
558 	return(retval);
559 }
560 
561 /*
562  * Scaled down version of vsnprintf(3).
563  */
564 int
565 vsnprintf(char *str, size_t size, const char *format, va_list ap)
566 {
567 	struct snprintf_arg info;
568 	int retval;
569 
570 	info.str = str;
571 	info.remain = size;
572 	retval = kvprintf(format, snprintf_func, &info, 10, ap);
573 	if (info.remain >= 1)
574 		*info.str++ = '\0';
575 	return (retval);
576 }
577 
578 /*
579  * Kernel version which takes radix argument vsnprintf(3).
580  */
581 int
582 vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
583 {
584 	struct snprintf_arg info;
585 	int retval;
586 
587 	info.str = str;
588 	info.remain = size;
589 	retval = kvprintf(format, snprintf_func, &info, radix, ap);
590 	if (info.remain >= 1)
591 		*info.str++ = '\0';
592 	return (retval);
593 }
594 
595 static void
596 snprintf_func(int ch, void *arg)
597 {
598 	struct snprintf_arg *const info = arg;
599 
600 	if (info->remain >= 2) {
601 		*info->str++ = ch;
602 		info->remain--;
603 	}
604 }
605 
606 /*
607  * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
608  * order; return an optional length and a pointer to the last character
609  * written in the buffer (i.e., the first character of the string).
610  * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
611  */
612 static char *
613 ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
614 {
615 	char *p, c;
616 
617 	p = nbuf;
618 	*p = '\0';
619 	do {
620 		c = hex2ascii(num % base);
621 		*++p = upper ? toupper(c) : c;
622 	} while (num /= base);
623 	if (lenp)
624 		*lenp = p - nbuf;
625 	return (p);
626 }
627 
628 /*
629  * Scaled down version of printf(3).
630  *
631  * Two additional formats:
632  *
633  * The format %b is supported to decode error registers.
634  * Its usage is:
635  *
636  *	printf("reg=%b\n", regval, "<base><arg>*");
637  *
638  * where <base> is the output base expressed as a control character, e.g.
639  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
640  * the first of which gives the bit number to be inspected (origin 1), and
641  * the next characters (up to a control character, i.e. a character <= 32),
642  * give the name of the register.  Thus:
643  *
644  *	kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE");
645  *
646  * would produce output:
647  *
648  *	reg=3<BITTWO,BITONE>
649  *
650  * XXX:  %D  -- Hexdump, takes pointer and separator string:
651  *		("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
652  *		("%*D", len, ptr, " " -> XX XX XX XX ...
653  */
654 int
655 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
656 {
657 #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
658 	char nbuf[MAXNBUF];
659 	char *d;
660 	const char *p, *percent, *q;
661 	u_char *up;
662 	int ch, n;
663 	uintmax_t num;
664 	int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
665 	int cflag, hflag, jflag, tflag, zflag;
666 	int bconv, dwidth, upper;
667 	char padc;
668 	int stop = 0, retval = 0;
669 
670 	num = 0;
671 	q = NULL;
672 	if (!func)
673 		d = (char *) arg;
674 	else
675 		d = NULL;
676 
677 	if (fmt == NULL)
678 		fmt = "(fmt null)\n";
679 
680 	if (radix < 2 || radix > 36)
681 		radix = 10;
682 
683 	for (;;) {
684 		padc = ' ';
685 		width = 0;
686 		while ((ch = (u_char)*fmt++) != '%' || stop) {
687 			if (ch == '\0')
688 				return (retval);
689 			PCHAR(ch);
690 		}
691 		percent = fmt - 1;
692 		qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
693 		sign = 0; dot = 0; bconv = 0; dwidth = 0; upper = 0;
694 		cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
695 reswitch:	switch (ch = (u_char)*fmt++) {
696 		case '.':
697 			dot = 1;
698 			goto reswitch;
699 		case '#':
700 			sharpflag = 1;
701 			goto reswitch;
702 		case '+':
703 			sign = 1;
704 			goto reswitch;
705 		case '-':
706 			ladjust = 1;
707 			goto reswitch;
708 		case '%':
709 			PCHAR(ch);
710 			break;
711 		case '*':
712 			if (!dot) {
713 				width = va_arg(ap, int);
714 				if (width < 0) {
715 					ladjust = !ladjust;
716 					width = -width;
717 				}
718 			} else {
719 				dwidth = va_arg(ap, int);
720 			}
721 			goto reswitch;
722 		case '0':
723 			if (!dot) {
724 				padc = '0';
725 				goto reswitch;
726 			}
727 			/* FALLTHROUGH */
728 		case '1': case '2': case '3': case '4':
729 		case '5': case '6': case '7': case '8': case '9':
730 				for (n = 0;; ++fmt) {
731 					n = n * 10 + ch - '0';
732 					ch = *fmt;
733 					if (ch < '0' || ch > '9')
734 						break;
735 				}
736 			if (dot)
737 				dwidth = n;
738 			else
739 				width = n;
740 			goto reswitch;
741 		case 'b':
742 			ladjust = 1;
743 			bconv = 1;
744 			goto handle_nosign;
745 		case 'c':
746 			width -= 1;
747 
748 			if (!ladjust && width > 0)
749 				while (width--)
750 					PCHAR(padc);
751 			PCHAR(va_arg(ap, int));
752 			if (ladjust && width > 0)
753 				while (width--)
754 					PCHAR(padc);
755 			break;
756 		case 'D':
757 			up = va_arg(ap, u_char *);
758 			p = va_arg(ap, char *);
759 			if (!width)
760 				width = 16;
761 			while(width--) {
762 				PCHAR(hex2ascii(*up >> 4));
763 				PCHAR(hex2ascii(*up & 0x0f));
764 				up++;
765 				if (width)
766 					for (q=p;*q;q++)
767 						PCHAR(*q);
768 			}
769 			break;
770 		case 'd':
771 		case 'i':
772 			base = 10;
773 			sign = 1;
774 			goto handle_sign;
775 		case 'h':
776 			if (hflag) {
777 				hflag = 0;
778 				cflag = 1;
779 			} else
780 				hflag = 1;
781 			goto reswitch;
782 		case 'j':
783 			jflag = 1;
784 			goto reswitch;
785 		case 'l':
786 			if (lflag) {
787 				lflag = 0;
788 				qflag = 1;
789 			} else
790 				lflag = 1;
791 			goto reswitch;
792 		case 'n':
793 			/*
794 			 * We do not support %n in kernel, but consume the
795 			 * argument.
796 			 */
797 			if (jflag)
798 				(void)va_arg(ap, intmax_t *);
799 			else if (qflag)
800 				(void)va_arg(ap, quad_t *);
801 			else if (lflag)
802 				(void)va_arg(ap, long *);
803 			else if (zflag)
804 				(void)va_arg(ap, size_t *);
805 			else if (hflag)
806 				(void)va_arg(ap, short *);
807 			else if (cflag)
808 				(void)va_arg(ap, char *);
809 			else
810 				(void)va_arg(ap, int *);
811 			break;
812 		case 'o':
813 			base = 8;
814 			goto handle_nosign;
815 		case 'p':
816 			base = 16;
817 			sharpflag = (width == 0);
818 			sign = 0;
819 			num = (uintptr_t)va_arg(ap, void *);
820 			goto number;
821 		case 'q':
822 			qflag = 1;
823 			goto reswitch;
824 		case 'r':
825 			base = radix;
826 			if (sign)
827 				goto handle_sign;
828 			goto handle_nosign;
829 		case 's':
830 			p = va_arg(ap, char *);
831 			if (p == NULL)
832 				p = "(null)";
833 			if (!dot)
834 				n = strlen (p);
835 			else
836 				for (n = 0; n < dwidth && p[n]; n++)
837 					continue;
838 
839 			width -= n;
840 
841 			if (!ladjust && width > 0)
842 				while (width--)
843 					PCHAR(padc);
844 			while (n--)
845 				PCHAR(*p++);
846 			if (ladjust && width > 0)
847 				while (width--)
848 					PCHAR(padc);
849 			break;
850 		case 't':
851 			tflag = 1;
852 			goto reswitch;
853 		case 'u':
854 			base = 10;
855 			goto handle_nosign;
856 		case 'X':
857 			upper = 1;
858 			/* FALLTHROUGH */
859 		case 'x':
860 			base = 16;
861 			goto handle_nosign;
862 		case 'y':
863 			base = 16;
864 			sign = 1;
865 			goto handle_sign;
866 		case 'z':
867 			zflag = 1;
868 			goto reswitch;
869 handle_nosign:
870 			sign = 0;
871 			if (jflag)
872 				num = va_arg(ap, uintmax_t);
873 			else if (qflag)
874 				num = va_arg(ap, u_quad_t);
875 			else if (tflag)
876 				num = va_arg(ap, ptrdiff_t);
877 			else if (lflag)
878 				num = va_arg(ap, u_long);
879 			else if (zflag)
880 				num = va_arg(ap, size_t);
881 			else if (hflag)
882 				num = (u_short)va_arg(ap, int);
883 			else if (cflag)
884 				num = (u_char)va_arg(ap, int);
885 			else
886 				num = va_arg(ap, u_int);
887 			if (bconv) {
888 				q = va_arg(ap, char *);
889 				base = *q++;
890 			}
891 			goto number;
892 handle_sign:
893 			if (jflag)
894 				num = va_arg(ap, intmax_t);
895 			else if (qflag)
896 				num = va_arg(ap, quad_t);
897 			else if (tflag)
898 				num = va_arg(ap, ptrdiff_t);
899 			else if (lflag)
900 				num = va_arg(ap, long);
901 			else if (zflag)
902 				num = va_arg(ap, ssize_t);
903 			else if (hflag)
904 				num = (short)va_arg(ap, int);
905 			else if (cflag)
906 				num = (char)va_arg(ap, int);
907 			else
908 				num = va_arg(ap, int);
909 number:
910 			if (sign && (intmax_t)num < 0) {
911 				neg = 1;
912 				num = -(intmax_t)num;
913 			}
914 			p = ksprintn(nbuf, num, base, &n, upper);
915 			tmp = 0;
916 			if (sharpflag && num != 0) {
917 				if (base == 8)
918 					tmp++;
919 				else if (base == 16)
920 					tmp += 2;
921 			}
922 			if (neg)
923 				tmp++;
924 
925 			if (!ladjust && padc == '0')
926 				dwidth = width - tmp;
927 			width -= tmp + imax(dwidth, n);
928 			dwidth -= n;
929 			if (!ladjust)
930 				while (width-- > 0)
931 					PCHAR(' ');
932 			if (neg)
933 				PCHAR('-');
934 			if (sharpflag && num != 0) {
935 				if (base == 8) {
936 					PCHAR('0');
937 				} else if (base == 16) {
938 					PCHAR('0');
939 					PCHAR('x');
940 				}
941 			}
942 			while (dwidth-- > 0)
943 				PCHAR('0');
944 
945 			while (*p)
946 				PCHAR(*p--);
947 
948 			if (bconv && num != 0) {
949 				/* %b conversion flag format. */
950 				tmp = retval;
951 				while (*q) {
952 					n = *q++;
953 					if (num & (1 << (n - 1))) {
954 						PCHAR(retval != tmp ?
955 						    ',' : '<');
956 						for (; (n = *q) > ' '; ++q)
957 							PCHAR(n);
958 					} else
959 						for (; *q > ' '; ++q)
960 							continue;
961 				}
962 				if (retval != tmp) {
963 					PCHAR('>');
964 					width -= retval - tmp;
965 				}
966 			}
967 
968 			if (ladjust)
969 				while (width-- > 0)
970 					PCHAR(' ');
971 
972 			break;
973 		default:
974 			while (percent < fmt)
975 				PCHAR(*percent++);
976 			/*
977 			 * Since we ignore a formatting argument it is no
978 			 * longer safe to obey the remaining formatting
979 			 * arguments as the arguments will no longer match
980 			 * the format specs.
981 			 */
982 			stop = 1;
983 			break;
984 		}
985 	}
986 #undef PCHAR
987 }
988 
989 /*
990  * Put character in log buffer with a particular priority.
991  */
992 static void
993 msglogchar(int c, int pri)
994 {
995 	static int lastpri = -1;
996 	static int dangling;
997 	char nbuf[MAXNBUF];
998 	char *p;
999 
1000 	if (!msgbufmapped)
1001 		return;
1002 	if (c == '\0' || c == '\r')
1003 		return;
1004 	if (pri != -1 && pri != lastpri) {
1005 		if (dangling) {
1006 			msgbuf_addchar(msgbufp, '\n');
1007 			dangling = 0;
1008 		}
1009 		msgbuf_addchar(msgbufp, '<');
1010 		for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL, 0); *p;)
1011 			msgbuf_addchar(msgbufp, *p--);
1012 		msgbuf_addchar(msgbufp, '>');
1013 		lastpri = pri;
1014 	}
1015 	msgbuf_addchar(msgbufp, c);
1016 	if (c == '\n') {
1017 		dangling = 0;
1018 		lastpri = -1;
1019 	} else {
1020 		dangling = 1;
1021 	}
1022 }
1023 
1024 static void
1025 msglogstr(char *str, int pri, int filter_cr)
1026 {
1027 	if (!msgbufmapped)
1028 		return;
1029 
1030 	msgbuf_addstr(msgbufp, pri, str, filter_cr);
1031 }
1032 
1033 void
1034 msgbufinit(void *ptr, int size)
1035 {
1036 	char *cp;
1037 	static struct msgbuf *oldp = NULL;
1038 	bool print_boot_tag;
1039 
1040 	size -= sizeof(*msgbufp);
1041 	cp = (char *)ptr;
1042 	print_boot_tag = !msgbufmapped;
1043 	/* Attempt to fetch kern.boot_tag tunable on first mapping */
1044 	if (!msgbufmapped)
1045 		TUNABLE_STR_FETCH("kern.boot_tag", current_boot_tag,
1046 		    sizeof(current_boot_tag));
1047 	msgbufp = (struct msgbuf *)(cp + size);
1048 	msgbuf_reinit(msgbufp, cp, size);
1049 	if (msgbufmapped && oldp != msgbufp)
1050 		msgbuf_copy(oldp, msgbufp);
1051 	msgbufmapped = true;
1052 	if (print_boot_tag && *current_boot_tag != '\0')
1053 		printf("%s\n", current_boot_tag);
1054 	oldp = msgbufp;
1055 }
1056 
1057 /* Sysctls for accessing/clearing the msgbuf */
1058 static int
1059 sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
1060 {
1061 	char buf[128], *bp;
1062 	u_int seq;
1063 	int error, len;
1064 	bool wrap;
1065 
1066 	error = priv_check(req->td, PRIV_MSGBUF);
1067 	if (error)
1068 		return (error);
1069 
1070 	/* Read the whole buffer, one chunk at a time. */
1071 	mtx_lock(&msgbuf_lock);
1072 	msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
1073 	wrap = (seq != 0);
1074 	for (;;) {
1075 		len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq);
1076 		mtx_unlock(&msgbuf_lock);
1077 		if (len == 0)
1078 			return (SYSCTL_OUT(req, "", 1)); /* add nulterm */
1079 		if (wrap) {
1080 			/* Skip the first line, as it is probably incomplete. */
1081 			bp = memchr(buf, '\n', len);
1082 			if (bp == NULL) {
1083 				mtx_lock(&msgbuf_lock);
1084 				continue;
1085 			}
1086 			wrap = false;
1087 			bp++;
1088 			len -= bp - buf;
1089 			if (len == 0) {
1090 				mtx_lock(&msgbuf_lock);
1091 				continue;
1092 			}
1093 		} else
1094 			bp = buf;
1095 		error = sysctl_handle_opaque(oidp, bp, len, req);
1096 		if (error)
1097 			return (error);
1098 
1099 		mtx_lock(&msgbuf_lock);
1100 	}
1101 }
1102 
1103 SYSCTL_PROC(_kern, OID_AUTO, msgbuf,
1104     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
1105     NULL, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
1106 
1107 static int msgbuf_clearflag;
1108 
1109 static int
1110 sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
1111 {
1112 	int error;
1113 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
1114 	if (!error && req->newptr) {
1115 		mtx_lock(&msgbuf_lock);
1116 		msgbuf_clear(msgbufp);
1117 		mtx_unlock(&msgbuf_lock);
1118 		msgbuf_clearflag = 0;
1119 	}
1120 	return (error);
1121 }
1122 
1123 SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
1124     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE | CTLFLAG_MPSAFE,
1125     &msgbuf_clearflag, 0, sysctl_kern_msgbuf_clear, "I",
1126     "Clear kernel message buffer");
1127 
1128 #ifdef DDB
1129 
1130 DB_SHOW_COMMAND_FLAGS(msgbuf, db_show_msgbuf, DB_CMD_MEMSAFE)
1131 {
1132 	int i, j;
1133 
1134 	if (!msgbufmapped) {
1135 		db_printf("msgbuf not mapped yet\n");
1136 		return;
1137 	}
1138 	db_printf("msgbufp = %p\n", msgbufp);
1139 	db_printf("magic = %x, size = %d, r= %u, w = %u, ptr = %p, cksum= %u\n",
1140 	    msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_rseq,
1141 	    msgbufp->msg_wseq, msgbufp->msg_ptr, msgbufp->msg_cksum);
1142 	for (i = 0; i < msgbufp->msg_size && !db_pager_quit; i++) {
1143 		j = MSGBUF_SEQ_TO_POS(msgbufp, i + msgbufp->msg_rseq);
1144 		db_printf("%c", msgbufp->msg_ptr[j]);
1145 	}
1146 	db_printf("\n");
1147 }
1148 
1149 #endif /* DDB */
1150 
1151 void
1152 hexdump(const void *ptr, int length, const char *hdr, int flags)
1153 {
1154 	int i, j, k;
1155 	int cols;
1156 	const unsigned char *cp;
1157 	char delim;
1158 
1159 	if ((flags & HD_DELIM_MASK) != 0)
1160 		delim = (flags & HD_DELIM_MASK) >> 8;
1161 	else
1162 		delim = ' ';
1163 
1164 	if ((flags & HD_COLUMN_MASK) != 0)
1165 		cols = flags & HD_COLUMN_MASK;
1166 	else
1167 		cols = 16;
1168 
1169 	cp = ptr;
1170 	for (i = 0; i < length; i+= cols) {
1171 		if (hdr != NULL)
1172 			printf("%s", hdr);
1173 
1174 		if ((flags & HD_OMIT_COUNT) == 0)
1175 			printf("%04x  ", i);
1176 
1177 		if ((flags & HD_OMIT_HEX) == 0) {
1178 			for (j = 0; j < cols; j++) {
1179 				k = i + j;
1180 				if (k < length)
1181 					printf("%c%02x", delim, cp[k]);
1182 				else
1183 					printf("   ");
1184 			}
1185 		}
1186 
1187 		if ((flags & HD_OMIT_CHARS) == 0) {
1188 			printf("  |");
1189 			for (j = 0; j < cols; j++) {
1190 				k = i + j;
1191 				if (k >= length)
1192 					printf(" ");
1193 				else if (cp[k] >= ' ' && cp[k] <= '~')
1194 					printf("%c", cp[k]);
1195 				else
1196 					printf(".");
1197 			}
1198 			printf("|");
1199 		}
1200 		printf("\n");
1201 	}
1202 }
1203 #endif /* _KERNEL */
1204 
1205 void
1206 sbuf_hexdump(struct sbuf *sb, const void *ptr, int length, const char *hdr,
1207 	     int flags)
1208 {
1209 	int i, j, k;
1210 	int cols;
1211 	const unsigned char *cp;
1212 	char delim;
1213 
1214 	if ((flags & HD_DELIM_MASK) != 0)
1215 		delim = (flags & HD_DELIM_MASK) >> 8;
1216 	else
1217 		delim = ' ';
1218 
1219 	if ((flags & HD_COLUMN_MASK) != 0)
1220 		cols = flags & HD_COLUMN_MASK;
1221 	else
1222 		cols = 16;
1223 
1224 	cp = ptr;
1225 	for (i = 0; i < length; i+= cols) {
1226 		if (hdr != NULL)
1227 			sbuf_printf(sb, "%s", hdr);
1228 
1229 		if ((flags & HD_OMIT_COUNT) == 0)
1230 			sbuf_printf(sb, "%04x  ", i);
1231 
1232 		if ((flags & HD_OMIT_HEX) == 0) {
1233 			for (j = 0; j < cols; j++) {
1234 				k = i + j;
1235 				if (k < length)
1236 					sbuf_printf(sb, "%c%02x", delim, cp[k]);
1237 				else
1238 					sbuf_printf(sb, "   ");
1239 			}
1240 		}
1241 
1242 		if ((flags & HD_OMIT_CHARS) == 0) {
1243 			sbuf_printf(sb, "  |");
1244 			for (j = 0; j < cols; j++) {
1245 				k = i + j;
1246 				if (k >= length)
1247 					sbuf_printf(sb, " ");
1248 				else if (cp[k] >= ' ' && cp[k] <= '~')
1249 					sbuf_printf(sb, "%c", cp[k]);
1250 				else
1251 					sbuf_printf(sb, ".");
1252 			}
1253 			sbuf_printf(sb, "|");
1254 		}
1255 		sbuf_printf(sb, "\n");
1256 	}
1257 }
1258 
1259 #ifdef _KERNEL
1260 void
1261 counted_warning(unsigned *counter, const char *msg)
1262 {
1263 	struct thread *td;
1264 	unsigned c;
1265 
1266 	for (;;) {
1267 		c = *counter;
1268 		if (c == 0)
1269 			break;
1270 		if (atomic_cmpset_int(counter, c, c - 1)) {
1271 			td = curthread;
1272 			log(LOG_INFO, "pid %d (%s) %s%s\n",
1273 			    td->td_proc->p_pid, td->td_name, msg,
1274 			    c > 1 ? "" : " - not logging anymore");
1275 			break;
1276 		}
1277 	}
1278 }
1279 #endif
1280 
1281 #ifdef _KERNEL
1282 void
1283 sbuf_putbuf(struct sbuf *sb)
1284 {
1285 
1286 	prf_putbuf(sbuf_data(sb), TOLOG | TOCONS, -1);
1287 }
1288 #else
1289 void
1290 sbuf_putbuf(struct sbuf *sb)
1291 {
1292 
1293 	printf("%s", sbuf_data(sb));
1294 }
1295 #endif
1296 
1297 int
1298 sbuf_printf_drain(void *arg, const char *data, int len)
1299 {
1300 	size_t *retvalptr;
1301 	int r;
1302 #ifdef _KERNEL
1303 	char *dataptr;
1304 	char oldchr;
1305 
1306 	/*
1307 	 * This is allowed as an extra byte is always resvered for
1308 	 * terminating NUL byte.  Save and restore the byte because
1309 	 * we might be flushing a record, and there may be valid
1310 	 * data after the buffer.
1311 	 */
1312 	oldchr = data[len];
1313 	dataptr = __DECONST(char *, data);
1314 	dataptr[len] = '\0';
1315 
1316 	prf_putbuf(dataptr, TOLOG | TOCONS, -1);
1317 	r = len;
1318 
1319 	dataptr[len] = oldchr;
1320 
1321 #else /* !_KERNEL */
1322 
1323 	r = printf("%.*s", len, data);
1324 	if (r < 0)
1325 		return (-errno);
1326 
1327 #endif
1328 
1329 	retvalptr = arg;
1330 	if (retvalptr != NULL)
1331 		*retvalptr += r;
1332 
1333 	return (r);
1334 }
1335