1 /* $NetBSD: subr_prf.c,v 1.150 2013/02/10 11:04:19 apb 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.150 2013/02/10 11:04:19 apb 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/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 IPKDB 72 #include <ipkdb/ipkdb.h> 73 #endif 74 75 static kmutex_t kprintf_mtx; 76 static bool kprintf_inited = false; 77 78 #ifdef KGDB 79 #include <sys/kgdb.h> 80 #endif 81 82 #ifdef DDB 83 #include <ddb/ddbvar.h> /* db_panic */ 84 #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */ 85 #endif 86 87 88 /* 89 * defines 90 */ 91 92 93 /* 94 * local prototypes 95 */ 96 97 static void putchar(int, int, struct tty *); 98 99 100 /* 101 * globals 102 */ 103 104 extern struct tty *constty; /* pointer to console "window" tty */ 105 extern int log_open; /* subr_log: is /dev/klog open? */ 106 const char *panicstr; /* arg to first call to panic (used as a flag 107 to indicate that panic has already been called). */ 108 struct cpu_info *paniccpu; /* cpu that first paniced */ 109 long panicstart, panicend; /* position in the msgbuf of the start and 110 end of the formatted panicstr. */ 111 int doing_shutdown; /* set to indicate shutdown in progress */ 112 113 #ifndef DUMP_ON_PANIC 114 #define DUMP_ON_PANIC 1 115 #endif 116 int dumponpanic = DUMP_ON_PANIC; 117 118 /* 119 * v_putc: routine to putc on virtual console 120 * 121 * the v_putc pointer can be used to redirect the console cnputc elsewhere 122 * [e.g. to a "virtual console"]. 123 */ 124 125 void (*v_putc)(int) = cnputc; /* start with cnputc (normal cons) */ 126 void (*v_flush)(void) = cnflush; /* start with cnflush (normal cons) */ 127 128 const char hexdigits[] = "0123456789abcdef"; 129 const char HEXDIGITS[] = "0123456789ABCDEF"; 130 131 132 /* 133 * functions 134 */ 135 136 /* 137 * Locking is inited fairly early in MI bootstrap. Before that 138 * prints are done unlocked. But that doesn't really matter, 139 * since nothing can preempt us before interrupts are enabled. 140 */ 141 void 142 kprintf_init(void) 143 { 144 145 KASSERT(!kprintf_inited && cold); /* not foolproof, but ... */ 146 mutex_init(&kprintf_mtx, MUTEX_DEFAULT, IPL_HIGH); 147 kprintf_inited = true; 148 } 149 150 void 151 kprintf_lock(void) 152 { 153 154 if (__predict_true(kprintf_inited)) 155 mutex_enter(&kprintf_mtx); 156 } 157 158 void 159 kprintf_unlock(void) 160 { 161 162 if (__predict_true(kprintf_inited)) { 163 /* assert kprintf wasn't somehow inited while we were in */ 164 KASSERT(mutex_owned(&kprintf_mtx)); 165 mutex_exit(&kprintf_mtx); 166 } 167 } 168 169 /* 170 * twiddle: spin a little propellor on the console. 171 */ 172 173 void 174 twiddle(void) 175 { 176 static const char twiddle_chars[] = "|/-\\"; 177 static int pos; 178 179 kprintf_lock(); 180 181 putchar(twiddle_chars[pos++ & 3], TOCONS, NULL); 182 putchar('\b', TOCONS, NULL); 183 184 kprintf_unlock(); 185 } 186 187 /* 188 * panic: handle an unresolvable fatal error 189 * 190 * prints "panic: <message>" and reboots. if called twice (i.e. recursive 191 * call) we avoid trying to dump and just reboot (to avoid recursive panics). 192 */ 193 194 void 195 panic(const char *fmt, ...) 196 { 197 va_list ap; 198 199 va_start(ap, fmt); 200 vpanic(fmt, ap); 201 va_end(ap); 202 } 203 204 void 205 vpanic(const char *fmt, va_list ap) 206 { 207 CPU_INFO_ITERATOR cii; 208 struct cpu_info *ci, *oci; 209 int bootopt; 210 static char scratchstr[256]; /* stores panic message */ 211 212 spldebug_stop(); 213 214 if (lwp0.l_cpu && curlwp) { 215 /* 216 * Disable preemption. If already panicing on another CPU, sit 217 * here and spin until the system is rebooted. Allow the CPU that 218 * first paniced to panic again. 219 */ 220 kpreempt_disable(); 221 ci = curcpu(); 222 oci = atomic_cas_ptr((void *)&paniccpu, NULL, ci); 223 if (oci != NULL && oci != ci) { 224 /* Give interrupts a chance to try and prevent deadlock. */ 225 for (;;) { 226 #ifndef _RUMPKERNEL /* XXXpooka: temporary build fix, see kern/40505 */ 227 DELAY(10); 228 #endif /* _RUMPKERNEL */ 229 } 230 } 231 232 /* 233 * Convert the current thread to a bound thread and prevent all 234 * CPUs from scheduling unbound jobs. Do so without taking any 235 * locks. 236 */ 237 curlwp->l_pflag |= LP_BOUND; 238 for (CPU_INFO_FOREACH(cii, ci)) { 239 ci->ci_schedstate.spc_flags |= SPCF_OFFLINE; 240 } 241 } 242 243 bootopt = RB_AUTOBOOT | RB_NOSYNC; 244 if (!doing_shutdown) { 245 if (dumponpanic) 246 bootopt |= RB_DUMP; 247 } else 248 printf("Skipping crash dump on recursive panic\n"); 249 250 doing_shutdown = 1; 251 252 if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC) 253 panicstart = msgbufp->msg_bufx; 254 255 printf("panic: "); 256 if (panicstr == NULL) { 257 /* first time in panic - store fmt first for precaution */ 258 panicstr = fmt; 259 260 vsnprintf(scratchstr, sizeof(scratchstr), fmt, ap); 261 printf("%s", scratchstr); 262 panicstr = scratchstr; 263 } else { 264 vprintf(fmt, ap); 265 } 266 printf("\n"); 267 268 if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC) 269 panicend = msgbufp->msg_bufx; 270 271 #ifdef IPKDB 272 ipkdb_panic(); 273 #endif 274 #ifdef KGDB 275 kgdb_panic(); 276 #endif 277 #ifdef KADB 278 if (boothowto & RB_KDB) 279 kdbpanic(); 280 #endif 281 #ifdef DDB 282 db_panic(); 283 #endif 284 cpu_reboot(bootopt, NULL); 285 } 286 287 /* 288 * kernel logging functions: log, logpri, addlog 289 */ 290 291 /* 292 * log: write to the log buffer 293 * 294 * => will not sleep [so safe to call from interrupt] 295 * => will log to console if /dev/klog isn't open 296 */ 297 298 void 299 log(int level, const char *fmt, ...) 300 { 301 va_list ap; 302 303 kprintf_lock(); 304 305 klogpri(level); /* log the level first */ 306 va_start(ap, fmt); 307 kprintf(fmt, TOLOG, NULL, NULL, ap); 308 va_end(ap); 309 if (!log_open) { 310 va_start(ap, fmt); 311 kprintf(fmt, TOCONS, NULL, NULL, ap); 312 va_end(ap); 313 } 314 315 kprintf_unlock(); 316 317 logwakeup(); /* wake up anyone waiting for log msgs */ 318 } 319 320 /* 321 * vlog: write to the log buffer [already have va_list] 322 */ 323 324 void 325 vlog(int level, const char *fmt, va_list ap) 326 { 327 va_list cap; 328 329 va_copy(cap, ap); 330 kprintf_lock(); 331 332 klogpri(level); /* log the level first */ 333 kprintf(fmt, TOLOG, NULL, NULL, ap); 334 if (!log_open) 335 kprintf(fmt, TOCONS, NULL, NULL, cap); 336 337 kprintf_unlock(); 338 va_end(cap); 339 340 logwakeup(); /* wake up anyone waiting for log msgs */ 341 } 342 343 /* 344 * logpri: log the priority level to the klog 345 */ 346 347 void 348 logpri(int level) 349 { 350 351 kprintf_lock(); 352 klogpri(level); 353 kprintf_unlock(); 354 } 355 356 /* 357 * Note: we must be in the mutex here! 358 */ 359 void 360 klogpri(int level) 361 { 362 char *p; 363 char snbuf[KPRINTF_BUFSIZE]; 364 365 putchar('<', TOLOG, NULL); 366 snprintf(snbuf, sizeof(snbuf), "%d", level); 367 for (p = snbuf ; *p ; p++) 368 putchar(*p, TOLOG, NULL); 369 putchar('>', TOLOG, NULL); 370 } 371 372 /* 373 * addlog: add info to previous log message 374 */ 375 376 void 377 addlog(const char *fmt, ...) 378 { 379 va_list ap; 380 381 kprintf_lock(); 382 383 va_start(ap, fmt); 384 kprintf(fmt, TOLOG, NULL, NULL, ap); 385 va_end(ap); 386 if (!log_open) { 387 va_start(ap, fmt); 388 kprintf(fmt, TOCONS, NULL, NULL, ap); 389 va_end(ap); 390 } 391 392 kprintf_unlock(); 393 394 logwakeup(); 395 } 396 397 398 /* 399 * putchar: print a single character on console or user terminal. 400 * 401 * => if console, then the last MSGBUFS chars are saved in msgbuf 402 * for inspection later (e.g. dmesg/syslog) 403 * => we must already be in the mutex! 404 */ 405 static void 406 putchar(int c, int flags, struct tty *tp) 407 { 408 409 if (panicstr) 410 constty = NULL; 411 if ((flags & TOCONS) && tp == NULL && constty) { 412 tp = constty; 413 flags |= TOTTY; 414 } 415 if ((flags & TOTTY) && tp && 416 tputchar(c, flags, tp) < 0 && 417 (flags & TOCONS) && tp == constty) 418 constty = NULL; 419 if ((flags & TOLOG) && 420 c != '\0' && c != '\r' && c != 0177) 421 logputchar(c); 422 if ((flags & TOCONS) && constty == NULL && c != '\0') 423 (*v_putc)(c); 424 #ifdef DDB 425 if (flags & TODDB) 426 db_putchar(c); 427 #endif 428 } 429 430 /* 431 * tablefull: warn that a system table is full 432 */ 433 434 void 435 tablefull(const char *tab, const char *hint) 436 { 437 if (hint) 438 log(LOG_ERR, "%s: table is full - %s\n", tab, hint); 439 else 440 log(LOG_ERR, "%s: table is full\n", tab); 441 } 442 443 444 /* 445 * uprintf: print to the controlling tty of the current process 446 * 447 * => we may block if the tty queue is full 448 * => no message is printed if the queue doesn't clear in a reasonable 449 * time 450 */ 451 452 void 453 uprintf(const char *fmt, ...) 454 { 455 struct proc *p = curproc; 456 va_list ap; 457 458 /* mutex_enter(proc_lock); XXXSMP */ 459 460 if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) { 461 /* No mutex needed; going to process TTY. */ 462 va_start(ap, fmt); 463 kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap); 464 va_end(ap); 465 } 466 467 /* mutex_exit(proc_lock); XXXSMP */ 468 } 469 470 void 471 uprintf_locked(const char *fmt, ...) 472 { 473 struct proc *p = curproc; 474 va_list ap; 475 476 if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) { 477 /* No mutex needed; going to process TTY. */ 478 va_start(ap, fmt); 479 kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap); 480 va_end(ap); 481 } 482 } 483 484 /* 485 * tprintf functions: used to send messages to a specific process 486 * 487 * usage: 488 * get a tpr_t handle on a process "p" by using "tprintf_open(p)" 489 * use the handle when calling "tprintf" 490 * when done, do a "tprintf_close" to drop the handle 491 */ 492 493 /* 494 * tprintf_open: get a tprintf handle on a process "p" 495 * 496 * => returns NULL if process can't be printed to 497 */ 498 499 tpr_t 500 tprintf_open(struct proc *p) 501 { 502 tpr_t cookie; 503 504 cookie = NULL; 505 506 mutex_enter(proc_lock); 507 if (p->p_lflag & PL_CONTROLT && p->p_session->s_ttyvp) { 508 proc_sesshold(p->p_session); 509 cookie = (tpr_t)p->p_session; 510 } 511 mutex_exit(proc_lock); 512 513 return cookie; 514 } 515 516 /* 517 * tprintf_close: dispose of a tprintf handle obtained with tprintf_open 518 */ 519 520 void 521 tprintf_close(tpr_t sess) 522 { 523 524 if (sess) { 525 mutex_enter(proc_lock); 526 /* Releases proc_lock. */ 527 proc_sessrele((struct session *)sess); 528 } 529 } 530 531 /* 532 * tprintf: given tprintf handle to a process [obtained with tprintf_open], 533 * send a message to the controlling tty for that process. 534 * 535 * => also sends message to /dev/klog 536 */ 537 void 538 tprintf(tpr_t tpr, const char *fmt, ...) 539 { 540 struct session *sess = (struct session *)tpr; 541 struct tty *tp = NULL; 542 int flags = TOLOG; 543 va_list ap; 544 545 /* mutex_enter(proc_lock); XXXSMP */ 546 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) { 547 flags |= TOTTY; 548 tp = sess->s_ttyp; 549 } 550 551 kprintf_lock(); 552 553 klogpri(LOG_INFO); 554 va_start(ap, fmt); 555 kprintf(fmt, flags, tp, NULL, ap); 556 va_end(ap); 557 558 kprintf_unlock(); 559 /* mutex_exit(proc_lock); XXXSMP */ 560 561 logwakeup(); 562 } 563 564 565 /* 566 * ttyprintf: send a message to a specific tty 567 * 568 * => should be used only by tty driver or anything that knows the 569 * underlying tty will not be revoked(2)'d away. [otherwise, 570 * use tprintf] 571 */ 572 void 573 ttyprintf(struct tty *tp, const char *fmt, ...) 574 { 575 va_list ap; 576 577 /* No mutex needed; going to process TTY. */ 578 va_start(ap, fmt); 579 kprintf(fmt, TOTTY, tp, NULL, ap); 580 va_end(ap); 581 } 582 583 #ifdef DDB 584 585 /* 586 * db_printf: printf for DDB (via db_putchar) 587 */ 588 589 void 590 db_printf(const char *fmt, ...) 591 { 592 va_list ap; 593 594 /* No mutex needed; DDB pauses all processors. */ 595 va_start(ap, fmt); 596 kprintf(fmt, TODDB, NULL, NULL, ap); 597 va_end(ap); 598 599 if (db_tee_msgbuf) { 600 va_start(ap, fmt); 601 kprintf(fmt, TOLOG, NULL, NULL, ap); 602 va_end(ap); 603 }; 604 } 605 606 void 607 db_vprintf(const char *fmt, va_list ap) 608 { 609 va_list cap; 610 611 va_copy(cap, ap); 612 /* No mutex needed; DDB pauses all processors. */ 613 kprintf(fmt, TODDB, NULL, NULL, ap); 614 if (db_tee_msgbuf) 615 kprintf(fmt, TOLOG, NULL, NULL, cap); 616 va_end(cap); 617 } 618 619 #endif /* DDB */ 620 621 static void 622 kprintf_internal(const char *fmt, int oflags, void *vp, char *sbuf, ...) 623 { 624 va_list ap; 625 626 va_start(ap, sbuf); 627 (void)kprintf(fmt, oflags, vp, sbuf, ap); 628 va_end(ap); 629 } 630 631 /* 632 * Device autoconfiguration printf routines. These change their 633 * behavior based on the AB_* flags in boothowto. If AB_SILENT 634 * is set, messages never go to the console (but they still always 635 * go to the log). AB_VERBOSE overrides AB_SILENT. 636 */ 637 638 /* 639 * aprint_normal: Send to console unless AB_QUIET. Always goes 640 * to the log. 641 */ 642 static void 643 aprint_normal_internal(const char *prefix, const char *fmt, va_list ap) 644 { 645 int flags = TOLOG; 646 647 if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 || 648 (boothowto & AB_VERBOSE) != 0) 649 flags |= TOCONS; 650 651 kprintf_lock(); 652 653 if (prefix) 654 kprintf_internal("%s: ", flags, NULL, NULL, prefix); 655 kprintf(fmt, flags, NULL, NULL, ap); 656 657 kprintf_unlock(); 658 659 if (!panicstr) 660 logwakeup(); 661 } 662 663 void 664 aprint_normal(const char *fmt, ...) 665 { 666 va_list ap; 667 668 va_start(ap, fmt); 669 aprint_normal_internal(NULL, fmt, ap); 670 va_end(ap); 671 } 672 673 void 674 aprint_normal_dev(device_t dv, const char *fmt, ...) 675 { 676 va_list ap; 677 678 va_start(ap, fmt); 679 aprint_normal_internal(device_xname(dv), fmt, ap); 680 va_end(ap); 681 } 682 683 void 684 aprint_normal_ifnet(struct ifnet *ifp, const char *fmt, ...) 685 { 686 va_list ap; 687 688 va_start(ap, fmt); 689 aprint_normal_internal(ifp->if_xname, fmt, ap); 690 va_end(ap); 691 } 692 693 /* 694 * aprint_error: Send to console unless AB_QUIET. Always goes 695 * to the log. Also counts the number of times called so other 696 * parts of the kernel can report the number of errors during a 697 * given phase of system startup. 698 */ 699 static int aprint_error_count; 700 701 int 702 aprint_get_error_count(void) 703 { 704 int count; 705 706 kprintf_lock(); 707 708 count = aprint_error_count; 709 aprint_error_count = 0; 710 711 kprintf_unlock(); 712 713 return (count); 714 } 715 716 static void 717 aprint_error_internal(const char *prefix, const char *fmt, va_list ap) 718 { 719 int flags = TOLOG; 720 721 if ((boothowto & (AB_SILENT|AB_QUIET)) == 0 || 722 (boothowto & AB_VERBOSE) != 0) 723 flags |= TOCONS; 724 725 kprintf_lock(); 726 727 aprint_error_count++; 728 729 if (prefix) 730 kprintf_internal("%s: ", flags, NULL, NULL, prefix); 731 kprintf(fmt, flags, NULL, NULL, ap); 732 733 kprintf_unlock(); 734 735 if (!panicstr) 736 logwakeup(); 737 } 738 739 void 740 aprint_error(const char *fmt, ...) 741 { 742 va_list ap; 743 744 va_start(ap, fmt); 745 aprint_error_internal(NULL, fmt, ap); 746 va_end(ap); 747 } 748 749 void 750 aprint_error_dev(device_t dv, const char *fmt, ...) 751 { 752 va_list ap; 753 754 va_start(ap, fmt); 755 aprint_error_internal(device_xname(dv), fmt, ap); 756 va_end(ap); 757 } 758 759 void 760 aprint_error_ifnet(struct ifnet *ifp, const char *fmt, ...) 761 { 762 va_list ap; 763 764 va_start(ap, fmt); 765 aprint_error_internal(ifp->if_xname, fmt, ap); 766 va_end(ap); 767 } 768 769 /* 770 * aprint_naive: Send to console only if AB_QUIET. Never goes 771 * to the log. 772 */ 773 static void 774 aprint_naive_internal(const char *prefix, const char *fmt, va_list ap) 775 { 776 777 if ((boothowto & (AB_QUIET|AB_SILENT|AB_VERBOSE)) != AB_QUIET) 778 return; 779 780 kprintf_lock(); 781 782 if (prefix) 783 kprintf_internal("%s: ", TOCONS, NULL, NULL, prefix); 784 kprintf(fmt, TOCONS, NULL, NULL, ap); 785 786 kprintf_unlock(); 787 } 788 789 void 790 aprint_naive(const char *fmt, ...) 791 { 792 va_list ap; 793 794 va_start(ap, fmt); 795 aprint_naive_internal(NULL, fmt, ap); 796 va_end(ap); 797 } 798 799 void 800 aprint_naive_dev(device_t dv, const char *fmt, ...) 801 { 802 va_list ap; 803 804 va_start(ap, fmt); 805 aprint_naive_internal(device_xname(dv), fmt, ap); 806 va_end(ap); 807 } 808 809 void 810 aprint_naive_ifnet(struct ifnet *ifp, const char *fmt, ...) 811 { 812 va_list ap; 813 814 va_start(ap, fmt); 815 aprint_naive_internal(ifp->if_xname, fmt, ap); 816 va_end(ap); 817 } 818 819 /* 820 * aprint_verbose: Send to console only if AB_VERBOSE. Always 821 * goes to the log. 822 */ 823 static void 824 aprint_verbose_internal(const char *prefix, const char *fmt, va_list ap) 825 { 826 int flags = TOLOG; 827 828 if (boothowto & AB_VERBOSE) 829 flags |= TOCONS; 830 831 kprintf_lock(); 832 833 if (prefix) 834 kprintf_internal("%s: ", flags, NULL, NULL, prefix); 835 kprintf(fmt, flags, NULL, NULL, ap); 836 837 kprintf_unlock(); 838 839 if (!panicstr) 840 logwakeup(); 841 } 842 843 void 844 aprint_verbose(const char *fmt, ...) 845 { 846 va_list ap; 847 848 va_start(ap, fmt); 849 aprint_verbose_internal(NULL, fmt, ap); 850 va_end(ap); 851 } 852 853 void 854 aprint_verbose_dev(device_t dv, const char *fmt, ...) 855 { 856 va_list ap; 857 858 va_start(ap, fmt); 859 aprint_verbose_internal(device_xname(dv), fmt, ap); 860 va_end(ap); 861 } 862 863 void 864 aprint_verbose_ifnet(struct ifnet *ifp, const char *fmt, ...) 865 { 866 va_list ap; 867 868 va_start(ap, fmt); 869 aprint_verbose_internal(ifp->if_xname, fmt, ap); 870 va_end(ap); 871 } 872 873 /* 874 * aprint_debug: Send to console and log only if AB_DEBUG. 875 */ 876 static void 877 aprint_debug_internal(const char *prefix, const char *fmt, va_list ap) 878 { 879 880 if ((boothowto & AB_DEBUG) == 0) 881 return; 882 883 kprintf_lock(); 884 885 if (prefix) 886 kprintf_internal("%s: ", TOCONS | TOLOG, NULL, NULL, prefix); 887 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); 888 889 kprintf_unlock(); 890 } 891 892 void 893 aprint_debug(const char *fmt, ...) 894 { 895 va_list ap; 896 897 va_start(ap, fmt); 898 aprint_debug_internal(NULL, fmt, ap); 899 va_end(ap); 900 } 901 902 void 903 aprint_debug_dev(device_t dv, const char *fmt, ...) 904 { 905 va_list ap; 906 907 va_start(ap, fmt); 908 aprint_debug_internal(device_xname(dv), fmt, ap); 909 va_end(ap); 910 } 911 912 void 913 aprint_debug_ifnet(struct ifnet *ifp, const char *fmt, ...) 914 { 915 va_list ap; 916 917 va_start(ap, fmt); 918 aprint_debug_internal(ifp->if_xname, fmt, ap); 919 va_end(ap); 920 } 921 922 void 923 printf_tolog(const char *fmt, ...) 924 { 925 va_list ap; 926 927 kprintf_lock(); 928 929 va_start(ap, fmt); 930 (void)kprintf(fmt, TOLOG, NULL, NULL, ap); 931 va_end(ap); 932 933 kprintf_unlock(); 934 } 935 936 /* 937 * printf_nolog: Like printf(), but does not send message to the log. 938 */ 939 940 void 941 printf_nolog(const char *fmt, ...) 942 { 943 va_list ap; 944 945 kprintf_lock(); 946 947 va_start(ap, fmt); 948 kprintf(fmt, TOCONS, NULL, NULL, ap); 949 va_end(ap); 950 951 kprintf_unlock(); 952 } 953 954 /* 955 * normal kernel printf functions: printf, vprintf, snprintf, vsnprintf 956 */ 957 958 /* 959 * printf: print a message to the console and the log 960 */ 961 void 962 printf(const char *fmt, ...) 963 { 964 va_list ap; 965 966 kprintf_lock(); 967 968 va_start(ap, fmt); 969 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); 970 va_end(ap); 971 972 kprintf_unlock(); 973 974 if (!panicstr) 975 logwakeup(); 976 } 977 978 /* 979 * vprintf: print a message to the console and the log [already have 980 * va_list] 981 */ 982 983 void 984 vprintf(const char *fmt, va_list ap) 985 { 986 987 kprintf_lock(); 988 989 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap); 990 991 kprintf_unlock(); 992 993 if (!panicstr) 994 logwakeup(); 995 } 996 997 /* 998 * sprintf: print a message to a buffer 999 */ 1000 int 1001 sprintf(char *bf, const char *fmt, ...) 1002 { 1003 int retval; 1004 va_list ap; 1005 1006 va_start(ap, fmt); 1007 retval = kprintf(fmt, TOBUFONLY, NULL, bf, ap); 1008 va_end(ap); 1009 if (bf) 1010 bf[retval] = '\0'; /* nul terminate */ 1011 return retval; 1012 } 1013 1014 /* 1015 * vsprintf: print a message to a buffer [already have va_list] 1016 */ 1017 1018 int 1019 vsprintf(char *bf, const char *fmt, va_list ap) 1020 { 1021 int retval; 1022 1023 retval = kprintf(fmt, TOBUFONLY, NULL, bf, ap); 1024 if (bf) 1025 bf[retval] = '\0'; /* nul terminate */ 1026 return retval; 1027 } 1028 1029 /* 1030 * snprintf: print a message to a buffer 1031 */ 1032 int 1033 snprintf(char *bf, size_t size, const char *fmt, ...) 1034 { 1035 int retval; 1036 va_list ap; 1037 1038 va_start(ap, fmt); 1039 retval = vsnprintf(bf, size, fmt, ap); 1040 va_end(ap); 1041 1042 return retval; 1043 } 1044 1045 /* 1046 * vsnprintf: print a message to a buffer [already have va_list] 1047 */ 1048 int 1049 vsnprintf(char *bf, size_t size, const char *fmt, va_list ap) 1050 { 1051 int retval; 1052 char *p; 1053 1054 p = bf + size; 1055 retval = kprintf(fmt, TOBUFONLY, &p, bf, ap); 1056 if (bf && size > 0) { 1057 /* nul terminate */ 1058 if (size <= (size_t)retval) 1059 bf[size - 1] = '\0'; 1060 else 1061 bf[retval] = '\0'; 1062 } 1063 return retval; 1064 } 1065 1066 /* 1067 * kprintf: scaled down version of printf(3). 1068 * 1069 * this version based on vfprintf() from libc which was derived from 1070 * software contributed to Berkeley by Chris Torek. 1071 * 1072 * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS! 1073 */ 1074 1075 /* 1076 * macros for converting digits to letters and vice versa 1077 */ 1078 #define to_digit(c) ((c) - '0') 1079 #define is_digit(c) ((unsigned)to_digit(c) <= 9) 1080 #define to_char(n) ((n) + '0') 1081 1082 /* 1083 * flags used during conversion. 1084 */ 1085 #define ALT 0x001 /* alternate form */ 1086 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */ 1087 #define LADJUST 0x004 /* left adjustment */ 1088 #define LONGDBL 0x008 /* long double; unimplemented */ 1089 #define LONGINT 0x010 /* long integer */ 1090 #define QUADINT 0x020 /* quad integer */ 1091 #define SHORTINT 0x040 /* short integer */ 1092 #define MAXINT 0x080 /* intmax_t */ 1093 #define PTRINT 0x100 /* intptr_t */ 1094 #define SIZEINT 0x200 /* size_t */ 1095 #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */ 1096 #define FPT 0x800 /* Floating point number */ 1097 1098 /* 1099 * To extend shorts properly, we need both signed and unsigned 1100 * argument extraction methods. 1101 */ 1102 #define SARG() \ 1103 (flags&MAXINT ? va_arg(ap, intmax_t) : \ 1104 flags&PTRINT ? va_arg(ap, intptr_t) : \ 1105 flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \ 1106 flags&QUADINT ? va_arg(ap, quad_t) : \ 1107 flags&LONGINT ? va_arg(ap, long) : \ 1108 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \ 1109 (long)va_arg(ap, int)) 1110 #define UARG() \ 1111 (flags&MAXINT ? va_arg(ap, uintmax_t) : \ 1112 flags&PTRINT ? va_arg(ap, uintptr_t) : \ 1113 flags&SIZEINT ? va_arg(ap, size_t) : \ 1114 flags&QUADINT ? va_arg(ap, u_quad_t) : \ 1115 flags&LONGINT ? va_arg(ap, u_long) : \ 1116 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \ 1117 (u_long)va_arg(ap, u_int)) 1118 1119 #define KPRINTF_PUTCHAR(C) { \ 1120 if (oflags == TOBUFONLY) { \ 1121 if (sbuf && ((vp == NULL) || (sbuf < tailp))) \ 1122 *sbuf++ = (C); \ 1123 } else { \ 1124 putchar((C), oflags, vp); \ 1125 } \ 1126 } 1127 1128 void 1129 device_printf(device_t dev, const char *fmt, ...) 1130 { 1131 va_list ap; 1132 1133 va_start(ap, fmt); 1134 printf("%s: ", device_xname(dev)); 1135 vprintf(fmt, ap); 1136 va_end(ap); 1137 return; 1138 } 1139 1140 /* 1141 * Guts of kernel printf. Note, we already expect to be in a mutex! 1142 */ 1143 int 1144 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap) 1145 { 1146 const char *fmt; /* format string */ 1147 int ch; /* character from fmt */ 1148 int n; /* handy integer (short term usage) */ 1149 char *cp; /* handy char pointer (short term usage) */ 1150 int flags; /* flags as above */ 1151 int ret; /* return value accumulator */ 1152 int width; /* width from format (%8d), or 0 */ 1153 int prec; /* precision from format (%.3d), or -1 */ 1154 char sign; /* sign prefix (' ', '+', '-', or \0) */ 1155 1156 u_quad_t _uquad; /* integer arguments %[diouxX] */ 1157 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */ 1158 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 1159 int realsz; /* field size expanded by dprec */ 1160 int size; /* size of converted field or string */ 1161 const char *xdigs; /* digits for [xX] conversion */ 1162 char bf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */ 1163 char *tailp; /* tail pointer for snprintf */ 1164 1165 if (oflags == TOBUFONLY && (vp != NULL)) 1166 tailp = *(char **)vp; 1167 else 1168 tailp = NULL; 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 for (; *fmt != '%' && *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 for (; size--; cp++) 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 return ret; 1512 } 1513