crash_handler_x11.cpp 4.7 KB

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