crash_handler_linuxbsd.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**************************************************************************/
  2. /* crash_handler_linuxbsd.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_linuxbsd.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. #ifndef DEBUG_ENABLED
  38. #undef CRASH_HANDLER_ENABLED
  39. #endif
  40. #ifdef CRASH_HANDLER_ENABLED
  41. #include <cxxabi.h>
  42. #include <dlfcn.h>
  43. #include <execinfo.h>
  44. #include <link.h>
  45. #include <csignal>
  46. #include <cstdlib>
  47. static void handle_crash(int sig) {
  48. signal(SIGSEGV, SIG_DFL);
  49. signal(SIGFPE, SIG_DFL);
  50. signal(SIGILL, SIG_DFL);
  51. if (OS::get_singleton() == nullptr) {
  52. abort();
  53. }
  54. if (OS::get_singleton()->is_crash_handler_silent()) {
  55. std::_Exit(0);
  56. }
  57. void *bt_buffer[256];
  58. size_t size = backtrace(bt_buffer, 256);
  59. String _execpath = OS::get_singleton()->get_executable_path();
  60. String msg;
  61. const ProjectSettings *proj_settings = ProjectSettings::get_singleton();
  62. if (proj_settings) {
  63. msg = proj_settings->get("debug/settings/crash_handler/message");
  64. }
  65. // Tell MainLoop about the crash. This can be handled by users too in Node.
  66. if (OS::get_singleton()->get_main_loop()) {
  67. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
  68. }
  69. // Dump the backtrace to stderr with a message to the user
  70. print_error("\n================================================================");
  71. print_error(vformat("%s: Program crashed with signal %d", __FUNCTION__, sig));
  72. // Print the engine version just before, so that people are reminded to include the version in backtrace reports.
  73. if (String(GODOT_VERSION_HASH).is_empty()) {
  74. print_error(vformat("Engine version: %s", GODOT_VERSION_FULL_NAME));
  75. } else {
  76. print_error(vformat("Engine version: %s (%s)", GODOT_VERSION_FULL_NAME, GODOT_VERSION_HASH));
  77. }
  78. print_error(vformat("Dumping the backtrace. %s", msg));
  79. char **strings = backtrace_symbols(bt_buffer, size);
  80. // PIE executable relocation, zero for non-PIE executables
  81. #ifdef __GLIBC__
  82. // This is a glibc only thing apparently.
  83. uintptr_t relocation = _r_debug.r_map->l_addr;
  84. #else
  85. // Non glibc systems apparently don't give PIE relocation info.
  86. uintptr_t relocation = 0;
  87. #endif //__GLIBC__
  88. if (strings) {
  89. List<String> args;
  90. for (size_t i = 0; i < size; i++) {
  91. char str[1024];
  92. snprintf(str, 1024, "%p", (void *)((uintptr_t)bt_buffer[i] - relocation));
  93. args.push_back(str);
  94. }
  95. args.push_back("-e");
  96. args.push_back(_execpath);
  97. // Try to get the file/line number using addr2line
  98. int ret;
  99. String output = "";
  100. Error err = OS::get_singleton()->execute(String("addr2line"), args, &output, &ret);
  101. Vector<String> addr2line_results;
  102. if (err == OK) {
  103. addr2line_results = output.substr(0, output.length() - 1).split("\n", false);
  104. }
  105. for (size_t i = 1; i < size; i++) {
  106. char fname[1024];
  107. Dl_info info;
  108. snprintf(fname, 1024, "%s", strings[i]);
  109. // Try to demangle the function name to provide a more readable one
  110. if (dladdr(bt_buffer[i], &info) && info.dli_sname) {
  111. if (info.dli_sname[0] == '_') {
  112. int status = 0;
  113. char *demangled = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status);
  114. if (status == 0 && demangled) {
  115. snprintf(fname, 1024, "%s", demangled);
  116. }
  117. if (demangled) {
  118. free(demangled);
  119. }
  120. }
  121. }
  122. // Simplify printed file paths to remove redundant `/./` sections (e.g. `/opt/godot/./core` -> `/opt/godot/core`).
  123. print_error(vformat("[%d] %s (%s)", (int64_t)i, fname, err == OK ? addr2line_results[i].replace("/./", "/") : ""));
  124. }
  125. free(strings);
  126. }
  127. print_error("-- END OF BACKTRACE --");
  128. print_error("================================================================");
  129. Vector<Ref<ScriptBacktrace>> script_backtraces;
  130. if (ScriptServer::are_languages_initialized()) {
  131. script_backtraces = ScriptServer::capture_script_backtraces(false);
  132. }
  133. if (!script_backtraces.is_empty()) {
  134. for (const Ref<ScriptBacktrace> &backtrace : script_backtraces) {
  135. print_error(backtrace->format());
  136. }
  137. print_error("-- END OF SCRIPT BACKTRACE --");
  138. print_error("================================================================");
  139. }
  140. // Abort to pass the error to the OS
  141. abort();
  142. }
  143. #endif
  144. CrashHandler::CrashHandler() {
  145. disabled = false;
  146. }
  147. CrashHandler::~CrashHandler() {
  148. disable();
  149. }
  150. void CrashHandler::disable() {
  151. if (disabled) {
  152. return;
  153. }
  154. #ifdef CRASH_HANDLER_ENABLED
  155. signal(SIGSEGV, SIG_DFL);
  156. signal(SIGFPE, SIG_DFL);
  157. signal(SIGILL, SIG_DFL);
  158. #endif
  159. disabled = true;
  160. }
  161. void CrashHandler::initialize() {
  162. #ifdef CRASH_HANDLER_ENABLED
  163. signal(SIGSEGV, handle_crash);
  164. signal(SIGFPE, handle_crash);
  165. signal(SIGILL, handle_crash);
  166. #endif
  167. }