Home | History | Annotate | Line # | Download | only in kern
subr_prf.c revision 1.31
      1 /*	$NetBSD: subr_prf.c,v 1.31 1996/10/15 21:35:56 cgd Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1986, 1988, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  * (c) UNIX System Laboratories, Inc.
      7  * All or some portions of this file are derived from material licensed
      8  * to the University of California by American Telephone and Telegraph
      9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  * the permission of UNIX System Laboratories, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *	This product includes software developed by the University of
     23  *	California, Berkeley and its contributors.
     24  * 4. Neither the name of the University nor the names of its contributors
     25  *    may be used to endorse or promote products derived from this software
     26  *    without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  * SUCH DAMAGE.
     39  *
     40  *	@(#)subr_prf.c	8.3 (Berkeley) 1/21/94
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/buf.h>
     46 #include <sys/conf.h>
     47 #include <sys/reboot.h>
     48 #include <sys/msgbuf.h>
     49 #include <sys/proc.h>
     50 #include <sys/ioctl.h>
     51 #include <sys/vnode.h>
     52 #include <sys/file.h>
     53 #include <sys/tty.h>
     54 #include <sys/tprintf.h>
     55 #include <sys/syslog.h>
     56 #include <sys/malloc.h>
     57 
     58 #include <dev/cons.h>
     59 
     60 /*
     61  * Note that stdarg.h and the ANSI style va_start macro is used for both
     62  * ANSI and traditional C compilers.
     63  */
     64 #include <machine/stdarg.h>
     65 
     66 #include "kgdb.h"
     67 
     68 #ifdef KADB
     69 #include <machine/kdbparam.h>
     70 #endif
     71 #ifdef KGDB
     72 #include <machine/cpu.h>
     73 #endif
     74 
     75 #define TOCONS	0x01
     76 #define TOTTY	0x02
     77 #define TOLOG	0x04
     78 
     79 struct	tty *constty;			/* pointer to console "window" tty */
     80 
     81 void	(*v_putc) __P((int)) = cnputc;	/* routine to putc on virtual console */
     82 
     83 static void putchar __P((int, int, struct tty *));
     84 static char *ksprintn __P((u_long, int, int *));
     85 void kprintf __P((const char *, int, struct tty *, va_list));
     86 
     87 int consintr = 1;			/* Ok to handle console interrupts? */
     88 
     89 /*
     90  * Variable panicstr contains argument to first call to panic; used as flag
     91  * to indicate that the kernel has already called panic.
     92  */
     93 const char *panicstr;
     94 
     95 /*
     96  * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
     97  * and then reboots.  If we are called twice, then we avoid trying to sync
     98  * the disks as this often leads to recursive panics.
     99  */
    100 void
    101 #ifdef __STDC__
    102 panic(const char *fmt, ...)
    103 #else
    104 panic(fmt, va_alist)
    105 	char *fmt;
    106 	va_dcl
    107 #endif
    108 {
    109 	int bootopt;
    110 	va_list ap;
    111 
    112 	bootopt = RB_AUTOBOOT | RB_DUMP;
    113 	if (panicstr)
    114 		bootopt |= RB_NOSYNC;
    115 	else
    116 		panicstr = fmt;
    117 
    118 	va_start(ap, fmt);
    119 	printf("panic: %:\n", fmt, ap);
    120 	va_end(ap);
    121 
    122 #if NKGDB > 0
    123 	kgdb_panic();
    124 #endif
    125 #ifdef KGDB
    126 	kgdb_panic();
    127 #endif
    128 #ifdef KADB
    129 	if (boothowto & RB_KDB)
    130 		kdbpanic();
    131 #endif
    132 #ifdef DDB
    133 	Debugger();
    134 #endif
    135 	boot(bootopt, NULL);
    136 }
    137 
    138 /*
    139  * Warn that a system table is full.
    140  */
    141 void
    142 tablefull(tab)
    143 	const char *tab;
    144 {
    145 
    146 	log(LOG_ERR, "%s: table is full\n", tab);
    147 }
    148 
    149 /*
    150  * Uprintf prints to the controlling terminal for the current process.
    151  * It may block if the tty queue is overfull.  No message is printed if
    152  * the queue does not clear in a reasonable time.
    153  */
    154 void
    155 #ifdef __STDC__
    156 uprintf(const char *fmt, ...)
    157 #else
    158 uprintf(fmt, va_alist)
    159 	char *fmt;
    160 	va_dcl
    161 #endif
    162 {
    163 	register struct proc *p = curproc;
    164 	va_list ap;
    165 
    166 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
    167 		va_start(ap, fmt);
    168 		kprintf(fmt, TOTTY, p->p_session->s_ttyp, ap);
    169 		va_end(ap);
    170 	}
    171 }
    172 
    173 tpr_t
    174 tprintf_open(p)
    175 	register struct proc *p;
    176 {
    177 
    178 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
    179 		SESSHOLD(p->p_session);
    180 		return ((tpr_t) p->p_session);
    181 	}
    182 	return ((tpr_t) NULL);
    183 }
    184 
    185 void
    186 tprintf_close(sess)
    187 	tpr_t sess;
    188 {
    189 
    190 	if (sess)
    191 		SESSRELE((struct session *) sess);
    192 }
    193 
    194 /*
    195  * tprintf prints on the controlling terminal associated
    196  * with the given session.
    197  */
    198 void
    199 #ifdef __STDC__
    200 tprintf(tpr_t tpr, const char *fmt, ...)
    201 #else
    202 tprintf(tpr, fmt, va_alist)
    203 	tpr_t tpr;
    204 	char *fmt;
    205 	va_dcl
    206 #endif
    207 {
    208 	register struct session *sess = (struct session *)tpr;
    209 	struct tty *tp = NULL;
    210 	int flags = TOLOG;
    211 	va_list ap;
    212 
    213 	logpri(LOG_INFO);
    214 	if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
    215 		flags |= TOTTY;
    216 		tp = sess->s_ttyp;
    217 	}
    218 	va_start(ap, fmt);
    219 	kprintf(fmt, flags, tp, ap);
    220 	va_end(ap);
    221 	logwakeup();
    222 }
    223 
    224 /*
    225  * Ttyprintf displays a message on a tty; it should be used only by
    226  * the tty driver, or anything that knows the underlying tty will not
    227  * be revoke(2)'d away.  Other callers should use tprintf.
    228  */
    229 void
    230 #ifdef __STDC__
    231 ttyprintf(struct tty *tp, const char *fmt, ...)
    232 #else
    233 ttyprintf(tp, fmt, va_alist)
    234 	struct tty *tp;
    235 	char *fmt;
    236 	va_dcl
    237 #endif
    238 {
    239 	va_list ap;
    240 
    241 	va_start(ap, fmt);
    242 	kprintf(fmt, TOTTY, tp, ap);
    243 	va_end(ap);
    244 }
    245 
    246 extern	int log_open;
    247 
    248 /*
    249  * Log writes to the log buffer, and guarantees not to sleep (so can be
    250  * called by interrupt routines).  If there is no process reading the
    251  * log yet, it writes to the console also.
    252  */
    253 void
    254 #ifdef __STDC__
    255 log(int level, const char *fmt, ...)
    256 #else
    257 log(level, fmt, va_alist)
    258 	int level;
    259 	char *fmt;
    260 	va_dcl
    261 #endif
    262 {
    263 	register int s;
    264 	va_list ap;
    265 
    266 	s = splhigh();
    267 	logpri(level);
    268 	va_start(ap, fmt);
    269 	kprintf(fmt, TOLOG, NULL, ap);
    270 	splx(s);
    271 	va_end(ap);
    272 	if (!log_open) {
    273 		va_start(ap, fmt);
    274 		kprintf(fmt, TOCONS, NULL, ap);
    275 		va_end(ap);
    276 	}
    277 	logwakeup();
    278 }
    279 
    280 void
    281 logpri(level)
    282 	int level;
    283 {
    284 	register int ch;
    285 	register char *p;
    286 
    287 	putchar('<', TOLOG, NULL);
    288 	for (p = ksprintn((u_long)level, 10, NULL); (ch = *p--) != 0;)
    289 		putchar(ch, TOLOG, NULL);
    290 	putchar('>', TOLOG, NULL);
    291 }
    292 
    293 void
    294 #ifdef __STDC__
    295 addlog(const char *fmt, ...)
    296 #else
    297 addlog(fmt, va_alist)
    298 	char *fmt;
    299 	va_dcl
    300 #endif
    301 {
    302 	register int s;
    303 	va_list ap;
    304 
    305 	s = splhigh();
    306 	va_start(ap, fmt);
    307 	kprintf(fmt, TOLOG, NULL, ap);
    308 	splx(s);
    309 	va_end(ap);
    310 	if (!log_open) {
    311 		va_start(ap, fmt);
    312 		kprintf(fmt, TOCONS, NULL, ap);
    313 		va_end(ap);
    314 	}
    315 	logwakeup();
    316 }
    317 
    318 void
    319 #ifdef __STDC__
    320 printf(const char *fmt, ...)
    321 #else
    322 printf(fmt, va_alist)
    323 	char *fmt;
    324 	va_dcl
    325 #endif
    326 {
    327 	va_list ap;
    328 	register int savintr;
    329 
    330 	savintr = consintr;		/* disable interrupts */
    331 	consintr = 0;
    332 	va_start(ap, fmt);
    333 	kprintf(fmt, TOCONS | TOLOG, NULL, ap);
    334 	va_end(ap);
    335 	if (!panicstr)
    336 		logwakeup();
    337 	consintr = savintr;		/* reenable interrupts */
    338 }
    339 
    340 /*
    341  * Scaled down version of printf(3).
    342  *
    343  * Two additional formats:
    344  *
    345  * The format %b is supported to decode error registers.
    346  * Its usage is:
    347  *
    348  *	printf("reg=%b\n", regval, "<base><arg>*");
    349  *
    350  * where <base> is the output base expressed as a control character, e.g.
    351  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
    352  * the first of which gives the bit number to be inspected (origin 1), and
    353  * the next characters (up to a control character, i.e. a character <= 32),
    354  * give the name of the register.  Thus:
    355  *
    356  *	kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
    357  *
    358  * would produce output:
    359  *
    360  *	reg=3<BITTWO,BITONE>
    361  *
    362  * The format %: passes an additional format string and argument list
    363  * recursively.  Its usage is:
    364  *
    365  * fn(char *fmt, ...)
    366  * {
    367  *	va_list ap;
    368  *	va_start(ap, fmt);
    369  *	printf("prefix: %: suffix\n", fmt, ap);
    370  *	va_end(ap);
    371  * }
    372  *
    373  * Space or zero padding and a field width are supported for the numeric
    374  * formats only.
    375  */
    376 void
    377 kprintf(fmt, flags, tp, ap)
    378 	register const char *fmt;
    379 	int flags;
    380 	struct tty *tp;
    381 	va_list ap;
    382 {
    383 	register char *p, *q;
    384 	register int ch, n;
    385 	u_long ul;
    386 	int base, lflag, tmp, width;
    387 	char padc;
    388 
    389 	for (;;) {
    390 		padc = ' ';
    391 		width = 0;
    392 		while ((ch = *(u_char *)fmt++) != '%') {
    393 			if (ch == '\0')
    394 				return;
    395 			putchar(ch, flags, tp);
    396 		}
    397 		lflag = 0;
    398 reswitch:	switch (ch = *(u_char *)fmt++) {
    399 		case '0':
    400 		case '.':
    401 			padc = '0';
    402 			goto reswitch;
    403 		case '1': case '2': case '3': case '4':
    404 		case '5': case '6': case '7': case '8': case '9':
    405 			for (width = 0;; ++fmt) {
    406 				width = width * 10 + ch - '0';
    407 				ch = *fmt;
    408 				if (ch < '0' || ch > '9')
    409 					break;
    410 			}
    411 			goto reswitch;
    412 		case 'l':
    413 			lflag = 1;
    414 			goto reswitch;
    415 		case 'b':
    416 			ul = va_arg(ap, int);
    417 			p = va_arg(ap, char *);
    418 			for (q = ksprintn(ul, *p++, NULL); (ch = *q--) != 0;)
    419 				putchar(ch, flags, tp);
    420 
    421 			if (!ul)
    422 				break;
    423 
    424 			for (tmp = 0; (n = *p++) != 0;) {
    425 				if (ul & (1 << (n - 1))) {
    426 					putchar(tmp ? ',' : '<', flags, tp);
    427 					for (; (n = *p) > ' '; ++p)
    428 						putchar(n, flags, tp);
    429 					tmp = 1;
    430 				} else
    431 					for (; *p > ' '; ++p)
    432 						continue;
    433 			}
    434 			if (tmp)
    435 				putchar('>', flags, tp);
    436 			break;
    437 		case 'c':
    438 			putchar(va_arg(ap, int), flags, tp);
    439 			break;
    440 		case ':':
    441 			p = va_arg(ap, char *);
    442 			kprintf(p, flags, tp, va_arg(ap, va_list));
    443 			break;
    444 		case 's':
    445 			if ((p = va_arg(ap, char *)) == NULL)
    446 				p = "(null)";
    447 			while ((ch = *p++) != 0)
    448 				putchar(ch, flags, tp);
    449 			break;
    450 		case 'd':
    451 			ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
    452 			if ((long)ul < 0) {
    453 				putchar('-', flags, tp);
    454 				ul = -(long)ul;
    455 			}
    456 			base = 10;
    457 			goto number;
    458 		case 'o':
    459 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
    460 			base = 8;
    461 			goto number;
    462 		case 'u':
    463 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
    464 			base = 10;
    465 			goto number;
    466 		case 'p':
    467 			putchar('0', flags, tp);
    468 			putchar('x', flags, tp);
    469 			ul = (u_long)va_arg(ap, void *);
    470 			base = 16;
    471 			goto number;
    472 		case 'x':
    473 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
    474 			base = 16;
    475 number:			p = ksprintn(ul, base, &tmp);
    476 			if (width && (width -= tmp) > 0)
    477 				while (width--)
    478 					putchar(padc, flags, tp);
    479 			while ((ch = *p--) != 0)
    480 				putchar(ch, flags, tp);
    481 			break;
    482 		default:
    483 			putchar('%', flags, tp);
    484 			if (lflag)
    485 				putchar('l', flags, tp);
    486 			/* FALLTHROUGH */
    487 		case '%':
    488 			putchar(ch, flags, tp);
    489 		}
    490 	}
    491 }
    492 
    493 /*
    494  * Print a character on console or users terminal.  If destination is
    495  * the console then the last MSGBUFS characters are saved in msgbuf for
    496  * inspection later.
    497  */
    498 static void
    499 putchar(c, flags, tp)
    500 	register int c;
    501 	int flags;
    502 	struct tty *tp;
    503 {
    504 	extern int msgbufmapped;
    505 	register struct msgbuf *mbp;
    506 
    507 	if (panicstr)
    508 		constty = NULL;
    509 	if ((flags & TOCONS) && tp == NULL && constty) {
    510 		tp = constty;
    511 		flags |= TOTTY;
    512 	}
    513 	if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
    514 	    (flags & TOCONS) && tp == constty)
    515 		constty = NULL;
    516 	if ((flags & TOLOG) &&
    517 	    c != '\0' && c != '\r' && c != 0177 && msgbufmapped) {
    518 		mbp = msgbufp;
    519 		if (mbp->msg_magic != MSG_MAGIC) {
    520 			bzero((caddr_t)mbp, sizeof(*mbp));
    521 			mbp->msg_magic = MSG_MAGIC;
    522 		}
    523 		mbp->msg_bufc[mbp->msg_bufx++] = c;
    524 		if (mbp->msg_bufx < 0 || mbp->msg_bufx >= MSG_BSIZE)
    525 			mbp->msg_bufx = 0;
    526 	}
    527 	if ((flags & TOCONS) && constty == NULL && c != '\0')
    528 		(*v_putc)(c);
    529 }
    530 
    531 /*
    532  * Scaled down version of sprintf(3).
    533  */
    534 int
    535 #ifdef __STDC__
    536 sprintf(char *buf, const char *cfmt, ...)
    537 #else
    538 sprintf(buf, cfmt, va_alist)
    539 	char *buf,
    540 	const char *cfmt;
    541 	va_dcl
    542 #endif
    543 {
    544 	register const char *fmt = cfmt;
    545 	register char *p, *bp;
    546 	register int ch, base;
    547 	u_long ul;
    548 	int lflag, tmp, width;
    549 	va_list ap;
    550 	char padc;
    551 
    552 	va_start(ap, cfmt);
    553 	for (bp = buf; ; ) {
    554 		padc = ' ';
    555 		width = 0;
    556 		while ((ch = *(u_char *)fmt++) != '%')
    557 			if ((*bp++ = ch) == '\0')
    558 				return ((bp - buf) - 1);
    559 
    560 		lflag = 0;
    561 reswitch:	switch (ch = *(u_char *)fmt++) {
    562 		case '0':
    563 			padc = '0';
    564 			goto reswitch;
    565 		case '1': case '2': case '3': case '4':
    566 		case '5': case '6': case '7': case '8': case '9':
    567 			for (width = 0;; ++fmt) {
    568 				width = width * 10 + ch - '0';
    569 				ch = *fmt;
    570 				if (ch < '0' || ch > '9')
    571 					break;
    572 			}
    573 			goto reswitch;
    574 		case 'l':
    575 			lflag = 1;
    576 			goto reswitch;
    577 		/* case 'b': ... break; XXX */
    578 		case 'c':
    579 			*bp++ = va_arg(ap, int);
    580 			break;
    581 		/* case 'r': ... break; XXX */
    582 		case 's':
    583 			p = va_arg(ap, char *);
    584 			while ((*bp++ = *p++) != 0)
    585 				continue;
    586 			--bp;
    587 			break;
    588 		case 'd':
    589 			ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
    590 			if ((long)ul < 0) {
    591 				*bp++ = '-';
    592 				ul = -(long)ul;
    593 			}
    594 			base = 10;
    595 			goto number;
    596 			break;
    597 		case 'o':
    598 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
    599 			base = 8;
    600 			goto number;
    601 			break;
    602 		case 'u':
    603 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
    604 			base = 10;
    605 			goto number;
    606 			break;
    607 		case 'p':
    608 			*bp++ = '0';
    609 			*bp++ = 'x';
    610 			ul = (u_long)va_arg(ap, void *);
    611 			base = 16;
    612 			goto number;
    613 		case 'x':
    614 			ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
    615 			base = 16;
    616 number:			p = ksprintn(ul, base, &tmp);
    617 			if (width && (width -= tmp) > 0)
    618 				while (width--)
    619 					*bp++ = padc;
    620 			while ((ch = *p--) != 0)
    621 				*bp++ = ch;
    622 			break;
    623 		default:
    624 			*bp++ = '%';
    625 			if (lflag)
    626 				*bp++ = 'l';
    627 			/* FALLTHROUGH */
    628 		case '%':
    629 			*bp++ = ch;
    630 		}
    631 	}
    632 	va_end(ap);
    633 }
    634 
    635 /*
    636  * Put a number (base <= 16) in a buffer in reverse order; return an
    637  * optional length and a pointer to the NULL terminated (preceded?)
    638  * buffer.
    639  */
    640 static char *
    641 ksprintn(ul, base, lenp)
    642 	register u_long ul;
    643 	register int base, *lenp;
    644 {					/* A long in base 8, plus NULL. */
    645 	static char buf[sizeof(long) * NBBY / 3 + 2];
    646 	register char *p;
    647 
    648 	p = buf;
    649 	do {
    650 		*++p = "0123456789abcdef"[ul % base];
    651 	} while (ul /= base);
    652 	if (lenp)
    653 		*lenp = p - buf;
    654 	return (p);
    655 }
    656