Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 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 | // Produce stack trace. ABI documentation reference can be found at: |
| 32 | // * PowerPC32 ABI: https://www.power.org/documentation/ |
| 33 | // power-architecture-32-bit-abi-supplement-1-0-embeddedlinuxunified/ |
| 34 | // * PowerPC64 ABI: |
| 35 | // http://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK |
| 36 | |
| 37 | #ifndef BASE_STACKTRACE_POWERPC_INL_H_ |
| 38 | #define BASE_STACKTRACE_POWERPC_INL_H_ |
| 39 | // Note: this file is included into stacktrace.cc more than once. |
| 40 | // Anything that should only be defined once should be here: |
| 41 | |
| 42 | #include <stdint.h> // for uintptr_t |
| 43 | #include <stdlib.h> // for NULL |
| 44 | #include <gperftools/stacktrace.h> |
| 45 | |
| 46 | // Given a pointer to a stack frame, locate and return the calling |
| 47 | // stackframe, or return NULL if no stackframe can be found. Perform sanity |
| 48 | // checks (the strictness of which is controlled by the boolean parameter |
| 49 | // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned. |
| 50 | template<bool STRICT_UNWINDING> |
| 51 | static void **NextStackFrame(void **old_sp) { |
| 52 | void **new_sp = (void **) *old_sp; |
| 53 | |
| 54 | // Check that the transition from frame pointer old_sp to frame |
| 55 | // pointer new_sp isn't clearly bogus |
| 56 | if (STRICT_UNWINDING) { |
| 57 | // With the stack growing downwards, older stack frame must be |
| 58 | // at a greater address that the current one. |
| 59 | if (new_sp <= old_sp) return NULL; |
| 60 | // Assume stack frames larger than 100,000 bytes are bogus. |
| 61 | if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL; |
| 62 | } else { |
| 63 | // In the non-strict mode, allow discontiguous stack frames. |
| 64 | // (alternate-signal-stacks for example). |
| 65 | if (new_sp == old_sp) return NULL; |
| 66 | // And allow frames upto about 1MB. |
| 67 | if ((new_sp > old_sp) |
| 68 | && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL; |
| 69 | } |
| 70 | if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL; |
| 71 | return new_sp; |
| 72 | } |
| 73 | |
| 74 | // This ensures that GetStackTrace stes up the Link Register properly. |
| 75 | void StacktracePowerPCDummyFunction() __attribute__((noinline)); |
| 76 | void StacktracePowerPCDummyFunction() { __asm__ volatile(""); } |
| 77 | #endif // BASE_STACKTRACE_POWERPC_INL_H_ |
| 78 | |
| 79 | // Note: this part of the file is included several times. |
| 80 | // Do not put globals below. |
| 81 | |
| 82 | // The following 4 functions are generated from the code below: |
| 83 | // GetStack{Trace,Frames}() |
| 84 | // GetStack{Trace,Frames}WithContext() |
| 85 | // |
| 86 | // These functions take the following args: |
| 87 | // void** result: the stack-trace, as an array |
| 88 | // int* sizes: the size of each stack frame, as an array |
| 89 | // (GetStackFrames* only) |
| 90 | // int max_depth: the size of the result (and sizes) array(s) |
| 91 | // int skip_count: how many stack pointers to skip before storing in result |
| 92 | // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only) |
| 93 | int GET_STACK_TRACE_OR_FRAMES { |
| 94 | void **sp; |
| 95 | // Apple OS X uses an old version of gnu as -- both Darwin 7.9.0 (Panther) |
| 96 | // and Darwin 8.8.1 (Tiger) use as 1.38. This means we have to use a |
| 97 | // different asm syntax. I don't know quite the best way to discriminate |
| 98 | // systems using the old as from the new one; I've gone with __APPLE__. |
| 99 | // TODO(csilvers): use autoconf instead, to look for 'as --version' == 1 or 2 |
| 100 | __asm__ volatile ("mr %0,r1" : "=r" (sp)); |
| 101 | |
| 102 | // On PowerPC, the "Link Register" or "Link Record" (LR), is a stack |
| 103 | // entry that holds the return address of the subroutine call (what |
| 104 | // instruction we run after our function finishes). This is the |
| 105 | // same as the stack-pointer of our parent routine, which is what we |
| 106 | // want here. While the compiler will always(?) set up LR for |
| 107 | // subroutine calls, it may not for leaf functions (such as this one). |
| 108 | // This routine forces the compiler (at least gcc) to push it anyway. |
| 109 | StacktracePowerPCDummyFunction(); |
| 110 | |
| 111 | #if IS_STACK_FRAMES |
| 112 | // Note we do *not* increment skip_count here for the SYSV ABI. If |
| 113 | // we did, the list of stack frames wouldn't properly match up with |
| 114 | // the list of return addresses. Note this means the top pc entry |
| 115 | // is probably bogus for linux/ppc (and other SYSV-ABI systems). |
| 116 | #else |
| 117 | // The LR save area is used by the callee, so the top entry is bogus. |
| 118 | skip_count++; |
| 119 | #endif |
| 120 | |
| 121 | int n = 0; |
| 122 | while (sp && n < max_depth) { |
| 123 | // The GetStackFrames routine is called when we are in some |
| 124 | // informational context (the failure signal handler for example). |
| 125 | // Use the non-strict unwinding rules to produce a stack trace |
| 126 | // that is as complete as possible (even if it contains a few |
| 127 | // bogus entries in some rare cases). |
| 128 | void **next_sp = NextStackFrame<!IS_STACK_FRAMES>(sp); |
| 129 | |
| 130 | if (skip_count > 0) { |
| 131 | skip_count--; |
| 132 | } else { |
| 133 | // PowerPC has 3 main ABIs, which say where in the stack the |
| 134 | // Link Register is. For DARWIN and AIX (used by apple and |
| 135 | // linux ppc64), it's in sp[2]. For SYSV (used by linux ppc), |
| 136 | // it's in sp[1]. |
| 137 | #if defined(__PPC64__) |
| 138 | // This check is in case the compiler doesn't define _CALL_AIX/etc. |
| 139 | result[n] = *(sp+2); |
| 140 | #elif defined(__linux) |
| 141 | // This check is in case the compiler doesn't define _CALL_SYSV. |
| 142 | result[n] = *(sp+1); |
| 143 | #endif |
| 144 | |
| 145 | #if IS_STACK_FRAMES |
| 146 | if (next_sp > sp) { |
| 147 | sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp; |
| 148 | } else { |
| 149 | // A frame-size of 0 is used to indicate unknown frame size. |
| 150 | sizes[n] = 0; |
| 151 | } |
| 152 | #endif |
| 153 | n++; |
| 154 | } |
| 155 | sp = next_sp; |
| 156 | } |
| 157 | return n; |
| 158 | } |