subr_prf.c revision 1.87 1 /* $NetBSD: subr_prf.c,v 1.87 2002/12/31 16:53:27 thorpej 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)subr_prf.c 8.4 (Berkeley) 5/4/95
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: subr_prf.c,v 1.87 2002/12/31 16:53:27 thorpej Exp $");
45
46 #include "opt_ddb.h"
47 #include "opt_ipkdb.h"
48 #include "opt_kgdb.h"
49 #include "opt_multiprocessor.h"
50
51 #include <sys/param.h>
52 #include <sys/stdint.h>
53 #include <sys/systm.h>
54 #include <sys/buf.h>
55 #include <sys/reboot.h>
56 #include <sys/msgbuf.h>
57 #include <sys/proc.h>
58 #include <sys/ioctl.h>
59 #include <sys/vnode.h>
60 #include <sys/file.h>
61 #include <sys/tty.h>
62 #include <sys/tprintf.h>
63 #include <sys/syslog.h>
64 #include <sys/malloc.h>
65 #include <sys/lock.h>
66 #include <sys/kprintf.h>
67
68 #include <dev/cons.h>
69
70 #ifdef DDB
71 #include <ddb/ddbvar.h>
72 #include <machine/db_machdep.h>
73 #include <ddb/db_command.h>
74 #include <ddb/db_interface.h>
75 #endif
76
77 #ifdef IPKDB
78 #include <ipkdb/ipkdb.h>
79 #endif
80
81 #if defined(MULTIPROCESSOR)
82 struct simplelock kprintf_slock = SIMPLELOCK_INITIALIZER;
83 #endif /* MULTIPROCESSOR */
84
85 /*
86 * note that stdarg.h and the ansi style va_start macro is used for both
87 * ansi and traditional c complers.
88 * XXX: this requires that stdarg.h define: va_alist and va_dcl
89 */
90 #include <machine/stdarg.h>
91
92
93 #ifdef KGDB
94 #include <sys/kgdb.h>
95 #include <machine/cpu.h>
96 #endif
97 #ifdef DDB
98 #include <ddb/db_output.h> /* db_printf, db_putchar prototypes */
99 #endif
100
101
102 /*
103 * defines
104 */
105
106 /* max size buffer kprintf needs to print quad_t [size in base 8 + \0] */
107 #define KPRINTF_BUFSIZE (sizeof(quad_t) * NBBY / 3 + 2)
108
109
110 /*
111 * local prototypes
112 */
113
114 static void putchar __P((int, int, struct tty *));
115
116
117 /*
118 * globals
119 */
120
121 extern struct tty *constty; /* pointer to console "window" tty */
122 extern int log_open; /* subr_log: is /dev/klog open? */
123 const char *panicstr; /* arg to first call to panic (used as a flag
124 to indicate that panic has already been called). */
125 long panicstart, panicend; /* position in the msgbuf of the start and
126 end of the formatted panicstr. */
127 int doing_shutdown; /* set to indicate shutdown in progress */
128
129 /*
130 * v_putc: routine to putc on virtual console
131 *
132 * the v_putc pointer can be used to redirect the console cnputc elsewhere
133 * [e.g. to a "virtual console"].
134 */
135
136 void (*v_putc) __P((int)) = cnputc; /* start with cnputc (normal cons) */
137
138
139 /*
140 * functions
141 */
142
143 /*
144 * tablefull: warn that a system table is full
145 */
146
147 void
148 tablefull(tab, hint)
149 const char *tab, *hint;
150 {
151 if (hint)
152 log(LOG_ERR, "%s: table is full - %s\n", tab, hint);
153 else
154 log(LOG_ERR, "%s: table is full\n", tab);
155 }
156
157 /*
158 * panic: handle an unresolvable fatal error
159 *
160 * prints "panic: <message>" and reboots. if called twice (i.e. recursive
161 * call) we avoid trying to sync the disk and just reboot (to avoid
162 * recursive panics).
163 */
164
165 void
166 #ifdef __STDC__
167 panic(const char *fmt, ...)
168 #else
169 panic(fmt, va_alist)
170 char *fmt;
171 va_dcl
172 #endif
173 {
174 int bootopt;
175 va_list ap;
176
177 bootopt = RB_AUTOBOOT | RB_DUMP;
178 if (doing_shutdown)
179 bootopt |= RB_NOSYNC;
180 if (!panicstr)
181 panicstr = fmt;
182 doing_shutdown = 1;
183
184 if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC)
185 panicstart = msgbufp->msg_bufx;
186
187 va_start(ap, fmt);
188 printf("panic: ");
189 vprintf(fmt, ap);
190 printf("\n");
191 va_end(ap);
192
193 if (msgbufenabled && msgbufp->msg_magic == MSG_MAGIC)
194 panicend = msgbufp->msg_bufx;
195
196 #ifdef IPKDB
197 ipkdb_panic();
198 #endif
199 #ifdef KGDB
200 kgdb_panic();
201 #endif
202 #ifdef KADB
203 if (boothowto & RB_KDB)
204 kdbpanic();
205 #endif
206 #ifdef DDB
207 if (db_onpanic)
208 Debugger();
209 else {
210 static int intrace = 0;
211
212 if (intrace==0) {
213 intrace=1;
214 printf("Begin traceback...\n");
215 db_stack_trace_print(
216 (db_expr_t)(intptr_t)__builtin_frame_address(0),
217 TRUE, 65535, "", printf);
218 printf("End traceback...\n");
219 intrace=0;
220 } else
221 printf("Faulted in mid-traceback; aborting...");
222 }
223 #endif
224 cpu_reboot(bootopt, NULL);
225 }
226
227 /*
228 * kernel logging functions: log, logpri, addlog
229 */
230
231 /*
232 * log: write to the log buffer
233 *
234 * => will not sleep [so safe to call from interrupt]
235 * => will log to console if /dev/klog isn't open
236 */
237
238 void
239 #ifdef __STDC__
240 log(int level, const char *fmt, ...)
241 #else
242 log(level, fmt, va_alist)
243 int level;
244 char *fmt;
245 va_dcl
246 #endif
247 {
248 int s;
249 va_list ap;
250
251 KPRINTF_MUTEX_ENTER(s);
252
253 klogpri(level); /* log the level first */
254 va_start(ap, fmt);
255 kprintf(fmt, TOLOG, NULL, NULL, ap);
256 va_end(ap);
257 if (!log_open) {
258 va_start(ap, fmt);
259 kprintf(fmt, TOCONS, NULL, NULL, ap);
260 va_end(ap);
261 }
262
263 KPRINTF_MUTEX_EXIT(s);
264
265 logwakeup(); /* wake up anyone waiting for log msgs */
266 }
267
268 /*
269 * vlog: write to the log buffer [already have va_alist]
270 */
271
272 void
273 vlog(level, fmt, ap)
274 int level;
275 const char *fmt;
276 va_list ap;
277 {
278 int s;
279
280 KPRINTF_MUTEX_ENTER(s);
281
282 klogpri(level); /* log the level first */
283 kprintf(fmt, TOLOG, NULL, NULL, ap);
284 if (!log_open)
285 kprintf(fmt, TOCONS, NULL, NULL, ap);
286
287 KPRINTF_MUTEX_EXIT(s);
288
289 logwakeup(); /* wake up anyone waiting for log msgs */
290 }
291
292 /*
293 * logpri: log the priority level to the klog
294 */
295
296 void
297 logpri(level)
298 int level;
299 {
300 int s;
301
302 KPRINTF_MUTEX_ENTER(s);
303 klogpri(level);
304 KPRINTF_MUTEX_EXIT(s);
305 }
306
307 /*
308 * Note: we must be in the mutex here!
309 */
310 void
311 klogpri(level)
312 int level;
313 {
314 char *p;
315 char snbuf[KPRINTF_BUFSIZE];
316
317 putchar('<', TOLOG, NULL);
318 snprintf(snbuf, sizeof(snbuf), "%d", level);
319 for (p = snbuf ; *p ; p++)
320 putchar(*p, TOLOG, NULL);
321 putchar('>', TOLOG, NULL);
322 }
323
324 /*
325 * addlog: add info to previous log message
326 */
327
328 void
329 #ifdef __STDC__
330 addlog(const char *fmt, ...)
331 #else
332 addlog(fmt, va_alist)
333 char *fmt;
334 va_dcl
335 #endif
336 {
337 int s;
338 va_list ap;
339
340 KPRINTF_MUTEX_ENTER(s);
341
342 va_start(ap, fmt);
343 kprintf(fmt, TOLOG, NULL, NULL, ap);
344 va_end(ap);
345 if (!log_open) {
346 va_start(ap, fmt);
347 kprintf(fmt, TOCONS, NULL, NULL, ap);
348 va_end(ap);
349 }
350
351 KPRINTF_MUTEX_EXIT(s);
352
353 logwakeup();
354 }
355
356
357 /*
358 * putchar: print a single character on console or user terminal.
359 *
360 * => if console, then the last MSGBUFS chars are saved in msgbuf
361 * for inspection later (e.g. dmesg/syslog)
362 * => we must already be in the mutex!
363 */
364 static void
365 putchar(c, flags, tp)
366 int c;
367 int flags;
368 struct tty *tp;
369 {
370 struct kern_msgbuf *mbp;
371
372 if (panicstr)
373 constty = NULL;
374 if ((flags & TOCONS) && tp == NULL && constty) {
375 tp = constty;
376 flags |= TOTTY;
377 }
378 if ((flags & TOTTY) && tp && tputchar(c, tp) < 0 &&
379 (flags & TOCONS) && tp == constty)
380 constty = NULL;
381 if ((flags & TOLOG) &&
382 c != '\0' && c != '\r' && c != 0177 && msgbufenabled) {
383 mbp = msgbufp;
384 if (mbp->msg_magic != MSG_MAGIC) {
385 /*
386 * Arguably should panic or somehow notify the
387 * user... but how? Panic may be too drastic,
388 * and would obliterate the message being kicked
389 * out (maybe a panic itself), and printf
390 * would invoke us recursively. Silently punt
391 * for now. If syslog is running, it should
392 * notice.
393 */
394 msgbufenabled = 0;
395 } else {
396 mbp->msg_bufc[mbp->msg_bufx++] = c;
397 if (mbp->msg_bufx < 0 || mbp->msg_bufx >= mbp->msg_bufs)
398 mbp->msg_bufx = 0;
399 /* If the buffer is full, keep the most recent data. */
400 if (mbp->msg_bufr == mbp->msg_bufx) {
401 if (++mbp->msg_bufr >= mbp->msg_bufs)
402 mbp->msg_bufr = 0;
403 }
404 }
405 }
406 if ((flags & TOCONS) && constty == NULL && c != '\0')
407 (*v_putc)(c);
408 #ifdef DDB
409 if (flags & TODDB)
410 db_putchar(c);
411 #endif
412 }
413
414
415 /*
416 * uprintf: print to the controlling tty of the current process
417 *
418 * => we may block if the tty queue is full
419 * => no message is printed if the queue doesn't clear in a reasonable
420 * time
421 */
422
423 void
424 #ifdef __STDC__
425 uprintf(const char *fmt, ...)
426 #else
427 uprintf(fmt, va_alist)
428 char *fmt;
429 va_dcl
430 #endif
431 {
432 struct proc *p = curproc;
433 va_list ap;
434
435 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
436 /* No mutex needed; going to process TTY. */
437 va_start(ap, fmt);
438 kprintf(fmt, TOTTY, p->p_session->s_ttyp, NULL, ap);
439 va_end(ap);
440 }
441 }
442
443 /*
444 * tprintf functions: used to send messages to a specific process
445 *
446 * usage:
447 * get a tpr_t handle on a process "p" by using "tprintf_open(p)"
448 * use the handle when calling "tprintf"
449 * when done, do a "tprintf_close" to drop the handle
450 */
451
452 /*
453 * tprintf_open: get a tprintf handle on a process "p"
454 *
455 * => returns NULL if process can't be printed to
456 */
457
458 tpr_t
459 tprintf_open(p)
460 struct proc *p;
461 {
462
463 if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
464 SESSHOLD(p->p_session);
465 return ((tpr_t) p->p_session);
466 }
467 return ((tpr_t) NULL);
468 }
469
470 /*
471 * tprintf_close: dispose of a tprintf handle obtained with tprintf_open
472 */
473
474 void
475 tprintf_close(sess)
476 tpr_t sess;
477 {
478
479 if (sess)
480 SESSRELE((struct session *) sess);
481 }
482
483 /*
484 * tprintf: given tprintf handle to a process [obtained with tprintf_open],
485 * send a message to the controlling tty for that process.
486 *
487 * => also sends message to /dev/klog
488 */
489 void
490 #ifdef __STDC__
491 tprintf(tpr_t tpr, const char *fmt, ...)
492 #else
493 tprintf(tpr, fmt, va_alist)
494 tpr_t tpr;
495 char *fmt;
496 va_dcl
497 #endif
498 {
499 struct session *sess = (struct session *)tpr;
500 struct tty *tp = NULL;
501 int s, flags = TOLOG;
502 va_list ap;
503
504 if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
505 flags |= TOTTY;
506 tp = sess->s_ttyp;
507 }
508
509 KPRINTF_MUTEX_ENTER(s);
510
511 klogpri(LOG_INFO);
512 va_start(ap, fmt);
513 kprintf(fmt, flags, tp, NULL, ap);
514 va_end(ap);
515
516 KPRINTF_MUTEX_EXIT(s);
517
518 logwakeup();
519 }
520
521
522 /*
523 * ttyprintf: send a message to a specific tty
524 *
525 * => should be used only by tty driver or anything that knows the
526 * underlying tty will not be revoked(2)'d away. [otherwise,
527 * use tprintf]
528 */
529 void
530 #ifdef __STDC__
531 ttyprintf(struct tty *tp, const char *fmt, ...)
532 #else
533 ttyprintf(tp, fmt, va_alist)
534 struct tty *tp;
535 char *fmt;
536 va_dcl
537 #endif
538 {
539 va_list ap;
540
541 /* No mutex needed; going to process TTY. */
542 va_start(ap, fmt);
543 kprintf(fmt, TOTTY, tp, NULL, ap);
544 va_end(ap);
545 }
546
547 #ifdef DDB
548
549 /*
550 * db_printf: printf for DDB (via db_putchar)
551 */
552
553 void
554 #ifdef __STDC__
555 db_printf(const char *fmt, ...)
556 #else
557 db_printf(fmt, va_alist)
558 char *fmt;
559 va_dcl
560 #endif
561 {
562 va_list ap;
563
564 /* No mutex needed; DDB pauses all processors. */
565 va_start(ap, fmt);
566 kprintf(fmt, TODDB, NULL, NULL, ap);
567 va_end(ap);
568 }
569
570 void
571 db_vprintf(fmt, ap)
572 const char *fmt;
573 va_list ap;
574 {
575
576 /* No mutex needed; DDB pauses all processors. */
577 kprintf(fmt, TODDB, NULL, NULL, ap);
578 }
579
580 #endif /* DDB */
581
582
583 /*
584 * normal kernel printf functions: printf, vprintf, snprintf, vsnprintf
585 */
586
587 /*
588 * printf: print a message to the console and the log
589 */
590 void
591 #ifdef __STDC__
592 printf(const char *fmt, ...)
593 #else
594 printf(fmt, va_alist)
595 char *fmt;
596 va_dcl
597 #endif
598 {
599 va_list ap;
600 int s;
601
602 KPRINTF_MUTEX_ENTER(s);
603
604 va_start(ap, fmt);
605 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
606 va_end(ap);
607
608 KPRINTF_MUTEX_EXIT(s);
609
610 if (!panicstr)
611 logwakeup();
612 }
613
614 /*
615 * vprintf: print a message to the console and the log [already have
616 * va_alist]
617 */
618
619 void
620 vprintf(fmt, ap)
621 const char *fmt;
622 va_list ap;
623 {
624 int s;
625
626 KPRINTF_MUTEX_ENTER(s);
627
628 kprintf(fmt, TOCONS | TOLOG, NULL, NULL, ap);
629
630 KPRINTF_MUTEX_EXIT(s);
631
632 if (!panicstr)
633 logwakeup();
634 }
635
636 /*
637 * sprintf: print a message to a buffer
638 */
639 int
640 #ifdef __STDC__
641 sprintf(char *buf, const char *fmt, ...)
642 #else
643 sprintf(buf, fmt, va_alist)
644 char *buf;
645 const char *cfmt;
646 va_dcl
647 #endif
648 {
649 int retval;
650 va_list ap;
651
652 va_start(ap, fmt);
653 retval = kprintf(fmt, TOBUFONLY, NULL, buf, ap);
654 va_end(ap);
655 *(buf + retval) = 0; /* null terminate */
656 return(retval);
657 }
658
659 /*
660 * vsprintf: print a message to a buffer [already have va_alist]
661 */
662
663 int
664 vsprintf(buf, fmt, ap)
665 char *buf;
666 const char *fmt;
667 va_list ap;
668 {
669 int retval;
670
671 retval = kprintf(fmt, TOBUFONLY, NULL, buf, ap);
672 *(buf + retval) = 0; /* null terminate */
673 return (retval);
674 }
675
676 /*
677 * snprintf: print a message to a buffer
678 */
679 int
680 #ifdef __STDC__
681 snprintf(char *buf, size_t size, const char *fmt, ...)
682 #else
683 snprintf(buf, size, fmt, va_alist)
684 char *buf;
685 size_t size;
686 const char *cfmt;
687 va_dcl
688 #endif
689 {
690 int retval;
691 va_list ap;
692 char *p;
693
694 if (size < 1)
695 return (-1);
696 p = buf + size - 1;
697 va_start(ap, fmt);
698 retval = kprintf(fmt, TOBUFONLY, &p, buf, ap);
699 va_end(ap);
700 *(p) = 0; /* null terminate */
701 return(retval);
702 }
703
704 /*
705 * vsnprintf: print a message to a buffer [already have va_alist]
706 */
707 int
708 vsnprintf(buf, size, fmt, ap)
709 char *buf;
710 size_t size;
711 const char *fmt;
712 va_list ap;
713 {
714 int retval;
715 char *p;
716
717 if (size < 1)
718 return (-1);
719 p = buf + size - 1;
720 retval = kprintf(fmt, TOBUFONLY, &p, buf, ap);
721 *(p) = 0; /* null terminate */
722 return(retval);
723 }
724
725 /*
726 * bitmask_snprintf: print an interpreted bitmask to a buffer
727 *
728 * => returns pointer to the buffer
729 */
730 char *
731 bitmask_snprintf(val, p, buf, buflen)
732 u_quad_t val;
733 const char *p;
734 char *buf;
735 size_t buflen;
736 {
737 char *bp, *q;
738 size_t left;
739 char *sbase, snbuf[KPRINTF_BUFSIZE];
740 int base, bit, ch, len, sep;
741 u_quad_t field;
742
743 bp = buf;
744 memset(buf, 0, buflen);
745
746 /*
747 * Always leave room for the trailing NULL.
748 */
749 left = buflen - 1;
750
751 /*
752 * Print the value into the buffer. Abort if there's not
753 * enough room.
754 */
755 if (buflen < KPRINTF_BUFSIZE)
756 return (buf);
757
758 ch = *p++;
759 base = ch != '\177' ? ch : *p++;
760 sbase = base == 8 ? "%qo" : base == 10 ? "%qd" : base == 16 ? "%qx" : 0;
761 if (sbase == 0)
762 return (buf); /* punt if not oct, dec, or hex */
763
764 snprintf(snbuf, sizeof(snbuf), sbase, val);
765 for (q = snbuf ; *q ; q++) {
766 *bp++ = *q;
767 left--;
768 }
769
770 /*
771 * If the value we printed was 0 and we're using the old-style format,
772 * or if we don't have room for "<x>", we're done.
773 */
774 if (((val == 0) && (ch != '\177')) || left < 3)
775 return (buf);
776
777 #define PUTBYTE(b, c, l) do { \
778 *(b)++ = (c); \
779 if (--(l) == 0) \
780 goto out; \
781 } while (/*CONSTCOND*/ 0)
782 #define PUTSTR(b, p, l) do { \
783 int c; \
784 while ((c = *(p)++) != 0) { \
785 *(b)++ = c; \
786 if (--(l) == 0) \
787 goto out; \
788 } \
789 } while (/*CONSTCOND*/ 0)
790
791 /*
792 * Chris Torek's new bitmask format is identified by a leading \177
793 */
794 sep = '<';
795 if (ch != '\177') {
796 /* old (standard) format. */
797 for (;(bit = *p++) != 0;) {
798 if (val & (1 << (bit - 1))) {
799 PUTBYTE(bp, sep, left);
800 for (; (ch = *p) > ' '; ++p) {
801 PUTBYTE(bp, ch, left);
802 }
803 sep = ',';
804 } else
805 for (; *p > ' '; ++p)
806 continue;
807 }
808 } else {
809 /* new quad-capable format; also does fields. */
810 field = val;
811 while ((ch = *p++) != '\0') {
812 bit = *p++; /* now 0-origin */
813 switch (ch) {
814 case 'b':
815 if (((u_int)(val >> bit) & 1) == 0)
816 goto skip;
817 PUTBYTE(bp, sep, left);
818 PUTSTR(bp, p, left);
819 sep = ',';
820 break;
821 case 'f':
822 case 'F':
823 len = *p++; /* field length */
824 field = (val >> bit) & ((1ULL << len) - 1);
825 if (ch == 'F') /* just extract */
826 break;
827 PUTBYTE(bp, sep, left);
828 sep = ',';
829 PUTSTR(bp, p, left);
830 PUTBYTE(bp, '=', left);
831 sprintf(snbuf, sbase, field);
832 q = snbuf; PUTSTR(bp, q, left);
833 break;
834 case '=':
835 case ':':
836 /*
837 * Here "bit" is actually a value instead,
838 * to be compared against the last field.
839 * This only works for values in [0..255],
840 * of course.
841 */
842 if ((int)field != bit)
843 goto skip;
844 if (ch == '=')
845 PUTBYTE(bp, '=', left);
846 PUTSTR(bp, p, left);
847 break;
848 default:
849 skip:
850 while (*p++ != '\0')
851 continue;
852 break;
853 }
854 }
855 }
856 if (sep != '<')
857 PUTBYTE(bp, '>', left);
858
859 out:
860 return (buf);
861
862 #undef PUTBYTE
863 #undef PUTSTR
864 }
865
866 /*
867 * kprintf: scaled down version of printf(3).
868 *
869 * this version based on vfprintf() from libc which was derived from
870 * software contributed to Berkeley by Chris Torek.
871 *
872 * NOTE: The kprintf mutex must be held if we're going TOBUF or TOCONS!
873 */
874
875 /*
876 * macros for converting digits to letters and vice versa
877 */
878 #define to_digit(c) ((c) - '0')
879 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
880 #define to_char(n) ((n) + '0')
881
882 /*
883 * flags used during conversion.
884 */
885 #define ALT 0x001 /* alternate form */
886 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
887 #define LADJUST 0x004 /* left adjustment */
888 #define LONGDBL 0x008 /* long double; unimplemented */
889 #define LONGINT 0x010 /* long integer */
890 #define QUADINT 0x020 /* quad integer */
891 #define SHORTINT 0x040 /* short integer */
892 #define MAXINT 0x080 /* intmax_t */
893 #define PTRINT 0x100 /* intptr_t */
894 #define SIZEINT 0x200 /* size_t */
895 #define ZEROPAD 0x400 /* zero (as opposed to blank) pad */
896 #define FPT 0x800 /* Floating point number */
897
898 /*
899 * To extend shorts properly, we need both signed and unsigned
900 * argument extraction methods.
901 */
902 #define SARG() \
903 (flags&MAXINT ? va_arg(ap, intmax_t) : \
904 flags&PTRINT ? va_arg(ap, intptr_t) : \
905 flags&SIZEINT ? va_arg(ap, ssize_t) : /* XXX */ \
906 flags&QUADINT ? va_arg(ap, quad_t) : \
907 flags&LONGINT ? va_arg(ap, long) : \
908 flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
909 (long)va_arg(ap, int))
910 #define UARG() \
911 (flags&MAXINT ? va_arg(ap, uintmax_t) : \
912 flags&PTRINT ? va_arg(ap, uintptr_t) : \
913 flags&SIZEINT ? va_arg(ap, size_t) : \
914 flags&QUADINT ? va_arg(ap, u_quad_t) : \
915 flags&LONGINT ? va_arg(ap, u_long) : \
916 flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
917 (u_long)va_arg(ap, u_int))
918
919 #define KPRINTF_PUTCHAR(C) { \
920 if (oflags == TOBUFONLY) { \
921 if ((vp != NULL) && (sbuf == tailp)) { \
922 ret += 1; /* indicate error */ \
923 goto overflow; \
924 } \
925 *sbuf++ = (C); \
926 } else { \
927 putchar((C), oflags, (struct tty *)vp); \
928 } \
929 }
930
931 /*
932 * Guts of kernel printf. Note, we already expect to be in a mutex!
933 */
934 int
935 kprintf(fmt0, oflags, vp, sbuf, ap)
936 const char *fmt0;
937 int oflags;
938 void *vp;
939 char *sbuf;
940 va_list ap;
941 {
942 char *fmt; /* format string */
943 int ch; /* character from fmt */
944 int n; /* handy integer (short term usage) */
945 char *cp; /* handy char pointer (short term usage) */
946 int flags; /* flags as above */
947 int ret; /* return value accumulator */
948 int width; /* width from format (%8d), or 0 */
949 int prec; /* precision from format (%.3d), or -1 */
950 char sign; /* sign prefix (' ', '+', '-', or \0) */
951
952 u_quad_t _uquad; /* integer arguments %[diouxX] */
953 enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
954 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
955 int realsz; /* field size expanded by dprec */
956 int size; /* size of converted field or string */
957 char *xdigs; /* digits for [xX] conversion */
958 char buf[KPRINTF_BUFSIZE]; /* space for %c, %[diouxX] */
959 char *tailp; /* tail pointer for snprintf */
960
961 tailp = NULL; /* XXX: shutup gcc */
962 if (oflags == TOBUFONLY && (vp != NULL))
963 tailp = *(char **)vp;
964
965 cp = NULL; /* XXX: shutup gcc */
966 size = 0; /* XXX: shutup gcc */
967
968 fmt = (char *)fmt0;
969 ret = 0;
970
971 xdigs = NULL; /* XXX: shut up gcc warning */
972
973 /*
974 * Scan the format for conversions (`%' character).
975 */
976 for (;;) {
977 while (*fmt != '%' && *fmt) {
978 ret++;
979 KPRINTF_PUTCHAR(*fmt++);
980 }
981 if (*fmt == 0)
982 goto done;
983
984 fmt++; /* skip over '%' */
985
986 flags = 0;
987 dprec = 0;
988 width = 0;
989 prec = -1;
990 sign = '\0';
991
992 rflag: ch = *fmt++;
993 reswitch: switch (ch) {
994 case ' ':
995 /*
996 * ``If the space and + flags both appear, the space
997 * flag will be ignored.''
998 * -- ANSI X3J11
999 */
1000 if (!sign)
1001 sign = ' ';
1002 goto rflag;
1003 case '#':
1004 flags |= ALT;
1005 goto rflag;
1006 case '*':
1007 /*
1008 * ``A negative field width argument is taken as a
1009 * - flag followed by a positive field width.''
1010 * -- ANSI X3J11
1011 * They don't exclude field widths read from args.
1012 */
1013 if ((width = va_arg(ap, int)) >= 0)
1014 goto rflag;
1015 width = -width;
1016 /* FALLTHROUGH */
1017 case '-':
1018 flags |= LADJUST;
1019 goto rflag;
1020 case '+':
1021 sign = '+';
1022 goto rflag;
1023 case '.':
1024 if ((ch = *fmt++) == '*') {
1025 n = va_arg(ap, int);
1026 prec = n < 0 ? -1 : n;
1027 goto rflag;
1028 }
1029 n = 0;
1030 while (is_digit(ch)) {
1031 n = 10 * n + to_digit(ch);
1032 ch = *fmt++;
1033 }
1034 prec = n < 0 ? -1 : n;
1035 goto reswitch;
1036 case '0':
1037 /*
1038 * ``Note that 0 is taken as a flag, not as the
1039 * beginning of a field width.''
1040 * -- ANSI X3J11
1041 */
1042 flags |= ZEROPAD;
1043 goto rflag;
1044 case '1': case '2': case '3': case '4':
1045 case '5': case '6': case '7': case '8': case '9':
1046 n = 0;
1047 do {
1048 n = 10 * n + to_digit(ch);
1049 ch = *fmt++;
1050 } while (is_digit(ch));
1051 width = n;
1052 goto reswitch;
1053 case 'h':
1054 flags |= SHORTINT;
1055 goto rflag;
1056 case 'j':
1057 flags |= MAXINT;
1058 goto rflag;
1059 case 'l':
1060 if (*fmt == 'l') {
1061 fmt++;
1062 flags |= QUADINT;
1063 } else {
1064 flags |= LONGINT;
1065 }
1066 goto rflag;
1067 case 'q':
1068 flags |= QUADINT;
1069 goto rflag;
1070 case 't':
1071 flags |= PTRINT;
1072 goto rflag;
1073 case 'z':
1074 flags |= SIZEINT;
1075 goto rflag;
1076 case 'c':
1077 *(cp = buf) = va_arg(ap, int);
1078 size = 1;
1079 sign = '\0';
1080 break;
1081 case 'D':
1082 flags |= LONGINT;
1083 /*FALLTHROUGH*/
1084 case 'd':
1085 case 'i':
1086 _uquad = SARG();
1087 if ((quad_t)_uquad < 0) {
1088 _uquad = -_uquad;
1089 sign = '-';
1090 }
1091 base = DEC;
1092 goto number;
1093 case 'n':
1094 if (flags & MAXINT)
1095 *va_arg(ap, intmax_t *) = ret;
1096 else if (flags & PTRINT)
1097 *va_arg(ap, intptr_t *) = ret;
1098 else if (flags & SIZEINT)
1099 *va_arg(ap, ssize_t *) = ret;
1100 else if (flags & QUADINT)
1101 *va_arg(ap, quad_t *) = ret;
1102 else if (flags & LONGINT)
1103 *va_arg(ap, long *) = ret;
1104 else if (flags & SHORTINT)
1105 *va_arg(ap, short *) = ret;
1106 else
1107 *va_arg(ap, int *) = ret;
1108 continue; /* no output */
1109 case 'O':
1110 flags |= LONGINT;
1111 /*FALLTHROUGH*/
1112 case 'o':
1113 _uquad = UARG();
1114 base = OCT;
1115 goto nosign;
1116 case 'p':
1117 /*
1118 * ``The argument shall be a pointer to void. The
1119 * value of the pointer is converted to a sequence
1120 * of printable characters, in an implementation-
1121 * defined manner.''
1122 * -- ANSI X3J11
1123 */
1124 /* NOSTRICT */
1125 _uquad = (u_long)va_arg(ap, void *);
1126 base = HEX;
1127 xdigs = "0123456789abcdef";
1128 flags |= HEXPREFIX;
1129 ch = 'x';
1130 goto nosign;
1131 case 's':
1132 if ((cp = va_arg(ap, char *)) == NULL)
1133 cp = "(null)";
1134 if (prec >= 0) {
1135 /*
1136 * can't use strlen; can only look for the
1137 * NUL in the first `prec' characters, and
1138 * strlen() will go further.
1139 */
1140 char *p = memchr(cp, 0, prec);
1141
1142 if (p != NULL) {
1143 size = p - cp;
1144 if (size > prec)
1145 size = prec;
1146 } else
1147 size = prec;
1148 } else
1149 size = strlen(cp);
1150 sign = '\0';
1151 break;
1152 case 'U':
1153 flags |= LONGINT;
1154 /*FALLTHROUGH*/
1155 case 'u':
1156 _uquad = UARG();
1157 base = DEC;
1158 goto nosign;
1159 case 'X':
1160 xdigs = "0123456789ABCDEF";
1161 goto hex;
1162 case 'x':
1163 xdigs = "0123456789abcdef";
1164 hex: _uquad = UARG();
1165 base = HEX;
1166 /* leading 0x/X only if non-zero */
1167 if (flags & ALT && _uquad != 0)
1168 flags |= HEXPREFIX;
1169
1170 /* unsigned conversions */
1171 nosign: sign = '\0';
1172 /*
1173 * ``... diouXx conversions ... if a precision is
1174 * specified, the 0 flag will be ignored.''
1175 * -- ANSI X3J11
1176 */
1177 number: if ((dprec = prec) >= 0)
1178 flags &= ~ZEROPAD;
1179
1180 /*
1181 * ``The result of converting a zero value with an
1182 * explicit precision of zero is no characters.''
1183 * -- ANSI X3J11
1184 */
1185 cp = buf + KPRINTF_BUFSIZE;
1186 if (_uquad != 0 || prec != 0) {
1187 /*
1188 * Unsigned mod is hard, and unsigned mod
1189 * by a constant is easier than that by
1190 * a variable; hence this switch.
1191 */
1192 switch (base) {
1193 case OCT:
1194 do {
1195 *--cp = to_char(_uquad & 7);
1196 _uquad >>= 3;
1197 } while (_uquad);
1198 /* handle octal leading 0 */
1199 if (flags & ALT && *cp != '0')
1200 *--cp = '0';
1201 break;
1202
1203 case DEC:
1204 /* many numbers are 1 digit */
1205 while (_uquad >= 10) {
1206 *--cp = to_char(_uquad % 10);
1207 _uquad /= 10;
1208 }
1209 *--cp = to_char(_uquad);
1210 break;
1211
1212 case HEX:
1213 do {
1214 *--cp = xdigs[_uquad & 15];
1215 _uquad >>= 4;
1216 } while (_uquad);
1217 break;
1218
1219 default:
1220 cp = "bug in kprintf: bad base";
1221 size = strlen(cp);
1222 goto skipsize;
1223 }
1224 }
1225 size = buf + KPRINTF_BUFSIZE - cp;
1226 skipsize:
1227 break;
1228 default: /* "%?" prints ?, unless ? is NUL */
1229 if (ch == '\0')
1230 goto done;
1231 /* pretend it was %c with argument ch */
1232 cp = buf;
1233 *cp = ch;
1234 size = 1;
1235 sign = '\0';
1236 break;
1237 }
1238
1239 /*
1240 * All reasonable formats wind up here. At this point, `cp'
1241 * points to a string which (if not flags&LADJUST) should be
1242 * padded out to `width' places. If flags&ZEROPAD, it should
1243 * first be prefixed by any sign or other prefix; otherwise,
1244 * it should be blank padded before the prefix is emitted.
1245 * After any left-hand padding and prefixing, emit zeroes
1246 * required by a decimal [diouxX] precision, then print the
1247 * string proper, then emit zeroes required by any leftover
1248 * floating precision; finally, if LADJUST, pad with blanks.
1249 *
1250 * Compute actual size, so we know how much to pad.
1251 * size excludes decimal prec; realsz includes it.
1252 */
1253 realsz = dprec > size ? dprec : size;
1254 if (sign)
1255 realsz++;
1256 else if (flags & HEXPREFIX)
1257 realsz+= 2;
1258
1259 /* adjust ret */
1260 ret += width > realsz ? width : realsz;
1261
1262 /* right-adjusting blank padding */
1263 if ((flags & (LADJUST|ZEROPAD)) == 0) {
1264 n = width - realsz;
1265 while (n-- > 0)
1266 KPRINTF_PUTCHAR(' ');
1267 }
1268
1269 /* prefix */
1270 if (sign) {
1271 KPRINTF_PUTCHAR(sign);
1272 } else if (flags & HEXPREFIX) {
1273 KPRINTF_PUTCHAR('0');
1274 KPRINTF_PUTCHAR(ch);
1275 }
1276
1277 /* right-adjusting zero padding */
1278 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) {
1279 n = width - realsz;
1280 while (n-- > 0)
1281 KPRINTF_PUTCHAR('0');
1282 }
1283
1284 /* leading zeroes from decimal precision */
1285 n = dprec - size;
1286 while (n-- > 0)
1287 KPRINTF_PUTCHAR('0');
1288
1289 /* the string or number proper */
1290 while (size--)
1291 KPRINTF_PUTCHAR(*cp++);
1292 /* left-adjusting padding (always blank) */
1293 if (flags & LADJUST) {
1294 n = width - realsz;
1295 while (n-- > 0)
1296 KPRINTF_PUTCHAR(' ');
1297 }
1298 }
1299
1300 done:
1301 if ((oflags == TOBUFONLY) && (vp != NULL))
1302 *(char **)vp = sbuf;
1303 overflow:
1304 return (ret);
1305 /* NOTREACHED */
1306 }
1307