xref: /freebsd/sys/kern/subr_prf.c (revision 4e31a37b9419cc79a7bcf95358cb192fab58aee7)
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  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
394e31a37bSGary Palmer  * $Id: subr_prf.c,v 1.36 1996/05/08 04:28:51 gpalmer Exp $
40df8bae1dSRodney W. Grimes  */
41df8bae1dSRodney W. Grimes 
420e41ee30SGarrett Wollman #include "opt_ddb.h"
430e41ee30SGarrett Wollman 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46df8bae1dSRodney W. Grimes #include <sys/reboot.h>
47df8bae1dSRodney W. Grimes #include <sys/msgbuf.h>
48df8bae1dSRodney W. Grimes #include <sys/proc.h>
49df8bae1dSRodney W. Grimes #include <sys/vnode.h>
50df8bae1dSRodney W. Grimes #include <sys/tty.h>
51df8bae1dSRodney W. Grimes #include <sys/tprintf.h>
52df8bae1dSRodney W. Grimes #include <sys/syslog.h>
53df8bae1dSRodney W. Grimes #include <sys/malloc.h>
54924dfd98SPoul-Henning Kamp #include <sys/kernel.h>
55924dfd98SPoul-Henning Kamp #include <sys/sysctl.h>
564fb0b0deSJoerg Wunsch #include <machine/cons.h>
57df8bae1dSRodney W. Grimes 
58df8bae1dSRodney W. Grimes /*
59df8bae1dSRodney W. Grimes  * Note that stdarg.h and the ANSI style va_start macro is used for both
60df8bae1dSRodney W. Grimes  * ANSI and traditional C compilers.
61df8bae1dSRodney W. Grimes  */
62df8bae1dSRodney W. Grimes #include <machine/stdarg.h>
63df8bae1dSRodney W. Grimes 
64a8c5fef5SPoul-Henning Kamp #if defined(DDB)
65924dfd98SPoul-Henning Kamp #ifdef DDB_UNATTENDED
66924dfd98SPoul-Henning Kamp 	static int debugger_on_panic = 0;
67924dfd98SPoul-Henning Kamp #else
68924dfd98SPoul-Henning Kamp 	static int debugger_on_panic = 1;
69df8bae1dSRodney W. Grimes #endif
70df8bae1dSRodney W. Grimes 
71924dfd98SPoul-Henning Kamp SYSCTL_INT(_debug, OID_AUTO, debugger_on_panic, CTLFLAG_RW,
72924dfd98SPoul-Henning Kamp 	&debugger_on_panic, 0, "");
73924dfd98SPoul-Henning Kamp #endif
744fb0b0deSJoerg Wunsch 
75df8bae1dSRodney W. Grimes #define TOCONS	0x01
76df8bae1dSRodney W. Grimes #define TOTTY	0x02
77df8bae1dSRodney W. Grimes #define TOLOG	0x04
78df8bae1dSRodney W. Grimes 
79df8bae1dSRodney W. Grimes struct	tty *constty;			/* pointer to console "window" tty */
80df8bae1dSRodney W. Grimes 
8187b6de2bSPoul-Henning Kamp static void (*v_putc)(int) = cnputc;	/* routine to putc on virtual console */
82df8bae1dSRodney W. Grimes 
8387b6de2bSPoul-Henning Kamp static void  logpri __P((int level));
84791d77e0SPoul-Henning Kamp static void  msglogchar(int c, void *dummyarg);
85791d77e0SPoul-Henning Kamp struct putchar_arg {int flags; struct tty *tty; };
86791d77e0SPoul-Henning Kamp static void  putchar __P((int ch, void *arg));
87df8bae1dSRodney W. Grimes static char *ksprintn __P((u_long num, int base, int *len));
88df8bae1dSRodney W. Grimes 
8987b6de2bSPoul-Henning Kamp static int consintr = 1;		/* Ok to handle console interrupts? */
90df8bae1dSRodney W. Grimes 
91df8bae1dSRodney W. Grimes /*
92df8bae1dSRodney W. Grimes  * Variable panicstr contains argument to first call to panic; used as flag
93df8bae1dSRodney W. Grimes  * to indicate that the kernel has already called panic.
94df8bae1dSRodney W. Grimes  */
95df8bae1dSRodney W. Grimes const char *panicstr;
96df8bae1dSRodney W. Grimes 
97df8bae1dSRodney W. Grimes /*
98df8bae1dSRodney W. Grimes  * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
99df8bae1dSRodney W. Grimes  * and then reboots.  If we are called twice, then we avoid trying to sync
100df8bae1dSRodney W. Grimes  * the disks as this often leads to recursive panics.
101df8bae1dSRodney W. Grimes  */
102df8bae1dSRodney W. Grimes #ifdef __GNUC__
1030e427608SDavid Greenman __dead			/* panic() does not return */
104df8bae1dSRodney W. Grimes #endif
105df8bae1dSRodney W. Grimes void
106df8bae1dSRodney W. Grimes panic(const char *fmt, ...)
107df8bae1dSRodney W. Grimes {
108df8bae1dSRodney W. Grimes 	int bootopt;
109df8bae1dSRodney W. Grimes 	va_list ap;
110df8bae1dSRodney W. Grimes 
111df8bae1dSRodney W. Grimes 	bootopt = RB_AUTOBOOT | RB_DUMP;
112df8bae1dSRodney W. Grimes 	if (panicstr)
113df8bae1dSRodney W. Grimes 		bootopt |= RB_NOSYNC;
114df8bae1dSRodney W. Grimes 	else
115df8bae1dSRodney W. Grimes 		panicstr = fmt;
116df8bae1dSRodney W. Grimes 
117120f0783SPoul-Henning Kamp 	printf("panic: ");
118bf124e25SBruce Evans 	va_start(ap, fmt);
119120f0783SPoul-Henning Kamp 	vprintf(fmt, ap);
120df8bae1dSRodney W. Grimes 	va_end(ap);
121bf124e25SBruce Evans 	printf("\n");
122df8bae1dSRodney W. Grimes 
123a8c5fef5SPoul-Henning Kamp #if defined(DDB)
124a8c5fef5SPoul-Henning Kamp 	if (debugger_on_panic)
12526f9a767SRodney W. Grimes 		Debugger ("panic");
12626f9a767SRodney W. Grimes #endif
1272438fbbaSJordan K. Hubbard 	boot(bootopt);
128df8bae1dSRodney W. Grimes }
129df8bae1dSRodney W. Grimes 
130df8bae1dSRodney W. Grimes /*
131df8bae1dSRodney W. Grimes  * Warn that a system table is full.
132df8bae1dSRodney W. Grimes  */
133df8bae1dSRodney W. Grimes void
134df8bae1dSRodney W. Grimes tablefull(tab)
135df8bae1dSRodney W. Grimes 	const char *tab;
136df8bae1dSRodney W. Grimes {
137df8bae1dSRodney W. Grimes 
138df8bae1dSRodney W. Grimes 	log(LOG_ERR, "%s: table is full\n", tab);
139df8bae1dSRodney W. Grimes }
140df8bae1dSRodney W. Grimes 
141df8bae1dSRodney W. Grimes /*
142df8bae1dSRodney W. Grimes  * Uprintf prints to the controlling terminal for the current process.
143df8bae1dSRodney W. Grimes  * It may block if the tty queue is overfull.  No message is printed if
144df8bae1dSRodney W. Grimes  * the queue does not clear in a reasonable time.
145df8bae1dSRodney W. Grimes  */
146df8bae1dSRodney W. Grimes void
147df8bae1dSRodney W. Grimes uprintf(const char *fmt, ...)
148df8bae1dSRodney W. Grimes {
149791d77e0SPoul-Henning Kamp 	struct proc *p = curproc;
150df8bae1dSRodney W. Grimes 	va_list ap;
151791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
152df8bae1dSRodney W. Grimes 
153df8bae1dSRodney W. Grimes 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
154df8bae1dSRodney W. Grimes 		va_start(ap, fmt);
155791d77e0SPoul-Henning Kamp 		pca.tty = p->p_session->s_ttyp;
156791d77e0SPoul-Henning Kamp 		pca.flags = TOTTY;
157791d77e0SPoul-Henning Kamp 		kvprintf(fmt, putchar, &pca, 10, ap);
158df8bae1dSRodney W. Grimes 		va_end(ap);
159df8bae1dSRodney W. Grimes 	}
160df8bae1dSRodney W. Grimes }
161df8bae1dSRodney W. Grimes 
162df8bae1dSRodney W. Grimes tpr_t
163df8bae1dSRodney W. Grimes tprintf_open(p)
164df8bae1dSRodney W. Grimes 	register struct proc *p;
165df8bae1dSRodney W. Grimes {
166df8bae1dSRodney W. Grimes 
167df8bae1dSRodney W. Grimes 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
168df8bae1dSRodney W. Grimes 		SESSHOLD(p->p_session);
169df8bae1dSRodney W. Grimes 		return ((tpr_t) p->p_session);
170df8bae1dSRodney W. Grimes 	}
171df8bae1dSRodney W. Grimes 	return ((tpr_t) NULL);
172df8bae1dSRodney W. Grimes }
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes void
175df8bae1dSRodney W. Grimes tprintf_close(sess)
176df8bae1dSRodney W. Grimes 	tpr_t sess;
177df8bae1dSRodney W. Grimes {
178df8bae1dSRodney W. Grimes 
179df8bae1dSRodney W. Grimes 	if (sess)
180df8bae1dSRodney W. Grimes 		SESSRELE((struct session *) sess);
181df8bae1dSRodney W. Grimes }
182df8bae1dSRodney W. Grimes 
183df8bae1dSRodney W. Grimes /*
184df8bae1dSRodney W. Grimes  * tprintf prints on the controlling terminal associated
185df8bae1dSRodney W. Grimes  * with the given session.
186df8bae1dSRodney W. Grimes  */
187df8bae1dSRodney W. Grimes void
188df8bae1dSRodney W. Grimes tprintf(tpr_t tpr, const char *fmt, ...)
189df8bae1dSRodney W. Grimes {
190df8bae1dSRodney W. Grimes 	register struct session *sess = (struct session *)tpr;
191df8bae1dSRodney W. Grimes 	struct tty *tp = NULL;
192df8bae1dSRodney W. Grimes 	int flags = TOLOG;
193df8bae1dSRodney W. Grimes 	va_list ap;
194791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
195df8bae1dSRodney W. Grimes 
196df8bae1dSRodney W. Grimes 	logpri(LOG_INFO);
197df8bae1dSRodney W. Grimes 	if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
198df8bae1dSRodney W. Grimes 		flags |= TOTTY;
199df8bae1dSRodney W. Grimes 		tp = sess->s_ttyp;
200df8bae1dSRodney W. Grimes 	}
201df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
202791d77e0SPoul-Henning Kamp 	pca.tty = tp;
203791d77e0SPoul-Henning Kamp 	pca.flags = flags;
204791d77e0SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
205df8bae1dSRodney W. Grimes 	va_end(ap);
206df8bae1dSRodney W. Grimes 	logwakeup();
207df8bae1dSRodney W. Grimes }
208df8bae1dSRodney W. Grimes 
209df8bae1dSRodney W. Grimes /*
210df8bae1dSRodney W. Grimes  * Ttyprintf displays a message on a tty; it should be used only by
211df8bae1dSRodney W. Grimes  * the tty driver, or anything that knows the underlying tty will not
212df8bae1dSRodney W. Grimes  * be revoke(2)'d away.  Other callers should use tprintf.
213df8bae1dSRodney W. Grimes  */
214df8bae1dSRodney W. Grimes void
215df8bae1dSRodney W. Grimes ttyprintf(struct tty *tp, const char *fmt, ...)
216df8bae1dSRodney W. Grimes {
217df8bae1dSRodney W. Grimes 	va_list ap;
218791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
219df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
220791d77e0SPoul-Henning Kamp 	pca.tty = tp;
221791d77e0SPoul-Henning Kamp 	pca.flags = TOTTY;
222791d77e0SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
223df8bae1dSRodney W. Grimes 	va_end(ap);
224df8bae1dSRodney W. Grimes }
225df8bae1dSRodney W. Grimes 
226df8bae1dSRodney W. Grimes extern	int log_open;
227df8bae1dSRodney W. Grimes 
228df8bae1dSRodney W. Grimes /*
229df8bae1dSRodney W. Grimes  * Log writes to the log buffer, and guarantees not to sleep (so can be
230df8bae1dSRodney W. Grimes  * called by interrupt routines).  If there is no process reading the
231df8bae1dSRodney W. Grimes  * log yet, it writes to the console also.
232df8bae1dSRodney W. Grimes  */
233df8bae1dSRodney W. Grimes void
234df8bae1dSRodney W. Grimes log(int level, const char *fmt, ...)
235df8bae1dSRodney W. Grimes {
236df8bae1dSRodney W. Grimes 	register int s;
237df8bae1dSRodney W. Grimes 	va_list ap;
238df8bae1dSRodney W. Grimes 
239df8bae1dSRodney W. Grimes 	s = splhigh();
240df8bae1dSRodney W. Grimes 	logpri(level);
241df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
242791d77e0SPoul-Henning Kamp 
243791d77e0SPoul-Henning Kamp 	kvprintf(fmt, msglogchar, NULL, 10, ap);
244df8bae1dSRodney W. Grimes 	va_end(ap);
245791d77e0SPoul-Henning Kamp 
246791d77e0SPoul-Henning Kamp 	splx(s);
247df8bae1dSRodney W. Grimes 	if (!log_open) {
248791d77e0SPoul-Henning Kamp 		struct putchar_arg pca;
249df8bae1dSRodney W. Grimes 		va_start(ap, fmt);
250791d77e0SPoul-Henning Kamp 		pca.tty = NULL;
251791d77e0SPoul-Henning Kamp 		pca.flags = TOCONS;
252791d77e0SPoul-Henning Kamp 		kvprintf(fmt, putchar, &pca, 10, ap);
253df8bae1dSRodney W. Grimes 		va_end(ap);
254df8bae1dSRodney W. Grimes 	}
255df8bae1dSRodney W. Grimes 	logwakeup();
256df8bae1dSRodney W. Grimes }
257df8bae1dSRodney W. Grimes 
25887b6de2bSPoul-Henning Kamp static void
259df8bae1dSRodney W. Grimes logpri(level)
260df8bae1dSRodney W. Grimes 	int level;
261df8bae1dSRodney W. Grimes {
262df8bae1dSRodney W. Grimes 	register char *p;
263df8bae1dSRodney W. Grimes 
264791d77e0SPoul-Henning Kamp 	msglogchar('<', NULL);
265797f2d22SPoul-Henning Kamp 	for (p = ksprintn((u_long)level, 10, NULL); *p;)
266791d77e0SPoul-Henning Kamp 		msglogchar(*p--, NULL);
267791d77e0SPoul-Henning Kamp 	msglogchar('>', NULL);
268df8bae1dSRodney W. Grimes }
269df8bae1dSRodney W. Grimes 
2706ddbf1e2SGary Palmer int
271df8bae1dSRodney W. Grimes addlog(const char *fmt, ...)
272df8bae1dSRodney W. Grimes {
273df8bae1dSRodney W. Grimes 	register int s;
274df8bae1dSRodney W. Grimes 	va_list ap;
2756ddbf1e2SGary Palmer 	int retval;
276df8bae1dSRodney W. Grimes 
277df8bae1dSRodney W. Grimes 	s = splhigh();
278df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
2796ddbf1e2SGary Palmer 	retval = kvprintf(fmt, msglogchar, NULL, 10, ap);
280df8bae1dSRodney W. Grimes 	splx(s);
281df8bae1dSRodney W. Grimes 	va_end(ap);
282df8bae1dSRodney W. Grimes 	if (!log_open) {
283791d77e0SPoul-Henning Kamp 		struct putchar_arg pca;
284df8bae1dSRodney W. Grimes 		va_start(ap, fmt);
285791d77e0SPoul-Henning Kamp 		pca.tty = NULL;
286791d77e0SPoul-Henning Kamp 		pca.flags = TOCONS;
287791d77e0SPoul-Henning Kamp 		kvprintf(fmt, putchar, &pca, 10, ap);
288df8bae1dSRodney W. Grimes 		va_end(ap);
289df8bae1dSRodney W. Grimes 	}
290df8bae1dSRodney W. Grimes 	logwakeup();
2916ddbf1e2SGary Palmer 	return (retval);
292df8bae1dSRodney W. Grimes }
293df8bae1dSRodney W. Grimes 
29465ed8cbdSJustin T. Gibbs int
295df8bae1dSRodney W. Grimes printf(const char *fmt, ...)
296df8bae1dSRodney W. Grimes {
297df8bae1dSRodney W. Grimes 	va_list ap;
298df8bae1dSRodney W. Grimes 	register int savintr;
299791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
30065ed8cbdSJustin T. Gibbs 	int retval;
301df8bae1dSRodney W. Grimes 
302df8bae1dSRodney W. Grimes 	savintr = consintr;		/* disable interrupts */
303df8bae1dSRodney W. Grimes 	consintr = 0;
304df8bae1dSRodney W. Grimes 	va_start(ap, fmt);
305791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
306791d77e0SPoul-Henning Kamp 	pca.flags = TOCONS | TOLOG;
30765ed8cbdSJustin T. Gibbs 	retval = kvprintf(fmt, putchar, &pca, 10, ap);
308df8bae1dSRodney W. Grimes 	va_end(ap);
309df8bae1dSRodney W. Grimes 	if (!panicstr)
310df8bae1dSRodney W. Grimes 		logwakeup();
311df8bae1dSRodney W. Grimes 	consintr = savintr;		/* reenable interrupts */
31265ed8cbdSJustin T. Gibbs 	return retval;
313df8bae1dSRodney W. Grimes }
314df8bae1dSRodney W. Grimes 
315791d77e0SPoul-Henning Kamp void
316791d77e0SPoul-Henning Kamp vprintf(const char *fmt, va_list ap)
317791d77e0SPoul-Henning Kamp {
318791d77e0SPoul-Henning Kamp 	register int savintr;
319791d77e0SPoul-Henning Kamp 	struct putchar_arg pca;
320791d77e0SPoul-Henning Kamp 
321791d77e0SPoul-Henning Kamp 	savintr = consintr;		/* disable interrupts */
322791d77e0SPoul-Henning Kamp 	consintr = 0;
323791d77e0SPoul-Henning Kamp 	pca.tty = NULL;
324791d77e0SPoul-Henning Kamp 	pca.flags = TOCONS | TOLOG;
325791d77e0SPoul-Henning Kamp 	kvprintf(fmt, putchar, &pca, 10, ap);
326791d77e0SPoul-Henning Kamp 	if (!panicstr)
327791d77e0SPoul-Henning Kamp 		logwakeup();
328791d77e0SPoul-Henning Kamp 	consintr = savintr;		/* reenable interrupts */
329791d77e0SPoul-Henning Kamp }
330791d77e0SPoul-Henning Kamp 
331791d77e0SPoul-Henning Kamp /*
332791d77e0SPoul-Henning Kamp  * Print a character on console or users terminal.  If destination is
333791d77e0SPoul-Henning Kamp  * the console then the last MSGBUFS characters are saved in msgbuf for
334791d77e0SPoul-Henning Kamp  * inspection later.
335791d77e0SPoul-Henning Kamp  */
336791d77e0SPoul-Henning Kamp static void
337791d77e0SPoul-Henning Kamp putchar(int c, void *arg)
338791d77e0SPoul-Henning Kamp {
339791d77e0SPoul-Henning Kamp 	struct putchar_arg *ap = (struct putchar_arg*) arg;
340791d77e0SPoul-Henning Kamp 	int flags = ap->flags;
341791d77e0SPoul-Henning Kamp 	struct tty *tp = ap->tty;
342791d77e0SPoul-Henning Kamp 	if (panicstr)
343791d77e0SPoul-Henning Kamp 		constty = NULL;
344791d77e0SPoul-Henning Kamp 	if ((flags & TOCONS) && tp == NULL && constty) {
345791d77e0SPoul-Henning Kamp 		tp = constty;
346791d77e0SPoul-Henning Kamp 		flags |= TOTTY;
347791d77e0SPoul-Henning Kamp 	}
348791d77e0SPoul-Henning Kamp 	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
349791d77e0SPoul-Henning Kamp 	    (flags & TOCONS) && tp == constty)
350791d77e0SPoul-Henning Kamp 		constty = NULL;
351791d77e0SPoul-Henning Kamp 	if ((flags & TOLOG))
352791d77e0SPoul-Henning Kamp 		msglogchar(c, NULL);
353791d77e0SPoul-Henning Kamp 	if ((flags & TOCONS) && constty == NULL && c != '\0')
354791d77e0SPoul-Henning Kamp 		(*v_putc)(c);
355791d77e0SPoul-Henning Kamp }
356791d77e0SPoul-Henning Kamp 
357791d77e0SPoul-Henning Kamp /*
358791d77e0SPoul-Henning Kamp  * Scaled down version of sprintf(3).
359791d77e0SPoul-Henning Kamp  */
360791d77e0SPoul-Henning Kamp int
361791d77e0SPoul-Henning Kamp sprintf(char *buf, const char *cfmt, ...)
362791d77e0SPoul-Henning Kamp {
363791d77e0SPoul-Henning Kamp 	int retval;
364791d77e0SPoul-Henning Kamp 	va_list ap;
365791d77e0SPoul-Henning Kamp 
366791d77e0SPoul-Henning Kamp 	va_start(ap, cfmt);
367791d77e0SPoul-Henning Kamp 	retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap);
368fe96d47dSPoul-Henning Kamp 	buf[retval] = '\0';
369791d77e0SPoul-Henning Kamp 	va_end(ap);
370791d77e0SPoul-Henning Kamp 	return retval;
371791d77e0SPoul-Henning Kamp }
372791d77e0SPoul-Henning Kamp 
373791d77e0SPoul-Henning Kamp /*
374791d77e0SPoul-Henning Kamp  * Put a number (base <= 16) in a buffer in reverse order; return an
375791d77e0SPoul-Henning Kamp  * optional length and a pointer to the NULL terminated (preceded?)
376791d77e0SPoul-Henning Kamp  * buffer.
377791d77e0SPoul-Henning Kamp  */
378791d77e0SPoul-Henning Kamp static char *
379791d77e0SPoul-Henning Kamp ksprintn(ul, base, lenp)
380791d77e0SPoul-Henning Kamp 	register u_long ul;
381791d77e0SPoul-Henning Kamp 	register int base, *lenp;
382791d77e0SPoul-Henning Kamp {					/* A long in base 8, plus NULL. */
383791d77e0SPoul-Henning Kamp 	static char buf[sizeof(long) * NBBY / 3 + 2];
384791d77e0SPoul-Henning Kamp 	register char *p;
385791d77e0SPoul-Henning Kamp 
386791d77e0SPoul-Henning Kamp 	p = buf;
387791d77e0SPoul-Henning Kamp 	do {
388791d77e0SPoul-Henning Kamp 		*++p = hex2ascii(ul % base);
389791d77e0SPoul-Henning Kamp 	} while (ul /= base);
390791d77e0SPoul-Henning Kamp 	if (lenp)
391791d77e0SPoul-Henning Kamp 		*lenp = p - buf;
392791d77e0SPoul-Henning Kamp 	return (p);
393791d77e0SPoul-Henning Kamp }
394791d77e0SPoul-Henning Kamp 
395df8bae1dSRodney W. Grimes /*
396df8bae1dSRodney W. Grimes  * Scaled down version of printf(3).
397df8bae1dSRodney W. Grimes  *
398df8bae1dSRodney W. Grimes  * Two additional formats:
399df8bae1dSRodney W. Grimes  *
400df8bae1dSRodney W. Grimes  * The format %b is supported to decode error registers.
401df8bae1dSRodney W. Grimes  * Its usage is:
402df8bae1dSRodney W. Grimes  *
403df8bae1dSRodney W. Grimes  *	printf("reg=%b\n", regval, "<base><arg>*");
404df8bae1dSRodney W. Grimes  *
405df8bae1dSRodney W. Grimes  * where <base> is the output base expressed as a control character, e.g.
406df8bae1dSRodney W. Grimes  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
407df8bae1dSRodney W. Grimes  * the first of which gives the bit number to be inspected (origin 1), and
408df8bae1dSRodney W. Grimes  * the next characters (up to a control character, i.e. a character <= 32),
409df8bae1dSRodney W. Grimes  * give the name of the register.  Thus:
410df8bae1dSRodney W. Grimes  *
4114e31a37bSGary Palmer  *	kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
412df8bae1dSRodney W. Grimes  *
413df8bae1dSRodney W. Grimes  * would produce output:
414df8bae1dSRodney W. Grimes  *
415df8bae1dSRodney W. Grimes  *	reg=3<BITTWO,BITONE>
416df8bae1dSRodney W. Grimes  *
417120f0783SPoul-Henning Kamp  * XXX:  %D  -- Hexdump, takes pointer and separator string:
418120f0783SPoul-Henning Kamp  *		("%6D", ptr, ":")   -> XX:XX:XX:XX:XX:XX
419120f0783SPoul-Henning Kamp  *		("%*D", len, ptr, " " -> XX XX XX XX ...
420df8bae1dSRodney W. Grimes  */
421791d77e0SPoul-Henning Kamp int
422791d77e0SPoul-Henning Kamp kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
423df8bae1dSRodney W. Grimes {
424791d77e0SPoul-Henning Kamp #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; }
425791d77e0SPoul-Henning Kamp 	char *p, *q, *d;
426120f0783SPoul-Henning Kamp 	u_char *up;
427791d77e0SPoul-Henning Kamp 	int ch, n;
428df8bae1dSRodney W. Grimes 	u_long ul;
4294830092aSPoul-Henning Kamp 	int base, lflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
4304f20e4b1SPoul-Henning Kamp 	int dwidth;
431df8bae1dSRodney W. Grimes 	char padc;
432791d77e0SPoul-Henning Kamp 	int retval = 0;
433791d77e0SPoul-Henning Kamp 
434fe96d47dSPoul-Henning Kamp 	if (!func)
435791d77e0SPoul-Henning Kamp 		d = (char *) arg;
436791d77e0SPoul-Henning Kamp 	else
437fe96d47dSPoul-Henning Kamp 		d = NULL;
4388f5067baSDavid Greenman 
4398f5067baSDavid Greenman 	if (fmt == NULL)
4402336b9d7SBruce Evans 		fmt = "(fmt null)\n";
441b4b2f81eSPoul-Henning Kamp 
442120f0783SPoul-Henning Kamp 	if (radix < 2 || radix > 36)
443b4b2f81eSPoul-Henning Kamp 		radix = 10;
444b4b2f81eSPoul-Henning Kamp 
445df8bae1dSRodney W. Grimes 	for (;;) {
446df8bae1dSRodney W. Grimes 		padc = ' ';
447df8bae1dSRodney W. Grimes 		width = 0;
448df8bae1dSRodney W. Grimes 		while ((ch = *(u_char *)fmt++) != '%') {
449df8bae1dSRodney W. Grimes 			if (ch == '\0')
450791d77e0SPoul-Henning Kamp 				return retval;
451791d77e0SPoul-Henning Kamp 			PCHAR(ch);
452df8bae1dSRodney W. Grimes 		}
4534f20e4b1SPoul-Henning Kamp 		lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
4544f20e4b1SPoul-Henning Kamp 		sign = 0; dot = 0; dwidth = 0;
455df8bae1dSRodney W. Grimes reswitch:	switch (ch = *(u_char *)fmt++) {
4564830092aSPoul-Henning Kamp 		case '.':
4574830092aSPoul-Henning Kamp 			dot = 1;
4584830092aSPoul-Henning Kamp 			goto reswitch;
459791d77e0SPoul-Henning Kamp 		case '#':
460791d77e0SPoul-Henning Kamp 			sharpflag = 1;
461791d77e0SPoul-Henning Kamp 			goto reswitch;
462791d77e0SPoul-Henning Kamp 		case '+':
463791d77e0SPoul-Henning Kamp 			sign = 1;
464791d77e0SPoul-Henning Kamp 			goto reswitch;
465791d77e0SPoul-Henning Kamp 		case '-':
466791d77e0SPoul-Henning Kamp 			ladjust = 1;
467791d77e0SPoul-Henning Kamp 			goto reswitch;
468791d77e0SPoul-Henning Kamp 		case '%':
469791d77e0SPoul-Henning Kamp 			PCHAR(ch);
470791d77e0SPoul-Henning Kamp 			break;
471791d77e0SPoul-Henning Kamp 		case '*':
472ed71c342SPoul-Henning Kamp 			if (!dot) {
473791d77e0SPoul-Henning Kamp 				width = va_arg(ap, int);
474791d77e0SPoul-Henning Kamp 				if (width < 0) {
475791d77e0SPoul-Henning Kamp 					ladjust = !ladjust;
476791d77e0SPoul-Henning Kamp 					width = -width;
477791d77e0SPoul-Henning Kamp 				}
478ed71c342SPoul-Henning Kamp 			} else {
479ed71c342SPoul-Henning Kamp 				dwidth = va_arg(ap, int);
480ed71c342SPoul-Henning Kamp 			}
481791d77e0SPoul-Henning Kamp 			goto reswitch;
482df8bae1dSRodney W. Grimes 		case '0':
4834f20e4b1SPoul-Henning Kamp 			if (!dot) {
484df8bae1dSRodney W. Grimes 				padc = '0';
485df8bae1dSRodney W. Grimes 				goto reswitch;
4864f20e4b1SPoul-Henning Kamp 			}
487df8bae1dSRodney W. Grimes 		case '1': case '2': case '3': case '4':
488df8bae1dSRodney W. Grimes 		case '5': case '6': case '7': case '8': case '9':
4894f20e4b1SPoul-Henning Kamp 				for (n = 0;; ++fmt) {
4904f20e4b1SPoul-Henning Kamp 					n = n * 10 + ch - '0';
491df8bae1dSRodney W. Grimes 					ch = *fmt;
492df8bae1dSRodney W. Grimes 					if (ch < '0' || ch > '9')
493df8bae1dSRodney W. Grimes 						break;
494df8bae1dSRodney W. Grimes 				}
4954f20e4b1SPoul-Henning Kamp 			if (dot)
4964f20e4b1SPoul-Henning Kamp 				dwidth = n;
4974f20e4b1SPoul-Henning Kamp 			else
4984f20e4b1SPoul-Henning Kamp 				width = n;
499df8bae1dSRodney W. Grimes 			goto reswitch;
500df8bae1dSRodney W. Grimes 		case 'b':
501df8bae1dSRodney W. Grimes 			ul = va_arg(ap, int);
502df8bae1dSRodney W. Grimes 			p = va_arg(ap, char *);
503797f2d22SPoul-Henning Kamp 			for (q = ksprintn(ul, *p++, NULL); *q;)
504791d77e0SPoul-Henning Kamp 				PCHAR(*q--);
505df8bae1dSRodney W. Grimes 
506df8bae1dSRodney W. Grimes 			if (!ul)
507df8bae1dSRodney W. Grimes 				break;
508df8bae1dSRodney W. Grimes 
509797f2d22SPoul-Henning Kamp 			for (tmp = 0; *p;) {
510797f2d22SPoul-Henning Kamp 				n = *p++;
511df8bae1dSRodney W. Grimes 				if (ul & (1 << (n - 1))) {
512791d77e0SPoul-Henning Kamp 					PCHAR(tmp ? ',' : '<');
513df8bae1dSRodney W. Grimes 					for (; (n = *p) > ' '; ++p)
514791d77e0SPoul-Henning Kamp 						PCHAR(n);
515df8bae1dSRodney W. Grimes 					tmp = 1;
516df8bae1dSRodney W. Grimes 				} else
517df8bae1dSRodney W. Grimes 					for (; *p > ' '; ++p)
518df8bae1dSRodney W. Grimes 						continue;
519df8bae1dSRodney W. Grimes 			}
520df8bae1dSRodney W. Grimes 			if (tmp)
521791d77e0SPoul-Henning Kamp 				PCHAR('>');
522df8bae1dSRodney W. Grimes 			break;
523df8bae1dSRodney W. Grimes 		case 'c':
524791d77e0SPoul-Henning Kamp 			PCHAR(va_arg(ap, int));
525df8bae1dSRodney W. Grimes 			break;
526120f0783SPoul-Henning Kamp 		case 'D':
527120f0783SPoul-Henning Kamp 			up = va_arg(ap, u_char *);
528120f0783SPoul-Henning Kamp 			p = va_arg(ap, char *);
529120f0783SPoul-Henning Kamp 			if (!width)
530120f0783SPoul-Henning Kamp 				width = 16;
531120f0783SPoul-Henning Kamp 			while(width--) {
532120f0783SPoul-Henning Kamp 				PCHAR(hex2ascii(*up >> 4));
533120f0783SPoul-Henning Kamp 				PCHAR(hex2ascii(*up & 0x0f));
534120f0783SPoul-Henning Kamp 				up++;
535120f0783SPoul-Henning Kamp 				if (width)
536120f0783SPoul-Henning Kamp 					for (q=p;*q;q++)
537120f0783SPoul-Henning Kamp 						PCHAR(*q);
538120f0783SPoul-Henning Kamp 			}
539120f0783SPoul-Henning Kamp 			break;
540df8bae1dSRodney W. Grimes 		case 'd':
541df8bae1dSRodney W. Grimes 			ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
542791d77e0SPoul-Henning Kamp 			sign = 1;
543df8bae1dSRodney W. Grimes 			base = 10;
544df8bae1dSRodney W. Grimes 			goto number;
545791d77e0SPoul-Henning Kamp 		case 'l':
546791d77e0SPoul-Henning Kamp 			lflag = 1;
547791d77e0SPoul-Henning Kamp 			goto reswitch;
548791d77e0SPoul-Henning Kamp 		case 'n':
549791d77e0SPoul-Henning Kamp 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
550791d77e0SPoul-Henning Kamp 			base = radix;
551791d77e0SPoul-Henning Kamp 			goto number;
552df8bae1dSRodney W. Grimes 		case 'o':
553df8bae1dSRodney W. Grimes 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
554df8bae1dSRodney W. Grimes 			base = 8;
555df8bae1dSRodney W. Grimes 			goto number;
55612d17f65SPoul-Henning Kamp 		case 'p':
55712d17f65SPoul-Henning Kamp 			ul = (u_long)va_arg(ap, void *);
55812d17f65SPoul-Henning Kamp 			base = 16;
559791d77e0SPoul-Henning Kamp 			PCHAR('0');
560791d77e0SPoul-Henning Kamp 			PCHAR('x');
56112d17f65SPoul-Henning Kamp 			goto number;
562791d77e0SPoul-Henning Kamp 		case 's':
563791d77e0SPoul-Henning Kamp 			p = va_arg(ap, char *);
564791d77e0SPoul-Henning Kamp 			if (p == NULL)
565791d77e0SPoul-Henning Kamp 				p = "(null)";
5664830092aSPoul-Henning Kamp 			if (!dot)
5674830092aSPoul-Henning Kamp 				n = strlen (p);
5684830092aSPoul-Henning Kamp 			else
5694f20e4b1SPoul-Henning Kamp 				for (n = 0; n < dwidth && p[n]; n++)
5704830092aSPoul-Henning Kamp 					continue;
5714f20e4b1SPoul-Henning Kamp 
5724830092aSPoul-Henning Kamp 			width -= n;
5734f20e4b1SPoul-Henning Kamp 
574791d77e0SPoul-Henning Kamp 			if (!ladjust && width > 0)
575791d77e0SPoul-Henning Kamp 				while (width--)
576791d77e0SPoul-Henning Kamp 					PCHAR(padc);
5774830092aSPoul-Henning Kamp 			while (n--)
578791d77e0SPoul-Henning Kamp 				PCHAR(*p++);
579791d77e0SPoul-Henning Kamp 			if (ladjust && width > 0)
580791d77e0SPoul-Henning Kamp 				while (width--)
581791d77e0SPoul-Henning Kamp 					PCHAR(padc);
582791d77e0SPoul-Henning Kamp 			break;
583f53dbe97SBruce Evans 		case 'u':
584f53dbe97SBruce Evans 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
585f53dbe97SBruce Evans 			base = 10;
586f53dbe97SBruce Evans 			goto number;
587df8bae1dSRodney W. Grimes 		case 'x':
588df8bae1dSRodney W. Grimes 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
589df8bae1dSRodney W. Grimes 			base = 16;
590791d77e0SPoul-Henning Kamp number:			if (sign && (long)ul < 0L) {
591791d77e0SPoul-Henning Kamp 				neg = 1;
592791d77e0SPoul-Henning Kamp 				ul = -(long)ul;
593791d77e0SPoul-Henning Kamp 			}
594791d77e0SPoul-Henning Kamp 			p = ksprintn(ul, base, &tmp);
595791d77e0SPoul-Henning Kamp 			if (sharpflag && ul != 0) {
596791d77e0SPoul-Henning Kamp 				if (base == 8)
597791d77e0SPoul-Henning Kamp 					tmp++;
598791d77e0SPoul-Henning Kamp 				else if (base == 16)
599791d77e0SPoul-Henning Kamp 					tmp += 2;
600791d77e0SPoul-Henning Kamp 			}
601791d77e0SPoul-Henning Kamp 			if (neg)
602791d77e0SPoul-Henning Kamp 				tmp++;
603791d77e0SPoul-Henning Kamp 
604791d77e0SPoul-Henning Kamp 			if (!ladjust && width && (width -= tmp) > 0)
605df8bae1dSRodney W. Grimes 				while (width--)
606791d77e0SPoul-Henning Kamp 					PCHAR(padc);
607791d77e0SPoul-Henning Kamp 			if (neg)
608791d77e0SPoul-Henning Kamp 				PCHAR('-');
609791d77e0SPoul-Henning Kamp 			if (sharpflag && ul != 0) {
610791d77e0SPoul-Henning Kamp 				if (base == 8) {
611791d77e0SPoul-Henning Kamp 					PCHAR('0');
612791d77e0SPoul-Henning Kamp 				} else if (base == 16) {
613791d77e0SPoul-Henning Kamp 					PCHAR('0');
614791d77e0SPoul-Henning Kamp 					PCHAR('x');
615791d77e0SPoul-Henning Kamp 				}
616791d77e0SPoul-Henning Kamp 			}
617791d77e0SPoul-Henning Kamp 
618797f2d22SPoul-Henning Kamp 			while (*p)
619791d77e0SPoul-Henning Kamp 				PCHAR(*p--);
620791d77e0SPoul-Henning Kamp 
621791d77e0SPoul-Henning Kamp 			if (ladjust && width && (width -= tmp) > 0)
622791d77e0SPoul-Henning Kamp 				while (width--)
623791d77e0SPoul-Henning Kamp 					PCHAR(padc);
624791d77e0SPoul-Henning Kamp 
625df8bae1dSRodney W. Grimes 			break;
626df8bae1dSRodney W. Grimes 		default:
627791d77e0SPoul-Henning Kamp 			PCHAR('%');
628df8bae1dSRodney W. Grimes 			if (lflag)
629791d77e0SPoul-Henning Kamp 				PCHAR('l');
630791d77e0SPoul-Henning Kamp 			PCHAR(ch);
631791d77e0SPoul-Henning Kamp 			break;
632df8bae1dSRodney W. Grimes 		}
633df8bae1dSRodney W. Grimes 	}
634791d77e0SPoul-Henning Kamp #undef PCHAR
635df8bae1dSRodney W. Grimes }
636df8bae1dSRodney W. Grimes 
637df8bae1dSRodney W. Grimes /*
638791d77e0SPoul-Henning Kamp  * Put character in log buffer.
639df8bae1dSRodney W. Grimes  */
640df8bae1dSRodney W. Grimes static void
641791d77e0SPoul-Henning Kamp msglogchar(int c, void *dummyarg)
642df8bae1dSRodney W. Grimes {
643791d77e0SPoul-Henning Kamp 	struct msgbuf *mbp;
644df8bae1dSRodney W. Grimes 
645791d77e0SPoul-Henning Kamp 	if (c != '\0' && c != '\r' && c != 0177 && msgbufmapped) {
646df8bae1dSRodney W. Grimes 		mbp = msgbufp;
6476c8897cfSDavid Greenman 		if (mbp->msg_magic != MSG_MAGIC ||
6486c8897cfSDavid Greenman 		    mbp->msg_bufx >= MSG_BSIZE ||
6496c8897cfSDavid Greenman 		    mbp->msg_bufr >= MSG_BSIZE) {
6506c8897cfSDavid Greenman 			bzero(mbp, sizeof(struct msgbuf));
651df8bae1dSRodney W. Grimes 			mbp->msg_magic = MSG_MAGIC;
652df8bae1dSRodney W. Grimes 		}
653df8bae1dSRodney W. Grimes 		mbp->msg_bufc[mbp->msg_bufx++] = c;
654b4224c9cSDavid Greenman 		if (mbp->msg_bufx >= MSG_BSIZE)
655df8bae1dSRodney W. Grimes 			mbp->msg_bufx = 0;
65602d5c7b1SDavid Greenman 		/* If the buffer is full, keep the most recent data. */
657a3f4faceSBruce Evans 		if (mbp->msg_bufr == mbp->msg_bufx) {
65802d5c7b1SDavid Greenman 			if (++mbp->msg_bufr >= MSG_BSIZE)
659a3f4faceSBruce Evans 				mbp->msg_bufr = 0;
660a3f4faceSBruce Evans 		}
661df8bae1dSRodney W. Grimes 	}
662df8bae1dSRodney W. Grimes }
663