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