1 /* $NetBSD: subr_prf.c,v 1.153 2014/03/26 18:03:47 christos 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.153 2014/03/26 18:03:47 christos 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 * snprintf: print a message to a buffer 999 */ 1000 int 1001 snprintf(char *bf, size_t size, const char *fmt, ...) 1002 { 1003 int retval; 1004 va_list ap; 1005 1006 va_start(ap, fmt); 1007 retval = vsnprintf(bf, size, fmt, ap); 1008 va_end(ap); 1009 1010 return retval; 1011 } 1012 1013 /* 1014 * vsnprintf: print a message to a buffer [already have va_list] 1015 */ 1016 int 1017 vsnprintf(char *bf, size_t size, const char *fmt, va_list ap) 1018 { 1019 int retval; 1020 char *p; 1021 1022 p = bf + size; 1023 retval = kprintf(fmt, TOBUFONLY, &p, bf, ap); 1024 if (bf && size > 0) { 1025 /* nul terminate */ 1026 if (size <= (size_t)retval) 1027 bf[size - 1] = '\0'; 1028 else 1029 bf[retval] = '\0'; 1030 } 1031 return retval; 1032 } 1033 1034 /* 1035 * kprintf: scaled down version of printf(3). 1036 * 1037 * this version based on vfprintf() from libc which was derived from 1038 * software contributed to Berkeley by Chris Torek. 1039 * 1040 * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS! 1041 */ 1042 1043 /* 1044 * macros for converting digits to letters and vice versa 1045 */ 1046 #define to_digit(c) ((c) - '0') 1047 #define is_digit(c) ((unsigned)to_digit(c) <= 9) 1048 #define to_char(n) ((n) + '0') 1049 1050 /* 1051 * flags used during conversion. 1052 */ 1053 #define ALT 0x001 /* alternate form */ 1054 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */ 1055 #define LADJUST 0x004 /* left adjustment */ 1056 #define LONGDBL 0x008 /* long double; unimplemented */ 1057 #define LONGINT 0x010 /* long integer */ 1058 #define QUADINT 0x020 /* quad integer */ 1059 #define SHORTINT 0x040 /* short integer */ 1060 #define MAXINT 0x080 /* intmax_t */ 1061 #define PTRINT 0x100 /* intptr_t */ 1062 #define SIZEINT 0x200 /* size_t */ 1063 #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */ 1064 #define FPT 0x800 /* Floating point number */ 1065 1066 /* 1067 * To extend shorts properly, we need both signed and unsigned 1068 * argument extraction methods. 1069 */ 1070 #define SARG() \ 1071 (flags&MAXINT ? va_arg(ap, intmax_t) : \ 1072 flags&PTRINT ? va_arg(ap, intptr_t) : \ 1073 flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \ 1074 flags&QUADINT ? va_arg(ap, quad_t) : \ 1075 flags&LONGINT ? va_arg(ap, long) : \ 1076 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \ 1077 (long)va_arg(ap, int)) 1078 #define UARG() \ 1079 (flags&MAXINT ? va_arg(ap, uintmax_t) : \ 1080 flags&PTRINT ? va_arg(ap, uintptr_t) : \ 1081 flags&SIZEINT ? va_arg(ap, size_t) : \ 1082 flags&QUADINT ? va_arg(ap, u_quad_t) : \ 1083 flags&LONGINT ? va_arg(ap, u_long) : \ 1084 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \ 1085 (u_long)va_arg(ap, u_int)) 1086 1087 #define KPRINTF_PUTCHAR(C) { \ 1088 if (oflags == TOBUFONLY) { \ 1089 if (sbuf && ((vp == NULL) || (sbuf < tailp))) \ 1090 *sbuf++ = (C); \ 1091 } else { \ 1092 putchar((C), oflags, vp); \ 1093 } \ 1094 } 1095 1096 void 1097 device_printf(device_t dev, const char *fmt, ...) 1098 { 1099 va_list ap; 1100 1101 va_start(ap, fmt); 1102 printf("%s: ", device_xname(dev)); 1103 vprintf(fmt, ap); 1104 va_end(ap); 1105 return; 1106 } 1107 1108 /* 1109 * Guts of kernel printf. Note, we already expect to be in a mutex! 1110 */ 1111 int 1112 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap) 1113 { 1114 const char *fmt; /* format string */ 1115 int ch; /* character from fmt */ 1116 int n; /* handy integer (short term usage) */ 1117 char *cp; /* handy char pointer (short term usage) */ 1118 int flags; /* flags as above */ 1119 int ret; /* return value accumulator */ 1120 int width; /* width from format (%8d), or 0 */ 1121 int prec; /* precision from format (%.3d), or -1 */ 1122 char sign; /* sign prefix (' ', '+', '-', or \0) */ 1123 1124 u_quad_t _uquad; /* integer arguments %[diouxX] */ 1125 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */ 1126 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 1127 int realsz; /* field size expanded by dprec */ 1128 int size; /* size of converted field or string */ 1129 const char *xdigs; /* digits for [xX] conversion */ 1130 char bf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */ 1131 char *tailp; /* tail pointer for snprintf */ 1132 1133 if (oflags == TOBUFONLY && (vp != NULL)) 1134 tailp = *(char **)vp; 1135 else 1136 tailp = NULL; 1137 1138 cp = NULL; /* XXX: shutup gcc */ 1139 size = 0; /* XXX: shutup gcc */ 1140 1141 fmt = fmt0; 1142 ret = 0; 1143 1144 xdigs = NULL; /* XXX: shut up gcc warning */ 1145 1146 /* 1147 * Scan the format for conversions (`%' character). 1148 */ 1149 for (;;) { 1150 for (; *fmt != '%' && *fmt; fmt++) { 1151 ret++; 1152 KPRINTF_PUTCHAR(*fmt); 1153 } 1154 if (*fmt == 0) 1155 goto done; 1156 1157 fmt++; /* skip over '%' */ 1158 1159 flags = 0; 1160 dprec = 0; 1161 width = 0; 1162 prec = -1; 1163 sign = '\0'; 1164 1165 rflag: ch = *fmt++; 1166 reswitch: switch (ch) { 1167 case ' ': 1168 /* 1169 * ``If the space and + flags both appear, the space 1170 * flag will be ignored.'' 1171 * -- ANSI X3J11 1172 */ 1173 if (!sign) 1174 sign = ' '; 1175 goto rflag; 1176 case '#': 1177 flags |= ALT; 1178 goto rflag; 1179 case '*': 1180 /* 1181 * ``A negative field width argument is taken as a 1182 * - flag followed by a positive field width.'' 1183 * -- ANSI X3J11 1184 * They don't exclude field widths read from args. 1185 */ 1186 if ((width = va_arg(ap, int)) >= 0) 1187 goto rflag; 1188 width = -width; 1189 /* FALLTHROUGH */ 1190 case '-': 1191 flags |= LADJUST; 1192 goto rflag; 1193 case '+': 1194 sign = '+'; 1195 goto rflag; 1196 case '.': 1197 if ((ch = *fmt++) == '*') { 1198 n = va_arg(ap, int); 1199 prec = n < 0 ? -1 : n; 1200 goto rflag; 1201 } 1202 n = 0; 1203 while (is_digit(ch)) { 1204 n = 10 * n + to_digit(ch); 1205 ch = *fmt++; 1206 } 1207 prec = n < 0 ? -1 : n; 1208 goto reswitch; 1209 case '0': 1210 /* 1211 * ``Note that 0 is taken as a flag, not as the 1212 * beginning of a field width.'' 1213 * -- ANSI X3J11 1214 */ 1215 flags |= ZEROPAD; 1216 goto rflag; 1217 case '1': case '2': case '3': case '4': 1218 case '5': case '6': case '7': case '8': case '9': 1219 n = 0; 1220 do { 1221 n = 10 * n + to_digit(ch); 1222 ch = *fmt++; 1223 } while (is_digit(ch)); 1224 width = n; 1225 goto reswitch; 1226 case 'h': 1227 flags |= SHORTINT; 1228 goto rflag; 1229 case 'j': 1230 flags |= MAXINT; 1231 goto rflag; 1232 case 'l': 1233 if (*fmt == 'l') { 1234 fmt++; 1235 flags |= QUADINT; 1236 } else { 1237 flags |= LONGINT; 1238 } 1239 goto rflag; 1240 case 'q': 1241 flags |= QUADINT; 1242 goto rflag; 1243 case 't': 1244 flags |= PTRINT; 1245 goto rflag; 1246 case 'z': 1247 flags |= SIZEINT; 1248 goto rflag; 1249 case 'c': 1250 *(cp = bf) = va_arg(ap, int); 1251 size = 1; 1252 sign = '\0'; 1253 break; 1254 case 'D': 1255 flags |= LONGINT; 1256 /*FALLTHROUGH*/ 1257 case 'd': 1258 case 'i': 1259 _uquad = SARG(); 1260 if ((quad_t)_uquad < 0) { 1261 _uquad = -_uquad; 1262 sign = '-'; 1263 } 1264 base = DEC; 1265 goto number; 1266 case 'n': 1267 if (flags & MAXINT) 1268 *va_arg(ap, intmax_t *) = ret; 1269 else if (flags & PTRINT) 1270 *va_arg(ap, intptr_t *) = ret; 1271 else if (flags & SIZEINT) 1272 *va_arg(ap, ssize_t *) = ret; 1273 else if (flags & QUADINT) 1274 *va_arg(ap, quad_t *) = ret; 1275 else if (flags & LONGINT) 1276 *va_arg(ap, long *) = ret; 1277 else if (flags & SHORTINT) 1278 *va_arg(ap, short *) = ret; 1279 else 1280 *va_arg(ap, int *) = ret; 1281 continue; /* no output */ 1282 case 'O': 1283 flags |= LONGINT; 1284 /*FALLTHROUGH*/ 1285 case 'o': 1286 _uquad = UARG(); 1287 base = OCT; 1288 goto nosign; 1289 case 'p': 1290 /* 1291 * ``The argument shall be a pointer to void. The 1292 * value of the pointer is converted to a sequence 1293 * of printable characters, in an implementation- 1294 * defined manner.'' 1295 * -- ANSI X3J11 1296 */ 1297 /* NOSTRICT */ 1298 _uquad = (u_long)va_arg(ap, void *); 1299 base = HEX; 1300 xdigs = hexdigits; 1301 flags |= HEXPREFIX; 1302 ch = 'x'; 1303 goto nosign; 1304 case 's': 1305 if ((cp = va_arg(ap, char *)) == NULL) 1306 /*XXXUNCONST*/ 1307 cp = __UNCONST("(null)"); 1308 if (prec >= 0) { 1309 /* 1310 * can't use strlen; can only look for the 1311 * NUL in the first `prec' characters, and 1312 * strlen() will go further. 1313 */ 1314 char *p = memchr(cp, 0, prec); 1315 1316 if (p != NULL) { 1317 size = p - cp; 1318 if (size > prec) 1319 size = prec; 1320 } else 1321 size = prec; 1322 } else 1323 size = strlen(cp); 1324 sign = '\0'; 1325 break; 1326 case 'U': 1327 flags |= LONGINT; 1328 /*FALLTHROUGH*/ 1329 case 'u': 1330 _uquad = UARG(); 1331 base = DEC; 1332 goto nosign; 1333 case 'X': 1334 xdigs = HEXDIGITS; 1335 goto hex; 1336 case 'x': 1337 xdigs = hexdigits; 1338 hex: _uquad = UARG(); 1339 base = HEX; 1340 /* leading 0x/X only if non-zero */ 1341 if (flags & ALT && _uquad != 0) 1342 flags |= HEXPREFIX; 1343 1344 /* unsigned conversions */ 1345 nosign: sign = '\0'; 1346 /* 1347 * ``... diouXx conversions ... if a precision is 1348 * specified, the 0 flag will be ignored.'' 1349 * -- ANSI X3J11 1350 */ 1351 number: if ((dprec = prec) >= 0) 1352 flags &= ~ZEROPAD; 1353 1354 /* 1355 * ``The result of converting a zero value with an 1356 * explicit precision of zero is no characters.'' 1357 * -- ANSI X3J11 1358 */ 1359 cp = bf + KPRINTF_BUFSIZE; 1360 if (_uquad != 0 || prec != 0) { 1361 /* 1362 * Unsigned mod is hard, and unsigned mod 1363 * by a constant is easier than that by 1364 * a variable; hence this switch. 1365 */ 1366 switch (base) { 1367 case OCT: 1368 do { 1369 *--cp = to_char(_uquad & 7); 1370 _uquad >>= 3; 1371 } while (_uquad); 1372 /* handle octal leading 0 */ 1373 if (flags & ALT && *cp != '0') 1374 *--cp = '0'; 1375 break; 1376 1377 case DEC: 1378 /* many numbers are 1 digit */ 1379 while (_uquad >= 10) { 1380 *--cp = to_char(_uquad % 10); 1381 _uquad /= 10; 1382 } 1383 *--cp = to_char(_uquad); 1384 break; 1385 1386 case HEX: 1387 do { 1388 *--cp = xdigs[_uquad & 15]; 1389 _uquad >>= 4; 1390 } while (_uquad); 1391 break; 1392 1393 default: 1394 /*XXXUNCONST*/ 1395 cp = __UNCONST("bug in kprintf: bad base"); 1396 size = strlen(cp); 1397 goto skipsize; 1398 } 1399 } 1400 size = bf + KPRINTF_BUFSIZE - cp; 1401 skipsize: 1402 break; 1403 default: /* "%?" prints ?, unless ? is NUL */ 1404 if (ch == '\0') 1405 goto done; 1406 /* pretend it was %c with argument ch */ 1407 cp = bf; 1408 *cp = ch; 1409 size = 1; 1410 sign = '\0'; 1411 break; 1412 } 1413 1414 /* 1415 * All reasonable formats wind up here. At this point, `cp' 1416 * points to a string which (if not flags&LADJUST) should be 1417 * padded out to `width' places. If flags&ZEROPAD, it should 1418 * first be prefixed by any sign or other prefix; otherwise, 1419 * it should be blank padded before the prefix is emitted. 1420 * After any left-hand padding and prefixing, emit zeroes 1421 * required by a decimal [diouxX] precision, then print the 1422 * string proper, then emit zeroes required by any leftover 1423 * floating precision; finally, if LADJUST, pad with blanks. 1424 * 1425 * Compute actual size, so we know how much to pad. 1426 * size excludes decimal prec; realsz includes it. 1427 */ 1428 realsz = dprec > size ? dprec : size; 1429 if (sign) 1430 realsz++; 1431 else if (flags & HEXPREFIX) 1432 realsz+= 2; 1433 1434 /* adjust ret */ 1435 ret += width > realsz ? width : realsz; 1436 1437 /* right-adjusting blank padding */ 1438 if ((flags & (LADJUST|ZEROPAD)) == 0) { 1439 n = width - realsz; 1440 while (n-- > 0) 1441 KPRINTF_PUTCHAR(' '); 1442 } 1443 1444 /* prefix */ 1445 if (sign) { 1446 KPRINTF_PUTCHAR(sign); 1447 } else if (flags & HEXPREFIX) { 1448 KPRINTF_PUTCHAR('0'); 1449 KPRINTF_PUTCHAR(ch); 1450 } 1451 1452 /* right-adjusting zero padding */ 1453 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) { 1454 n = width - realsz; 1455 while (n-- > 0) 1456 KPRINTF_PUTCHAR('0'); 1457 } 1458 1459 /* leading zeroes from decimal precision */ 1460 n = dprec - size; 1461 while (n-- > 0) 1462 KPRINTF_PUTCHAR('0'); 1463 1464 /* the string or number proper */ 1465 for (; size--; cp++) 1466 KPRINTF_PUTCHAR(*cp); 1467 /* left-adjusting padding (always blank) */ 1468 if (flags & LADJUST) { 1469 n = width - realsz; 1470 while (n-- > 0) 1471 KPRINTF_PUTCHAR(' '); 1472 } 1473 } 1474 1475 done: 1476 if ((oflags == TOBUFONLY) && (vp != NULL)) 1477 *(char **)vp = sbuf; 1478 (*v_flush)(); 1479 return ret; 1480 } 1481