Austin Schuh | 906616c | 2019-01-21 20:25:11 -0800 | [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 | // Author: Craig Silverstein |
| 31 | // |
| 32 | // Produce stack trace. I'm guessing (hoping!) the code is much like |
| 33 | // for x86. For apple machines, at least, it seems to be; see |
| 34 | // http://developer.apple.com/documentation/mac/runtimehtml/RTArch-59.html |
| 35 | // http://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK |
| 36 | // Linux has similar code: http://patchwork.ozlabs.org/linuxppc/patch?id=8882 |
| 37 | |
| 38 | #include <stdio.h> |
| 39 | #include <stdint.h> // for uintptr_t |
| 40 | #include "stacktrace.h" |
| 41 | |
| 42 | _START_GOOGLE_NAMESPACE_ |
| 43 | |
| 44 | // Given a pointer to a stack frame, locate and return the calling |
| 45 | // stackframe, or return NULL if no stackframe can be found. Perform sanity |
| 46 | // checks (the strictness of which is controlled by the boolean parameter |
| 47 | // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned. |
| 48 | template<bool STRICT_UNWINDING> |
| 49 | static void **NextStackFrame(void **old_sp) { |
| 50 | void **new_sp = (void **) *old_sp; |
| 51 | |
| 52 | // Check that the transition from frame pointer old_sp to frame |
| 53 | // pointer new_sp isn't clearly bogus |
| 54 | if (STRICT_UNWINDING) { |
| 55 | // With the stack growing downwards, older stack frame must be |
| 56 | // at a greater address that the current one. |
| 57 | if (new_sp <= old_sp) return NULL; |
| 58 | // Assume stack frames larger than 100,000 bytes are bogus. |
| 59 | if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL; |
| 60 | } else { |
| 61 | // In the non-strict mode, allow discontiguous stack frames. |
| 62 | // (alternate-signal-stacks for example). |
| 63 | if (new_sp == old_sp) return NULL; |
| 64 | // And allow frames upto about 1MB. |
| 65 | if ((new_sp > old_sp) |
| 66 | && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL; |
| 67 | } |
| 68 | if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL; |
| 69 | return new_sp; |
| 70 | } |
| 71 | |
| 72 | // This ensures that GetStackTrace stes up the Link Register properly. |
| 73 | void StacktracePowerPCDummyFunction() __attribute__((noinline)); |
| 74 | void StacktracePowerPCDummyFunction() { __asm__ volatile(""); } |
| 75 | |
| 76 | // If you change this function, also change GetStackFrames below. |
| 77 | int GetStackTrace(void** result, int max_depth, int skip_count) { |
| 78 | void **sp; |
| 79 | // Apple OS X uses an old version of gnu as -- both Darwin 7.9.0 (Panther) |
| 80 | // and Darwin 8.8.1 (Tiger) use as 1.38. This means we have to use a |
| 81 | // different asm syntax. I don't know quite the best way to discriminate |
| 82 | // systems using the old as from the new one; I've gone with __APPLE__. |
| 83 | #ifdef __APPLE__ |
| 84 | __asm__ volatile ("mr %0,r1" : "=r" (sp)); |
| 85 | #else |
| 86 | __asm__ volatile ("mr %0,1" : "=r" (sp)); |
| 87 | #endif |
| 88 | |
| 89 | // On PowerPC, the "Link Register" or "Link Record" (LR), is a stack |
| 90 | // entry that holds the return address of the subroutine call (what |
| 91 | // instruction we run after our function finishes). This is the |
| 92 | // same as the stack-pointer of our parent routine, which is what we |
| 93 | // want here. While the compiler will always(?) set up LR for |
| 94 | // subroutine calls, it may not for leaf functions (such as this one). |
| 95 | // This routine forces the compiler (at least gcc) to push it anyway. |
| 96 | StacktracePowerPCDummyFunction(); |
| 97 | |
| 98 | // The LR save area is used by the callee, so the top entry is bogus. |
| 99 | skip_count++; |
| 100 | |
| 101 | int n = 0; |
| 102 | while (sp && n < max_depth) { |
| 103 | if (skip_count > 0) { |
| 104 | skip_count--; |
| 105 | } else { |
| 106 | // PowerPC has 3 main ABIs, which say where in the stack the |
| 107 | // Link Register is. For DARWIN and AIX (used by apple and |
| 108 | // linux ppc64), it's in sp[2]. For SYSV (used by linux ppc), |
| 109 | // it's in sp[1]. |
| 110 | #if defined(_CALL_AIX) || defined(_CALL_DARWIN) |
| 111 | result[n++] = *(sp+2); |
| 112 | #elif defined(_CALL_SYSV) |
| 113 | result[n++] = *(sp+1); |
| 114 | #elif defined(__APPLE__) || ((defined(__linux) || defined(__linux__)) && defined(__PPC64__)) |
| 115 | // This check is in case the compiler doesn't define _CALL_AIX/etc. |
| 116 | result[n++] = *(sp+2); |
| 117 | #elif defined(__linux) |
| 118 | // This check is in case the compiler doesn't define _CALL_SYSV. |
| 119 | result[n++] = *(sp+1); |
| 120 | #else |
| 121 | #error Need to specify the PPC ABI for your archiecture. |
| 122 | #endif |
| 123 | } |
| 124 | // Use strict unwinding rules. |
| 125 | sp = NextStackFrame<true>(sp); |
| 126 | } |
| 127 | return n; |
| 128 | } |
| 129 | |
| 130 | _END_GOOGLE_NAMESPACE_ |