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