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