| 1 | /**************************************************************************/ |
| 2 | /* crash_handler_windows_seh.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 "crash_handler_windows.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/object/script_language.h" |
| 35 | #include "core/os/main_loop.h" |
| 36 | #include "core/os/os.h" |
| 37 | #include "core/string/print_string.h" |
| 38 | #include "core/version.h" |
| 39 | #include "main/main.h" |
| 40 | |
| 41 | #ifdef CRASH_HANDLER_EXCEPTION |
| 42 | |
| 43 | // Backtrace code based on: https://stackoverflow.com/questions/6205981/windows-c-stack-trace-from-a-running-app |
| 44 | |
| 45 | #include <algorithm> |
| 46 | #include <cstdlib> |
| 47 | #include <iterator> |
| 48 | #include <string> |
| 49 | #include <vector> |
| 50 | |
| 51 | #include <psapi.h> |
| 52 | |
| 53 | // Some versions of imagehlp.dll lack the proper packing directives themselves |
| 54 | // so we need to do it. |
| 55 | #pragma pack(push, before_imagehlp, 8) |
| 56 | #include <imagehlp.h> |
| 57 | #pragma pack(pop, before_imagehlp) |
| 58 | |
| 59 | struct module_data { |
| 60 | std::string image_name; |
| 61 | std::string module_name; |
| 62 | void *base_address = nullptr; |
| 63 | DWORD load_size; |
| 64 | }; |
| 65 | |
| 66 | class symbol { |
| 67 | typedef IMAGEHLP_SYMBOL64 sym_type; |
| 68 | sym_type *sym; |
| 69 | static const int max_name_len = 1024; |
| 70 | |
| 71 | public: |
| 72 | symbol(HANDLE process, DWORD64 address) : |
| 73 | sym((sym_type *)::operator new(sizeof(*sym) + max_name_len)) { |
| 74 | memset(sym, '\0', sizeof(*sym) + max_name_len); |
| 75 | sym->SizeOfStruct = sizeof(*sym); |
| 76 | sym->MaxNameLength = max_name_len; |
| 77 | DWORD64 displacement; |
| 78 | |
| 79 | SymGetSymFromAddr64(process, address, &displacement, sym); |
| 80 | } |
| 81 | |
| 82 | std::string name() { return std::string(sym->Name); } |
| 83 | std::string undecorated_name() { |
| 84 | if (*sym->Name == '\0') { |
| 85 | return "<couldn't map PC to fn name>"; |
| 86 | } |
| 87 | std::vector<char> und_name(max_name_len); |
| 88 | UnDecorateSymbolName(sym->Name, &und_name[0], max_name_len, UNDNAME_COMPLETE); |
| 89 | return std::string(&und_name[0], strlen(&und_name[0])); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | class get_mod_info { |
| 94 | HANDLE process; |
| 95 | |
| 96 | public: |
| 97 | get_mod_info(HANDLE h) : |
| 98 | process(h) {} |
| 99 | |
| 100 | module_data operator()(HMODULE module) { |
| 101 | module_data ret; |
| 102 | char temp[4096]; |
| 103 | MODULEINFO mi; |
| 104 | |
| 105 | GetModuleInformation(process, module, &mi, sizeof(mi)); |
| 106 | ret.base_address = mi.lpBaseOfDll; |
| 107 | ret.load_size = mi.SizeOfImage; |
| 108 | |
| 109 | GetModuleFileNameEx(process, module, temp, sizeof(temp)); |
| 110 | ret.image_name = temp; |
| 111 | GetModuleBaseName(process, module, temp, sizeof(temp)); |
| 112 | ret.module_name = temp; |
| 113 | std::vector<char> img(ret.image_name.begin(), ret.image_name.end()); |
| 114 | std::vector<char> mod(ret.module_name.begin(), ret.module_name.end()); |
| 115 | SymLoadModule64(process, nullptr, &img[0], &mod[0], (DWORD64)ret.base_address, ret.load_size); |
| 116 | return ret; |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | DWORD CrashHandlerException(EXCEPTION_POINTERS *ep) { |
| 121 | HANDLE process = GetCurrentProcess(); |
| 122 | HANDLE hThread = GetCurrentThread(); |
| 123 | DWORD offset_from_symbol = 0; |
| 124 | IMAGEHLP_LINE64 line = {}; |
| 125 | std::vector<module_data> modules; |
| 126 | DWORD cbNeeded; |
| 127 | std::vector<HMODULE> module_handles(1); |
| 128 | |
| 129 | if (OS::get_singleton() == nullptr || OS::get_singleton()->is_disable_crash_handler() || IsDebuggerPresent()) { |
| 130 | return EXCEPTION_CONTINUE_SEARCH; |
| 131 | } |
| 132 | |
| 133 | if (OS::get_singleton()->is_crash_handler_silent()) { |
| 134 | std::_Exit(0); |
| 135 | } |
| 136 | |
| 137 | String msg; |
| 138 | if (ProjectSettings::get_singleton()) { |
| 139 | msg = GLOBAL_GET("debug/settings/crash_handler/message"); |
| 140 | } |
| 141 | |
| 142 | // Tell MainLoop about the crash. This can be handled by users too in Node. |
| 143 | if (OS::get_singleton()->get_main_loop()) { |
| 144 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH); |
| 145 | } |
| 146 | |
| 147 | print_error("\n================================================================"); |
| 148 | print_error(vformat("%s: Program crashed", __FUNCTION__)); |
| 149 | |
| 150 | // Print the engine version just before, so that people are reminded to include the version in backtrace reports. |
| 151 | if (String(GODOT_VERSION_HASH).is_empty()) { |
| 152 | print_error(vformat("Engine version: %s", GODOT_VERSION_FULL_NAME)); |
| 153 | } else { |
| 154 | print_error(vformat("Engine version: %s (%s)", GODOT_VERSION_FULL_NAME, GODOT_VERSION_HASH)); |
| 155 | } |
| 156 | print_error(vformat("Dumping the backtrace. %s", msg)); |
| 157 | |
| 158 | // Load the symbols: |
| 159 | if (!SymInitialize(process, nullptr, false)) { |
| 160 | return EXCEPTION_CONTINUE_SEARCH; |
| 161 | } |
| 162 | |
| 163 | SymSetOptions(SymGetOptions() | SYMOPT_LOAD_LINES | SYMOPT_UNDNAME | SYMOPT_EXACT_SYMBOLS); |
| 164 | EnumProcessModules(process, &module_handles[0], module_handles.size() * sizeof(HMODULE), &cbNeeded); |
| 165 | module_handles.resize(cbNeeded / sizeof(HMODULE)); |
| 166 | EnumProcessModules(process, &module_handles[0], module_handles.size() * sizeof(HMODULE), &cbNeeded); |
| 167 | std::transform(module_handles.begin(), module_handles.end(), std::back_inserter(modules), get_mod_info(process)); |
| 168 | void *base = modules[0].base_address; |
| 169 | |
| 170 | // Setup stuff: |
| 171 | CONTEXT *context = ep->ContextRecord; |
| 172 | STACKFRAME64 frame; |
| 173 | bool skip_first = false; |
| 174 | |
| 175 | frame.AddrPC.Mode = AddrModeFlat; |
| 176 | frame.AddrStack.Mode = AddrModeFlat; |
| 177 | frame.AddrFrame.Mode = AddrModeFlat; |
| 178 | |
| 179 | #if defined(_M_X64) |
| 180 | frame.AddrPC.Offset = context->Rip; |
| 181 | frame.AddrStack.Offset = context->Rsp; |
| 182 | frame.AddrFrame.Offset = context->Rbp; |
| 183 | #elif defined(_M_ARM64) || defined(_M_ARM64EC) |
| 184 | frame.AddrPC.Offset = context->Pc; |
| 185 | frame.AddrStack.Offset = context->Sp; |
| 186 | frame.AddrFrame.Offset = context->Fp; |
| 187 | #elif defined(_M_ARM) |
| 188 | frame.AddrPC.Offset = context->Pc; |
| 189 | frame.AddrStack.Offset = context->Sp; |
| 190 | frame.AddrFrame.Offset = context->R11; |
| 191 | #else |
| 192 | frame.AddrPC.Offset = context->Eip; |
| 193 | frame.AddrStack.Offset = context->Esp; |
| 194 | frame.AddrFrame.Offset = context->Ebp; |
| 195 | |
| 196 | // Skip the first one to avoid a duplicate on 32-bit mode |
| 197 | skip_first = true; |
| 198 | #endif |
| 199 | |
| 200 | line.SizeOfStruct = sizeof(line); |
| 201 | IMAGE_NT_HEADERS *h = ImageNtHeader(base); |
| 202 | DWORD image_type = h->FileHeader.Machine; |
| 203 | |
| 204 | int n = 0; |
| 205 | do { |
| 206 | if (skip_first) { |
| 207 | skip_first = false; |
| 208 | } else { |
| 209 | if (frame.AddrPC.Offset != 0) { |
| 210 | std::string fnName = symbol(process, frame.AddrPC.Offset).undecorated_name(); |
| 211 | |
| 212 | if (SymGetLineFromAddr64(process, frame.AddrPC.Offset, &offset_from_symbol, &line)) { |
| 213 | print_error(vformat("[%d] %s (%s:%d)", n, fnName.c_str(), (char *)line.FileName, (int)line.LineNumber)); |
| 214 | } else { |
| 215 | print_error(vformat("[%d] %s", n, fnName.c_str())); |
| 216 | } |
| 217 | } else { |
| 218 | print_error(vformat("[%d] ???", n)); |
| 219 | } |
| 220 | |
| 221 | n++; |
| 222 | } |
| 223 | |
| 224 | if (!StackWalk64(image_type, process, hThread, &frame, context, nullptr, SymFunctionTableAccess64, SymGetModuleBase64, nullptr)) { |
| 225 | break; |
| 226 | } |
| 227 | } while (frame.AddrReturn.Offset != 0 && n < 256); |
| 228 | |
| 229 | print_error("-- END OF C++ BACKTRACE --"); |
| 230 | print_error("================================================================"); |
| 231 | |
| 232 | SymCleanup(process); |
| 233 | |
| 234 | for (const Ref<ScriptBacktrace> &backtrace : ScriptServer::capture_script_backtraces(false)) { |
| 235 | if (!backtrace->is_empty()) { |
| 236 | print_error(backtrace->format()); |
| 237 | print_error(vformat("-- END OF %s BACKTRACE --", backtrace->get_language_name().to_upper())); |
| 238 | print_error("================================================================"); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | // Pass the exception to the OS |
| 243 | return EXCEPTION_CONTINUE_SEARCH; |
| 244 | } |
| 245 | #endif |
| 246 | |
| 247 | CrashHandler::CrashHandler() { |
| 248 | disabled = false; |
| 249 | } |
| 250 | |
| 251 | CrashHandler::~CrashHandler() { |
| 252 | } |
| 253 | |
| 254 | void CrashHandler::disable() { |
| 255 | if (disabled) { |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | disabled = true; |
| 260 | } |
| 261 | |
| 262 | void CrashHandler::initialize() { |
| 263 | } |