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 39a8c5fef5SPoul-Henning Kamp * $Id: subr_prf.c,v 1.34 1996/03/25 17:06:34 jkh 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 270df8bae1dSRodney W. Grimes void 271df8bae1dSRodney W. Grimes addlog(const char *fmt, ...) 272df8bae1dSRodney W. Grimes { 273df8bae1dSRodney W. Grimes register int s; 274df8bae1dSRodney W. Grimes va_list ap; 275df8bae1dSRodney W. Grimes 276df8bae1dSRodney W. Grimes s = splhigh(); 277df8bae1dSRodney W. Grimes va_start(ap, fmt); 278791d77e0SPoul-Henning Kamp kvprintf(fmt, msglogchar, NULL, 10, ap); 279df8bae1dSRodney W. Grimes splx(s); 280df8bae1dSRodney W. Grimes va_end(ap); 281df8bae1dSRodney W. Grimes if (!log_open) { 282791d77e0SPoul-Henning Kamp struct putchar_arg pca; 283df8bae1dSRodney W. Grimes va_start(ap, fmt); 284791d77e0SPoul-Henning Kamp pca.tty = NULL; 285791d77e0SPoul-Henning Kamp pca.flags = TOCONS; 286791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 287df8bae1dSRodney W. Grimes va_end(ap); 288df8bae1dSRodney W. Grimes } 289df8bae1dSRodney W. Grimes logwakeup(); 290df8bae1dSRodney W. Grimes } 291df8bae1dSRodney W. Grimes 29265ed8cbdSJustin T. Gibbs int 293df8bae1dSRodney W. Grimes printf(const char *fmt, ...) 294df8bae1dSRodney W. Grimes { 295df8bae1dSRodney W. Grimes va_list ap; 296df8bae1dSRodney W. Grimes register int savintr; 297791d77e0SPoul-Henning Kamp struct putchar_arg pca; 29865ed8cbdSJustin T. Gibbs int retval; 299df8bae1dSRodney W. Grimes 300df8bae1dSRodney W. Grimes savintr = consintr; /* disable interrupts */ 301df8bae1dSRodney W. Grimes consintr = 0; 302df8bae1dSRodney W. Grimes va_start(ap, fmt); 303791d77e0SPoul-Henning Kamp pca.tty = NULL; 304791d77e0SPoul-Henning Kamp pca.flags = TOCONS | TOLOG; 30565ed8cbdSJustin T. Gibbs retval = kvprintf(fmt, putchar, &pca, 10, ap); 306df8bae1dSRodney W. Grimes va_end(ap); 307df8bae1dSRodney W. Grimes if (!panicstr) 308df8bae1dSRodney W. Grimes logwakeup(); 309df8bae1dSRodney W. Grimes consintr = savintr; /* reenable interrupts */ 31065ed8cbdSJustin T. Gibbs return retval; 311df8bae1dSRodney W. Grimes } 312df8bae1dSRodney W. Grimes 313791d77e0SPoul-Henning Kamp void 314791d77e0SPoul-Henning Kamp vprintf(const char *fmt, va_list ap) 315791d77e0SPoul-Henning Kamp { 316791d77e0SPoul-Henning Kamp register int savintr; 317791d77e0SPoul-Henning Kamp struct putchar_arg pca; 318791d77e0SPoul-Henning Kamp 319791d77e0SPoul-Henning Kamp savintr = consintr; /* disable interrupts */ 320791d77e0SPoul-Henning Kamp consintr = 0; 321791d77e0SPoul-Henning Kamp pca.tty = NULL; 322791d77e0SPoul-Henning Kamp pca.flags = TOCONS | TOLOG; 323791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 324791d77e0SPoul-Henning Kamp if (!panicstr) 325791d77e0SPoul-Henning Kamp logwakeup(); 326791d77e0SPoul-Henning Kamp consintr = savintr; /* reenable interrupts */ 327791d77e0SPoul-Henning Kamp } 328791d77e0SPoul-Henning Kamp 329791d77e0SPoul-Henning Kamp /* 330791d77e0SPoul-Henning Kamp * Print a character on console or users terminal. If destination is 331791d77e0SPoul-Henning Kamp * the console then the last MSGBUFS characters are saved in msgbuf for 332791d77e0SPoul-Henning Kamp * inspection later. 333791d77e0SPoul-Henning Kamp */ 334791d77e0SPoul-Henning Kamp static void 335791d77e0SPoul-Henning Kamp putchar(int c, void *arg) 336791d77e0SPoul-Henning Kamp { 337791d77e0SPoul-Henning Kamp struct putchar_arg *ap = (struct putchar_arg*) arg; 338791d77e0SPoul-Henning Kamp int flags = ap->flags; 339791d77e0SPoul-Henning Kamp struct tty *tp = ap->tty; 340791d77e0SPoul-Henning Kamp if (panicstr) 341791d77e0SPoul-Henning Kamp constty = NULL; 342791d77e0SPoul-Henning Kamp if ((flags & TOCONS) && tp == NULL && constty) { 343791d77e0SPoul-Henning Kamp tp = constty; 344791d77e0SPoul-Henning Kamp flags |= TOTTY; 345791d77e0SPoul-Henning Kamp } 346791d77e0SPoul-Henning Kamp if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 && 347791d77e0SPoul-Henning Kamp (flags & TOCONS) && tp == constty) 348791d77e0SPoul-Henning Kamp constty = NULL; 349791d77e0SPoul-Henning Kamp if ((flags & TOLOG)) 350791d77e0SPoul-Henning Kamp msglogchar(c, NULL); 351791d77e0SPoul-Henning Kamp if ((flags & TOCONS) && constty == NULL && c != '\0') 352791d77e0SPoul-Henning Kamp (*v_putc)(c); 353791d77e0SPoul-Henning Kamp } 354791d77e0SPoul-Henning Kamp 355791d77e0SPoul-Henning Kamp /* 356791d77e0SPoul-Henning Kamp * Scaled down version of sprintf(3). 357791d77e0SPoul-Henning Kamp */ 358791d77e0SPoul-Henning Kamp int 359791d77e0SPoul-Henning Kamp sprintf(char *buf, const char *cfmt, ...) 360791d77e0SPoul-Henning Kamp { 361791d77e0SPoul-Henning Kamp int retval; 362791d77e0SPoul-Henning Kamp va_list ap; 363791d77e0SPoul-Henning Kamp 364791d77e0SPoul-Henning Kamp va_start(ap, cfmt); 365791d77e0SPoul-Henning Kamp retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); 366fe96d47dSPoul-Henning Kamp buf[retval] = '\0'; 367791d77e0SPoul-Henning Kamp va_end(ap); 368791d77e0SPoul-Henning Kamp return retval; 369791d77e0SPoul-Henning Kamp } 370791d77e0SPoul-Henning Kamp 371791d77e0SPoul-Henning Kamp /* 372791d77e0SPoul-Henning Kamp * Put a number (base <= 16) in a buffer in reverse order; return an 373791d77e0SPoul-Henning Kamp * optional length and a pointer to the NULL terminated (preceded?) 374791d77e0SPoul-Henning Kamp * buffer. 375791d77e0SPoul-Henning Kamp */ 376791d77e0SPoul-Henning Kamp static char * 377791d77e0SPoul-Henning Kamp ksprintn(ul, base, lenp) 378791d77e0SPoul-Henning Kamp register u_long ul; 379791d77e0SPoul-Henning Kamp register int base, *lenp; 380791d77e0SPoul-Henning Kamp { /* A long in base 8, plus NULL. */ 381791d77e0SPoul-Henning Kamp static char buf[sizeof(long) * NBBY / 3 + 2]; 382791d77e0SPoul-Henning Kamp register char *p; 383791d77e0SPoul-Henning Kamp 384791d77e0SPoul-Henning Kamp p = buf; 385791d77e0SPoul-Henning Kamp do { 386791d77e0SPoul-Henning Kamp *++p = hex2ascii(ul % base); 387791d77e0SPoul-Henning Kamp } while (ul /= base); 388791d77e0SPoul-Henning Kamp if (lenp) 389791d77e0SPoul-Henning Kamp *lenp = p - buf; 390791d77e0SPoul-Henning Kamp return (p); 391791d77e0SPoul-Henning Kamp } 392791d77e0SPoul-Henning Kamp 393df8bae1dSRodney W. Grimes /* 394df8bae1dSRodney W. Grimes * Scaled down version of printf(3). 395df8bae1dSRodney W. Grimes * 396df8bae1dSRodney W. Grimes * Two additional formats: 397df8bae1dSRodney W. Grimes * 398df8bae1dSRodney W. Grimes * The format %b is supported to decode error registers. 399df8bae1dSRodney W. Grimes * Its usage is: 400df8bae1dSRodney W. Grimes * 401df8bae1dSRodney W. Grimes * printf("reg=%b\n", regval, "<base><arg>*"); 402df8bae1dSRodney W. Grimes * 403df8bae1dSRodney W. Grimes * where <base> is the output base expressed as a control character, e.g. 404df8bae1dSRodney W. Grimes * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 405df8bae1dSRodney W. Grimes * the first of which gives the bit number to be inspected (origin 1), and 406df8bae1dSRodney W. Grimes * the next characters (up to a control character, i.e. a character <= 32), 407df8bae1dSRodney W. Grimes * give the name of the register. Thus: 408df8bae1dSRodney W. Grimes * 409df8bae1dSRodney W. Grimes * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 410df8bae1dSRodney W. Grimes * 411df8bae1dSRodney W. Grimes * would produce output: 412df8bae1dSRodney W. Grimes * 413df8bae1dSRodney W. Grimes * reg=3<BITTWO,BITONE> 414df8bae1dSRodney W. Grimes * 415120f0783SPoul-Henning Kamp * XXX: %D -- Hexdump, takes pointer and separator string: 416120f0783SPoul-Henning Kamp * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX 417120f0783SPoul-Henning Kamp * ("%*D", len, ptr, " " -> XX XX XX XX ... 418df8bae1dSRodney W. Grimes */ 419791d77e0SPoul-Henning Kamp int 420791d77e0SPoul-Henning Kamp kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap) 421df8bae1dSRodney W. Grimes { 422791d77e0SPoul-Henning Kamp #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; } 423791d77e0SPoul-Henning Kamp char *p, *q, *d; 424120f0783SPoul-Henning Kamp u_char *up; 425791d77e0SPoul-Henning Kamp int ch, n; 426df8bae1dSRodney W. Grimes u_long ul; 4274830092aSPoul-Henning Kamp int base, lflag, tmp, width, ladjust, sharpflag, neg, sign, dot; 4284f20e4b1SPoul-Henning Kamp int dwidth; 429df8bae1dSRodney W. Grimes char padc; 430791d77e0SPoul-Henning Kamp int retval = 0; 431791d77e0SPoul-Henning Kamp 432fe96d47dSPoul-Henning Kamp if (!func) 433791d77e0SPoul-Henning Kamp d = (char *) arg; 434791d77e0SPoul-Henning Kamp else 435fe96d47dSPoul-Henning Kamp d = NULL; 4368f5067baSDavid Greenman 4378f5067baSDavid Greenman if (fmt == NULL) 4382336b9d7SBruce Evans fmt = "(fmt null)\n"; 439b4b2f81eSPoul-Henning Kamp 440120f0783SPoul-Henning Kamp if (radix < 2 || radix > 36) 441b4b2f81eSPoul-Henning Kamp radix = 10; 442b4b2f81eSPoul-Henning Kamp 443df8bae1dSRodney W. Grimes for (;;) { 444df8bae1dSRodney W. Grimes padc = ' '; 445df8bae1dSRodney W. Grimes width = 0; 446df8bae1dSRodney W. Grimes while ((ch = *(u_char *)fmt++) != '%') { 447df8bae1dSRodney W. Grimes if (ch == '\0') 448791d77e0SPoul-Henning Kamp return retval; 449791d77e0SPoul-Henning Kamp PCHAR(ch); 450df8bae1dSRodney W. Grimes } 4514f20e4b1SPoul-Henning Kamp lflag = 0; ladjust = 0; sharpflag = 0; neg = 0; 4524f20e4b1SPoul-Henning Kamp sign = 0; dot = 0; dwidth = 0; 453df8bae1dSRodney W. Grimes reswitch: switch (ch = *(u_char *)fmt++) { 4544830092aSPoul-Henning Kamp case '.': 4554830092aSPoul-Henning Kamp dot = 1; 4564830092aSPoul-Henning Kamp goto reswitch; 457791d77e0SPoul-Henning Kamp case '#': 458791d77e0SPoul-Henning Kamp sharpflag = 1; 459791d77e0SPoul-Henning Kamp goto reswitch; 460791d77e0SPoul-Henning Kamp case '+': 461791d77e0SPoul-Henning Kamp sign = 1; 462791d77e0SPoul-Henning Kamp goto reswitch; 463791d77e0SPoul-Henning Kamp case '-': 464791d77e0SPoul-Henning Kamp ladjust = 1; 465791d77e0SPoul-Henning Kamp goto reswitch; 466791d77e0SPoul-Henning Kamp case '%': 467791d77e0SPoul-Henning Kamp PCHAR(ch); 468791d77e0SPoul-Henning Kamp break; 469791d77e0SPoul-Henning Kamp case '*': 470ed71c342SPoul-Henning Kamp if (!dot) { 471791d77e0SPoul-Henning Kamp width = va_arg(ap, int); 472791d77e0SPoul-Henning Kamp if (width < 0) { 473791d77e0SPoul-Henning Kamp ladjust = !ladjust; 474791d77e0SPoul-Henning Kamp width = -width; 475791d77e0SPoul-Henning Kamp } 476ed71c342SPoul-Henning Kamp } else { 477ed71c342SPoul-Henning Kamp dwidth = va_arg(ap, int); 478ed71c342SPoul-Henning Kamp } 479791d77e0SPoul-Henning Kamp goto reswitch; 480df8bae1dSRodney W. Grimes case '0': 4814f20e4b1SPoul-Henning Kamp if (!dot) { 482df8bae1dSRodney W. Grimes padc = '0'; 483df8bae1dSRodney W. Grimes goto reswitch; 4844f20e4b1SPoul-Henning Kamp } 485df8bae1dSRodney W. Grimes case '1': case '2': case '3': case '4': 486df8bae1dSRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 4874f20e4b1SPoul-Henning Kamp for (n = 0;; ++fmt) { 4884f20e4b1SPoul-Henning Kamp n = n * 10 + ch - '0'; 489df8bae1dSRodney W. Grimes ch = *fmt; 490df8bae1dSRodney W. Grimes if (ch < '0' || ch > '9') 491df8bae1dSRodney W. Grimes break; 492df8bae1dSRodney W. Grimes } 4934f20e4b1SPoul-Henning Kamp if (dot) 4944f20e4b1SPoul-Henning Kamp dwidth = n; 4954f20e4b1SPoul-Henning Kamp else 4964f20e4b1SPoul-Henning Kamp width = n; 497df8bae1dSRodney W. Grimes goto reswitch; 498df8bae1dSRodney W. Grimes case 'b': 499df8bae1dSRodney W. Grimes ul = va_arg(ap, int); 500df8bae1dSRodney W. Grimes p = va_arg(ap, char *); 501797f2d22SPoul-Henning Kamp for (q = ksprintn(ul, *p++, NULL); *q;) 502791d77e0SPoul-Henning Kamp PCHAR(*q--); 503df8bae1dSRodney W. Grimes 504df8bae1dSRodney W. Grimes if (!ul) 505df8bae1dSRodney W. Grimes break; 506df8bae1dSRodney W. Grimes 507797f2d22SPoul-Henning Kamp for (tmp = 0; *p;) { 508797f2d22SPoul-Henning Kamp n = *p++; 509df8bae1dSRodney W. Grimes if (ul & (1 << (n - 1))) { 510791d77e0SPoul-Henning Kamp PCHAR(tmp ? ',' : '<'); 511df8bae1dSRodney W. Grimes for (; (n = *p) > ' '; ++p) 512791d77e0SPoul-Henning Kamp PCHAR(n); 513df8bae1dSRodney W. Grimes tmp = 1; 514df8bae1dSRodney W. Grimes } else 515df8bae1dSRodney W. Grimes for (; *p > ' '; ++p) 516df8bae1dSRodney W. Grimes continue; 517df8bae1dSRodney W. Grimes } 518df8bae1dSRodney W. Grimes if (tmp) 519791d77e0SPoul-Henning Kamp PCHAR('>'); 520df8bae1dSRodney W. Grimes break; 521df8bae1dSRodney W. Grimes case 'c': 522791d77e0SPoul-Henning Kamp PCHAR(va_arg(ap, int)); 523df8bae1dSRodney W. Grimes break; 524120f0783SPoul-Henning Kamp case 'D': 525120f0783SPoul-Henning Kamp up = va_arg(ap, u_char *); 526120f0783SPoul-Henning Kamp p = va_arg(ap, char *); 527120f0783SPoul-Henning Kamp if (!width) 528120f0783SPoul-Henning Kamp width = 16; 529120f0783SPoul-Henning Kamp while(width--) { 530120f0783SPoul-Henning Kamp PCHAR(hex2ascii(*up >> 4)); 531120f0783SPoul-Henning Kamp PCHAR(hex2ascii(*up & 0x0f)); 532120f0783SPoul-Henning Kamp up++; 533120f0783SPoul-Henning Kamp if (width) 534120f0783SPoul-Henning Kamp for (q=p;*q;q++) 535120f0783SPoul-Henning Kamp PCHAR(*q); 536120f0783SPoul-Henning Kamp } 537120f0783SPoul-Henning Kamp break; 538df8bae1dSRodney W. Grimes case 'd': 539df8bae1dSRodney W. Grimes ul = lflag ? va_arg(ap, long) : va_arg(ap, int); 540791d77e0SPoul-Henning Kamp sign = 1; 541df8bae1dSRodney W. Grimes base = 10; 542df8bae1dSRodney W. Grimes goto number; 543791d77e0SPoul-Henning Kamp case 'l': 544791d77e0SPoul-Henning Kamp lflag = 1; 545791d77e0SPoul-Henning Kamp goto reswitch; 546791d77e0SPoul-Henning Kamp case 'n': 547791d77e0SPoul-Henning Kamp ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 548791d77e0SPoul-Henning Kamp base = radix; 549791d77e0SPoul-Henning Kamp goto number; 550df8bae1dSRodney W. Grimes case 'o': 551df8bae1dSRodney W. Grimes ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 552df8bae1dSRodney W. Grimes base = 8; 553df8bae1dSRodney W. Grimes goto number; 55412d17f65SPoul-Henning Kamp case 'p': 55512d17f65SPoul-Henning Kamp ul = (u_long)va_arg(ap, void *); 55612d17f65SPoul-Henning Kamp base = 16; 557791d77e0SPoul-Henning Kamp PCHAR('0'); 558791d77e0SPoul-Henning Kamp PCHAR('x'); 55912d17f65SPoul-Henning Kamp goto number; 560791d77e0SPoul-Henning Kamp case 's': 561791d77e0SPoul-Henning Kamp p = va_arg(ap, char *); 562791d77e0SPoul-Henning Kamp if (p == NULL) 563791d77e0SPoul-Henning Kamp p = "(null)"; 5644830092aSPoul-Henning Kamp if (!dot) 5654830092aSPoul-Henning Kamp n = strlen (p); 5664830092aSPoul-Henning Kamp else 5674f20e4b1SPoul-Henning Kamp for (n = 0; n < dwidth && p[n]; n++) 5684830092aSPoul-Henning Kamp continue; 5694f20e4b1SPoul-Henning Kamp 5704830092aSPoul-Henning Kamp width -= n; 5714f20e4b1SPoul-Henning Kamp 572791d77e0SPoul-Henning Kamp if (!ladjust && width > 0) 573791d77e0SPoul-Henning Kamp while (width--) 574791d77e0SPoul-Henning Kamp PCHAR(padc); 5754830092aSPoul-Henning Kamp while (n--) 576791d77e0SPoul-Henning Kamp PCHAR(*p++); 577791d77e0SPoul-Henning Kamp if (ladjust && width > 0) 578791d77e0SPoul-Henning Kamp while (width--) 579791d77e0SPoul-Henning Kamp PCHAR(padc); 580791d77e0SPoul-Henning Kamp break; 581f53dbe97SBruce Evans case 'u': 582f53dbe97SBruce Evans ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 583f53dbe97SBruce Evans base = 10; 584f53dbe97SBruce Evans goto number; 585df8bae1dSRodney W. Grimes case 'x': 586df8bae1dSRodney W. Grimes ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 587df8bae1dSRodney W. Grimes base = 16; 588791d77e0SPoul-Henning Kamp number: if (sign && (long)ul < 0L) { 589791d77e0SPoul-Henning Kamp neg = 1; 590791d77e0SPoul-Henning Kamp ul = -(long)ul; 591791d77e0SPoul-Henning Kamp } 592791d77e0SPoul-Henning Kamp p = ksprintn(ul, base, &tmp); 593791d77e0SPoul-Henning Kamp if (sharpflag && ul != 0) { 594791d77e0SPoul-Henning Kamp if (base == 8) 595791d77e0SPoul-Henning Kamp tmp++; 596791d77e0SPoul-Henning Kamp else if (base == 16) 597791d77e0SPoul-Henning Kamp tmp += 2; 598791d77e0SPoul-Henning Kamp } 599791d77e0SPoul-Henning Kamp if (neg) 600791d77e0SPoul-Henning Kamp tmp++; 601791d77e0SPoul-Henning Kamp 602791d77e0SPoul-Henning Kamp if (!ladjust && width && (width -= tmp) > 0) 603df8bae1dSRodney W. Grimes while (width--) 604791d77e0SPoul-Henning Kamp PCHAR(padc); 605791d77e0SPoul-Henning Kamp if (neg) 606791d77e0SPoul-Henning Kamp PCHAR('-'); 607791d77e0SPoul-Henning Kamp if (sharpflag && ul != 0) { 608791d77e0SPoul-Henning Kamp if (base == 8) { 609791d77e0SPoul-Henning Kamp PCHAR('0'); 610791d77e0SPoul-Henning Kamp } else if (base == 16) { 611791d77e0SPoul-Henning Kamp PCHAR('0'); 612791d77e0SPoul-Henning Kamp PCHAR('x'); 613791d77e0SPoul-Henning Kamp } 614791d77e0SPoul-Henning Kamp } 615791d77e0SPoul-Henning Kamp 616797f2d22SPoul-Henning Kamp while (*p) 617791d77e0SPoul-Henning Kamp PCHAR(*p--); 618791d77e0SPoul-Henning Kamp 619791d77e0SPoul-Henning Kamp if (ladjust && width && (width -= tmp) > 0) 620791d77e0SPoul-Henning Kamp while (width--) 621791d77e0SPoul-Henning Kamp PCHAR(padc); 622791d77e0SPoul-Henning Kamp 623df8bae1dSRodney W. Grimes break; 624df8bae1dSRodney W. Grimes default: 625791d77e0SPoul-Henning Kamp PCHAR('%'); 626df8bae1dSRodney W. Grimes if (lflag) 627791d77e0SPoul-Henning Kamp PCHAR('l'); 628791d77e0SPoul-Henning Kamp PCHAR(ch); 629791d77e0SPoul-Henning Kamp break; 630df8bae1dSRodney W. Grimes } 631df8bae1dSRodney W. Grimes } 632791d77e0SPoul-Henning Kamp #undef PCHAR 633df8bae1dSRodney W. Grimes } 634df8bae1dSRodney W. Grimes 635df8bae1dSRodney W. Grimes /* 636791d77e0SPoul-Henning Kamp * Put character in log buffer. 637df8bae1dSRodney W. Grimes */ 638df8bae1dSRodney W. Grimes static void 639791d77e0SPoul-Henning Kamp msglogchar(int c, void *dummyarg) 640df8bae1dSRodney W. Grimes { 641791d77e0SPoul-Henning Kamp struct msgbuf *mbp; 642df8bae1dSRodney W. Grimes 643791d77e0SPoul-Henning Kamp if (c != '\0' && c != '\r' && c != 0177 && msgbufmapped) { 644df8bae1dSRodney W. Grimes mbp = msgbufp; 6456c8897cfSDavid Greenman if (mbp->msg_magic != MSG_MAGIC || 6466c8897cfSDavid Greenman mbp->msg_bufx >= MSG_BSIZE || 6476c8897cfSDavid Greenman mbp->msg_bufr >= MSG_BSIZE) { 6486c8897cfSDavid Greenman bzero(mbp, sizeof(struct msgbuf)); 649df8bae1dSRodney W. Grimes mbp->msg_magic = MSG_MAGIC; 650df8bae1dSRodney W. Grimes } 651df8bae1dSRodney W. Grimes mbp->msg_bufc[mbp->msg_bufx++] = c; 652b4224c9cSDavid Greenman if (mbp->msg_bufx >= MSG_BSIZE) 653df8bae1dSRodney W. Grimes mbp->msg_bufx = 0; 65402d5c7b1SDavid Greenman /* If the buffer is full, keep the most recent data. */ 655a3f4faceSBruce Evans if (mbp->msg_bufr == mbp->msg_bufx) { 65602d5c7b1SDavid Greenman if (++mbp->msg_bufr >= MSG_BSIZE) 657a3f4faceSBruce Evans mbp->msg_bufr = 0; 658a3f4faceSBruce Evans } 659df8bae1dSRodney W. Grimes } 660df8bae1dSRodney W. Grimes } 661