]> git.saurik.com Git - apple/xnu.git/blob - libkern/gen/OSDebug.cpp
7afc03ad490860a34b37bbf561b4310b848b93a2
[apple/xnu.git] / libkern / gen / OSDebug.cpp
1 /*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 // NOTE: This file is only c++ so I can get static initialisers going
25 #include <libkern/OSDebug.h>
26
27 #include <sys/cdefs.h>
28
29 #include <stdarg.h>
30 #include <mach/mach_types.h>
31 #include <mach/kmod.h>
32 #include <kern/lock.h>
33
34 #include <libkern/libkern.h> // From bsd's libkern directory
35
36 __BEGIN_DECLS
37 // From osmfk/kern/thread.h but considered to be private
38 extern vm_offset_t min_valid_stack_address(void);
39 extern vm_offset_t max_valid_stack_address(void);
40
41 // From osfmk/kmod.c
42 extern void kmod_dump_log(vm_offset_t *addr, unsigned int cnt);
43 __END_DECLS
44
45 static mutex_t *sOSReportLock = mutex_alloc(0);
46
47 /* Report a message with a 4 entry backtrace - very slow */
48 void
49 OSReportWithBacktrace(const char *str, ...)
50 {
51 char buf[128];
52 void *bt[9];
53 const unsigned cnt = sizeof(bt) / sizeof(bt[0]);
54 va_list listp;
55
56 // Ignore the our and our callers stackframes, skipping frames 0 & 1
57 (void) OSBacktrace(bt, cnt);
58
59 va_start(listp, str);
60 vsnprintf(buf, sizeof(buf), str, listp);
61 va_end(listp);
62
63 mutex_lock(sOSReportLock);
64 {
65 printf("%s\nBacktrace %p %p %p %p %p %p %p\n",
66 buf, bt[2], bt[3], bt[4], bt[5], bt[6], bt[7], bt[8]);
67 kmod_dump_log((vm_offset_t *) &bt[2], cnt - 2);
68 }
69 mutex_unlock(sOSReportLock);
70 }
71
72 static vm_offset_t minstackaddr = min_valid_stack_address();
73 static vm_offset_t maxstackaddr = max_valid_stack_address();
74
75 unsigned OSBacktrace(void **bt, unsigned maxAddrs)
76 {
77 unsigned frame;
78
79 #if __ppc__
80 vm_offset_t stackptr, stackptr_prev;
81 const vm_offset_t * const mem = (vm_offset_t *) 0;
82 unsigned i = 0;
83
84 __asm__ volatile("mflr %0" : "=r" (stackptr));
85 bt[i++] = (void *) stackptr;
86
87 __asm__ volatile("mr %0,r1" : "=r" (stackptr));
88 for ( ; i < maxAddrs; i++) {
89 // Validate we have a reasonable stackptr
90 if ( !(minstackaddr <= stackptr && stackptr < maxstackaddr)
91 || (stackptr & 3))
92 break;
93
94 stackptr_prev = stackptr;
95 stackptr = mem[stackptr_prev >> 2];
96 if ((stackptr_prev ^ stackptr) > 8 * 1024) // Sanity check
97 break;
98
99 vm_offset_t addr = mem[(stackptr >> 2) + 2];
100 if ((addr & 3) || (addr < 0x8000)) // More sanity checks
101 break;
102 bt[i] = (void *) addr;
103 }
104 frame = i;
105
106 for ( ; i < maxAddrs; i++)
107 bt[i] = (void *) 0;
108 #elif 0 && __i386__ // Note that this should be ported for i386
109 // This function is not safe, we should get this code ported appropriately
110 if (maxAddrs > 16) {
111 for (frame = 16; frame < maxAddrs; frame++)
112 bt[frame] = __builtin_return_address(frame);
113 maxAddrs = 16;
114 }
115
116 switch(maxAddrs) {
117 case 15+1: bt[15] = __builtin_return_address(15);
118 case 14+1: bt[14] = __builtin_return_address(14);
119 case 13+1: bt[13] = __builtin_return_address(13);
120 case 12+1: bt[12] = __builtin_return_address(12);
121 case 11+1: bt[11] = __builtin_return_address(11);
122 case 10+1: bt[10] = __builtin_return_address(10);
123 case 9+1: bt[ 9] = __builtin_return_address( 9);
124 case 8+1: bt[ 8] = __builtin_return_address( 8);
125 case 7+1: bt[ 7] = __builtin_return_address( 7);
126 case 6+1: bt[ 6] = __builtin_return_address( 6);
127 case 5+1: bt[ 5] = __builtin_return_address( 5);
128 case 4+1: bt[ 4] = __builtin_return_address( 4);
129 case 3+1: bt[ 3] = __builtin_return_address( 3);
130 case 2+1: bt[ 2] = __builtin_return_address( 2);
131 case 1+1: bt[ 1] = __builtin_return_address( 1);
132 case 0+1: bt[ 0] = __builtin_return_address( 0);
133 case 0: default: break;
134 }
135
136 frame = maxAddrs;
137 #else
138 // This function is not safe, we should get this code ported appropriately
139 if (maxAddrs > 16) {
140 for (frame = 16; frame < maxAddrs; frame++)
141 bt[frame] = 0;
142 maxAddrs = 16;
143 }
144
145 switch (maxAddrs) {
146 case 15+1: bt[15] = __builtin_return_address(15);
147 case 14+1: bt[14] = __builtin_return_address(14);
148 case 13+1: bt[13] = __builtin_return_address(13);
149 case 12+1: bt[12] = __builtin_return_address(12);
150 case 11+1: bt[11] = __builtin_return_address(11);
151 case 10+1: bt[10] = __builtin_return_address(10);
152 case 9+1: bt[ 9] = __builtin_return_address( 9);
153 case 8+1: bt[ 8] = __builtin_return_address( 8);
154 case 7+1: bt[ 7] = __builtin_return_address( 7);
155 case 6+1: bt[ 6] = __builtin_return_address( 6);
156 case 5+1: bt[ 5] = __builtin_return_address( 5);
157 case 4+1: bt[ 4] = __builtin_return_address( 4);
158 case 3+1: bt[ 3] = __builtin_return_address( 3);
159 case 2+1: bt[ 2] = __builtin_return_address( 2);
160 case 1+1: bt[ 1] = __builtin_return_address( 1);
161 case 0+1: bt[ 0] = __builtin_return_address( 0);
162 case 0:
163 default :
164 break;
165 }
166
167 frame = maxAddrs;
168 #endif
169
170 return frame;
171 }