crash_handler_osx.mm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*************************************************************************/
  2. /* crash_handler_osx.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "main/main.h"
  31. #include "os_osx.h"
  32. #include "project_settings.h"
  33. #include <string.h>
  34. #include <unistd.h>
  35. // Note: Dump backtrace in 32bit mode is getting a bus error on the fgets by the ->execute, so enable only on 64bit
  36. #if defined(DEBUG_ENABLED) && defined(__x86_64__)
  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. #include <mach-o/dyld.h>
  46. #include <mach-o/getsect.h>
  47. #ifdef __x86_64__
  48. static uint64_t load_address() {
  49. const struct segment_command_64 *cmd = getsegbyname("__TEXT");
  50. #else
  51. static uint32_t load_address() {
  52. const struct segment_command *cmd = getsegbyname("__TEXT");
  53. #endif
  54. char full_path[1024];
  55. uint32_t size = sizeof(full_path);
  56. if (cmd && !_NSGetExecutablePath(full_path, &size)) {
  57. uint32_t dyld_count = _dyld_image_count();
  58. for (uint32_t i = 0; i < dyld_count; i++) {
  59. const char *image_name = _dyld_get_image_name(i);
  60. if (image_name && strncmp(image_name, full_path, 1024) == 0) {
  61. return cmd->vmaddr + _dyld_get_image_vmaddr_slide(i);
  62. }
  63. }
  64. }
  65. return 0;
  66. }
  67. static void handle_crash(int sig) {
  68. if (OS::get_singleton() == NULL)
  69. return;
  70. void *bt_buffer[256];
  71. size_t size = backtrace(bt_buffer, 256);
  72. String _execpath = OS::get_singleton()->get_executable_path();
  73. String msg = GLOBAL_GET("debug/settings/crash_handler/message");
  74. // Dump the backtrace to stderr with a message to the user
  75. fprintf(stderr, "%s: Program crashed with signal %d\n", __FUNCTION__, sig);
  76. fprintf(stderr, "Dumping the backtrace. %ls\n", msg.c_str());
  77. char **strings = backtrace_symbols(bt_buffer, size);
  78. if (strings) {
  79. void *load_addr = (void *)load_address();
  80. for (int i = 1; i < size; i++) {
  81. char fname[1024];
  82. Dl_info info;
  83. snprintf(fname, 1024, "%s", strings[i]);
  84. // Try to demangle the function name to provide a more readable one
  85. if (dladdr(bt_buffer[i], &info) && info.dli_sname) {
  86. if (info.dli_sname[0] == '_') {
  87. int status;
  88. char *demangled = abi::__cxa_demangle(info.dli_sname, NULL, 0, &status);
  89. if (status == 0 && demangled) {
  90. snprintf(fname, 1024, "%s", demangled);
  91. }
  92. if (demangled)
  93. free(demangled);
  94. }
  95. }
  96. String output = fname;
  97. // Try to get the file/line number using atos
  98. if (bt_buffer[i] > (void *)0x0 && OS::get_singleton()) {
  99. List<String> args;
  100. char str[1024];
  101. args.push_back("-o");
  102. args.push_back(_execpath);
  103. args.push_back("-arch");
  104. #ifdef __x86_64__
  105. args.push_back("x86_64");
  106. #else
  107. args.push_back("i386");
  108. #endif
  109. args.push_back("-l");
  110. snprintf(str, 1024, "%p", load_addr);
  111. args.push_back(str);
  112. snprintf(str, 1024, "%p", bt_buffer[i]);
  113. args.push_back(str);
  114. int ret;
  115. String out = "";
  116. Error err = OS::get_singleton()->execute(String("atos"), args, true, NULL, &out, &ret);
  117. if (err == OK && out.substr(0, 2) != "0x") {
  118. out.erase(out.length() - 1, 1);
  119. output = out;
  120. }
  121. }
  122. fprintf(stderr, "[%d] %ls\n", i, output.c_str());
  123. }
  124. free(strings);
  125. }
  126. fprintf(stderr, "-- END OF BACKTRACE --\n");
  127. // Abort to pass the error to the OS
  128. abort();
  129. }
  130. #endif
  131. CrashHandler::CrashHandler() {
  132. disabled = false;
  133. }
  134. CrashHandler::~CrashHandler() {
  135. }
  136. void CrashHandler::disable() {
  137. if (disabled)
  138. return;
  139. #ifdef CRASH_HANDLER_ENABLED
  140. signal(SIGSEGV, NULL);
  141. signal(SIGFPE, NULL);
  142. signal(SIGILL, NULL);
  143. #endif
  144. disabled = true;
  145. }
  146. void CrashHandler::initialize() {
  147. #ifdef CRASH_HANDLER_ENABLED
  148. signal(SIGSEGV, handle_crash);
  149. signal(SIGFPE, handle_crash);
  150. signal(SIGILL, handle_crash);
  151. #endif
  152. }