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) 2005, 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: Arun Sharma |
| 33 | // |
| 34 | // Produce stack trace using libunwind |
| 35 | |
| 36 | #ifndef BASE_STACKTRACE_LIBINWIND_INL_H_ |
| 37 | #define BASE_STACKTRACE_LIBINWIND_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 | // We only need local unwinder. |
| 42 | #define UNW_LOCAL_ONLY |
| 43 | |
| 44 | extern "C" { |
| 45 | #include <assert.h> |
| 46 | #include <string.h> // for memset() |
| 47 | #include <libunwind.h> |
| 48 | } |
| 49 | #include "gperftools/stacktrace.h" |
| 50 | #include "base/logging.h" |
| 51 | |
| 52 | // Sometimes, we can try to get a stack trace from within a stack |
| 53 | // trace, because libunwind can call mmap (maybe indirectly via an |
| 54 | // internal mmap based memory allocator), and that mmap gets trapped |
| 55 | // and causes a stack-trace request. If were to try to honor that |
| 56 | // recursive request, we'd end up with infinite recursion or deadlock. |
| 57 | // Luckily, it's safe to ignore those subsequent traces. In such |
| 58 | // cases, we return 0 to indicate the situation. |
| 59 | static __thread int recursive; |
| 60 | |
| 61 | #if defined(TCMALLOC_ENABLE_UNWIND_FROM_UCONTEXT) && (defined(__i386__) || defined(__x86_64__)) && defined(__GNU_LIBRARY__) |
| 62 | #define BASE_STACKTRACE_UNW_CONTEXT_IS_UCONTEXT 1 |
| 63 | #endif |
| 64 | |
| 65 | #endif // BASE_STACKTRACE_LIBINWIND_INL_H_ |
| 66 | |
| 67 | // Note: this part of the file is included several times. |
| 68 | // Do not put globals below. |
| 69 | |
| 70 | // The following 4 functions are generated from the code below: |
| 71 | // GetStack{Trace,Frames}() |
| 72 | // GetStack{Trace,Frames}WithContext() |
| 73 | // |
| 74 | // These functions take the following args: |
| 75 | // void** result: the stack-trace, as an array |
| 76 | // int* sizes: the size of each stack frame, as an array |
| 77 | // (GetStackFrames* only) |
| 78 | // int max_depth: the size of the result (and sizes) array(s) |
| 79 | // int skip_count: how many stack pointers to skip before storing in result |
| 80 | // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only) |
| 81 | static int GET_STACK_TRACE_OR_FRAMES { |
| 82 | void *ip; |
| 83 | int n = 0; |
| 84 | unw_cursor_t cursor; |
| 85 | unw_context_t uc; |
| 86 | #if IS_STACK_FRAMES |
| 87 | unw_word_t sp = 0, next_sp = 0; |
| 88 | #endif |
| 89 | |
| 90 | if (recursive) { |
| 91 | return 0; |
| 92 | } |
| 93 | ++recursive; |
| 94 | |
| 95 | #if (IS_WITH_CONTEXT && defined(BASE_STACKTRACE_UNW_CONTEXT_IS_UCONTEXT)) |
| 96 | if (ucp) { |
| 97 | uc = *(static_cast<unw_context_t *>(const_cast<void *>(ucp))); |
| 98 | /* this is a bit weird. profiler.cc calls us with signal's ucontext |
| 99 | * yet passing us 2 as skip_count and essentially assuming we won't |
| 100 | * use ucontext. */ |
| 101 | /* In order to fix that I'm going to assume that if ucp is |
| 102 | * non-null we're asked to ignore skip_count in case we're |
| 103 | * able to use ucp */ |
| 104 | skip_count = 0; |
| 105 | } else { |
| 106 | unw_getcontext(&uc); |
| 107 | skip_count += 2; // Do not include current and parent frame |
| 108 | } |
| 109 | #else |
| 110 | unw_getcontext(&uc); |
| 111 | skip_count += 2; // Do not include current and parent frame |
| 112 | #endif |
| 113 | |
| 114 | int ret = unw_init_local(&cursor, &uc); |
| 115 | assert(ret >= 0); |
| 116 | |
| 117 | while (skip_count--) { |
| 118 | if (unw_step(&cursor) <= 0) { |
| 119 | goto out; |
| 120 | } |
| 121 | #if IS_STACK_FRAMES |
| 122 | if (unw_get_reg(&cursor, UNW_REG_SP, &next_sp)) { |
| 123 | goto out; |
| 124 | } |
| 125 | #endif |
| 126 | } |
| 127 | |
| 128 | while (n < max_depth) { |
| 129 | if (unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip) < 0) { |
| 130 | break; |
| 131 | } |
| 132 | #if IS_STACK_FRAMES |
| 133 | sizes[n] = 0; |
| 134 | #endif |
| 135 | result[n++] = ip; |
| 136 | if (unw_step(&cursor) <= 0) { |
| 137 | break; |
| 138 | } |
| 139 | #if IS_STACK_FRAMES |
| 140 | sp = next_sp; |
| 141 | if (unw_get_reg(&cursor, UNW_REG_SP, &next_sp) , 0) { |
| 142 | break; |
| 143 | } |
| 144 | sizes[n - 1] = next_sp - sp; |
| 145 | #endif |
| 146 | } |
| 147 | out: |
| 148 | --recursive; |
| 149 | return n; |
| 150 | } |