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