Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/windows/crash_handler_windows_signal.cpp
20934 views
1
/**************************************************************************/
2
/* crash_handler_windows_signal.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_windows.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
#ifdef CRASH_HANDLER_EXCEPTION
43
44
#include <cxxabi.h>
45
#include <algorithm>
46
#include <csignal>
47
#include <cstdlib>
48
#include <string>
49
#include <vector>
50
51
#include <psapi.h>
52
53
#include "thirdparty/libbacktrace/backtrace.h"
54
55
struct CrashHandlerData {
56
int64_t index = 0;
57
backtrace_state *state = nullptr;
58
int64_t offset = 0;
59
};
60
61
int symbol_callback(void *data, uintptr_t pc, const char *filename, int lineno, const char *function) {
62
CrashHandlerData *ch_data = reinterpret_cast<CrashHandlerData *>(data);
63
if (!function) {
64
return 0;
65
}
66
67
char fname[1024];
68
snprintf(fname, 1024, "%s", function);
69
70
if (function[0] == '_') {
71
int status;
72
char *demangled = abi::__cxa_demangle(function, nullptr, nullptr, &status);
73
74
if (status == 0 && demangled) {
75
snprintf(fname, 1024, "%s", demangled);
76
}
77
78
if (demangled) {
79
free(demangled);
80
}
81
}
82
83
print_error(vformat("[%d] %s (%s:%d)", ch_data->index++, String::utf8(fname), String::utf8(filename), lineno));
84
return 0;
85
}
86
87
void error_callback(void *data, const char *msg, int errnum) {
88
CrashHandlerData *ch_data = reinterpret_cast<CrashHandlerData *>(data);
89
if (ch_data->index == 0) {
90
print_error(vformat("Error(%d): %s", errnum, String::utf8(msg)));
91
} else {
92
print_error(vformat("[%d] error(%d): %s", ch_data->index++, errnum, String::utf8(msg)));
93
}
94
}
95
96
int trace_callback(void *data, uintptr_t pc) {
97
CrashHandlerData *ch_data = reinterpret_cast<CrashHandlerData *>(data);
98
backtrace_pcinfo(ch_data->state, pc - ch_data->offset, &symbol_callback, &error_callback, data);
99
return 0;
100
}
101
102
int64_t get_image_base(const String &p_path) {
103
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
104
if (f.is_null()) {
105
return 0;
106
}
107
{
108
f->seek(0x3c);
109
uint32_t pe_pos = f->get_32();
110
111
f->seek(pe_pos);
112
uint32_t magic = f->get_32();
113
if (magic != 0x00004550) {
114
return 0;
115
}
116
}
117
int64_t opt_header_pos = f->get_position() + 0x14;
118
f->seek(opt_header_pos);
119
120
uint16_t opt_header_magic = f->get_16();
121
if (opt_header_magic == 0x10B) {
122
f->seek(opt_header_pos + 0x1C);
123
return f->get_32();
124
} else if (opt_header_magic == 0x20B) {
125
f->seek(opt_header_pos + 0x18);
126
return f->get_64();
127
} else {
128
return 0;
129
}
130
}
131
132
extern void CrashHandlerException(int signal) {
133
CrashHandlerData data;
134
135
if (OS::get_singleton() == nullptr || OS::get_singleton()->is_disable_crash_handler() || IsDebuggerPresent()) {
136
return;
137
}
138
139
if (OS::get_singleton()->is_crash_handler_silent()) {
140
std::_Exit(0);
141
}
142
143
String msg;
144
if (ProjectSettings::get_singleton()) {
145
msg = GLOBAL_GET("debug/settings/crash_handler/message");
146
}
147
148
// Tell MainLoop about the crash. This can be handled by users too in Node.
149
if (OS::get_singleton()->get_main_loop()) {
150
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
151
}
152
153
print_error("\n================================================================");
154
print_error(vformat("%s: Program crashed with signal %d", __FUNCTION__, signal));
155
156
// Print the engine version just before, so that people are reminded to include the version in backtrace reports.
157
if (String(GODOT_VERSION_HASH).is_empty()) {
158
print_error(vformat("Engine version: %s", GODOT_VERSION_FULL_NAME));
159
} else {
160
print_error(vformat("Engine version: %s (%s)", GODOT_VERSION_FULL_NAME, GODOT_VERSION_HASH));
161
}
162
print_error(vformat("Dumping the backtrace. %s", msg));
163
164
String _execpath = OS::get_singleton()->get_executable_path();
165
166
// Load process and image info to determine ASLR addresses offset.
167
MODULEINFO mi;
168
GetModuleInformation(GetCurrentProcess(), GetModuleHandle(nullptr), &mi, sizeof(mi));
169
int64_t image_mem_base = reinterpret_cast<int64_t>(mi.lpBaseOfDll);
170
int64_t image_file_base = get_image_base(_execpath);
171
data.offset = image_mem_base - image_file_base;
172
173
if (FileAccess::exists(_execpath + ".debugsymbols")) {
174
_execpath = _execpath + ".debugsymbols";
175
}
176
_execpath = _execpath.replace_char('/', '\\');
177
178
CharString cs = _execpath.utf8(); // Note: should remain in scope during backtrace_simple call.
179
data.state = backtrace_create_state(cs.get_data(), 0, &error_callback, reinterpret_cast<void *>(&data));
180
if (data.state != nullptr) {
181
data.index = 1;
182
backtrace_simple(data.state, 1, &trace_callback, &error_callback, reinterpret_cast<void *>(&data));
183
}
184
185
print_error("-- END OF C++ BACKTRACE --");
186
print_error("================================================================");
187
188
for (const Ref<ScriptBacktrace> &backtrace : ScriptServer::capture_script_backtraces(false)) {
189
if (!backtrace->is_empty()) {
190
print_error(backtrace->format());
191
print_error(vformat("-- END OF %s BACKTRACE --", backtrace->get_language_name().to_upper()));
192
print_error("================================================================");
193
}
194
}
195
}
196
#endif
197
198
CrashHandler::CrashHandler() {
199
disabled = false;
200
}
201
202
CrashHandler::~CrashHandler() {
203
}
204
205
void CrashHandler::disable() {
206
if (disabled) {
207
return;
208
}
209
210
#if defined(CRASH_HANDLER_EXCEPTION)
211
signal(SIGSEGV, nullptr);
212
signal(SIGFPE, nullptr);
213
signal(SIGILL, nullptr);
214
#endif
215
216
disabled = true;
217
}
218
219
void CrashHandler::initialize() {
220
#if defined(CRASH_HANDLER_EXCEPTION)
221
signal(SIGSEGV, CrashHandlerException);
222
signal(SIGFPE, CrashHandlerException);
223
signal(SIGILL, CrashHandlerException);
224
#endif
225
}
226
227