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