]> git.saurik.com Git - apple/bootx.git/blob - bootx.tproj/libclite.subproj/printf.c
BootX-81.tar.gz
[apple/bootx.git] / bootx.tproj / libclite.subproj / printf.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * printf.c - printf
24 *
25 * Copyright (c) 1998-2002 Apple Computer, Inc.
26 *
27 * DRI: Josh de Cesare
28 */
29
30 #include <libclite.h>
31
32 int printf(const char *format, ...)
33 {
34 va_list ap;
35 va_start(ap, format);
36 prf(format, (unsigned int *)ap, putchar, 0);
37 va_end(ap);
38 return 0;
39 }
40
41
42 #if DEBUG
43 #include <stdint.h>
44
45 #define BTLEN 10
46 // ctx and id optional
47 void dump_backtrace(char *ctx, int id)
48 {
49 void *bt[BTLEN];
50 int cnt;
51
52 if (ctx)
53 printf("%s ", ctx);
54 if (id)
55 printf("(%d)", id);
56 if (ctx || id)
57 printf(":\n");
58
59 cnt = OSBacktrace_ppc(bt, BTLEN);
60 while(cnt <= BTLEN && cnt--)
61 printf("bt[%d]: %x\n", cnt, bt[cnt]);
62 }
63
64 // ported from xnu/libkern/gen/OSDebug.cpp
65 // (non-__ppc__ #if branches and min/maxstackaddr checks omitted)
66 unsigned OSBacktrace_ppc(void **bt, unsigned maxAddrs)
67 {
68 unsigned frame;
69
70 #if __ppc__
71 uint32_t stackptr, stackptr_prev;
72 const uint32_t * const mem = (uint32_t *) 0;
73 unsigned i = 0;
74
75 __asm__ volatile("mflr %0" : "=r" (stackptr));
76 bt[i++] = (void *) stackptr;
77
78 __asm__ volatile("mr %0,r1" : "=r" (stackptr));
79 for ( ; i < maxAddrs; i++) {
80 // Validate we have a reasonable stackptr
81 if ( /* !(minstackaddr <= stackptr && stackptr < maxstackaddr)
82 || */ (stackptr & 3))
83 break;
84
85 stackptr_prev = stackptr;
86 stackptr = mem[stackptr_prev >> 2];
87 if ((stackptr_prev ^ stackptr) > 8 * 1024) // Sanity check
88 break;
89
90 uint32_t addr = mem[(stackptr >> 2) + 2];
91 if ((addr & 3) || (addr < 0x8000)) // More sanity checks
92 break;
93 bt[i] = (void *) addr;
94 }
95 frame = i;
96
97 for ( ; i < maxAddrs; i++)
98 bt[i] = (void *) 0;
99 #else
100 #warning "BootX's OSBacktrace_ppc() not intended for other architectures"
101 #endif
102
103 return frame;
104 }
105 #endif // DEBUG