crash_handler_x11.cpp 5.4 KB

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