OLD | NEW |
1 // Copyright (c) 2007, Google Inc. | 1 // Copyright (c) 2011, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
11 // copyright notice, this list of conditions and the following disclaimer | 11 // copyright notice, this list of conditions and the following disclaimer |
12 // in the documentation and/or other materials provided with the | 12 // in the documentation and/or other materials provided with the |
13 // distribution. | 13 // distribution. |
14 // * Neither the name of Google Inc. nor the names of its | 14 // * Neither the name of Google Inc. nor the names of its |
15 // contributors may be used to endorse or promote products derived from | 15 // contributors may be used to endorse or promote products derived from |
16 // this software without specific prior written permission. | 16 // this software without specific prior written permission. |
17 // | 17 // |
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | 29 |
30 // --- | 30 // --- |
31 // Author: Craig Silverstein | 31 // Author: Doug Kwan |
| 32 // This is inspired by Craig Silverstein's PowerPC stacktrace code. |
32 // | 33 // |
33 // Produce stack trace. I'm guessing (hoping!) the code is much like | |
34 // for x86. For apple machines, at least, it seems to be; see | |
35 // http://developer.apple.com/documentation/mac/runtimehtml/RTArch-59.html | |
36 // http://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK | |
37 // Linux has similar code: http://patchwork.ozlabs.org/linuxppc/patch?id=8882 | |
38 | 34 |
39 #ifndef BASE_STACKTRACE_POWERPC_INL_H_ | 35 #ifndef BASE_STACKTRACE_ARM_INL_H_ |
40 #define BASE_STACKTRACE_POWERPC_INL_H_ | 36 #define BASE_STACKTRACE_ARM_INL_H_ |
41 // Note: this file is included into stacktrace.cc more than once. | 37 // Note: this file is included into stacktrace.cc more than once. |
42 // Anything that should only be defined once should be here: | 38 // Anything that should only be defined once should be here: |
43 | 39 |
44 #include <stdint.h> // for uintptr_t | 40 #include <stdint.h> // for uintptr_t |
45 #include <stdlib.h> // for NULL | 41 #include "base/basictypes.h" // for NULL |
46 #include <google/stacktrace.h> | 42 #include <gperftools/stacktrace.h> |
| 43 |
| 44 // WARNING: |
| 45 // This only works if all your code is in either ARM or THUMB mode. With |
| 46 // interworking, the frame pointer of the caller can either be in r11 (ARM |
| 47 // mode) or r7 (THUMB mode). A callee only saves the frame pointer of its |
| 48 // mode in a fixed location on its stack frame. If the caller is a different |
| 49 // mode, there is no easy way to find the frame pointer. It can either be |
| 50 // still in the designated register or saved on stack along with other callee |
| 51 // saved registers. |
47 | 52 |
48 // Given a pointer to a stack frame, locate and return the calling | 53 // Given a pointer to a stack frame, locate and return the calling |
49 // stackframe, or return NULL if no stackframe can be found. Perform sanity | 54 // stackframe, or return NULL if no stackframe can be found. Perform sanity |
50 // checks (the strictness of which is controlled by the boolean parameter | 55 // checks (the strictness of which is controlled by the boolean parameter |
51 // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned. | 56 // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned. |
52 template<bool STRICT_UNWINDING> | 57 template<bool STRICT_UNWINDING> |
53 static void **NextStackFrame(void **old_sp) { | 58 static void **NextStackFrame(void **old_sp) { |
54 void **new_sp = (void **) *old_sp; | 59 void **new_sp = (void**) old_sp[-1]; |
55 | 60 |
56 // Check that the transition from frame pointer old_sp to frame | 61 // Check that the transition from frame pointer old_sp to frame |
57 // pointer new_sp isn't clearly bogus | 62 // pointer new_sp isn't clearly bogus |
58 if (STRICT_UNWINDING) { | 63 if (STRICT_UNWINDING) { |
59 // With the stack growing downwards, older stack frame must be | 64 // With the stack growing downwards, older stack frame must be |
60 // at a greater address that the current one. | 65 // at a greater address that the current one. |
61 if (new_sp <= old_sp) return NULL; | 66 if (new_sp <= old_sp) return NULL; |
62 // Assume stack frames larger than 100,000 bytes are bogus. | 67 // Assume stack frames larger than 100,000 bytes are bogus. |
63 if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL; | 68 if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL; |
64 } else { | 69 } else { |
65 // In the non-strict mode, allow discontiguous stack frames. | 70 // In the non-strict mode, allow discontiguous stack frames. |
66 // (alternate-signal-stacks for example). | 71 // (alternate-signal-stacks for example). |
67 if (new_sp == old_sp) return NULL; | 72 if (new_sp == old_sp) return NULL; |
68 // And allow frames upto about 1MB. | 73 // And allow frames upto about 1MB. |
69 if ((new_sp > old_sp) | 74 if ((new_sp > old_sp) |
70 && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL; | 75 && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL; |
71 } | 76 } |
72 if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL; | 77 if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL; |
73 return new_sp; | 78 return new_sp; |
74 } | 79 } |
75 | 80 |
76 // This ensures that GetStackTrace stes up the Link Register properly. | 81 // This ensures that GetStackTrace stes up the Link Register properly. |
77 void StacktracePowerPCDummyFunction() __attribute__((noinline)); | 82 #ifdef __GNUC__ |
78 void StacktracePowerPCDummyFunction() { __asm__ volatile(""); } | 83 void StacktraceArmDummyFunction() __attribute__((noinline)); |
79 #endif // BASE_STACKTRACE_POWERPC_INL_H_ | 84 void StacktraceArmDummyFunction() { __asm__ volatile(""); } |
| 85 #else |
| 86 # error StacktraceArmDummyFunction() needs to be ported to this platform. |
| 87 #endif |
| 88 #endif // BASE_STACKTRACE_ARM_INL_H_ |
80 | 89 |
81 // Note: this part of the file is included several times. | 90 // Note: this part of the file is included several times. |
82 // Do not put globals below. | 91 // Do not put globals below. |
83 | 92 |
84 // The following 4 functions are generated from the code below: | 93 // The following 4 functions are generated from the code below: |
85 // GetStack{Trace,Frames}() | 94 // GetStack{Trace,Frames}() |
86 // GetStack{Trace,Frames}WithContext() | 95 // GetStack{Trace,Frames}WithContext() |
87 // | 96 // |
88 // These functions take the following args: | 97 // These functions take the following args: |
89 // void** result: the stack-trace, as an array | 98 // void** result: the stack-trace, as an array |
90 // int* sizes: the size of each stack frame, as an array | 99 // int* sizes: the size of each stack frame, as an array |
91 // (GetStackFrames* only) | 100 // (GetStackFrames* only) |
92 // int max_depth: the size of the result (and sizes) array(s) | 101 // int max_depth: the size of the result (and sizes) array(s) |
93 // int skip_count: how many stack pointers to skip before storing in result | 102 // int skip_count: how many stack pointers to skip before storing in result |
94 // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only) | 103 // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only) |
95 int GET_STACK_TRACE_OR_FRAMES { | 104 int GET_STACK_TRACE_OR_FRAMES { |
96 void **sp; | 105 #ifdef __GNUC__ |
97 // Apple OS X uses an old version of gnu as -- both Darwin 7.9.0 (Panther) | 106 void **sp = reinterpret_cast<void**>(__builtin_frame_address(0)); |
98 // and Darwin 8.8.1 (Tiger) use as 1.38. This means we have to use a | |
99 // different asm syntax. I don't know quite the best way to discriminate | |
100 // systems using the old as from the new one; I've gone with __APPLE__. | |
101 // TODO(csilvers): use autoconf instead, to look for 'as --version' == 1 or 2 | |
102 #ifdef __APPLE__ | |
103 __asm__ volatile ("mr %0,r1" : "=r" (sp)); | |
104 #else | 107 #else |
105 __asm__ volatile ("mr %0,1" : "=r" (sp)); | 108 # error reading stack point not yet supported on this platform. |
106 #endif | 109 #endif |
107 | 110 |
108 // On PowerPC, the "Link Register" or "Link Record" (LR), is a stack | 111 // On ARM, the return address is stored in the link register (r14). |
109 // entry that holds the return address of the subroutine call (what | 112 // This is not saved on the stack frame of a leaf function. To |
110 // instruction we run after our function finishes). This is the | 113 // simplify code that reads return addresses, we call a dummy |
111 // same as the stack-pointer of our parent routine, which is what we | 114 // function so that the return address of this function is also |
112 // want here. While the compiler will always(?) set up LR for | 115 // stored in the stack frame. This works at least for gcc. |
113 // subroutine calls, it may not for leaf functions (such as this one). | 116 StacktraceArmDummyFunction(); |
114 // This routine forces the compiler (at least gcc) to push it anyway. | |
115 StacktracePowerPCDummyFunction(); | |
116 | |
117 #if IS_STACK_FRAMES | |
118 // Note we do *not* increment skip_count here for the SYSV ABI. If | |
119 // we did, the list of stack frames wouldn't properly match up with | |
120 // the list of return addresses. Note this means the top pc entry | |
121 // is probably bogus for linux/ppc (and other SYSV-ABI systems). | |
122 #else | |
123 // The LR save area is used by the callee, so the top entry is bogus. | |
124 skip_count++; | |
125 #endif | |
126 | 117 |
127 int n = 0; | 118 int n = 0; |
128 while (sp && n < max_depth) { | 119 while (sp && n < max_depth) { |
129 #if IS_STACK_FRAMES | |
130 // The GetStackFrames routine is called when we are in some | 120 // The GetStackFrames routine is called when we are in some |
131 // informational context (the failure signal handler for example). | 121 // informational context (the failure signal handler for example). |
132 // Use the non-strict unwinding rules to produce a stack trace | 122 // Use the non-strict unwinding rules to produce a stack trace |
133 // that is as complete as possible (even if it contains a few bogus | 123 // that is as complete as possible (even if it contains a few bogus |
134 // entries in some rare cases). | 124 // entries in some rare cases). |
135 void **next_sp = NextStackFrame<false>(sp); | 125 void **next_sp = NextStackFrame<IS_STACK_FRAMES == 0>(sp); |
136 #else | |
137 void **next_sp = NextStackFrame<true>(sp); | |
138 #endif | |
139 | 126 |
140 if (skip_count > 0) { | 127 if (skip_count > 0) { |
141 skip_count--; | 128 skip_count--; |
142 } else { | 129 } else { |
143 // PowerPC has 3 main ABIs, which say where in the stack the | 130 result[n] = *sp; |
144 // Link Register is. For DARWIN and AIX (used by apple and | |
145 // linux ppc64), it's in sp[2]. For SYSV (used by linux ppc), | |
146 // it's in sp[1]. | |
147 #if defined(_CALL_AIX) || defined(_CALL_DARWIN) | |
148 result[n++] = *(sp+2); | |
149 #elif defined(_CALL_SYSV) | |
150 result[n++] = *(sp+1); | |
151 #elif defined(__APPLE__) || (defined(__linux) && defined(__PPC64__)) | |
152 // This check is in case the compiler doesn't define _CALL_AIX/etc. | |
153 result[n++] = *(sp+2); | |
154 #elif defined(__linux) | |
155 // This check is in case the compiler doesn't define _CALL_SYSV. | |
156 result[n++] = *(sp+1); | |
157 #else | |
158 #error Need to specify the PPC ABI for your archiecture. | |
159 #endif | |
160 | 131 |
161 #if IS_STACK_FRAME | 132 #if IS_STACK_FRAMES |
162 if (next_sp > sp) { | 133 if (next_sp > sp) { |
163 sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp; | 134 sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp; |
164 } else { | 135 } else { |
165 // A frame-size of 0 is used to indicate unknown frame size. | 136 // A frame-size of 0 is used to indicate unknown frame size. |
166 sizes[n] = 0; | 137 sizes[n] = 0; |
167 } | 138 } |
168 #endif | 139 #endif |
| 140 n++; |
169 } | 141 } |
170 sp = next_sp; | 142 sp = next_sp; |
171 } | 143 } |
172 return n; | 144 return n; |
173 } | 145 } |
OLD | NEW |