149062Sbostic /*- 249062Sbostic * Copyright (c) 1986, 1988, 1991 The Regents of the University of California. 349062Sbostic * All rights reserved. 423381Smckusick * 549062Sbostic * %sccs.include.redist.c% 649062Sbostic * 7*49094Sbostic * @(#)subr_prf.c 7.23 (Berkeley) 05/04/91 823381Smckusick */ 931Sbill 1017094Sbloom #include "param.h" 1117094Sbloom #include "systm.h" 1217094Sbloom #include "buf.h" 1317094Sbloom #include "conf.h" 1417094Sbloom #include "reboot.h" 1517094Sbloom #include "msgbuf.h" 1617094Sbloom #include "proc.h" 1717577Sbloom #include "ioctl.h" 1839560Smarc #include "vnode.h" 1939560Smarc #include "file.h" 2017094Sbloom #include "tty.h" 2144386Smarc #include "tprintf.h" 2218364Skarels #include "syslog.h" 2344386Smarc #include "malloc.h" 2431Sbill 2549062Sbostic /* 2649062Sbostic * Note that stdarg.h and the ANSI style va_start macro is used for both 2749062Sbostic * ANSI and traditional C compilers. 2849062Sbostic */ 2949062Sbostic #include <machine/stdarg.h> 3049062Sbostic 3135282Skarels #ifdef KADB 3237496Smckusick #include "machine/kdbparam.h" 3330625Skarels #endif 3430625Skarels 3549062Sbostic #define TOCONS 0x01 3649062Sbostic #define TOTTY 0x02 3749062Sbostic #define TOLOG 0x04 3816724Sralph 3930549Skarels struct tty *constty; /* pointer to console "window" tty */ 4030549Skarels 4140808Smarc #ifdef KADB 4240808Smarc extern cngetc(); /* standard console getc */ 4349062Sbostic int (*v_getc)() = cngetc; /* "" getc from virtual console */ 4440808Smarc extern cnpoll(); 4540808Smarc int (*v_poll)() = cnpoll; /* kdb hook to enable input polling */ 4640808Smarc #endif 4749062Sbostic extern cnputc(); /* standard console putc */ 4849062Sbostic int (*v_putc)() = cnputc; /* routine to putc on virtual console */ 4940808Smarc 50*49094Sbostic static void logpri __P((int level)); 51*49094Sbostic static void putchar __P((int ch, int flags, struct tty *tp)); 52*49094Sbostic static void kprintf __P((const char *fmt, int flags, struct tty *tp, va_list)); 53*49094Sbostic static void kprintn __P((u_long num, int base, int flags, struct tty *tp)); 5449062Sbostic 5531Sbill /* 5649062Sbostic * Variable panicstr contains argument to first call to panic; used 5749062Sbostic * as flag to indicate that the kernel has already called panic. 5831Sbill */ 5949062Sbostic char *panicstr; 6029946Skarels 6149062Sbostic /* 6249062Sbostic * Panic is called on unresolvable fatal errors. It prints "panic: mesg", 6349062Sbostic * and then reboots. If we are called twice, then we avoid trying to sync 6449062Sbostic * the disks as this often leads to recursive panics. 6549062Sbostic */ 6649062Sbostic void 6749062Sbostic panic(msg) 6849062Sbostic char *msg; 6949062Sbostic { 7049062Sbostic int bootopt = RB_AUTOBOOT | RB_DUMP; 7149062Sbostic int s; 7248426Skarels 7349062Sbostic if (panicstr) 7449062Sbostic bootopt |= RB_NOSYNC; 7549062Sbostic else 7649062Sbostic panicstr = msg; 7749062Sbostic printf("panic: %s\n", msg); 7849062Sbostic #ifdef KGDB 7949062Sbostic kgdb_panic(); 8049062Sbostic #endif 8149062Sbostic #ifdef KADB 8249062Sbostic if (boothowto & RB_KDB) { 8349062Sbostic s = splnet(); /* below kdb pri */ 8449062Sbostic setsoftkdb(); 8549062Sbostic splx(s); 8649062Sbostic } 8749062Sbostic #endif 8849062Sbostic boot(bootopt); 8949062Sbostic } 9049062Sbostic 9149062Sbostic /* 9249062Sbostic * Warn that a system table is full. 9349062Sbostic */ 9449062Sbostic void 9549062Sbostic tablefull(tab) 9649062Sbostic char *tab; 9731Sbill { 98285Sbill 9949062Sbostic log(LOG_ERR, "%s: table is full\n", tab); 100285Sbill } 101285Sbill 1022377Swnj /* 10339560Smarc * Uprintf prints to the controlling terminal for the current process. 10449062Sbostic * It may block if the tty queue is overfull. No message is printed if 10549062Sbostic * the queue does not clear in a reasonable time. 1062377Swnj */ 10749062Sbostic void 10849062Sbostic #ifdef __STDC__ 10949062Sbostic uprintf(const char *fmt, ...) 11049062Sbostic #else 11149062Sbostic uprintf(fmt /*, va_alist */) 1122781Swnj char *fmt; 11349062Sbostic #endif 114285Sbill { 11547540Skarels register struct proc *p = curproc; 11649062Sbostic va_list ap; 117285Sbill 11849062Sbostic va_start(ap, fmt); 11944386Smarc if (p->p_flag & SCTTY && p->p_session->s_ttyvp) 12049062Sbostic kprintf(fmt, TOTTY, p->p_session->s_ttyp, ap); 12149062Sbostic va_end(ap); 122285Sbill } 123285Sbill 12444386Smarc tpr_t 12548426Skarels tprintf_open(p) 12648426Skarels register struct proc *p; 12744386Smarc { 12844386Smarc if (p->p_flag & SCTTY && p->p_session->s_ttyvp) { 12944386Smarc SESSHOLD(p->p_session); 13048426Skarels return ((tpr_t) p->p_session); 13149062Sbostic } 13249062Sbostic return ((tpr_t) NULL); 13344386Smarc } 13444386Smarc 13548426Skarels void 13644386Smarc tprintf_close(sess) 13744386Smarc tpr_t sess; 13844386Smarc { 13944386Smarc if (sess) 14048426Skarels SESSRELE((struct session *) sess); 14144386Smarc } 14244386Smarc 14318364Skarels /* 14444386Smarc * tprintf prints on the controlling terminal associated 14549062Sbostic * with the given session. 14618364Skarels */ 14749062Sbostic void 14849062Sbostic #ifdef __STDC__ 14949062Sbostic tprintf(tpr_t tpr, const char *fmt, ...) 15049062Sbostic #else 15149062Sbostic tprintf(tpr, fmt /*, va_alist */) 15248426Skarels tpr_t tpr; 15316724Sralph char *fmt; 15449062Sbostic #endif 15516724Sralph { 15648426Skarels register struct session *sess = (struct session *)tpr; 15748426Skarels struct tty *tp = NULL; 15844386Smarc int flags = TOLOG; 15949062Sbostic va_list ap; 16016724Sralph 16125389Skarels logpri(LOG_INFO); 16248426Skarels if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { 16344386Smarc flags |= TOTTY; 16448426Skarels tp = sess->s_ttyp; 16548426Skarels } 16649062Sbostic va_start(ap, fmt); 16749062Sbostic kprintf(fmt, flags, tp, ap); 16849062Sbostic va_end(ap); 16925389Skarels logwakeup(); 17016724Sralph } 17116724Sralph 17249062Sbostic /* 17349062Sbostic * Ttyprintf displays a message on a tty; it should be used only by 17449062Sbostic * the tty driver, or anything that knows the underlying tty will not 17549062Sbostic * be revoke(2)'d away. Other callers should use tprintf. 17649062Sbostic */ 17749062Sbostic void 17849062Sbostic #ifdef __STDC__ 17949062Sbostic ttyprintf(struct tty *tp, const char *fmt, ...) 18049062Sbostic #else 18149062Sbostic ttyprintf(tp, fmt /*, va_alist */) 18249062Sbostic struct tty *tp; 18349062Sbostic char *fmt; 18449062Sbostic #endif 18549062Sbostic { 18649062Sbostic va_list ap; 18749062Sbostic 18849062Sbostic va_start(ap, fmt); 18949062Sbostic kprintf(fmt, TOTTY, tp, ap); 19049062Sbostic va_end(ap); 19149062Sbostic } 19249062Sbostic 19348426Skarels extern int log_open; 19444386Smarc 19516724Sralph /* 19649062Sbostic * Log writes to the log buffer, and guarantees not to sleep (so can be 19749062Sbostic * called by interrupt routines). If there is no process reading the 19849062Sbostic * log yet, it writes to the console also. 19916724Sralph */ 20049062Sbostic void 20149062Sbostic #ifdef __STDC__ 20249062Sbostic log(int level, const char *fmt, ...) 20349062Sbostic #else 20449062Sbostic log(level, fmt /*, va_alist */) 20549062Sbostic int level; 20616724Sralph char *fmt; 20749062Sbostic #endif 20816724Sralph { 20916724Sralph register s = splhigh(); 21049062Sbostic va_list ap; 21116724Sralph 21225389Skarels logpri(level); 21349062Sbostic va_start(ap, fmt); 21449062Sbostic kprintf(fmt, TOLOG, NULL, ap); 21516724Sralph splx(s); 21618364Skarels if (!log_open) 21749062Sbostic kprintf(fmt, TOCONS, NULL, ap); 21849062Sbostic va_end(ap); 21916724Sralph logwakeup(); 22016724Sralph } 22116724Sralph 22249062Sbostic static void 22325389Skarels logpri(level) 22425389Skarels int level; 22525389Skarels { 22625389Skarels 22749062Sbostic putchar('<', TOLOG, NULL); 22849062Sbostic kprintn((u_long)level, 10, TOLOG, NULL); 22949062Sbostic putchar('>', TOLOG, NULL); 23025389Skarels } 23125389Skarels 23249062Sbostic void 23349062Sbostic #ifdef __STDC__ 23449062Sbostic addlog(const char *fmt, ...) 23549062Sbostic #else 23649062Sbostic addlog(fmt /*, va_alist */) 23733479Skarels char *fmt; 23849062Sbostic #endif 23933479Skarels { 24033479Skarels register s = splhigh(); 24149062Sbostic va_list ap; 24233479Skarels 24349062Sbostic va_start(ap, fmt); 24449062Sbostic kprintf(fmt, TOLOG, NULL, ap); 24533479Skarels splx(s); 24633479Skarels if (!log_open) 24749062Sbostic kprintf(fmt, TOCONS, NULL, ap); 24849062Sbostic va_end(ap); 24933479Skarels logwakeup(); 25033479Skarels } 25133479Skarels 25249062Sbostic int consintr = 1; /* ok to handle console interrupts? */ 25331Sbill 25449062Sbostic void 25549062Sbostic #ifdef __STDC__ 25649062Sbostic printf(const char *fmt, ...) 25749062Sbostic #else 25849062Sbostic printf(fmt /*, va_alist */) 25949062Sbostic char *fmt; 26029946Skarels #endif 26149062Sbostic { 26249062Sbostic register int savintr; 26349062Sbostic va_list ap; 2642678Swnj 26549062Sbostic savintr = consintr; /* disable interrupts */ 26649062Sbostic consintr = 0; 26749062Sbostic va_start(ap, fmt); 26849062Sbostic kprintf(fmt, TOCONS | TOLOG, NULL, ap); 26949062Sbostic va_end(ap); 27049062Sbostic if (!panicstr) 27149062Sbostic logwakeup(); 27249062Sbostic consintr = savintr; /* reenable interrupts */ 27331Sbill } 27431Sbill 2752781Swnj /* 27649062Sbostic * Scaled down version of printf(3). 27749062Sbostic * 27849062Sbostic * Two additional formats: 27949062Sbostic * 28049062Sbostic * The format %b is supported to decode error registers. 28149062Sbostic * Its usage is: 28249062Sbostic * 28349062Sbostic * kprintf("reg=%b\n", regval, "<base><arg>*"); 28449062Sbostic * 28549062Sbostic * where <base> is the output base expressed as a control character, e.g. 28649062Sbostic * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 28749062Sbostic * the first of which gives the bit number to be inspected (origin 1), and 28849062Sbostic * the next characters (up to a control character, i.e. a character <= 32), 28949062Sbostic * give the name of the register. Thus: 29049062Sbostic * 29149062Sbostic * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 29249062Sbostic * 29349062Sbostic * would produce output: 29449062Sbostic * 29549062Sbostic * reg=3<BITTWO,BITONE> 29649062Sbostic * 29749062Sbostic * The format %r is supposed to pass an additional format string and argument 29849062Sbostic * list recursively. 29949062Sbostic * Its usage is: 30049062Sbostic * 30149062Sbostic * fn(otherstuff, fmt [, arg1, ... ]) 30249062Sbostic * char *fmt; 30349062Sbostic * u_int arg1, ...; 30449062Sbostic * 30549062Sbostic * kprintf("prefix: %r, other stuff\n", fmt, ap); 3062781Swnj */ 30749062Sbostic static void 308*49094Sbostic kprintf(fmt, flags, tp, ap) 309*49094Sbostic register const char *fmt; 31049062Sbostic int flags; 311*49094Sbostic struct tty *tp; 31249062Sbostic va_list ap; 31331Sbill { 31449062Sbostic register char *p; 31549062Sbostic register int ch, n; 31649062Sbostic unsigned long ul; 31749062Sbostic int lflag, set; 31831Sbill 31949062Sbostic for (;;) { 32049062Sbostic while ((ch = *fmt++) != '%') { 32149062Sbostic if (ch == '\0') 32249062Sbostic return; 323*49094Sbostic putchar(ch, flags, tp); 32429946Skarels } 32549062Sbostic lflag = 0; 32649062Sbostic reswitch: switch (ch = *fmt++) { 32749062Sbostic case 'l': 32849062Sbostic lflag = 1; 32949062Sbostic goto reswitch; 33049062Sbostic case 'b': 33149062Sbostic ul = va_arg(ap, int); 33249062Sbostic p = va_arg(ap, char *); 333*49094Sbostic kprintn(ul, *p++, flags, tp); 33431Sbill 33549062Sbostic if (!ul) 33649062Sbostic break; 3372377Swnj 33849062Sbostic for (set = 0; n = *p++;) { 33949062Sbostic if (ul & (1 << (n - 1))) { 340*49094Sbostic putchar(set ? ',' : '<', flags, tp); 34149062Sbostic for (; (n = *p) > ' '; ++p) 342*49094Sbostic putchar(n, flags, tp); 34349062Sbostic set = 1; 34449062Sbostic } else 34549062Sbostic for (; *p > ' '; ++p); 34649062Sbostic } 34749062Sbostic if (set) 348*49094Sbostic putchar('>', flags, tp); 34949062Sbostic break; 35049062Sbostic case 'c': 351*49094Sbostic putchar(va_arg(ap, int), flags, tp); 35249062Sbostic break; 35349062Sbostic case 'r': 35449062Sbostic p = va_arg(ap, char *); 355*49094Sbostic kprintf(p, flags, tp, va_arg(ap, va_list)); 35649062Sbostic break; 35749062Sbostic case 's': 35849062Sbostic p = va_arg(ap, char *); 35949062Sbostic while (ch = *p++) 360*49094Sbostic putchar(ch, flags, tp); 36149062Sbostic break; 36249062Sbostic case 'D': 36349062Sbostic lflag = 1; 36449062Sbostic /* FALLTHROUGH */ 36549062Sbostic case 'd': 36649062Sbostic ul = lflag ? 36749062Sbostic va_arg(ap, long) : va_arg(ap, int); 36849062Sbostic if ((long)ul < 0) { 369*49094Sbostic putchar('-', flags, tp); 37049062Sbostic ul = -(long)ul; 37149062Sbostic } 372*49094Sbostic kprintn(ul, 10, flags, tp); 37349062Sbostic break; 37449062Sbostic case 'O': 37549062Sbostic lflag = 1; 37649062Sbostic /* FALLTHROUGH */ 37749062Sbostic case 'o': 37849062Sbostic ul = lflag ? 37949062Sbostic va_arg(ap, u_long) : va_arg(ap, u_int); 380*49094Sbostic kprintn(ul, 8, flags, tp); 38149062Sbostic break; 38249062Sbostic case 'U': 38349062Sbostic lflag = 1; 38449062Sbostic /* FALLTHROUGH */ 38549062Sbostic case 'u': 38649062Sbostic ul = lflag ? 38749062Sbostic va_arg(ap, u_long) : va_arg(ap, u_int); 388*49094Sbostic kprintn(ul, 10, flags, tp); 38949062Sbostic break; 39049062Sbostic case 'X': 39149062Sbostic lflag = 1; 39249062Sbostic /* FALLTHROUGH */ 39349062Sbostic case 'x': 39449062Sbostic ul = lflag ? 39549062Sbostic va_arg(ap, u_long) : va_arg(ap, u_int); 396*49094Sbostic kprintn(ul, 16, flags, tp); 39749062Sbostic break; 39849062Sbostic default: 399*49094Sbostic putchar('%', flags, tp); 40049062Sbostic if (lflag) 401*49094Sbostic putchar('l', flags, tp); 402*49094Sbostic putchar(ch, flags, tp); 40349062Sbostic } 40430625Skarels } 40549062Sbostic va_end(ap); 40631Sbill } 40731Sbill 40849062Sbostic static void 409*49094Sbostic kprintn(ul, base, flags, tp) 41049062Sbostic u_long ul; 411*49094Sbostic int base, flags; 412*49094Sbostic struct tty *tp; 4132941Swnj { 41449062Sbostic /* hold a long in base 8 */ 41549062Sbostic char *p, buf[(sizeof(long) * NBBY >> 3) + 1]; 4162941Swnj 41749062Sbostic p = buf; 41849062Sbostic do { 41949062Sbostic *p++ = "0123456789abcdef"[ul % base]; 42049062Sbostic } while (ul /= base); 42149062Sbostic do { 422*49094Sbostic putchar(*--p, flags, tp); 42349062Sbostic } while (p > buf); 4242941Swnj } 4252941Swnj 4262941Swnj /* 42749062Sbostic * Print a character on console or users terminal. If destination is 42849062Sbostic * the console then the last MSGBUFS characters are saved in msgbuf for 42949062Sbostic * inspection later. 430285Sbill */ 43149062Sbostic static void 432*49094Sbostic putchar(c, flags, tp) 4332377Swnj register int c; 43449062Sbostic int flags; 435*49094Sbostic struct tty *tp; 436285Sbill { 43733479Skarels extern int msgbufmapped; 43849062Sbostic register struct msgbuf *mbp; 439285Sbill 44030549Skarels if (panicstr) 44149062Sbostic constty = NULL; 442*49094Sbostic if ((flags & TOCONS) && tp == NULL && constty) { 443*49094Sbostic tp = constty; 44430549Skarels flags |= TOTTY; 44530549Skarels } 446*49094Sbostic if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 && 447*49094Sbostic (flags & TOCONS) && tp == constty) 44849062Sbostic constty = NULL; 44949062Sbostic if ((flags & TOLOG) && 45049062Sbostic c != '\0' && c != '\r' && c != 0177 && msgbufmapped) { 45149062Sbostic mbp = msgbufp; 45245733Smckusick if (mbp->msg_magic != MSG_MAGIC) { 45312494Ssam register int i; 45412494Ssam 45545733Smckusick mbp->msg_magic = MSG_MAGIC; 45645733Smckusick mbp->msg_bufx = mbp->msg_bufr = 0; 45749062Sbostic for (i = 0; i < MSG_BSIZE; i++) 45845733Smckusick mbp->msg_bufc[i] = 0; 4592172Swnj } 46045733Smckusick mbp->msg_bufc[mbp->msg_bufx++] = c; 46145733Smckusick if (mbp->msg_bufx < 0 || mbp->msg_bufx >= MSG_BSIZE) 46245733Smckusick mbp->msg_bufx = 0; 463285Sbill } 46449062Sbostic if ((flags & TOCONS) && constty == NULL && c != '\0') 46530549Skarels (*v_putc)(c); 466285Sbill } 467