|
| 1 | +/**************************************************************************/ |
| 2 | +/* error_backtrace.cpp */ |
| 3 | +/**************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/**************************************************************************/ |
| 8 | +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | +/* */ |
| 11 | +/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | +/* a copy of this software and associated documentation files (the */ |
| 13 | +/* "Software"), to deal in the Software without restriction, including */ |
| 14 | +/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | +/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | +/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | +/* the following conditions: */ |
| 18 | +/* */ |
| 19 | +/* The above copyright notice and this permission notice shall be */ |
| 20 | +/* included in all copies or substantial portions of the Software. */ |
| 21 | +/* */ |
| 22 | +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | +/**************************************************************************/ |
| 30 | + |
| 31 | +#include "core/error/error_backtrace.h" |
| 32 | + |
| 33 | +#include "core/string/ustring.h" |
| 34 | + |
| 35 | +#include <cstring> |
| 36 | + |
| 37 | +#if defined(ERROR_BACKTRACE_ENABLED) |
| 38 | + |
| 39 | +#include <cstdio> |
| 40 | + |
| 41 | +#ifdef WINDOWS_ENABLED |
| 42 | + |
| 43 | +#include "core/os/mutex.h" |
| 44 | + |
| 45 | +#include <windows.h> |
| 46 | + |
| 47 | +// Some versions of imagehlp.dll lack the proper packing directives themselves |
| 48 | +// so we need to do it. |
| 49 | +#pragma pack(push, before_imagehlp, 8) |
| 50 | +#include <imagehlp.h> |
| 51 | +#pragma pack(pop, before_imagehlp) |
| 52 | + |
| 53 | +static Mutex sym_init_mutex; |
| 54 | +static bool sym_initialized = false; |
| 55 | + |
| 56 | +static void _ensure_sym_initialized() { |
| 57 | + MutexLock lock(sym_init_mutex); |
| 58 | + if (sym_initialized) { |
| 59 | + return; |
| 60 | + } |
| 61 | + HANDLE process = GetCurrentProcess(); |
| 62 | + if (SymInitialize(process, nullptr, TRUE)) { |
| 63 | + SymSetOptions(SymGetOptions() | SYMOPT_LOAD_LINES | SYMOPT_UNDNAME); |
| 64 | + sym_initialized = true; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +static String _native_backtrace_string_windows() { |
| 69 | + _ensure_sym_initialized(); |
| 70 | + |
| 71 | + void *stack[62]; |
| 72 | + const USHORT frame_count = CaptureStackBackTrace(0, 62, stack, nullptr); |
| 73 | + |
| 74 | + String out = " Native C++ backtrace (" + String::num_int64((int64_t)frame_count) + " frames):\n"; |
| 75 | + |
| 76 | + HANDLE process = GetCurrentProcess(); |
| 77 | + for (USHORT i = 0; i < frame_count; i++) { |
| 78 | + const DWORD64 address = reinterpret_cast<DWORD64>(stack[i]); |
| 79 | + if (!sym_initialized) { |
| 80 | + out += " [" + String::num_int64((int64_t)i) + "] 0x" + String::num_uint64((uint64_t)(uintptr_t)stack[i], 16, true) + "\n"; |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + DWORD64 displacement = 0; |
| 85 | + char symbol_buffer[sizeof(IMAGEHLP_SYMBOL64) + 1024]; |
| 86 | + IMAGEHLP_SYMBOL64 *symbol = reinterpret_cast<IMAGEHLP_SYMBOL64 *>(symbol_buffer); |
| 87 | + memset(symbol, 0, sizeof(IMAGEHLP_SYMBOL64)); |
| 88 | + symbol->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL64); |
| 89 | + symbol->MaxNameLength = 1024; |
| 90 | + |
| 91 | + if (SymGetSymFromAddr64(process, address, &displacement, symbol) && symbol->Name[0] != '\0') { |
| 92 | + char und_name[1024]; |
| 93 | + if (UnDecorateSymbolName(symbol->Name, und_name, sizeof(und_name), UNDNAME_COMPLETE) == 0) { |
| 94 | + snprintf(und_name, sizeof(und_name), "%s", symbol->Name); |
| 95 | + } |
| 96 | + DWORD offset_line = 0; |
| 97 | + IMAGEHLP_LINE64 line = {}; |
| 98 | + line.SizeOfStruct = sizeof(IMAGEHLP_LINE64); |
| 99 | + if (SymGetLineFromAddr64(process, address, &offset_line, &line) && line.FileName) { |
| 100 | + out += " [" + String::num_int64((int64_t)i) + "] " + String::utf8(und_name) + " (" + String::utf8(line.FileName) + ":" + String::num_uint64((uint64_t)line.LineNumber) + ")\n"; |
| 101 | + } else { |
| 102 | + out += " [" + String::num_int64((int64_t)i) + "] " + String::utf8(und_name) + "\n"; |
| 103 | + } |
| 104 | + } else { |
| 105 | + out += " [" + String::num_int64((int64_t)i) + "] 0x" + String::num_uint64((uint64_t)(uintptr_t)stack[i], 16, true) + "\n"; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + out += " -- end native C++ backtrace --\n"; |
| 110 | + return out; |
| 111 | +} |
| 112 | + |
| 113 | +#elif defined(__has_include) |
| 114 | + |
| 115 | +#if __has_include(<execinfo.h>) |
| 116 | + |
| 117 | +#include <cxxabi.h> |
| 118 | +#include <dlfcn.h> |
| 119 | +#include <execinfo.h> |
| 120 | + |
| 121 | +#include <cstdlib> |
| 122 | + |
| 123 | +static String _native_backtrace_string_unix() { |
| 124 | + void *frames[64]; |
| 125 | + const int n = backtrace(frames, 64); |
| 126 | + if (n <= 0) { |
| 127 | + return String(" Native C++ backtrace: (unavailable)\n"); |
| 128 | + } |
| 129 | + |
| 130 | + char **symbols = backtrace_symbols(frames, n); |
| 131 | + String out = " Native C++ backtrace (" + String::num_int64((int64_t)n) + " frames):\n"; |
| 132 | + |
| 133 | + for (int i = 0; i < n; i++) { |
| 134 | + char fname[1024] = "???"; |
| 135 | + if (symbols && symbols[i]) { |
| 136 | + snprintf(fname, sizeof(fname), "%s", symbols[i]); |
| 137 | + } |
| 138 | + |
| 139 | + Dl_info info; |
| 140 | + if (dladdr(frames[i], &info) && info.dli_sname && info.dli_sname[0] == '_') { |
| 141 | + int status = 0; |
| 142 | + char *demangled = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status); |
| 143 | + if (status == 0 && demangled) { |
| 144 | + snprintf(fname, sizeof(fname), "%s", demangled); |
| 145 | + } |
| 146 | + if (demangled) { |
| 147 | + free(demangled); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + out += " [" + String::num_int64((int64_t)i) + "] " + String::utf8(fname) + "\n"; |
| 152 | + } |
| 153 | + |
| 154 | + if (symbols) { |
| 155 | + free(symbols); |
| 156 | + } |
| 157 | + |
| 158 | + out += " -- end native C++ backtrace --\n"; |
| 159 | + return out; |
| 160 | +} |
| 161 | + |
| 162 | +#else |
| 163 | + |
| 164 | +static String _native_backtrace_string_stub() { |
| 165 | + return String(" Native C++ backtrace: (execinfo.h not available on this platform)\n"); |
| 166 | +} |
| 167 | + |
| 168 | +#endif |
| 169 | + |
| 170 | +#else |
| 171 | + |
| 172 | +static String _native_backtrace_string_stub() { |
| 173 | + return String(" Native C++ backtrace: (__has_include not supported; rebuild with a C++17 compiler)\n"); |
| 174 | +} |
| 175 | + |
| 176 | +#endif |
| 177 | + |
| 178 | +String backtrace_dump() { |
| 179 | +#ifdef WINDOWS_ENABLED |
| 180 | + return _native_backtrace_string_windows(); |
| 181 | +#elif defined(__has_include) && __has_include(<execinfo.h>) |
| 182 | + return _native_backtrace_string_unix(); |
| 183 | +#else |
| 184 | + return _native_backtrace_string_stub(); |
| 185 | +#endif |
| 186 | +} |
| 187 | + |
| 188 | +#else |
| 189 | + |
| 190 | +String backtrace_dump() { |
| 191 | + return String(); |
| 192 | +} |
| 193 | + |
| 194 | +#endif |
0 commit comments