]> git.saurik.com Git - apple/xnu.git/blame - libkern/gen/OSDebug.cpp
xnu-1228.0.2.tar.gz
[apple/xnu.git] / libkern / gen / OSDebug.cpp
CommitLineData
91447636
A
1/*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
91447636 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@
91447636
A
27 */
28
29// NOTE: This file is only c++ so I can get static initialisers going
30#include <libkern/OSDebug.h>
31
32#include <sys/cdefs.h>
33
34#include <stdarg.h>
35#include <mach/mach_types.h>
36#include <mach/kmod.h>
37#include <kern/lock.h>
38
39#include <libkern/libkern.h> // From bsd's libkern directory
0c530ab8 40#include <mach/vm_param.h>
91447636 41
2d21ac55
A
42#include <sys/kdebug.h>
43extern int etext;
91447636
A
44__BEGIN_DECLS
45// From osmfk/kern/thread.h but considered to be private
46extern vm_offset_t min_valid_stack_address(void);
47extern vm_offset_t max_valid_stack_address(void);
48
49// From osfmk/kmod.c
50extern void kmod_dump_log(vm_offset_t *addr, unsigned int cnt);
0c530ab8
A
51
52extern addr64_t kvtophys(vm_offset_t va);
2d21ac55 53
91447636
A
54__END_DECLS
55
56static mutex_t *sOSReportLock = mutex_alloc(0);
57
2d21ac55
A
58/* Use kernel_debug() to log a backtrace */
59void
60trace_backtrace(unsigned int debugid, unsigned int debugid2, int size, int data) {
61 void *bt[16];
62 const unsigned cnt = sizeof(bt) / sizeof(bt[0]);
63 unsigned i;
64 int found = 0;
65
66 OSBacktrace(bt, cnt);
67
68 /* find first non-kernel frame */
69 for (i = 3; i < cnt && bt[i]; i++) {
70 if (bt[i] > (void*)&etext) {
71 found = 1;
72 break;
73 }
74 }
75 /*
76 * if there are non-kernel frames, only log these
77 * otherwise, log everything but the first two
78 */
79 if (!found) i=2;
80
81#define safe_bt(a) (int)(a<cnt ? bt[a] : 0)
82 kernel_debug(debugid, data, size, safe_bt(i), safe_bt(i+1), 0);
83 kernel_debug(debugid2, safe_bt(i+2), safe_bt(i+3), safe_bt(i+4), safe_bt(i+5), 0);
84}
85
91447636
A
86/* Report a message with a 4 entry backtrace - very slow */
87void
88OSReportWithBacktrace(const char *str, ...)
89{
90 char buf[128];
91 void *bt[9];
92 const unsigned cnt = sizeof(bt) / sizeof(bt[0]);
93 va_list listp;
94
95 // Ignore the our and our callers stackframes, skipping frames 0 & 1
96 (void) OSBacktrace(bt, cnt);
97
98 va_start(listp, str);
99 vsnprintf(buf, sizeof(buf), str, listp);
100 va_end(listp);
101
102 mutex_lock(sOSReportLock);
103 {
104 printf("%s\nBacktrace %p %p %p %p %p %p %p\n",
105 buf, bt[2], bt[3], bt[4], bt[5], bt[6], bt[7], bt[8]);
106 kmod_dump_log((vm_offset_t *) &bt[2], cnt - 2);
107 }
108 mutex_unlock(sOSReportLock);
109}
110
111static vm_offset_t minstackaddr = min_valid_stack_address();
112static vm_offset_t maxstackaddr = max_valid_stack_address();
113
0c530ab8
A
114#if __i386__
115#define i386_RETURN_OFFSET 4
116
117static unsigned int
118i386_validate_stackptr(vm_offset_t stackptr)
119{
120 /* Existence and alignment check
121 */
122 if (!stackptr || (stackptr & 0x3))
123 return 0;
124
125 /* Is a virtual->physical translation present?
126 */
127 if (!kvtophys(stackptr))
128 return 0;
129
130 /* Check if the return address lies on the same page;
131 * If not, verify that a translation exists.
132 */
133 if (((PAGE_SIZE - (stackptr & PAGE_MASK)) < i386_RETURN_OFFSET) &&
134 !kvtophys(stackptr + i386_RETURN_OFFSET))
135 return 0;
136 return 1;
137}
138
139static unsigned int
140i386_validate_raddr(vm_offset_t raddr)
141{
142 return ((raddr > VM_MIN_KERNEL_ADDRESS) &&
143 (raddr < VM_MAX_KERNEL_ADDRESS));
144}
145#endif
146
91447636
A
147unsigned OSBacktrace(void **bt, unsigned maxAddrs)
148{
149 unsigned frame;
150
151#if __ppc__
152 vm_offset_t stackptr, stackptr_prev;
153 const vm_offset_t * const mem = (vm_offset_t *) 0;
154 unsigned i = 0;
155
156 __asm__ volatile("mflr %0" : "=r" (stackptr));
157 bt[i++] = (void *) stackptr;
158
159 __asm__ volatile("mr %0,r1" : "=r" (stackptr));
160 for ( ; i < maxAddrs; i++) {
161 // Validate we have a reasonable stackptr
162 if ( !(minstackaddr <= stackptr && stackptr < maxstackaddr)
163 || (stackptr & 3))
164 break;
165
166 stackptr_prev = stackptr;
167 stackptr = mem[stackptr_prev >> 2];
168 if ((stackptr_prev ^ stackptr) > 8 * 1024) // Sanity check
169 break;
170
171 vm_offset_t addr = mem[(stackptr >> 2) + 2];
172 if ((addr & 3) || (addr < 0x8000)) // More sanity checks
173 break;
174 bt[i] = (void *) addr;
175 }
176 frame = i;
177
178 for ( ; i < maxAddrs; i++)
179 bt[i] = (void *) 0;
0c530ab8
A
180#elif __i386__
181#define SANE_i386_FRAME_SIZE 8*1024
182 vm_offset_t stackptr, stackptr_prev, raddr;
183 unsigned frame_index = 0;
184/* Obtain current frame pointer */
185 __asm__ volatile("movl %%ebp, %0" : "=m" (stackptr));
4452a7af 186
0c530ab8
A
187 if (!i386_validate_stackptr(stackptr))
188 goto pad;
4452a7af 189
0c530ab8
A
190 raddr = *((vm_offset_t *) (stackptr + i386_RETURN_OFFSET));
191
192 if (!i386_validate_raddr(raddr))
193 goto pad;
194
195 bt[frame_index++] = (void *) raddr;
196
197 for ( ; frame_index < maxAddrs; frame_index++) {
198 stackptr_prev = stackptr;
199 stackptr = *((vm_offset_t *) stackptr_prev);
200
201 if (!i386_validate_stackptr(stackptr))
202 break;
203 /* Stack grows downwards */
204 if (stackptr < stackptr_prev)
205 break;
4452a7af 206
0c530ab8
A
207 if ((stackptr_prev ^ stackptr) > SANE_i386_FRAME_SIZE)
208 break;
209
210 raddr = *((vm_offset_t *) (stackptr + i386_RETURN_OFFSET));
211
212 if (!i386_validate_raddr(raddr))
213 break;
214
215 bt[frame_index] = (void *) raddr;
91447636 216 }
0c530ab8
A
217pad:
218 frame = frame_index;
91447636 219
0c530ab8
A
220 for ( ; frame_index < maxAddrs; frame_index++)
221 bt[frame_index] = (void *) 0;
222#else
223#error arch
91447636 224#endif
91447636
A
225 return frame;
226}