crash_handler_windows_signal.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**************************************************************************/
  2. /* crash_handler_windows_signal.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. #include "crash_handler_windows.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/object/script_language.h"
  33. #include "core/os/os.h"
  34. #include "core/string/print_string.h"
  35. #include "core/version.h"
  36. #include "main/main.h"
  37. #ifdef CRASH_HANDLER_EXCEPTION
  38. #include <cxxabi.h>
  39. #include <algorithm>
  40. #include <csignal>
  41. #include <cstdlib>
  42. #include <iterator>
  43. #include <string>
  44. #include <vector>
  45. #include <psapi.h>
  46. #include "thirdparty/libbacktrace/backtrace.h"
  47. struct CrashHandlerData {
  48. int64_t index = 0;
  49. backtrace_state *state = nullptr;
  50. int64_t offset = 0;
  51. };
  52. int symbol_callback(void *data, uintptr_t pc, const char *filename, int lineno, const char *function) {
  53. CrashHandlerData *ch_data = reinterpret_cast<CrashHandlerData *>(data);
  54. if (!function) {
  55. return 0;
  56. }
  57. char fname[1024];
  58. snprintf(fname, 1024, "%s", function);
  59. if (function[0] == '_') {
  60. int status;
  61. char *demangled = abi::__cxa_demangle(function, nullptr, nullptr, &status);
  62. if (status == 0 && demangled) {
  63. snprintf(fname, 1024, "%s", demangled);
  64. }
  65. if (demangled) {
  66. free(demangled);
  67. }
  68. }
  69. print_error(vformat("[%d] %s (%s:%d)", ch_data->index++, String::utf8(fname), String::utf8(filename), lineno));
  70. return 0;
  71. }
  72. void error_callback(void *data, const char *msg, int errnum) {
  73. CrashHandlerData *ch_data = reinterpret_cast<CrashHandlerData *>(data);
  74. if (ch_data->index == 0) {
  75. print_error(vformat("Error(%d): %s", errnum, String::utf8(msg)));
  76. } else {
  77. print_error(vformat("[%d] error(%d): %s", ch_data->index++, errnum, String::utf8(msg)));
  78. }
  79. }
  80. int trace_callback(void *data, uintptr_t pc) {
  81. CrashHandlerData *ch_data = reinterpret_cast<CrashHandlerData *>(data);
  82. backtrace_pcinfo(ch_data->state, pc - ch_data->offset, &symbol_callback, &error_callback, data);
  83. return 0;
  84. }
  85. int64_t get_image_base(const String &p_path) {
  86. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  87. if (f.is_null()) {
  88. return 0;
  89. }
  90. {
  91. f->seek(0x3c);
  92. uint32_t pe_pos = f->get_32();
  93. f->seek(pe_pos);
  94. uint32_t magic = f->get_32();
  95. if (magic != 0x00004550) {
  96. return 0;
  97. }
  98. }
  99. int64_t opt_header_pos = f->get_position() + 0x14;
  100. f->seek(opt_header_pos);
  101. uint16_t opt_header_magic = f->get_16();
  102. if (opt_header_magic == 0x10B) {
  103. f->seek(opt_header_pos + 0x1C);
  104. return f->get_32();
  105. } else if (opt_header_magic == 0x20B) {
  106. f->seek(opt_header_pos + 0x18);
  107. return f->get_64();
  108. } else {
  109. return 0;
  110. }
  111. }
  112. extern void CrashHandlerException(int signal) {
  113. CrashHandlerData data;
  114. if (OS::get_singleton() == nullptr || OS::get_singleton()->is_disable_crash_handler() || IsDebuggerPresent()) {
  115. return;
  116. }
  117. if (OS::get_singleton()->is_crash_handler_silent()) {
  118. std::_Exit(0);
  119. }
  120. String msg;
  121. if (ProjectSettings::get_singleton()) {
  122. msg = GLOBAL_GET("debug/settings/crash_handler/message");
  123. }
  124. // Tell MainLoop about the crash. This can be handled by users too in Node.
  125. if (OS::get_singleton()->get_main_loop()) {
  126. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
  127. }
  128. print_error("\n================================================================");
  129. print_error(vformat("%s: Program crashed with signal %d", __FUNCTION__, signal));
  130. // Print the engine version just before, so that people are reminded to include the version in backtrace reports.
  131. if (String(GODOT_VERSION_HASH).is_empty()) {
  132. print_error(vformat("Engine version: %s", GODOT_VERSION_FULL_NAME));
  133. } else {
  134. print_error(vformat("Engine version: %s (%s)", GODOT_VERSION_FULL_NAME, GODOT_VERSION_HASH));
  135. }
  136. print_error(vformat("Dumping the backtrace. %s", msg));
  137. String _execpath = OS::get_singleton()->get_executable_path();
  138. // Load process and image info to determine ASLR addresses offset.
  139. MODULEINFO mi;
  140. GetModuleInformation(GetCurrentProcess(), GetModuleHandle(nullptr), &mi, sizeof(mi));
  141. int64_t image_mem_base = reinterpret_cast<int64_t>(mi.lpBaseOfDll);
  142. int64_t image_file_base = get_image_base(_execpath);
  143. data.offset = image_mem_base - image_file_base;
  144. if (FileAccess::exists(_execpath + ".debugsymbols")) {
  145. _execpath = _execpath + ".debugsymbols";
  146. }
  147. _execpath = _execpath.replace_char('/', '\\');
  148. CharString cs = _execpath.utf8(); // Note: should remain in scope during backtrace_simple call.
  149. data.state = backtrace_create_state(cs.get_data(), 0, &error_callback, reinterpret_cast<void *>(&data));
  150. if (data.state != nullptr) {
  151. data.index = 1;
  152. backtrace_simple(data.state, 1, &trace_callback, &error_callback, reinterpret_cast<void *>(&data));
  153. }
  154. print_error("-- END OF C++ BACKTRACE --");
  155. print_error("================================================================");
  156. for (const Ref<ScriptBacktrace> &backtrace : ScriptServer::capture_script_backtraces(false)) {
  157. if (!backtrace->is_empty()) {
  158. print_error(backtrace->format());
  159. print_error(vformat("-- END OF %s BACKTRACE --", backtrace->get_language_name().to_upper()));
  160. print_error("================================================================");
  161. }
  162. }
  163. }
  164. #endif
  165. CrashHandler::CrashHandler() {
  166. disabled = false;
  167. }
  168. CrashHandler::~CrashHandler() {
  169. }
  170. void CrashHandler::disable() {
  171. if (disabled) {
  172. return;
  173. }
  174. #if defined(CRASH_HANDLER_EXCEPTION)
  175. signal(SIGSEGV, nullptr);
  176. signal(SIGFPE, nullptr);
  177. signal(SIGILL, nullptr);
  178. #endif
  179. disabled = true;
  180. }
  181. void CrashHandler::initialize() {
  182. #if defined(CRASH_HANDLER_EXCEPTION)
  183. signal(SIGSEGV, CrashHandlerException);
  184. signal(SIGFPE, CrashHandlerException);
  185. signal(SIGILL, CrashHandlerException);
  186. #endif
  187. }