xref: /freebsd/sys/kern/subr_prf.c (revision 8e1b797456b4eea2504b691019bef4ead133557f)
1df8bae1dSRodney W. Grimes /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1986, 1988, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
403ae59505SPoul-Henning Kamp #include "opt_ddb.h"
413ae59505SPoul-Henning Kamp 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43df8bae1dSRodney W. Grimes #include <sys/systm.h>
44f591779bSSeigo Tanimura #include <sys/lock.h>
45f591779bSSeigo Tanimura #include <sys/mutex.h>
46f591779bSSeigo Tanimura #include <sys/sx.h>
47e796e00dSPoul-Henning Kamp #include <sys/kernel.h>
48df8bae1dSRodney W. Grimes #include <sys/msgbuf.h>
49a1c995b6SPoul-Henning Kamp #include <sys/malloc.h>
50df8bae1dSRodney W. Grimes #include <sys/proc.h>
512bb95458SMaxime Henrion #include <sys/stddef.h>
5258a24f79SPoul-Henning Kamp #include <sys/sysctl.h>
53df8bae1dSRodney W. Grimes #include <sys/tty.h>
54df8bae1dSRodney W. Grimes #include <sys/syslog.h>
55ce9edcf5SPoul-Henning Kamp #include <sys/cons.h>
56e2a09b26SPoul-Henning Kamp #include <sys/uio.h>
57df8bae1dSRodney W. Grimes 
583ae59505SPoul-Henning Kamp #ifdef DDB
593ae59505SPoul-Henning Kamp #include <ddb/ddb.h>
603ae59505SPoul-Henning Kamp #endif
613ae59505SPoul-Henning Kamp 
62df8bae1dSRodney W. Grimes /*
63df8bae1dSRodney W. Grimes  * Note that stdarg.h and the ANSI style va_start macro is used for both
64df8bae1dSRodney W. Grimes  * ANSI and traditional C compilers.
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes #include <machine/stdarg.h>
67df8bae1dSRodney W. Grimes 
68df8bae1dSRodney W. Grimes #define TOCONS	0x01
69df8bae1dSRodney W. Grimes #define TOTTY	0x02
70df8bae1dSRodney W. Grimes #define TOLOG	0x04
71df8bae1dSRodney W. Grimes 
7282941964SPeter Wemm /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
733b1f7e7dSDag-Erling Smørgrav #define MAXNBUF	(sizeof(intmax_t) * NBBY + 1)
74df8bae1dSRodney W. Grimes 
758245f3f5SArchie Cobbs struct putchar_arg {
768245f3f5SArchie Cobbs 	int	flags;
77e2a09b26SPoul-Henning Kamp 	int	pri;
788245f3f5SArchie Cobbs 	struct	tty *tty;
798245f3f5SArchie Cobbs };
808245f3f5SArchie Cobbs 
818245f3f5SArchie Cobbs struct snprintf_arg {
828245f3f5SArchie Cobbs 	char	*str;
838245f3f5SArchie Cobbs 	size_t	remain;
848245f3f5SArchie Cobbs };
858245f3f5SArchie Cobbs 
86e2a09b26SPoul-Henning Kamp extern	int log_open;
87e2a09b26SPoul-Henning Kamp 
88e2a09b26SPoul-Henning Kamp static void  msglogchar(int c, int pri);
894d77a549SAlfred Perlstein static void  putchar(int ch, void *arg);
903b1f7e7dSDag-Erling Smørgrav static char *ksprintn(char *nbuf, uintmax_t num, int base, int *len);
914d77a549SAlfred Perlstein static void  snprintf_func(int ch, void *arg);
92df8bae1dSRodney W. Grimes 
9387b6de2bSPoul-Henning Kamp static int consintr = 1;		/* Ok to handle console interrupts? */
94e796e00dSPoul-Henning Kamp static int msgbufmapped;		/* Set when safe to use msgbuf */
95e2a09b26SPoul-Henning Kamp int msgbuftrigger;
96df8bae1dSRodney W. Grimes 
97dbe620d3SDavid Malone static int      log_console_output = 1;
98af338beaSDavid Malone TUNABLE_INT("kern.log_console_output", &log_console_output);
99dbe620d3SDavid Malone SYSCTL_INT(_kern, OID_AUTO, log_console_output, CTLFLAG_RW,
1008e1b7974SBrian Feldman     &log_console_output, 0, "Duplicate console output to the syslog.");
1018e1b7974SBrian Feldman 
1028e1b7974SBrian Feldman static int	always_console_output = 0;
1038e1b7974SBrian Feldman TUNABLE_INT("kern.always_console_output", &always_console_output);
1048e1b7974SBrian Feldman SYSCTL_INT(_kern, OID_AUTO, always_console_output, CTLFLAG_RW,
1058e1b7974SBrian Feldman     &always_console_output, 0, "Always output to console despite TIOCCONS.");
106dbe620d3SDavid Malone 
107df8bae1dSRodney W. Grimes /*
108df8bae1dSRodney W. Grimes  * Warn that a system table is full.
109df8bae1dSRodney W. Grimes  */
110df8bae1dSRodney W. Grimes void
111e2a09b26SPoul-Henning Kamp tablefull(const char *tab)
112df8bae1dSRodney W. Grimes {
113df8bae1dSRodney W. Grimes 
114df8bae1dSRodney W. Grimes 	log(LOG_ERR, "%s: table is full\n", tab);
115df8bae1dSRodney W. Grimes }
116df8bae1dSRodney W. Grimes 
117df8bae1dSRodney W. Grimes /*
118df8bae1dSRodney W. Grimes  * Uprintf prints to the controlling terminal for the current process.
119df8bae1dSRodney W. Grimes  * It may block if the tty queue is overfull.  No message is printed if
120df8bae1dSRodney W. Grimes  * the queue does not clear in a reasonable time.
121df8bae1dSRodney W. Grimes  */
122f1550d9dSDoug Rabson int
123df8bae1dSRodney W. Grimes uprintf(const char *fmt, ...)
124df8bae1dSRodney W. Grimes {
125b40ce416SJulian Elischer 	struct thread *td = curthread;
126b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
127df8bae1dSRodney W. Grimes 	va_list ap;
128791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
129f591779bSSeigo Tanimura 	int retval;
130df8bae1dSRodney W. Grimes 
131f591779bSSeigo Tanimura 	if (td == NULL || td == PCPU_GET(idlethread))
132f591779bSSeigo Tanimura 		return (0);
133f591779bSSeigo Tanimura 
134f591779bSSeigo Tanimura 	p = td->td_proc;
135f591779bSSeigo Tanimura 	PROC_LOCK(p);
136f591779bSSeigo Tanimura 	if ((p->p_flag & P_CONTROLT) == 0) {
137f591779bSSeigo Tanimura 		PROC_UNLOCK(p);
138f591779bSSeigo Tanimura 		return (0);
139f591779bSSeigo Tanimura 	}
140f591779bSSeigo Tanimura 	SESS_LOCK(p->p_session);
141791d77e0SPoul-Henning Kamp 	pca.tty = p->p_session->s_ttyp;
142f591779bSSeigo Tanimura 	SESS_UNLOCK(p->p_session);
143f591779bSSeigo Tanimura 	PROC_UNLOCK(p);
144f591779bSSeigo Tanimura 	if (pca.tty == NULL)
145f591779bSSeigo Tanimura 		return (0);
146791d77e0SPoul-Henning Kamp 	pca.flags = TOTTY;
147f591779bSSeigo Tanimura 	va_start(ap, fmt);
148f1550d9dSDoug Rabson 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
149df8bae1dSRodney W. Grimes 	va_end(ap);
150f591779bSSeigo Tanimura 
15158a24f79SPoul-Henning Kamp 	return (retval);
152df8bae1dSRodney W. Grimes }
153df8bae1dSRodney W. Grimes 
154df8bae1dSRodney W. Grimes /*
155df8bae1dSRodney W. Grimes  * tprintf prints on the controlling terminal associated
156a52585d7SPoul-Henning Kamp  * with the given session, possibly to the log as well.
157df8bae1dSRodney W. Grimes  */
158a52585d7SPoul-Henning Kamp void
159a52585d7SPoul-Henning Kamp tprintf(struct proc *p, int pri, const char *fmt, ...)
160df8bae1dSRodney W. Grimes {
161df8bae1dSRodney W. Grimes 	struct tty *tp = NULL;
162b5a2bad1SJohn Baldwin 	int flags = 0;
163df8bae1dSRodney W. Grimes 	va_list ap;
164791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
165b5a2bad1SJohn Baldwin 	struct session *sess = NULL;
166df8bae1dSRodney W. Grimes 
167e2a09b26SPoul-Henning Kamp 	if (pri != -1)
168a52585d7SPoul-Henning Kamp 		flags |= TOLOG;
169f591779bSSeigo Tanimura 	if (p != NULL) {
170f591779bSSeigo Tanimura 		PROC_LOCK(p);
171f591779bSSeigo Tanimura 		if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
172b5a2bad1SJohn Baldwin 			sess = p->p_session;
173b5a2bad1SJohn Baldwin 			SESS_LOCK(sess);
174f591779bSSeigo Tanimura 			PROC_UNLOCK(p);
175b5a2bad1SJohn Baldwin 			SESSHOLD(sess);
176b5a2bad1SJohn Baldwin 			tp = sess->s_ttyp;
177b5a2bad1SJohn Baldwin 			SESS_UNLOCK(sess);
178f591779bSSeigo Tanimura 			if (ttycheckoutq(tp, 0))
179f591779bSSeigo Tanimura 				flags |= TOTTY;
180f591779bSSeigo Tanimura 			else
181f591779bSSeigo Tanimura 				tp = NULL;
182f591779bSSeigo Tanimura 		} else
183f591779bSSeigo Tanimura 			PROC_UNLOCK(p);
184a52585d7SPoul-Henning Kamp 	}
185e2a09b26SPoul-Henning Kamp 	pca.pri = pri;
186791d77e0SPoul-Henning Kamp 	pca.tty = tp;
187791d77e0SPoul-Henning Kamp 	pca.flags = flags;
188a52585d7SPoul-Henning Kamp 	va_start(ap, fmt);
18974f1af01SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
190df8bae1dSRodney W. Grimes 	va_end(ap);
191b5a2bad1SJohn Baldwin 	if (sess != NULL) {
192b5a2bad1SJohn Baldwin 		SESS_LOCK(sess);
193b5a2bad1SJohn Baldwin 		SESSRELE(sess);
194b5a2bad1SJohn Baldwin 		SESS_UNLOCK(sess);
195f591779bSSeigo Tanimura 	}
196e2a09b26SPoul-Henning Kamp 	msgbuftrigger = 1;
197df8bae1dSRodney W. Grimes }
198df8bae1dSRodney W. Grimes 
199df8bae1dSRodney W. Grimes /*
200df8bae1dSRodney W. Grimes  * Ttyprintf displays a message on a tty; it should be used only by
201df8bae1dSRodney W. Grimes  * the tty driver, or anything that knows the underlying tty will not
202df8bae1dSRodney W. Grimes  * be revoke(2)'d away.  Other callers should use tprintf.
203df8bae1dSRodney W. Grimes  */
204f1550d9dSDoug Rabson int
205df8bae1dSRodney W. Grimes ttyprintf(struct tty *tp, const char *fmt, ...)
206df8bae1dSRodney W. Grimes {
207df8bae1dSRodney W. Grimes 	va_list ap;
208791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
209f1550d9dSDoug Rabson 	int retval;
210f1550d9dSDoug Rabson 
211df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
212791d77e0SPoul-Henning Kamp 	pca.tty = tp;
213791d77e0SPoul-Henning Kamp 	pca.flags = TOTTY;
214f1550d9dSDoug Rabson 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
215df8bae1dSRodney W. Grimes 	va_end(ap);
21658a24f79SPoul-Henning Kamp 	return (retval);
217df8bae1dSRodney W. Grimes }
218df8bae1dSRodney W. Grimes 
219df8bae1dSRodney W. Grimes /*
220df8bae1dSRodney W. Grimes  * Log writes to the log buffer, and guarantees not to sleep (so can be
221df8bae1dSRodney W. Grimes  * called by interrupt routines).  If there is no process reading the
222df8bae1dSRodney W. Grimes  * log yet, it writes to the console also.
223df8bae1dSRodney W. Grimes  */
2244d88c459SPoul-Henning Kamp void
225df8bae1dSRodney W. Grimes log(int level, const char *fmt, ...)
226df8bae1dSRodney W. Grimes {
227df8bae1dSRodney W. Grimes 	va_list ap;
228791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
229e2a09b26SPoul-Henning Kamp 
230791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
231e2a09b26SPoul-Henning Kamp 	pca.pri = level;
232e2a09b26SPoul-Henning Kamp 	pca.flags = log_open ? TOLOG : TOCONS;
233e2a09b26SPoul-Henning Kamp 
234e2a09b26SPoul-Henning Kamp 	va_start(ap, fmt);
23574f1af01SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
236df8bae1dSRodney W. Grimes 	va_end(ap);
237e2a09b26SPoul-Henning Kamp 
238b80e3b41SPoul-Henning Kamp 	msgbuftrigger = 1;
239df8bae1dSRodney W. Grimes }
240df8bae1dSRodney W. Grimes 
241e2a09b26SPoul-Henning Kamp #define CONSCHUNK 128
242e2a09b26SPoul-Henning Kamp 
243e2a09b26SPoul-Henning Kamp void
244e2a09b26SPoul-Henning Kamp log_console(struct uio *uio)
245df8bae1dSRodney W. Grimes {
246e2a09b26SPoul-Henning Kamp 	int c, i, error, iovlen, nl;
247e2a09b26SPoul-Henning Kamp 	struct uio muio;
248e2a09b26SPoul-Henning Kamp 	struct iovec *miov = NULL;
249e2a09b26SPoul-Henning Kamp 	char *consbuffer;
250e2a09b26SPoul-Henning Kamp 	int pri;
251df8bae1dSRodney W. Grimes 
252dbe620d3SDavid Malone 	if (!log_console_output)
253dbe620d3SDavid Malone 		return;
254dbe620d3SDavid Malone 
255e2a09b26SPoul-Henning Kamp 	pri = LOG_INFO | LOG_CONSOLE;
256e2a09b26SPoul-Henning Kamp 	muio = *uio;
257e2a09b26SPoul-Henning Kamp 	iovlen = uio->uio_iovcnt * sizeof (struct iovec);
258a163d034SWarner Losh 	MALLOC(miov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
259a163d034SWarner Losh 	MALLOC(consbuffer, char *, CONSCHUNK, M_TEMP, M_WAITOK);
26080208239SAlfred Perlstein 	bcopy(muio.uio_iov, miov, iovlen);
261e2a09b26SPoul-Henning Kamp 	muio.uio_iov = miov;
262e2a09b26SPoul-Henning Kamp 	uio = &muio;
263e2a09b26SPoul-Henning Kamp 
264e2a09b26SPoul-Henning Kamp 	nl = 0;
265e2a09b26SPoul-Henning Kamp 	while (uio->uio_resid > 0) {
266e2a09b26SPoul-Henning Kamp 		c = imin(uio->uio_resid, CONSCHUNK);
267e2a09b26SPoul-Henning Kamp 		error = uiomove(consbuffer, c, uio);
268e2a09b26SPoul-Henning Kamp 		if (error != 0)
2692f9752e9SPoul-Henning Kamp 			break;
270e2a09b26SPoul-Henning Kamp 		for (i = 0; i < c; i++) {
271e2a09b26SPoul-Henning Kamp 			msglogchar(consbuffer[i], pri);
272e2a09b26SPoul-Henning Kamp 			if (consbuffer[i] == '\n')
273e2a09b26SPoul-Henning Kamp 				nl = 1;
274e2a09b26SPoul-Henning Kamp 			else
275e2a09b26SPoul-Henning Kamp 				nl = 0;
276e2a09b26SPoul-Henning Kamp 		}
277e2a09b26SPoul-Henning Kamp 	}
278e2a09b26SPoul-Henning Kamp 	if (!nl)
279e2a09b26SPoul-Henning Kamp 		msglogchar('\n', pri);
280b80e3b41SPoul-Henning Kamp 	msgbuftrigger = 1;
281e2a09b26SPoul-Henning Kamp 	FREE(miov, M_TEMP);
282e2a09b26SPoul-Henning Kamp 	FREE(consbuffer, M_TEMP);
283e2a09b26SPoul-Henning Kamp 	return;
284df8bae1dSRodney W. Grimes }
285df8bae1dSRodney W. Grimes 
2866ddbf1e2SGary Palmer int
287df8bae1dSRodney W. Grimes printf(const char *fmt, ...)
288df8bae1dSRodney W. Grimes {
289df8bae1dSRodney W. Grimes 	va_list ap;
2904d88c459SPoul-Henning Kamp 	int savintr;
291791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
29265ed8cbdSJustin T. Gibbs 	int retval;
293df8bae1dSRodney W. Grimes 
294df8bae1dSRodney W. Grimes 	savintr = consintr;		/* disable interrupts */
295df8bae1dSRodney W. Grimes 	consintr = 0;
296df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
297791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
298791d77e0SPoul-Henning Kamp 	pca.flags = TOCONS | TOLOG;
299e2a09b26SPoul-Henning Kamp 	pca.pri = -1;
30065ed8cbdSJustin T. Gibbs 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
301df8bae1dSRodney W. Grimes 	va_end(ap);
302df8bae1dSRodney W. Grimes 	if (!panicstr)
303b80e3b41SPoul-Henning Kamp 		msgbuftrigger = 1;
304df8bae1dSRodney W. Grimes 	consintr = savintr;		/* reenable interrupts */
30558a24f79SPoul-Henning Kamp 	return (retval);
306df8bae1dSRodney W. Grimes }
307df8bae1dSRodney W. Grimes 
308f1550d9dSDoug Rabson int
309791d77e0SPoul-Henning Kamp vprintf(const char *fmt, va_list ap)
310791d77e0SPoul-Henning Kamp {
3114d88c459SPoul-Henning Kamp 	int savintr;
312791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
313f1550d9dSDoug Rabson 	int retval;
314791d77e0SPoul-Henning Kamp 
315791d77e0SPoul-Henning Kamp 	savintr = consintr;		/* disable interrupts */
316791d77e0SPoul-Henning Kamp 	consintr = 0;
317791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
318791d77e0SPoul-Henning Kamp 	pca.flags = TOCONS | TOLOG;
319e2a09b26SPoul-Henning Kamp 	pca.pri = -1;
320f1550d9dSDoug Rabson 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
321791d77e0SPoul-Henning Kamp 	if (!panicstr)
322b80e3b41SPoul-Henning Kamp 		msgbuftrigger = 1;
323791d77e0SPoul-Henning Kamp 	consintr = savintr;		/* reenable interrupts */
32458a24f79SPoul-Henning Kamp 	return (retval);
325791d77e0SPoul-Henning Kamp }
326791d77e0SPoul-Henning Kamp 
327791d77e0SPoul-Henning Kamp /*
328791d77e0SPoul-Henning Kamp  * Print a character on console or users terminal.  If destination is
329e796e00dSPoul-Henning Kamp  * the console then the last bunch of characters are saved in msgbuf for
330791d77e0SPoul-Henning Kamp  * inspection later.
331791d77e0SPoul-Henning Kamp  */
332791d77e0SPoul-Henning Kamp static void
333791d77e0SPoul-Henning Kamp putchar(int c, void *arg)
334791d77e0SPoul-Henning Kamp {
335791d77e0SPoul-Henning Kamp 	struct putchar_arg *ap = (struct putchar_arg*) arg;
336791d77e0SPoul-Henning Kamp 	struct tty *tp = ap->tty;
337d29bf12fSIan Dowse 	int consdirect, flags = ap->flags;
338d29bf12fSIan Dowse 
339d29bf12fSIan Dowse 	consdirect = ((flags & TOCONS) && constty == NULL);
340adef9265SIan Dowse 	/* Don't use the tty code after a panic or while in ddb. */
341791d77e0SPoul-Henning Kamp 	if (panicstr)
342d29bf12fSIan Dowse 		consdirect = 1;
343adef9265SIan Dowse #ifdef DDB
344adef9265SIan Dowse 	if (db_active)
345adef9265SIan Dowse 		consdirect = 1;
346adef9265SIan Dowse #endif
347d29bf12fSIan Dowse 	if (consdirect) {
348d29bf12fSIan Dowse 		if (c != '\0')
349d29bf12fSIan Dowse 			cnputc(c);
350d29bf12fSIan Dowse 	} else {
351d29bf12fSIan Dowse 		if ((flags & TOTTY) && tp != NULL)
352d29bf12fSIan Dowse 			tputchar(c, tp);
3538e1b7974SBrian Feldman 		if (flags & TOCONS) {
3548e1b7974SBrian Feldman 			if (constty != NULL)
355d29bf12fSIan Dowse 				msgbuf_addchar(&consmsgbuf, c);
3568e1b7974SBrian Feldman 			if (always_console_output && c != '\0')
3578e1b7974SBrian Feldman 				cnputc(c);
3588e1b7974SBrian Feldman 		}
359791d77e0SPoul-Henning Kamp 	}
360791d77e0SPoul-Henning Kamp 	if ((flags & TOLOG))
361e2a09b26SPoul-Henning Kamp 		msglogchar(c, ap->pri);
362791d77e0SPoul-Henning Kamp }
363791d77e0SPoul-Henning Kamp 
364791d77e0SPoul-Henning Kamp /*
365791d77e0SPoul-Henning Kamp  * Scaled down version of sprintf(3).
366791d77e0SPoul-Henning Kamp  */
367791d77e0SPoul-Henning Kamp int
368791d77e0SPoul-Henning Kamp sprintf(char *buf, const char *cfmt, ...)
369791d77e0SPoul-Henning Kamp {
370791d77e0SPoul-Henning Kamp 	int retval;
371791d77e0SPoul-Henning Kamp 	va_list ap;
372791d77e0SPoul-Henning Kamp 
373791d77e0SPoul-Henning Kamp 	va_start(ap, cfmt);
374791d77e0SPoul-Henning Kamp 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
375fe96d47dSPoul-Henning Kamp 	buf[retval] = '\0';
376791d77e0SPoul-Henning Kamp 	va_end(ap);
37758a24f79SPoul-Henning Kamp 	return (retval);
378791d77e0SPoul-Henning Kamp }
379791d77e0SPoul-Henning Kamp 
380791d77e0SPoul-Henning Kamp /*
38199237364SAndrey A. Chernov  * Scaled down version of vsprintf(3).
38299237364SAndrey A. Chernov  */
38399237364SAndrey A. Chernov int
38499237364SAndrey A. Chernov vsprintf(char *buf, const char *cfmt, va_list ap)
38599237364SAndrey A. Chernov {
38699237364SAndrey A. Chernov 	int retval;
38799237364SAndrey A. Chernov 
38899237364SAndrey A. Chernov 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
38999237364SAndrey A. Chernov 	buf[retval] = '\0';
39058a24f79SPoul-Henning Kamp 	return (retval);
39199237364SAndrey A. Chernov }
39299237364SAndrey A. Chernov 
39399237364SAndrey A. Chernov /*
3948245f3f5SArchie Cobbs  * Scaled down version of snprintf(3).
3958245f3f5SArchie Cobbs  */
3968245f3f5SArchie Cobbs int
3978245f3f5SArchie Cobbs snprintf(char *str, size_t size, const char *format, ...)
3988245f3f5SArchie Cobbs {
3998245f3f5SArchie Cobbs 	int retval;
4008245f3f5SArchie Cobbs 	va_list ap;
4018245f3f5SArchie Cobbs 
4028245f3f5SArchie Cobbs 	va_start(ap, format);
4038245f3f5SArchie Cobbs 	retval = vsnprintf(str, size, format, ap);
4048245f3f5SArchie Cobbs 	va_end(ap);
4058245f3f5SArchie Cobbs 	return(retval);
4068245f3f5SArchie Cobbs }
4078245f3f5SArchie Cobbs 
4088245f3f5SArchie Cobbs /*
4098245f3f5SArchie Cobbs  * Scaled down version of vsnprintf(3).
4108245f3f5SArchie Cobbs  */
4118245f3f5SArchie Cobbs int
4128245f3f5SArchie Cobbs vsnprintf(char *str, size_t size, const char *format, va_list ap)
4138245f3f5SArchie Cobbs {
4148245f3f5SArchie Cobbs 	struct snprintf_arg info;
4158245f3f5SArchie Cobbs 	int retval;
4168245f3f5SArchie Cobbs 
4178245f3f5SArchie Cobbs 	info.str = str;
4188245f3f5SArchie Cobbs 	info.remain = size;
4198245f3f5SArchie Cobbs 	retval = kvprintf(format, snprintf_func, &info, 10, ap);
4208245f3f5SArchie Cobbs 	if (info.remain >= 1)
4218245f3f5SArchie Cobbs 		*info.str++ = '\0';
42258a24f79SPoul-Henning Kamp 	return (retval);
4238245f3f5SArchie Cobbs }
4248245f3f5SArchie Cobbs 
4258751a8c7SPoul-Henning Kamp /*
4268751a8c7SPoul-Henning Kamp  * Kernel version which takes radix argument vsnprintf(3).
4278751a8c7SPoul-Henning Kamp  */
4288751a8c7SPoul-Henning Kamp int
4298751a8c7SPoul-Henning Kamp vsnrprintf(char *str, size_t size, int radix, const char *format, va_list ap)
4308751a8c7SPoul-Henning Kamp {
4318751a8c7SPoul-Henning Kamp 	struct snprintf_arg info;
4328751a8c7SPoul-Henning Kamp 	int retval;
4338751a8c7SPoul-Henning Kamp 
4348751a8c7SPoul-Henning Kamp 	info.str = str;
4358751a8c7SPoul-Henning Kamp 	info.remain = size;
4368751a8c7SPoul-Henning Kamp 	retval = kvprintf(format, snprintf_func, &info, radix, ap);
4378751a8c7SPoul-Henning Kamp 	if (info.remain >= 1)
4388751a8c7SPoul-Henning Kamp 		*info.str++ = '\0';
4398751a8c7SPoul-Henning Kamp 	return (retval);
4408751a8c7SPoul-Henning Kamp }
4418751a8c7SPoul-Henning Kamp 
4428245f3f5SArchie Cobbs static void
4438245f3f5SArchie Cobbs snprintf_func(int ch, void *arg)
4448245f3f5SArchie Cobbs {
4458245f3f5SArchie Cobbs 	struct snprintf_arg *const info = arg;
4468245f3f5SArchie Cobbs 
4478245f3f5SArchie Cobbs 	if (info->remain >= 2) {
4488245f3f5SArchie Cobbs 		*info->str++ = ch;
4498245f3f5SArchie Cobbs 		info->remain--;
4508245f3f5SArchie Cobbs 	}
4518245f3f5SArchie Cobbs }
4528245f3f5SArchie Cobbs 
4538245f3f5SArchie Cobbs /*
45482941964SPeter Wemm  * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
45505292ba2SArchie Cobbs  * order; return an optional length and a pointer to the last character
45605292ba2SArchie Cobbs  * written in the buffer (i.e., the first character of the string).
45705292ba2SArchie Cobbs  * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
458791d77e0SPoul-Henning Kamp  */
459791d77e0SPoul-Henning Kamp static char *
460f16a5176SDag-Erling Smørgrav ksprintn(char *nbuf, uintmax_t num, int base, int *lenp)
46105292ba2SArchie Cobbs {
4624d88c459SPoul-Henning Kamp 	char *p;
463791d77e0SPoul-Henning Kamp 
46405292ba2SArchie Cobbs 	p = nbuf;
465ad4f8dbdSArchie Cobbs 	*p = '\0';
466791d77e0SPoul-Henning Kamp 	do {
4673b1f7e7dSDag-Erling Smørgrav 		*++p = hex2ascii(num % base);
4683b1f7e7dSDag-Erling Smørgrav 	} while (num /= base);
4697d921a01SPeter Wemm 	if (lenp)
4707d921a01SPeter Wemm 		*lenp = p - nbuf;
4717d921a01SPeter Wemm 	return (p);
4727d921a01SPeter Wemm }
473791d77e0SPoul-Henning Kamp 
474df8bae1dSRodney W. Grimes /*
475df8bae1dSRodney W. Grimes  * Scaled down version of printf(3).
476df8bae1dSRodney W. Grimes  *
477df8bae1dSRodney W. Grimes  * Two additional formats:
478df8bae1dSRodney W. Grimes  *
479df8bae1dSRodney W. Grimes  * The format %b is supported to decode error registers.
480df8bae1dSRodney W. Grimes  * Its usage is:
481df8bae1dSRodney W. Grimes  *
482df8bae1dSRodney W. Grimes  *	printf("reg=%b\n", regval, "<base><arg>*");
483df8bae1dSRodney W. Grimes  *
484df8bae1dSRodney W. Grimes  * where <base> is the output base expressed as a control character, e.g.
485df8bae1dSRodney W. Grimes  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
486df8bae1dSRodney W. Grimes  * the first of which gives the bit number to be inspected (origin 1), and
487df8bae1dSRodney W. Grimes  * the next characters (up to a control character, i.e. a character <= 32),
488df8bae1dSRodney W. Grimes  * give the name of the register.  Thus:
489df8bae1dSRodney W. Grimes  *
4904e31a37bSGary Palmer  *	kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
491df8bae1dSRodney W. Grimes  *
492df8bae1dSRodney W. Grimes  * would produce output:
493df8bae1dSRodney W. Grimes  *
494df8bae1dSRodney W. Grimes  *	reg=3<BITTWO,BITONE>
495df8bae1dSRodney W. Grimes  *
496120f0783SPoul-Henning Kamp  * XXX:  %D  -- Hexdump, takes pointer and separator string:
497120f0783SPoul-Henning Kamp  *		("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
498120f0783SPoul-Henning Kamp  *		("%*D", len, ptr, " " -> XX XX XX XX ...
499df8bae1dSRodney W. Grimes  */
500791d77e0SPoul-Henning Kamp int
501791d77e0SPoul-Henning Kamp kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
502df8bae1dSRodney W. Grimes {
503791d77e0SPoul-Henning Kamp #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
504ad4f8dbdSArchie Cobbs 	char nbuf[MAXNBUF];
5053b1f7e7dSDag-Erling Smørgrav 	char *d;
5063b1f7e7dSDag-Erling Smørgrav 	const char *p, *percent, *q;
507120f0783SPoul-Henning Kamp 	u_char *up;
508791d77e0SPoul-Henning Kamp 	int ch, n;
5093b1f7e7dSDag-Erling Smørgrav 	uintmax_t num;
5107d921a01SPeter Wemm 	int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
51132869e71SNate Lawson 	int cflag, hflag, jflag, tflag, zflag;
5124f20e4b1SPoul-Henning Kamp 	int dwidth;
513df8bae1dSRodney W. Grimes 	char padc;
514791d77e0SPoul-Henning Kamp 	int retval = 0;
515791d77e0SPoul-Henning Kamp 
5163b1f7e7dSDag-Erling Smørgrav 	num = 0;
517fe96d47dSPoul-Henning Kamp 	if (!func)
518791d77e0SPoul-Henning Kamp 		d = (char *) arg;
519791d77e0SPoul-Henning Kamp 	else
520fe96d47dSPoul-Henning Kamp 		d = NULL;
5218f5067baSDavid Greenman 
5228f5067baSDavid Greenman 	if (fmt == NULL)
5232336b9d7SBruce Evans 		fmt = "(fmt null)\n";
524b4b2f81eSPoul-Henning Kamp 
525120f0783SPoul-Henning Kamp 	if (radix < 2 || radix > 36)
526b4b2f81eSPoul-Henning Kamp 		radix = 10;
527b4b2f81eSPoul-Henning Kamp 
528df8bae1dSRodney W. Grimes 	for (;;) {
529df8bae1dSRodney W. Grimes 		padc = ' ';
530df8bae1dSRodney W. Grimes 		width = 0;
531e0c95ed9SBruce Evans 		while ((ch = (u_char)*fmt++) != '%') {
532df8bae1dSRodney W. Grimes 			if (ch == '\0')
53358a24f79SPoul-Henning Kamp 				return (retval);
534791d77e0SPoul-Henning Kamp 			PCHAR(ch);
535df8bae1dSRodney W. Grimes 		}
5363b1f7e7dSDag-Erling Smørgrav 		percent = fmt - 1;
5377d921a01SPeter Wemm 		qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
5384f20e4b1SPoul-Henning Kamp 		sign = 0; dot = 0; dwidth = 0;
53932869e71SNate Lawson 		cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
540e0c95ed9SBruce Evans reswitch:	switch (ch = (u_char)*fmt++) {
5414830092aSPoul-Henning Kamp 		case '.':
5424830092aSPoul-Henning Kamp 			dot = 1;
5434830092aSPoul-Henning Kamp 			goto reswitch;
544791d77e0SPoul-Henning Kamp 		case '#':
545791d77e0SPoul-Henning Kamp 			sharpflag = 1;
546791d77e0SPoul-Henning Kamp 			goto reswitch;
547791d77e0SPoul-Henning Kamp 		case '+':
548791d77e0SPoul-Henning Kamp 			sign = 1;
549791d77e0SPoul-Henning Kamp 			goto reswitch;
550791d77e0SPoul-Henning Kamp 		case '-':
551791d77e0SPoul-Henning Kamp 			ladjust = 1;
552791d77e0SPoul-Henning Kamp 			goto reswitch;
553791d77e0SPoul-Henning Kamp 		case '%':
554791d77e0SPoul-Henning Kamp 			PCHAR(ch);
555791d77e0SPoul-Henning Kamp 			break;
556791d77e0SPoul-Henning Kamp 		case '*':
557ed71c342SPoul-Henning Kamp 			if (!dot) {
558791d77e0SPoul-Henning Kamp 				width = va_arg(ap, int);
559791d77e0SPoul-Henning Kamp 				if (width < 0) {
560791d77e0SPoul-Henning Kamp 					ladjust = !ladjust;
561791d77e0SPoul-Henning Kamp 					width = -width;
562791d77e0SPoul-Henning Kamp 				}
563ed71c342SPoul-Henning Kamp 			} else {
564ed71c342SPoul-Henning Kamp 				dwidth = va_arg(ap, int);
565ed71c342SPoul-Henning Kamp 			}
566791d77e0SPoul-Henning Kamp 			goto reswitch;
567df8bae1dSRodney W. Grimes 		case '0':
5684f20e4b1SPoul-Henning Kamp 			if (!dot) {
569df8bae1dSRodney W. Grimes 				padc = '0';
570df8bae1dSRodney W. Grimes 				goto reswitch;
5714f20e4b1SPoul-Henning Kamp 			}
572df8bae1dSRodney W. Grimes 		case '1': case '2': case '3': case '4':
573df8bae1dSRodney W. Grimes 		case '5': case '6': case '7': case '8': case '9':
5744f20e4b1SPoul-Henning Kamp 				for (n = 0;; ++fmt) {
5754f20e4b1SPoul-Henning Kamp 					n = n * 10 + ch - '0';
576df8bae1dSRodney W. Grimes 					ch = *fmt;
577df8bae1dSRodney W. Grimes 					if (ch < '0' || ch > '9')
578df8bae1dSRodney W. Grimes 						break;
579df8bae1dSRodney W. Grimes 				}
5804f20e4b1SPoul-Henning Kamp 			if (dot)
5814f20e4b1SPoul-Henning Kamp 				dwidth = n;
5824f20e4b1SPoul-Henning Kamp 			else
5834f20e4b1SPoul-Henning Kamp 				width = n;
584df8bae1dSRodney W. Grimes 			goto reswitch;
585df8bae1dSRodney W. Grimes 		case 'b':
586377a66bcSJohn Baldwin 			num = (u_int)va_arg(ap, int);
587df8bae1dSRodney W. Grimes 			p = va_arg(ap, char *);
5883b1f7e7dSDag-Erling Smørgrav 			for (q = ksprintn(nbuf, num, *p++, NULL); *q;)
589791d77e0SPoul-Henning Kamp 				PCHAR(*q--);
590df8bae1dSRodney W. Grimes 
5913b1f7e7dSDag-Erling Smørgrav 			if (num == 0)
592df8bae1dSRodney W. Grimes 				break;
593df8bae1dSRodney W. Grimes 
594797f2d22SPoul-Henning Kamp 			for (tmp = 0; *p;) {
595797f2d22SPoul-Henning Kamp 				n = *p++;
5963b1f7e7dSDag-Erling Smørgrav 				if (num & (1 << (n - 1))) {
597791d77e0SPoul-Henning Kamp 					PCHAR(tmp ? ',' : '<');
598df8bae1dSRodney W. Grimes 					for (; (n = *p) > ' '; ++p)
599791d77e0SPoul-Henning Kamp 						PCHAR(n);
600df8bae1dSRodney W. Grimes 					tmp = 1;
601df8bae1dSRodney W. Grimes 				} else
602df8bae1dSRodney W. Grimes 					for (; *p > ' '; ++p)
603df8bae1dSRodney W. Grimes 						continue;
604df8bae1dSRodney W. Grimes 			}
605df8bae1dSRodney W. Grimes 			if (tmp)
606791d77e0SPoul-Henning Kamp 				PCHAR('>');
607df8bae1dSRodney W. Grimes 			break;
608df8bae1dSRodney W. Grimes 		case 'c':
609791d77e0SPoul-Henning Kamp 			PCHAR(va_arg(ap, int));
610df8bae1dSRodney W. Grimes 			break;
611120f0783SPoul-Henning Kamp 		case 'D':
612120f0783SPoul-Henning Kamp 			up = va_arg(ap, u_char *);
613120f0783SPoul-Henning Kamp 			p = va_arg(ap, char *);
614120f0783SPoul-Henning Kamp 			if (!width)
615120f0783SPoul-Henning Kamp 				width = 16;
616120f0783SPoul-Henning Kamp 			while(width--) {
617120f0783SPoul-Henning Kamp 				PCHAR(hex2ascii(*up >> 4));
618120f0783SPoul-Henning Kamp 				PCHAR(hex2ascii(*up & 0x0f));
619120f0783SPoul-Henning Kamp 				up++;
620120f0783SPoul-Henning Kamp 				if (width)
621120f0783SPoul-Henning Kamp 					for (q=p;*q;q++)
622120f0783SPoul-Henning Kamp 						PCHAR(*q);
623120f0783SPoul-Henning Kamp 			}
624120f0783SPoul-Henning Kamp 			break;
625df8bae1dSRodney W. Grimes 		case 'd':
626e0b74464SWarner Losh 		case 'i':
627df8bae1dSRodney W. Grimes 			base = 10;
6283b1f7e7dSDag-Erling Smørgrav 			sign = 1;
6293b1f7e7dSDag-Erling Smørgrav 			goto handle_sign;
63032869e71SNate Lawson 		case 'h':
63132869e71SNate Lawson 			if (hflag) {
63232869e71SNate Lawson 				hflag = 0;
63332869e71SNate Lawson 				cflag = 1;
63432869e71SNate Lawson 			} else
63532869e71SNate Lawson 				hflag = 1;
63632869e71SNate Lawson 			goto reswitch;
6373b1f7e7dSDag-Erling Smørgrav 		case 'j':
6383b1f7e7dSDag-Erling Smørgrav 			jflag = 1;
6393b1f7e7dSDag-Erling Smørgrav 			goto reswitch;
640791d77e0SPoul-Henning Kamp 		case 'l':
641301ca4ffSBrian Feldman 			if (lflag) {
642301ca4ffSBrian Feldman 				lflag = 0;
643301ca4ffSBrian Feldman 				qflag = 1;
644301ca4ffSBrian Feldman 			} else
645791d77e0SPoul-Henning Kamp 				lflag = 1;
646791d77e0SPoul-Henning Kamp 			goto reswitch;
6473b1f7e7dSDag-Erling Smørgrav 		case 'n':
6483b1f7e7dSDag-Erling Smørgrav 			if (jflag)
6493b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, intmax_t *)) = retval;
6503b1f7e7dSDag-Erling Smørgrav 			else if (qflag)
6513b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, quad_t *)) = retval;
6527d921a01SPeter Wemm 			else if (lflag)
6533b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, long *)) = retval;
6544578a2e6SMaxime Henrion 			else if (zflag)
6554578a2e6SMaxime Henrion 				*(va_arg(ap, size_t *)) = retval;
65632869e71SNate Lawson 			else if (hflag)
65732869e71SNate Lawson 				*(va_arg(ap, short *)) = retval;
65832869e71SNate Lawson 			else if (cflag)
65932869e71SNate Lawson 				*(va_arg(ap, char *)) = retval;
6607d921a01SPeter Wemm 			else
6613b1f7e7dSDag-Erling Smørgrav 				*(va_arg(ap, int *)) = retval;
6623b1f7e7dSDag-Erling Smørgrav 			break;
6633b1f7e7dSDag-Erling Smørgrav 		case 'o':
664df8bae1dSRodney W. Grimes 			base = 8;
6653b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
66612d17f65SPoul-Henning Kamp 		case 'p':
66712d17f65SPoul-Henning Kamp 			base = 16;
668c41141b0SBruce Evans 			sharpflag = (width == 0);
6693b1f7e7dSDag-Erling Smørgrav 			sign = 0;
6703b1f7e7dSDag-Erling Smørgrav 			num = (uintptr_t)va_arg(ap, void *);
6713b1f7e7dSDag-Erling Smørgrav 			goto number;
6727d921a01SPeter Wemm 		case 'q':
6737d921a01SPeter Wemm 			qflag = 1;
6747d921a01SPeter Wemm 			goto reswitch;
675e0c38587SBruce Evans 		case 'r':
676e0c38587SBruce Evans 			base = radix;
6773b1f7e7dSDag-Erling Smørgrav 			if (sign)
6783b1f7e7dSDag-Erling Smørgrav 				goto handle_sign;
6793b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
680791d77e0SPoul-Henning Kamp 		case 's':
681791d77e0SPoul-Henning Kamp 			p = va_arg(ap, char *);
682791d77e0SPoul-Henning Kamp 			if (p == NULL)
683791d77e0SPoul-Henning Kamp 				p = "(null)";
6844830092aSPoul-Henning Kamp 			if (!dot)
6854830092aSPoul-Henning Kamp 				n = strlen (p);
6864830092aSPoul-Henning Kamp 			else
6874f20e4b1SPoul-Henning Kamp 				for (n = 0; n < dwidth && p[n]; n++)
6884830092aSPoul-Henning Kamp 					continue;
6894f20e4b1SPoul-Henning Kamp 
6904830092aSPoul-Henning Kamp 			width -= n;
6914f20e4b1SPoul-Henning Kamp 
692791d77e0SPoul-Henning Kamp 			if (!ladjust && width > 0)
693791d77e0SPoul-Henning Kamp 				while (width--)
694791d77e0SPoul-Henning Kamp 					PCHAR(padc);
6954830092aSPoul-Henning Kamp 			while (n--)
696791d77e0SPoul-Henning Kamp 				PCHAR(*p++);
697791d77e0SPoul-Henning Kamp 			if (ladjust && width > 0)
698791d77e0SPoul-Henning Kamp 				while (width--)
699791d77e0SPoul-Henning Kamp 					PCHAR(padc);
700791d77e0SPoul-Henning Kamp 			break;
7012bb95458SMaxime Henrion 		case 't':
7022bb95458SMaxime Henrion 			tflag = 1;
7032bb95458SMaxime Henrion 			goto reswitch;
704f53dbe97SBruce Evans 		case 'u':
705f53dbe97SBruce Evans 			base = 10;
7063b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
707df8bae1dSRodney W. Grimes 		case 'x':
708aa998012SMike Smith 		case 'X':
709df8bae1dSRodney W. Grimes 			base = 16;
7103b1f7e7dSDag-Erling Smørgrav 			goto handle_nosign;
7114578a2e6SMaxime Henrion 		case 'y':
712e0c38587SBruce Evans 			base = 16;
71385594430SJohn Baldwin 			sign = 1;
7143b1f7e7dSDag-Erling Smørgrav 			goto handle_sign;
7154578a2e6SMaxime Henrion 		case 'z':
7164578a2e6SMaxime Henrion 			zflag = 1;
7174578a2e6SMaxime Henrion 			goto reswitch;
7183b1f7e7dSDag-Erling Smørgrav handle_nosign:
7193b1f7e7dSDag-Erling Smørgrav 			sign = 0;
7203b1f7e7dSDag-Erling Smørgrav 			if (jflag)
7213b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, uintmax_t);
7223b1f7e7dSDag-Erling Smørgrav 			else if (qflag)
7233b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, u_quad_t);
7242bb95458SMaxime Henrion 			else if (tflag)
7252bb95458SMaxime Henrion 				num = va_arg(ap, ptrdiff_t);
7263b1f7e7dSDag-Erling Smørgrav 			else if (lflag)
7273b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, u_long);
7284578a2e6SMaxime Henrion 			else if (zflag)
7294578a2e6SMaxime Henrion 				num = va_arg(ap, size_t);
73032869e71SNate Lawson 			else if (hflag)
73132869e71SNate Lawson 				num = (u_short)va_arg(ap, int);
73232869e71SNate Lawson 			else if (cflag)
73332869e71SNate Lawson 				num = (u_char)va_arg(ap, int);
7343b1f7e7dSDag-Erling Smørgrav 			else
7353b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, u_int);
736e0c38587SBruce Evans 			goto number;
7373b1f7e7dSDag-Erling Smørgrav handle_sign:
7383b1f7e7dSDag-Erling Smørgrav 			if (jflag)
7393b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, intmax_t);
7403b1f7e7dSDag-Erling Smørgrav 			else if (qflag)
7413b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, quad_t);
7422bb95458SMaxime Henrion 			else if (tflag)
7432bb95458SMaxime Henrion 				num = va_arg(ap, ptrdiff_t);
7443b1f7e7dSDag-Erling Smørgrav 			else if (lflag)
7453b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, long);
7464578a2e6SMaxime Henrion 			else if (zflag)
7474578a2e6SMaxime Henrion 				num = va_arg(ap, size_t);
74832869e71SNate Lawson 			else if (hflag)
74932869e71SNate Lawson 				num = (short)va_arg(ap, int);
75032869e71SNate Lawson 			else if (cflag)
75132869e71SNate Lawson 				num = (char)va_arg(ap, int);
7523b1f7e7dSDag-Erling Smørgrav 			else
7533b1f7e7dSDag-Erling Smørgrav 				num = va_arg(ap, int);
7547d921a01SPeter Wemm number:
7553b1f7e7dSDag-Erling Smørgrav 			if (sign && (intmax_t)num < 0) {
7567d921a01SPeter Wemm 				neg = 1;
7573b1f7e7dSDag-Erling Smørgrav 				num = -(intmax_t)num;
7587d921a01SPeter Wemm 			}
7593b1f7e7dSDag-Erling Smørgrav 			p = ksprintn(nbuf, num, base, &tmp);
7603b1f7e7dSDag-Erling Smørgrav 			if (sharpflag && num != 0) {
761791d77e0SPoul-Henning Kamp 				if (base == 8)
762791d77e0SPoul-Henning Kamp 					tmp++;
763791d77e0SPoul-Henning Kamp 				else if (base == 16)
764791d77e0SPoul-Henning Kamp 					tmp += 2;
765791d77e0SPoul-Henning Kamp 			}
766791d77e0SPoul-Henning Kamp 			if (neg)
767791d77e0SPoul-Henning Kamp 				tmp++;
768791d77e0SPoul-Henning Kamp 
769791d77e0SPoul-Henning Kamp 			if (!ladjust && width && (width -= tmp) > 0)
770df8bae1dSRodney W. Grimes 				while (width--)
771791d77e0SPoul-Henning Kamp 					PCHAR(padc);
772791d77e0SPoul-Henning Kamp 			if (neg)
773791d77e0SPoul-Henning Kamp 				PCHAR('-');
7743b1f7e7dSDag-Erling Smørgrav 			if (sharpflag && num != 0) {
775791d77e0SPoul-Henning Kamp 				if (base == 8) {
776791d77e0SPoul-Henning Kamp 					PCHAR('0');
777791d77e0SPoul-Henning Kamp 				} else if (base == 16) {
778791d77e0SPoul-Henning Kamp 					PCHAR('0');
779791d77e0SPoul-Henning Kamp 					PCHAR('x');
780791d77e0SPoul-Henning Kamp 				}
781791d77e0SPoul-Henning Kamp 			}
782791d77e0SPoul-Henning Kamp 
783797f2d22SPoul-Henning Kamp 			while (*p)
784791d77e0SPoul-Henning Kamp 				PCHAR(*p--);
785791d77e0SPoul-Henning Kamp 
786791d77e0SPoul-Henning Kamp 			if (ladjust && width && (width -= tmp) > 0)
787791d77e0SPoul-Henning Kamp 				while (width--)
788791d77e0SPoul-Henning Kamp 					PCHAR(padc);
789791d77e0SPoul-Henning Kamp 
790df8bae1dSRodney W. Grimes 			break;
791df8bae1dSRodney W. Grimes 		default:
7923b1f7e7dSDag-Erling Smørgrav 			while (percent < fmt)
7933b1f7e7dSDag-Erling Smørgrav 				PCHAR(*percent++);
794791d77e0SPoul-Henning Kamp 			break;
795df8bae1dSRodney W. Grimes 		}
796df8bae1dSRodney W. Grimes 	}
797791d77e0SPoul-Henning Kamp #undef PCHAR
798df8bae1dSRodney W. Grimes }
799df8bae1dSRodney W. Grimes 
800df8bae1dSRodney W. Grimes /*
801e2a09b26SPoul-Henning Kamp  * Put character in log buffer with a particular priority.
802df8bae1dSRodney W. Grimes  */
803df8bae1dSRodney W. Grimes static void
804e2a09b26SPoul-Henning Kamp msglogchar(int c, int pri)
805e2a09b26SPoul-Henning Kamp {
806e2a09b26SPoul-Henning Kamp 	static int lastpri = -1;
807e2a09b26SPoul-Henning Kamp 	static int dangling;
808e2a09b26SPoul-Henning Kamp 	char nbuf[MAXNBUF];
809e2a09b26SPoul-Henning Kamp 	char *p;
810e2a09b26SPoul-Henning Kamp 
811e2a09b26SPoul-Henning Kamp 	if (!msgbufmapped)
812e2a09b26SPoul-Henning Kamp 		return;
813e2a09b26SPoul-Henning Kamp 	if (c == '\0' || c == '\r')
814e2a09b26SPoul-Henning Kamp 		return;
815e2a09b26SPoul-Henning Kamp 	if (pri != -1 && pri != lastpri) {
816e2a09b26SPoul-Henning Kamp 		if (dangling) {
8174784a469SIan Dowse 			msgbuf_addchar(msgbufp, '\n');
818e2a09b26SPoul-Henning Kamp 			dangling = 0;
819e2a09b26SPoul-Henning Kamp 		}
8204784a469SIan Dowse 		msgbuf_addchar(msgbufp, '<');
8213b1f7e7dSDag-Erling Smørgrav 		for (p = ksprintn(nbuf, (uintmax_t)pri, 10, NULL); *p;)
8224784a469SIan Dowse 			msgbuf_addchar(msgbufp, *p--);
8234784a469SIan Dowse 		msgbuf_addchar(msgbufp, '>');
824e2a09b26SPoul-Henning Kamp 		lastpri = pri;
825e2a09b26SPoul-Henning Kamp 	}
8264784a469SIan Dowse 	msgbuf_addchar(msgbufp, c);
827e2a09b26SPoul-Henning Kamp 	if (c == '\n') {
828e2a09b26SPoul-Henning Kamp 		dangling = 0;
829e2a09b26SPoul-Henning Kamp 		lastpri = -1;
830e2a09b26SPoul-Henning Kamp 	} else {
831e2a09b26SPoul-Henning Kamp 		dangling = 1;
832e2a09b26SPoul-Henning Kamp 	}
833e2a09b26SPoul-Henning Kamp }
834e2a09b26SPoul-Henning Kamp 
835e796e00dSPoul-Henning Kamp void
83601ee4395SThomas Moestl msgbufinit(void *ptr, int size)
837e796e00dSPoul-Henning Kamp {
838e796e00dSPoul-Henning Kamp 	char *cp;
839eb9d435aSJonathan Lemon 	static struct msgbuf *oldp = NULL;
840e796e00dSPoul-Henning Kamp 
841cca8f980SIan Dowse 	size -= sizeof(*msgbufp);
842e796e00dSPoul-Henning Kamp 	cp = (char *)ptr;
843cca8f980SIan Dowse 	msgbufp = (struct msgbuf *)(cp + size);
8444784a469SIan Dowse 	msgbuf_reinit(msgbufp, cp, size);
845eb9d435aSJonathan Lemon 	if (msgbufmapped && oldp != msgbufp)
8464784a469SIan Dowse 		msgbuf_copy(oldp, msgbufp);
847e796e00dSPoul-Henning Kamp 	msgbufmapped = 1;
848eb9d435aSJonathan Lemon 	oldp = msgbufp;
849e796e00dSPoul-Henning Kamp }
850e796e00dSPoul-Henning Kamp 
851d0615c64SAndrew R. Reiter SYSCTL_DECL(_security_bsd);
8526f3933faSRobert Watson 
8536f3933faSRobert Watson static int unprivileged_read_msgbuf = 1;
854d0615c64SAndrew R. Reiter SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_read_msgbuf,
8556f3933faSRobert Watson     CTLFLAG_RW, &unprivileged_read_msgbuf, 0,
8566f3933faSRobert Watson     "Unprivileged processes may read the kernel message buffer");
8576f3933faSRobert Watson 
858948d3d94SThomas Moestl /* Sysctls for accessing/clearing the msgbuf */
859948d3d94SThomas Moestl static int
860948d3d94SThomas Moestl sysctl_kern_msgbuf(SYSCTL_HANDLER_ARGS)
861948d3d94SThomas Moestl {
8624784a469SIan Dowse 	char buf[128];
8634784a469SIan Dowse 	u_int seq;
8644784a469SIan Dowse 	int error, len;
865948d3d94SThomas Moestl 
8666f3933faSRobert Watson 	if (!unprivileged_read_msgbuf) {
86744731cabSJohn Baldwin 		error = suser(req->td);
8686f3933faSRobert Watson 		if (error)
8696f3933faSRobert Watson 			return (error);
8706f3933faSRobert Watson 	}
8716f3933faSRobert Watson 
8724784a469SIan Dowse 	/* Read the whole buffer, one chunk at a time. */
8734784a469SIan Dowse 	msgbuf_peekbytes(msgbufp, NULL, 0, &seq);
8744784a469SIan Dowse 	while ((len = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq)) > 0) {
8754784a469SIan Dowse 		error = sysctl_handle_opaque(oidp, buf, len, req);
876948d3d94SThomas Moestl 		if (error)
877948d3d94SThomas Moestl 			return (error);
878948d3d94SThomas Moestl 	}
8794784a469SIan Dowse 	return (0);
880948d3d94SThomas Moestl }
881948d3d94SThomas Moestl 
882948d3d94SThomas Moestl SYSCTL_PROC(_kern, OID_AUTO, msgbuf, CTLTYPE_STRING | CTLFLAG_RD,
883948d3d94SThomas Moestl     0, 0, sysctl_kern_msgbuf, "A", "Contents of kernel message buffer");
884948d3d94SThomas Moestl 
8854784a469SIan Dowse static int msgbuf_clearflag;
886948d3d94SThomas Moestl 
887948d3d94SThomas Moestl static int
888948d3d94SThomas Moestl sysctl_kern_msgbuf_clear(SYSCTL_HANDLER_ARGS)
889948d3d94SThomas Moestl {
890948d3d94SThomas Moestl 	int error;
891948d3d94SThomas Moestl 	error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
892948d3d94SThomas Moestl 	if (!error && req->newptr) {
8934784a469SIan Dowse 		msgbuf_clear(msgbufp);
8944784a469SIan Dowse 		msgbuf_clearflag = 0;
895948d3d94SThomas Moestl 	}
896948d3d94SThomas Moestl 	return (error);
897948d3d94SThomas Moestl }
898948d3d94SThomas Moestl 
899948d3d94SThomas Moestl SYSCTL_PROC(_kern, OID_AUTO, msgbuf_clear,
9004784a469SIan Dowse     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE, &msgbuf_clearflag, 0,
901948d3d94SThomas Moestl     sysctl_kern_msgbuf_clear, "I", "Clear kernel message buffer");
902948d3d94SThomas Moestl 
903e796e00dSPoul-Henning Kamp #ifdef DDB
904e796e00dSPoul-Henning Kamp 
905e796e00dSPoul-Henning Kamp DB_SHOW_COMMAND(msgbuf, db_show_msgbuf)
906e796e00dSPoul-Henning Kamp {
907e796e00dSPoul-Henning Kamp 	int i, j;
908e796e00dSPoul-Henning Kamp 
909e796e00dSPoul-Henning Kamp 	if (!msgbufmapped) {
910e796e00dSPoul-Henning Kamp 		db_printf("msgbuf not mapped yet\n");
911e796e00dSPoul-Henning Kamp 		return;
912e796e00dSPoul-Henning Kamp 	}
913e796e00dSPoul-Henning Kamp 	db_printf("msgbufp = %p\n", msgbufp);
9144784a469SIan Dowse 	db_printf("magic = %x, size = %d, r= %u, w = %u, ptr = %p, cksum= %u\n",
9154784a469SIan Dowse 	    msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_rseq,
9164784a469SIan Dowse 	    msgbufp->msg_wseq, msgbufp->msg_ptr, msgbufp->msg_cksum);
917e796e00dSPoul-Henning Kamp 	for (i = 0; i < msgbufp->msg_size; i++) {
9184784a469SIan Dowse 		j = MSGBUF_SEQ_TO_POS(msgbufp, i + msgbufp->msg_rseq);
919e796e00dSPoul-Henning Kamp 		db_printf("%c", msgbufp->msg_ptr[j]);
920e796e00dSPoul-Henning Kamp 	}
921e796e00dSPoul-Henning Kamp 	db_printf("\n");
922e796e00dSPoul-Henning Kamp }
923e796e00dSPoul-Henning Kamp 
924e796e00dSPoul-Henning Kamp #endif /* DDB */
92577411499SScott Long 
92677411499SScott Long void
92777411499SScott Long hexdump(void *ptr, int length, const char *hdr, int flags)
92877411499SScott Long {
92977411499SScott Long 	int i, j, k;
93077411499SScott Long 	int cols;
93177411499SScott Long 	unsigned char *cp;
93277411499SScott Long 	char delim;
93377411499SScott Long 
93477411499SScott Long 	if ((flags & HD_DELIM_MASK) != 0)
93577411499SScott Long 		delim = (flags & HD_DELIM_MASK) >> 8;
93677411499SScott Long 	else
93777411499SScott Long 		delim = ' ';
93877411499SScott Long 
93977411499SScott Long 	if ((flags & HD_COLUMN_MASK) != 0)
94077411499SScott Long 		cols = flags & HD_COLUMN_MASK;
94177411499SScott Long 	else
94277411499SScott Long 		cols = 16;
94377411499SScott Long 
94477411499SScott Long 	cp = ptr;
94577411499SScott Long 	for (i = 0; i < length; i+= cols) {
94677411499SScott Long 		if (hdr != NULL)
94777411499SScott Long 			printf("%s", hdr);
94877411499SScott Long 
94977411499SScott Long 		if ((flags & HD_OMIT_COUNT) == 0)
95077411499SScott Long 			printf("%04x  ", i);
95177411499SScott Long 
95277411499SScott Long 		if ((flags & HD_OMIT_HEX) == 0) {
95377411499SScott Long 			for (j = 0; j < cols; j++) {
95477411499SScott Long 				k = i + j;
95577411499SScott Long 				if (k < length)
95677411499SScott Long 					printf("%c%02x", delim, cp[k]);
95777411499SScott Long 				else
95877411499SScott Long 					printf("   ");
95977411499SScott Long 			}
96077411499SScott Long 		}
96177411499SScott Long 
96277411499SScott Long 		if ((flags & HD_OMIT_CHARS) == 0) {
96377411499SScott Long 			printf("  |");
96477411499SScott Long 			for (j = 0; j < cols; j++) {
96577411499SScott Long 				k = i + j;
96677411499SScott Long 				if (k >= length)
96777411499SScott Long 					printf(" ");
96877411499SScott Long 				else if (cp[k] >= ' ' && cp[k] <= '~')
96977411499SScott Long 					printf("%c", cp[k]);
97077411499SScott Long 				else
97177411499SScott Long 					printf(".");
97277411499SScott Long 			}
97377411499SScott Long 			printf("|\n");
97477411499SScott Long 		}
97577411499SScott Long 	}
97677411499SScott Long }
97777411499SScott Long 
978