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