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) 2011, 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: Doug Kwan |
| 33 | // This is inspired by Craig Silverstein's PowerPC stacktrace code. |
| 34 | // |
| 35 | |
| 36 | #ifndef BASE_STACKTRACE_ARM_INL_H_ |
| 37 | #define BASE_STACKTRACE_ARM_INL_H_ |
| 38 | // Note: this file is included into stacktrace.cc more than once. |
| 39 | // Anything that should only be defined once should be here: |
| 40 | |
| 41 | #include <stdint.h> // for uintptr_t |
| 42 | #include "base/basictypes.h" // for NULL |
| 43 | #include <gperftools/stacktrace.h> |
| 44 | |
| 45 | // WARNING: |
| 46 | // This only works if all your code is in either ARM or THUMB mode. With |
| 47 | // interworking, the frame pointer of the caller can either be in r11 (ARM |
| 48 | // mode) or r7 (THUMB mode). A callee only saves the frame pointer of its |
| 49 | // mode in a fixed location on its stack frame. If the caller is a different |
| 50 | // mode, there is no easy way to find the frame pointer. It can either be |
| 51 | // still in the designated register or saved on stack along with other callee |
| 52 | // saved registers. |
| 53 | |
| 54 | // Given a pointer to a stack frame, locate and return the calling |
| 55 | // stackframe, or return NULL if no stackframe can be found. Perform sanity |
| 56 | // checks (the strictness of which is controlled by the boolean parameter |
| 57 | // "STRICT_UNWINDING") to reduce the chance that a bad pointer is returned. |
| 58 | template<bool STRICT_UNWINDING> |
| 59 | static void **NextStackFrame(void **old_sp) { |
| 60 | void **new_sp = (void**) old_sp[-1]; |
| 61 | |
| 62 | // Check that the transition from frame pointer old_sp to frame |
| 63 | // pointer new_sp isn't clearly bogus |
| 64 | if (STRICT_UNWINDING) { |
| 65 | // With the stack growing downwards, older stack frame must be |
| 66 | // at a greater address that the current one. |
| 67 | if (new_sp <= old_sp) return NULL; |
| 68 | // Assume stack frames larger than 100,000 bytes are bogus. |
| 69 | if ((uintptr_t)new_sp - (uintptr_t)old_sp > 100000) return NULL; |
| 70 | } else { |
| 71 | // In the non-strict mode, allow discontiguous stack frames. |
| 72 | // (alternate-signal-stacks for example). |
| 73 | if (new_sp == old_sp) return NULL; |
| 74 | // And allow frames upto about 1MB. |
| 75 | if ((new_sp > old_sp) |
| 76 | && ((uintptr_t)new_sp - (uintptr_t)old_sp > 1000000)) return NULL; |
| 77 | } |
| 78 | if ((uintptr_t)new_sp & (sizeof(void *) - 1)) return NULL; |
| 79 | return new_sp; |
| 80 | } |
| 81 | |
| 82 | // This ensures that GetStackTrace stes up the Link Register properly. |
| 83 | #ifdef __GNUC__ |
| 84 | void StacktraceArmDummyFunction() __attribute__((noinline)); |
| 85 | void StacktraceArmDummyFunction() { __asm__ volatile(""); } |
| 86 | #else |
| 87 | # error StacktraceArmDummyFunction() needs to be ported to this platform. |
| 88 | #endif |
| 89 | #endif // BASE_STACKTRACE_ARM_INL_H_ |
| 90 | |
| 91 | // Note: this part of the file is included several times. |
| 92 | // Do not put globals below. |
| 93 | |
| 94 | // The following 4 functions are generated from the code below: |
| 95 | // GetStack{Trace,Frames}() |
| 96 | // GetStack{Trace,Frames}WithContext() |
| 97 | // |
| 98 | // These functions take the following args: |
| 99 | // void** result: the stack-trace, as an array |
| 100 | // int* sizes: the size of each stack frame, as an array |
| 101 | // (GetStackFrames* only) |
| 102 | // int max_depth: the size of the result (and sizes) array(s) |
| 103 | // int skip_count: how many stack pointers to skip before storing in result |
| 104 | // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only) |
| 105 | static int GET_STACK_TRACE_OR_FRAMES { |
| 106 | #ifdef __GNUC__ |
| 107 | void **sp = reinterpret_cast<void**>(__builtin_frame_address(0)); |
| 108 | #else |
| 109 | # error reading stack point not yet supported on this platform. |
| 110 | #endif |
| 111 | |
| 112 | // On ARM, the return address is stored in the link register (r14). |
| 113 | // This is not saved on the stack frame of a leaf function. To |
| 114 | // simplify code that reads return addresses, we call a dummy |
| 115 | // function so that the return address of this function is also |
| 116 | // stored in the stack frame. This works at least for gcc. |
| 117 | StacktraceArmDummyFunction(); |
| 118 | |
| 119 | skip_count++; // skip parent frame due to indirection in stacktrace.cc |
| 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 bogus |
| 127 | // entries in some rare cases). |
| 128 | void **next_sp = NextStackFrame<IS_STACK_FRAMES == 0>(sp); |
| 129 | |
| 130 | if (skip_count > 0) { |
| 131 | skip_count--; |
| 132 | } else { |
| 133 | result[n] = *sp; |
| 134 | |
| 135 | #if IS_STACK_FRAMES |
| 136 | if (next_sp > sp) { |
| 137 | sizes[n] = (uintptr_t)next_sp - (uintptr_t)sp; |
| 138 | } else { |
| 139 | // A frame-size of 0 is used to indicate unknown frame size. |
| 140 | sizes[n] = 0; |
| 141 | } |
| 142 | #endif |
| 143 | n++; |
| 144 | } |
| 145 | sp = next_sp; |
| 146 | } |
| 147 | return n; |
| 148 | } |