crash_handler_osx.mm 5.4 KB

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