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