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