]>
git.saurik.com Git - apple/xnu.git/blob - libkern/gen/OSDebug.cpp
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 // NOTE: This file is only c++ so I can get static initialisers going
24 #include <libkern/OSDebug.h>
26 #include <sys/cdefs.h>
29 #include <mach/mach_types.h>
30 #include <mach/kmod.h>
31 #include <kern/lock.h>
33 #include <libkern/libkern.h> // From bsd's libkern directory
34 #include <mach/vm_param.h>
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
);
44 extern addr64_t
kvtophys(vm_offset_t va
);
47 static mutex_t
*sOSReportLock
= mutex_alloc(0);
49 /* Report a message with a 4 entry backtrace - very slow */
51 OSReportWithBacktrace(const char *str
, ...)
55 const unsigned cnt
= sizeof(bt
) / sizeof(bt
[0]);
58 // Ignore the our and our callers stackframes, skipping frames 0 & 1
59 (void) OSBacktrace(bt
, cnt
);
62 vsnprintf(buf
, sizeof(buf
), str
, listp
);
65 mutex_lock(sOSReportLock
);
67 printf("%s\nBacktrace %p %p %p %p %p %p %p\n",
68 buf
, bt
[2], bt
[3], bt
[4], bt
[5], bt
[6], bt
[7], bt
[8]);
69 kmod_dump_log((vm_offset_t
*) &bt
[2], cnt
- 2);
71 mutex_unlock(sOSReportLock
);
74 static vm_offset_t minstackaddr
= min_valid_stack_address();
75 static vm_offset_t maxstackaddr
= max_valid_stack_address();
78 #define i386_RETURN_OFFSET 4
81 i386_validate_stackptr(vm_offset_t stackptr
)
83 /* Existence and alignment check
85 if (!stackptr
|| (stackptr
& 0x3))
88 /* Is a virtual->physical translation present?
90 if (!kvtophys(stackptr
))
93 /* Check if the return address lies on the same page;
94 * If not, verify that a translation exists.
96 if (((PAGE_SIZE
- (stackptr
& PAGE_MASK
)) < i386_RETURN_OFFSET
) &&
97 !kvtophys(stackptr
+ i386_RETURN_OFFSET
))
103 i386_validate_raddr(vm_offset_t raddr
)
105 return ((raddr
> VM_MIN_KERNEL_ADDRESS
) &&
106 (raddr
< VM_MAX_KERNEL_ADDRESS
));
110 unsigned OSBacktrace(void **bt
, unsigned maxAddrs
)
115 vm_offset_t stackptr
, stackptr_prev
;
116 const vm_offset_t
* const mem
= (vm_offset_t
*) 0;
119 __asm__
volatile("mflr %0" : "=r" (stackptr
));
120 bt
[i
++] = (void *) stackptr
;
122 __asm__
volatile("mr %0,r1" : "=r" (stackptr
));
123 for ( ; i
< maxAddrs
; i
++) {
124 // Validate we have a reasonable stackptr
125 if ( !(minstackaddr
<= stackptr
&& stackptr
< maxstackaddr
)
129 stackptr_prev
= stackptr
;
130 stackptr
= mem
[stackptr_prev
>> 2];
131 if ((stackptr_prev
^ stackptr
) > 8 * 1024) // Sanity check
134 vm_offset_t addr
= mem
[(stackptr
>> 2) + 2];
135 if ((addr
& 3) || (addr
< 0x8000)) // More sanity checks
137 bt
[i
] = (void *) addr
;
141 for ( ; i
< maxAddrs
; i
++)
144 #define SANE_i386_FRAME_SIZE 8*1024
145 vm_offset_t stackptr
, stackptr_prev
, raddr
;
146 unsigned frame_index
= 0;
147 /* Obtain current frame pointer */
148 __asm__
volatile("movl %%ebp, %0" : "=m" (stackptr
));
150 if (!i386_validate_stackptr(stackptr
))
153 raddr
= *((vm_offset_t
*) (stackptr
+ i386_RETURN_OFFSET
));
155 if (!i386_validate_raddr(raddr
))
158 bt
[frame_index
++] = (void *) raddr
;
160 for ( ; frame_index
< maxAddrs
; frame_index
++) {
161 stackptr_prev
= stackptr
;
162 stackptr
= *((vm_offset_t
*) stackptr_prev
);
164 if (!i386_validate_stackptr(stackptr
))
166 /* Stack grows downwards */
167 if (stackptr
< stackptr_prev
)
170 if ((stackptr_prev
^ stackptr
) > SANE_i386_FRAME_SIZE
)
173 raddr
= *((vm_offset_t
*) (stackptr
+ i386_RETURN_OFFSET
));
175 if (!i386_validate_raddr(raddr
))
178 bt
[frame_index
] = (void *) raddr
;
183 for ( ; frame_index
< maxAddrs
; frame_index
++)
184 bt
[frame_index
] = (void *) 0;