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 39e796e00dSPoul-Henning Kamp * $Id: subr_prf.c,v 1.45 1998/05/19 08:58:51 phk Exp $ 40df8bae1dSRodney W. Grimes */ 41df8bae1dSRodney W. Grimes 42df8bae1dSRodney W. Grimes #include <sys/param.h> 43df8bae1dSRodney W. Grimes #include <sys/systm.h> 44e796e00dSPoul-Henning Kamp #include <sys/kernel.h> 45df8bae1dSRodney W. Grimes #include <sys/msgbuf.h> 46a1c995b6SPoul-Henning Kamp #include <sys/malloc.h> 47df8bae1dSRodney W. Grimes #include <sys/proc.h> 48df8bae1dSRodney W. Grimes #include <sys/tty.h> 49df8bae1dSRodney W. Grimes #include <sys/tprintf.h> 50df8bae1dSRodney W. Grimes #include <sys/syslog.h> 514fb0b0deSJoerg Wunsch #include <machine/cons.h> 52df8bae1dSRodney W. Grimes 53df8bae1dSRodney W. Grimes /* 54df8bae1dSRodney W. Grimes * Note that stdarg.h and the ANSI style va_start macro is used for both 55df8bae1dSRodney W. Grimes * ANSI and traditional C compilers. 56df8bae1dSRodney W. Grimes */ 57df8bae1dSRodney W. Grimes #include <machine/stdarg.h> 58df8bae1dSRodney W. Grimes 59df8bae1dSRodney W. Grimes #define TOCONS 0x01 60df8bae1dSRodney W. Grimes #define TOTTY 0x02 61df8bae1dSRodney W. Grimes #define TOLOG 0x04 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes struct tty *constty; /* pointer to console "window" tty */ 64df8bae1dSRodney W. Grimes 6587b6de2bSPoul-Henning Kamp static void (*v_putc)(int) = cnputc; /* routine to putc on virtual console */ 6687b6de2bSPoul-Henning Kamp static void logpri __P((int level)); 67791d77e0SPoul-Henning Kamp static void msglogchar(int c, void *dummyarg); 68791d77e0SPoul-Henning Kamp struct putchar_arg {int flags; struct tty *tty; }; 69791d77e0SPoul-Henning Kamp static void putchar __P((int ch, void *arg)); 70df8bae1dSRodney W. Grimes static char *ksprintn __P((u_long num, int base, int *len)); 71df8bae1dSRodney W. Grimes 7287b6de2bSPoul-Henning Kamp static int consintr = 1; /* Ok to handle console interrupts? */ 73e796e00dSPoul-Henning Kamp static int msgbufmapped; /* Set when safe to use msgbuf */ 74df8bae1dSRodney W. Grimes 75df8bae1dSRodney W. Grimes /* 76df8bae1dSRodney W. Grimes * Warn that a system table is full. 77df8bae1dSRodney W. Grimes */ 78df8bae1dSRodney W. Grimes void 79df8bae1dSRodney W. Grimes tablefull(tab) 80df8bae1dSRodney W. Grimes const char *tab; 81df8bae1dSRodney W. Grimes { 82df8bae1dSRodney W. Grimes 83df8bae1dSRodney W. Grimes log(LOG_ERR, "%s: table is full\n", tab); 84df8bae1dSRodney W. Grimes } 85df8bae1dSRodney W. Grimes 86df8bae1dSRodney W. Grimes /* 87df8bae1dSRodney W. Grimes * Uprintf prints to the controlling terminal for the current process. 88df8bae1dSRodney W. Grimes * It may block if the tty queue is overfull. No message is printed if 89df8bae1dSRodney W. Grimes * the queue does not clear in a reasonable time. 90df8bae1dSRodney W. Grimes */ 91df8bae1dSRodney W. Grimes void 92df8bae1dSRodney W. Grimes uprintf(const char *fmt, ...) 93df8bae1dSRodney W. Grimes { 94791d77e0SPoul-Henning Kamp struct proc *p = curproc; 95df8bae1dSRodney W. Grimes va_list ap; 96791d77e0SPoul-Henning Kamp struct putchar_arg pca; 97df8bae1dSRodney W. Grimes 98df8bae1dSRodney W. Grimes if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { 99df8bae1dSRodney W. Grimes va_start(ap, fmt); 100791d77e0SPoul-Henning Kamp pca.tty = p->p_session->s_ttyp; 101791d77e0SPoul-Henning Kamp pca.flags = TOTTY; 102791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 103df8bae1dSRodney W. Grimes va_end(ap); 104df8bae1dSRodney W. Grimes } 105df8bae1dSRodney W. Grimes } 106df8bae1dSRodney W. Grimes 107df8bae1dSRodney W. Grimes tpr_t 108df8bae1dSRodney W. Grimes tprintf_open(p) 109df8bae1dSRodney W. Grimes register struct proc *p; 110df8bae1dSRodney W. Grimes { 111df8bae1dSRodney W. Grimes 112df8bae1dSRodney W. Grimes if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { 113df8bae1dSRodney W. Grimes SESSHOLD(p->p_session); 114df8bae1dSRodney W. Grimes return ((tpr_t) p->p_session); 115df8bae1dSRodney W. Grimes } 116df8bae1dSRodney W. Grimes return ((tpr_t) NULL); 117df8bae1dSRodney W. Grimes } 118df8bae1dSRodney W. Grimes 119df8bae1dSRodney W. Grimes void 120df8bae1dSRodney W. Grimes tprintf_close(sess) 121df8bae1dSRodney W. Grimes tpr_t sess; 122df8bae1dSRodney W. Grimes { 123df8bae1dSRodney W. Grimes 124df8bae1dSRodney W. Grimes if (sess) 125df8bae1dSRodney W. Grimes SESSRELE((struct session *) sess); 126df8bae1dSRodney W. Grimes } 127df8bae1dSRodney W. Grimes 128df8bae1dSRodney W. Grimes /* 129df8bae1dSRodney W. Grimes * tprintf prints on the controlling terminal associated 130df8bae1dSRodney W. Grimes * with the given session. 131df8bae1dSRodney W. Grimes */ 132df8bae1dSRodney W. Grimes void 133df8bae1dSRodney W. Grimes tprintf(tpr_t tpr, const char *fmt, ...) 134df8bae1dSRodney W. Grimes { 135df8bae1dSRodney W. Grimes register struct session *sess = (struct session *)tpr; 136df8bae1dSRodney W. Grimes struct tty *tp = NULL; 137df8bae1dSRodney W. Grimes int flags = TOLOG; 138df8bae1dSRodney W. Grimes va_list ap; 139791d77e0SPoul-Henning Kamp struct putchar_arg pca; 140df8bae1dSRodney W. Grimes 141df8bae1dSRodney W. Grimes logpri(LOG_INFO); 142df8bae1dSRodney W. Grimes if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { 143df8bae1dSRodney W. Grimes flags |= TOTTY; 144df8bae1dSRodney W. Grimes tp = sess->s_ttyp; 145df8bae1dSRodney W. Grimes } 146df8bae1dSRodney W. Grimes va_start(ap, fmt); 147791d77e0SPoul-Henning Kamp pca.tty = tp; 148791d77e0SPoul-Henning Kamp pca.flags = flags; 149791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 150df8bae1dSRodney W. Grimes va_end(ap); 151df8bae1dSRodney W. Grimes logwakeup(); 152df8bae1dSRodney W. Grimes } 153df8bae1dSRodney W. Grimes 154df8bae1dSRodney W. Grimes /* 155df8bae1dSRodney W. Grimes * Ttyprintf displays a message on a tty; it should be used only by 156df8bae1dSRodney W. Grimes * the tty driver, or anything that knows the underlying tty will not 157df8bae1dSRodney W. Grimes * be revoke(2)'d away. Other callers should use tprintf. 158df8bae1dSRodney W. Grimes */ 159df8bae1dSRodney W. Grimes void 160df8bae1dSRodney W. Grimes ttyprintf(struct tty *tp, const char *fmt, ...) 161df8bae1dSRodney W. Grimes { 162df8bae1dSRodney W. Grimes va_list ap; 163791d77e0SPoul-Henning Kamp struct putchar_arg pca; 164df8bae1dSRodney W. Grimes va_start(ap, fmt); 165791d77e0SPoul-Henning Kamp pca.tty = tp; 166791d77e0SPoul-Henning Kamp pca.flags = TOTTY; 167791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 168df8bae1dSRodney W. Grimes va_end(ap); 169df8bae1dSRodney W. Grimes } 170df8bae1dSRodney W. Grimes 171df8bae1dSRodney W. Grimes extern int log_open; 172df8bae1dSRodney W. Grimes 173df8bae1dSRodney W. Grimes /* 174df8bae1dSRodney W. Grimes * Log writes to the log buffer, and guarantees not to sleep (so can be 175df8bae1dSRodney W. Grimes * called by interrupt routines). If there is no process reading the 176df8bae1dSRodney W. Grimes * log yet, it writes to the console also. 177df8bae1dSRodney W. Grimes */ 178df8bae1dSRodney W. Grimes void 179df8bae1dSRodney W. Grimes log(int level, const char *fmt, ...) 180df8bae1dSRodney W. Grimes { 181df8bae1dSRodney W. Grimes register int s; 182df8bae1dSRodney W. Grimes va_list ap; 183df8bae1dSRodney W. Grimes 184df8bae1dSRodney W. Grimes s = splhigh(); 185df8bae1dSRodney W. Grimes logpri(level); 186df8bae1dSRodney W. Grimes va_start(ap, fmt); 187791d77e0SPoul-Henning Kamp 188791d77e0SPoul-Henning Kamp kvprintf(fmt, msglogchar, NULL, 10, ap); 189df8bae1dSRodney W. Grimes va_end(ap); 190791d77e0SPoul-Henning Kamp 191791d77e0SPoul-Henning Kamp splx(s); 192df8bae1dSRodney W. Grimes if (!log_open) { 193791d77e0SPoul-Henning Kamp struct putchar_arg pca; 194df8bae1dSRodney W. Grimes va_start(ap, fmt); 195791d77e0SPoul-Henning Kamp pca.tty = NULL; 196791d77e0SPoul-Henning Kamp pca.flags = TOCONS; 197791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 198df8bae1dSRodney W. Grimes va_end(ap); 199df8bae1dSRodney W. Grimes } 200df8bae1dSRodney W. Grimes logwakeup(); 201df8bae1dSRodney W. Grimes } 202df8bae1dSRodney W. Grimes 20387b6de2bSPoul-Henning Kamp static void 204df8bae1dSRodney W. Grimes logpri(level) 205df8bae1dSRodney W. Grimes int level; 206df8bae1dSRodney W. Grimes { 207df8bae1dSRodney W. Grimes register char *p; 208df8bae1dSRodney W. Grimes 209791d77e0SPoul-Henning Kamp msglogchar('<', NULL); 210797f2d22SPoul-Henning Kamp for (p = ksprintn((u_long)level, 10, NULL); *p;) 211791d77e0SPoul-Henning Kamp msglogchar(*p--, NULL); 212791d77e0SPoul-Henning Kamp msglogchar('>', NULL); 213df8bae1dSRodney W. Grimes } 214df8bae1dSRodney W. Grimes 2156ddbf1e2SGary Palmer int 216df8bae1dSRodney W. Grimes addlog(const char *fmt, ...) 217df8bae1dSRodney W. Grimes { 218df8bae1dSRodney W. Grimes register int s; 219df8bae1dSRodney W. Grimes va_list ap; 2206ddbf1e2SGary Palmer int retval; 221df8bae1dSRodney W. Grimes 222df8bae1dSRodney W. Grimes s = splhigh(); 223df8bae1dSRodney W. Grimes va_start(ap, fmt); 2246ddbf1e2SGary Palmer retval = kvprintf(fmt, msglogchar, NULL, 10, ap); 225df8bae1dSRodney W. Grimes splx(s); 226df8bae1dSRodney W. Grimes va_end(ap); 227df8bae1dSRodney W. Grimes if (!log_open) { 228791d77e0SPoul-Henning Kamp struct putchar_arg pca; 229df8bae1dSRodney W. Grimes va_start(ap, fmt); 230791d77e0SPoul-Henning Kamp pca.tty = NULL; 231791d77e0SPoul-Henning Kamp pca.flags = TOCONS; 232791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 233df8bae1dSRodney W. Grimes va_end(ap); 234df8bae1dSRodney W. Grimes } 235df8bae1dSRodney W. Grimes logwakeup(); 2366ddbf1e2SGary Palmer return (retval); 237df8bae1dSRodney W. Grimes } 238df8bae1dSRodney W. Grimes 23965ed8cbdSJustin T. Gibbs int 240df8bae1dSRodney W. Grimes printf(const char *fmt, ...) 241df8bae1dSRodney W. Grimes { 242df8bae1dSRodney W. Grimes va_list ap; 243df8bae1dSRodney W. Grimes register int savintr; 244791d77e0SPoul-Henning Kamp struct putchar_arg pca; 24565ed8cbdSJustin T. Gibbs int retval; 246df8bae1dSRodney W. Grimes 247df8bae1dSRodney W. Grimes savintr = consintr; /* disable interrupts */ 248df8bae1dSRodney W. Grimes consintr = 0; 249df8bae1dSRodney W. Grimes va_start(ap, fmt); 250791d77e0SPoul-Henning Kamp pca.tty = NULL; 251791d77e0SPoul-Henning Kamp pca.flags = TOCONS | TOLOG; 25265ed8cbdSJustin T. Gibbs retval = kvprintf(fmt, putchar, &pca, 10, ap); 253df8bae1dSRodney W. Grimes va_end(ap); 254df8bae1dSRodney W. Grimes if (!panicstr) 255df8bae1dSRodney W. Grimes logwakeup(); 256df8bae1dSRodney W. Grimes consintr = savintr; /* reenable interrupts */ 25765ed8cbdSJustin T. Gibbs return retval; 258df8bae1dSRodney W. Grimes } 259df8bae1dSRodney W. Grimes 260791d77e0SPoul-Henning Kamp void 261791d77e0SPoul-Henning Kamp vprintf(const char *fmt, va_list ap) 262791d77e0SPoul-Henning Kamp { 263791d77e0SPoul-Henning Kamp register int savintr; 264791d77e0SPoul-Henning Kamp struct putchar_arg pca; 265791d77e0SPoul-Henning Kamp 266791d77e0SPoul-Henning Kamp savintr = consintr; /* disable interrupts */ 267791d77e0SPoul-Henning Kamp consintr = 0; 268791d77e0SPoul-Henning Kamp pca.tty = NULL; 269791d77e0SPoul-Henning Kamp pca.flags = TOCONS | TOLOG; 270791d77e0SPoul-Henning Kamp kvprintf(fmt, putchar, &pca, 10, ap); 271791d77e0SPoul-Henning Kamp if (!panicstr) 272791d77e0SPoul-Henning Kamp logwakeup(); 273791d77e0SPoul-Henning Kamp consintr = savintr; /* reenable interrupts */ 274791d77e0SPoul-Henning Kamp } 275791d77e0SPoul-Henning Kamp 276791d77e0SPoul-Henning Kamp /* 277791d77e0SPoul-Henning Kamp * Print a character on console or users terminal. If destination is 278e796e00dSPoul-Henning Kamp * the console then the last bunch of characters are saved in msgbuf for 279791d77e0SPoul-Henning Kamp * inspection later. 280791d77e0SPoul-Henning Kamp */ 281791d77e0SPoul-Henning Kamp static void 282791d77e0SPoul-Henning Kamp putchar(int c, void *arg) 283791d77e0SPoul-Henning Kamp { 284791d77e0SPoul-Henning Kamp struct putchar_arg *ap = (struct putchar_arg*) arg; 285791d77e0SPoul-Henning Kamp int flags = ap->flags; 286791d77e0SPoul-Henning Kamp struct tty *tp = ap->tty; 287791d77e0SPoul-Henning Kamp if (panicstr) 288791d77e0SPoul-Henning Kamp constty = NULL; 289791d77e0SPoul-Henning Kamp if ((flags & TOCONS) && tp == NULL && constty) { 290791d77e0SPoul-Henning Kamp tp = constty; 291791d77e0SPoul-Henning Kamp flags |= TOTTY; 292791d77e0SPoul-Henning Kamp } 293791d77e0SPoul-Henning Kamp if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 && 294791d77e0SPoul-Henning Kamp (flags & TOCONS) && tp == constty) 295791d77e0SPoul-Henning Kamp constty = NULL; 296791d77e0SPoul-Henning Kamp if ((flags & TOLOG)) 297791d77e0SPoul-Henning Kamp msglogchar(c, NULL); 298791d77e0SPoul-Henning Kamp if ((flags & TOCONS) && constty == NULL && c != '\0') 299791d77e0SPoul-Henning Kamp (*v_putc)(c); 300791d77e0SPoul-Henning Kamp } 301791d77e0SPoul-Henning Kamp 302791d77e0SPoul-Henning Kamp /* 303791d77e0SPoul-Henning Kamp * Scaled down version of sprintf(3). 304791d77e0SPoul-Henning Kamp */ 305791d77e0SPoul-Henning Kamp int 306791d77e0SPoul-Henning Kamp sprintf(char *buf, const char *cfmt, ...) 307791d77e0SPoul-Henning Kamp { 308791d77e0SPoul-Henning Kamp int retval; 309791d77e0SPoul-Henning Kamp va_list ap; 310791d77e0SPoul-Henning Kamp 311791d77e0SPoul-Henning Kamp va_start(ap, cfmt); 312791d77e0SPoul-Henning Kamp retval = kvprintf(cfmt, NULL, (void *)buf, 10, ap); 313fe96d47dSPoul-Henning Kamp buf[retval] = '\0'; 314791d77e0SPoul-Henning Kamp va_end(ap); 315791d77e0SPoul-Henning Kamp return retval; 316791d77e0SPoul-Henning Kamp } 317791d77e0SPoul-Henning Kamp 318791d77e0SPoul-Henning Kamp /* 319791d77e0SPoul-Henning Kamp * Put a number (base <= 16) in a buffer in reverse order; return an 320791d77e0SPoul-Henning Kamp * optional length and a pointer to the NULL terminated (preceded?) 321791d77e0SPoul-Henning Kamp * buffer. 322791d77e0SPoul-Henning Kamp */ 323791d77e0SPoul-Henning Kamp static char * 324791d77e0SPoul-Henning Kamp ksprintn(ul, base, lenp) 325791d77e0SPoul-Henning Kamp register u_long ul; 326791d77e0SPoul-Henning Kamp register int base, *lenp; 327791d77e0SPoul-Henning Kamp { /* A long in base 8, plus NULL. */ 328791d77e0SPoul-Henning Kamp static char buf[sizeof(long) * NBBY / 3 + 2]; 329791d77e0SPoul-Henning Kamp register char *p; 330791d77e0SPoul-Henning Kamp 331791d77e0SPoul-Henning Kamp p = buf; 332791d77e0SPoul-Henning Kamp do { 333791d77e0SPoul-Henning Kamp *++p = hex2ascii(ul % base); 334791d77e0SPoul-Henning Kamp } while (ul /= base); 335791d77e0SPoul-Henning Kamp if (lenp) 336791d77e0SPoul-Henning Kamp *lenp = p - buf; 337791d77e0SPoul-Henning Kamp return (p); 338791d77e0SPoul-Henning Kamp } 339791d77e0SPoul-Henning Kamp 340df8bae1dSRodney W. Grimes /* 341df8bae1dSRodney W. Grimes * Scaled down version of printf(3). 342df8bae1dSRodney W. Grimes * 343df8bae1dSRodney W. Grimes * Two additional formats: 344df8bae1dSRodney W. Grimes * 345df8bae1dSRodney W. Grimes * The format %b is supported to decode error registers. 346df8bae1dSRodney W. Grimes * Its usage is: 347df8bae1dSRodney W. Grimes * 348df8bae1dSRodney W. Grimes * printf("reg=%b\n", regval, "<base><arg>*"); 349df8bae1dSRodney W. Grimes * 350df8bae1dSRodney W. Grimes * where <base> is the output base expressed as a control character, e.g. 351df8bae1dSRodney W. Grimes * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 352df8bae1dSRodney W. Grimes * the first of which gives the bit number to be inspected (origin 1), and 353df8bae1dSRodney W. Grimes * the next characters (up to a control character, i.e. a character <= 32), 354df8bae1dSRodney W. Grimes * give the name of the register. Thus: 355df8bae1dSRodney W. Grimes * 3564e31a37bSGary Palmer * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 357df8bae1dSRodney W. Grimes * 358df8bae1dSRodney W. Grimes * would produce output: 359df8bae1dSRodney W. Grimes * 360df8bae1dSRodney W. Grimes * reg=3<BITTWO,BITONE> 361df8bae1dSRodney W. Grimes * 362120f0783SPoul-Henning Kamp * XXX: %D -- Hexdump, takes pointer and separator string: 363120f0783SPoul-Henning Kamp * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX 364120f0783SPoul-Henning Kamp * ("%*D", len, ptr, " " -> XX XX XX XX ... 365df8bae1dSRodney W. Grimes */ 366791d77e0SPoul-Henning Kamp int 367791d77e0SPoul-Henning Kamp kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap) 368df8bae1dSRodney W. Grimes { 369791d77e0SPoul-Henning Kamp #define PCHAR(c) {int cc=(c); if (func) (*func)(cc,arg); else *d++ = cc; retval++; } 370791d77e0SPoul-Henning Kamp char *p, *q, *d; 371120f0783SPoul-Henning Kamp u_char *up; 372791d77e0SPoul-Henning Kamp int ch, n; 373df8bae1dSRodney W. Grimes u_long ul; 3744830092aSPoul-Henning Kamp int base, lflag, tmp, width, ladjust, sharpflag, neg, sign, dot; 3754f20e4b1SPoul-Henning Kamp int dwidth; 376df8bae1dSRodney W. Grimes char padc; 377791d77e0SPoul-Henning Kamp int retval = 0; 378791d77e0SPoul-Henning Kamp 379fe96d47dSPoul-Henning Kamp if (!func) 380791d77e0SPoul-Henning Kamp d = (char *) arg; 381791d77e0SPoul-Henning Kamp else 382fe96d47dSPoul-Henning Kamp d = NULL; 3838f5067baSDavid Greenman 3848f5067baSDavid Greenman if (fmt == NULL) 3852336b9d7SBruce Evans fmt = "(fmt null)\n"; 386b4b2f81eSPoul-Henning Kamp 387120f0783SPoul-Henning Kamp if (radix < 2 || radix > 36) 388b4b2f81eSPoul-Henning Kamp radix = 10; 389b4b2f81eSPoul-Henning Kamp 390df8bae1dSRodney W. Grimes for (;;) { 391df8bae1dSRodney W. Grimes padc = ' '; 392df8bae1dSRodney W. Grimes width = 0; 393e0c95ed9SBruce Evans while ((ch = (u_char)*fmt++) != '%') { 394df8bae1dSRodney W. Grimes if (ch == '\0') 395791d77e0SPoul-Henning Kamp return retval; 396791d77e0SPoul-Henning Kamp PCHAR(ch); 397df8bae1dSRodney W. Grimes } 3984f20e4b1SPoul-Henning Kamp lflag = 0; ladjust = 0; sharpflag = 0; neg = 0; 3994f20e4b1SPoul-Henning Kamp sign = 0; dot = 0; dwidth = 0; 400e0c95ed9SBruce Evans reswitch: switch (ch = (u_char)*fmt++) { 4014830092aSPoul-Henning Kamp case '.': 4024830092aSPoul-Henning Kamp dot = 1; 4034830092aSPoul-Henning Kamp goto reswitch; 404791d77e0SPoul-Henning Kamp case '#': 405791d77e0SPoul-Henning Kamp sharpflag = 1; 406791d77e0SPoul-Henning Kamp goto reswitch; 407791d77e0SPoul-Henning Kamp case '+': 408791d77e0SPoul-Henning Kamp sign = 1; 409791d77e0SPoul-Henning Kamp goto reswitch; 410791d77e0SPoul-Henning Kamp case '-': 411791d77e0SPoul-Henning Kamp ladjust = 1; 412791d77e0SPoul-Henning Kamp goto reswitch; 413791d77e0SPoul-Henning Kamp case '%': 414791d77e0SPoul-Henning Kamp PCHAR(ch); 415791d77e0SPoul-Henning Kamp break; 416791d77e0SPoul-Henning Kamp case '*': 417ed71c342SPoul-Henning Kamp if (!dot) { 418791d77e0SPoul-Henning Kamp width = va_arg(ap, int); 419791d77e0SPoul-Henning Kamp if (width < 0) { 420791d77e0SPoul-Henning Kamp ladjust = !ladjust; 421791d77e0SPoul-Henning Kamp width = -width; 422791d77e0SPoul-Henning Kamp } 423ed71c342SPoul-Henning Kamp } else { 424ed71c342SPoul-Henning Kamp dwidth = va_arg(ap, int); 425ed71c342SPoul-Henning Kamp } 426791d77e0SPoul-Henning Kamp goto reswitch; 427df8bae1dSRodney W. Grimes case '0': 4284f20e4b1SPoul-Henning Kamp if (!dot) { 429df8bae1dSRodney W. Grimes padc = '0'; 430df8bae1dSRodney W. Grimes goto reswitch; 4314f20e4b1SPoul-Henning Kamp } 432df8bae1dSRodney W. Grimes case '1': case '2': case '3': case '4': 433df8bae1dSRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 4344f20e4b1SPoul-Henning Kamp for (n = 0;; ++fmt) { 4354f20e4b1SPoul-Henning Kamp n = n * 10 + ch - '0'; 436df8bae1dSRodney W. Grimes ch = *fmt; 437df8bae1dSRodney W. Grimes if (ch < '0' || ch > '9') 438df8bae1dSRodney W. Grimes break; 439df8bae1dSRodney W. Grimes } 4404f20e4b1SPoul-Henning Kamp if (dot) 4414f20e4b1SPoul-Henning Kamp dwidth = n; 4424f20e4b1SPoul-Henning Kamp else 4434f20e4b1SPoul-Henning Kamp width = n; 444df8bae1dSRodney W. Grimes goto reswitch; 445df8bae1dSRodney W. Grimes case 'b': 446df8bae1dSRodney W. Grimes ul = va_arg(ap, int); 447df8bae1dSRodney W. Grimes p = va_arg(ap, char *); 448797f2d22SPoul-Henning Kamp for (q = ksprintn(ul, *p++, NULL); *q;) 449791d77e0SPoul-Henning Kamp PCHAR(*q--); 450df8bae1dSRodney W. Grimes 451df8bae1dSRodney W. Grimes if (!ul) 452df8bae1dSRodney W. Grimes break; 453df8bae1dSRodney W. Grimes 454797f2d22SPoul-Henning Kamp for (tmp = 0; *p;) { 455797f2d22SPoul-Henning Kamp n = *p++; 456df8bae1dSRodney W. Grimes if (ul & (1 << (n - 1))) { 457791d77e0SPoul-Henning Kamp PCHAR(tmp ? ',' : '<'); 458df8bae1dSRodney W. Grimes for (; (n = *p) > ' '; ++p) 459791d77e0SPoul-Henning Kamp PCHAR(n); 460df8bae1dSRodney W. Grimes tmp = 1; 461df8bae1dSRodney W. Grimes } else 462df8bae1dSRodney W. Grimes for (; *p > ' '; ++p) 463df8bae1dSRodney W. Grimes continue; 464df8bae1dSRodney W. Grimes } 465df8bae1dSRodney W. Grimes if (tmp) 466791d77e0SPoul-Henning Kamp PCHAR('>'); 467df8bae1dSRodney W. Grimes break; 468df8bae1dSRodney W. Grimes case 'c': 469791d77e0SPoul-Henning Kamp PCHAR(va_arg(ap, int)); 470df8bae1dSRodney W. Grimes break; 471120f0783SPoul-Henning Kamp case 'D': 472120f0783SPoul-Henning Kamp up = va_arg(ap, u_char *); 473120f0783SPoul-Henning Kamp p = va_arg(ap, char *); 474120f0783SPoul-Henning Kamp if (!width) 475120f0783SPoul-Henning Kamp width = 16; 476120f0783SPoul-Henning Kamp while(width--) { 477120f0783SPoul-Henning Kamp PCHAR(hex2ascii(*up >> 4)); 478120f0783SPoul-Henning Kamp PCHAR(hex2ascii(*up & 0x0f)); 479120f0783SPoul-Henning Kamp up++; 480120f0783SPoul-Henning Kamp if (width) 481120f0783SPoul-Henning Kamp for (q=p;*q;q++) 482120f0783SPoul-Henning Kamp PCHAR(*q); 483120f0783SPoul-Henning Kamp } 484120f0783SPoul-Henning Kamp break; 485df8bae1dSRodney W. Grimes case 'd': 486df8bae1dSRodney W. Grimes ul = lflag ? va_arg(ap, long) : va_arg(ap, int); 487791d77e0SPoul-Henning Kamp sign = 1; 488df8bae1dSRodney W. Grimes base = 10; 489df8bae1dSRodney W. Grimes goto number; 490791d77e0SPoul-Henning Kamp case 'l': 491791d77e0SPoul-Henning Kamp lflag = 1; 492791d77e0SPoul-Henning Kamp goto reswitch; 493791d77e0SPoul-Henning Kamp case 'n': 494791d77e0SPoul-Henning Kamp ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 495791d77e0SPoul-Henning Kamp base = radix; 496791d77e0SPoul-Henning Kamp goto number; 497df8bae1dSRodney W. Grimes case 'o': 498df8bae1dSRodney W. Grimes ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 499df8bae1dSRodney W. Grimes base = 8; 500df8bae1dSRodney W. Grimes goto number; 50112d17f65SPoul-Henning Kamp case 'p': 50212d17f65SPoul-Henning Kamp ul = (u_long)va_arg(ap, void *); 50312d17f65SPoul-Henning Kamp base = 16; 504f82057beSBruce Evans sharpflag = 1; 50512d17f65SPoul-Henning Kamp goto number; 506791d77e0SPoul-Henning Kamp case 's': 507791d77e0SPoul-Henning Kamp p = va_arg(ap, char *); 508791d77e0SPoul-Henning Kamp if (p == NULL) 509791d77e0SPoul-Henning Kamp p = "(null)"; 5104830092aSPoul-Henning Kamp if (!dot) 5114830092aSPoul-Henning Kamp n = strlen (p); 5124830092aSPoul-Henning Kamp else 5134f20e4b1SPoul-Henning Kamp for (n = 0; n < dwidth && p[n]; n++) 5144830092aSPoul-Henning Kamp continue; 5154f20e4b1SPoul-Henning Kamp 5164830092aSPoul-Henning Kamp width -= n; 5174f20e4b1SPoul-Henning Kamp 518791d77e0SPoul-Henning Kamp if (!ladjust && width > 0) 519791d77e0SPoul-Henning Kamp while (width--) 520791d77e0SPoul-Henning Kamp PCHAR(padc); 5214830092aSPoul-Henning Kamp while (n--) 522791d77e0SPoul-Henning Kamp PCHAR(*p++); 523791d77e0SPoul-Henning Kamp if (ladjust && width > 0) 524791d77e0SPoul-Henning Kamp while (width--) 525791d77e0SPoul-Henning Kamp PCHAR(padc); 526791d77e0SPoul-Henning Kamp break; 527f53dbe97SBruce Evans case 'u': 528f53dbe97SBruce Evans ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 529f53dbe97SBruce Evans base = 10; 530f53dbe97SBruce Evans goto number; 531df8bae1dSRodney W. Grimes case 'x': 532df8bae1dSRodney W. Grimes ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int); 533df8bae1dSRodney W. Grimes base = 16; 534791d77e0SPoul-Henning Kamp number: if (sign && (long)ul < 0L) { 535791d77e0SPoul-Henning Kamp neg = 1; 536791d77e0SPoul-Henning Kamp ul = -(long)ul; 537791d77e0SPoul-Henning Kamp } 538791d77e0SPoul-Henning Kamp p = ksprintn(ul, base, &tmp); 539791d77e0SPoul-Henning Kamp if (sharpflag && ul != 0) { 540791d77e0SPoul-Henning Kamp if (base == 8) 541791d77e0SPoul-Henning Kamp tmp++; 542791d77e0SPoul-Henning Kamp else if (base == 16) 543791d77e0SPoul-Henning Kamp tmp += 2; 544791d77e0SPoul-Henning Kamp } 545791d77e0SPoul-Henning Kamp if (neg) 546791d77e0SPoul-Henning Kamp tmp++; 547791d77e0SPoul-Henning Kamp 548791d77e0SPoul-Henning Kamp if (!ladjust && width && (width -= tmp) > 0) 549df8bae1dSRodney W. Grimes while (width--) 550791d77e0SPoul-Henning Kamp PCHAR(padc); 551791d77e0SPoul-Henning Kamp if (neg) 552791d77e0SPoul-Henning Kamp PCHAR('-'); 553791d77e0SPoul-Henning Kamp if (sharpflag && ul != 0) { 554791d77e0SPoul-Henning Kamp if (base == 8) { 555791d77e0SPoul-Henning Kamp PCHAR('0'); 556791d77e0SPoul-Henning Kamp } else if (base == 16) { 557791d77e0SPoul-Henning Kamp PCHAR('0'); 558791d77e0SPoul-Henning Kamp PCHAR('x'); 559791d77e0SPoul-Henning Kamp } 560791d77e0SPoul-Henning Kamp } 561791d77e0SPoul-Henning Kamp 562797f2d22SPoul-Henning Kamp while (*p) 563791d77e0SPoul-Henning Kamp PCHAR(*p--); 564791d77e0SPoul-Henning Kamp 565791d77e0SPoul-Henning Kamp if (ladjust && width && (width -= tmp) > 0) 566791d77e0SPoul-Henning Kamp while (width--) 567791d77e0SPoul-Henning Kamp PCHAR(padc); 568791d77e0SPoul-Henning Kamp 569df8bae1dSRodney W. Grimes break; 570df8bae1dSRodney W. Grimes default: 571791d77e0SPoul-Henning Kamp PCHAR('%'); 572df8bae1dSRodney W. Grimes if (lflag) 573791d77e0SPoul-Henning Kamp PCHAR('l'); 574791d77e0SPoul-Henning Kamp PCHAR(ch); 575791d77e0SPoul-Henning Kamp break; 576df8bae1dSRodney W. Grimes } 577df8bae1dSRodney W. Grimes } 578791d77e0SPoul-Henning Kamp #undef PCHAR 579df8bae1dSRodney W. Grimes } 580df8bae1dSRodney W. Grimes 581df8bae1dSRodney W. Grimes /* 582791d77e0SPoul-Henning Kamp * Put character in log buffer. 583df8bae1dSRodney W. Grimes */ 584df8bae1dSRodney W. Grimes static void 585791d77e0SPoul-Henning Kamp msglogchar(int c, void *dummyarg) 586df8bae1dSRodney W. Grimes { 587791d77e0SPoul-Henning Kamp struct msgbuf *mbp; 588df8bae1dSRodney W. Grimes 589791d77e0SPoul-Henning Kamp if (c != '\0' && c != '\r' && c != 0177 && msgbufmapped) { 590df8bae1dSRodney W. Grimes mbp = msgbufp; 59158067a99SPoul-Henning Kamp mbp->msg_ptr[mbp->msg_bufx++] = c; 59258067a99SPoul-Henning Kamp if (mbp->msg_bufx >= mbp->msg_size) 593df8bae1dSRodney W. Grimes mbp->msg_bufx = 0; 59402d5c7b1SDavid Greenman /* If the buffer is full, keep the most recent data. */ 595a3f4faceSBruce Evans if (mbp->msg_bufr == mbp->msg_bufx) { 59658067a99SPoul-Henning Kamp if (++mbp->msg_bufr >= mbp->msg_size) 597a3f4faceSBruce Evans mbp->msg_bufr = 0; 598a3f4faceSBruce Evans } 599df8bae1dSRodney W. Grimes } 600df8bae1dSRodney W. Grimes } 601e796e00dSPoul-Henning Kamp 602e796e00dSPoul-Henning Kamp void 603e796e00dSPoul-Henning Kamp msgbufinit(void *ptr, size_t size) 604e796e00dSPoul-Henning Kamp { 605e796e00dSPoul-Henning Kamp char *cp; 606e796e00dSPoul-Henning Kamp 607e796e00dSPoul-Henning Kamp cp = (char *)ptr; 608e796e00dSPoul-Henning Kamp msgbufp = (struct msgbuf *) (cp + size - sizeof(*msgbufp)); 609e796e00dSPoul-Henning Kamp if (msgbufp->msg_magic != MSG_MAGIC || msgbufp->msg_ptr != cp) { 610e796e00dSPoul-Henning Kamp bzero(cp, size); 611e796e00dSPoul-Henning Kamp msgbufp->msg_magic = MSG_MAGIC; 612e796e00dSPoul-Henning Kamp msgbufp->msg_size = (char *)msgbufp - cp; 613e796e00dSPoul-Henning Kamp msgbufp->msg_ptr = cp; 614e796e00dSPoul-Henning Kamp } 615e796e00dSPoul-Henning Kamp msgbufmapped = 1; 616e796e00dSPoul-Henning Kamp } 617e796e00dSPoul-Henning Kamp 618e796e00dSPoul-Henning Kamp #include "opt_ddb.h" 619e796e00dSPoul-Henning Kamp #ifdef DDB 620e796e00dSPoul-Henning Kamp #include <ddb/ddb.h> 621e796e00dSPoul-Henning Kamp 622e796e00dSPoul-Henning Kamp DB_SHOW_COMMAND(msgbuf, db_show_msgbuf) 623e796e00dSPoul-Henning Kamp { 624e796e00dSPoul-Henning Kamp int i, j; 625e796e00dSPoul-Henning Kamp 626e796e00dSPoul-Henning Kamp if (!msgbufmapped) { 627e796e00dSPoul-Henning Kamp db_printf("msgbuf not mapped yet\n"); 628e796e00dSPoul-Henning Kamp return; 629e796e00dSPoul-Henning Kamp } 630e796e00dSPoul-Henning Kamp db_printf("msgbufp = %p\n", msgbufp); 631e796e00dSPoul-Henning Kamp db_printf("magic = %x, size = %d, r= %d, w = %d, ptr = %p\n", 632e796e00dSPoul-Henning Kamp msgbufp->msg_magic, msgbufp->msg_size, msgbufp->msg_bufr, 633e796e00dSPoul-Henning Kamp msgbufp->msg_bufx, msgbufp->msg_ptr); 634e796e00dSPoul-Henning Kamp for (i = 0; i < msgbufp->msg_size; i++) { 635e796e00dSPoul-Henning Kamp j = (i + msgbufp->msg_bufr) % msgbufp->msg_size; 636e796e00dSPoul-Henning Kamp db_printf("%c", msgbufp->msg_ptr[j]); 637e796e00dSPoul-Henning Kamp } 638e796e00dSPoul-Henning Kamp db_printf("\n"); 639e796e00dSPoul-Henning Kamp } 640e796e00dSPoul-Henning Kamp 641e796e00dSPoul-Henning Kamp #endif /* DDB */ 642