2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
53 * Common code for printf et al.
55 * The calling routine typically takes a variable number of arguments,
56 * and passes the address of the first one. This implementation
57 * assumes a straightforward, stack implementation, aligned to the
58 * machine's wordsize. Increasing addresses are assumed to point to
59 * successive arguments (left-to-right), as is the case for a machine
60 * with a downward-growing stack with arguments pushed right-to-left.
62 * To write, for example, fprintf() using this routine, the code
64 * fprintf(fd, format, args)
68 * _doprnt(format, &args, fd);
71 * would suffice. (This example does not handle the fprintf's "return
72 * value" correctly, but who looks at the return value of fprintf
75 * This version implements the following printf features:
77 * %d decimal conversion
78 * %u unsigned conversion
79 * %x hexadecimal conversion
80 * %X hexadecimal conversion with capital letters
81 * %D hexdump, ptr & separator string ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
82 * if you use, "%*D" then there's a length, the data ptr and then the separator
86 * %m.n field width, precision
87 * %-m.n left adjustment
89 * %*.* width and precision taken from arguments
91 * This version does not implement %f, %e, or %g. It accepts, but
92 * ignores, an `l' as in %ld, %lo, %lx, and %lu, and therefore will not
93 * work correctly on machines for which sizeof(long) != sizeof(int).
95 * As mentioned, this version does not return any reasonable value.
97 * Permission is granted to use, modify, or propagate this code as
98 * long as this notice is incorporated.
100 * Steve Summit 3/25/87
102 * Tweaked for long long support and extended to support the hexdump %D
103 * specifier by dbg 05/02/02.
107 * Added formats for decoding device registers:
109 * printf("reg = %b", regval, "<base><arg>*")
111 * where <base> is the output base expressed as a control character:
112 * i.e. '\10' gives octal, '\20' gives hex. Each <arg> is a sequence of
113 * characters, the first of which gives the bit number to be inspected
114 * (origin 1), and the rest (up to a control character (<= 32)) give the
115 * name of the register. Thus
116 * printf("reg = %b\n", 3, "\10\2BITTWO\1BITONE")
118 * reg = 3<BITTWO,BITONE>
120 * If the second character in <arg> is also a control character, it
121 * indicates the last bit of a bit field. In this case, printf will extract
122 * bits <1> to <2> and print it. Characters following the second control
123 * character are printed before the bit field.
124 * printf("reg = %b\n", 0xb, "\10\4\3FIELD1=\2BITTWO\1BITONE")
126 * reg = b<FIELD1=2,BITONE>
128 * The %B format is like %b but the bits are numbered from the most
129 * significant (the bit weighted 31), which is called 1, to the least
130 * significant, called 32.
133 * Added for general use:
134 * # prefix for alternate format:
136 * leading 0 for octal
137 * + print '+' if positive
138 * blank print ' ' if positive
140 * z signed hexadecimal
142 * n unsigned, 'radix'
144 * D,U,O,Z same as corresponding lower-case versions
148 * Added support for print long long (64-bit) integers.
149 * Use %lld, %Ld or %qd to print a 64-bit int. Other
150 * output bases such as x, X, u, U, o, and O also work.
154 #include <mach_kdb.h>
155 #include <mach_kdp.h>
156 #include <platforms.h>
157 #include <mach/boolean.h>
158 #include <kern/cpu_number.h>
159 #include <kern/lock.h>
160 #include <kern/thread.h>
161 #include <kern/sched_prim.h>
162 #include <kern/misc_protos.h>
165 #include <mach_assert.h>
167 #include <sys/msgbuf.h>
171 #include <ppc/Firmware.h>
174 #define isdigit(d) ((d) >= '0' && (d) <= '9')
175 #define Ctod(c) ((c) - '0')
177 #define MAXBUF (sizeof(long long int) * 8) /* enough for binary */
178 static char digs
[] = "0123456789abcdef";
182 register unsigned long long int u
, /* number to print */
184 void (*putc
)(int, void *),
187 char buf
[MAXBUF
]; /* build number here */
188 register char * p
= &buf
[MAXBUF
-1];
192 *p
-- = digs
[u
% base
];
196 while (++p
!= &buf
[MAXBUF
]) {
204 boolean_t _doprnt_truncates
= FALSE
;
208 register const char *fmt
,
210 /* character output routine */
211 void (*putc
)(int, void *arg
),
213 int radix
) /* default radix - for '%r' */
220 unsigned long long u
;
223 boolean_t altfmt
, truncate
;
230 while ((c
= *fmt
) != '\0') {
276 length
= 10 * length
+ Ctod(c
);
281 length
= va_arg(*argp
, int);
294 prec
= 10 * prec
+ Ctod(c
);
299 prec
= va_arg(*argp
, int);
305 c
= *++fmt
; /* need it if sizeof(int) < sizeof(long) */
310 } else if (c
== 'q' || c
== 'L') {
316 capitals
=0; /* Assume lower case printing */
327 u
= va_arg(*argp
, unsigned long long);
329 u
= va_arg(*argp
, unsigned long);
331 p
= va_arg(*argp
, char *);
333 nprinted
+= printnum(u
, base
, putc
, arg
);
339 while ((i
= *p
++) != '\0') {
357 for (; (c
= *p
) > 32; p
++) {
361 nprinted
+= printnum((unsigned)( (u
>>(j
-1)) & ((2<<(i
-j
))-1)),
364 else if (u
& (1<<(i
-1))) {
372 for (; (c
= *p
) > 32; p
++) {
390 c
= va_arg(*argp
, int);
401 prec
= 0x7fffffff; /* MAXINT */
403 p
= va_arg(*argp
, char *);
408 if (length
> 0 && !ladjust
) {
412 for (; *p
!= '\0' && n
< prec
; p
++)
427 if (++n
> prec
|| (length
> 0 && n
> length
))
434 if (n
< length
&& ladjust
) {
446 truncate
= _doprnt_truncates
;
455 up
= (unsigned char *)va_arg(*argp
, unsigned char *);
456 p
= (char *)va_arg(*argp
, char *);
460 (*putc
)(digs
[(*up
>> 4)], arg
);
461 (*putc
)(digs
[(*up
& 0x0f)], arg
);
475 truncate
= _doprnt_truncates
;
480 truncate
= _doprnt_truncates
;
488 truncate
= _doprnt_truncates
;
494 capitals
=16; /* Print in upper case */
498 truncate
= _doprnt_truncates
;
504 capitals
=16; /* Print in upper case */
508 truncate
= _doprnt_truncates
;
514 truncate
= _doprnt_truncates
;
521 n
= va_arg(*argp
, long long);
523 n
= va_arg(*argp
, long);
527 sign_char
= plus_sign
;
537 u
= va_arg(*argp
, unsigned long long);
539 u
= va_arg(*argp
, unsigned long);
545 char buf
[MAXBUF
]; /* build number here */
546 register char * p
= &buf
[MAXBUF
-1];
547 static char digits
[] = "0123456789abcdef0123456789ABCDEF";
550 if (truncate
) u
= (long long)((int)(u
));
552 if (u
!= 0 && altfmt
) {
560 /* Print in the correct case */
561 *p
-- = digits
[(u
% base
)+capitals
];
565 length
-= (&buf
[MAXBUF
-1] - p
);
569 length
-= strlen((const char *) prefix
);
571 if (padc
== ' ' && !ladjust
) {
572 /* blank padding goes before prefix */
573 while (--length
>= 0) {
579 (*putc
)(sign_char
, arg
);
584 (*putc
)(*prefix
++, arg
);
589 /* zero padding goes after sign and prefix */
590 while (--length
>= 0) {
595 while (++p
!= &buf
[MAXBUF
]) {
601 while (--length
>= 0) {
624 dummy_putc(int ch
, void *arg
)
626 void (*real_putc
)(char) = arg
;
633 register const char *fmt
,
635 /* character output routine */
637 int radix
) /* default radix - for '%r' */
639 __doprnt(fmt
, argp
, dummy_putc
, putc
, radix
);
643 boolean_t new_printf_cpu_number
= FALSE
;
644 #endif /* MP_PRINTF */
647 decl_simple_lock_data(,printf_lock
)
648 decl_simple_lock_data(,bsd_log_spinlock
)
649 decl_mutex_data(,sprintf_lock
)
650 extern void bsd_log_init(void);
651 void bsd_log_lock(void);
652 void bsd_log_unlock(void);
658 * Lock is only really needed after the first thread is created.
660 simple_lock_init(&printf_lock
, 0);
661 simple_lock_init(&bsd_log_spinlock
, 0);
663 mutex_init(&sprintf_lock
, 0);
669 simple_lock(&bsd_log_spinlock
);
675 simple_unlock(&bsd_log_spinlock
);
678 /* derived from boot_gets */
686 char *strmax
= str
+ maxlen
- 1; /* allow space for trailing 0 */
714 if (c
>= ' ' && c
< '\177') {
720 printf("%c", '\007'); /* beep */
731 extern unsigned int debug_mode
, disableDebugOuput
, disableConsoleOutput
;
733 if ((debug_mode
&& !disableDebugOuput
) || !disableConsoleOutput
)
742 dbugprintf(const char *fmt
, ...)
747 extern void db_putchar(char c
);
750 va_start(listp
, fmt
);
751 _doprnt(fmt
, &listp
, db_putchar
, 16);
758 printf(const char *fmt
, ...)
762 disable_preemption();
763 va_start(listp
, fmt
);
764 _doprnt(fmt
, &listp
, conslog_putc
, 16);
773 extern unsigned int debug_mode
, disableDebugOuput
, disableConsoleOutput
;
775 if ((debug_mode
&& !disableDebugOuput
) || !disableConsoleOutput
)
781 if (!console_is_serial())
787 kdb_printf(const char *fmt
, ...)
791 va_start(listp
, fmt
);
792 _doprnt(fmt
, &listp
, consdebug_putc
, 16);
796 static char *copybyte_str
;
802 *copybyte_str
++ = byte
;
803 *copybyte_str
= '\0';
807 sprintf(char *buf
, const char *fmt
, ...)
811 va_start(listp
, fmt
);
812 mutex_lock(&sprintf_lock
);
814 _doprnt(fmt
, &listp
, copybyte
, 16);
815 mutex_unlock(&sprintf_lock
);