1 // Copyright (c) 2007, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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. 29 30 // --- 31 // Author: Craig Silverstein 32 // 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 39 #ifndef BASE_STACKTRACE_POWERPC_INL_H_ 40 #define BASE_STACKTRACE_POWERPC_INL_H_ 41 // Note: this file is included into stacktrace.cc more than once. 42 // Anything that should only be defined once should be here: 43 44 #include <stdint.h> // for uintptr_t 45 #include <stdlib.h> // for NULL 46 #include <gperftools/stacktrace.h> 47 48 // 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 50 // checks (the strictness of which is controlled by the boolean parameter 51 // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned. 52 template<bool STRICT_UNWINDING> 53 static void **NextStackFrame(void **old_sp) { 54 void **new_sp = (void **) *old_sp; 55 56 // Check that the transition from frame pointer old_sp to frame 57 // pointer new_sp isn't clearly bogus 58 if (STRICT_UNWINDING) { 59 // With the stack growing downwards, older stack frame must be 60 // at a greater address that the current one. 61 if (new_sp <= old_sp) return NULL; 62 // Assume stack frames larger than 100,000 bytes are bogus. 63 if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL; 64 } else { 65 // In the non-strict mode, allow discontiguous stack frames. 66 // (alternate-signal-stacks for example). 67 if (new_sp == old_sp) return NULL; 68 // And allow frames upto about 1MB. 69 if ((new_sp > old_sp) 70 && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL; 71 } 72 if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL; 73 return new_sp; 74 } 75 76 // This ensures that GetStackTrace stes up the Link Register properly. 77 void StacktracePowerPCDummyFunction() __attribute__((noinline)); 78 void StacktracePowerPCDummyFunction() { __asm__ volatile(""); } 79 #endif // BASE_STACKTRACE_POWERPC_INL_H_ 80 81 // Note: this part of the file is included several times. 82 // Do not put globals below. 83 84 // The following 4 functions are generated from the code below: 85 // GetStack{Trace,Frames}() 86 // GetStack{Trace,Frames}WithContext() 87 // 88 // These functions take the following args: 89 // void** result: the stack-trace, as an array 90 // int* sizes: the size of each stack frame, as an array 91 // (GetStackFrames* only) 92 // 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 94 // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only) 95 int GET_STACK_TRACE_OR_FRAMES { 96 void **sp; 97 // Apple OS X uses an old version of gnu as -- both Darwin 7.9.0 (Panther) 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 105 __asm__ volatile ("mr %0,1" : "=r" (sp)); 106 #endif 107 108 // On PowerPC, the "Link Register" or "Link Record" (LR), is a stack 109 // entry that holds the return address of the subroutine call (what 110 // instruction we run after our function finishes). This is the 111 // same as the stack-pointer of our parent routine, which is what we 112 // want here. While the compiler will always(?) set up LR for 113 // subroutine calls, it may not for leaf functions (such as this one). 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 127 int n = 0; 128 while (sp && n < max_depth) { 129 // The GetStackFrames routine is called when we are in some 130 // informational context (the failure signal handler for example). 131 // Use the non-strict unwinding rules to produce a stack trace 132 // that is as complete as possible (even if it contains a few 133 // bogus entries in some rare cases). 134 void **next_sp = NextStackFrame<!IS_STACK_FRAMES>(sp); 135 136 if (skip_count > 0) { 137 skip_count--; 138 } else { 139 // PowerPC has 3 main ABIs, which say where in the stack the 140 // Link Register is. For DARWIN and AIX (used by apple and 141 // linux ppc64), it's in sp[2]. For SYSV (used by linux ppc), 142 // it's in sp[1]. 143 #if defined(_CALL_AIX) || defined(_CALL_DARWIN) 144 result[n] = *(sp+2); 145 #elif defined(_CALL_SYSV) 146 result[n] = *(sp+1); 147 #elif defined(__APPLE__) || (defined(__linux) && defined(__PPC64__)) 148 // This check is in case the compiler doesn't define _CALL_AIX/etc. 149 result[n] = *(sp+2); 150 #elif defined(__linux) 151 // This check is in case the compiler doesn't define _CALL_SYSV. 152 result[n] = *(sp+1); 153 #else 154 #error Need to specify the PPC ABI for your archiecture. 155 #endif 156 157 #if IS_STACK_FRAMES 158 if (next_sp > sp) { 159 sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp; 160 } else { 161 // A frame-size of 0 is used to indicate unknown frame size. 162 sizes[n] = 0; 163 } 164 #endif 165 n++; 166 } 167 sp = next_sp; 168 } 169 return n; 170 } 171