Home | History | Annotate | Line # | Download | only in kern
subr_prf.c revision 1.27
      1 /*	$NetBSD: subr_prf.c,v 1.27 1996/09/25 21:03:45 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 
     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 #ifdef KADB
     67 #include <machine/kdbparam.h>
     68 #endif
     69 #ifdef KGDB
     70 #include <machine/cpu.h>
     71 #endif
     72 
     73 #define TOCONS	0x01
     74 #define TOTTY	0x02
     75 #define TOLOG	0x04
     76 
     77 struct	tty *constty;			/* pointer to console "window" tty */
     78 
     79 void	(*v_putc) __P((int)) = cnputc;	/* routine to putc on virtual console */
     80 
     81 static void putchar __P((int, int, struct tty *));
     82 static char *ksprintn __P((u_long, int, int *));
     83 void kprintf __P((const char *, int, struct tty *, va_list));
     84 
     85 int consintr = 1;			/* Ok to handle console interrupts? */
     86 
     87 /*
     88  * Variable panicstr contains argument to first call to panic; used as flag
     89  * to indicate that the kernel has already called panic.
     90  */
     91 const char *panicstr;
     92 
     93 /*
     94  * Panic is called on unresolvable fatal errors.  It prints "panic: mesg",
     95  * and then reboots.  If we are called twice, then we avoid trying to sync
     96  * the disks as this often leads to recursive panics.
     97  */
     98 void
     99 #ifdef __STDC__
    100 panic(const char *fmt, ...)
    101 #else
    102 panic(fmt, va_alist)
    103 	char *fmt;
    104 	va_dcl
    105 #endif
    106 {
    107 	int bootopt;
    108 	va_list ap;
    109 
    110 	bootopt = RB_AUTOBOOT | RB_DUMP;
    111 	if (panicstr)
    112 		bootopt |= RB_NOSYNC;
    113 	else
    114 		panicstr = fmt;
    115 
    116 	va_start(ap, fmt);
    117 	printf("panic: %:\n", fmt, ap);
    118 	va_end(ap);
    119 
    120 #ifdef KGDB
    121 	kgdb_panic();
    122 #endif
    123 #ifdef KADB
    124 	if (boothowto & RB_KDB)
    125 		kdbpanic();
    126 #endif
    127 #ifdef DDB
    128 	Debugger();
    129 #endif
    130 	boot(bootopt, NULL);
    131 }
    132 
    133 /*
    134  * Warn that a system table is full.
    135  */
    136 void
    137 tablefull(tab)
    138 	const char *tab;
    139 {
    140 
    141 	log(LOG_ERR, "%s: table is full\n", tab);
    142 }
    143 
    144 /*
    145  * Uprintf prints to the controlling terminal for the current process.
    146  * It may block if the tty queue is overfull.  No message is printed if
    147  * the queue does not clear in a reasonable time.
    148  */
    149 void
    150 #ifdef __STDC__
    151 uprintf(const char *fmt, ...)
    152 #else
    153 uprintf(fmt, va_alist)
    154 	char *fmt;
    155 	va_dcl
    156 #endif
    157 {
    158 	register struct proc *p = curproc;
    159 	va_list ap;
    160 
    161 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
    162 		va_start(ap, fmt);
    163 		kprintf(fmt, TOTTY, p->p_session->s_ttyp, ap);
    164 		va_end(ap);
    165 	}
    166 }
    167 
    168 tpr_t
    169 tprintf_open(p)
    170 	register struct proc *p;
    171 {
    172 
    173 	if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
    174 		SESSHOLD(p->p_session);
    175 		return ((tpr_t) p->p_session);
    176 	}
    177 	return ((tpr_t) NULL);
    178 }
    179 
    180 void
    181 tprintf_close(sess)
    182 	tpr_t sess;
    183 {
    184 
    185 	if (sess)
    186 		SESSRELE((struct session *) sess);
    187 }
    188 
    189 /*
    190  * tprintf prints on the controlling terminal associated
    191  * with the given session.
    192  */
    193 void
    194 #ifdef __STDC__
    195 tprintf(tpr_t tpr, const char *fmt, ...)
    196 #else
    197 tprintf(tpr, fmt, va_alist)
    198 	tpr_t tpr;
    199 	char *fmt;
    200 	va_dcl
    201 #endif
    202 {
    203 	register struct session *sess = (struct session *)tpr;
    204 	struct tty *tp = NULL;
    205 	int flags = TOLOG;
    206 	va_list ap;
    207 
    208 	logpri(LOG_INFO);
    209 	if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
    210 		flags |= TOTTY;
    211 		tp = sess->s_ttyp;
    212 	}
    213 	va_start(ap, fmt);
    214 	kprintf(fmt, flags, tp, ap);
    215 	va_end(ap);
    216 	logwakeup();
    217 }
    218 
    219 /*
    220  * Ttyprintf displays a message on a tty; it should be used only by
    221  * the tty driver, or anything that knows the underlying tty will not
    222  * be revoke(2)'d away.  Other callers should use tprintf.
    223  */
    224 void
    225 #ifdef __STDC__
    226 ttyprintf(struct tty *tp, const char *fmt, ...)
    227 #else
    228 ttyprintf(tp, fmt, va_alist)
    229 	struct tty *tp;
    230 	char *fmt;
    231 	va_dcl
    232 #endif
    233 {
    234 	va_list ap;
    235 
    236 	va_start(ap, fmt);
    237 	kprintf(fmt, TOTTY, tp, ap);
    238 	va_end(ap);
    239 }
    240 
    241 extern	int log_open;
    242 
    243 /*
    244  * Log writes to the log buffer, and guarantees not to sleep (so can be
    245  * called by interrupt routines).  If there is no process reading the
    246  * log yet, it writes to the console also.
    247  */
    248 void
    249 #ifdef __STDC__
    250 log(int level, const char *fmt, ...)
    251 #else
    252 log(level, fmt, va_alist)
    253 	int level;
    254 	char *fmt;
    255 	va_dcl
    256 #endif
    257 {
    258 	register int s;
    259 	va_list ap;
    260 
    261 	s = splhigh();
    262 	logpri(level);
    263 	va_start(ap, fmt);
    264 	kprintf(fmt, TOLOG, NULL, ap);
    265 	splx(s);
    266 	va_end(ap);
    267 	if (!log_open) {
    268 		va_start(ap, fmt);
    269 		kprintf(fmt, TOCONS, NULL, ap);
    270 		va_end(ap);
    271 	}
    272 	logwakeup();
    273 }
    274 
    275 void
    276 logpri(level)
    277 	int level;
    278 {
    279 	register int ch;
    280 	register char *p;
    281 
    282 	putchar('<', TOLOG, NULL);
    283 	for (p = ksprintn((u_long)level, 10, NULL); (ch = *p--) != 0;)
    284 		putchar(ch, TOLOG, NULL);
    285 	putchar('>', TOLOG, NULL);
    286 }
    287 
    288 void
    289 #ifdef __STDC__
    290 addlog(const char *fmt, ...)
    291 #else
    292 addlog(fmt, va_alist)
    293 	char *fmt;
    294 	va_dcl
    295 #endif
    296 {
    297 	register int s;
    298 	va_list ap;
    299 
    300 	s = splhigh();
    301 	va_start(ap, fmt);
    302 	kprintf(fmt, TOLOG, NULL, ap);
    303 	splx(s);
    304 	va_end(ap);
    305 	if (!log_open) {
    306 		va_start(ap, fmt);
    307 		kprintf(fmt, TOCONS, NULL, ap);
    308 		va_end(ap);
    309 	}
    310 	logwakeup();
    311 }
    312 
    313 void
    314 #ifdef __STDC__
    315 printf(const char *fmt, ...)
    316 #else
    317 printf(fmt, va_alist)
    318 	char *fmt;
    319 	va_dcl
    320 #endif
    321 {
    322 	va_list ap;
    323 	register int savintr;
    324 
    325 	savintr = consintr;		/* disable interrupts */
    326 	consintr = 0;
    327 	va_start(ap, fmt);
    328 	kprintf(fmt, TOCONS | TOLOG, NULL, ap);
    329 	va_end(ap);
    330 	if (!panicstr)
    331 		logwakeup();
    332 	consintr = savintr;		/* reenable interrupts */
    333 }
    334 
    335 /*
    336  * Scaled down version of printf(3).
    337  *
    338  * Two additional formats:
    339  *
    340  * The format %b is supported to decode error registers.
    341  * Its usage is:
    342  *
    343  *	printf("reg=%b\n", regval, "<base><arg>*");
    344  *
    345  * where <base> is the output base expressed as a control character, e.g.
    346  * \10 gives octal; \20 gives hex.  Each arg is a sequence of characters,
    347  * the first of which gives the bit number to be inspected (origin 1), and
    348  * the next characters (up to a control character, i.e. a character <= 32),
    349  * give the name of the register.  Thus:
    350  *
    351  *	kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
    352  *
    353  * would produce output:
    354  *
    355  *	reg=3<BITTWO,BITONE>
    356  *
    357  * The format %: passes an additional format string and argument list
    358  * recursively.  Its usage is:
    359  *
    360  * fn(char *fmt, ...)
    361  * {
    362  *	va_list ap;
    363  *	va_start(ap, fmt);
    364  *	printf("prefix: %: suffix\n", fmt, ap);
    365  *	va_end(ap);
    366  * }
    367  *
    368  * Space or zero padding and a field width are supported for the numeric
    369  * formats only.
    370  */
    371 void
    372 kprintf(fmt, flags, tp, ap)
    373 	register const char *fmt;
    374 	int flags;
    375 	struct tty *tp;
    376 	va_list ap;
    377 {
    378 	register char *p, *q;
    379 	register int ch, n;
    380 	u_long ul;
    381 	int base, lflag, tmp, width;
    382 	char padc;
    383 
    384 	for (;;) {
    385 		padc = ' ';
    386 		width = 0;
    387 		while ((ch = *(u_char *)fmt++) != '%') {
    388 			if (ch == '\0')
    389 				return;
    390 			putchar(ch, flags, tp);
    391 		}
    392 		lflag = 0;
    393 reswitch:	switch (ch = *(u_char *)fmt++) {
    394 		case '0':
    395 		case '.':
    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