2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
35 * Mach Operating System
36 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
37 * All Rights Reserved.
39 * Permission to use, copy, modify and distribute this software and its
40 * documentation is hereby granted, provided that both the copyright
41 * notice and this permission notice appear in all copies of the
42 * software, derivative works or modified versions, and any portions
43 * thereof, and that both notices appear in supporting documentation.
45 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
46 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
47 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49 * Carnegie Mellon requests users of this software to return to
51 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
52 * School of Computer Science
53 * Carnegie Mellon University
54 * Pittsburgh PA 15213-3890
56 * any improvements or extensions that they make and grant Carnegie Mellon
57 * the rights to redistribute these changes.
61 * Common code for printf et al.
63 * The calling routine typically takes a variable number of arguments,
64 * and passes the address of the first one. This implementation
65 * assumes a straightforward, stack implementation, aligned to the
66 * machine's wordsize. Increasing addresses are assumed to point to
67 * successive arguments (left-to-right), as is the case for a machine
68 * with a downward-growing stack with arguments pushed right-to-left.
70 * To write, for example, fprintf() using this routine, the code
72 * fprintf(fd, format, args)
76 * _doprnt(format, &args, fd);
79 * would suffice. (This example does not handle the fprintf's "return
80 * value" correctly, but who looks at the return value of fprintf
83 * This version implements the following printf features:
85 * %d decimal conversion
86 * %u unsigned conversion
87 * %x hexadecimal conversion
88 * %X hexadecimal conversion with capital letters
89 * %D hexdump, ptr & separator string ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
90 * if you use, "%*D" then there's a length, the data ptr and then the separator
94 * %m.n field width, precision
95 * %-m.n left adjustment
97 * %*.* width and precision taken from arguments
99 * This version does not implement %f, %e, or %g. It accepts, but
100 * ignores, an `l' as in %ld, %lo, %lx, and %lu, and therefore will not
101 * work correctly on machines for which sizeof(long) != sizeof(int).
103 * As mentioned, this version does not return any reasonable value.
105 * Permission is granted to use, modify, or propagate this code as
106 * long as this notice is incorporated.
108 * Steve Summit 3/25/87
110 * Tweaked for long long support and extended to support the hexdump %D
111 * specifier by dbg 05/02/02.
115 * Added formats for decoding device registers:
117 * printf("reg = %b", regval, "<base><arg>*")
119 * where <base> is the output base expressed as a control character:
120 * i.e. '\10' gives octal, '\20' gives hex. Each <arg> is a sequence of
121 * characters, the first of which gives the bit number to be inspected
122 * (origin 1), and the rest (up to a control character (<= 32)) give the
123 * name of the register. Thus
124 * printf("reg = %b\n", 3, "\10\2BITTWO\1BITONE")
126 * reg = 3<BITTWO,BITONE>
128 * If the second character in <arg> is also a control character, it
129 * indicates the last bit of a bit field. In this case, printf will extract
130 * bits <1> to <2> and print it. Characters following the second control
131 * character are printed before the bit field.
132 * printf("reg = %b\n", 0xb, "\10\4\3FIELD1=\2BITTWO\1BITONE")
134 * reg = b<FIELD1=2,BITONE>
136 * The %B format is like %b but the bits are numbered from the most
137 * significant (the bit weighted 31), which is called 1, to the least
138 * significant, called 32.
141 * Added for general use:
142 * # prefix for alternate format:
144 * leading 0 for octal
145 * + print '+' if positive
146 * blank print ' ' if positive
148 * z signed hexadecimal
150 * n unsigned, 'radix'
152 * D,U,O,Z same as corresponding lower-case versions
156 * Added support for print long long (64-bit) integers.
157 * Use %lld, %Ld or %qd to print a 64-bit int. Other
158 * output bases such as x, X, u, U, o, and O also work.
162 #include <mach_kdb.h>
163 #include <mach_kdp.h>
164 #include <platforms.h>
165 #include <mach/boolean.h>
166 #include <kern/cpu_number.h>
167 #include <kern/lock.h>
168 #include <kern/thread.h>
169 #include <kern/sched_prim.h>
170 #include <kern/misc_protos.h>
173 #include <mach_assert.h>
175 #include <sys/msgbuf.h>
179 #include <ppc/Firmware.h>
182 #define isdigit(d) ((d) >= '0' && (d) <= '9')
183 #define Ctod(c) ((c) - '0')
185 #define MAXBUF (sizeof(long long int) * 8) /* enough for binary */
186 static char digs
[] = "0123456789abcdef";
190 register unsigned long long int u
, /* number to print */
192 void (*putc
)(int, void *),
195 char buf
[MAXBUF
]; /* build number here */
196 register char * p
= &buf
[MAXBUF
-1];
200 *p
-- = digs
[u
% base
];
204 while (++p
!= &buf
[MAXBUF
]) {
212 boolean_t _doprnt_truncates
= FALSE
;
216 register const char *fmt
,
218 /* character output routine */
219 void (*putc
)(int, void *arg
),
221 int radix
) /* default radix - for '%r' */
228 unsigned long long u
;
231 boolean_t altfmt
, truncate
;
238 while ((c
= *fmt
) != '\0') {
284 length
= 10 * length
+ Ctod(c
);
289 length
= va_arg(*argp
, int);
302 prec
= 10 * prec
+ Ctod(c
);
307 prec
= va_arg(*argp
, int);
313 c
= *++fmt
; /* need it if sizeof(int) < sizeof(long) */
318 } else if (c
== 'q' || c
== 'L') {
324 capitals
=0; /* Assume lower case printing */
335 u
= va_arg(*argp
, unsigned long long);
337 u
= va_arg(*argp
, unsigned long);
339 p
= va_arg(*argp
, char *);
341 nprinted
+= printnum(u
, base
, putc
, arg
);
347 while ((i
= *p
++) != '\0') {
365 for (; (c
= *p
) > 32; p
++) {
369 nprinted
+= printnum((unsigned)( (u
>>(j
-1)) & ((2<<(i
-j
))-1)),
372 else if (u
& (1<<(i
-1))) {
380 for (; (c
= *p
) > 32; p
++) {
398 c
= va_arg(*argp
, int);
409 prec
= 0x7fffffff; /* MAXINT */
411 p
= va_arg(*argp
, char *);
416 if (length
> 0 && !ladjust
) {
420 for (; *p
!= '\0' && n
< prec
; p
++)
435 if (++n
> prec
|| (length
> 0 && n
> length
))
442 if (n
< length
&& ladjust
) {
454 truncate
= _doprnt_truncates
;
463 up
= (unsigned char *)va_arg(*argp
, unsigned char *);
464 p
= (char *)va_arg(*argp
, char *);
468 (*putc
)(digs
[(*up
>> 4)], arg
);
469 (*putc
)(digs
[(*up
& 0x0f)], arg
);
483 truncate
= _doprnt_truncates
;
488 truncate
= _doprnt_truncates
;
496 truncate
= _doprnt_truncates
;
502 capitals
=16; /* Print in upper case */
506 truncate
= _doprnt_truncates
;
512 capitals
=16; /* Print in upper case */
516 truncate
= _doprnt_truncates
;
522 truncate
= _doprnt_truncates
;
529 n
= va_arg(*argp
, long long);
531 n
= va_arg(*argp
, long);
535 sign_char
= plus_sign
;
545 u
= va_arg(*argp
, unsigned long long);
547 u
= va_arg(*argp
, unsigned long);
553 char buf
[MAXBUF
]; /* build number here */
554 register char * p
= &buf
[MAXBUF
-1];
555 static char digits
[] = "0123456789abcdef0123456789ABCDEF";
558 if (truncate
) u
= (long long)((int)(u
));
560 if (u
!= 0 && altfmt
) {
568 /* Print in the correct case */
569 *p
-- = digits
[(u
% base
)+capitals
];
573 length
-= (&buf
[MAXBUF
-1] - p
);
577 length
-= strlen((const char *) prefix
);
579 if (padc
== ' ' && !ladjust
) {
580 /* blank padding goes before prefix */
581 while (--length
>= 0) {
587 (*putc
)(sign_char
, arg
);
592 (*putc
)(*prefix
++, arg
);
597 /* zero padding goes after sign and prefix */
598 while (--length
>= 0) {
603 while (++p
!= &buf
[MAXBUF
]) {
609 while (--length
>= 0) {
632 dummy_putc(int ch
, void *arg
)
634 void (*real_putc
)(char) = arg
;
641 register const char *fmt
,
643 /* character output routine */
645 int radix
) /* default radix - for '%r' */
647 __doprnt(fmt
, argp
, dummy_putc
, putc
, radix
);
651 boolean_t new_printf_cpu_number
= FALSE
;
652 #endif /* MP_PRINTF */
655 decl_simple_lock_data(,printf_lock
)
656 decl_simple_lock_data(,bsd_log_spinlock
)
657 decl_mutex_data(,sprintf_lock
)
658 extern void bsd_log_init(void);
659 void bsd_log_lock(void);
660 void bsd_log_unlock(void);
666 * Lock is only really needed after the first thread is created.
668 simple_lock_init(&printf_lock
, 0);
669 simple_lock_init(&bsd_log_spinlock
, 0);
671 mutex_init(&sprintf_lock
, 0);
677 simple_lock(&bsd_log_spinlock
);
683 simple_unlock(&bsd_log_spinlock
);
686 /* derived from boot_gets */
694 char *strmax
= str
+ maxlen
- 1; /* allow space for trailing 0 */
722 if (c
>= ' ' && c
< '\177') {
728 printf("%c", '\007'); /* beep */
739 extern unsigned int debug_mode
, disableDebugOuput
, disableConsoleOutput
;
741 if ((debug_mode
&& !disableDebugOuput
) || !disableConsoleOutput
)
750 dbugprintf(const char *fmt
, ...)
755 extern void db_putchar(char c
);
758 va_start(listp
, fmt
);
759 _doprnt(fmt
, &listp
, db_putchar
, 16);
766 printf(const char *fmt
, ...)
770 disable_preemption();
771 va_start(listp
, fmt
);
772 _doprnt(fmt
, &listp
, conslog_putc
, 16);
781 extern unsigned int debug_mode
, disableDebugOuput
, disableConsoleOutput
;
783 if ((debug_mode
&& !disableDebugOuput
) || !disableConsoleOutput
)
789 if (!console_is_serial())
795 kdb_printf(const char *fmt
, ...)
799 va_start(listp
, fmt
);
800 _doprnt(fmt
, &listp
, consdebug_putc
, 16);
804 static char *copybyte_str
;
810 *copybyte_str
++ = byte
;
811 *copybyte_str
= '\0';
815 sprintf(char *buf
, const char *fmt
, ...)
819 va_start(listp
, fmt
);
820 mutex_lock(&sprintf_lock
);
822 _doprnt(fmt
, &listp
, copybyte
, 16);
823 mutex_unlock(&sprintf_lock
);