crash_handler_linuxbsd.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/os/os.h"
  32. #include "core/project_settings.h"
  33. #include "main/main.h"
  34. #ifdef DEBUG_ENABLED
  35. #define CRASH_HANDLER_ENABLED 1
  36. #endif
  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. if (OS::get_singleton()->get_main_loop())
  58. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
  59. fprintf(stderr, "Dumping the backtrace. %ls\n", msg.c_str());
  60. char **strings = backtrace_symbols(bt_buffer, size);
  61. if (strings) {
  62. for (size_t i = 1; i < size; i++) {
  63. char fname[1024];
  64. Dl_info info;
  65. snprintf(fname, 1024, "%s", strings[i]);
  66. // Try to demangle the function name to provide a more readable one
  67. if (dladdr(bt_buffer[i], &info) && info.dli_sname) {
  68. if (info.dli_sname[0] == '_') {
  69. int status;
  70. char *demangled = abi::__cxa_demangle(info.dli_sname, NULL, 0, &status);
  71. if (status == 0 && demangled) {
  72. snprintf(fname, 1024, "%s", demangled);
  73. }
  74. if (demangled)
  75. free(demangled);
  76. }
  77. }
  78. List<String> args;
  79. char str[1024];
  80. snprintf(str, 1024, "%p", bt_buffer[i]);
  81. args.push_back(str);
  82. args.push_back("-e");
  83. args.push_back(_execpath);
  84. String output = "";
  85. // Try to get the file/line number using addr2line
  86. int ret;
  87. Error err = OS::get_singleton()->execute(String("addr2line"), args, true, NULL, &output, &ret);
  88. if (err == OK) {
  89. output.erase(output.length() - 1, 1);
  90. }
  91. fprintf(stderr, "[%ld] %s (%ls)\n", (long int)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. }