]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/printf.c
xnu-6153.11.26.tar.gz
[apple/xnu.git] / osfmk / kern / printf.c
1 /*
2 * Copyright (c) 2000-2019 Apple 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 #ifdef HAS_APPLE_PAC
183 #include <mach/vm_param.h>
184 #include <ptrauth.h>
185 #endif /* HAS_APPLE_PAC */
186
187 #define isdigit(d) ((d) >= '0' && (d) <= '9')
188 #define Ctod(c) ((c) - '0')
189
190 #define MAXBUF (sizeof(long long int) * 8) /* enough for binary */
191 static char digs[] = "0123456789abcdef";
192
193 #if CONFIG_NO_PRINTF_STRINGS
194 /* Prevent CPP from breaking the definition below */
195 #undef printf
196 #endif
197
198 int
199 _consume_printf_args(int a __unused, ...)
200 {
201 return 0;
202 }
203 void
204 _consume_kprintf_args(int a __unused, ...)
205 {
206 }
207
208 static int
209 printnum(
210 unsigned long long int u, /* number to print */
211 int base,
212 void (*putc)(int, void *),
213 void *arg)
214 {
215 char buf[MAXBUF]; /* build number here */
216 char * p = &buf[MAXBUF - 1];
217 int nprinted = 0;
218
219 do {
220 *p-- = digs[u % base];
221 u /= base;
222 } while (u != 0);
223
224 while (++p != &buf[MAXBUF]) {
225 (*putc)(*p, arg);
226 nprinted++;
227 }
228
229 return nprinted;
230 }
231
232 boolean_t _doprnt_truncates = FALSE;
233
234 #if (DEVELOPMENT || DEBUG)
235 boolean_t doprnt_hide_pointers = FALSE;
236 #else
237 boolean_t doprnt_hide_pointers = TRUE;
238 #endif
239
240 int
241 __doprnt(
242 const char *fmt,
243 va_list argp,
244 /* character output routine */
245 void (*putc)(int, void *arg),
246 void *arg,
247 int radix, /* default radix - for '%r' */
248 int is_log)
249 {
250 int length;
251 int prec;
252 boolean_t ladjust;
253 char padc;
254 long long n;
255 unsigned long long u;
256 int plus_sign;
257 int sign_char;
258 boolean_t altfmt, truncate;
259 int base;
260 char c;
261 int capitals;
262 int long_long;
263 enum {
264 INT,
265 SHORT,
266 CHAR,
267 } numeric_type = INT;
268 int nprinted = 0;
269
270 while ((c = *fmt) != '\0') {
271 if (c != '%') {
272 (*putc)(c, arg);
273 nprinted++;
274 fmt++;
275 continue;
276 }
277
278 fmt++;
279
280 long_long = 0;
281 numeric_type = INT;
282 length = 0;
283 prec = -1;
284 ladjust = FALSE;
285 padc = ' ';
286 plus_sign = 0;
287 sign_char = 0;
288 altfmt = FALSE;
289
290 while (TRUE) {
291 c = *fmt;
292 if (c == '#') {
293 altfmt = TRUE;
294 } else if (c == '-') {
295 ladjust = TRUE;
296 } else if (c == '+') {
297 plus_sign = '+';
298 } else if (c == ' ') {
299 if (plus_sign == 0) {
300 plus_sign = ' ';
301 }
302 } else {
303 break;
304 }
305 fmt++;
306 }
307
308 if (c == '0') {
309 padc = '0';
310 c = *++fmt;
311 }
312
313 if (isdigit(c)) {
314 while (isdigit(c)) {
315 length = 10 * length + Ctod(c);
316 c = *++fmt;
317 }
318 } else if (c == '*') {
319 length = va_arg(argp, int);
320 c = *++fmt;
321 if (length < 0) {
322 ladjust = !ladjust;
323 length = -length;
324 }
325 }
326
327 if (c == '.') {
328 c = *++fmt;
329 if (isdigit(c)) {
330 prec = 0;
331 while (isdigit(c)) {
332 prec = 10 * prec + Ctod(c);
333 c = *++fmt;
334 }
335 } else if (c == '*') {
336 prec = va_arg(argp, int);
337 c = *++fmt;
338 }
339 }
340
341 if (c == 'l') {
342 c = *++fmt; /* need it if sizeof(int) < sizeof(long) */
343 if (sizeof(int) < sizeof(long)) {
344 long_long = 1;
345 }
346 if (c == 'l') {
347 long_long = 1;
348 c = *++fmt;
349 }
350 } else if (c == 'h') {
351 c = *++fmt;
352 numeric_type = SHORT;
353 if (c == 'h') {
354 numeric_type = CHAR;
355 c = *++fmt;
356 }
357 } else if (c == 'q' || c == 'L') {
358 long_long = 1;
359 c = *++fmt;
360 }
361
362 if (c == 'z' || c == 'Z') {
363 c = *++fmt;
364 if (sizeof(size_t) == sizeof(unsigned long)) {
365 long_long = 1;
366 }
367 }
368
369 truncate = FALSE;
370 capitals = 0; /* Assume lower case printing */
371
372 switch (c) {
373 case 'b':
374 case 'B':
375 {
376 char *p;
377 boolean_t any;
378 int i;
379
380 if (long_long) {
381 u = va_arg(argp, unsigned long long);
382 } else {
383 u = va_arg(argp, unsigned int);
384 }
385 p = va_arg(argp, char *);
386 base = *p++;
387 nprinted += printnum(u, base, putc, arg);
388
389 if (u == 0) {
390 break;
391 }
392
393 any = FALSE;
394 while ((i = *p++) != '\0') {
395 if (*fmt == 'B') {
396 i = 33 - i;
397 }
398 if (*p <= 32) {
399 /*
400 * Bit field
401 */
402 int j;
403 if (any) {
404 (*putc)(',', arg);
405 } else {
406 (*putc)('<', arg);
407 any = TRUE;
408 }
409 nprinted++;
410 j = *p++;
411 if (*fmt == 'B') {
412 j = 32 - j;
413 }
414 for (; (c = *p) > 32; p++) {
415 (*putc)(c, arg);
416 nprinted++;
417 }
418 nprinted += printnum((unsigned)((u >> (j - 1)) & ((2 << (i - j)) - 1)),
419 base, putc, arg);
420 } else if (u & (1 << (i - 1))) {
421 if (any) {
422 (*putc)(',', arg);
423 } else {
424 (*putc)('<', arg);
425 any = TRUE;
426 }
427 nprinted++;
428 for (; (c = *p) > 32; p++) {
429 (*putc)(c, arg);
430 nprinted++;
431 }
432 } else {
433 for (; *p > 32; p++) {
434 continue;
435 }
436 }
437 }
438 if (any) {
439 (*putc)('>', arg);
440 nprinted++;
441 }
442 break;
443 }
444
445 case 'c':
446 c = va_arg(argp, int);
447 (*putc)(c, arg);
448 nprinted++;
449 break;
450
451 case 's':
452 {
453 const char *p;
454 const char *p2;
455
456 if (prec == -1) {
457 prec = 0x7fffffff; /* MAXINT */
458 }
459 p = va_arg(argp, char *);
460
461 if (p == NULL) {
462 p = "";
463 }
464
465 if (length > 0 && !ladjust) {
466 n = 0;
467 p2 = p;
468
469 for (; *p != '\0' && n < prec; p++) {
470 n++;
471 }
472
473 p = p2;
474
475 while (n < length) {
476 (*putc)(' ', arg);
477 n++;
478 nprinted++;
479 }
480 }
481
482 n = 0;
483
484 while ((n < prec) && (!(length > 0 && n >= length))) {
485 if (*p == '\0') {
486 break;
487 }
488 (*putc)(*p++, arg);
489 nprinted++;
490 n++;
491 }
492
493 if (n < length && ladjust) {
494 while (n < length) {
495 (*putc)(' ', arg);
496 n++;
497 nprinted++;
498 }
499 }
500
501 break;
502 }
503
504 case 'o':
505 truncate = _doprnt_truncates;
506 case 'O':
507 base = 8;
508 goto print_unsigned;
509
510 case 'D': {
511 unsigned char *up;
512 char *q, *p;
513
514 up = (unsigned char *)va_arg(argp, unsigned char *);
515 p = (char *)va_arg(argp, char *);
516 if (length == -1) {
517 length = 16;
518 }
519 while (length--) {
520 (*putc)(digs[(*up >> 4)], arg);
521 (*putc)(digs[(*up & 0x0f)], arg);
522 nprinted += 2;
523 up++;
524 if (length) {
525 for (q = p; *q; q++) {
526 (*putc)(*q, arg);
527 nprinted++;
528 }
529 }
530 }
531 break;
532 }
533
534 case 'd':
535 truncate = _doprnt_truncates;
536 base = 10;
537 goto print_signed;
538
539 case 'u':
540 truncate = _doprnt_truncates;
541 /* FALLTHROUGH */
542 case 'U':
543 base = 10;
544 goto print_unsigned;
545
546 case 'p':
547 altfmt = TRUE;
548 if (sizeof(int) < sizeof(void *)) {
549 long_long = 1;
550 }
551 /* FALLTHROUGH */
552 case 'x':
553 truncate = _doprnt_truncates;
554 base = 16;
555 goto print_unsigned;
556
557 case 'X':
558 base = 16;
559 capitals = 16; /* Print in upper case */
560 goto print_unsigned;
561
562 case 'r':
563 truncate = _doprnt_truncates;
564 /* FALLTHROUGH */
565 case 'R':
566 base = radix;
567 goto print_signed;
568
569 case 'n':
570 truncate = _doprnt_truncates;
571 /* FALLTHROUGH */
572 case 'N':
573 base = radix;
574 goto print_unsigned;
575
576 print_signed:
577 if (long_long) {
578 n = va_arg(argp, long long);
579 } else {
580 n = va_arg(argp, int);
581 }
582 switch (numeric_type) {
583 case SHORT:
584 n = (short)n;
585 break;
586 case CHAR:
587 n = (char)n;
588 break;
589 default:
590 break;
591 }
592 if (n >= 0) {
593 u = n;
594 sign_char = plus_sign;
595 } else {
596 u = -n;
597 sign_char = '-';
598 }
599 goto print_num;
600
601 print_unsigned:
602 if (long_long) {
603 u = va_arg(argp, unsigned long long);
604 } else {
605 u = va_arg(argp, unsigned int);
606 }
607 switch (numeric_type) {
608 case SHORT:
609 u = (unsigned short)u;
610 break;
611 case CHAR:
612 u = (unsigned char)u;
613 break;
614 default:
615 break;
616 }
617 goto print_num;
618
619 print_num:
620 {
621 char buf[MAXBUF];/* build number here */
622 char * p = &buf[MAXBUF - 1];
623 static char digits[] = "0123456789abcdef0123456789ABCDEF";
624 const char *prefix = NULL;
625
626 if (truncate) {
627 u = (long long)((int)(u));
628 }
629
630 if (doprnt_hide_pointers && is_log) {
631 const char str[] = "<ptr>";
632 const char* strp = str;
633 int strl = sizeof(str) - 1;
634
635 #ifdef HAS_APPLE_PAC
636 /**
637 * Strip out the pointer authentication code before
638 * checking whether the pointer is a kernel address.
639 */
640 u = (unsigned long long)VM_KERNEL_STRIP_PTR(u);
641 #endif /* HAS_APPLE_PAC */
642
643 if (u >= VM_MIN_KERNEL_AND_KEXT_ADDRESS && u <= VM_MAX_KERNEL_ADDRESS) {
644 while (*strp != '\0') {
645 (*putc)(*strp, arg);
646 strp++;
647 }
648 nprinted += strl;
649 break;
650 }
651 }
652
653 if (u != 0 && altfmt) {
654 if (base == 8) {
655 prefix = "0";
656 } else if (base == 16) {
657 prefix = "0x";
658 }
659 }
660
661 do {
662 /* Print in the correct case */
663 *p-- = digits[(u % base) + capitals];
664 u /= base;
665 } while (u != 0);
666
667 length -= (int)(&buf[MAXBUF - 1] - p);
668 if (sign_char) {
669 length--;
670 }
671 if (prefix) {
672 length -= (int)strlen(prefix);
673 }
674
675 if (padc == ' ' && !ladjust) {
676 /* blank padding goes before prefix */
677 while (--length >= 0) {
678 (*putc)(' ', arg);
679 nprinted++;
680 }
681 }
682 if (sign_char) {
683 (*putc)(sign_char, arg);
684 nprinted++;
685 }
686 if (prefix) {
687 while (*prefix) {
688 (*putc)(*prefix++, arg);
689 nprinted++;
690 }
691 }
692 if (padc == '0') {
693 /* zero padding goes after sign and prefix */
694 while (--length >= 0) {
695 (*putc)('0', arg);
696 nprinted++;
697 }
698 }
699 while (++p != &buf[MAXBUF]) {
700 (*putc)(*p, arg);
701 nprinted++;
702 }
703
704 if (ladjust) {
705 while (--length >= 0) {
706 (*putc)(' ', arg);
707 nprinted++;
708 }
709 }
710 break;
711 }
712
713 case '\0':
714 fmt--;
715 break;
716
717 default:
718 (*putc)(c, arg);
719 nprinted++;
720 }
721 fmt++;
722 }
723
724 return nprinted;
725 }
726
727 static void
728 dummy_putc(int ch, void *arg)
729 {
730 void (*real_putc)(char) = arg;
731
732 /*
733 * Attempts to panic (or otherwise log to console) during early boot
734 * can result in _doprnt() and _doprnt_log() being called from
735 * _kprintf() before PE_init_kprintf() has been called. This causes
736 * the "putc" param to _doprnt() and _doprnt_log() to be passed as
737 * NULL. That NULL makes its way here, and we would try jump to it.
738 * Given that this is a poor idea, and this happens at very early
739 * boot, there is not a way to report this easily (we are likely
740 * already panicing), so we'll just do nothing instead of crashing.
741 */
742 if (real_putc) {
743 real_putc(ch);
744 }
745 }
746
747 void
748 _doprnt(
749 const char *fmt,
750 va_list *argp,
751 /* character output routine */
752 void (*putc)(char),
753 int radix) /* default radix - for '%r' */
754 {
755 __doprnt(fmt, *argp, dummy_putc, putc, radix, FALSE);
756 }
757
758 void
759 _doprnt_log(
760 const char *fmt,
761 va_list *argp,
762 /* character output routine */
763 void (*putc)(char),
764 int radix) /* default radix - for '%r' */
765 {
766 __doprnt(fmt, *argp, dummy_putc, putc, radix, TRUE);
767 }
768
769 #if MP_PRINTF
770 boolean_t new_printf_cpu_number = FALSE;
771 #endif /* MP_PRINTF */
772
773 decl_simple_lock_data(, printf_lock);
774 decl_simple_lock_data(, bsd_log_spinlock);
775
776 lck_grp_t oslog_stream_lock_grp;
777 decl_lck_spin_data(, oslog_stream_lock);
778 void oslog_lock_init(void);
779
780 extern void bsd_log_init(void);
781 void bsd_log_lock(void);
782 void bsd_log_unlock(void);
783
784 void
785 printf_init(void)
786 {
787 /*
788 * Lock is only really needed after the first thread is created.
789 */
790 simple_lock_init(&printf_lock, 0);
791 simple_lock_init(&bsd_log_spinlock, 0);
792 bsd_log_init();
793 }
794
795 void
796 bsd_log_lock(void)
797 {
798 simple_lock(&bsd_log_spinlock, LCK_GRP_NULL);
799 }
800
801 void
802 bsd_log_unlock(void)
803 {
804 simple_unlock(&bsd_log_spinlock);
805 }
806
807 void
808 oslog_lock_init(void)
809 {
810 lck_grp_init(&oslog_stream_lock_grp, "oslog stream", LCK_GRP_ATTR_NULL);
811 lck_spin_init(&oslog_stream_lock, &oslog_stream_lock_grp, LCK_ATTR_NULL);
812 }
813
814 /* derived from boot_gets */
815 void
816 safe_gets(
817 char *str,
818 int maxlen)
819 {
820 char *lp;
821 int c;
822 char *strmax = str + maxlen - 1; /* allow space for trailing 0 */
823
824 lp = str;
825 for (;;) {
826 c = cngetc();
827 switch (c) {
828 case '\n':
829 case '\r':
830 printf("\n");
831 *lp++ = 0;
832 return;
833
834 case '\b':
835 case '#':
836 case '\177':
837 if (lp > str) {
838 printf("\b \b");
839 lp--;
840 }
841 continue;
842
843 case '@':
844 case 'u'&037:
845 lp = str;
846 printf("\n\r");
847 continue;
848
849 default:
850 if (c >= ' ' && c < '\177') {
851 if (lp < strmax) {
852 *lp++ = c;
853 printf("%c", c);
854 } else {
855 printf("%c", '\007'); /* beep */
856 }
857 }
858 }
859 }
860 }
861
862 extern int disableConsoleOutput;
863
864 void
865 conslog_putc(
866 char c)
867 {
868 if (!disableConsoleOutput) {
869 cnputc(c);
870 }
871
872 #ifdef MACH_BSD
873 if (!kernel_debugger_entry_count) {
874 log_putc(c);
875 }
876 #endif
877 }
878
879 void
880 cons_putc_locked(
881 char c)
882 {
883 if (!disableConsoleOutput) {
884 cnputc(c);
885 }
886 }
887
888 static int
889 vprintf_internal(const char *fmt, va_list ap_in, void *caller)
890 {
891 cpu_data_t * cpu_data_p;
892 if (fmt) {
893 struct console_printbuf_state info_data;
894 cpu_data_p = current_cpu_datap();
895
896 va_list ap;
897 va_copy(ap, ap_in);
898 /*
899 * for early boot printf()s console may not be setup,
900 * fallback to good old cnputc
901 */
902 if (cpu_data_p->cpu_console_buf != NULL) {
903 console_printbuf_state_init(&info_data, TRUE, TRUE);
904 __doprnt(fmt, ap, console_printbuf_putc, &info_data, 16, TRUE);
905 console_printbuf_clear(&info_data);
906 } else {
907 disable_preemption();
908 _doprnt_log(fmt, &ap, cons_putc_locked, 16);
909 enable_preemption();
910 }
911
912 va_end(ap);
913
914 os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, ap_in, caller);
915 }
916 return 0;
917 }
918
919 __attribute__((noinline, not_tail_called))
920 int
921 printf(const char *fmt, ...)
922 {
923 int ret;
924
925 va_list ap;
926 va_start(ap, fmt);
927 ret = vprintf_internal(fmt, ap, __builtin_return_address(0));
928 va_end(ap);
929
930 return ret;
931 }
932
933 __attribute__((noinline, not_tail_called))
934 int
935 vprintf(const char *fmt, va_list ap)
936 {
937 return vprintf_internal(fmt, ap, __builtin_return_address(0));
938 }
939
940 void
941 consdebug_putc(char c)
942 {
943 if (!disableConsoleOutput) {
944 cnputc(c);
945 }
946
947 debug_putc(c);
948
949 if (!console_is_serial() && !disable_serial_output) {
950 PE_kputc(c);
951 }
952 }
953
954 void
955 consdebug_putc_unbuffered(char c)
956 {
957 if (!disableConsoleOutput) {
958 cnputc_unbuffered(c);
959 }
960
961 debug_putc(c);
962
963 if (!console_is_serial() && !disable_serial_output) {
964 PE_kputc(c);
965 }
966 }
967
968 void
969 consdebug_log(char c)
970 {
971 debug_putc(c);
972 }
973
974 /*
975 * Append contents to the paniclog buffer but don't flush
976 * it. This is mainly used for writing the actual paniclog
977 * contents since flushing once for every line written
978 * would be prohibitively expensive for the paniclog
979 */
980 int
981 paniclog_append_noflush(const char *fmt, ...)
982 {
983 va_list listp;
984
985 va_start(listp, fmt);
986 _doprnt_log(fmt, &listp, consdebug_putc, 16);
987 va_end(listp);
988
989 return 0;
990 }
991
992 int
993 kdb_printf(const char *fmt, ...)
994 {
995 va_list listp;
996
997 va_start(listp, fmt);
998 _doprnt_log(fmt, &listp, consdebug_putc, 16);
999 va_end(listp);
1000
1001 #if CONFIG_EMBEDDED
1002 paniclog_flush();
1003 #endif
1004
1005 return 0;
1006 }
1007
1008 int
1009 kdb_log(const char *fmt, ...)
1010 {
1011 va_list listp;
1012
1013 va_start(listp, fmt);
1014 _doprnt(fmt, &listp, consdebug_log, 16);
1015 va_end(listp);
1016
1017 #if CONFIG_EMBEDDED
1018 paniclog_flush();
1019 #endif
1020
1021 return 0;
1022 }
1023
1024 int
1025 kdb_printf_unbuffered(const char *fmt, ...)
1026 {
1027 va_list listp;
1028
1029 va_start(listp, fmt);
1030 _doprnt(fmt, &listp, consdebug_putc_unbuffered, 16);
1031 va_end(listp);
1032
1033 #if CONFIG_EMBEDDED
1034 paniclog_flush();
1035 #endif
1036
1037 return 0;
1038 }
1039
1040 #if !CONFIG_EMBEDDED
1041
1042 static void
1043 copybyte(int c, void *arg)
1044 {
1045 /*
1046 * arg is a pointer (outside pointer) to the pointer
1047 * (inside pointer) which points to the character.
1048 * We pass a double pointer, so that we can increment
1049 * the inside pointer.
1050 */
1051 char** p = arg; /* cast outside pointer */
1052 **p = c; /* store character */
1053 (*p)++; /* increment inside pointer */
1054 }
1055
1056 /*
1057 * Deprecation Warning:
1058 * sprintf() is being deprecated. Please use snprintf() instead.
1059 */
1060 int
1061 sprintf(char *buf, const char *fmt, ...)
1062 {
1063 va_list listp;
1064 char *copybyte_str;
1065
1066 va_start(listp, fmt);
1067 copybyte_str = buf;
1068 __doprnt(fmt, listp, copybyte, &copybyte_str, 16, FALSE);
1069 va_end(listp);
1070 *copybyte_str = '\0';
1071 return (int)strlen(buf);
1072 }
1073 #endif /* !CONFIG_EMBEDDED */