]>
git.saurik.com Git - apple/xnu.git/blob - libkern/gen/OSDebug.cpp
7afc03ad490860a34b37bbf561b4310b848b93a2
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 // NOTE: This file is only c++ so I can get static initialisers going
25 #include <libkern/OSDebug.h>
27 #include <sys/cdefs.h>
30 #include <mach/mach_types.h>
31 #include <mach/kmod.h>
32 #include <kern/lock.h>
34 #include <libkern/libkern.h> // From bsd's libkern directory
37 // From osmfk/kern/thread.h but considered to be private
38 extern vm_offset_t
min_valid_stack_address(void);
39 extern vm_offset_t
max_valid_stack_address(void);
42 extern void kmod_dump_log(vm_offset_t
*addr
, unsigned int cnt
);
45 static mutex_t
*sOSReportLock
= mutex_alloc(0);
47 /* Report a message with a 4 entry backtrace - very slow */
49 OSReportWithBacktrace(const char *str
, ...)
53 const unsigned cnt
= sizeof(bt
) / sizeof(bt
[0]);
56 // Ignore the our and our callers stackframes, skipping frames 0 & 1
57 (void) OSBacktrace(bt
, cnt
);
60 vsnprintf(buf
, sizeof(buf
), str
, listp
);
63 mutex_lock(sOSReportLock
);
65 printf("%s\nBacktrace %p %p %p %p %p %p %p\n",
66 buf
, bt
[2], bt
[3], bt
[4], bt
[5], bt
[6], bt
[7], bt
[8]);
67 kmod_dump_log((vm_offset_t
*) &bt
[2], cnt
- 2);
69 mutex_unlock(sOSReportLock
);
72 static vm_offset_t minstackaddr
= min_valid_stack_address();
73 static vm_offset_t maxstackaddr
= max_valid_stack_address();
75 unsigned OSBacktrace(void **bt
, unsigned maxAddrs
)
80 vm_offset_t stackptr
, stackptr_prev
;
81 const vm_offset_t
* const mem
= (vm_offset_t
*) 0;
84 __asm__
volatile("mflr %0" : "=r" (stackptr
));
85 bt
[i
++] = (void *) stackptr
;
87 __asm__
volatile("mr %0,r1" : "=r" (stackptr
));
88 for ( ; i
< maxAddrs
; i
++) {
89 // Validate we have a reasonable stackptr
90 if ( !(minstackaddr
<= stackptr
&& stackptr
< maxstackaddr
)
94 stackptr_prev
= stackptr
;
95 stackptr
= mem
[stackptr_prev
>> 2];
96 if ((stackptr_prev
^ stackptr
) > 8 * 1024) // Sanity check
99 vm_offset_t addr
= mem
[(stackptr
>> 2) + 2];
100 if ((addr
& 3) || (addr
< 0x8000)) // More sanity checks
102 bt
[i
] = (void *) addr
;
106 for ( ; i
< maxAddrs
; i
++)
108 #elif 0 && __i386__ // Note that this should be ported for i386
109 // This function is not safe, we should get this code ported appropriately
111 for (frame
= 16; frame
< maxAddrs
; frame
++)
112 bt
[frame
] = __builtin_return_address(frame
);
117 case 15+1: bt
[15] = __builtin_return_address(15);
118 case 14+1: bt
[14] = __builtin_return_address(14);
119 case 13+1: bt
[13] = __builtin_return_address(13);
120 case 12+1: bt
[12] = __builtin_return_address(12);
121 case 11+1: bt
[11] = __builtin_return_address(11);
122 case 10+1: bt
[10] = __builtin_return_address(10);
123 case 9+1: bt
[ 9] = __builtin_return_address( 9);
124 case 8+1: bt
[ 8] = __builtin_return_address( 8);
125 case 7+1: bt
[ 7] = __builtin_return_address( 7);
126 case 6+1: bt
[ 6] = __builtin_return_address( 6);
127 case 5+1: bt
[ 5] = __builtin_return_address( 5);
128 case 4+1: bt
[ 4] = __builtin_return_address( 4);
129 case 3+1: bt
[ 3] = __builtin_return_address( 3);
130 case 2+1: bt
[ 2] = __builtin_return_address( 2);
131 case 1+1: bt
[ 1] = __builtin_return_address( 1);
132 case 0+1: bt
[ 0] = __builtin_return_address( 0);
133 case 0: default: break;
138 // This function is not safe, we should get this code ported appropriately
140 for (frame
= 16; frame
< maxAddrs
; frame
++)
146 case 15+1: bt
[15] = __builtin_return_address(15);
147 case 14+1: bt
[14] = __builtin_return_address(14);
148 case 13+1: bt
[13] = __builtin_return_address(13);
149 case 12+1: bt
[12] = __builtin_return_address(12);
150 case 11+1: bt
[11] = __builtin_return_address(11);
151 case 10+1: bt
[10] = __builtin_return_address(10);
152 case 9+1: bt
[ 9] = __builtin_return_address( 9);
153 case 8+1: bt
[ 8] = __builtin_return_address( 8);
154 case 7+1: bt
[ 7] = __builtin_return_address( 7);
155 case 6+1: bt
[ 6] = __builtin_return_address( 6);
156 case 5+1: bt
[ 5] = __builtin_return_address( 5);
157 case 4+1: bt
[ 4] = __builtin_return_address( 4);
158 case 3+1: bt
[ 3] = __builtin_return_address( 3);
159 case 2+1: bt
[ 2] = __builtin_return_address( 2);
160 case 1+1: bt
[ 1] = __builtin_return_address( 1);
161 case 0+1: bt
[ 0] = __builtin_return_address( 0);