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.
99 * As mentioned, this version does not return any reasonable value.
101 * Permission is granted to use, modify, or propagate this code as
102 * long as this notice is incorporated.
104 * Steve Summit 3/25/87
106 * Tweaked for long long support and extended to support the hexdump %D
107 * specifier by dbg 05/02/02.
111 * Added formats for decoding device registers:
113 * printf("reg = %b", regval, "<base><arg>*")
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")
122 * reg = 3<BITTWO,BITONE>
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")
130 * reg = b<FIELD1=2,BITONE>
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.
137 * Added for general use:
138 * # prefix for alternate format:
140 * leading 0 for octal
141 * + print '+' if positive
142 * blank print ' ' if positive
144 * z signed hexadecimal
146 * n unsigned, 'radix'
148 * D,U,O,Z same as corresponding lower-case versions
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.
158 #include <mach_kdp.h>
159 #include <platforms.h>
160 #include <mach/boolean.h>
161 #include <kern/cpu_number.h>
162 #include <kern/lock.h>
163 #include <kern/thread.h>
164 #include <kern/sched_prim.h>
165 #include <kern/misc_protos.h>
168 #include <mach_assert.h>
170 #include <sys/msgbuf.h>
172 #include <console/serial_protos.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";
180 #if CONFIG_NO_PRINTF_STRINGS
181 /* Prevent CPP from breaking the definition below */
185 int _consume_printf_args(int a __unused
, ...)
189 void _consume_kprintf_args(int a __unused
, ...)
195 unsigned long long int u
, /* number to print */
197 void (*putc
)(int, void *),
200 char buf
[MAXBUF
]; /* build number here */
201 char * p
= &buf
[MAXBUF
-1];
205 *p
-- = digs
[u
% base
];
209 while (++p
!= &buf
[MAXBUF
]) {
217 boolean_t _doprnt_truncates
= FALSE
;
223 /* character output routine */
224 void (*putc
)(int, void *arg
),
226 int radix
) /* default radix - for '%r' */
233 unsigned long long u
;
236 boolean_t altfmt
, truncate
;
243 while ((c
= *fmt
) != '\0') {
289 length
= 10 * length
+ Ctod(c
);
294 length
= va_arg(argp
, int);
307 prec
= 10 * prec
+ Ctod(c
);
312 prec
= va_arg(argp
, int);
318 c
= *++fmt
; /* need it if sizeof(int) < sizeof(long) */
319 if (sizeof(int)<sizeof(long))
325 } else if (c
== 'q' || c
== 'L') {
331 capitals
=0; /* Assume lower case printing */
342 u
= va_arg(argp
, unsigned long long);
344 u
= va_arg(argp
, unsigned int);
346 p
= va_arg(argp
, char *);
348 nprinted
+= printnum(u
, base
, putc
, arg
);
354 while ((i
= *p
++) != '\0') {
372 for (; (c
= *p
) > 32; p
++) {
376 nprinted
+= printnum((unsigned)( (u
>>(j
-1)) & ((2<<(i
-j
))-1)),
379 else if (u
& (1<<(i
-1))) {
387 for (; (c
= *p
) > 32; p
++) {
405 c
= va_arg(argp
, int);
412 register const char *p
;
413 register const char *p2
;
416 prec
= 0x7fffffff; /* MAXINT */
418 p
= va_arg(argp
, char *);
423 if (length
> 0 && !ladjust
) {
427 for (; *p
!= '\0' && n
< prec
; p
++)
441 while ((n
< prec
) && (!(length
> 0 && n
>= length
))) {
450 if (n
< length
&& ladjust
) {
462 truncate
= _doprnt_truncates
;
471 up
= (unsigned char *)va_arg(argp
, unsigned char *);
472 p
= (char *)va_arg(argp
, char *);
476 (*putc
)(digs
[(*up
>> 4)], arg
);
477 (*putc
)(digs
[(*up
& 0x0f)], arg
);
491 truncate
= _doprnt_truncates
;
496 truncate
= _doprnt_truncates
;
503 if (sizeof(int)<sizeof(void *)) {
507 truncate
= _doprnt_truncates
;
513 capitals
=16; /* Print in upper case */
517 truncate
= _doprnt_truncates
;
523 capitals
=16; /* Print in upper case */
527 truncate
= _doprnt_truncates
;
533 truncate
= _doprnt_truncates
;
540 n
= va_arg(argp
, long long);
542 n
= va_arg(argp
, int);
546 sign_char
= plus_sign
;
556 u
= va_arg(argp
, unsigned long long);
558 u
= va_arg(argp
, unsigned int);
564 char buf
[MAXBUF
]; /* build number here */
565 register char * p
= &buf
[MAXBUF
-1];
566 static char digits
[] = "0123456789abcdef0123456789ABCDEF";
567 const char *prefix
= NULL
;
569 if (truncate
) u
= (long long)((int)(u
));
571 if (u
!= 0 && altfmt
) {
579 /* Print in the correct case */
580 *p
-- = digits
[(u
% base
)+capitals
];
584 length
-= (int)(&buf
[MAXBUF
-1] - p
);
588 length
-= (int)strlen(prefix
);
590 if (padc
== ' ' && !ladjust
) {
591 /* blank padding goes before prefix */
592 while (--length
>= 0) {
598 (*putc
)(sign_char
, arg
);
603 (*putc
)(*prefix
++, arg
);
608 /* zero padding goes after sign and prefix */
609 while (--length
>= 0) {
614 while (++p
!= &buf
[MAXBUF
]) {
620 while (--length
>= 0) {
643 dummy_putc(int ch
, void *arg
)
645 void (*real_putc
)(char) = arg
;
652 register const char *fmt
,
654 /* character output routine */
656 int radix
) /* default radix - for '%r' */
658 __doprnt(fmt
, *argp
, dummy_putc
, putc
, radix
);
662 boolean_t new_printf_cpu_number
= FALSE
;
663 #endif /* MP_PRINTF */
666 decl_simple_lock_data(,printf_lock
)
667 decl_simple_lock_data(,bsd_log_spinlock
)
668 extern void bsd_log_init(void);
669 void bsd_log_lock(void);
670 void bsd_log_unlock(void);
676 * Lock is only really needed after the first thread is created.
678 simple_lock_init(&printf_lock
, 0);
679 simple_lock_init(&bsd_log_spinlock
, 0);
686 simple_lock(&bsd_log_spinlock
);
692 simple_unlock(&bsd_log_spinlock
);
695 /* derived from boot_gets */
703 char *strmax
= str
+ maxlen
- 1; /* allow space for trailing 0 */
731 if (c
>= ' ' && c
< '\177') {
737 printf("%c", '\007'); /* beep */
744 extern int disableConsoleOutput
;
750 if ((debug_mode
&& !disable_debug_output
) || !disableConsoleOutput
)
763 if ((debug_mode
&& !disable_debug_output
) || !disableConsoleOutput
)
768 printf(const char *fmt
, ...)
773 disable_preemption();
774 va_start(listp
, fmt
);
775 _doprnt(fmt
, &listp
, conslog_putc
, 16);
783 consdebug_putc(char c
)
785 if ((debug_mode
&& !disable_debug_output
) || !disableConsoleOutput
)
790 if (!console_is_serial())
791 if (!disable_serial_output
)
796 consdebug_putc_unbuffered(char c
)
798 if ((debug_mode
&& !disable_debug_output
) || !disableConsoleOutput
)
799 cnputc_unbuffered(c
);
803 if (!console_is_serial())
804 if (!disable_serial_output
)
809 consdebug_log(char c
)
815 kdb_printf(const char *fmt
, ...)
819 va_start(listp
, fmt
);
820 _doprnt(fmt
, &listp
, consdebug_putc
, 16);
826 kdb_log(const char *fmt
, ...)
830 va_start(listp
, fmt
);
831 _doprnt(fmt
, &listp
, consdebug_log
, 16);
837 kdb_printf_unbuffered(const char *fmt
, ...)
841 va_start(listp
, fmt
);
842 _doprnt(fmt
, &listp
, consdebug_putc_unbuffered
, 16);
850 copybyte(int c
, void *arg
)
853 * arg is a pointer (outside pointer) to the pointer
854 * (inside pointer) which points to the character.
855 * We pass a double pointer, so that we can increment
856 * the inside pointer.
858 char** p
= arg
; /* cast outside pointer */
859 **p
= c
; /* store character */
860 (*p
)++; /* increment inside pointer */
864 * Deprecation Warning:
865 * sprintf() is being deprecated. Please use snprintf() instead.
868 sprintf(char *buf
, const char *fmt
, ...)
873 va_start(listp
, fmt
);
875 __doprnt(fmt
, listp
, copybyte
, ©byte_str
, 16);
877 *copybyte_str
= '\0';
878 return (int)strlen(buf
);
880 #endif /* !CONFIG_EMBEDDED */