]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
0c530ab8 | 2 | * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | ||
32 | /* | |
33 | * Mach Operating System | |
34 | * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University | |
35 | * All Rights Reserved. | |
36 | * | |
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. | |
42 | * | |
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. | |
46 | * | |
47 | * Carnegie Mellon requests users of this software to return to | |
48 | * | |
49 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU | |
50 | * School of Computer Science | |
51 | * Carnegie Mellon University | |
52 | * Pittsburgh PA 15213-3890 | |
53 | * | |
54 | * any improvements or extensions that they make and grant Carnegie Mellon | |
55 | * the rights to redistribute these changes. | |
56 | */ | |
57 | ||
58 | /* | |
59 | * Common code for printf et al. | |
60 | * | |
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. | |
67 | * | |
68 | * To write, for example, fprintf() using this routine, the code | |
69 | * | |
70 | * fprintf(fd, format, args) | |
71 | * FILE *fd; | |
72 | * char *format; | |
73 | * { | |
74 | * _doprnt(format, &args, fd); | |
75 | * } | |
76 | * | |
77 | * would suffice. (This example does not handle the fprintf's "return | |
78 | * value" correctly, but who looks at the return value of fprintf | |
79 | * anyway?) | |
80 | * | |
81 | * This version implements the following printf features: | |
82 | * | |
83 | * %d decimal conversion | |
84 | * %u unsigned conversion | |
85 | * %x hexadecimal conversion | |
86 | * %X hexadecimal conversion with capital letters | |
9bccf70c A |
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 | |
1c79356b A |
89 | * %o octal conversion |
90 | * %c character | |
91 | * %s string | |
92 | * %m.n field width, precision | |
93 | * %-m.n left adjustment | |
94 | * %0m.n zero-padding | |
95 | * %*.* width and precision taken from arguments | |
96 | * | |
b0d623f7 | 97 | * This version does not implement %f, %e, or %g. |
1c79356b A |
98 | * |
99 | * As mentioned, this version does not return any reasonable value. | |
100 | * | |
101 | * Permission is granted to use, modify, or propagate this code as | |
102 | * long as this notice is incorporated. | |
103 | * | |
104 | * Steve Summit 3/25/87 | |
9bccf70c A |
105 | * |
106 | * Tweaked for long long support and extended to support the hexdump %D | |
107 | * specifier by dbg 05/02/02. | |
1c79356b A |
108 | */ |
109 | ||
110 | /* | |
111 | * Added formats for decoding device registers: | |
112 | * | |
113 | * printf("reg = %b", regval, "<base><arg>*") | |
114 | * | |
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") | |
121 | * would produce | |
122 | * reg = 3<BITTWO,BITONE> | |
123 | * | |
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") | |
129 | * would produce | |
130 | * reg = b<FIELD1=2,BITONE> | |
131 | * | |
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. | |
135 | */ | |
136 | /* | |
137 | * Added for general use: | |
138 | * # prefix for alternate format: | |
139 | * 0x (0X) for hex | |
140 | * leading 0 for octal | |
141 | * + print '+' if positive | |
142 | * blank print ' ' if positive | |
143 | * | |
144 | * z signed hexadecimal | |
145 | * r signed, 'radix' | |
146 | * n unsigned, 'radix' | |
147 | * | |
148 | * D,U,O,Z same as corresponding lower-case versions | |
149 | * (compatibility) | |
150 | */ | |
9bccf70c A |
151 | /* |
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. | |
155 | */ | |
1c79356b | 156 | |
55e303ae A |
157 | #include <debug.h> |
158 | #include <mach_kdb.h> | |
159 | #include <mach_kdp.h> | |
1c79356b A |
160 | #include <platforms.h> |
161 | #include <mach/boolean.h> | |
1c79356b A |
162 | #include <kern/cpu_number.h> |
163 | #include <kern/lock.h> | |
164 | #include <kern/thread.h> | |
165 | #include <kern/sched_prim.h> | |
166 | #include <kern/misc_protos.h> | |
167 | #include <stdarg.h> | |
168 | #include <string.h> | |
169 | #include <mach_assert.h> | |
170 | #ifdef MACH_BSD | |
171 | #include <sys/msgbuf.h> | |
172 | #endif | |
2d21ac55 | 173 | #include <console/serial_protos.h> |
1c79356b A |
174 | |
175 | #ifdef __ppc__ | |
176 | #include <ppc/Firmware.h> | |
177 | #endif | |
178 | ||
1c79356b A |
179 | #define isdigit(d) ((d) >= '0' && (d) <= '9') |
180 | #define Ctod(c) ((c) - '0') | |
181 | ||
9bccf70c A |
182 | #define MAXBUF (sizeof(long long int) * 8) /* enough for binary */ |
183 | static char digs[] = "0123456789abcdef"; | |
1c79356b | 184 | |
2d21ac55 A |
185 | |
186 | #if CONFIG_NO_PRINTF_STRINGS | |
b0d623f7 A |
187 | /* Prevent CPP from breaking the definition below */ |
188 | #undef printf | |
2d21ac55 A |
189 | #endif |
190 | ||
b0d623f7 A |
191 | int _consume_printf_args(int a __unused, ...) |
192 | { | |
193 | return 0; | |
194 | } | |
195 | void _consume_kprintf_args(int a __unused, ...) | |
196 | { | |
197 | } | |
198 | ||
9bccf70c | 199 | static int |
1c79356b | 200 | printnum( |
2d21ac55 A |
201 | unsigned long long int u, /* number to print */ |
202 | int base, | |
9bccf70c A |
203 | void (*putc)(int, void *), |
204 | void *arg) | |
1c79356b A |
205 | { |
206 | char buf[MAXBUF]; /* build number here */ | |
2d21ac55 | 207 | char * p = &buf[MAXBUF-1]; |
9bccf70c | 208 | int nprinted = 0; |
1c79356b A |
209 | |
210 | do { | |
211 | *p-- = digs[u % base]; | |
212 | u /= base; | |
213 | } while (u != 0); | |
214 | ||
9bccf70c A |
215 | while (++p != &buf[MAXBUF]) { |
216 | (*putc)(*p, arg); | |
217 | nprinted++; | |
218 | } | |
1c79356b | 219 | |
9bccf70c | 220 | return nprinted; |
1c79356b A |
221 | } |
222 | ||
223 | boolean_t _doprnt_truncates = FALSE; | |
224 | ||
9bccf70c A |
225 | int |
226 | __doprnt( | |
2d21ac55 | 227 | const char *fmt, |
b0d623f7 | 228 | va_list argp, |
1c79356b | 229 | /* character output routine */ |
9bccf70c A |
230 | void (*putc)(int, void *arg), |
231 | void *arg, | |
1c79356b A |
232 | int radix) /* default radix - for '%r' */ |
233 | { | |
234 | int length; | |
235 | int prec; | |
236 | boolean_t ladjust; | |
237 | char padc; | |
9bccf70c A |
238 | long long n; |
239 | unsigned long long u; | |
1c79356b A |
240 | int plus_sign; |
241 | int sign_char; | |
242 | boolean_t altfmt, truncate; | |
243 | int base; | |
2d21ac55 | 244 | char c; |
1c79356b | 245 | int capitals; |
9bccf70c A |
246 | int long_long; |
247 | int nprinted = 0; | |
1c79356b A |
248 | |
249 | while ((c = *fmt) != '\0') { | |
250 | if (c != '%') { | |
9bccf70c A |
251 | (*putc)(c, arg); |
252 | nprinted++; | |
1c79356b A |
253 | fmt++; |
254 | continue; | |
255 | } | |
256 | ||
257 | fmt++; | |
258 | ||
9bccf70c | 259 | long_long = 0; |
1c79356b A |
260 | length = 0; |
261 | prec = -1; | |
262 | ladjust = FALSE; | |
263 | padc = ' '; | |
264 | plus_sign = 0; | |
265 | sign_char = 0; | |
266 | altfmt = FALSE; | |
267 | ||
268 | while (TRUE) { | |
269 | c = *fmt; | |
270 | if (c == '#') { | |
271 | altfmt = TRUE; | |
272 | } | |
273 | else if (c == '-') { | |
274 | ladjust = TRUE; | |
275 | } | |
276 | else if (c == '+') { | |
277 | plus_sign = '+'; | |
278 | } | |
279 | else if (c == ' ') { | |
280 | if (plus_sign == 0) | |
281 | plus_sign = ' '; | |
282 | } | |
283 | else | |
284 | break; | |
285 | fmt++; | |
286 | } | |
287 | ||
288 | if (c == '0') { | |
289 | padc = '0'; | |
290 | c = *++fmt; | |
291 | } | |
292 | ||
293 | if (isdigit(c)) { | |
294 | while(isdigit(c)) { | |
295 | length = 10 * length + Ctod(c); | |
296 | c = *++fmt; | |
297 | } | |
298 | } | |
299 | else if (c == '*') { | |
b0d623f7 | 300 | length = va_arg(argp, int); |
1c79356b A |
301 | c = *++fmt; |
302 | if (length < 0) { | |
303 | ladjust = !ladjust; | |
304 | length = -length; | |
305 | } | |
306 | } | |
307 | ||
308 | if (c == '.') { | |
309 | c = *++fmt; | |
310 | if (isdigit(c)) { | |
311 | prec = 0; | |
312 | while(isdigit(c)) { | |
313 | prec = 10 * prec + Ctod(c); | |
314 | c = *++fmt; | |
315 | } | |
316 | } | |
317 | else if (c == '*') { | |
b0d623f7 | 318 | prec = va_arg(argp, int); |
1c79356b A |
319 | c = *++fmt; |
320 | } | |
321 | } | |
322 | ||
9bccf70c | 323 | if (c == 'l') { |
1c79356b | 324 | c = *++fmt; /* need it if sizeof(int) < sizeof(long) */ |
b0d623f7 A |
325 | if (sizeof(int)<sizeof(long)) |
326 | long_long = 1; | |
9bccf70c A |
327 | if (c == 'l') { |
328 | long_long = 1; | |
329 | c = *++fmt; | |
330 | } | |
331 | } else if (c == 'q' || c == 'L') { | |
332 | long_long = 1; | |
333 | c = *++fmt; | |
334 | } | |
1c79356b A |
335 | |
336 | truncate = FALSE; | |
337 | capitals=0; /* Assume lower case printing */ | |
338 | ||
339 | switch(c) { | |
340 | case 'b': | |
341 | case 'B': | |
342 | { | |
343 | register char *p; | |
344 | boolean_t any; | |
345 | register int i; | |
346 | ||
9bccf70c | 347 | if (long_long) { |
b0d623f7 | 348 | u = va_arg(argp, unsigned long long); |
9bccf70c | 349 | } else { |
b0d623f7 | 350 | u = va_arg(argp, unsigned int); |
9bccf70c | 351 | } |
b0d623f7 | 352 | p = va_arg(argp, char *); |
1c79356b | 353 | base = *p++; |
9bccf70c | 354 | nprinted += printnum(u, base, putc, arg); |
1c79356b A |
355 | |
356 | if (u == 0) | |
357 | break; | |
358 | ||
359 | any = FALSE; | |
360 | while ((i = *p++) != '\0') { | |
361 | if (*fmt == 'B') | |
362 | i = 33 - i; | |
363 | if (*p <= 32) { | |
364 | /* | |
365 | * Bit field | |
366 | */ | |
367 | register int j; | |
368 | if (any) | |
9bccf70c | 369 | (*putc)(',', arg); |
1c79356b | 370 | else { |
9bccf70c | 371 | (*putc)('<', arg); |
1c79356b A |
372 | any = TRUE; |
373 | } | |
9bccf70c | 374 | nprinted++; |
1c79356b A |
375 | j = *p++; |
376 | if (*fmt == 'B') | |
377 | j = 32 - j; | |
9bccf70c A |
378 | for (; (c = *p) > 32; p++) { |
379 | (*putc)(c, arg); | |
380 | nprinted++; | |
381 | } | |
382 | nprinted += printnum((unsigned)( (u>>(j-1)) & ((2<<(i-j))-1)), | |
383 | base, putc, arg); | |
1c79356b A |
384 | } |
385 | else if (u & (1<<(i-1))) { | |
386 | if (any) | |
9bccf70c | 387 | (*putc)(',', arg); |
1c79356b | 388 | else { |
9bccf70c | 389 | (*putc)('<', arg); |
1c79356b A |
390 | any = TRUE; |
391 | } | |
9bccf70c A |
392 | nprinted++; |
393 | for (; (c = *p) > 32; p++) { | |
394 | (*putc)(c, arg); | |
395 | nprinted++; | |
396 | } | |
1c79356b A |
397 | } |
398 | else { | |
399 | for (; *p > 32; p++) | |
400 | continue; | |
401 | } | |
402 | } | |
9bccf70c A |
403 | if (any) { |
404 | (*putc)('>', arg); | |
405 | nprinted++; | |
406 | } | |
1c79356b A |
407 | break; |
408 | } | |
409 | ||
410 | case 'c': | |
b0d623f7 | 411 | c = va_arg(argp, int); |
9bccf70c A |
412 | (*putc)(c, arg); |
413 | nprinted++; | |
1c79356b A |
414 | break; |
415 | ||
416 | case 's': | |
417 | { | |
2d21ac55 A |
418 | register const char *p; |
419 | register const char *p2; | |
1c79356b A |
420 | |
421 | if (prec == -1) | |
422 | prec = 0x7fffffff; /* MAXINT */ | |
423 | ||
b0d623f7 | 424 | p = va_arg(argp, char *); |
1c79356b | 425 | |
2d21ac55 | 426 | if (p == NULL) |
1c79356b A |
427 | p = ""; |
428 | ||
429 | if (length > 0 && !ladjust) { | |
430 | n = 0; | |
431 | p2 = p; | |
432 | ||
433 | for (; *p != '\0' && n < prec; p++) | |
434 | n++; | |
435 | ||
436 | p = p2; | |
437 | ||
438 | while (n < length) { | |
9bccf70c | 439 | (*putc)(' ', arg); |
1c79356b | 440 | n++; |
9bccf70c | 441 | nprinted++; |
1c79356b A |
442 | } |
443 | } | |
444 | ||
445 | n = 0; | |
446 | ||
cf7d32b8 A |
447 | while ((n < prec) && (!(length > 0 && n >= length))) { |
448 | if (*p == '\0') { | |
449 | break; | |
450 | } | |
451 | (*putc)(*p++, arg); | |
452 | nprinted++; | |
453 | n++; | |
1c79356b A |
454 | } |
455 | ||
456 | if (n < length && ladjust) { | |
457 | while (n < length) { | |
9bccf70c | 458 | (*putc)(' ', arg); |
1c79356b | 459 | n++; |
9bccf70c | 460 | nprinted++; |
1c79356b A |
461 | } |
462 | } | |
463 | ||
464 | break; | |
465 | } | |
466 | ||
467 | case 'o': | |
468 | truncate = _doprnt_truncates; | |
469 | case 'O': | |
470 | base = 8; | |
471 | goto print_unsigned; | |
472 | ||
9bccf70c A |
473 | case 'D': { |
474 | unsigned char *up; | |
475 | char *q, *p; | |
476 | ||
b0d623f7 A |
477 | up = (unsigned char *)va_arg(argp, unsigned char *); |
478 | p = (char *)va_arg(argp, char *); | |
9bccf70c A |
479 | if (length == -1) |
480 | length = 16; | |
481 | while(length--) { | |
482 | (*putc)(digs[(*up >> 4)], arg); | |
483 | (*putc)(digs[(*up & 0x0f)], arg); | |
484 | nprinted += 2; | |
485 | up++; | |
486 | if (length) { | |
487 | for (q=p;*q;q++) { | |
488 | (*putc)(*q, arg); | |
489 | nprinted++; | |
490 | } | |
491 | } | |
492 | } | |
493 | break; | |
494 | } | |
495 | ||
1c79356b A |
496 | case 'd': |
497 | truncate = _doprnt_truncates; | |
1c79356b A |
498 | base = 10; |
499 | goto print_signed; | |
500 | ||
501 | case 'u': | |
502 | truncate = _doprnt_truncates; | |
503 | case 'U': | |
504 | base = 10; | |
505 | goto print_unsigned; | |
506 | ||
507 | case 'p': | |
508 | altfmt = TRUE; | |
b0d623f7 A |
509 | if (sizeof(int)<sizeof(void *)) { |
510 | long_long = 1; | |
511 | } | |
1c79356b A |
512 | case 'x': |
513 | truncate = _doprnt_truncates; | |
514 | base = 16; | |
515 | goto print_unsigned; | |
516 | ||
517 | case 'X': | |
518 | base = 16; | |
519 | capitals=16; /* Print in upper case */ | |
520 | goto print_unsigned; | |
521 | ||
522 | case 'z': | |
523 | truncate = _doprnt_truncates; | |
524 | base = 16; | |
525 | goto print_signed; | |
526 | ||
527 | case 'Z': | |
528 | base = 16; | |
529 | capitals=16; /* Print in upper case */ | |
530 | goto print_signed; | |
531 | ||
532 | case 'r': | |
533 | truncate = _doprnt_truncates; | |
534 | case 'R': | |
535 | base = radix; | |
536 | goto print_signed; | |
537 | ||
538 | case 'n': | |
539 | truncate = _doprnt_truncates; | |
540 | case 'N': | |
541 | base = radix; | |
542 | goto print_unsigned; | |
543 | ||
544 | print_signed: | |
9bccf70c | 545 | if (long_long) { |
b0d623f7 | 546 | n = va_arg(argp, long long); |
9bccf70c | 547 | } else { |
b0d623f7 | 548 | n = va_arg(argp, int); |
9bccf70c | 549 | } |
1c79356b A |
550 | if (n >= 0) { |
551 | u = n; | |
552 | sign_char = plus_sign; | |
553 | } | |
554 | else { | |
555 | u = -n; | |
556 | sign_char = '-'; | |
557 | } | |
558 | goto print_num; | |
559 | ||
560 | print_unsigned: | |
9bccf70c | 561 | if (long_long) { |
b0d623f7 | 562 | u = va_arg(argp, unsigned long long); |
9bccf70c | 563 | } else { |
b0d623f7 | 564 | u = va_arg(argp, unsigned int); |
9bccf70c | 565 | } |
1c79356b A |
566 | goto print_num; |
567 | ||
568 | print_num: | |
569 | { | |
570 | char buf[MAXBUF]; /* build number here */ | |
571 | register char * p = &buf[MAXBUF-1]; | |
572 | static char digits[] = "0123456789abcdef0123456789ABCDEF"; | |
2d21ac55 | 573 | const char *prefix = NULL; |
1c79356b | 574 | |
9bccf70c | 575 | if (truncate) u = (long long)((int)(u)); |
1c79356b A |
576 | |
577 | if (u != 0 && altfmt) { | |
578 | if (base == 8) | |
579 | prefix = "0"; | |
580 | else if (base == 16) | |
581 | prefix = "0x"; | |
582 | } | |
583 | ||
584 | do { | |
585 | /* Print in the correct case */ | |
586 | *p-- = digits[(u % base)+capitals]; | |
587 | u /= base; | |
588 | } while (u != 0); | |
589 | ||
b0d623f7 | 590 | length -= (int)(&buf[MAXBUF-1] - p); |
1c79356b A |
591 | if (sign_char) |
592 | length--; | |
593 | if (prefix) | |
b0d623f7 | 594 | length -= (int)strlen(prefix); |
1c79356b A |
595 | |
596 | if (padc == ' ' && !ladjust) { | |
597 | /* blank padding goes before prefix */ | |
9bccf70c A |
598 | while (--length >= 0) { |
599 | (*putc)(' ', arg); | |
600 | nprinted++; | |
601 | } | |
602 | } | |
603 | if (sign_char) { | |
604 | (*putc)(sign_char, arg); | |
605 | nprinted++; | |
606 | } | |
607 | if (prefix) { | |
608 | while (*prefix) { | |
609 | (*putc)(*prefix++, arg); | |
610 | nprinted++; | |
611 | } | |
1c79356b | 612 | } |
1c79356b A |
613 | if (padc == '0') { |
614 | /* zero padding goes after sign and prefix */ | |
9bccf70c A |
615 | while (--length >= 0) { |
616 | (*putc)('0', arg); | |
617 | nprinted++; | |
618 | } | |
1c79356b | 619 | } |
9bccf70c A |
620 | while (++p != &buf[MAXBUF]) { |
621 | (*putc)(*p, arg); | |
622 | nprinted++; | |
623 | } | |
624 | ||
1c79356b | 625 | if (ladjust) { |
9bccf70c A |
626 | while (--length >= 0) { |
627 | (*putc)(' ', arg); | |
628 | nprinted++; | |
629 | } | |
1c79356b A |
630 | } |
631 | break; | |
632 | } | |
633 | ||
634 | case '\0': | |
635 | fmt--; | |
636 | break; | |
637 | ||
638 | default: | |
9bccf70c A |
639 | (*putc)(c, arg); |
640 | nprinted++; | |
1c79356b A |
641 | } |
642 | fmt++; | |
643 | } | |
9bccf70c A |
644 | |
645 | return nprinted; | |
646 | } | |
647 | ||
648 | static void | |
649 | dummy_putc(int ch, void *arg) | |
650 | { | |
651 | void (*real_putc)(char) = arg; | |
652 | ||
653 | real_putc(ch); | |
654 | } | |
655 | ||
656 | void | |
657 | _doprnt( | |
658 | register const char *fmt, | |
659 | va_list *argp, | |
660 | /* character output routine */ | |
661 | void (*putc)(char), | |
662 | int radix) /* default radix - for '%r' */ | |
663 | { | |
b0d623f7 | 664 | __doprnt(fmt, *argp, dummy_putc, putc, radix); |
1c79356b A |
665 | } |
666 | ||
667 | #if MP_PRINTF | |
668 | boolean_t new_printf_cpu_number = FALSE; | |
669 | #endif /* MP_PRINTF */ | |
670 | ||
671 | ||
672 | decl_simple_lock_data(,printf_lock) | |
91447636 | 673 | decl_simple_lock_data(,bsd_log_spinlock) |
91447636 A |
674 | extern void bsd_log_init(void); |
675 | void bsd_log_lock(void); | |
676 | void bsd_log_unlock(void); | |
1c79356b A |
677 | |
678 | void | |
679 | printf_init(void) | |
680 | { | |
681 | /* | |
682 | * Lock is only really needed after the first thread is created. | |
683 | */ | |
91447636 A |
684 | simple_lock_init(&printf_lock, 0); |
685 | simple_lock_init(&bsd_log_spinlock, 0); | |
686 | bsd_log_init(); | |
91447636 A |
687 | } |
688 | ||
689 | void | |
2d21ac55 | 690 | bsd_log_lock(void) |
91447636 A |
691 | { |
692 | simple_lock(&bsd_log_spinlock); | |
693 | } | |
694 | ||
695 | void | |
2d21ac55 | 696 | bsd_log_unlock(void) |
91447636 A |
697 | { |
698 | simple_unlock(&bsd_log_spinlock); | |
1c79356b A |
699 | } |
700 | ||
701 | /* derived from boot_gets */ | |
702 | void | |
703 | safe_gets( | |
704 | char *str, | |
705 | int maxlen) | |
706 | { | |
707 | register char *lp; | |
708 | register int c; | |
709 | char *strmax = str + maxlen - 1; /* allow space for trailing 0 */ | |
710 | ||
711 | lp = str; | |
712 | for (;;) { | |
713 | c = cngetc(); | |
714 | switch (c) { | |
715 | case '\n': | |
716 | case '\r': | |
717 | printf("\n"); | |
718 | *lp++ = 0; | |
719 | return; | |
720 | ||
721 | case '\b': | |
722 | case '#': | |
723 | case '\177': | |
724 | if (lp > str) { | |
725 | printf("\b \b"); | |
726 | lp--; | |
727 | } | |
728 | continue; | |
729 | ||
730 | case '@': | |
731 | case 'u'&037: | |
732 | lp = str; | |
733 | printf("\n\r"); | |
734 | continue; | |
735 | ||
736 | default: | |
737 | if (c >= ' ' && c < '\177') { | |
738 | if (lp < strmax) { | |
739 | *lp++ = c; | |
740 | printf("%c", c); | |
741 | } | |
742 | else { | |
743 | printf("%c", '\007'); /* beep */ | |
744 | } | |
745 | } | |
746 | } | |
747 | } | |
748 | } | |
749 | ||
2d21ac55 A |
750 | extern int disableConsoleOutput; |
751 | ||
1c79356b A |
752 | void |
753 | conslog_putc( | |
754 | char c) | |
755 | { | |
2d21ac55 | 756 | if ((debug_mode && !disable_debug_output) || !disableConsoleOutput) |
1c79356b A |
757 | cnputc(c); |
758 | ||
759 | #ifdef MACH_BSD | |
593a1d5f A |
760 | if (debug_mode == 0) |
761 | log_putc(c); | |
1c79356b A |
762 | #endif |
763 | } | |
764 | ||
2d21ac55 A |
765 | #if MACH_KDB |
766 | extern void db_putchar(char c); | |
767 | #endif | |
768 | ||
55e303ae | 769 | void |
2d21ac55 | 770 | dbugprintf(__unused const char *fmt, ...) |
55e303ae A |
771 | { |
772 | ||
773 | #if MACH_KDB | |
55e303ae A |
774 | va_list listp; |
775 | ||
776 | va_start(listp, fmt); | |
777 | _doprnt(fmt, &listp, db_putchar, 16); | |
778 | va_end(listp); | |
779 | #endif | |
780 | return; | |
781 | } | |
782 | ||
2d21ac55 | 783 | int |
1c79356b A |
784 | printf(const char *fmt, ...) |
785 | { | |
786 | va_list listp; | |
787 | ||
2d21ac55 A |
788 | if (fmt) { |
789 | disable_preemption(); | |
790 | va_start(listp, fmt); | |
791 | _doprnt(fmt, &listp, conslog_putc, 16); | |
792 | va_end(listp); | |
793 | enable_preemption(); | |
794 | } | |
795 | return 0; | |
1c79356b A |
796 | } |
797 | ||
9bccf70c | 798 | void |
2d21ac55 | 799 | consdebug_putc(char c) |
9bccf70c | 800 | { |
2d21ac55 | 801 | if ((debug_mode && !disable_debug_output) || !disableConsoleOutput) |
9bccf70c A |
802 | cnputc(c); |
803 | ||
804 | debug_putc(c); | |
91447636 | 805 | |
91447636 | 806 | if (!console_is_serial()) |
2d21ac55 | 807 | if (!disable_serial_output) |
0c530ab8 | 808 | PE_kputc(c); |
9bccf70c A |
809 | } |
810 | ||
b0d623f7 A |
811 | void |
812 | consdebug_putc_unbuffered(char c) | |
813 | { | |
814 | if ((debug_mode && !disable_debug_output) || !disableConsoleOutput) | |
815 | cnputc_unbuffered(c); | |
816 | ||
817 | debug_putc(c); | |
818 | ||
819 | if (!console_is_serial()) | |
820 | if (!disable_serial_output) | |
821 | PE_kputc(c); | |
822 | } | |
c910b4d9 A |
823 | |
824 | void | |
825 | consdebug_log(char c) | |
826 | { | |
827 | debug_putc(c); | |
828 | } | |
829 | ||
2d21ac55 | 830 | int |
9bccf70c A |
831 | kdb_printf(const char *fmt, ...) |
832 | { | |
833 | va_list listp; | |
834 | ||
835 | va_start(listp, fmt); | |
836 | _doprnt(fmt, &listp, consdebug_putc, 16); | |
837 | va_end(listp); | |
2d21ac55 | 838 | return 0; |
9bccf70c A |
839 | } |
840 | ||
c910b4d9 A |
841 | int |
842 | kdb_log(const char *fmt, ...) | |
843 | { | |
844 | va_list listp; | |
845 | ||
846 | va_start(listp, fmt); | |
847 | _doprnt(fmt, &listp, consdebug_log, 16); | |
848 | va_end(listp); | |
849 | return 0; | |
850 | } | |
851 | ||
b0d623f7 A |
852 | int |
853 | kdb_printf_unbuffered(const char *fmt, ...) | |
854 | { | |
855 | va_list listp; | |
856 | ||
857 | va_start(listp, fmt); | |
858 | _doprnt(fmt, &listp, consdebug_putc_unbuffered, 16); | |
859 | va_end(listp); | |
860 | return 0; | |
861 | } | |
862 | ||
1c79356b | 863 | static void |
2d21ac55 | 864 | copybyte(int c, void *arg) |
1c79356b | 865 | { |
2d21ac55 A |
866 | /* |
867 | * arg is a pointer (outside pointer) to the pointer | |
868 | * (inside pointer) which points to the character. | |
869 | * We pass a double pointer, so that we can increment | |
870 | * the inside pointer. | |
871 | */ | |
872 | char** p = arg; /* cast outside pointer */ | |
873 | **p = c; /* store character */ | |
874 | (*p)++; /* increment inside pointer */ | |
1c79356b A |
875 | } |
876 | ||
2d21ac55 A |
877 | /* |
878 | * Deprecation Warning: | |
879 | * sprintf() is being deprecated. Please use snprintf() instead. | |
880 | */ | |
1c79356b A |
881 | int |
882 | sprintf(char *buf, const char *fmt, ...) | |
883 | { | |
884 | va_list listp; | |
2d21ac55 | 885 | char *copybyte_str; |
1c79356b A |
886 | |
887 | va_start(listp, fmt); | |
1c79356b | 888 | copybyte_str = buf; |
b0d623f7 | 889 | __doprnt(fmt, listp, copybyte, ©byte_str, 16); |
1c79356b | 890 | va_end(listp); |
2d21ac55 | 891 | *copybyte_str = '\0'; |
b0d623f7 | 892 | return (int)strlen(buf); |
1c79356b | 893 | } |