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