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