2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
33 * Mach Operating System
34 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
35 * All Rights Reserved.
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.
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.
47 * Carnegie Mellon requests users of this software to return to
49 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
50 * School of Computer Science
51 * Carnegie Mellon University
52 * Pittsburgh PA 15213-3890
54 * any improvements or extensions that they make and grant Carnegie Mellon
55 * the rights to redistribute these changes.
59 * Common code for printf et al.
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.
68 * To write, for example, fprintf() using this routine, the code
70 * fprintf(fd, format, args)
74 * _doprnt(format, &args, fd);
77 * would suffice. (This example does not handle the fprintf's "return
78 * value" correctly, but who looks at the return value of fprintf
81 * This version implements the following printf features:
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
92 * %m.n field width, precision
93 * %-m.n left adjustment
95 * %*.* width and precision taken from arguments
97 * This version does not implement %f, %e, or %g. It accepts, but
98 * ignores, an `l' as in %ld, %lo, %lx, and %lu, and therefore will not
99 * work correctly on machines for which sizeof(long) != sizeof(int).
101 * As mentioned, this version does not return any reasonable value.
103 * Permission is granted to use, modify, or propagate this code as
104 * long as this notice is incorporated.
106 * Steve Summit 3/25/87
108 * Tweaked for long long support and extended to support the hexdump %D
109 * specifier by dbg 05/02/02.
113 * Added formats for decoding device registers:
115 * printf("reg = %b", regval, "<base><arg>*")
117 * where <base> is the output base expressed as a control character:
118 * i.e. '\10' gives octal, '\20' gives hex. Each <arg> is a sequence of
119 * characters, the first of which gives the bit number to be inspected
120 * (origin 1), and the rest (up to a control character (<= 32)) give the
121 * name of the register. Thus
122 * printf("reg = %b\n", 3, "\10\2BITTWO\1BITONE")
124 * reg = 3<BITTWO,BITONE>
126 * If the second character in <arg> is also a control character, it
127 * indicates the last bit of a bit field. In this case, printf will extract
128 * bits <1> to <2> and print it. Characters following the second control
129 * character are printed before the bit field.
130 * printf("reg = %b\n", 0xb, "\10\4\3FIELD1=\2BITTWO\1BITONE")
132 * reg = b<FIELD1=2,BITONE>
134 * The %B format is like %b but the bits are numbered from the most
135 * significant (the bit weighted 31), which is called 1, to the least
136 * significant, called 32.
139 * Added for general use:
140 * # prefix for alternate format:
142 * leading 0 for octal
143 * + print '+' if positive
144 * blank print ' ' if positive
146 * z signed hexadecimal
148 * n unsigned, 'radix'
150 * D,U,O,Z same as corresponding lower-case versions
154 * Added support for print long long (64-bit) integers.
155 * Use %lld, %Ld or %qd to print a 64-bit int. Other
156 * output bases such as x, X, u, U, o, and O also work.
160 #include <mach_kdb.h>
161 #include <mach_kdp.h>
162 #include <platforms.h>
163 #include <mach/boolean.h>
164 #include <kern/cpu_number.h>
165 #include <kern/lock.h>
166 #include <kern/thread.h>
167 #include <kern/sched_prim.h>
168 #include <kern/misc_protos.h>
171 #include <mach_assert.h>
173 #include <sys/msgbuf.h>
177 #include <ppc/Firmware.h>
180 #define isdigit(d) ((d) >= '0' && (d) <= '9')
181 #define Ctod(c) ((c) - '0')
183 #define MAXBUF (sizeof(long long int) * 8) /* enough for binary */
184 static char digs
[] = "0123456789abcdef";
188 register unsigned long long int u
, /* number to print */
190 void (*putc
)(int, void *),
193 char buf
[MAXBUF
]; /* build number here */
194 register char * p
= &buf
[MAXBUF
-1];
198 *p
-- = digs
[u
% base
];
202 while (++p
!= &buf
[MAXBUF
]) {
210 boolean_t _doprnt_truncates
= FALSE
;
214 register const char *fmt
,
216 /* character output routine */
217 void (*putc
)(int, void *arg
),
219 int radix
) /* default radix - for '%r' */
226 unsigned long long u
;
229 boolean_t altfmt
, truncate
;
236 while ((c
= *fmt
) != '\0') {
282 length
= 10 * length
+ Ctod(c
);
287 length
= va_arg(*argp
, int);
300 prec
= 10 * prec
+ Ctod(c
);
305 prec
= va_arg(*argp
, int);
311 c
= *++fmt
; /* need it if sizeof(int) < sizeof(long) */
316 } else if (c
== 'q' || c
== 'L') {
322 capitals
=0; /* Assume lower case printing */
333 u
= va_arg(*argp
, unsigned long long);
335 u
= va_arg(*argp
, unsigned long);
337 p
= va_arg(*argp
, char *);
339 nprinted
+= printnum(u
, base
, putc
, arg
);
345 while ((i
= *p
++) != '\0') {
363 for (; (c
= *p
) > 32; p
++) {
367 nprinted
+= printnum((unsigned)( (u
>>(j
-1)) & ((2<<(i
-j
))-1)),
370 else if (u
& (1<<(i
-1))) {
378 for (; (c
= *p
) > 32; p
++) {
396 c
= va_arg(*argp
, int);
407 prec
= 0x7fffffff; /* MAXINT */
409 p
= va_arg(*argp
, char *);
414 if (length
> 0 && !ladjust
) {
418 for (; *p
!= '\0' && n
< prec
; p
++)
433 if (++n
> prec
|| (length
> 0 && n
> length
))
440 if (n
< length
&& ladjust
) {
452 truncate
= _doprnt_truncates
;
461 up
= (unsigned char *)va_arg(*argp
, unsigned char *);
462 p
= (char *)va_arg(*argp
, char *);
466 (*putc
)(digs
[(*up
>> 4)], arg
);
467 (*putc
)(digs
[(*up
& 0x0f)], arg
);
481 truncate
= _doprnt_truncates
;
486 truncate
= _doprnt_truncates
;
494 truncate
= _doprnt_truncates
;
500 capitals
=16; /* Print in upper case */
504 truncate
= _doprnt_truncates
;
510 capitals
=16; /* Print in upper case */
514 truncate
= _doprnt_truncates
;
520 truncate
= _doprnt_truncates
;
527 n
= va_arg(*argp
, long long);
529 n
= va_arg(*argp
, long);
533 sign_char
= plus_sign
;
543 u
= va_arg(*argp
, unsigned long long);
545 u
= va_arg(*argp
, unsigned long);
551 char buf
[MAXBUF
]; /* build number here */
552 register char * p
= &buf
[MAXBUF
-1];
553 static char digits
[] = "0123456789abcdef0123456789ABCDEF";
556 if (truncate
) u
= (long long)((int)(u
));
558 if (u
!= 0 && altfmt
) {
566 /* Print in the correct case */
567 *p
-- = digits
[(u
% base
)+capitals
];
571 length
-= (&buf
[MAXBUF
-1] - p
);
575 length
-= strlen((const char *) prefix
);
577 if (padc
== ' ' && !ladjust
) {
578 /* blank padding goes before prefix */
579 while (--length
>= 0) {
585 (*putc
)(sign_char
, arg
);
590 (*putc
)(*prefix
++, arg
);
595 /* zero padding goes after sign and prefix */
596 while (--length
>= 0) {
601 while (++p
!= &buf
[MAXBUF
]) {
607 while (--length
>= 0) {
630 dummy_putc(int ch
, void *arg
)
632 void (*real_putc
)(char) = arg
;
639 register const char *fmt
,
641 /* character output routine */
643 int radix
) /* default radix - for '%r' */
645 __doprnt(fmt
, argp
, dummy_putc
, putc
, radix
);
649 boolean_t new_printf_cpu_number
= FALSE
;
650 #endif /* MP_PRINTF */
653 decl_simple_lock_data(,printf_lock
)
654 decl_simple_lock_data(,bsd_log_spinlock
)
655 decl_mutex_data(,sprintf_lock
)
656 extern void bsd_log_init(void);
657 void bsd_log_lock(void);
658 void bsd_log_unlock(void);
664 * Lock is only really needed after the first thread is created.
666 simple_lock_init(&printf_lock
, 0);
667 simple_lock_init(&bsd_log_spinlock
, 0);
669 mutex_init(&sprintf_lock
, 0);
675 simple_lock(&bsd_log_spinlock
);
681 simple_unlock(&bsd_log_spinlock
);
684 /* derived from boot_gets */
692 char *strmax
= str
+ maxlen
- 1; /* allow space for trailing 0 */
720 if (c
>= ' ' && c
< '\177') {
726 printf("%c", '\007'); /* beep */
737 extern unsigned int debug_mode
, disableDebugOuput
, disableConsoleOutput
;
739 if ((debug_mode
&& !disableDebugOuput
) || !disableConsoleOutput
)
748 dbugprintf(const char *fmt
, ...)
753 extern void db_putchar(char c
);
756 va_start(listp
, fmt
);
757 _doprnt(fmt
, &listp
, db_putchar
, 16);
764 printf(const char *fmt
, ...)
768 disable_preemption();
769 va_start(listp
, fmt
);
770 _doprnt(fmt
, &listp
, conslog_putc
, 16);
775 extern unsigned int disableSerialOuput
;
781 extern unsigned int debug_mode
, disableDebugOuput
, disableConsoleOutput
;
783 if ((debug_mode
&& !disableDebugOuput
) || !disableConsoleOutput
)
788 if (!console_is_serial())
789 if (!disableSerialOuput
)
794 kdb_printf(const char *fmt
, ...)
798 va_start(listp
, fmt
);
799 _doprnt(fmt
, &listp
, consdebug_putc
, 16);
803 static char *copybyte_str
;
809 *copybyte_str
++ = byte
;
810 *copybyte_str
= '\0';
814 sprintf(char *buf
, const char *fmt
, ...)
818 va_start(listp
, fmt
);
819 mutex_lock(&sprintf_lock
);
821 _doprnt(fmt
, &listp
, copybyte
, 16);
822 mutex_unlock(&sprintf_lock
);