crash_handler_windows_signal.cpp 7.1 KB

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