]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/printf.c
xnu-1504.7.4.tar.gz
[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 signed hexadecimal
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_kdb.h>
159 #include <mach_kdp.h>
160 #include <platforms.h>
161 #include <mach/boolean.h>
162 #include <kern/cpu_number.h>
163 #include <kern/lock.h>
164 #include <kern/thread.h>
165 #include <kern/sched_prim.h>
166 #include <kern/misc_protos.h>
167 #include <stdarg.h>
168 #include <string.h>
169 #include <mach_assert.h>
170 #ifdef MACH_BSD
171 #include <sys/msgbuf.h>
172 #endif
173 #include <console/serial_protos.h>
174
175 #ifdef __ppc__
176 #include <ppc/Firmware.h>
177 #endif
178
179 #define isdigit(d) ((d) >= '0' && (d) <= '9')
180 #define Ctod(c) ((c) - '0')
181
182 #define MAXBUF (sizeof(long long int) * 8) /* enough for binary */
183 static char digs[] = "0123456789abcdef";
184
185
186 #if CONFIG_NO_PRINTF_STRINGS
187 /* Prevent CPP from breaking the definition below */
188 #undef printf
189 #endif
190
191 int _consume_printf_args(int a __unused, ...)
192 {
193 return 0;
194 }
195 void _consume_kprintf_args(int a __unused, ...)
196 {
197 }
198
199 static int
200 printnum(
201 unsigned long long int u, /* number to print */
202 int base,
203 void (*putc)(int, void *),
204 void *arg)
205 {
206 char buf[MAXBUF]; /* build number here */
207 char * p = &buf[MAXBUF-1];
208 int nprinted = 0;
209
210 do {
211 *p-- = digs[u % base];
212 u /= base;
213 } while (u != 0);
214
215 while (++p != &buf[MAXBUF]) {
216 (*putc)(*p, arg);
217 nprinted++;
218 }
219
220 return nprinted;
221 }
222
223 boolean_t _doprnt_truncates = FALSE;
224
225 int
226 __doprnt(
227 const char *fmt,
228 va_list argp,
229 /* character output routine */
230 void (*putc)(int, void *arg),
231 void *arg,
232 int radix) /* default radix - for '%r' */
233 {
234 int length;
235 int prec;
236 boolean_t ladjust;
237 char padc;
238 long long n;
239 unsigned long long u;
240 int plus_sign;
241 int sign_char;
242 boolean_t altfmt, truncate;
243 int base;
244 char c;
245 int capitals;
246 int long_long;
247 int nprinted = 0;
248
249 while ((c = *fmt) != '\0') {
250 if (c != '%') {
251 (*putc)(c, arg);
252 nprinted++;
253 fmt++;
254 continue;
255 }
256
257 fmt++;
258
259 long_long = 0;
260 length = 0;
261 prec = -1;
262 ladjust = FALSE;
263 padc = ' ';
264 plus_sign = 0;
265 sign_char = 0;
266 altfmt = FALSE;
267
268 while (TRUE) {
269 c = *fmt;
270 if (c == '#') {
271 altfmt = TRUE;
272 }
273 else if (c == '-') {
274 ladjust = TRUE;
275 }
276 else if (c == '+') {
277 plus_sign = '+';
278 }
279 else if (c == ' ') {
280 if (plus_sign == 0)
281 plus_sign = ' ';
282 }
283 else
284 break;
285 fmt++;
286 }
287
288 if (c == '0') {
289 padc = '0';
290 c = *++fmt;
291 }
292
293 if (isdigit(c)) {
294 while(isdigit(c)) {
295 length = 10 * length + Ctod(c);
296 c = *++fmt;
297 }
298 }
299 else if (c == '*') {
300 length = va_arg(argp, int);
301 c = *++fmt;
302 if (length < 0) {
303 ladjust = !ladjust;
304 length = -length;
305 }
306 }
307
308 if (c == '.') {
309 c = *++fmt;
310 if (isdigit(c)) {
311 prec = 0;
312 while(isdigit(c)) {
313 prec = 10 * prec + Ctod(c);
314 c = *++fmt;
315 }
316 }
317 else if (c == '*') {
318 prec = va_arg(argp, int);
319 c = *++fmt;
320 }
321 }
322
323 if (c == 'l') {
324 c = *++fmt; /* need it if sizeof(int) < sizeof(long) */
325 if (sizeof(int)<sizeof(long))
326 long_long = 1;
327 if (c == 'l') {
328 long_long = 1;
329 c = *++fmt;
330 }
331 } else if (c == 'q' || c == 'L') {
332 long_long = 1;
333 c = *++fmt;
334 }
335
336 truncate = FALSE;
337 capitals=0; /* Assume lower case printing */
338
339 switch(c) {
340 case 'b':
341 case 'B':
342 {
343 register char *p;
344 boolean_t any;
345 register int i;
346
347 if (long_long) {
348 u = va_arg(argp, unsigned long long);
349 } else {
350 u = va_arg(argp, unsigned int);
351 }
352 p = va_arg(argp, char *);
353 base = *p++;
354 nprinted += printnum(u, base, putc, arg);
355
356 if (u == 0)
357 break;
358
359 any = FALSE;
360 while ((i = *p++) != '\0') {
361 if (*fmt == 'B')
362 i = 33 - i;
363 if (*p <= 32) {
364 /*
365 * Bit field
366 */
367 register int j;
368 if (any)
369 (*putc)(',', arg);
370 else {
371 (*putc)('<', arg);
372 any = TRUE;
373 }
374 nprinted++;
375 j = *p++;
376 if (*fmt == 'B')
377 j = 32 - j;
378 for (; (c = *p) > 32; p++) {
379 (*putc)(c, arg);
380 nprinted++;
381 }
382 nprinted += printnum((unsigned)( (u>>(j-1)) & ((2<<(i-j))-1)),
383 base, putc, arg);
384 }
385 else if (u & (1<<(i-1))) {
386 if (any)
387 (*putc)(',', arg);
388 else {
389 (*putc)('<', arg);
390 any = TRUE;
391 }
392 nprinted++;
393 for (; (c = *p) > 32; p++) {
394 (*putc)(c, arg);
395 nprinted++;
396 }
397 }
398 else {
399 for (; *p > 32; p++)
400 continue;
401 }
402 }
403 if (any) {
404 (*putc)('>', arg);
405 nprinted++;
406 }
407 break;
408 }
409
410 case 'c':
411 c = va_arg(argp, int);
412 (*putc)(c, arg);
413 nprinted++;
414 break;
415
416 case 's':
417 {
418 register const char *p;
419 register const char *p2;
420
421 if (prec == -1)
422 prec = 0x7fffffff; /* MAXINT */
423
424 p = va_arg(argp, char *);
425
426 if (p == NULL)
427 p = "";
428
429 if (length > 0 && !ladjust) {
430 n = 0;
431 p2 = p;
432
433 for (; *p != '\0' && n < prec; p++)
434 n++;
435
436 p = p2;
437
438 while (n < length) {
439 (*putc)(' ', arg);
440 n++;
441 nprinted++;
442 }
443 }
444
445 n = 0;
446
447 while ((n < prec) && (!(length > 0 && n >= length))) {
448 if (*p == '\0') {
449 break;
450 }
451 (*putc)(*p++, arg);
452 nprinted++;
453 n++;
454 }
455
456 if (n < length && ladjust) {
457 while (n < length) {
458 (*putc)(' ', arg);
459 n++;
460 nprinted++;
461 }
462 }
463
464 break;
465 }
466
467 case 'o':
468 truncate = _doprnt_truncates;
469 case 'O':
470 base = 8;
471 goto print_unsigned;
472
473 case 'D': {
474 unsigned char *up;
475 char *q, *p;
476
477 up = (unsigned char *)va_arg(argp, unsigned char *);
478 p = (char *)va_arg(argp, char *);
479 if (length == -1)
480 length = 16;
481 while(length--) {
482 (*putc)(digs[(*up >> 4)], arg);
483 (*putc)(digs[(*up & 0x0f)], arg);
484 nprinted += 2;
485 up++;
486 if (length) {
487 for (q=p;*q;q++) {
488 (*putc)(*q, arg);
489 nprinted++;
490 }
491 }
492 }
493 break;
494 }
495
496 case 'd':
497 truncate = _doprnt_truncates;
498 base = 10;
499 goto print_signed;
500
501 case 'u':
502 truncate = _doprnt_truncates;
503 case 'U':
504 base = 10;
505 goto print_unsigned;
506
507 case 'p':
508 altfmt = TRUE;
509 if (sizeof(int)<sizeof(void *)) {
510 long_long = 1;
511 }
512 case 'x':
513 truncate = _doprnt_truncates;
514 base = 16;
515 goto print_unsigned;
516
517 case 'X':
518 base = 16;
519 capitals=16; /* Print in upper case */
520 goto print_unsigned;
521
522 case 'z':
523 truncate = _doprnt_truncates;
524 base = 16;
525 goto print_signed;
526
527 case 'Z':
528 base = 16;
529 capitals=16; /* Print in upper case */
530 goto print_signed;
531
532 case 'r':
533 truncate = _doprnt_truncates;
534 case 'R':
535 base = radix;
536 goto print_signed;
537
538 case 'n':
539 truncate = _doprnt_truncates;
540 case 'N':
541 base = radix;
542 goto print_unsigned;
543
544 print_signed:
545 if (long_long) {
546 n = va_arg(argp, long long);
547 } else {
548 n = va_arg(argp, int);
549 }
550 if (n >= 0) {
551 u = n;
552 sign_char = plus_sign;
553 }
554 else {
555 u = -n;
556 sign_char = '-';
557 }
558 goto print_num;
559
560 print_unsigned:
561 if (long_long) {
562 u = va_arg(argp, unsigned long long);
563 } else {
564 u = va_arg(argp, unsigned int);
565 }
566 goto print_num;
567
568 print_num:
569 {
570 char buf[MAXBUF]; /* build number here */
571 register char * p = &buf[MAXBUF-1];
572 static char digits[] = "0123456789abcdef0123456789ABCDEF";
573 const char *prefix = NULL;
574
575 if (truncate) u = (long long)((int)(u));
576
577 if (u != 0 && altfmt) {
578 if (base == 8)
579 prefix = "0";
580 else if (base == 16)
581 prefix = "0x";
582 }
583
584 do {
585 /* Print in the correct case */
586 *p-- = digits[(u % base)+capitals];
587 u /= base;
588 } while (u != 0);
589
590 length -= (int)(&buf[MAXBUF-1] - p);
591 if (sign_char)
592 length--;
593 if (prefix)
594 length -= (int)strlen(prefix);
595
596 if (padc == ' ' && !ladjust) {
597 /* blank padding goes before prefix */
598 while (--length >= 0) {
599 (*putc)(' ', arg);
600 nprinted++;
601 }
602 }
603 if (sign_char) {
604 (*putc)(sign_char, arg);
605 nprinted++;
606 }
607 if (prefix) {
608 while (*prefix) {
609 (*putc)(*prefix++, arg);
610 nprinted++;
611 }
612 }
613 if (padc == '0') {
614 /* zero padding goes after sign and prefix */
615 while (--length >= 0) {
616 (*putc)('0', arg);
617 nprinted++;
618 }
619 }
620 while (++p != &buf[MAXBUF]) {
621 (*putc)(*p, arg);
622 nprinted++;
623 }
624
625 if (ladjust) {
626 while (--length >= 0) {
627 (*putc)(' ', arg);
628 nprinted++;
629 }
630 }
631 break;
632 }
633
634 case '\0':
635 fmt--;
636 break;
637
638 default:
639 (*putc)(c, arg);
640 nprinted++;
641 }
642 fmt++;
643 }
644
645 return nprinted;
646 }
647
648 static void
649 dummy_putc(int ch, void *arg)
650 {
651 void (*real_putc)(char) = arg;
652
653 real_putc(ch);
654 }
655
656 void
657 _doprnt(
658 register const char *fmt,
659 va_list *argp,
660 /* character output routine */
661 void (*putc)(char),
662 int radix) /* default radix - for '%r' */
663 {
664 __doprnt(fmt, *argp, dummy_putc, putc, radix);
665 }
666
667 #if MP_PRINTF
668 boolean_t new_printf_cpu_number = FALSE;
669 #endif /* MP_PRINTF */
670
671
672 decl_simple_lock_data(,printf_lock)
673 decl_simple_lock_data(,bsd_log_spinlock)
674 extern void bsd_log_init(void);
675 void bsd_log_lock(void);
676 void bsd_log_unlock(void);
677
678 void
679 printf_init(void)
680 {
681 /*
682 * Lock is only really needed after the first thread is created.
683 */
684 simple_lock_init(&printf_lock, 0);
685 simple_lock_init(&bsd_log_spinlock, 0);
686 bsd_log_init();
687 }
688
689 void
690 bsd_log_lock(void)
691 {
692 simple_lock(&bsd_log_spinlock);
693 }
694
695 void
696 bsd_log_unlock(void)
697 {
698 simple_unlock(&bsd_log_spinlock);
699 }
700
701 /* derived from boot_gets */
702 void
703 safe_gets(
704 char *str,
705 int maxlen)
706 {
707 register char *lp;
708 register int c;
709 char *strmax = str + maxlen - 1; /* allow space for trailing 0 */
710
711 lp = str;
712 for (;;) {
713 c = cngetc();
714 switch (c) {
715 case '\n':
716 case '\r':
717 printf("\n");
718 *lp++ = 0;
719 return;
720
721 case '\b':
722 case '#':
723 case '\177':
724 if (lp > str) {
725 printf("\b \b");
726 lp--;
727 }
728 continue;
729
730 case '@':
731 case 'u'&037:
732 lp = str;
733 printf("\n\r");
734 continue;
735
736 default:
737 if (c >= ' ' && c < '\177') {
738 if (lp < strmax) {
739 *lp++ = c;
740 printf("%c", c);
741 }
742 else {
743 printf("%c", '\007'); /* beep */
744 }
745 }
746 }
747 }
748 }
749
750 extern int disableConsoleOutput;
751
752 void
753 conslog_putc(
754 char c)
755 {
756 if ((debug_mode && !disable_debug_output) || !disableConsoleOutput)
757 cnputc(c);
758
759 #ifdef MACH_BSD
760 if (debug_mode == 0)
761 log_putc(c);
762 #endif
763 }
764
765 #if MACH_KDB
766 extern void db_putchar(char c);
767 #endif
768
769 void
770 dbugprintf(__unused const char *fmt, ...)
771 {
772
773 #if MACH_KDB
774 va_list listp;
775
776 va_start(listp, fmt);
777 _doprnt(fmt, &listp, db_putchar, 16);
778 va_end(listp);
779 #endif
780 return;
781 }
782
783 int
784 printf(const char *fmt, ...)
785 {
786 va_list listp;
787
788 if (fmt) {
789 disable_preemption();
790 va_start(listp, fmt);
791 _doprnt(fmt, &listp, conslog_putc, 16);
792 va_end(listp);
793 enable_preemption();
794 }
795 return 0;
796 }
797
798 void
799 consdebug_putc(char c)
800 {
801 if ((debug_mode && !disable_debug_output) || !disableConsoleOutput)
802 cnputc(c);
803
804 debug_putc(c);
805
806 if (!console_is_serial())
807 if (!disable_serial_output)
808 PE_kputc(c);
809 }
810
811 void
812 consdebug_putc_unbuffered(char c)
813 {
814 if ((debug_mode && !disable_debug_output) || !disableConsoleOutput)
815 cnputc_unbuffered(c);
816
817 debug_putc(c);
818
819 if (!console_is_serial())
820 if (!disable_serial_output)
821 PE_kputc(c);
822 }
823
824 void
825 consdebug_log(char c)
826 {
827 debug_putc(c);
828 }
829
830 int
831 kdb_printf(const char *fmt, ...)
832 {
833 va_list listp;
834
835 va_start(listp, fmt);
836 _doprnt(fmt, &listp, consdebug_putc, 16);
837 va_end(listp);
838 return 0;
839 }
840
841 int
842 kdb_log(const char *fmt, ...)
843 {
844 va_list listp;
845
846 va_start(listp, fmt);
847 _doprnt(fmt, &listp, consdebug_log, 16);
848 va_end(listp);
849 return 0;
850 }
851
852 int
853 kdb_printf_unbuffered(const char *fmt, ...)
854 {
855 va_list listp;
856
857 va_start(listp, fmt);
858 _doprnt(fmt, &listp, consdebug_putc_unbuffered, 16);
859 va_end(listp);
860 return 0;
861 }
862
863 static void
864 copybyte(int c, void *arg)
865 {
866 /*
867 * arg is a pointer (outside pointer) to the pointer
868 * (inside pointer) which points to the character.
869 * We pass a double pointer, so that we can increment
870 * the inside pointer.
871 */
872 char** p = arg; /* cast outside pointer */
873 **p = c; /* store character */
874 (*p)++; /* increment inside pointer */
875 }
876
877 /*
878 * Deprecation Warning:
879 * sprintf() is being deprecated. Please use snprintf() instead.
880 */
881 int
882 sprintf(char *buf, const char *fmt, ...)
883 {
884 va_list listp;
885 char *copybyte_str;
886
887 va_start(listp, fmt);
888 copybyte_str = buf;
889 __doprnt(fmt, listp, copybyte, &copybyte_str, 16);
890 va_end(listp);
891 *copybyte_str = '\0';
892 return (int)strlen(buf);
893 }