crash_handler_linuxbsd.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*************************************************************************/
  2. /* crash_handler_linuxbsd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/os/os.h"
  33. #include "core/version.h"
  34. #include "main/main.h"
  35. #ifdef DEBUG_ENABLED
  36. #define CRASH_HANDLER_ENABLED 1
  37. #endif
  38. #ifdef CRASH_HANDLER_ENABLED
  39. #include <cxxabi.h>
  40. #include <dlfcn.h>
  41. #include <execinfo.h>
  42. #include <signal.h>
  43. #include <stdlib.h>
  44. static void handle_crash(int sig) {
  45. if (OS::get_singleton() == nullptr) {
  46. abort();
  47. }
  48. void *bt_buffer[256];
  49. size_t size = backtrace(bt_buffer, 256);
  50. String _execpath = OS::get_singleton()->get_executable_path();
  51. String msg;
  52. const ProjectSettings *proj_settings = ProjectSettings::get_singleton();
  53. if (proj_settings) {
  54. msg = proj_settings->get("debug/settings/crash_handler/message");
  55. }
  56. // Dump the backtrace to stderr with a message to the user
  57. fprintf(stderr, "\n================================================================\n");
  58. fprintf(stderr, "%s: Program crashed with signal %d\n", __FUNCTION__, sig);
  59. if (OS::get_singleton()->get_main_loop()) {
  60. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
  61. }
  62. // Print the engine version just before, so that people are reminded to include the version in backtrace reports.
  63. if (String(VERSION_HASH).is_empty()) {
  64. fprintf(stderr, "Engine version: %s\n", VERSION_FULL_NAME);
  65. } else {
  66. fprintf(stderr, "Engine version: %s (%s)\n", VERSION_FULL_NAME, VERSION_HASH);
  67. }
  68. fprintf(stderr, "Dumping the backtrace. %s\n", msg.utf8().get_data());
  69. char **strings = backtrace_symbols(bt_buffer, size);
  70. if (strings) {
  71. for (size_t i = 1; i < size; i++) {
  72. char fname[1024];
  73. Dl_info info;
  74. snprintf(fname, 1024, "%s", strings[i]);
  75. // Try to demangle the function name to provide a more readable one
  76. if (dladdr(bt_buffer[i], &info) && info.dli_sname) {
  77. if (info.dli_sname[0] == '_') {
  78. int status;
  79. char *demangled = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status);
  80. if (status == 0 && demangled) {
  81. snprintf(fname, 1024, "%s", demangled);
  82. }
  83. if (demangled) {
  84. free(demangled);
  85. }
  86. }
  87. }
  88. List<String> args;
  89. char str[1024];
  90. snprintf(str, 1024, "%p", bt_buffer[i]);
  91. args.push_back(str);
  92. args.push_back("-e");
  93. args.push_back(_execpath);
  94. String output = "";
  95. // Try to get the file/line number using addr2line
  96. int ret;
  97. Error err = OS::get_singleton()->execute(String("addr2line"), args, &output, &ret);
  98. if (err == OK) {
  99. output = output.substr(0, output.length() - 1);
  100. }
  101. fprintf(stderr, "[%ld] %s (%s)\n", (long int)i, fname, output.utf8().get_data());
  102. }
  103. free(strings);
  104. }
  105. fprintf(stderr, "-- END OF BACKTRACE --\n");
  106. fprintf(stderr, "================================================================\n");
  107. // Abort to pass the error to the OS
  108. abort();
  109. }
  110. #endif
  111. CrashHandler::CrashHandler() {
  112. disabled = false;
  113. }
  114. CrashHandler::~CrashHandler() {
  115. disable();
  116. }
  117. void CrashHandler::disable() {
  118. if (disabled) {
  119. return;
  120. }
  121. #ifdef CRASH_HANDLER_ENABLED
  122. signal(SIGSEGV, nullptr);
  123. signal(SIGFPE, nullptr);
  124. signal(SIGILL, nullptr);
  125. #endif
  126. disabled = true;
  127. }
  128. void CrashHandler::initialize() {
  129. #ifdef CRASH_HANDLER_ENABLED
  130. signal(SIGSEGV, handle_crash);
  131. signal(SIGFPE, handle_crash);
  132. signal(SIGILL, handle_crash);
  133. #endif
  134. }