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