| 1 | // Implementation of <stacktrace> -*- C++ -*- |
| 2 | |
| 3 | // Copyright The GNU Toolchain Authors. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3. |
| 9 | |
| 10 | // This library is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | |
| 15 | // Under Section 7 of GPL version 3, you are granted additional |
| 16 | // permissions described in the GCC Runtime Library Exception, version |
| 17 | // 3.1, as published by the Free Software Foundation. |
| 18 | |
| 19 | // You should have received a copy of the GNU General Public License and |
| 20 | // a copy of the GCC Runtime Library Exception along with this program; |
| 21 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 22 | // <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | #include <stacktrace> |
| 25 | #include <cstdlib> |
| 26 | |
| 27 | #ifdef __cpp_lib_stacktrace // C++ >= 23 && hosted && HAVE_STACKTRACE |
| 28 | struct __glibcxx_backtrace_state; |
| 29 | |
| 30 | extern "C" |
| 31 | { |
| 32 | __glibcxx_backtrace_state* |
| 33 | __glibcxx_backtrace_create_state(const char*, int, |
| 34 | void(*)(void*, const char*, int), |
| 35 | void*); |
| 36 | |
| 37 | int |
| 38 | __glibcxx_backtrace_simple(__glibcxx_backtrace_state*, int, |
| 39 | int (*) (void*, __UINTPTR_TYPE__), |
| 40 | void(*)(void*, const char*, int), |
| 41 | void*); |
| 42 | int |
| 43 | __glibcxx_backtrace_pcinfo(__glibcxx_backtrace_state*, __UINTPTR_TYPE__, |
| 44 | int (*)(void*, __UINTPTR_TYPE__, |
| 45 | const char*, int, const char*), |
| 46 | void(*)(void*, const char*, int), |
| 47 | void*); |
| 48 | |
| 49 | int |
| 50 | __glibcxx_backtrace_syminfo(__glibcxx_backtrace_state*, __UINTPTR_TYPE__ addr, |
| 51 | void (*) (void*, __UINTPTR_TYPE__, const char*, |
| 52 | __UINTPTR_TYPE__, __UINTPTR_TYPE__), |
| 53 | void(*)(void*, const char*, int), |
| 54 | void*); |
| 55 | } |
| 56 | |
| 57 | namespace __cxxabiv1 |
| 58 | { |
| 59 | extern "C" char* |
| 60 | __cxa_demangle(const char* mangled_name, char* output_buffer, size_t* length, |
| 61 | int* status); |
| 62 | } |
| 63 | |
| 64 | namespace std |
| 65 | { |
| 66 | namespace |
| 67 | { |
| 68 | char* |
| 69 | demangle(const char* name) |
| 70 | { |
| 71 | int status; |
| 72 | char* str = __cxxabiv1::__cxa_demangle(name, nullptr, nullptr, &status); |
| 73 | if (status == 0) |
| 74 | return str; |
| 75 | else |
| 76 | { |
| 77 | std::free(str); |
| 78 | return nullptr; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | err_handler(void*, const char*, int) |
| 84 | { } |
| 85 | |
| 86 | __glibcxx_backtrace_state* |
| 87 | init() |
| 88 | { |
| 89 | #if __GTHREADS && ! defined(__cpp_threadsafe_static_init) |
| 90 | # warning "std::stacktrace initialization will not be thread-safe" |
| 91 | #endif |
| 92 | static __glibcxx_backtrace_state* state |
| 93 | = __glibcxx_backtrace_create_state(nullptr, 1, err_handler, nullptr); |
| 94 | return state; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | stacktrace_entry::_Info::_M_set_file(const char* filename) |
| 100 | { |
| 101 | if (filename && _M_file) |
| 102 | _M_set(_M_file, filename); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | stacktrace_entry::_Info::_M_set_desc(const char* function) |
| 107 | { |
| 108 | if (function && _M_desc) |
| 109 | { |
| 110 | if (char* s = demangle(function)) |
| 111 | { |
| 112 | _M_set(_M_desc, s); |
| 113 | std::free(s); |
| 114 | } |
| 115 | else |
| 116 | _M_set(_M_desc, function); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #pragma GCC diagnostic push |
| 121 | // The closure types below don't escape so we don't care about their mangling. |
| 122 | #pragma GCC diagnostic ignored "-Wabi" |
| 123 | bool |
| 124 | stacktrace_entry::_Info::_M_populate(native_handle_type pc) |
| 125 | { |
| 126 | auto cb = [](void* self, uintptr_t, const char* filename, int lineno, |
| 127 | const char* function) -> int |
| 128 | { |
| 129 | auto& info = *static_cast<_Info*>(self); |
| 130 | info._M_set_desc(function); |
| 131 | info._M_set_file(filename); |
| 132 | if (info._M_line) |
| 133 | *info._M_line = lineno; |
| 134 | return function != nullptr; |
| 135 | }; |
| 136 | const auto state = init(); |
| 137 | if (::__glibcxx_backtrace_pcinfo(state, pc, +cb, err_handler, this)) |
| 138 | return true; |
| 139 | |
| 140 | // If we get here then backtrace_pcinfo did not give us a function name. |
| 141 | // If the caller wanted a function name, try again using backtrace_syminfo. |
| 142 | if (_M_desc) |
| 143 | { |
| 144 | auto cb2 = [](void* self, uintptr_t, const char* symname, |
| 145 | uintptr_t, uintptr_t) |
| 146 | { |
| 147 | static_cast<_Info*>(self)->_M_set_desc(symname); |
| 148 | }; |
| 149 | if (::__glibcxx_backtrace_syminfo(state, pc, +cb2, err_handler, this)) |
| 150 | return true; |
| 151 | } |
| 152 | return false; |
| 153 | } |
| 154 | #pragma GCC diagnostic pop |
| 155 | |
| 156 | // Ensure no tail-call optimization, so that this frame isn't reused for the |
| 157 | // backtrace_simple call, so that the number of frames to skip doesn't vary. |
| 158 | [[gnu::optimize("no-optimize-sibling-calls")]] |
| 159 | int |
| 160 | __stacktrace_impl::_S_current(int (*cb) (void*, __UINTPTR_TYPE__), void* obj, |
| 161 | int skip) |
| 162 | { |
| 163 | const auto state = init(); |
| 164 | // skip+2 because we don't want this function or its caller to be included. |
| 165 | int r = ::__glibcxx_backtrace_simple(state, skip + 2, cb, err_handler, obj); |
| 166 | // Could also use this to prevent tail-call optim: __asm ("" : "+g" (r)); |
| 167 | return r; |
| 168 | } |
| 169 | |
| 170 | } |
| 171 | #endif |