Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame^] | 1 | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 | // Copyright (c) 2007, Google Inc. |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // --- |
| 32 | // Author: Craig Silverstein |
| 33 | // |
| 34 | // Produce stack trace. ABI documentation reference can be found at: |
| 35 | // * PowerPC32 ABI: https://www.power.org/documentation/ |
| 36 | // power-architecture-32-bit-abi-supplement-1-0-embeddedlinuxunified/ |
| 37 | // * PowerPC64 ABI: |
| 38 | // http://www.linux-foundation.org/spec/ELF/ppc64/PPC-elf64abi-1.9.html#STACK |
| 39 | |
| 40 | #ifndef BASE_STACKTRACE_POWERPC_INL_H_ |
| 41 | #define BASE_STACKTRACE_POWERPC_INL_H_ |
| 42 | // Note: this file is included into stacktrace.cc more than once. |
| 43 | // Anything that should only be defined once should be here: |
| 44 | |
| 45 | #include <stdint.h> // for uintptr_t |
| 46 | #include <stdlib.h> // for NULL |
| 47 | #include <gperftools/stacktrace.h> |
| 48 | #include <base/vdso_support.h> |
| 49 | |
| 50 | #if defined(HAVE_SYS_UCONTEXT_H) |
| 51 | #include <sys/ucontext.h> |
| 52 | #elif defined(HAVE_UCONTEXT_H) |
| 53 | #include <ucontext.h> // for ucontext_t |
| 54 | #endif |
| 55 | typedef ucontext ucontext_t; |
| 56 | |
| 57 | // PowerPC64 Little Endian follows BE wrt. backchain, condition register, |
| 58 | // and LR save area, so no need to adjust the reading struct. |
| 59 | struct layout_ppc { |
| 60 | struct layout_ppc *next; |
| 61 | #ifdef __PPC64__ |
| 62 | long condition_register; |
| 63 | #endif |
| 64 | void *return_addr; |
| 65 | }; |
| 66 | |
| 67 | // Signal callbacks are handled by the vDSO symbol: |
| 68 | // |
| 69 | // * PowerPC64 Linux (arch/powerpc/kernel/vdso64/sigtramp.S): |
| 70 | // __kernel_sigtramp_rt64 |
| 71 | // * PowerPC32 Linux (arch/powerpc/kernel/vdso32/sigtramp.S): |
| 72 | // __kernel_sigtramp32 |
| 73 | // __kernel_sigtramp_rt32 |
| 74 | // |
| 75 | // So a backtrace may need to specially handling if the symbol readed is |
| 76 | // the signal trampoline. |
| 77 | |
| 78 | // Given a pointer to a stack frame, locate and return the calling |
| 79 | // stackframe, or return NULL if no stackframe can be found. Perform sanity |
| 80 | // checks (the strictness of which is controlled by the boolean parameter |
| 81 | // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned. |
| 82 | template<bool STRICT_UNWINDING> |
| 83 | static layout_ppc *NextStackFrame(layout_ppc *current) { |
| 84 | uintptr_t old_sp = (uintptr_t)(current); |
| 85 | uintptr_t new_sp = (uintptr_t)(current->next); |
| 86 | |
| 87 | // Check that the transition from frame pointer old_sp to frame |
| 88 | // pointer new_sp isn't clearly bogus |
| 89 | if (STRICT_UNWINDING) { |
| 90 | // With the stack growing downwards, older stack frame must be |
| 91 | // at a greater address that the current one. |
| 92 | if (new_sp <= old_sp) |
| 93 | return NULL; |
| 94 | // Assume stack frames larger than 100,000 bytes are bogus. |
| 95 | if (new_sp - old_sp > 100000) |
| 96 | return NULL; |
| 97 | } else { |
| 98 | // In the non-strict mode, allow discontiguous stack frames. |
| 99 | // (alternate-signal-stacks for example). |
| 100 | if (new_sp == old_sp) |
| 101 | return NULL; |
| 102 | // And allow frames upto about 1MB. |
| 103 | if ((new_sp > old_sp) && (new_sp - old_sp > 1000000)) |
| 104 | return NULL; |
| 105 | } |
| 106 | if (new_sp & (sizeof(void *) - 1)) |
| 107 | return NULL; |
| 108 | return current->next; |
| 109 | } |
| 110 | |
| 111 | // This ensures that GetStackTrace stes up the Link Register properly. |
| 112 | void StacktracePowerPCDummyFunction() __attribute__((noinline)); |
| 113 | void StacktracePowerPCDummyFunction() { __asm__ volatile(""); } |
| 114 | #endif // BASE_STACKTRACE_POWERPC_INL_H_ |
| 115 | |
| 116 | // Note: this part of the file is included several times. |
| 117 | // Do not put globals below. |
| 118 | |
| 119 | // Load instruction used on top-of-stack get. |
| 120 | #if defined(__PPC64__) || defined(__LP64__) |
| 121 | # define LOAD "ld" |
| 122 | #else |
| 123 | # define LOAD "lwz" |
| 124 | #endif |
| 125 | |
| 126 | // The following 4 functions are generated from the code below: |
| 127 | // GetStack{Trace,Frames}() |
| 128 | // GetStack{Trace,Frames}WithContext() |
| 129 | // |
| 130 | // These functions take the following args: |
| 131 | // void** result: the stack-trace, as an array |
| 132 | // int* sizes: the size of each stack frame, as an array |
| 133 | // (GetStackFrames* only) |
| 134 | // int max_depth: the size of the result (and sizes) array(s) |
| 135 | // int skip_count: how many stack pointers to skip before storing in result |
| 136 | // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only) |
| 137 | static int GET_STACK_TRACE_OR_FRAMES { |
| 138 | layout_ppc *current; |
| 139 | int n; |
| 140 | |
| 141 | // Get the address on top-of-stack |
| 142 | current = reinterpret_cast<layout_ppc*> (__builtin_frame_address (0)); |
| 143 | // And ignore the current symbol |
| 144 | current = current->next; |
| 145 | |
| 146 | StacktracePowerPCDummyFunction(); |
| 147 | |
| 148 | n = 0; |
| 149 | skip_count++; // skip parent's frame due to indirection in |
| 150 | // stacktrace.cc |
| 151 | |
| 152 | base::VDSOSupport vdso; |
| 153 | base::ElfMemImage::SymbolInfo rt_sigreturn_symbol_info; |
| 154 | #ifdef __PPC64__ |
| 155 | const void *sigtramp64_vdso = 0; |
| 156 | if (vdso.LookupSymbol("__kernel_sigtramp_rt64", "LINUX_2.6.15", STT_NOTYPE, |
| 157 | &rt_sigreturn_symbol_info)) |
| 158 | sigtramp64_vdso = rt_sigreturn_symbol_info.address; |
| 159 | #else |
| 160 | const void *sigtramp32_vdso = 0; |
| 161 | if (vdso.LookupSymbol("__kernel_sigtramp32", "LINUX_2.6.15", STT_NOTYPE, |
| 162 | &rt_sigreturn_symbol_info)) |
| 163 | sigtramp32_vdso = rt_sigreturn_symbol_info.address; |
| 164 | const void *sigtramp32_rt_vdso = 0; |
| 165 | if (vdso.LookupSymbol("__kernel_sigtramp_rt32", "LINUX_2.6.15", STT_NOTYPE, |
| 166 | &rt_sigreturn_symbol_info)) |
| 167 | sigtramp32_rt_vdso = rt_sigreturn_symbol_info.address; |
| 168 | #endif |
| 169 | |
| 170 | while (current && n < max_depth) { |
| 171 | |
| 172 | // The GetStackFrames routine is called when we are in some |
| 173 | // informational context (the failure signal handler for example). |
| 174 | // Use the non-strict unwinding rules to produce a stack trace |
| 175 | // that is as complete as possible (even if it contains a few |
| 176 | // bogus entries in some rare cases). |
| 177 | layout_ppc *next = NextStackFrame<!IS_STACK_FRAMES>(current); |
| 178 | if (skip_count > 0) { |
| 179 | skip_count--; |
| 180 | } else { |
| 181 | result[n] = current->return_addr; |
| 182 | #ifdef __PPC64__ |
| 183 | if (sigtramp64_vdso && (sigtramp64_vdso == current->return_addr)) { |
| 184 | struct signal_frame_64 { |
| 185 | char dummy[128]; |
| 186 | ucontext_t uc; |
| 187 | // We don't care about the rest, since the IP value is at 'uc' field. |
| 188 | } *sigframe = reinterpret_cast<signal_frame_64*>(current); |
| 189 | result[n] = (void*) sigframe->uc.uc_mcontext.gp_regs[PT_NIP]; |
| 190 | } |
| 191 | #else |
| 192 | if (sigtramp32_vdso && (sigtramp32_vdso == current->return_addr)) { |
| 193 | struct signal_frame_32 { |
| 194 | char dummy[64]; |
| 195 | struct sigcontext sctx; |
| 196 | mcontext_t mctx; |
| 197 | // We don't care about the rest, since IP value is at 'mctx' field. |
| 198 | } *sigframe = reinterpret_cast<signal_frame_32*>(current); |
| 199 | result[n] = (void*) sigframe->mctx.gregs[PT_NIP]; |
| 200 | } else if (sigtramp32_rt_vdso && (sigtramp32_rt_vdso == current->return_addr)) { |
| 201 | struct rt_signal_frame_32 { |
| 202 | char dummy[64 + 16]; |
| 203 | siginfo_t info; |
| 204 | struct ucontext uc; |
| 205 | // We don't care about the rest, since IP value is at 'uc' field.A |
| 206 | } *sigframe = reinterpret_cast<rt_signal_frame_32*>(current); |
| 207 | result[n] = (void*) sigframe->uc.uc_mcontext.uc_regs->gregs[PT_NIP]; |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | #if IS_STACK_FRAMES |
| 212 | if (next > current) { |
| 213 | sizes[n] = (uintptr_t)next - (uintptr_t)current; |
| 214 | } else { |
| 215 | // A frame-size of 0 is used to indicate unknown frame size. |
| 216 | sizes[n] = 0; |
| 217 | } |
| 218 | #endif |
| 219 | n++; |
| 220 | } |
| 221 | current = next; |
| 222 | } |
| 223 | |
| 224 | // It's possible the second-last stack frame can't return |
| 225 | // (that is, it's __libc_start_main), in which case |
| 226 | // the CRT startup code will have set its LR to 'NULL'. |
| 227 | if (n > 0 && result[n-1] == NULL) |
| 228 | n--; |
| 229 | |
| 230 | return n; |
| 231 | } |