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