crash_handler_x11.cpp 4.7 KB

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