2 * Copyright (c) 2005 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@
29 // NOTE: This file is only c++ so I can get static initialisers going
30 #include <libkern/OSDebug.h>
31 #include <IOKit/IOLib.h>
33 #include <sys/cdefs.h>
36 #include <mach/mach_types.h>
37 #include <mach/kmod.h>
38 #include <kern/locks.h>
40 #include <libkern/libkern.h> // From bsd's libkern directory
41 #include <mach/vm_param.h>
43 #include <sys/kdebug.h>
44 #include <kern/thread.h>
48 // From osmfk/kern/thread.h but considered to be private
49 extern vm_offset_t
min_valid_stack_address(void);
50 extern vm_offset_t
max_valid_stack_address(void);
53 extern void kmod_dump_log(vm_offset_t
*addr
, unsigned int cnt
);
55 extern addr64_t
kvtophys(vm_offset_t va
);
59 extern lck_grp_t
*IOLockGroup
;
61 static lck_mtx_t
*sOSReportLock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
63 /* Use kernel_debug() to log a backtrace */
65 trace_backtrace(uint32_t debugid
, uint32_t debugid2
, uintptr_t size
, uintptr_t data
) {
67 const unsigned cnt
= sizeof(bt
) / sizeof(bt
[0]);
73 /* find first non-kernel frame */
74 for (i
= 3; i
< cnt
&& bt
[i
]; i
++) {
75 if (bt
[i
] > (void*)&etext
) {
81 * if there are non-kernel frames, only log these
82 * otherwise, log everything but the first two
86 #define safe_bt(a) (uintptr_t)(a<cnt ? bt[a] : 0)
87 kernel_debug(debugid
, data
, size
, safe_bt(i
), safe_bt(i
+1), 0);
88 kernel_debug(debugid2
, safe_bt(i
+2), safe_bt(i
+3), safe_bt(i
+4), safe_bt(i
+5), 0);
91 /* Report a message with a 4 entry backtrace - very slow */
93 OSReportWithBacktrace(const char *str
, ...)
97 const unsigned cnt
= sizeof(bt
) / sizeof(bt
[0]);
100 // Ignore the our and our callers stackframes, skipping frames 0 & 1
101 (void) OSBacktrace(bt
, cnt
);
103 va_start(listp
, str
);
104 vsnprintf(buf
, sizeof(buf
), str
, listp
);
107 lck_mtx_lock(sOSReportLock
);
109 printf("%s\nBacktrace %p %p %p %p %p %p %p\n",
110 buf
, bt
[2], bt
[3], bt
[4], bt
[5], bt
[6], bt
[7], bt
[8]);
111 kmod_dump_log((vm_offset_t
*) &bt
[2], cnt
- 2);
113 lck_mtx_unlock(sOSReportLock
);
116 static vm_offset_t minstackaddr
= min_valid_stack_address();
117 static vm_offset_t maxstackaddr
= max_valid_stack_address();
120 #define i386_RETURN_OFFSET 4
123 i386_validate_stackptr(vm_offset_t stackptr
)
125 /* Existence and alignment check
127 if (!stackptr
|| (stackptr
& 0x3))
130 /* Is a virtual->physical translation present?
132 if (!kvtophys(stackptr
))
135 /* Check if the return address lies on the same page;
136 * If not, verify that a translation exists.
138 if (((PAGE_SIZE
- (stackptr
& PAGE_MASK
)) < i386_RETURN_OFFSET
) &&
139 !kvtophys(stackptr
+ i386_RETURN_OFFSET
))
145 i386_validate_raddr(vm_offset_t raddr
)
147 return ((raddr
> VM_MIN_KERNEL_AND_KEXT_ADDRESS
) &&
148 (raddr
< VM_MAX_KERNEL_ADDRESS
));
153 #define x86_64_RETURN_OFFSET 8
155 x86_64_validate_raddr(vm_offset_t raddr
)
157 return ((raddr
> VM_MIN_KERNEL_AND_KEXT_ADDRESS
) &&
158 (raddr
< VM_MAX_KERNEL_ADDRESS
));
161 x86_64_validate_stackptr(vm_offset_t stackptr
)
163 /* Existence and alignment check
165 if (!stackptr
|| (stackptr
& 0x7) || !x86_64_validate_raddr(stackptr
))
168 /* Is a virtual->physical translation present?
170 if (!kvtophys(stackptr
))
173 /* Check if the return address lies on the same page;
174 * If not, verify that a translation exists.
176 if (((PAGE_SIZE
- (stackptr
& PAGE_MASK
)) < x86_64_RETURN_OFFSET
) &&
177 !kvtophys(stackptr
+ x86_64_RETURN_OFFSET
))
184 OSPrintBacktrace(void)
187 int tmp
= OSBacktrace(btbuf
, 20);
191 kprintf("bt[%.2d] = %p\n", i
, btbuf
[i
]);
195 unsigned OSBacktrace(void **bt
, unsigned maxAddrs
)
200 #define SANE_i386_FRAME_SIZE (kernel_stack_size >> 1)
201 vm_offset_t stackptr
, stackptr_prev
, raddr
;
202 unsigned frame_index
= 0;
203 /* Obtain current frame pointer */
204 __asm__
volatile("movl %%ebp, %0" : "=m" (stackptr
));
206 if (!i386_validate_stackptr(stackptr
))
209 raddr
= *((vm_offset_t
*) (stackptr
+ i386_RETURN_OFFSET
));
211 if (!i386_validate_raddr(raddr
))
214 bt
[frame_index
++] = (void *) raddr
;
216 for ( ; frame_index
< maxAddrs
; frame_index
++) {
217 stackptr_prev
= stackptr
;
218 stackptr
= *((vm_offset_t
*) stackptr_prev
);
220 if (!i386_validate_stackptr(stackptr
))
222 /* Stack grows downwards */
223 if (stackptr
< stackptr_prev
)
226 if ((stackptr
- stackptr_prev
) > SANE_i386_FRAME_SIZE
)
229 raddr
= *((vm_offset_t
*) (stackptr
+ i386_RETURN_OFFSET
));
231 if (!i386_validate_raddr(raddr
))
234 bt
[frame_index
] = (void *) raddr
;
239 for ( ; frame_index
< maxAddrs
; frame_index
++)
240 bt
[frame_index
] = (void *) 0;
242 #define SANE_x86_64_FRAME_SIZE (kernel_stack_size >> 1)
243 vm_offset_t stackptr
, stackptr_prev
, raddr
;
244 unsigned frame_index
= 0;
245 /* Obtain current frame pointer */
247 __asm__
volatile("movq %%rbp, %0" : "=m" (stackptr
));
249 if (!x86_64_validate_stackptr(stackptr
))
252 raddr
= *((vm_offset_t
*) (stackptr
+ x86_64_RETURN_OFFSET
));
254 if (!x86_64_validate_raddr(raddr
))
257 bt
[frame_index
++] = (void *) raddr
;
259 for ( ; frame_index
< maxAddrs
; frame_index
++) {
260 stackptr_prev
= stackptr
;
261 stackptr
= *((vm_offset_t
*) stackptr_prev
);
263 if (!x86_64_validate_stackptr(stackptr
))
265 /* Stack grows downwards */
266 if (stackptr
< stackptr_prev
)
269 if ((stackptr
- stackptr_prev
) > SANE_x86_64_FRAME_SIZE
)
272 raddr
= *((vm_offset_t
*) (stackptr
+ x86_64_RETURN_OFFSET
));
274 if (!x86_64_validate_raddr(raddr
))
277 bt
[frame_index
] = (void *) raddr
;
282 for ( ; frame_index
< maxAddrs
; frame_index
++)
283 bt
[frame_index
] = (void *) 0;