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>
49 // From osmfk/kern/thread.h but considered to be private
50 extern vm_offset_t
min_valid_stack_address(void);
51 extern vm_offset_t
max_valid_stack_address(void);
53 // From osfmk/kern/printf.c
54 extern boolean_t doprnt_hide_pointers
;
57 extern void kmod_dump_log(vm_offset_t
*addr
, unsigned int cnt
, boolean_t doUnslide
);
59 extern addr64_t
kvtophys(vm_offset_t va
);
61 extern int copyinframe(vm_address_t fp
, char *frame
);
62 #elif defined(__arm64__)
63 extern int copyinframe(vm_address_t fp
, char *frame
, boolean_t is64bit
);
68 extern lck_grp_t
*IOLockGroup
;
70 static lck_mtx_t
*sOSReportLock
= lck_mtx_alloc_init(IOLockGroup
, LCK_ATTR_NULL
);
72 /* Use kernel_debug() to log a backtrace */
74 trace_backtrace(uint32_t debugid
, uint32_t debugid2
, uintptr_t size
, uintptr_t data
) {
76 const unsigned cnt
= sizeof(bt
) / sizeof(bt
[0]);
82 /* find first non-kernel frame */
83 for (i
= 3; i
< cnt
&& bt
[i
]; i
++) {
84 if (bt
[i
] > (void*)&etext
) {
90 * if there are non-kernel frames, only log these
91 * otherwise, log everything but the first two
95 #define safe_bt(a) (uintptr_t)(a<cnt ? bt[a] : 0)
96 kernel_debug(debugid
, data
, size
, safe_bt(i
), safe_bt(i
+1), 0);
97 kernel_debug(debugid2
, safe_bt(i
+2), safe_bt(i
+3), safe_bt(i
+4), safe_bt(i
+5), 0);
100 /* Report a message with a 4 entry backtrace - very slow */
102 OSReportWithBacktrace(const char *str
, ...)
106 const unsigned cnt
= sizeof(bt
) / sizeof(bt
[0]);
109 // Ignore the our and our callers stackframes, skipping frames 0 & 1
110 (void) OSBacktrace(bt
, cnt
);
112 va_start(listp
, str
);
113 vsnprintf(buf
, sizeof(buf
), str
, listp
);
116 lck_mtx_lock(sOSReportLock
);
118 boolean_t old_doprnt_hide_pointers
= doprnt_hide_pointers
;
119 doprnt_hide_pointers
= FALSE
;
120 printf("%s\nBacktrace 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n", buf
,
121 (unsigned long) VM_KERNEL_UNSLIDE(bt
[2]), (unsigned long) VM_KERNEL_UNSLIDE(bt
[3]),
122 (unsigned long) VM_KERNEL_UNSLIDE(bt
[4]), (unsigned long) VM_KERNEL_UNSLIDE(bt
[5]),
123 (unsigned long) VM_KERNEL_UNSLIDE(bt
[6]), (unsigned long) VM_KERNEL_UNSLIDE(bt
[7]),
124 (unsigned long) VM_KERNEL_UNSLIDE(bt
[8]));
125 kmod_dump_log((vm_offset_t
*) &bt
[2], cnt
- 2, TRUE
);
126 doprnt_hide_pointers
= old_doprnt_hide_pointers
;
128 lck_mtx_unlock(sOSReportLock
);
131 static vm_offset_t minstackaddr
= min_valid_stack_address();
132 static vm_offset_t maxstackaddr
= max_valid_stack_address();
136 #define x86_64_RETURN_OFFSET 8
138 x86_64_validate_raddr(vm_offset_t raddr
)
140 return ((raddr
> VM_MIN_KERNEL_AND_KEXT_ADDRESS
) &&
141 (raddr
< VM_MAX_KERNEL_ADDRESS
));
144 x86_64_validate_stackptr(vm_offset_t stackptr
)
146 /* Existence and alignment check
148 if (!stackptr
|| (stackptr
& 0x7) || !x86_64_validate_raddr(stackptr
))
151 /* Is a virtual->physical translation present?
153 if (!kvtophys(stackptr
))
156 /* Check if the return address lies on the same page;
157 * If not, verify that a translation exists.
159 if (((PAGE_SIZE
- (stackptr
& PAGE_MASK
)) < x86_64_RETURN_OFFSET
) &&
160 !kvtophys(stackptr
+ x86_64_RETURN_OFFSET
))
167 OSPrintBacktrace(void)
170 int tmp
= OSBacktrace(btbuf
, 20);
174 kprintf("bt[%.2d] = %p\n", i
, btbuf
[i
]);
178 unsigned OSBacktrace(void **bt
, unsigned maxAddrs
)
181 if (!current_thread()) return 0;
184 #define SANE_x86_64_FRAME_SIZE (kernel_stack_size >> 1)
185 vm_offset_t stackptr
, stackptr_prev
, raddr
;
186 unsigned frame_index
= 0;
187 /* Obtain current frame pointer */
189 __asm__
volatile("movq %%rbp, %0" : "=m" (stackptr
));
191 if (!x86_64_validate_stackptr(stackptr
))
194 raddr
= *((vm_offset_t
*) (stackptr
+ x86_64_RETURN_OFFSET
));
196 if (!x86_64_validate_raddr(raddr
))
199 bt
[frame_index
++] = (void *) raddr
;
201 for ( ; frame_index
< maxAddrs
; frame_index
++) {
202 stackptr_prev
= stackptr
;
203 stackptr
= *((vm_offset_t
*) stackptr_prev
);
205 if (!x86_64_validate_stackptr(stackptr
))
207 /* Stack grows downwards */
208 if (stackptr
< stackptr_prev
)
211 if ((stackptr
- stackptr_prev
) > SANE_x86_64_FRAME_SIZE
)
214 raddr
= *((vm_offset_t
*) (stackptr
+ x86_64_RETURN_OFFSET
));
216 if (!x86_64_validate_raddr(raddr
))
219 bt
[frame_index
] = (void *) raddr
;
224 for ( ; frame_index
< maxAddrs
; frame_index
++)
225 bt
[frame_index
] = (void *) 0;
226 #elif __arm__ || __arm64__
231 // get the current frame pointer for this thread
233 #define OSBacktraceFrameAlignOK(x) (((x) & 0x3) == 0)
234 __asm__
volatile("mov %0,r7" : "=r" (fp
));
235 #elif defined(__arm64__)
236 #define OSBacktraceFrameAlignOK(x) (((x) & 0xf) == 0)
237 __asm__
volatile("mov %0, fp" : "=r" (fp
));
239 #error Unknown architecture.
242 // now crawl up the stack recording the link value of each frame
245 if ((fp
== 0) || (!OSBacktraceFrameAlignOK(fp
)) || (fp
> VM_MAX_KERNEL_ADDRESS
) || (fp
< VM_MIN_KERNEL_AND_KEXT_ADDRESS
)) {
250 if (copyinframe(fp
, (char*)frameb
, TRUE
) != 0) {
252 if (copyinframe(fp
, (char*)frameb
) != 0) {
257 // No need to use copyin as this is always a kernel address, see check above
258 bt
[i
] = (void*)frameb
[1]; // link register
260 } while (++i
< maxAddrs
);