subr_prf.c revision 1.22 1 /* $NetBSD: subr_prf.c,v 1.22 1996/03/14 19:01:11 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 #ifdef __GNUC__
99 volatile void boot(int flags); /* boot() does not return */
100 volatile /* panic() does not return */
101 #endif
102 void
103 #ifdef __STDC__
104 panic(const char *fmt, ...)
105 #else
106 panic(fmt, va_alist)
107 char *fmt;
108 va_dcl
109 #endif
110 {
111 int bootopt;
112 va_list ap;
113 static const char fm[] = "panic: %r\n";
114
115 bootopt = RB_AUTOBOOT | RB_DUMP;
116 if (panicstr)
117 bootopt |= RB_NOSYNC;
118 else
119 panicstr = fmt;
120
121 va_start(ap, fmt);
122 printf(fm, fmt, ap);
123 va_end(ap);
124
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);
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 %r 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: %r: 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 padc = '0';
401 goto reswitch;
402 case '1': case '2': case '3': case '4':
403 case '5': case '6': case '7': case '8': case '9':
404 for (width = 0;; ++fmt) {
405 width = width * 10 + ch - '0';
406 ch = *fmt;
407 if (ch < '0' || ch > '9')
408 break;
409 }
410 goto reswitch;
411 case 'l':
412 lflag = 1;
413 goto reswitch;
414 case 'b':
415 ul = va_arg(ap, int);
416 p = va_arg(ap, char *);
417 for (q = ksprintn(ul, *p++, NULL); (ch = *q--) != 0;)
418 putchar(ch, flags, tp);
419
420 if (!ul)
421 break;
422
423 for (tmp = 0; (n = *p++) != 0;) {
424 if (ul & (1 << (n - 1))) {
425 putchar(tmp ? ',' : '<', flags, tp);
426 for (; (n = *p) > ' '; ++p)
427 putchar(n, flags, tp);
428 tmp = 1;
429 } else
430 for (; *p > ' '; ++p)
431 continue;
432 }
433 if (tmp)
434 putchar('>', flags, tp);
435 break;
436 case 'c':
437 putchar(va_arg(ap, int), flags, tp);
438 break;
439 case 'r':
440 p = va_arg(ap, char *);
441 kprintf(p, flags, tp, va_arg(ap, va_list));
442 break;
443 case 's':
444 if ((p = va_arg(ap, char *)) == NULL)
445 p = "(null)";
446 while ((ch = *p++) != 0)
447 putchar(ch, flags, tp);
448 break;
449 case 'd':
450 ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
451 if ((long)ul < 0) {
452 putchar('-', flags, tp);
453 ul = -(long)ul;
454 }
455 base = 10;
456 goto number;
457 case 'o':
458 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
459 base = 8;
460 goto number;
461 case 'u':
462 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
463 base = 10;
464 goto number;
465 case 'p':
466 putchar('0', flags, tp);
467 putchar('x', flags, tp);
468 ul = (u_long)va_arg(ap, void *);
469 base = 16;
470 goto number;
471 case 'x':
472 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
473 base = 16;
474 number: p = ksprintn(ul, base, &tmp);
475 if (width && (width -= tmp) > 0)
476 while (width--)
477 putchar(padc, flags, tp);
478 while ((ch = *p--) != 0)
479 putchar(ch, flags, tp);
480 break;
481 default:
482 putchar('%', flags, tp);
483 if (lflag)
484 putchar('l', flags, tp);
485 /* FALLTHROUGH */
486 case '%':
487 putchar(ch, flags, tp);
488 }
489 }
490 }
491
492 /*
493 * Print a character on console or users terminal. If destination is
494 * the console then the last MSGBUFS characters are saved in msgbuf for
495 * inspection later.
496 */
497 static void
498 putchar(c, flags, tp)
499 register int c;
500 int flags;
501 struct tty *tp;
502 {
503 extern int msgbufmapped;
504 register struct msgbuf *mbp;
505
506 if (panicstr)
507 constty = NULL;
508 if ((flags & TOCONS) && tp == NULL && constty) {
509 tp = constty;
510 flags |= TOTTY;
511 }
512 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
513 (flags & TOCONS) && tp == constty)
514 constty = NULL;
515 if ((flags & TOLOG) &&
516 c != '\0' && c != '\r' && c != 0177 && msgbufmapped) {
517 mbp = msgbufp;
518 if (mbp->msg_magic != MSG_MAGIC) {
519 bzero((caddr_t)mbp, sizeof(*mbp));
520 mbp->msg_magic = MSG_MAGIC;
521 }
522 mbp->msg_bufc[mbp->msg_bufx++] = c;
523 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= MSG_BSIZE)
524 mbp->msg_bufx = 0;
525 }
526 if ((flags & TOCONS) && constty == NULL && c != '\0')
527 (*v_putc)(c);
528 }
529
530 /*
531 * Scaled down version of sprintf(3).
532 */
533 int
534 #ifdef __STDC__
535 sprintf(char *buf, const char *cfmt, ...)
536 #else
537 sprintf(buf, cfmt, va_alist)
538 char *buf, *cfmt;
539 va_dcl
540 #endif
541 {
542 register const char *fmt = cfmt;
543 register char *p, *bp;
544 register int ch, base;
545 u_long ul;
546 int lflag, tmp, width;
547 va_list ap;
548 char padc;
549
550 va_start(ap, cfmt);
551 for (bp = buf; ; ) {
552 padc = ' ';
553 width = 0;
554 while ((ch = *(u_char *)fmt++) != '%')
555 if ((*bp++ = ch) == '\0')
556 return ((bp - buf) - 1);
557
558 lflag = 0;
559 reswitch: switch (ch = *(u_char *)fmt++) {
560 case '0':
561 padc = '0';
562 goto reswitch;
563 case '1': case '2': case '3': case '4':
564 case '5': case '6': case '7': case '8': case '9':
565 for (width = 0;; ++fmt) {
566 width = width * 10 + ch - '0';
567 ch = *fmt;
568 if (ch < '0' || ch > '9')
569 break;
570 }
571 goto reswitch;
572 case 'l':
573 lflag = 1;
574 goto reswitch;
575 /* case 'b': ... break; XXX */
576 case 'c':
577 *bp++ = va_arg(ap, int);
578 break;
579 /* case 'r': ... break; XXX */
580 case 's':
581 p = va_arg(ap, char *);
582 while ((*bp++ = *p++) != 0)
583 continue;
584 --bp;
585 break;
586 case 'd':
587 ul = lflag ? va_arg(ap, long) : va_arg(ap, int);
588 if ((long)ul < 0) {
589 *bp++ = '-';
590 ul = -(long)ul;
591 }
592 base = 10;
593 goto number;
594 break;
595 case 'o':
596 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
597 base = 8;
598 goto number;
599 break;
600 case 'u':
601 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
602 base = 10;
603 goto number;
604 break;
605 case 'p':
606 *bp++ = '0';
607 *bp++ = 'x';
608 ul = (u_long)va_arg(ap, void *);
609 base = 16;
610 goto number;
611 case 'x':
612 ul = lflag ? va_arg(ap, u_long) : va_arg(ap, u_int);
613 base = 16;
614 number: p = ksprintn(ul, base, &tmp);
615 if (width && (width -= tmp) > 0)
616 while (width--)
617 *bp++ = padc;
618 while ((ch = *p--) != 0)
619 *bp++ = ch;
620 break;
621 default:
622 *bp++ = '%';
623 if (lflag)
624 *bp++ = 'l';
625 /* FALLTHROUGH */
626 case '%':
627 *bp++ = ch;
628 }
629 }
630 va_end(ap);
631 }
632
633 /*
634 * Put a number (base <= 16) in a buffer in reverse order; return an
635 * optional length and a pointer to the NULL terminated (preceded?)
636 * buffer.
637 */
638 static char *
639 ksprintn(ul, base, lenp)
640 register u_long ul;
641 register int base, *lenp;
642 { /* A long in base 8, plus NULL. */
643 static char buf[sizeof(long) * NBBY / 3 + 2];
644 register char *p;
645
646 p = buf;
647 do {
648 *++p = "0123456789abcdef"[ul % base];
649 } while (ul /= base);
650 if (lenp)
651 *lenp = p - buf;
652 return (p);
653 }
654