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