crash_handler_windows_signal.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. const ProjectSettings *proj_settings = ProjectSettings::get_singleton();
  122. if (proj_settings) {
  123. msg = proj_settings->get("debug/settings/crash_handler/message");
  124. }
  125. // Tell MainLoop about the crash. This can be handled by users too in Node.
  126. if (OS::get_singleton()->get_main_loop()) {
  127. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
  128. }
  129. print_error("\n================================================================");
  130. print_error(vformat("%s: Program crashed with signal %d", __FUNCTION__, signal));
  131. // Print the engine version just before, so that people are reminded to include the version in backtrace reports.
  132. if (String(GODOT_VERSION_HASH).is_empty()) {
  133. print_error(vformat("Engine version: %s", GODOT_VERSION_FULL_NAME));
  134. } else {
  135. print_error(vformat("Engine version: %s (%s)", GODOT_VERSION_FULL_NAME, GODOT_VERSION_HASH));
  136. }
  137. print_error(vformat("Dumping the backtrace. %s", msg));
  138. String _execpath = OS::get_singleton()->get_executable_path();
  139. // Load process and image info to determine ASLR addresses offset.
  140. MODULEINFO mi;
  141. GetModuleInformation(GetCurrentProcess(), GetModuleHandle(nullptr), &mi, sizeof(mi));
  142. int64_t image_mem_base = reinterpret_cast<int64_t>(mi.lpBaseOfDll);
  143. int64_t image_file_base = get_image_base(_execpath);
  144. data.offset = image_mem_base - image_file_base;
  145. if (FileAccess::exists(_execpath + ".debugsymbols")) {
  146. _execpath = _execpath + ".debugsymbols";
  147. }
  148. _execpath = _execpath.replace_char('/', '\\');
  149. CharString cs = _execpath.utf8(); // Note: should remain in scope during backtrace_simple call.
  150. data.state = backtrace_create_state(cs.get_data(), 0, &error_callback, reinterpret_cast<void *>(&data));
  151. if (data.state != nullptr) {
  152. data.index = 1;
  153. backtrace_simple(data.state, 1, &trace_callback, &error_callback, reinterpret_cast<void *>(&data));
  154. }
  155. print_error("-- END OF BACKTRACE --");
  156. print_error("================================================================");
  157. Vector<Ref<ScriptBacktrace>> script_backtraces;
  158. if (ScriptServer::are_languages_initialized()) {
  159. script_backtraces = ScriptServer::capture_script_backtraces(false);
  160. }
  161. if (!script_backtraces.is_empty()) {
  162. for (const Ref<ScriptBacktrace> &backtrace : script_backtraces) {
  163. if (!backtrace->is_empty()) {
  164. print_error(backtrace->format());
  165. }
  166. }
  167. print_error("-- END OF SCRIPT BACKTRACE --");
  168. print_error("================================================================");
  169. }
  170. }
  171. #endif
  172. CrashHandler::CrashHandler() {
  173. disabled = false;
  174. }
  175. CrashHandler::~CrashHandler() {
  176. }
  177. void CrashHandler::disable() {
  178. if (disabled) {
  179. return;
  180. }
  181. #if defined(CRASH_HANDLER_EXCEPTION)
  182. signal(SIGSEGV, nullptr);
  183. signal(SIGFPE, nullptr);
  184. signal(SIGILL, nullptr);
  185. #endif
  186. disabled = true;
  187. }
  188. void CrashHandler::initialize() {
  189. #if defined(CRASH_HANDLER_EXCEPTION)
  190. signal(SIGSEGV, CrashHandlerException);
  191. signal(SIGFPE, CrashHandlerException);
  192. signal(SIGILL, CrashHandlerException);
  193. #endif
  194. }