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