1 /* $NetBSD: subr_prf.c,v 1.73 2000/05/29 23:10:03 jhawk 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.4 (Berkeley) 5/4/95 41 */ 42 43 #include "opt_ddb.h" 44 #include "opt_ipkdb.h" 45 #include "opt_multiprocessor.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/buf.h> 50 #include <sys/reboot.h> 51 #include <sys/msgbuf.h> 52 #include <sys/proc.h> 53 #include <sys/ioctl.h> 54 #include <sys/vnode.h> 55 #include <sys/file.h> 56 #include <sys/tty.h> 57 #include <sys/tprintf.h> 58 #include <sys/syslog.h> 59 #include <sys/malloc.h> 60 #include <sys/lock.h> 61 62 #include <dev/cons.h> 63 64 #ifdef DDB 65 #include <ddb/ddbvar.h> 66 #include <machine/db_machdep.h> 67 #include <ddb/db_command.h> 68 #include <ddb/db_interface.h> 69 #endif 70 71 #ifdef IPKDB 72 #include <ipkdb/ipkdb.h> 73 #endif 74 75 #if defined(MULTIPROCESSOR) 76 struct simplelock kprintf_slock = SIMPLELOCK_INITIALIZER; 77 78 /* 79 * Use cpu_simple_lock() and cpu_simple_unlock(). These are the actual 80 * atomic locking operations, and never attempt to print debugging 81 * information. 82 */ 83 #define KPRINTF_MUTEX_ENTER(s) \ 84 do { \ 85 (s) = splhigh(); \ 86 __cpu_simple_lock(&kprintf_slock.lock_data); \ 87 } while (0) 88 89 #define KPRINTF_MUTEX_EXIT(s) \ 90 do { \ 91 __cpu_simple_unlock(&kprintf_slock.lock_data); \ 92 splx((s)); \ 93 } while (0) 94 #else /* ! MULTIPROCESSOR */ 95 #define KPRINTF_MUTEX_ENTER(s) (s) = splhigh() 96 #define KPRINTF_MUTEX_EXIT(s) splx((s)) 97 #endif /* MULTIPROCESSOR */ 98 99 /* 100 * note that stdarg.h and the ansi style va_start macro is used for both 101 * ansi and traditional c complers. 102 * XXX: this requires that stdarg.h define: va_alist and va_dcl 103 */ 104 #include <machine/stdarg.h> 105 106 107 #ifdef KGDB 108 #include <sys/kgdb.h> 109 #include <machine/cpu.h> 110 #endif 111 #ifdef DDB 112 #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */ 113 #endif 114 115 116 /* 117 * defines 118 */ 119 120 /* flags for kprintf */ 121 #define TOCONS 0x01 /* to the console */ 122 #define TOTTY 0x02 /* to the process' tty */ 123 #define TOLOG 0x04 /* to the kernel message buffer */ 124 #define TOBUFONLY 0x08 /* to the buffer (only) [for sprintf] */ 125 #define TODDB 0x10 /* to ddb console */ 126 127 /* max size buffer kprintf needs to print quad_t [size in base 8 + \0] */ 128 #define KPRINTF_BUFSIZE (sizeof(quad_t) * NBBY / 3 + 2) 129 130 131 /* 132 * local prototypes 133 */ 134 135 static int kprintf __P((const char *, int, void *, 136 char *, va_list)); 137 static void putchar __P((int, int, struct tty *)); 138 static void klogpri __P((int)); 139 140 141 /* 142 * globals 143 */ 144 145 struct tty *constty; /* pointer to console "window" tty */ 146 extern int log_open; /* subr_log: is /dev/klog open? */ 147 const char *panicstr; /* arg to first call to panic (used as a flag 148 to indicate that panic has already been called). */ 149 150 /* 151 * v_putc: routine to putc on virtual console 152 * 153 * the v_putc pointer can be used to redirect the console cnputc elsewhere 154 * [e.g. to a "virtual console"]. 155 */ 156 157 void (*v_putc) __P((int)) = cnputc; /* start with cnputc (normal cons) */ 158 159 160 /* 161 * functions 162 */ 163 164 /* 165 * tablefull: warn that a system table is full 166 */ 167 168 void 169 tablefull(tab) 170 const char *tab; 171 { 172 log(LOG_ERR, "%s: table is full\n", tab); 173 } 174 175 /* 176 * panic: handle an unresolvable fatal error 177 * 178 * prints "panic: <message>" and reboots. if called twice (i.e. recursive 179 * call) we avoid trying to sync the disk and just reboot (to avoid 180 * recursive panics). 181 */ 182 183 void 184 #ifdef __STDC__ 185 panic(const char *fmt, ...) 186 #else 187 panic(fmt, va_alist) 188 char *fmt; 189 va_dcl 190 #endif 191 { 192 int bootopt; 193 va_list ap; 194 195 bootopt = RB_AUTOBOOT | RB_DUMP; 196 if (panicstr) 197 bootopt |= RB_NOSYNC; 198 else 199 panicstr = fmt; 200 201 va_start(ap, fmt); 202 printf("panic: "); 203 vprintf(fmt, ap); 204 printf("\n"); 205 va_end(ap); 206 207 #ifdef IPKDB 208 ipkdb_panic(); 209 #endif 210 #ifdef KGDB 211 kgdb_panic(); 212 #endif 213 #ifdef KADB 214 if (boothowto & RB_KDB) 215 kdbpanic(); 216 #endif 217 #ifdef DDB 218 if (db_onpanic) 219 Debugger(); 220 else { 221 static int intrace = 0; 222 223 if (intrace==0) { 224 intrace=1; 225 printf("Begin traceback...\n"); 226 db_stack_trace_print( 227 (db_expr_t)__builtin_frame_address(0), 228 TRUE, 65535, "", printf); 229 printf("End traceback...\n"); 230 intrace=0; 231 } else 232 printf("Faulted in mid-traceback; aborting..."); 233 } 234 #endif 235 cpu_reboot(bootopt, NULL); 236 } 237 238 /* 239 * kernel logging functions: log, logpri, addlog 240 */ 241 242 /* 243 * log: write to the log buffer 244 * 245 * => will not sleep [so safe to call from interrupt] 246 * => will log to console if /dev/klog isn't open 247 */ 248 249 void 250 #ifdef __STDC__ 251 log(int level, const char *fmt, ...) 252 #else 253 log(level, fmt, va_alist) 254 int level; 255 char *fmt; 256 va_dcl 257 #endif 258 { 259 int s; 260 va_list ap; 261 262 KPRINTF_MUTEX_ENTER(s); 263 264 klogpri(level); /* log the level first */ 265 va_start(ap, fmt); 266 kprintf(fmt, TOLOG, NULL, NULL, ap); 267 va_end(ap); 268 if (!log_open) { 269 va_start(ap, fmt); 270 kprintf(fmt, TOCONS, NULL, NULL, ap); 271 va_end(ap); 272 } 273 274 KPRINTF_MUTEX_EXIT(s); 275 276 logwakeup(); /* wake up anyone waiting for log msgs */ 277 } 278 279 /* 280 * vlog: write to the log buffer [already have va_alist] 281 */ 282 283 void 284 vlog(level, fmt, ap) 285 int level; 286 const char *fmt; 287 va_list ap; 288 { 289 int s; 290 291 KPRINTF_MUTEX_ENTER(s); 292 293 klogpri(level); /* log the level first */ 294 kprintf(fmt, TOLOG, NULL, NULL, ap); 295 if (!log_open) 296 kprintf(fmt, TOCONS, NULL, NULL, ap); 297 298 KPRINTF_MUTEX_EXIT(s); 299 300 logwakeup(); /* wake up anyone waiting for log msgs */ 301 } 302 303 /* 304 * logpri: log the priority level to the klog 305 */ 306 307 void 308 logpri(level) 309 int level; 310 { 311 int s; 312 313 KPRINTF_MUTEX_ENTER(s); 314 klogpri(level); 315 KPRINTF_MUTEX_EXIT(s); 316 } 317 318 /* 319 * Note: we must be in the mutex here! 320 */ 321 static void 322 klogpri(level) 323 int level; 324 { 325 char *p; 326 char snbuf[KPRINTF_BUFSIZE]; 327 328 putchar('<', TOLOG, NULL); 329 sprintf(snbuf, "%d", level); 330 for (p = snbuf ; *p ; p++) 331 putchar(*p, TOLOG, NULL); 332 putchar('>', TOLOG, NULL); 333 } 334 335 /* 336 * addlog: add info to previous log message 337 */ 338 339 void 340 #ifdef __STDC__ 341 addlog(const char *fmt, ...) 342 #else 343 addlog(fmt, va_alist) 344 char *fmt; 345 va_dcl 346 #endif 347 { 348 int s; 349 va_list ap; 350 351 KPRINTF_MUTEX_ENTER(s); 352 353 va_start(ap, fmt); 354 kprintf(fmt, TOLOG, NULL, NULL, ap); 355 va_end(ap); 356 if (!log_open) { 357 va_start(ap, fmt); 358 kprintf(fmt, TOCONS, NULL, NULL, ap); 359 va_end(ap); 360 } 361 362 KPRINTF_MUTEX_EXIT(s); 363 364 logwakeup(); 365 } 366 367 368 /* 369 * putchar: print a single character on console or user terminal. 370 * 371 * => if console, then the last MSGBUFS chars are saved in msgbuf 372 * for inspection later (e.g. dmesg/syslog) 373 * => we must already be in the mutex! 374 */ 375 static void 376 putchar(c, flags, tp) 377 int c; 378 int flags; 379 struct tty *tp; 380 { 381 struct kern_msgbuf *mbp; 382 383 if (panicstr) 384 constty = NULL; 385 if ((flags & TOCONS) && tp == NULL && constty) { 386 tp = constty; 387 flags |= TOTTY; 388 } 389 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 && 390 (flags & TOCONS) && tp == constty) 391 constty = NULL; 392 if ((flags & TOLOG) && 393 c != '\0' && c != '\r' && c != 0177 && msgbufenabled) { 394 mbp = msgbufp; 395 if (mbp->msg_magic != MSG_MAGIC) { 396 /* 397 * Arguably should panic or somehow notify the 398 * user... but how? Panic may be too drastic, 399 * and would obliterate the message being kicked 400 * out (maybe a panic itself), and printf 401 * would invoke us recursively. Silently punt 402 * for now. If syslog is running, it should 403 * notice. 404 */ 405 msgbufenabled = 0; 406 } else { 407 mbp->msg_bufc[mbp->msg_bufx++] = c; 408 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs) 409 mbp->msg_bufx = 0; 410 /* If the buffer is full, keep the most recent data. */ 411 if (mbp->msg_bufr == mbp->msg_bufx) { 412 if (++mbp->msg_bufr >= mbp->msg_bufs) 413 mbp->msg_bufr = 0; 414 } 415 } 416 } 417 if ((flags & TOCONS) && constty == NULL && c != '\0') 418 (*v_putc)(c); 419 #ifdef DDB 420 if (flags & TODDB) 421 db_putchar(c); 422 #endif 423 } 424 425 426 /* 427 * uprintf: print to the controlling tty of the current process 428 * 429 * => we may block if the tty queue is full 430 * => no message is printed if the queue doesn't clear in a reasonable 431 * time 432 */ 433 434 void 435 #ifdef __STDC__ 436 uprintf(const char *fmt, ...) 437 #else 438 uprintf(fmt, va_alist) 439 char *fmt; 440 va_dcl 441 #endif 442 { 443 struct proc *p = curproc; 444 va_list ap; 445 446 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { 447 /* No mutex needed; going to process TTY. */ 448 va_start(ap, fmt); 449 kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap); 450 va_end(ap); 451 } 452 } 453 454 /* 455 * tprintf functions: used to send messages to a specific process 456 * 457 * usage: 458 * get a tpr_t handle on a process "p" by using "tprintf_open(p)" 459 * use the handle when calling "tprintf" 460 * when done, do a "tprintf_close" to drop the handle 461 */ 462 463 /* 464 * tprintf_open: get a tprintf handle on a process "p" 465 * 466 * => returns NULL if process can't be printed to 467 */ 468 469 tpr_t 470 tprintf_open(p) 471 struct proc *p; 472 { 473 474 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) { 475 SESSHOLD(p->p_session); 476 return ((tpr_t) p->p_session); 477 } 478 return ((tpr_t) NULL); 479 } 480 481 /* 482 * tprintf_close: dispose of a tprintf handle obtained with tprintf_open 483 */ 484 485 void 486 tprintf_close(sess) 487 tpr_t sess; 488 { 489 490 if (sess) 491 SESSRELE((struct session *) sess); 492 } 493 494 /* 495 * tprintf: given tprintf handle to a process [obtained with tprintf_open], 496 * send a message to the controlling tty for that process. 497 * 498 * => also sends message to /dev/klog 499 */ 500 void 501 #ifdef __STDC__ 502 tprintf(tpr_t tpr, const char *fmt, ...) 503 #else 504 tprintf(tpr, fmt, va_alist) 505 tpr_t tpr; 506 char *fmt; 507 va_dcl 508 #endif 509 { 510 struct session *sess = (struct session *)tpr; 511 struct tty *tp = NULL; 512 int s, flags = TOLOG; 513 va_list ap; 514 515 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { 516 flags |= TOTTY; 517 tp = sess->s_ttyp; 518 } 519 520 KPRINTF_MUTEX_ENTER(s); 521 522 klogpri(LOG_INFO); 523 va_start(ap, fmt); 524 kprintf(fmt, flags, tp, NULL, ap); 525 va_end(ap); 526 527 KPRINTF_MUTEX_EXIT(s); 528 529 logwakeup(); 530 } 531 532 533 /* 534 * ttyprintf: send a message to a specific tty 535 * 536 * => should be used only by tty driver or anything that knows the 537 * underlying tty will not be revoked(2)'d away. [otherwise, 538 * use tprintf] 539 */ 540 void 541 #ifdef __STDC__ 542 ttyprintf(struct tty *tp, const char *fmt, ...) 543 #else 544 ttyprintf(tp, fmt, va_alist) 545 struct tty *tp; 546 char *fmt; 547 va_dcl 548 #endif 549 { 550 va_list ap; 551 552 /* No mutex needed; going to process TTY. */ 553 va_start(ap, fmt); 554 kprintf(fmt, TOTTY, tp, NULL, ap); 555 va_end(ap); 556 } 557 558 #ifdef DDB 559 560 /* 561 * db_printf: printf for DDB (via db_putchar) 562 */ 563 564 void 565 #ifdef __STDC__ 566 db_printf(const char *fmt, ...) 567 #else 568 db_printf(fmt, va_alist) 569 char *fmt; 570 va_dcl 571 #endif 572 { 573 va_list ap; 574 575 /* No mutex needed; DDB pauses all processors. */ 576 va_start(ap, fmt); 577 kprintf(fmt, TODDB, NULL, NULL, ap); 578 va_end(ap); 579 } 580 581 #endif /* DDB */ 582 583 584 /* 585 * normal kernel printf functions: printf, vprintf, sprintf 586 */ 587 588 /* 589 * printf: print a message to the console and the log 590 */ 591 void 592 #ifdef __STDC__ 593 printf(const char *fmt, ...) 594 #else 595 printf(fmt, va_alist) 596 char *fmt; 597 va_dcl 598 #endif 599 { 600 va_list ap; 601 int s; 602 603 KPRINTF_MUTEX_ENTER(s); 604 605 va_start(ap, fmt); 606 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); 607 va_end(ap); 608 609 KPRINTF_MUTEX_EXIT(s); 610 611 if (!panicstr) 612 logwakeup(); 613 } 614 615 /* 616 * vprintf: print a message to the console and the log [already have 617 * va_alist] 618 */ 619 620 void 621 vprintf(fmt, ap) 622 const char *fmt; 623 va_list ap; 624 { 625 int s; 626 627 KPRINTF_MUTEX_ENTER(s); 628 629 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); 630 631 KPRINTF_MUTEX_EXIT(s); 632 633 if (!panicstr) 634 logwakeup(); 635 } 636 637 /* 638 * sprintf: print a message to a buffer 639 */ 640 int 641 #ifdef __STDC__ 642 sprintf(char *buf, const char *fmt, ...) 643 #else 644 sprintf(buf, fmt, va_alist) 645 char *buf; 646 const char *cfmt; 647 va_dcl 648 #endif 649 { 650 int retval; 651 va_list ap; 652 653 va_start(ap, fmt); 654 retval = kprintf(fmt, TOBUFONLY, NULL, buf, ap); 655 va_end(ap); 656 *(buf + retval) = 0; /* null terminate */ 657 return(retval); 658 } 659 660 /* 661 * vsprintf: print a message to a buffer [already have va_alist] 662 */ 663 664 int 665 vsprintf(buf, fmt, ap) 666 char *buf; 667 const char *fmt; 668 va_list ap; 669 { 670 int retval; 671 672 retval = kprintf(fmt, TOBUFONLY, NULL, buf, ap); 673 *(buf + retval) = 0; /* null terminate */ 674 return (retval); 675 } 676 677 /* 678 * snprintf: print a message to a buffer 679 */ 680 int 681 #ifdef __STDC__ 682 snprintf(char *buf, size_t size, const char *fmt, ...) 683 #else 684 snprintf(buf, size, fmt, va_alist) 685 char *buf; 686 size_t size; 687 const char *cfmt; 688 va_dcl 689 #endif 690 { 691 int retval; 692 va_list ap; 693 char *p; 694 695 if (size < 1) 696 return (-1); 697 p = buf + size - 1; 698 va_start(ap, fmt); 699 retval = kprintf(fmt, TOBUFONLY, &p, buf, ap); 700 va_end(ap); 701 *(p) = 0; /* null terminate */ 702 return(retval); 703 } 704 705 /* 706 * vsnprintf: print a message to a buffer [already have va_alist] 707 */ 708 int 709 vsnprintf(buf, size, fmt, ap) 710 char *buf; 711 size_t size; 712 const char *fmt; 713 va_list ap; 714 { 715 int retval; 716 char *p; 717 718 if (size < 1) 719 return (-1); 720 p = buf + size - 1; 721 retval = kprintf(fmt, TOBUFONLY, &p, buf, ap); 722 *(p) = 0; /* null terminate */ 723 return(retval); 724 } 725 726 /* 727 * bitmask_snprintf: print a kernel-printf "%b" message to a buffer 728 * 729 * => returns pointer to the buffer 730 * => XXX: useful vs. kernel %b? 731 */ 732 char * 733 bitmask_snprintf(val, p, buf, buflen) 734 u_quad_t val; 735 const char *p; 736 char *buf; 737 size_t buflen; 738 { 739 char *bp, *q; 740 size_t left; 741 char *sbase, snbuf[KPRINTF_BUFSIZE]; 742 int base, bit, ch, len, sep; 743 u_quad_t field; 744 745 bp = buf; 746 memset(buf, 0, buflen); 747 748 /* 749 * Always leave room for the trailing NULL. 750 */ 751 left = buflen - 1; 752 753 /* 754 * Print the value into the buffer. Abort if there's not 755 * enough room. 756 */ 757 if (buflen < KPRINTF_BUFSIZE) 758 return (buf); 759 760 ch = *p++; 761 base = ch != '\177' ? ch : *p++; 762 sbase = base == 8 ? "%qo" : base == 10 ? "%qd" : base == 16 ? "%qx" : 0; 763 if (sbase == 0) 764 return (buf); /* punt if not oct, dec, or hex */ 765 766 sprintf(snbuf, sbase, val); 767 for (q = snbuf ; *q ; q++) { 768 *bp++ = *q; 769 left--; 770 } 771 772 /* 773 * If the value we printed was 0 and we're using the old-style format, 774 * or if we don't have room for "<x>", we're done. 775 */ 776 if (((val == 0) && (ch != '\177')) || left < 3) 777 return (buf); 778 779 #define PUTBYTE(b, c, l) \ 780 *(b)++ = (c); \ 781 if (--(l) == 0) \ 782 goto out; 783 #define PUTSTR(b, p, l) do { \ 784 int c; \ 785 while ((c = *(p)++) != 0) { \ 786 *(b)++ = c; \ 787 if (--(l) == 0) \ 788 goto out; \ 789 } \ 790 } while (0) 791 792 /* 793 * Chris Torek's new style %b format is identified by a leading \177 794 */ 795 sep = '<'; 796 if (ch != '\177') { 797 /* old (standard) %b format. */ 798 for (;(bit = *p++) != 0;) { 799 if (val & (1 << (bit - 1))) { 800 PUTBYTE(bp, sep, left); 801 for (; (ch = *p) > ' '; ++p) { 802 PUTBYTE(bp, ch, left); 803 } 804 sep = ','; 805 } else 806 for (; *p > ' '; ++p) 807 continue; 808 } 809 } else { 810 /* new quad-capable %b format; also does fields. */ 811 field = val; 812 while ((ch = *p++) != '\0') { 813 bit = *p++; /* now 0-origin */ 814 switch (ch) { 815 case 'b': 816 if (((u_int)(val >> bit) & 1) == 0) 817 goto skip; 818 PUTBYTE(bp, sep, left); 819 PUTSTR(bp, p, left); 820 sep = ','; 821 break; 822 case 'f': 823 case 'F': 824 len = *p++; /* field length */ 825 field = (val >> bit) & ((1ULL << len) - 1); 826 if (ch == 'F') /* just extract */ 827 break; 828 PUTBYTE(bp, sep, left); 829 sep = ','; 830 PUTSTR(bp, p, left); 831 PUTBYTE(bp, '=', left); 832 sprintf(snbuf, sbase, field); 833 q = snbuf; PUTSTR(bp, q, left); 834 break; 835 case '=': 836 case ':': 837 /* 838 * Here "bit" is actually a value instead, 839 * to be compared against the last field. 840 * This only works for values in [0..255], 841 * of course. 842 */ 843 if ((int)field != bit) 844 goto skip; 845 if (ch == '=') 846 PUTBYTE(bp, '=', left); 847 PUTSTR(bp, p, left); 848 break; 849 default: 850 skip: 851 while (*p++ != '\0') 852 continue; 853 break; 854 } 855 } 856 } 857 if (sep != '<') 858 PUTBYTE(bp, '>', left); 859 860 out: 861 return (buf); 862 863 #undef PUTBYTE 864 #undef PUTSTR 865 } 866 867 /* 868 * kprintf: scaled down version of printf(3). 869 * 870 * this version based on vfprintf() from libc which was derived from 871 * software contributed to Berkeley by Chris Torek. 872 * 873 * Two additional formats: 874 * 875 * The format %b is supported to decode error registers. 876 * Its usage is: 877 * 878 * printf("reg=%b\n", regval, "<base><arg>*"); 879 * 880 * where <base> is the output base expressed as a control character, e.g. 881 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters, 882 * the first of which gives the bit number to be inspected (origin 1), and 883 * the next characters (up to a control character, i.e. a character <= 32), 884 * give the name of the register. Thus: 885 * 886 * kprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n"); 887 * 888 * would produce output: 889 * 890 * reg=3<BITTWO,BITONE> 891 * 892 * The format %: passes an additional format string and argument list 893 * recursively. Its usage is: 894 * 895 * fn(char *fmt, ...) 896 * { 897 * va_list ap; 898 * va_start(ap, fmt); 899 * printf("prefix: %: suffix\n", fmt, ap); 900 * va_end(ap); 901 * } 902 * 903 * this is the actual printf innards 904 * 905 * This code is large and complicated... 906 * 907 * NOTE: The kprintf mutex must be held of we're going TOBUF or TOCONS! 908 */ 909 910 /* 911 * macros for converting digits to letters and vice versa 912 */ 913 #define to_digit(c) ((c) - '0') 914 #define is_digit(c) ((unsigned)to_digit(c) <= 9) 915 #define to_char(n) ((n) + '0') 916 917 /* 918 * flags used during conversion. 919 */ 920 #define ALT 0x001 /* alternate form */ 921 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */ 922 #define LADJUST 0x004 /* left adjustment */ 923 #define LONGDBL 0x008 /* long double; unimplemented */ 924 #define LONGINT 0x010 /* long integer */ 925 #define QUADINT 0x020 /* quad integer */ 926 #define SHORTINT 0x040 /* short integer */ 927 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ 928 #define FPT 0x100 /* Floating point number */ 929 930 /* 931 * To extend shorts properly, we need both signed and unsigned 932 * argument extraction methods. 933 */ 934 #define SARG() \ 935 (flags&QUADINT ? va_arg(ap, quad_t) : \ 936 flags&LONGINT ? va_arg(ap, long) : \ 937 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \ 938 (long)va_arg(ap, int)) 939 #define UARG() \ 940 (flags&QUADINT ? va_arg(ap, u_quad_t) : \ 941 flags&LONGINT ? va_arg(ap, u_long) : \ 942 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \ 943 (u_long)va_arg(ap, u_int)) 944 945 #define KPRINTF_PUTCHAR(C) { \ 946 if (oflags == TOBUFONLY) { \ 947 if ((vp != NULL) && (sbuf == tailp)) { \ 948 ret += 1; /* indicate error */ \ 949 goto overflow; \ 950 } \ 951 *sbuf++ = (C); \ 952 } else { \ 953 putchar((C), oflags, (struct tty *)vp); \ 954 } \ 955 } 956 957 /* 958 * Guts of kernel printf. Note, we already expect to be in a mutex! 959 */ 960 static int 961 kprintf(fmt0, oflags, vp, sbuf, ap) 962 const char *fmt0; 963 int oflags; 964 void *vp; 965 char *sbuf; 966 va_list ap; 967 { 968 char *fmt; /* format string */ 969 int ch; /* character from fmt */ 970 int n; /* handy integer (short term usage) */ 971 char *cp; /* handy char pointer (short term usage) */ 972 int flags; /* flags as above */ 973 int ret; /* return value accumulator */ 974 int width; /* width from format (%8d), or 0 */ 975 int prec; /* precision from format (%.3d), or -1 */ 976 char sign; /* sign prefix (' ', '+', '-', or \0) */ 977 978 u_quad_t _uquad; /* integer arguments %[diouxX] */ 979 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */ 980 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 981 int realsz; /* field size expanded by dprec */ 982 int size; /* size of converted field or string */ 983 char *xdigs; /* digits for [xX] conversion */ 984 char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */ 985 char *tailp; /* tail pointer for snprintf */ 986 987 tailp = NULL; /* XXX: shutup gcc */ 988 if (oflags == TOBUFONLY && (vp != NULL)) 989 tailp = *(char **)vp; 990 991 cp = NULL; /* XXX: shutup gcc */ 992 size = 0; /* XXX: shutup gcc */ 993 994 fmt = (char *)fmt0; 995 ret = 0; 996 997 xdigs = NULL; /* XXX: shut up gcc warning */ 998 999 /* 1000 * Scan the format for conversions (`%' character). 1001 */ 1002 for (;;) { 1003 while (*fmt != '%' && *fmt) { 1004 ret++; 1005 KPRINTF_PUTCHAR(*fmt++); 1006 } 1007 if (*fmt == 0) 1008 goto done; 1009 1010 fmt++; /* skip over '%' */ 1011 1012 flags = 0; 1013 dprec = 0; 1014 width = 0; 1015 prec = -1; 1016 sign = '\0'; 1017 1018 rflag: ch = *fmt++; 1019 reswitch: switch (ch) { 1020 /* XXX: non-standard '%:' format */ 1021 #ifndef __powerpc__ 1022 case ':': 1023 if (oflags != TOBUFONLY) { 1024 cp = va_arg(ap, char *); 1025 kprintf(cp, oflags, vp, 1026 NULL, va_arg(ap, va_list)); 1027 } 1028 continue; /* no output */ 1029 #endif 1030 /* XXX: non-standard '%b' format */ 1031 case 'b': { 1032 char *b, *z; 1033 int tmp; 1034 _uquad = va_arg(ap, int); 1035 b = va_arg(ap, char *); 1036 if (*b == 8) 1037 sprintf(buf, "%qo", (unsigned long long)_uquad); 1038 else if (*b == 10) 1039 sprintf(buf, "%qd", (unsigned long long)_uquad); 1040 else if (*b == 16) 1041 sprintf(buf, "%qx", (unsigned long long)_uquad); 1042 else 1043 break; 1044 b++; 1045 1046 z = buf; 1047 while (*z) { 1048 ret++; 1049 KPRINTF_PUTCHAR(*z++); 1050 } 1051 1052 if (_uquad) { 1053 tmp = 0; 1054 while ((n = *b++) != 0) { 1055 if (_uquad & (1 << (n - 1))) { 1056 ret++; 1057 KPRINTF_PUTCHAR(tmp ? ',':'<'); 1058 while ((n = *b) > ' ') { 1059 ret++; 1060 KPRINTF_PUTCHAR(n); 1061 b++; 1062 } 1063 tmp = 1; 1064 } else { 1065 while(*b > ' ') 1066 b++; 1067 } 1068 } 1069 if (tmp) { 1070 ret++; 1071 KPRINTF_PUTCHAR('>'); 1072 } 1073 } 1074 continue; /* no output */ 1075 } 1076 1077 #ifdef DDB 1078 /* XXX: non-standard '%r' format (print int in db_radix) */ 1079 case 'r': 1080 if (db_radix == 16) 1081 goto case_z; /* signed hex */ 1082 _uquad = SARG(); 1083 if ((quad_t)_uquad < 0) { 1084 _uquad = -_uquad; 1085 sign = '-'; 1086 } 1087 base = (db_radix == 8) ? OCT : DEC; 1088 goto number; 1089 1090 1091 /* XXX: non-standard '%z' format ("signed hex", a "hex %i")*/ 1092 case 'z': 1093 case_z: 1094 xdigs = "0123456789abcdef"; 1095 ch = 'x'; /* the 'x' in '0x' (below) */ 1096 _uquad = SARG(); 1097 base = HEX; 1098 /* leading 0x/X only if non-zero */ 1099 if (flags & ALT && _uquad != 0) 1100 flags |= HEXPREFIX; 1101 if ((quad_t)_uquad < 0) { 1102 _uquad = -_uquad; 1103 sign = '-'; 1104 } 1105 goto number; 1106 #endif 1107 1108 case ' ': 1109 /* 1110 * ``If the space and + flags both appear, the space 1111 * flag will be ignored.'' 1112 * -- ANSI X3J11 1113 */ 1114 if (!sign) 1115 sign = ' '; 1116 goto rflag; 1117 case '#': 1118 flags |= ALT; 1119 goto rflag; 1120 case '*': 1121 /* 1122 * ``A negative field width argument is taken as a 1123 * - flag followed by a positive field width.'' 1124 * -- ANSI X3J11 1125 * They don't exclude field widths read from args. 1126 */ 1127 if ((width = va_arg(ap, int)) >= 0) 1128 goto rflag; 1129 width = -width; 1130 /* FALLTHROUGH */ 1131 case '-': 1132 flags |= LADJUST; 1133 goto rflag; 1134 case '+': 1135 sign = '+'; 1136 goto rflag; 1137 case '.': 1138 if ((ch = *fmt++) == '*') { 1139 n = va_arg(ap, int); 1140 prec = n < 0 ? -1 : n; 1141 goto rflag; 1142 } 1143 n = 0; 1144 while (is_digit(ch)) { 1145 n = 10 * n + to_digit(ch); 1146 ch = *fmt++; 1147 } 1148 prec = n < 0 ? -1 : n; 1149 goto reswitch; 1150 case '0': 1151 /* 1152 * ``Note that 0 is taken as a flag, not as the 1153 * beginning of a field width.'' 1154 * -- ANSI X3J11 1155 */ 1156 flags |= ZEROPAD; 1157 goto rflag; 1158 case '1': case '2': case '3': case '4': 1159 case '5': case '6': case '7': case '8': case '9': 1160 n = 0; 1161 do { 1162 n = 10 * n + to_digit(ch); 1163 ch = *fmt++; 1164 } while (is_digit(ch)); 1165 width = n; 1166 goto reswitch; 1167 case 'h': 1168 flags |= SHORTINT; 1169 goto rflag; 1170 case 'l': 1171 if (*fmt == 'l') { 1172 fmt++; 1173 flags |= QUADINT; 1174 } else { 1175 flags |= LONGINT; 1176 } 1177 goto rflag; 1178 case 'q': 1179 flags |= QUADINT; 1180 goto rflag; 1181 case 'c': 1182 *(cp = buf) = va_arg(ap, int); 1183 size = 1; 1184 sign = '\0'; 1185 break; 1186 case 'D': 1187 flags |= LONGINT; 1188 /*FALLTHROUGH*/ 1189 case 'd': 1190 case 'i': 1191 _uquad = SARG(); 1192 if ((quad_t)_uquad < 0) { 1193 _uquad = -_uquad; 1194 sign = '-'; 1195 } 1196 base = DEC; 1197 goto number; 1198 case 'n': 1199 #ifdef DDB 1200 /* XXX: non-standard '%n' format */ 1201 /* 1202 * XXX: HACK! DDB wants '%n' to be a '%u' printed 1203 * in db_radix format. this should die since '%n' 1204 * is already defined in standard printf to write 1205 * the number of chars printed so far to the arg (which 1206 * should be a pointer. 1207 */ 1208 if (oflags & TODDB) { 1209 if (db_radix == 16) 1210 ch = 'x'; /* convert to %x */ 1211 else if (db_radix == 8) 1212 ch = 'o'; /* convert to %o */ 1213 else 1214 ch = 'u'; /* convert to %u */ 1215 1216 /* ... and start again */ 1217 goto reswitch; 1218 } 1219 1220 #endif 1221 if (flags & QUADINT) 1222 *va_arg(ap, quad_t *) = ret; 1223 else if (flags & LONGINT) 1224 *va_arg(ap, long *) = ret; 1225 else if (flags & SHORTINT) 1226 *va_arg(ap, short *) = ret; 1227 else 1228 *va_arg(ap, int *) = ret; 1229 continue; /* no output */ 1230 case 'O': 1231 flags |= LONGINT; 1232 /*FALLTHROUGH*/ 1233 case 'o': 1234 _uquad = UARG(); 1235 base = OCT; 1236 goto nosign; 1237 case 'p': 1238 /* 1239 * ``The argument shall be a pointer to void. The 1240 * value of the pointer is converted to a sequence 1241 * of printable characters, in an implementation- 1242 * defined manner.'' 1243 * -- ANSI X3J11 1244 */ 1245 /* NOSTRICT */ 1246 _uquad = (u_long)va_arg(ap, void *); 1247 base = HEX; 1248 xdigs = "0123456789abcdef"; 1249 flags |= HEXPREFIX; 1250 ch = 'x'; 1251 goto nosign; 1252 case 's': 1253 if ((cp = va_arg(ap, char *)) == NULL) 1254 cp = "(null)"; 1255 if (prec >= 0) { 1256 /* 1257 * can't use strlen; can only look for the 1258 * NUL in the first `prec' characters, and 1259 * strlen() will go further. 1260 */ 1261 char *p = memchr(cp, 0, prec); 1262 1263 if (p != NULL) { 1264 size = p - cp; 1265 if (size > prec) 1266 size = prec; 1267 } else 1268 size = prec; 1269 } else 1270 size = strlen(cp); 1271 sign = '\0'; 1272 break; 1273 case 'U': 1274 flags |= LONGINT; 1275 /*FALLTHROUGH*/ 1276 case 'u': 1277 _uquad = UARG(); 1278 base = DEC; 1279 goto nosign; 1280 case 'X': 1281 xdigs = "0123456789ABCDEF"; 1282 goto hex; 1283 case 'x': 1284 xdigs = "0123456789abcdef"; 1285 hex: _uquad = UARG(); 1286 base = HEX; 1287 /* leading 0x/X only if non-zero */ 1288 if (flags & ALT && _uquad != 0) 1289 flags |= HEXPREFIX; 1290 1291 /* unsigned conversions */ 1292 nosign: sign = '\0'; 1293 /* 1294 * ``... diouXx conversions ... if a precision is 1295 * specified, the 0 flag will be ignored.'' 1296 * -- ANSI X3J11 1297 */ 1298 number: if ((dprec = prec) >= 0) 1299 flags &= ~ZEROPAD; 1300 1301 /* 1302 * ``The result of converting a zero value with an 1303 * explicit precision of zero is no characters.'' 1304 * -- ANSI X3J11 1305 */ 1306 cp = buf + KPRINTF_BUFSIZE; 1307 if (_uquad != 0 || prec != 0) { 1308 /* 1309 * Unsigned mod is hard, and unsigned mod 1310 * by a constant is easier than that by 1311 * a variable; hence this switch. 1312 */ 1313 switch (base) { 1314 case OCT: 1315 do { 1316 *--cp = to_char(_uquad & 7); 1317 _uquad >>= 3; 1318 } while (_uquad); 1319 /* handle octal leading 0 */ 1320 if (flags & ALT && *cp != '0') 1321 *--cp = '0'; 1322 break; 1323 1324 case DEC: 1325 /* many numbers are 1 digit */ 1326 while (_uquad >= 10) { 1327 *--cp = to_char(_uquad % 10); 1328 _uquad /= 10; 1329 } 1330 *--cp = to_char(_uquad); 1331 break; 1332 1333 case HEX: 1334 do { 1335 *--cp = xdigs[_uquad & 15]; 1336 _uquad >>= 4; 1337 } while (_uquad); 1338 break; 1339 1340 default: 1341 cp = "bug in kprintf: bad base"; 1342 size = strlen(cp); 1343 goto skipsize; 1344 } 1345 } 1346 size = buf + KPRINTF_BUFSIZE - cp; 1347 skipsize: 1348 break; 1349 default: /* "%?" prints ?, unless ? is NUL */ 1350 if (ch == '\0') 1351 goto done; 1352 /* pretend it was %c with argument ch */ 1353 cp = buf; 1354 *cp = ch; 1355 size = 1; 1356 sign = '\0'; 1357 break; 1358 } 1359 1360 /* 1361 * All reasonable formats wind up here. At this point, `cp' 1362 * points to a string which (if not flags&LADJUST) should be 1363 * padded out to `width' places. If flags&ZEROPAD, it should 1364 * first be prefixed by any sign or other prefix; otherwise, 1365 * it should be blank padded before the prefix is emitted. 1366 * After any left-hand padding and prefixing, emit zeroes 1367 * required by a decimal [diouxX] precision, then print the 1368 * string proper, then emit zeroes required by any leftover 1369 * floating precision; finally, if LADJUST, pad with blanks. 1370 * 1371 * Compute actual size, so we know how much to pad. 1372 * size excludes decimal prec; realsz includes it. 1373 */ 1374 realsz = dprec > size ? dprec : size; 1375 if (sign) 1376 realsz++; 1377 else if (flags & HEXPREFIX) 1378 realsz+= 2; 1379 1380 /* adjust ret */ 1381 ret += width > realsz ? width : realsz; 1382 1383 /* right-adjusting blank padding */ 1384 if ((flags & (LADJUST|ZEROPAD)) == 0) { 1385 n = width - realsz; 1386 while (n-- > 0) 1387 KPRINTF_PUTCHAR(' '); 1388 } 1389 1390 /* prefix */ 1391 if (sign) { 1392 KPRINTF_PUTCHAR(sign); 1393 } else if (flags & HEXPREFIX) { 1394 KPRINTF_PUTCHAR('0'); 1395 KPRINTF_PUTCHAR(ch); 1396 } 1397 1398 /* right-adjusting zero padding */ 1399 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) { 1400 n = width - realsz; 1401 while (n-- > 0) 1402 KPRINTF_PUTCHAR('0'); 1403 } 1404 1405 /* leading zeroes from decimal precision */ 1406 n = dprec - size; 1407 while (n-- > 0) 1408 KPRINTF_PUTCHAR('0'); 1409 1410 /* the string or number proper */ 1411 while (size--) 1412 KPRINTF_PUTCHAR(*cp++); 1413 /* left-adjusting padding (always blank) */ 1414 if (flags & LADJUST) { 1415 n = width - realsz; 1416 while (n-- > 0) 1417 KPRINTF_PUTCHAR(' '); 1418 } 1419 } 1420 1421 done: 1422 if ((oflags == TOBUFONLY) && (vp != NULL)) 1423 *(char **)vp = sbuf; 1424 overflow: 1425 return (ret); 1426 /* NOTREACHED */ 1427 } 1428