crash_handler_windows_signal.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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/os/os.h"
  33. #include "core/string/print_string.h"
  34. #include "core/version.h"
  35. #include "main/main.h"
  36. #ifdef CRASH_HANDLER_EXCEPTION
  37. #include <cxxabi.h>
  38. #include <signal.h>
  39. #include <algorithm>
  40. #include <cstdlib>
  41. #include <iterator>
  42. #include <string>
  43. #include <vector>
  44. #include <psapi.h>
  45. #include "thirdparty/libbacktrace/backtrace.h"
  46. struct CrashHandlerData {
  47. int64_t index = 0;
  48. backtrace_state *state = nullptr;
  49. int64_t offset = 0;
  50. };
  51. int symbol_callback(void *data, uintptr_t pc, const char *filename, int lineno, const char *function) {
  52. CrashHandlerData *ch_data = reinterpret_cast<CrashHandlerData *>(data);
  53. if (!function) {
  54. return 0;
  55. }
  56. char fname[1024];
  57. snprintf(fname, 1024, "%s", function);
  58. if (function[0] == '_') {
  59. int status;
  60. char *demangled = abi::__cxa_demangle(function, nullptr, nullptr, &status);
  61. if (status == 0 && demangled) {
  62. snprintf(fname, 1024, "%s", demangled);
  63. }
  64. if (demangled) {
  65. free(demangled);
  66. }
  67. }
  68. print_error(vformat("[%d] %s (%s:%d)", ch_data->index++, String::utf8(fname), String::utf8(filename), lineno));
  69. return 0;
  70. }
  71. void error_callback(void *data, const char *msg, int errnum) {
  72. CrashHandlerData *ch_data = reinterpret_cast<CrashHandlerData *>(data);
  73. if (ch_data->index == 0) {
  74. print_error(vformat("Error(%d): %s", errnum, String::utf8(msg)));
  75. } else {
  76. print_error(vformat("[%d] error(%d): %s", ch_data->index++, errnum, String::utf8(msg)));
  77. }
  78. }
  79. int trace_callback(void *data, uintptr_t pc) {
  80. CrashHandlerData *ch_data = reinterpret_cast<CrashHandlerData *>(data);
  81. backtrace_pcinfo(ch_data->state, pc - ch_data->offset, &symbol_callback, &error_callback, data);
  82. return 0;
  83. }
  84. int64_t get_image_base(const String &p_path) {
  85. Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
  86. if (f.is_null()) {
  87. return 0;
  88. }
  89. {
  90. f->seek(0x3c);
  91. uint32_t pe_pos = f->get_32();
  92. f->seek(pe_pos);
  93. uint32_t magic = f->get_32();
  94. if (magic != 0x00004550) {
  95. return 0;
  96. }
  97. }
  98. int64_t opt_header_pos = f->get_position() + 0x14;
  99. f->seek(opt_header_pos);
  100. uint16_t opt_header_magic = f->get_16();
  101. if (opt_header_magic == 0x10B) {
  102. f->seek(opt_header_pos + 0x1C);
  103. return f->get_32();
  104. } else if (opt_header_magic == 0x20B) {
  105. f->seek(opt_header_pos + 0x18);
  106. return f->get_64();
  107. } else {
  108. return 0;
  109. }
  110. }
  111. extern void CrashHandlerException(int signal) {
  112. CrashHandlerData data;
  113. if (OS::get_singleton() == nullptr || OS::get_singleton()->is_disable_crash_handler() || IsDebuggerPresent()) {
  114. return;
  115. }
  116. if (OS::get_singleton()->is_crash_handler_silent()) {
  117. std::_Exit(0);
  118. }
  119. String msg;
  120. const ProjectSettings *proj_settings = ProjectSettings::get_singleton();
  121. if (proj_settings) {
  122. msg = proj_settings->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("/", "\\");
  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 BACKTRACE --");
  155. print_error("================================================================");
  156. }
  157. #endif
  158. CrashHandler::CrashHandler() {
  159. disabled = false;
  160. }
  161. CrashHandler::~CrashHandler() {
  162. }
  163. void CrashHandler::disable() {
  164. if (disabled) {
  165. return;
  166. }
  167. #if defined(CRASH_HANDLER_EXCEPTION)
  168. signal(SIGSEGV, nullptr);
  169. signal(SIGFPE, nullptr);
  170. signal(SIGILL, nullptr);
  171. #endif
  172. disabled = true;
  173. }
  174. void CrashHandler::initialize() {
  175. #if defined(CRASH_HANDLER_EXCEPTION)
  176. signal(SIGSEGV, CrashHandlerException);
  177. signal(SIGFPE, CrashHandlerException);
  178. signal(SIGILL, CrashHandlerException);
  179. #endif
  180. }