2 * Copyright (c) 2005-2012 Apple 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 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", buf
,
110 (unsigned long) VM_KERNEL_UNSLIDE(bt
[2]), (unsigned long) VM_KERNEL_UNSLIDE(bt
[3]),
111 (unsigned long) VM_KERNEL_UNSLIDE(bt
[4]), (unsigned long) VM_KERNEL_UNSLIDE(bt
[5]),
112 (unsigned long) VM_KERNEL_UNSLIDE(bt
[6]), (unsigned long) VM_KERNEL_UNSLIDE(bt
[7]),
113 (unsigned long) VM_KERNEL_UNSLIDE(bt
[8]));
114 kmod_dump_log((vm_offset_t
*) &bt
[2], cnt
- 2);
116 lck_mtx_unlock(sOSReportLock
);
119 static vm_offset_t minstackaddr
= min_valid_stack_address();
120 static vm_offset_t maxstackaddr
= max_valid_stack_address();
124 #define x86_64_RETURN_OFFSET 8
126 x86_64_validate_raddr(vm_offset_t raddr
)
128 return ((raddr
> VM_MIN_KERNEL_AND_KEXT_ADDRESS
) &&
129 (raddr
< VM_MAX_KERNEL_ADDRESS
));
132 x86_64_validate_stackptr(vm_offset_t stackptr
)
134 /* Existence and alignment check
136 if (!stackptr
|| (stackptr
& 0x7) || !x86_64_validate_raddr(stackptr
))
139 /* Is a virtual->physical translation present?
141 if (!kvtophys(stackptr
))
144 /* Check if the return address lies on the same page;
145 * If not, verify that a translation exists.
147 if (((PAGE_SIZE
- (stackptr
& PAGE_MASK
)) < x86_64_RETURN_OFFSET
) &&
148 !kvtophys(stackptr
+ x86_64_RETURN_OFFSET
))
155 OSPrintBacktrace(void)
158 int tmp
= OSBacktrace(btbuf
, 20);
162 kprintf("bt[%.2d] = %p\n", i
, btbuf
[i
]);
166 unsigned OSBacktrace(void **bt
, unsigned maxAddrs
)
171 #define SANE_x86_64_FRAME_SIZE (kernel_stack_size >> 1)
172 vm_offset_t stackptr
, stackptr_prev
, raddr
;
173 unsigned frame_index
= 0;
174 /* Obtain current frame pointer */
176 __asm__
volatile("movq %%rbp, %0" : "=m" (stackptr
));
178 if (!x86_64_validate_stackptr(stackptr
))
181 raddr
= *((vm_offset_t
*) (stackptr
+ x86_64_RETURN_OFFSET
));
183 if (!x86_64_validate_raddr(raddr
))
186 bt
[frame_index
++] = (void *) raddr
;
188 for ( ; frame_index
< maxAddrs
; frame_index
++) {
189 stackptr_prev
= stackptr
;
190 stackptr
= *((vm_offset_t
*) stackptr_prev
);
192 if (!x86_64_validate_stackptr(stackptr
))
194 /* Stack grows downwards */
195 if (stackptr
< stackptr_prev
)
198 if ((stackptr
- stackptr_prev
) > SANE_x86_64_FRAME_SIZE
)
201 raddr
= *((vm_offset_t
*) (stackptr
+ x86_64_RETURN_OFFSET
));
203 if (!x86_64_validate_raddr(raddr
))
206 bt
[frame_index
] = (void *) raddr
;
211 for ( ; frame_index
< maxAddrs
; frame_index
++)
212 bt
[frame_index
] = (void *) 0;