Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 1 | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 | // Copyright (c) 2016, gperftools Contributors |
| 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 | // This file implements backtrace capturing via libgcc's |
| 32 | // _Unwind_Backtrace. This generally works almost always. It will fail |
| 33 | // sometimes when we're trying to capture backtrace from signal |
| 34 | // handler (i.e. in cpu profiler) while some C++ code is throwing |
| 35 | // exception. |
| 36 | |
| 37 | #ifndef BASE_STACKTRACE_LIBGCC_INL_H_ |
| 38 | #define BASE_STACKTRACE_LIBGCC_INL_H_ |
| 39 | // Note: this file is included into stacktrace.cc more than once. |
| 40 | // Anything that should only be defined once should be here: |
| 41 | |
| 42 | extern "C" { |
| 43 | #include <assert.h> |
| 44 | #include <string.h> // for memset() |
| 45 | } |
| 46 | |
| 47 | #include <unwind.h> |
| 48 | |
| 49 | #include "gperftools/stacktrace.h" |
| 50 | |
| 51 | struct libgcc_backtrace_data { |
| 52 | void **array; |
| 53 | int skip; |
| 54 | int pos; |
| 55 | int limit; |
| 56 | }; |
| 57 | |
| 58 | static _Unwind_Reason_Code libgcc_backtrace_helper(struct _Unwind_Context *ctx, |
| 59 | void *_data) { |
| 60 | libgcc_backtrace_data *data = |
| 61 | reinterpret_cast<libgcc_backtrace_data *>(_data); |
| 62 | |
| 63 | if (data->skip > 0) { |
| 64 | data->skip--; |
| 65 | return _URC_NO_REASON; |
| 66 | } |
| 67 | |
| 68 | if (data->pos < data->limit) { |
| 69 | void *ip = reinterpret_cast<void *>(_Unwind_GetIP(ctx));; |
| 70 | data->array[data->pos++] = ip; |
| 71 | } |
| 72 | |
| 73 | return _URC_NO_REASON; |
| 74 | } |
| 75 | |
| 76 | #endif // BASE_STACKTRACE_LIBGCC_INL_H_ |
| 77 | |
| 78 | // Note: this part of the file is included several times. |
| 79 | // Do not put globals below. |
| 80 | |
| 81 | // The following 4 functions are generated from the code below: |
| 82 | // GetStack{Trace,Frames}() |
| 83 | // GetStack{Trace,Frames}WithContext() |
| 84 | // |
| 85 | // These functions take the following args: |
| 86 | // void** result: the stack-trace, as an array |
| 87 | // int* sizes: the size of each stack frame, as an array |
| 88 | // (GetStackFrames* only) |
| 89 | // int max_depth: the size of the result (and sizes) array(s) |
| 90 | // int skip_count: how many stack pointers to skip before storing in result |
| 91 | // void* ucp: a ucontext_t* (GetStack{Trace,Frames}WithContext only) |
| 92 | static int GET_STACK_TRACE_OR_FRAMES { |
| 93 | libgcc_backtrace_data data; |
| 94 | data.array = result; |
| 95 | // we're also skipping current and parent's frame |
| 96 | data.skip = skip_count + 2; |
| 97 | data.pos = 0; |
| 98 | data.limit = max_depth; |
| 99 | |
| 100 | _Unwind_Backtrace(libgcc_backtrace_helper, &data); |
| 101 | |
| 102 | if (data.pos > 1 && data.array[data.pos - 1] == NULL) |
| 103 | --data.pos; |
| 104 | |
| 105 | #if IS_STACK_FRAMES |
| 106 | // No implementation for finding out the stack frame sizes. |
| 107 | memset(sizes, 0, sizeof(*sizes) * data.pos); |
| 108 | #endif |
| 109 | |
| 110 | return data.pos; |
| 111 | } |