Debian Codegraph @

platform/macos/crash_handler_macos.mm

raw
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
31 #import "crash_handler_macos.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/object/script_language.h"
35 #include "core/os/main_loop.h"
36 #include "core/os/os.h"
37 #include "core/string/print_string.h"
38 #include "core/version.h"
39 #include "main/main.h"
40
41 #include <unistd.h>
42
43 #if defined(DEBUG_ENABLED)
44 #define CRASH_HANDLER_ENABLED 1
45 #endif
46
47 #ifdef CRASH_HANDLER_ENABLED
48 #include <cxxabi.h>
49 #include <dlfcn.h>
50 #include <execinfo.h>
51 #include <csignal>
52 #include <cstdlib>
53
54 #import <mach-o/dyld.h>
55 #import <mach-o/getsect.h>
56
57 static uint64_t load_address() {
58 char full_path[1024];
59 uint32_t size = sizeof(full_path);
60
61 if (!_NSGetExecutablePath(full_path, &size)) {
62 void *handle = dlopen(full_path, RTLD_LAZY | RTLD_NOLOAD);
63 void *addr = dlsym(handle, "main");
64 Dl_info info;
65 if (dladdr(addr, &info)) {
66 return (uint64_t)info.dli_fbase;
67 }
68 }
69
70 return 0;
71 }
72
73 static void handle_crash(int sig) {
74 signal(SIGSEGV, SIG_DFL);
75 signal(SIGFPE, SIG_DFL);
76 signal(SIGILL, SIG_DFL);
77 signal(SIGTRAP, SIG_DFL);
78
79 if (OS::get_singleton() == nullptr) {
80 abort();
81 }
82
83 if (OS::get_singleton()->is_crash_handler_silent()) {
84 std::_Exit(0);
85 }
86
87 void *bt_buffer[256];
88 size_t size = backtrace(bt_buffer, 256);
89 String _execpath = OS::get_singleton()->get_executable_path();
90
91 String msg;
92 if (ProjectSettings::get_singleton()) {
93 msg = GLOBAL_GET("debug/settings/crash_handler/message");
94 }
95
96 // Tell MainLoop about the crash. This can be handled by users too in Node.
97 if (OS::get_singleton()->get_main_loop()) {
98 OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
99 }
100
101 // Dump the backtrace to stderr with a message to the user.
102 print_error("\n================================================================");
103 print_error(vformat("%s: Program crashed with signal %d", __FUNCTION__, sig));
104
105 // Print the engine version just before, so that people are reminded to include the version in backtrace reports.
106 if (String(GODOT_VERSION_HASH).is_empty()) {
107 print_error(vformat("Engine version: %s", GODOT_VERSION_FULL_NAME));
108 } else {
109 print_error(vformat("Engine version: %s (%s)", GODOT_VERSION_FULL_NAME, GODOT_VERSION_HASH));
110 }
111 print_error(vformat("Dumping the backtrace. %s", msg));
112
113 List<String> args;
114 args.push_back("-o");
115 args.push_back(_execpath);
116
117 #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__)
118 args.push_back("-arch");
119 args.push_back("x86_64");
120 #elif defined(__aarch64__)
121 args.push_back("-arch");
122 args.push_back("arm64");
123 #endif
124
125 args.push_back("--fullPath");
126 args.push_back("-l");
127
128 char str[1024];
129 void *load_addr = (void *)load_address();
130 snprintf(str, 1024, "%p", load_addr);
131 args.push_back(str);
132
133 for (size_t i = 0; i < size; i++) {
134 snprintf(str, 1024, "%p", bt_buffer[i]);
135 args.push_back(str);
136 }
137
138 // Single execution of atos with all addresses.
139 String out;
140 int ret;
141 Error err = OS::get_singleton()->execute(String("atos"), args, &out, &ret);
142
143 if (err == OK) {
144 // Parse the multi-line output
145 Vector<String> lines = out.split("\n");
146
147 // Get demangled names from dladdr for fallback.
148 char **strings = backtrace_symbols(bt_buffer, size);
149
150 for (int i = 1; i < lines.size() && i < (int)size; i++) {
151 String output = lines[i];
152
153 // If atos failed for this address, fall back to dladdr.
154 if (output.substr(0, 2) == "0x" && strings) {
155 char fname[1024];
156 Dl_info info;
157
158 snprintf(fname, 1024, "%s", strings[i]);
159
160 if (dladdr(bt_buffer[i], &info) && info.dli_sname) {
161 if (info.dli_sname[0] == '_') {
162 int status;
163 char *demangled = abi::__cxa_demangle(info.dli_sname, nullptr, 0, &status);
164
165 if (status == 0 && demangled) {
166 snprintf(fname, 1024, "%s", demangled);
167 }
168
169 if (demangled) {
170 free(demangled);
171 }
172 }
173 }
174 output = fname;
175 }
176
177 print_error(vformat("[%d] %s", (int64_t)i, output));
178 }
179
180 if (strings) {
181 free(strings);
182 }
183 } else {
184 // Fallback if atos fails entirely
185 char **strings = backtrace_symbols(bt_buffer, size);
186 if (strings) {
187 for (size_t i = 0; i < size; i++) {
188 print_error(vformat("[%d] %s", (int64_t)i, strings[i]));
189 }
190 free(strings);
191 }
192 }
193
194 print_error("-- END OF C++ BACKTRACE --");
195 print_error("================================================================");
196
197 for (const Ref<ScriptBacktrace> &backtrace : ScriptServer::capture_script_backtraces(false)) {
198 if (!backtrace->is_empty()) {
199 print_error(backtrace->format());
200 print_error(vformat("-- END OF %s BACKTRACE --", backtrace->get_language_name().to_upper()));
201 print_error("================================================================");
202 }
203 }
204
205 // Abort to pass the error to the OS
206 abort();
207 }
208 #endif
209
210 CrashHandler::CrashHandler() {
211 disabled = false;
212 }
213
214 CrashHandler::~CrashHandler() {
215 disable();
216 }
217
218 void CrashHandler::disable() {
219 if (disabled) {
220 return;
221 }
222
223 #ifdef CRASH_HANDLER_ENABLED
224 signal(SIGSEGV, SIG_DFL);
225 signal(SIGFPE, SIG_DFL);
226 signal(SIGILL, SIG_DFL);
227 signal(SIGTRAP, SIG_DFL);
228 #endif
229
230 disabled = true;
231 }
232
233 void CrashHandler::initialize() {
234 #ifdef CRASH_HANDLER_ENABLED
235 signal(SIGSEGV, handle_crash);
236 signal(SIGFPE, handle_crash);
237 signal(SIGILL, handle_crash);
238 signal(SIGTRAP, handle_crash);
239 #endif
240 }