1 /* $NetBSD: subr_prf.c,v 1.201 2023/07/17 22:57:35 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.201 2023/07/17 22:57:35 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
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 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
log(int level,const char * fmt,...)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
vlog(int level,const char * fmt,va_list ap)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
logpri(int level)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
klogpri(int level)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
addlog(const char * fmt,...)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
putone(int c,int flags,struct tty * tp)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
putlogpri(int level)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
addtstamp(int flags,struct tty * tp)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
putchar(int c,int flags,struct tty * tp)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
tablefull(const char * tab,const char * hint)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
uprintf(const char * fmt,...)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
uprintf_locked(const char * fmt,...)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
tprintf_open(struct proc * p)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
tprintf_close(tpr_t sess)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
tprintf(tpr_t tpr,const char * fmt,...)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
ttyprintf(struct tty * tp,const char * fmt,...)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
db_printf(const char * fmt,...)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
db_vprintf(const char * fmt,va_list ap)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
kprintf_internal(const char * fmt,int oflags,void * vp,char * sbuf,...)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
aprint_normal_internal(const char * prefix,const char * fmt,va_list ap)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
aprint_normal(const char * fmt,...)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
aprint_normal_dev(device_t dv,const char * fmt,...)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
aprint_normal_ifnet(struct ifnet * ifp,const char * fmt,...)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
aprint_get_error_count(void)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
aprint_error_internal(const char * prefix,const char * fmt,va_list ap)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
aprint_error(const char * fmt,...)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
aprint_error_dev(device_t dv,const char * fmt,...)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
aprint_error_ifnet(struct ifnet * ifp,const char * fmt,...)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
aprint_naive_internal(const char * prefix,const char * fmt,va_list ap)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
aprint_naive(const char * fmt,...)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
aprint_naive_dev(device_t dv,const char * fmt,...)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
aprint_naive_ifnet(struct ifnet * ifp,const char * fmt,...)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
aprint_verbose_internal(const char * prefix,const char * fmt,va_list ap)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
aprint_verbose(const char * fmt,...)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
aprint_verbose_dev(device_t dv,const char * fmt,...)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
aprint_verbose_ifnet(struct ifnet * ifp,const char * fmt,...)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
aprint_debug_internal(const char * prefix,const char * fmt,va_list ap)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
aprint_debug(const char * fmt,...)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
aprint_debug_dev(device_t dv,const char * fmt,...)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
aprint_debug_ifnet(struct ifnet * ifp,const char * fmt,...)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
vprintf_flags(int flags,const char * fmt,va_list ap)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
printf_flags(int flags,const char * fmt,...)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
printf_tolog(const char * fmt,...)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
printf_nolog(const char * fmt,...)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
printf_nostamp(const char * fmt,...)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
printf(const char * fmt,...)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
vprintf(const char * fmt,va_list ap)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
snprintf(char * bf,size_t size,const char * fmt,...)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
vsnprintf(char * bf,size_t size,const char * fmt,va_list ap)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
vasprintf(char ** bf,const char * fmt,va_list ap)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
device_printf(device_t dev,const char * fmt,...)1240 device_printf(device_t dev, const char *fmt, ...)
1241 {
1242 va_list ap;
1243
1244 kprintf_lock();
1245 kprintf_internal("%s: ", TOCONS|TOLOG, NULL, NULL, device_xname(dev));
1246 va_start(ap, fmt);
1247 kprintf(fmt, TOCONS|TOLOG, NULL, NULL, ap);
1248 va_end(ap);
1249 kprintf_unlock();
1250 }
1251
1252 /*
1253 * Guts of kernel printf. Note, we already expect to be in a mutex!
1254 */
1255 int
kprintf(const char * fmt0,int oflags,void * vp,char * sbuf,va_list ap)1256 kprintf(const char *fmt0, int oflags, void *vp, char *sbuf, va_list ap)
1257 {
1258 const char *fmt; /* format string */
1259 int ch; /* character from fmt */
1260 int n; /* handy integer (short term usage) */
1261 char *cp; /* handy char pointer (short term usage) */
1262 int flags; /* flags as above */
1263 int ret; /* return value accumulator */
1264 int width; /* width from format (%8d), or 0 */
1265 int prec; /* precision from format (%.3d), or -1 */
1266 char sign; /* sign prefix (' ', '+', '-', or \0) */
1267
1268 u_quad_t _uquad; /* integer arguments %[diouxX] */
1269 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
1270 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
1271 int realsz; /* field size expanded by dprec */
1272 int size; /* size of converted field or string */
1273 const char *xdigs; /* digits for [xX] conversion */
1274 char bf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
1275 char *tailp; /* tail pointer for snprintf */
1276
1277 if (oflags == TOBUFONLY && (vp != NULL))
1278 tailp = *(char **)vp;
1279 else
1280 tailp = NULL;
1281
1282 cp = NULL; /* XXX: shutup gcc */
1283 size = 0; /* XXX: shutup gcc */
1284
1285 fmt = fmt0;
1286 ret = 0;
1287
1288 xdigs = NULL; /* XXX: shut up gcc warning */
1289
1290 /*
1291 * Scan the format for conversions (`%' character).
1292 */
1293 for (;;) {
1294 for (; *fmt != '%' && *fmt; fmt++) {
1295 ret++;
1296 KPRINTF_PUTCHAR(*fmt);
1297 }
1298 if (*fmt == 0)
1299 goto done;
1300
1301 fmt++; /* skip over '%' */
1302
1303 flags = 0;
1304 dprec = 0;
1305 width = 0;
1306 prec = -1;
1307 sign = '\0';
1308
1309 rflag: ch = *fmt++;
1310 reswitch: switch (ch) {
1311 case ' ':
1312 /*
1313 * ``If the space and + flags both appear, the space
1314 * flag will be ignored.''
1315 * -- ANSI X3J11
1316 */
1317 if (!sign)
1318 sign = ' ';
1319 goto rflag;
1320 case '#':
1321 flags |= ALT;
1322 goto rflag;
1323 case '*':
1324 /*
1325 * ``A negative field width argument is taken as a
1326 * - flag followed by a positive field width.''
1327 * -- ANSI X3J11
1328 * They don't exclude field widths read from args.
1329 */
1330 if ((width = va_arg(ap, int)) >= 0)
1331 goto rflag;
1332 width = -width;
1333 /* FALLTHROUGH */
1334 case '-':
1335 flags |= LADJUST;
1336 goto rflag;
1337 case '+':
1338 sign = '+';
1339 goto rflag;
1340 case '.':
1341 if ((ch = *fmt++) == '*') {
1342 n = va_arg(ap, int);
1343 prec = n < 0 ? -1 : n;
1344 goto rflag;
1345 }
1346 n = 0;
1347 while (is_digit(ch)) {
1348 n = 10 * n + to_digit(ch);
1349 ch = *fmt++;
1350 }
1351 prec = n < 0 ? -1 : n;
1352 goto reswitch;
1353 case '0':
1354 /*
1355 * ``Note that 0 is taken as a flag, not as the
1356 * beginning of a field width.''
1357 * -- ANSI X3J11
1358 */
1359 flags |= ZEROPAD;
1360 goto rflag;
1361 case '1': case '2': case '3': case '4':
1362 case '5': case '6': case '7': case '8': case '9':
1363 n = 0;
1364 do {
1365 n = 10 * n + to_digit(ch);
1366 ch = *fmt++;
1367 } while (is_digit(ch));
1368 width = n;
1369 goto reswitch;
1370 case 'h':
1371 flags |= SHORTINT;
1372 goto rflag;
1373 case 'j':
1374 flags |= MAXINT;
1375 goto rflag;
1376 case 'l':
1377 if (*fmt == 'l') {
1378 fmt++;
1379 flags |= QUADINT;
1380 } else {
1381 flags |= LONGINT;
1382 }
1383 goto rflag;
1384 case 'q':
1385 flags |= QUADINT;
1386 goto rflag;
1387 case 't':
1388 flags |= PTRINT;
1389 goto rflag;
1390 case 'z':
1391 flags |= SIZEINT;
1392 goto rflag;
1393 case 'c':
1394 *(cp = bf) = va_arg(ap, int);
1395 size = 1;
1396 sign = '\0';
1397 break;
1398 case 'D':
1399 flags |= LONGINT;
1400 /*FALLTHROUGH*/
1401 case 'd':
1402 case 'i':
1403 _uquad = SARG();
1404 if ((quad_t)_uquad < 0) {
1405 _uquad = -_uquad;
1406 sign = '-';
1407 }
1408 base = DEC;
1409 goto number;
1410 case 'n':
1411 /* no %n support in the kernel, consume and skip */
1412 if (flags & MAXINT)
1413 (void)va_arg(ap, intmax_t *);
1414 else if (flags & PTRINT)
1415 (void)va_arg(ap, intptr_t *);
1416 else if (flags & SIZEINT)
1417 (void)va_arg(ap, ssize_t *);
1418 else if (flags & QUADINT)
1419 (void)va_arg(ap, quad_t *);
1420 else if (flags & LONGINT)
1421 (void)va_arg(ap, long *);
1422 else if (flags & SHORTINT)
1423 (void)va_arg(ap, short *);
1424 else
1425 (void)va_arg(ap, int *);
1426 continue; /* no output */
1427 case 'O':
1428 flags |= LONGINT;
1429 /*FALLTHROUGH*/
1430 case 'o':
1431 _uquad = UARG();
1432 base = OCT;
1433 goto nosign;
1434 case 'p':
1435 /*
1436 * ``The argument shall be a pointer to void. The
1437 * value of the pointer is converted to a sequence
1438 * of printable characters, in an implementation-
1439 * defined manner.''
1440 * -- ANSI X3J11
1441 */
1442 /* NOSTRICT */
1443 _uquad = (u_long)va_arg(ap, void *);
1444 base = HEX;
1445 xdigs = hexdigits;
1446 flags |= HEXPREFIX;
1447 ch = 'x';
1448 goto nosign;
1449 case 's':
1450 if ((cp = va_arg(ap, char *)) == NULL)
1451 /*XXXUNCONST*/
1452 cp = __UNCONST("(null)");
1453 if (prec >= 0) {
1454 /*
1455 * can't use strlen; can only look for the
1456 * NUL in the first `prec' characters, and
1457 * strlen() will go further.
1458 */
1459 char *p = memchr(cp, 0, prec);
1460
1461 if (p != NULL) {
1462 size = p - cp;
1463 if (size > prec)
1464 size = prec;
1465 } else
1466 size = prec;
1467 } else
1468 size = strlen(cp);
1469 sign = '\0';
1470 break;
1471 case 'U':
1472 flags |= LONGINT;
1473 /*FALLTHROUGH*/
1474 case 'u':
1475 _uquad = UARG();
1476 base = DEC;
1477 goto nosign;
1478 case 'X':
1479 xdigs = HEXDIGITS;
1480 goto hex;
1481 case 'x':
1482 xdigs = hexdigits;
1483 hex: _uquad = UARG();
1484 base = HEX;
1485 /* leading 0x/X only if non-zero */
1486 if (flags & ALT && _uquad != 0)
1487 flags |= HEXPREFIX;
1488
1489 /* unsigned conversions */
1490 nosign: sign = '\0';
1491 /*
1492 * ``... diouXx conversions ... if a precision is
1493 * specified, the 0 flag will be ignored.''
1494 * -- ANSI X3J11
1495 */
1496 number: if ((dprec = prec) >= 0)
1497 flags &= ~ZEROPAD;
1498
1499 /*
1500 * ``The result of converting a zero value with an
1501 * explicit precision of zero is no characters.''
1502 * -- ANSI X3J11
1503 */
1504 cp = bf + KPRINTF_BUFSIZE;
1505 if (_uquad != 0 || prec != 0) {
1506 /*
1507 * Unsigned mod is hard, and unsigned mod
1508 * by a constant is easier than that by
1509 * a variable; hence this switch.
1510 */
1511 switch (base) {
1512 case OCT:
1513 do {
1514 *--cp = to_char(_uquad & 7);
1515 _uquad >>= 3;
1516 } while (_uquad);
1517 /* handle octal leading 0 */
1518 if (flags & ALT && *cp != '0')
1519 *--cp = '0';
1520 break;
1521
1522 case DEC:
1523 /* many numbers are 1 digit */
1524 while (_uquad >= 10) {
1525 *--cp = to_char(_uquad % 10);
1526 _uquad /= 10;
1527 }
1528 *--cp = to_char(_uquad);
1529 break;
1530
1531 case HEX:
1532 do {
1533 *--cp = xdigs[_uquad & 15];
1534 _uquad >>= 4;
1535 } while (_uquad);
1536 break;
1537
1538 default:
1539 /*XXXUNCONST*/
1540 cp = __UNCONST("bug in kprintf: bad base");
1541 size = strlen(cp);
1542 goto skipsize;
1543 }
1544 }
1545 size = bf + KPRINTF_BUFSIZE - cp;
1546 skipsize:
1547 break;
1548 default: /* "%?" prints ?, unless ? is NUL */
1549 if (ch == '\0')
1550 goto done;
1551 /* pretend it was %c with argument ch */
1552 cp = bf;
1553 *cp = ch;
1554 size = 1;
1555 sign = '\0';
1556 break;
1557 }
1558
1559 /*
1560 * All reasonable formats wind up here. At this point, `cp'
1561 * points to a string which (if not flags&LADJUST) should be
1562 * padded out to `width' places. If flags&ZEROPAD, it should
1563 * first be prefixed by any sign or other prefix; otherwise,
1564 * it should be blank padded before the prefix is emitted.
1565 * After any left-hand padding and prefixing, emit zeroes
1566 * required by a decimal [diouxX] precision, then print the
1567 * string proper, then emit zeroes required by any leftover
1568 * floating precision; finally, if LADJUST, pad with blanks.
1569 *
1570 * Compute actual size, so we know how much to pad.
1571 * size excludes decimal prec; realsz includes it.
1572 */
1573 realsz = dprec > size ? dprec : size;
1574 if (sign)
1575 realsz++;
1576 else if (flags & HEXPREFIX)
1577 realsz+= 2;
1578
1579 /* adjust ret */
1580 ret += width > realsz ? width : realsz;
1581
1582 /* right-adjusting blank padding */
1583 if ((flags & (LADJUST|ZEROPAD)) == 0) {
1584 n = width - realsz;
1585 while (n-- > 0)
1586 KPRINTF_PUTCHAR(' ');
1587 }
1588
1589 /* prefix */
1590 if (sign) {
1591 KPRINTF_PUTCHAR(sign);
1592 } else if (flags & HEXPREFIX) {
1593 KPRINTF_PUTCHAR('0');
1594 KPRINTF_PUTCHAR(ch);
1595 }
1596
1597 /* right-adjusting zero padding */
1598 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1599 n = width - realsz;
1600 while (n-- > 0)
1601 KPRINTF_PUTCHAR('0');
1602 }
1603
1604 /* leading zeroes from decimal precision */
1605 n = dprec - size;
1606 while (n-- > 0)
1607 KPRINTF_PUTCHAR('0');
1608
1609 /* the string or number proper */
1610 for (; size--; cp++)
1611 KPRINTF_PUTCHAR(*cp);
1612 /* left-adjusting padding (always blank) */
1613 if (flags & LADJUST) {
1614 n = width - realsz;
1615 while (n-- > 0)
1616 KPRINTF_PUTCHAR(' ');
1617 }
1618 }
1619
1620 done:
1621 if ((oflags == TOBUFONLY) && (vp != NULL))
1622 *(char **)vp = sbuf;
1623 (*v_flush)();
1624
1625 #ifdef RND_PRINTF
1626 if (__predict_true(kprintf_inited))
1627 rnd_add_data(&rnd_printf_source, NULL, 0, 0);
1628 #endif
1629 return ret;
1630 }
1631