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