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