1 | /**************************************************************************/ |
2 | /* crash_handler_linuxbsd.cpp */ |
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 | #include "crash_handler_linuxbsd.h" |
32 | |
33 | #include "core/config/project_settings.h" |
34 | #include "core/os/os.h" |
35 | #include "core/string/print_string.h" |
36 | #include "core/version.h" |
37 | #include "main/main.h" |
38 | |
39 | #ifdef DEBUG_ENABLED |
40 | #define CRASH_HANDLER_ENABLED 1 |
41 | #endif |
42 | |
43 | #ifdef CRASH_HANDLER_ENABLED |
44 | #include <cxxabi.h> |
45 | #include <dlfcn.h> |
46 | #include <execinfo.h> |
47 | #include <link.h> |
48 | #include <signal.h> |
49 | #include <stdlib.h> |
50 | |
51 | static void handle_crash(int sig) { |
52 | if (OS::get_singleton() == nullptr) { |
53 | abort(); |
54 | } |
55 | |
56 | void *bt_buffer[256]; |
57 | size_t size = backtrace(bt_buffer, 256); |
58 | String _execpath = OS::get_singleton()->get_executable_path(); |
59 | |
60 | String msg; |
61 | const ProjectSettings *proj_settings = ProjectSettings::get_singleton(); |
62 | if (proj_settings) { |
63 | msg = proj_settings->get("debug/settings/crash_handler/message" ); |
64 | } |
65 | |
66 | // Tell MainLoop about the crash. This can be handled by users too in Node. |
67 | if (OS::get_singleton()->get_main_loop()) { |
68 | OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH); |
69 | } |
70 | |
71 | // Dump the backtrace to stderr with a message to the user |
72 | print_error("\n================================================================" ); |
73 | print_error(vformat("%s: Program crashed with signal %d" , __FUNCTION__, sig)); |
74 | |
75 | // Print the engine version just before, so that people are reminded to include the version in backtrace reports. |
76 | if (String(VERSION_HASH).is_empty()) { |
77 | print_error(vformat("Engine version: %s" , VERSION_FULL_NAME)); |
78 | } else { |
79 | print_error(vformat("Engine version: %s (%s)" , VERSION_FULL_NAME, VERSION_HASH)); |
80 | } |
81 | print_error(vformat("Dumping the backtrace. %s" , msg)); |
82 | char **strings = backtrace_symbols(bt_buffer, size); |
83 | // PIE executable relocation, zero for non-PIE executables |
84 | #ifdef __GLIBC__ |
85 | // This is a glibc only thing apparently. |
86 | uintptr_t relocation = _r_debug.r_map->l_addr; |
87 | #else |
88 | // Non glibc systems apparently don't give PIE relocation info. |
89 | uintptr_t relocation = 0; |
90 | #endif //__GLIBC__ |
91 | if (strings) { |
92 | List<String> args; |
93 | for (size_t i = 0; i < size; i++) { |
94 | char str[1024]; |
95 | snprintf(str, 1024, "%p" , (void *)((uintptr_t)bt_buffer[i] - relocation)); |
96 | args.push_back(str); |
97 | } |
98 | args.push_back("-e" ); |
99 | args.push_back(_execpath); |
100 | |
101 | // Try to get the file/line number using addr2line |
102 | int ret; |
103 | String output = "" ; |
104 | Error err = OS::get_singleton()->execute(String("addr2line" ), args, &output, &ret); |
105 | Vector<String> addr2line_results; |
106 | if (err == OK) { |
107 | addr2line_results = output.substr(0, output.length() - 1).split("\n" , false); |
108 | } |
109 | |
110 | for (size_t i = 1; i < size; i++) { |
111 | char fname[1024]; |
112 | Dl_info info; |
113 | |
114 | snprintf(fname, 1024, "%s" , strings[i]); |
115 | |
116 | // Try to demangle the function name to provide a more readable one |
117 | if (dladdr(bt_buffer[i], &info) && info.dli_sname) { |
118 | if (info.dli_sname[0] == '_') { |
119 | int status = 0; |
120 | char *demangled = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status); |
121 | |
122 | if (status == 0 && demangled) { |
123 | snprintf(fname, 1024, "%s" , demangled); |
124 | } |
125 | |
126 | if (demangled) { |
127 | free(demangled); |
128 | } |
129 | } |
130 | } |
131 | |
132 | print_error(vformat("[%d] %s (%s)" , (int64_t)i, fname, err == OK ? addr2line_results[i] : "" )); |
133 | } |
134 | |
135 | free(strings); |
136 | } |
137 | print_error("-- END OF BACKTRACE --" ); |
138 | print_error("================================================================" ); |
139 | |
140 | // Abort to pass the error to the OS |
141 | abort(); |
142 | } |
143 | #endif |
144 | |
145 | CrashHandler::CrashHandler() { |
146 | disabled = false; |
147 | } |
148 | |
149 | CrashHandler::~CrashHandler() { |
150 | disable(); |
151 | } |
152 | |
153 | void CrashHandler::disable() { |
154 | if (disabled) { |
155 | return; |
156 | } |
157 | |
158 | #ifdef CRASH_HANDLER_ENABLED |
159 | signal(SIGSEGV, nullptr); |
160 | signal(SIGFPE, nullptr); |
161 | signal(SIGILL, nullptr); |
162 | #endif |
163 | |
164 | disabled = true; |
165 | } |
166 | |
167 | void CrashHandler::initialize() { |
168 | #ifdef CRASH_HANDLER_ENABLED |
169 | signal(SIGSEGV, handle_crash); |
170 | signal(SIGFPE, handle_crash); |
171 | signal(SIGILL, handle_crash); |
172 | #endif |
173 | } |
174 | |