crash_handler_macos.mm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**************************************************************************/
  2. /* crash_handler_macos.mm */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_macos.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/os/os.h"
  33. #include "core/string/print_string.h"
  34. #include "core/version.h"
  35. #include "main/main.h"
  36. #include <string.h>
  37. #include <unistd.h>
  38. #if defined(DEBUG_ENABLED)
  39. #define CRASH_HANDLER_ENABLED 1
  40. #endif
  41. #ifdef CRASH_HANDLER_ENABLED
  42. #include <cxxabi.h>
  43. #include <dlfcn.h>
  44. #include <execinfo.h>
  45. #include <signal.h>
  46. #include <stdlib.h>
  47. #include <mach-o/dyld.h>
  48. #include <mach-o/getsect.h>
  49. static uint64_t load_address() {
  50. const struct segment_command_64 *cmd = getsegbyname("__TEXT");
  51. char full_path[1024];
  52. uint32_t size = sizeof(full_path);
  53. if (cmd && !_NSGetExecutablePath(full_path, &size)) {
  54. uint32_t dyld_count = _dyld_image_count();
  55. for (uint32_t i = 0; i < dyld_count; i++) {
  56. const char *image_name = _dyld_get_image_name(i);
  57. if (image_name && strncmp(image_name, full_path, 1024) == 0) {
  58. return cmd->vmaddr + _dyld_get_image_vmaddr_slide(i);
  59. }
  60. }
  61. }
  62. return 0;
  63. }
  64. static void handle_crash(int sig) {
  65. signal(SIGSEGV, SIG_DFL);
  66. signal(SIGFPE, SIG_DFL);
  67. signal(SIGILL, SIG_DFL);
  68. signal(SIGTRAP, SIG_DFL);
  69. if (OS::get_singleton() == nullptr) {
  70. abort();
  71. }
  72. void *bt_buffer[256];
  73. size_t size = backtrace(bt_buffer, 256);
  74. String _execpath = OS::get_singleton()->get_executable_path();
  75. String msg;
  76. const ProjectSettings *proj_settings = ProjectSettings::get_singleton();
  77. if (proj_settings) {
  78. msg = proj_settings->get("debug/settings/crash_handler/message");
  79. }
  80. // Tell MainLoop about the crash. This can be handled by users too in Node.
  81. if (OS::get_singleton()->get_main_loop()) {
  82. OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
  83. }
  84. // Dump the backtrace to stderr with a message to the user
  85. print_error("\n================================================================");
  86. print_error(vformat("%s: Program crashed with signal %d", __FUNCTION__, sig));
  87. // Print the engine version just before, so that people are reminded to include the version in backtrace reports.
  88. if (String(VERSION_HASH).is_empty()) {
  89. print_error(vformat("Engine version: %s", VERSION_FULL_NAME));
  90. } else {
  91. print_error(vformat("Engine version: %s (%s)", VERSION_FULL_NAME, VERSION_HASH));
  92. }
  93. print_error(vformat("Dumping the backtrace. %s", msg));
  94. char **strings = backtrace_symbols(bt_buffer, size);
  95. if (strings) {
  96. void *load_addr = (void *)load_address();
  97. for (size_t i = 1; i < size; i++) {
  98. char fname[1024];
  99. Dl_info info;
  100. snprintf(fname, 1024, "%s", strings[i]);
  101. // Try to demangle the function name to provide a more readable one
  102. if (dladdr(bt_buffer[i], &info) && info.dli_sname) {
  103. if (info.dli_sname[0] == '_') {
  104. int status;
  105. char *demangled = abi::__cxa_demangle(info.dli_sname, nullptr, 0, &status);
  106. if (status == 0 && demangled) {
  107. snprintf(fname, 1024, "%s", demangled);
  108. }
  109. if (demangled) {
  110. free(demangled);
  111. }
  112. }
  113. }
  114. String output = fname;
  115. // Try to get the file/line number using atos
  116. if (bt_buffer[i] > (void *)0x0 && OS::get_singleton()) {
  117. List<String> args;
  118. char str[1024];
  119. args.push_back("-o");
  120. args.push_back(_execpath);
  121. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__)
  122. args.push_back("-arch");
  123. args.push_back("x86_64");
  124. #elif defined(__aarch64__)
  125. args.push_back("-arch");
  126. args.push_back("arm64");
  127. #endif
  128. args.push_back("-l");
  129. snprintf(str, 1024, "%p", load_addr);
  130. args.push_back(str);
  131. snprintf(str, 1024, "%p", bt_buffer[i]);
  132. args.push_back(str);
  133. int ret;
  134. String out = "";
  135. Error err = OS::get_singleton()->execute(String("atos"), args, &out, &ret);
  136. if (err == OK && out.substr(0, 2) != "0x") {
  137. out = out.substr(0, out.length() - 1);
  138. output = out;
  139. }
  140. }
  141. print_error(vformat("[%d] %s", (int64_t)i, output));
  142. }
  143. free(strings);
  144. }
  145. print_error("-- END OF BACKTRACE --");
  146. print_error("================================================================");
  147. // Abort to pass the error to the OS
  148. abort();
  149. }
  150. #endif
  151. CrashHandler::CrashHandler() {
  152. disabled = false;
  153. }
  154. CrashHandler::~CrashHandler() {
  155. disable();
  156. }
  157. void CrashHandler::disable() {
  158. if (disabled) {
  159. return;
  160. }
  161. #ifdef CRASH_HANDLER_ENABLED
  162. signal(SIGSEGV, SIG_DFL);
  163. signal(SIGFPE, SIG_DFL);
  164. signal(SIGILL, SIG_DFL);
  165. signal(SIGTRAP, SIG_DFL);
  166. #endif
  167. disabled = true;
  168. }
  169. void CrashHandler::initialize() {
  170. #ifdef CRASH_HANDLER_ENABLED
  171. signal(SIGSEGV, handle_crash);
  172. signal(SIGFPE, handle_crash);
  173. signal(SIGILL, handle_crash);
  174. signal(SIGTRAP, handle_crash);
  175. #endif
  176. }