]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/printf.c
6aeaa805625e7443e5b450b548da316de85e71d2
[apple/xnu.git] / osfmk / kern / printf.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28
29 /*
30 * Mach Operating System
31 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
32 * All Rights Reserved.
33 *
34 * Permission to use, copy, modify and distribute this software and its
35 * documentation is hereby granted, provided that both the copyright
36 * notice and this permission notice appear in all copies of the
37 * software, derivative works or modified versions, and any portions
38 * thereof, and that both notices appear in supporting documentation.
39 *
40 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
41 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
42 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 *
44 * Carnegie Mellon requests users of this software to return to
45 *
46 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
47 * School of Computer Science
48 * Carnegie Mellon University
49 * Pittsburgh PA 15213-3890
50 *
51 * any improvements or extensions that they make and grant Carnegie Mellon
52 * the rights to redistribute these changes.
53 */
54
55 /*
56 * Common code for printf et al.
57 *
58 * The calling routine typically takes a variable number of arguments,
59 * and passes the address of the first one. This implementation
60 * assumes a straightforward, stack implementation, aligned to the
61 * machine's wordsize. Increasing addresses are assumed to point to
62 * successive arguments (left-to-right), as is the case for a machine
63 * with a downward-growing stack with arguments pushed right-to-left.
64 *
65 * To write, for example, fprintf() using this routine, the code
66 *
67 * fprintf(fd, format, args)
68 * FILE *fd;
69 * char *format;
70 * {
71 * _doprnt(format, &args, fd);
72 * }
73 *
74 * would suffice. (This example does not handle the fprintf's "return
75 * value" correctly, but who looks at the return value of fprintf
76 * anyway?)
77 *
78 * This version implements the following printf features:
79 *
80 * %d decimal conversion
81 * %u unsigned conversion
82 * %x hexadecimal conversion
83 * %X hexadecimal conversion with capital letters
84 * %D hexdump, ptr & separator string ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
85 * if you use, "%*D" then there's a length, the data ptr and then the separator
86 * %o octal conversion
87 * %c character
88 * %s string
89 * %m.n field width, precision
90 * %-m.n left adjustment
91 * %0m.n zero-padding
92 * %*.* width and precision taken from arguments
93 *
94 * This version does not implement %f, %e, or %g. It accepts, but
95 * ignores, an `l' as in %ld, %lo, %lx, and %lu, and therefore will not
96 * work correctly on machines for which sizeof(long) != sizeof(int).
97 *
98 * As mentioned, this version does not return any reasonable value.
99 *
100 * Permission is granted to use, modify, or propagate this code as
101 * long as this notice is incorporated.
102 *
103 * Steve Summit 3/25/87
104 *
105 * Tweaked for long long support and extended to support the hexdump %D
106 * specifier by dbg 05/02/02.
107 */
108
109 /*
110 * Added formats for decoding device registers:
111 *
112 * printf("reg = %b", regval, "<base><arg>*")
113 *
114 * where <base> is the output base expressed as a control character:
115 * i.e. '\10' gives octal, '\20' gives hex. Each <arg> is a sequence of
116 * characters, the first of which gives the bit number to be inspected
117 * (origin 1), and the rest (up to a control character (<= 32)) give the
118 * name of the register. Thus
119 * printf("reg = %b\n", 3, "\10\2BITTWO\1BITONE")
120 * would produce
121 * reg = 3<BITTWO,BITONE>
122 *
123 * If the second character in <arg> is also a control character, it
124 * indicates the last bit of a bit field. In this case, printf will extract
125 * bits <1> to <2> and print it. Characters following the second control
126 * character are printed before the bit field.
127 * printf("reg = %b\n", 0xb, "\10\4\3FIELD1=\2BITTWO\1BITONE")
128 * would produce
129 * reg = b<FIELD1=2,BITONE>
130 *
131 * The %B format is like %b but the bits are numbered from the most
132 * significant (the bit weighted 31), which is called 1, to the least
133 * significant, called 32.
134 */
135 /*
136 * Added for general use:
137 * # prefix for alternate format:
138 * 0x (0X) for hex
139 * leading 0 for octal
140 * + print '+' if positive
141 * blank print ' ' if positive
142 *
143 * z signed hexadecimal
144 * r signed, 'radix'
145 * n unsigned, 'radix'
146 *
147 * D,U,O,Z same as corresponding lower-case versions
148 * (compatibility)
149 */
150 /*
151 * Added support for print long long (64-bit) integers.
152 * Use %lld, %Ld or %qd to print a 64-bit int. Other
153 * output bases such as x, X, u, U, o, and O also work.
154 */
155
156 #include <platforms.h>
157 #include <mach/boolean.h>
158 #include <cpus.h>
159 #include <kern/cpu_number.h>
160 #include <kern/lock.h>
161 #include <kern/thread.h>
162 #include <kern/sched_prim.h>
163 #include <kern/misc_protos.h>
164 #include <stdarg.h>
165 #include <string.h>
166 #include <mach_assert.h>
167 #ifdef MACH_BSD
168 #include <sys/msgbuf.h>
169 #endif
170
171 #ifdef __ppc__
172 #include <ppc/Firmware.h>
173 #endif
174
175 #define isdigit(d) ((d) >= '0' && (d) <= '9')
176 #define Ctod(c) ((c) - '0')
177
178 #define MAXBUF (sizeof(long long int) * 8) /* enough for binary */
179 static char digs[] = "0123456789abcdef";
180
181 static int
182 printnum(
183 register unsigned long long int u, /* number to print */
184 register int base,
185 void (*putc)(int, void *),
186 void *arg)
187 {
188 char buf[MAXBUF]; /* build number here */
189 register char * p = &buf[MAXBUF-1];
190 int nprinted = 0;
191
192 do {
193 *p-- = digs[u % base];
194 u /= base;
195 } while (u != 0);
196
197 while (++p != &buf[MAXBUF]) {
198 (*putc)(*p, arg);
199 nprinted++;
200 }
201
202 return nprinted;
203 }
204
205 boolean_t _doprnt_truncates = FALSE;
206
207 int
208 __doprnt(
209 register const char *fmt,
210 va_list *argp,
211 /* character output routine */
212 void (*putc)(int, void *arg),
213 void *arg,
214 int radix) /* default radix - for '%r' */
215 {
216 int length;
217 int prec;
218 boolean_t ladjust;
219 char padc;
220 long long n;
221 unsigned long long u;
222 int plus_sign;
223 int sign_char;
224 boolean_t altfmt, truncate;
225 int base;
226 register char c;
227 int capitals;
228 int long_long;
229 int nprinted = 0;
230
231 while ((c = *fmt) != '\0') {
232 if (c != '%') {
233 (*putc)(c, arg);
234 nprinted++;
235 fmt++;
236 continue;
237 }
238
239 fmt++;
240
241 long_long = 0;
242 length = 0;
243 prec = -1;
244 ladjust = FALSE;
245 padc = ' ';
246 plus_sign = 0;
247 sign_char = 0;
248 altfmt = FALSE;
249
250 while (TRUE) {
251 c = *fmt;
252 if (c == '#') {
253 altfmt = TRUE;
254 }
255 else if (c == '-') {
256 ladjust = TRUE;
257 }
258 else if (c == '+') {
259 plus_sign = '+';
260 }
261 else if (c == ' ') {
262 if (plus_sign == 0)
263 plus_sign = ' ';
264 }
265 else
266 break;
267 fmt++;
268 }
269
270 if (c == '0') {
271 padc = '0';
272 c = *++fmt;
273 }
274
275 if (isdigit(c)) {
276 while(isdigit(c)) {
277 length = 10 * length + Ctod(c);
278 c = *++fmt;
279 }
280 }
281 else if (c == '*') {
282 length = va_arg(*argp, int);
283 c = *++fmt;
284 if (length < 0) {
285 ladjust = !ladjust;
286 length = -length;
287 }
288 }
289
290 if (c == '.') {
291 c = *++fmt;
292 if (isdigit(c)) {
293 prec = 0;
294 while(isdigit(c)) {
295 prec = 10 * prec + Ctod(c);
296 c = *++fmt;
297 }
298 }
299 else if (c == '*') {
300 prec = va_arg(*argp, int);
301 c = *++fmt;
302 }
303 }
304
305 if (c == 'l') {
306 c = *++fmt; /* need it if sizeof(int) < sizeof(long) */
307 if (c == 'l') {
308 long_long = 1;
309 c = *++fmt;
310 }
311 } else if (c == 'q' || c == 'L') {
312 long_long = 1;
313 c = *++fmt;
314 }
315
316 truncate = FALSE;
317 capitals=0; /* Assume lower case printing */
318
319 switch(c) {
320 case 'b':
321 case 'B':
322 {
323 register char *p;
324 boolean_t any;
325 register int i;
326
327 if (long_long) {
328 u = va_arg(*argp, unsigned long long);
329 } else {
330 u = va_arg(*argp, unsigned long);
331 }
332 p = va_arg(*argp, char *);
333 base = *p++;
334 nprinted += printnum(u, base, putc, arg);
335
336 if (u == 0)
337 break;
338
339 any = FALSE;
340 while ((i = *p++) != '\0') {
341 if (*fmt == 'B')
342 i = 33 - i;
343 if (*p <= 32) {
344 /*
345 * Bit field
346 */
347 register int j;
348 if (any)
349 (*putc)(',', arg);
350 else {
351 (*putc)('<', arg);
352 any = TRUE;
353 }
354 nprinted++;
355 j = *p++;
356 if (*fmt == 'B')
357 j = 32 - j;
358 for (; (c = *p) > 32; p++) {
359 (*putc)(c, arg);
360 nprinted++;
361 }
362 nprinted += printnum((unsigned)( (u>>(j-1)) & ((2<<(i-j))-1)),
363 base, putc, arg);
364 }
365 else if (u & (1<<(i-1))) {
366 if (any)
367 (*putc)(',', arg);
368 else {
369 (*putc)('<', arg);
370 any = TRUE;
371 }
372 nprinted++;
373 for (; (c = *p) > 32; p++) {
374 (*putc)(c, arg);
375 nprinted++;
376 }
377 }
378 else {
379 for (; *p > 32; p++)
380 continue;
381 }
382 }
383 if (any) {
384 (*putc)('>', arg);
385 nprinted++;
386 }
387 break;
388 }
389
390 case 'c':
391 c = va_arg(*argp, int);
392 (*putc)(c, arg);
393 nprinted++;
394 break;
395
396 case 's':
397 {
398 register char *p;
399 register char *p2;
400
401 if (prec == -1)
402 prec = 0x7fffffff; /* MAXINT */
403
404 p = va_arg(*argp, char *);
405
406 if (p == (char *)0)
407 p = "";
408
409 if (length > 0 && !ladjust) {
410 n = 0;
411 p2 = p;
412
413 for (; *p != '\0' && n < prec; p++)
414 n++;
415
416 p = p2;
417
418 while (n < length) {
419 (*putc)(' ', arg);
420 n++;
421 nprinted++;
422 }
423 }
424
425 n = 0;
426
427 while (*p != '\0') {
428 if (++n > prec || (length > 0 && n > length))
429 break;
430
431 (*putc)(*p++, arg);
432 nprinted++;
433 }
434
435 if (n < length && ladjust) {
436 while (n < length) {
437 (*putc)(' ', arg);
438 n++;
439 nprinted++;
440 }
441 }
442
443 break;
444 }
445
446 case 'o':
447 truncate = _doprnt_truncates;
448 case 'O':
449 base = 8;
450 goto print_unsigned;
451
452 case 'D': {
453 unsigned char *up;
454 char *q, *p;
455
456 up = (unsigned char *)va_arg(*argp, unsigned char *);
457 p = (char *)va_arg(*argp, char *);
458 if (length == -1)
459 length = 16;
460 while(length--) {
461 (*putc)(digs[(*up >> 4)], arg);
462 (*putc)(digs[(*up & 0x0f)], arg);
463 nprinted += 2;
464 up++;
465 if (length) {
466 for (q=p;*q;q++) {
467 (*putc)(*q, arg);
468 nprinted++;
469 }
470 }
471 }
472 break;
473 }
474
475 case 'd':
476 truncate = _doprnt_truncates;
477 base = 10;
478 goto print_signed;
479
480 case 'u':
481 truncate = _doprnt_truncates;
482 case 'U':
483 base = 10;
484 goto print_unsigned;
485
486 case 'p':
487 altfmt = TRUE;
488 case 'x':
489 truncate = _doprnt_truncates;
490 base = 16;
491 goto print_unsigned;
492
493 case 'X':
494 base = 16;
495 capitals=16; /* Print in upper case */
496 goto print_unsigned;
497
498 case 'z':
499 truncate = _doprnt_truncates;
500 base = 16;
501 goto print_signed;
502
503 case 'Z':
504 base = 16;
505 capitals=16; /* Print in upper case */
506 goto print_signed;
507
508 case 'r':
509 truncate = _doprnt_truncates;
510 case 'R':
511 base = radix;
512 goto print_signed;
513
514 case 'n':
515 truncate = _doprnt_truncates;
516 case 'N':
517 base = radix;
518 goto print_unsigned;
519
520 print_signed:
521 if (long_long) {
522 n = va_arg(*argp, long long);
523 } else {
524 n = va_arg(*argp, long);
525 }
526 if (n >= 0) {
527 u = n;
528 sign_char = plus_sign;
529 }
530 else {
531 u = -n;
532 sign_char = '-';
533 }
534 goto print_num;
535
536 print_unsigned:
537 if (long_long) {
538 u = va_arg(*argp, unsigned long long);
539 } else {
540 u = va_arg(*argp, unsigned long);
541 }
542 goto print_num;
543
544 print_num:
545 {
546 char buf[MAXBUF]; /* build number here */
547 register char * p = &buf[MAXBUF-1];
548 static char digits[] = "0123456789abcdef0123456789ABCDEF";
549 char *prefix = 0;
550
551 if (truncate) u = (long long)((int)(u));
552
553 if (u != 0 && altfmt) {
554 if (base == 8)
555 prefix = "0";
556 else if (base == 16)
557 prefix = "0x";
558 }
559
560 do {
561 /* Print in the correct case */
562 *p-- = digits[(u % base)+capitals];
563 u /= base;
564 } while (u != 0);
565
566 length -= (&buf[MAXBUF-1] - p);
567 if (sign_char)
568 length--;
569 if (prefix)
570 length -= strlen((const char *) prefix);
571
572 if (padc == ' ' && !ladjust) {
573 /* blank padding goes before prefix */
574 while (--length >= 0) {
575 (*putc)(' ', arg);
576 nprinted++;
577 }
578 }
579 if (sign_char) {
580 (*putc)(sign_char, arg);
581 nprinted++;
582 }
583 if (prefix) {
584 while (*prefix) {
585 (*putc)(*prefix++, arg);
586 nprinted++;
587 }
588 }
589 if (padc == '0') {
590 /* zero padding goes after sign and prefix */
591 while (--length >= 0) {
592 (*putc)('0', arg);
593 nprinted++;
594 }
595 }
596 while (++p != &buf[MAXBUF]) {
597 (*putc)(*p, arg);
598 nprinted++;
599 }
600
601 if (ladjust) {
602 while (--length >= 0) {
603 (*putc)(' ', arg);
604 nprinted++;
605 }
606 }
607 break;
608 }
609
610 case '\0':
611 fmt--;
612 break;
613
614 default:
615 (*putc)(c, arg);
616 nprinted++;
617 }
618 fmt++;
619 }
620
621 return nprinted;
622 }
623
624 static void
625 dummy_putc(int ch, void *arg)
626 {
627 void (*real_putc)(char) = arg;
628
629 real_putc(ch);
630 }
631
632 void
633 _doprnt(
634 register const char *fmt,
635 va_list *argp,
636 /* character output routine */
637 void (*putc)(char),
638 int radix) /* default radix - for '%r' */
639 {
640 __doprnt(fmt, argp, dummy_putc, putc, radix);
641 }
642
643 #if MP_PRINTF
644 boolean_t new_printf_cpu_number = FALSE;
645 #endif /* MP_PRINTF */
646
647
648 decl_simple_lock_data(,printf_lock)
649 decl_mutex_data(,sprintf_lock)
650
651 void
652 printf_init(void)
653 {
654 /*
655 * Lock is only really needed after the first thread is created.
656 */
657 simple_lock_init(&printf_lock, ETAP_MISC_PRINTF);
658 mutex_init(&sprintf_lock, ETAP_MISC_PRINTF);
659 }
660
661 /* derived from boot_gets */
662 void
663 safe_gets(
664 char *str,
665 int maxlen)
666 {
667 register char *lp;
668 register int c;
669 char *strmax = str + maxlen - 1; /* allow space for trailing 0 */
670
671 lp = str;
672 for (;;) {
673 c = cngetc();
674 switch (c) {
675 case '\n':
676 case '\r':
677 printf("\n");
678 *lp++ = 0;
679 return;
680
681 case '\b':
682 case '#':
683 case '\177':
684 if (lp > str) {
685 printf("\b \b");
686 lp--;
687 }
688 continue;
689
690 case '@':
691 case 'u'&037:
692 lp = str;
693 printf("\n\r");
694 continue;
695
696 default:
697 if (c >= ' ' && c < '\177') {
698 if (lp < strmax) {
699 *lp++ = c;
700 printf("%c", c);
701 }
702 else {
703 printf("%c", '\007'); /* beep */
704 }
705 }
706 }
707 }
708 }
709
710 void
711 conslog_putc(
712 char c)
713 {
714 extern unsigned int debug_mode, disableDebugOuput, disableConsoleOutput;
715
716 if ((debug_mode && !disableDebugOuput) || !disableConsoleOutput)
717 cnputc(c);
718
719 #ifdef MACH_BSD
720 log_putc(c);
721 #endif
722 }
723
724 void
725 printf(const char *fmt, ...)
726 {
727 va_list listp;
728
729 disable_preemption();
730 va_start(listp, fmt);
731 _doprnt(fmt, &listp, conslog_putc, 16);
732 va_end(listp);
733 enable_preemption();
734 }
735
736 void
737 consdebug_putc(
738 char c)
739 {
740 extern unsigned int debug_mode, disableDebugOuput, disableConsoleOutput;
741
742 if ((debug_mode && !disableDebugOuput) || !disableConsoleOutput)
743 cnputc(c);
744
745 debug_putc(c);
746 }
747
748 void
749 kdb_printf(const char *fmt, ...)
750 {
751 va_list listp;
752
753 va_start(listp, fmt);
754 _doprnt(fmt, &listp, consdebug_putc, 16);
755 va_end(listp);
756 }
757
758 static char *copybyte_str;
759
760 static void
761 copybyte(
762 char byte)
763 {
764 *copybyte_str++ = byte;
765 *copybyte_str = '\0';
766 }
767
768 int
769 sprintf(char *buf, const char *fmt, ...)
770 {
771 va_list listp;
772
773 va_start(listp, fmt);
774 mutex_lock(&sprintf_lock);
775 copybyte_str = buf;
776 _doprnt(fmt, &listp, copybyte, 16);
777 mutex_unlock(&sprintf_lock);
778 va_end(listp);
779 return strlen(buf);
780 }