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