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