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