]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/printf.c
9fb14d2628975d70f4dac53a71f22f5e15c86228
[apple/xnu.git] / osfmk / kern / printf.c
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 */
31
32 /*
33 * Mach Operating System
34 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
35 * All Rights Reserved.
36 *
37 * Permission to use, copy, modify and distribute this software and its
38 * documentation is hereby granted, provided that both the copyright
39 * notice and this permission notice appear in all copies of the
40 * software, derivative works or modified versions, and any portions
41 * thereof, and that both notices appear in supporting documentation.
42 *
43 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
44 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
45 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 *
47 * Carnegie Mellon requests users of this software to return to
48 *
49 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
50 * School of Computer Science
51 * Carnegie Mellon University
52 * Pittsburgh PA 15213-3890
53 *
54 * any improvements or extensions that they make and grant Carnegie Mellon
55 * the rights to redistribute these changes.
56 */
57
58 /*
59 * Common code for printf et al.
60 *
61 * The calling routine typically takes a variable number of arguments,
62 * and passes the address of the first one. This implementation
63 * assumes a straightforward, stack implementation, aligned to the
64 * machine's wordsize. Increasing addresses are assumed to point to
65 * successive arguments (left-to-right), as is the case for a machine
66 * with a downward-growing stack with arguments pushed right-to-left.
67 *
68 * To write, for example, fprintf() using this routine, the code
69 *
70 * fprintf(fd, format, args)
71 * FILE *fd;
72 * char *format;
73 * {
74 * _doprnt(format, &args, fd);
75 * }
76 *
77 * would suffice. (This example does not handle the fprintf's "return
78 * value" correctly, but who looks at the return value of fprintf
79 * anyway?)
80 *
81 * This version implements the following printf features:
82 *
83 * %d decimal conversion
84 * %u unsigned conversion
85 * %x hexadecimal conversion
86 * %X hexadecimal conversion with capital letters
87 * %D hexdump, ptr & separator string ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
88 * if you use, "%*D" then there's a length, the data ptr and then the separator
89 * %o octal conversion
90 * %c character
91 * %s string
92 * %m.n field width, precision
93 * %-m.n left adjustment
94 * %0m.n zero-padding
95 * %*.* width and precision taken from arguments
96 *
97 * This version does not implement %f, %e, or %g.
98 *
99 * As mentioned, this version does not return any reasonable value.
100 *
101 * Permission is granted to use, modify, or propagate this code as
102 * long as this notice is incorporated.
103 *
104 * Steve Summit 3/25/87
105 *
106 * Tweaked for long long support and extended to support the hexdump %D
107 * specifier by dbg 05/02/02.
108 */
109
110 /*
111 * Added formats for decoding device registers:
112 *
113 * printf("reg = %b", regval, "<base><arg>*")
114 *
115 * where <base> is the output base expressed as a control character:
116 * i.e. '\10' gives octal, '\20' gives hex. Each <arg> is a sequence of
117 * characters, the first of which gives the bit number to be inspected
118 * (origin 1), and the rest (up to a control character (<= 32)) give the
119 * name of the register. Thus
120 * printf("reg = %b\n", 3, "\10\2BITTWO\1BITONE")
121 * would produce
122 * reg = 3<BITTWO,BITONE>
123 *
124 * If the second character in <arg> is also a control character, it
125 * indicates the last bit of a bit field. In this case, printf will extract
126 * bits <1> to <2> and print it. Characters following the second control
127 * character are printed before the bit field.
128 * printf("reg = %b\n", 0xb, "\10\4\3FIELD1=\2BITTWO\1BITONE")
129 * would produce
130 * reg = b<FIELD1=2,BITONE>
131 *
132 * The %B format is like %b but the bits are numbered from the most
133 * significant (the bit weighted 31), which is called 1, to the least
134 * significant, called 32.
135 */
136 /*
137 * Added for general use:
138 * # prefix for alternate format:
139 * 0x (0X) for hex
140 * leading 0 for octal
141 * + print '+' if positive
142 * blank print ' ' if positive
143 *
144 * z set length equal to size_t
145 * r signed, 'radix'
146 * n unsigned, 'radix'
147 *
148 * D,U,O,Z same as corresponding lower-case versions
149 * (compatibility)
150 */
151 /*
152 * Added support for print long long (64-bit) integers.
153 * Use %lld, %Ld or %qd to print a 64-bit int. Other
154 * output bases such as x, X, u, U, o, and O also work.
155 */
156
157 #include <debug.h>
158 #include <mach_kdp.h>
159 #include <mach/boolean.h>
160 #include <kern/cpu_number.h>
161 #include <kern/thread.h>
162 #include <kern/debug.h>
163 #include <kern/sched_prim.h>
164 #include <kern/misc_protos.h>
165 #include <stdarg.h>
166 #include <string.h>
167 #include <mach_assert.h>
168 #ifdef MACH_BSD
169 #include <sys/msgbuf.h>
170 #endif
171 #include <console/serial_protos.h>
172 #include <os/log_private.h>
173
174 #ifdef __x86_64__
175 #include <i386/cpu_data.h>
176 #endif /* __x86_64__ */
177
178 #if __arm__ || __arm64__
179 #include <arm/cpu_data_internal.h>
180 #endif
181
182
183 #define isdigit(d) ((d) >= '0' && (d) <= '9')
184 #define Ctod(c) ((c) - '0')
185
186 #define MAXBUF (sizeof(long long int) * 8) /* enough for binary */
187 static char digs[] = "0123456789abcdef";
188
189 #if CONFIG_NO_PRINTF_STRINGS
190 /* Prevent CPP from breaking the definition below */
191 #undef printf
192 #endif
193
194 int
195 _consume_printf_args(int a __unused, ...)
196 {
197 return 0;
198 }
199 void
200 _consume_kprintf_args(int a __unused, ...)
201 {
202 }
203
204 static int
205 printnum(
206 unsigned long long int u, /* number to print */
207 int base,
208 void (*putc)(int, void *),
209 void *arg)
210 {
211 char buf[MAXBUF]; /* build number here */
212 char * p = &buf[MAXBUF - 1];
213 int nprinted = 0;
214
215 do {
216 *p-- = digs[u % base];
217 u /= base;
218 } while (u != 0);
219
220 while (++p != &buf[MAXBUF]) {
221 (*putc)(*p, arg);
222 nprinted++;
223 }
224
225 return nprinted;
226 }
227
228 boolean_t _doprnt_truncates = FALSE;
229
230 #if (DEVELOPMENT || DEBUG)
231 boolean_t doprnt_hide_pointers = FALSE;
232 #else
233 boolean_t doprnt_hide_pointers = TRUE;
234 #endif
235
236 int
237 __doprnt(
238 const char *fmt,
239 va_list argp,
240 /* character output routine */
241 void (*putc)(int, void *arg),
242 void *arg,
243 int radix, /* default radix - for '%r' */
244 int is_log)
245 {
246 int length;
247 int prec;
248 boolean_t ladjust;
249 char padc;
250 long long n;
251 unsigned long long u;
252 int plus_sign;
253 int sign_char;
254 boolean_t altfmt, truncate;
255 int base;
256 char c;
257 int capitals;
258 int long_long;
259 int nprinted = 0;
260
261 while ((c = *fmt) != '\0') {
262 if (c != '%') {
263 (*putc)(c, arg);
264 nprinted++;
265 fmt++;
266 continue;
267 }
268
269 fmt++;
270
271 long_long = 0;
272 length = 0;
273 prec = -1;
274 ladjust = FALSE;
275 padc = ' ';
276 plus_sign = 0;
277 sign_char = 0;
278 altfmt = FALSE;
279
280 while (TRUE) {
281 c = *fmt;
282 if (c == '#') {
283 altfmt = TRUE;
284 } else if (c == '-') {
285 ladjust = TRUE;
286 } else if (c == '+') {
287 plus_sign = '+';
288 } else if (c == ' ') {
289 if (plus_sign == 0) {
290 plus_sign = ' ';
291 }
292 } else {
293 break;
294 }
295 fmt++;
296 }
297
298 if (c == '0') {
299 padc = '0';
300 c = *++fmt;
301 }
302
303 if (isdigit(c)) {
304 while (isdigit(c)) {
305 length = 10 * length + Ctod(c);
306 c = *++fmt;
307 }
308 } else if (c == '*') {
309 length = va_arg(argp, int);
310 c = *++fmt;
311 if (length < 0) {
312 ladjust = !ladjust;
313 length = -length;
314 }
315 }
316
317 if (c == '.') {
318 c = *++fmt;
319 if (isdigit(c)) {
320 prec = 0;
321 while (isdigit(c)) {
322 prec = 10 * prec + Ctod(c);
323 c = *++fmt;
324 }
325 } else if (c == '*') {
326 prec = va_arg(argp, int);
327 c = *++fmt;
328 }
329 }
330
331 if (c == 'l') {
332 c = *++fmt; /* need it if sizeof(int) < sizeof(long) */
333 if (sizeof(int) < sizeof(long)) {
334 long_long = 1;
335 }
336 if (c == 'l') {
337 long_long = 1;
338 c = *++fmt;
339 }
340 } else if (c == 'q' || c == 'L') {
341 long_long = 1;
342 c = *++fmt;
343 }
344
345 if (c == 'z' || c == 'Z') {
346 c = *++fmt;
347 if (sizeof(size_t) == sizeof(unsigned long)) {
348 long_long = 1;
349 }
350 }
351
352 truncate = FALSE;
353 capitals = 0; /* Assume lower case printing */
354
355 switch (c) {
356 case 'b':
357 case 'B':
358 {
359 char *p;
360 boolean_t any;
361 int i;
362
363 if (long_long) {
364 u = va_arg(argp, unsigned long long);
365 } else {
366 u = va_arg(argp, unsigned int);
367 }
368 p = va_arg(argp, char *);
369 base = *p++;
370 nprinted += printnum(u, base, putc, arg);
371
372 if (u == 0) {
373 break;
374 }
375
376 any = FALSE;
377 while ((i = *p++) != '\0') {
378 if (*fmt == 'B') {
379 i = 33 - i;
380 }
381 if (*p <= 32) {
382 /*
383 * Bit field
384 */
385 int j;
386 if (any) {
387 (*putc)(',', arg);
388 } else {
389 (*putc)('<', arg);
390 any = TRUE;
391 }
392 nprinted++;
393 j = *p++;
394 if (*fmt == 'B') {
395 j = 32 - j;
396 }
397 for (; (c = *p) > 32; p++) {
398 (*putc)(c, arg);
399 nprinted++;
400 }
401 nprinted += printnum((unsigned)((u >> (j - 1)) & ((2 << (i - j)) - 1)),
402 base, putc, arg);
403 } else if (u & (1 << (i - 1))) {
404 if (any) {
405 (*putc)(',', arg);
406 } else {
407 (*putc)('<', arg);
408 any = TRUE;
409 }
410 nprinted++;
411 for (; (c = *p) > 32; p++) {
412 (*putc)(c, arg);
413 nprinted++;
414 }
415 } else {
416 for (; *p > 32; p++) {
417 continue;
418 }
419 }
420 }
421 if (any) {
422 (*putc)('>', arg);
423 nprinted++;
424 }
425 break;
426 }
427
428 case 'c':
429 c = va_arg(argp, int);
430 (*putc)(c, arg);
431 nprinted++;
432 break;
433
434 case 's':
435 {
436 const char *p;
437 const char *p2;
438
439 if (prec == -1) {
440 prec = 0x7fffffff; /* MAXINT */
441 }
442 p = va_arg(argp, char *);
443
444 if (p == NULL) {
445 p = "";
446 }
447
448 if (length > 0 && !ladjust) {
449 n = 0;
450 p2 = p;
451
452 for (; *p != '\0' && n < prec; p++) {
453 n++;
454 }
455
456 p = p2;
457
458 while (n < length) {
459 (*putc)(' ', arg);
460 n++;
461 nprinted++;
462 }
463 }
464
465 n = 0;
466
467 while ((n < prec) && (!(length > 0 && n >= length))) {
468 if (*p == '\0') {
469 break;
470 }
471 (*putc)(*p++, arg);
472 nprinted++;
473 n++;
474 }
475
476 if (n < length && ladjust) {
477 while (n < length) {
478 (*putc)(' ', arg);
479 n++;
480 nprinted++;
481 }
482 }
483
484 break;
485 }
486
487 case 'o':
488 truncate = _doprnt_truncates;
489 case 'O':
490 base = 8;
491 goto print_unsigned;
492
493 case 'D': {
494 unsigned char *up;
495 char *q, *p;
496
497 up = (unsigned char *)va_arg(argp, unsigned char *);
498 p = (char *)va_arg(argp, char *);
499 if (length == -1) {
500 length = 16;
501 }
502 while (length--) {
503 (*putc)(digs[(*up >> 4)], arg);
504 (*putc)(digs[(*up & 0x0f)], arg);
505 nprinted += 2;
506 up++;
507 if (length) {
508 for (q = p; *q; q++) {
509 (*putc)(*q, arg);
510 nprinted++;
511 }
512 }
513 }
514 break;
515 }
516
517 case 'd':
518 truncate = _doprnt_truncates;
519 base = 10;
520 goto print_signed;
521
522 case 'u':
523 truncate = _doprnt_truncates;
524 case 'U':
525 base = 10;
526 goto print_unsigned;
527
528 case 'p':
529 altfmt = TRUE;
530 if (sizeof(int) < sizeof(void *)) {
531 long_long = 1;
532 }
533 case 'x':
534 truncate = _doprnt_truncates;
535 base = 16;
536 goto print_unsigned;
537
538 case 'X':
539 base = 16;
540 capitals = 16; /* Print in upper case */
541 goto print_unsigned;
542
543 case 'r':
544 truncate = _doprnt_truncates;
545 case 'R':
546 base = radix;
547 goto print_signed;
548
549 case 'n':
550 truncate = _doprnt_truncates;
551 case 'N':
552 base = radix;
553 goto print_unsigned;
554
555 print_signed:
556 if (long_long) {
557 n = va_arg(argp, long long);
558 } else {
559 n = va_arg(argp, int);
560 }
561 if (n >= 0) {
562 u = n;
563 sign_char = plus_sign;
564 } else {
565 u = -n;
566 sign_char = '-';
567 }
568 goto print_num;
569
570 print_unsigned:
571 if (long_long) {
572 u = va_arg(argp, unsigned long long);
573 } else {
574 u = va_arg(argp, unsigned int);
575 }
576 goto print_num;
577
578 print_num:
579 {
580 char buf[MAXBUF];/* build number here */
581 char * p = &buf[MAXBUF - 1];
582 static char digits[] = "0123456789abcdef0123456789ABCDEF";
583 const char *prefix = NULL;
584
585 if (truncate) {
586 u = (long long)((int)(u));
587 }
588
589 if (doprnt_hide_pointers && is_log) {
590 const char str[] = "<ptr>";
591 const char* strp = str;
592 int strl = sizeof(str) - 1;
593
594
595 if (u >= VM_MIN_KERNEL_AND_KEXT_ADDRESS && u <= VM_MAX_KERNEL_ADDRESS) {
596 while (*strp != '\0') {
597 (*putc)(*strp, arg);
598 strp++;
599 }
600 nprinted += strl;
601 break;
602 }
603 }
604
605 if (u != 0 && altfmt) {
606 if (base == 8) {
607 prefix = "0";
608 } else if (base == 16) {
609 prefix = "0x";
610 }
611 }
612
613 do {
614 /* Print in the correct case */
615 *p-- = digits[(u % base) + capitals];
616 u /= base;
617 } while (u != 0);
618
619 length -= (int)(&buf[MAXBUF - 1] - p);
620 if (sign_char) {
621 length--;
622 }
623 if (prefix) {
624 length -= (int)strlen(prefix);
625 }
626
627 if (padc == ' ' && !ladjust) {
628 /* blank padding goes before prefix */
629 while (--length >= 0) {
630 (*putc)(' ', arg);
631 nprinted++;
632 }
633 }
634 if (sign_char) {
635 (*putc)(sign_char, arg);
636 nprinted++;
637 }
638 if (prefix) {
639 while (*prefix) {
640 (*putc)(*prefix++, arg);
641 nprinted++;
642 }
643 }
644 if (padc == '0') {
645 /* zero padding goes after sign and prefix */
646 while (--length >= 0) {
647 (*putc)('0', arg);
648 nprinted++;
649 }
650 }
651 while (++p != &buf[MAXBUF]) {
652 (*putc)(*p, arg);
653 nprinted++;
654 }
655
656 if (ladjust) {
657 while (--length >= 0) {
658 (*putc)(' ', arg);
659 nprinted++;
660 }
661 }
662 break;
663 }
664
665 case '\0':
666 fmt--;
667 break;
668
669 default:
670 (*putc)(c, arg);
671 nprinted++;
672 }
673 fmt++;
674 }
675
676 return nprinted;
677 }
678
679 static void
680 dummy_putc(int ch, void *arg)
681 {
682 void (*real_putc)(char) = arg;
683
684 real_putc(ch);
685 }
686
687 void
688 _doprnt(
689 const char *fmt,
690 va_list *argp,
691 /* character output routine */
692 void (*putc)(char),
693 int radix) /* default radix - for '%r' */
694 {
695 __doprnt(fmt, *argp, dummy_putc, putc, radix, FALSE);
696 }
697
698 void
699 _doprnt_log(
700 const char *fmt,
701 va_list *argp,
702 /* character output routine */
703 void (*putc)(char),
704 int radix) /* default radix - for '%r' */
705 {
706 __doprnt(fmt, *argp, dummy_putc, putc, radix, TRUE);
707 }
708
709 #if MP_PRINTF
710 boolean_t new_printf_cpu_number = FALSE;
711 #endif /* MP_PRINTF */
712
713 decl_simple_lock_data(, printf_lock)
714 decl_simple_lock_data(, bsd_log_spinlock)
715
716 lck_grp_t oslog_stream_lock_grp;
717 decl_lck_spin_data(, oslog_stream_lock)
718 void oslog_lock_init(void);
719
720 extern void bsd_log_init(void);
721 void bsd_log_lock(void);
722 void bsd_log_unlock(void);
723
724 void
725 printf_init(void)
726 {
727 /*
728 * Lock is only really needed after the first thread is created.
729 */
730 simple_lock_init(&printf_lock, 0);
731 simple_lock_init(&bsd_log_spinlock, 0);
732 bsd_log_init();
733 }
734
735 void
736 bsd_log_lock(void)
737 {
738 simple_lock(&bsd_log_spinlock, LCK_GRP_NULL);
739 }
740
741 void
742 bsd_log_unlock(void)
743 {
744 simple_unlock(&bsd_log_spinlock);
745 }
746
747 void
748 oslog_lock_init(void)
749 {
750 lck_grp_init(&oslog_stream_lock_grp, "oslog stream", LCK_GRP_ATTR_NULL);
751 lck_spin_init(&oslog_stream_lock, &oslog_stream_lock_grp, LCK_ATTR_NULL);
752 }
753
754 /* derived from boot_gets */
755 void
756 safe_gets(
757 char *str,
758 int maxlen)
759 {
760 char *lp;
761 int c;
762 char *strmax = str + maxlen - 1; /* allow space for trailing 0 */
763
764 lp = str;
765 for (;;) {
766 c = cngetc();
767 switch (c) {
768 case '\n':
769 case '\r':
770 printf("\n");
771 *lp++ = 0;
772 return;
773
774 case '\b':
775 case '#':
776 case '\177':
777 if (lp > str) {
778 printf("\b \b");
779 lp--;
780 }
781 continue;
782
783 case '@':
784 case 'u'&037:
785 lp = str;
786 printf("\n\r");
787 continue;
788
789 default:
790 if (c >= ' ' && c < '\177') {
791 if (lp < strmax) {
792 *lp++ = c;
793 printf("%c", c);
794 } else {
795 printf("%c", '\007'); /* beep */
796 }
797 }
798 }
799 }
800 }
801
802 extern int disableConsoleOutput;
803
804 void
805 conslog_putc(
806 char c)
807 {
808 if (!disableConsoleOutput) {
809 cnputc(c);
810 }
811
812 #ifdef MACH_BSD
813 if (!kernel_debugger_entry_count) {
814 log_putc(c);
815 }
816 #endif
817 }
818
819 void
820 cons_putc_locked(
821 char c)
822 {
823 if (!disableConsoleOutput) {
824 cnputc(c);
825 }
826 }
827
828 static int
829 vprintf_internal(const char *fmt, va_list ap_in, void *caller)
830 {
831 cpu_data_t * cpu_data_p;
832 if (fmt) {
833 struct console_printbuf_state info_data;
834 cpu_data_p = current_cpu_datap();
835
836 va_list ap;
837 va_copy(ap, ap_in);
838 /*
839 * for early boot printf()s console may not be setup,
840 * fallback to good old cnputc
841 */
842 if (cpu_data_p->cpu_console_buf != NULL) {
843 console_printbuf_state_init(&info_data, TRUE, TRUE);
844 __doprnt(fmt, ap, console_printbuf_putc, &info_data, 16, TRUE);
845 console_printbuf_clear(&info_data);
846 } else {
847 disable_preemption();
848 _doprnt_log(fmt, &ap, cons_putc_locked, 16);
849 enable_preemption();
850 }
851
852 va_end(ap);
853
854 os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, ap_in, caller);
855 }
856 return 0;
857 }
858
859 __attribute__((noinline, not_tail_called))
860 int
861 printf(const char *fmt, ...)
862 {
863 int ret;
864
865 va_list ap;
866 va_start(ap, fmt);
867 ret = vprintf_internal(fmt, ap, __builtin_return_address(0));
868 va_end(ap);
869
870 return ret;
871 }
872
873 __attribute__((noinline, not_tail_called))
874 int
875 vprintf(const char *fmt, va_list ap)
876 {
877 return vprintf_internal(fmt, ap, __builtin_return_address(0));
878 }
879
880 void
881 consdebug_putc(char c)
882 {
883 if (!disableConsoleOutput) {
884 cnputc(c);
885 }
886
887 debug_putc(c);
888
889 if (!console_is_serial() && !disable_serial_output) {
890 PE_kputc(c);
891 }
892 }
893
894 void
895 consdebug_putc_unbuffered(char c)
896 {
897 if (!disableConsoleOutput) {
898 cnputc_unbuffered(c);
899 }
900
901 debug_putc(c);
902
903 if (!console_is_serial() && !disable_serial_output) {
904 PE_kputc(c);
905 }
906 }
907
908 void
909 consdebug_log(char c)
910 {
911 debug_putc(c);
912 }
913
914 /*
915 * Append contents to the paniclog buffer but don't flush
916 * it. This is mainly used for writing the actual paniclog
917 * contents since flushing once for every line written
918 * would be prohibitively expensive for the paniclog
919 */
920 int
921 paniclog_append_noflush(const char *fmt, ...)
922 {
923 va_list listp;
924
925 va_start(listp, fmt);
926 _doprnt_log(fmt, &listp, consdebug_putc, 16);
927 va_end(listp);
928
929 return 0;
930 }
931
932 int
933 kdb_printf(const char *fmt, ...)
934 {
935 va_list listp;
936
937 va_start(listp, fmt);
938 _doprnt_log(fmt, &listp, consdebug_putc, 16);
939 va_end(listp);
940
941 #if CONFIG_EMBEDDED
942 paniclog_flush();
943 #endif
944
945 return 0;
946 }
947
948 int
949 kdb_log(const char *fmt, ...)
950 {
951 va_list listp;
952
953 va_start(listp, fmt);
954 _doprnt(fmt, &listp, consdebug_log, 16);
955 va_end(listp);
956
957 #if CONFIG_EMBEDDED
958 paniclog_flush();
959 #endif
960
961 return 0;
962 }
963
964 int
965 kdb_printf_unbuffered(const char *fmt, ...)
966 {
967 va_list listp;
968
969 va_start(listp, fmt);
970 _doprnt(fmt, &listp, consdebug_putc_unbuffered, 16);
971 va_end(listp);
972
973 #if CONFIG_EMBEDDED
974 paniclog_flush();
975 #endif
976
977 return 0;
978 }
979
980 #if !CONFIG_EMBEDDED
981
982 static void
983 copybyte(int c, void *arg)
984 {
985 /*
986 * arg is a pointer (outside pointer) to the pointer
987 * (inside pointer) which points to the character.
988 * We pass a double pointer, so that we can increment
989 * the inside pointer.
990 */
991 char** p = arg; /* cast outside pointer */
992 **p = c; /* store character */
993 (*p)++; /* increment inside pointer */
994 }
995
996 /*
997 * Deprecation Warning:
998 * sprintf() is being deprecated. Please use snprintf() instead.
999 */
1000 int
1001 sprintf(char *buf, const char *fmt, ...)
1002 {
1003 va_list listp;
1004 char *copybyte_str;
1005
1006 va_start(listp, fmt);
1007 copybyte_str = buf;
1008 __doprnt(fmt, listp, copybyte, &copybyte_str, 16, FALSE);
1009 va_end(listp);
1010 *copybyte_str = '\0';
1011 return (int)strlen(buf);
1012 }
1013 #endif /* !CONFIG_EMBEDDED */