Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/linuxbsd/crash_handler_linuxbsd.cpp
20831 views
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/io/file_access.h"
35
#include "core/object/script_language.h"
36
#include "core/os/main_loop.h"
37
#include "core/os/os.h"
38
#include "core/string/print_string.h"
39
#include "core/version.h"
40
#include "main/main.h"
41
42
#ifndef DEBUG_ENABLED
43
#undef CRASH_HANDLER_ENABLED
44
#endif
45
46
#ifdef CRASH_HANDLER_ENABLED
47
#include <cxxabi.h>
48
#include <dlfcn.h>
49
#include <execinfo.h>
50
#include <link.h>
51
#include <csignal>
52
#include <cstdlib>
53
54
static void handle_crash(int sig) {
55
signal(SIGSEGV, SIG_DFL);
56
signal(SIGFPE, SIG_DFL);
57
signal(SIGILL, SIG_DFL);
58
59
if (OS::get_singleton() == nullptr) {
60
abort();
61
}
62
63
if (OS::get_singleton()->is_crash_handler_silent()) {
64
std::_Exit(0);
65
}
66
67
void *bt_buffer[256];
68
size_t size = backtrace(bt_buffer, 256);
69
String _execpath = OS::get_singleton()->get_executable_path();
70
71
if (FileAccess::exists(_execpath + ".debugsymbols")) {
72
_execpath = _execpath + ".debugsymbols";
73
}
74
75
String msg;
76
if (ProjectSettings::get_singleton()) {
77
msg = GLOBAL_GET("debug/settings/crash_handler/message");
78
}
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
85
// Dump the backtrace to stderr with a message to the user
86
print_error("\n================================================================");
87
print_error(vformat("%s: Program crashed with signal %d", __FUNCTION__, sig));
88
89
// Print the engine version just before, so that people are reminded to include the version in backtrace reports.
90
if (String(GODOT_VERSION_HASH).is_empty()) {
91
print_error(vformat("Engine version: %s", GODOT_VERSION_FULL_NAME));
92
} else {
93
print_error(vformat("Engine version: %s (%s)", GODOT_VERSION_FULL_NAME, GODOT_VERSION_HASH));
94
}
95
print_error(vformat("Dumping the backtrace. %s", msg));
96
char **strings = backtrace_symbols(bt_buffer, size);
97
// PIE executable relocation, zero for non-PIE executables
98
#ifdef __GLIBC__
99
// This is a glibc only thing apparently.
100
uintptr_t relocation = _r_debug.r_map->l_addr;
101
#else
102
// Non glibc systems apparently don't give PIE relocation info.
103
uintptr_t relocation = 0;
104
#endif //__GLIBC__
105
if (strings) {
106
List<String> args;
107
for (size_t i = 0; i < size; i++) {
108
char str[1024];
109
snprintf(str, 1024, "%p", (void *)((uintptr_t)bt_buffer[i] - relocation));
110
args.push_back(str);
111
}
112
args.push_back("-e");
113
args.push_back(_execpath);
114
115
// Try to get the file/line number using addr2line
116
int ret;
117
String output = "";
118
Error err = OS::get_singleton()->execute(String("addr2line"), args, &output, &ret);
119
Vector<String> addr2line_results;
120
if (err == OK) {
121
addr2line_results = output.substr(0, output.length() - 1).split("\n", false);
122
}
123
124
for (size_t i = 1; i < size; i++) {
125
char fname[1024];
126
Dl_info info;
127
128
snprintf(fname, 1024, "%s", strings[i]);
129
130
// Try to demangle the function name to provide a more readable one
131
if (dladdr(bt_buffer[i], &info) && info.dli_sname) {
132
if (info.dli_sname[0] == '_') {
133
int status = 0;
134
char *demangled = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status);
135
136
if (status == 0 && demangled) {
137
snprintf(fname, 1024, "%s", demangled);
138
}
139
140
if (demangled) {
141
free(demangled);
142
}
143
}
144
}
145
146
// Simplify printed file paths to remove redundant `/./` sections (e.g. `/opt/godot/./core` -> `/opt/godot/core`).
147
print_error(vformat("[%d] %s (%s)", (int64_t)i, fname, err == OK ? addr2line_results[i].replace("/./", "/") : ""));
148
}
149
150
free(strings);
151
}
152
print_error("-- END OF C++ BACKTRACE --");
153
print_error("================================================================");
154
155
for (const Ref<ScriptBacktrace> &backtrace : ScriptServer::capture_script_backtraces(false)) {
156
if (!backtrace->is_empty()) {
157
print_error(backtrace->format());
158
print_error(vformat("-- END OF %s BACKTRACE --", backtrace->get_language_name().to_upper()));
159
print_error("================================================================");
160
}
161
}
162
163
// Abort to pass the error to the OS
164
abort();
165
}
166
#endif
167
168
CrashHandler::CrashHandler() {
169
disabled = false;
170
}
171
172
CrashHandler::~CrashHandler() {
173
disable();
174
}
175
176
void CrashHandler::disable() {
177
if (disabled) {
178
return;
179
}
180
181
#ifdef CRASH_HANDLER_ENABLED
182
signal(SIGSEGV, SIG_DFL);
183
signal(SIGFPE, SIG_DFL);
184
signal(SIGILL, SIG_DFL);
185
#endif
186
187
disabled = true;
188
}
189
190
void CrashHandler::initialize() {
191
#ifdef CRASH_HANDLER_ENABLED
192
signal(SIGSEGV, handle_crash);
193
signal(SIGFPE, handle_crash);
194
signal(SIGILL, handle_crash);
195
#endif
196
}
197
198