Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/platform/windows/crash_handler_windows_seh.cpp
10277 views
1
/**************************************************************************/
2
/* crash_handler_windows_seh.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
// Backtrace code based on: https://stackoverflow.com/questions/6205981/windows-c-stack-trace-from-a-running-app
43
44
#include <algorithm>
45
#include <cstdlib>
46
#include <iterator>
47
#include <string>
48
#include <vector>
49
50
#include <psapi.h>
51
52
// Some versions of imagehlp.dll lack the proper packing directives themselves
53
// so we need to do it.
54
#pragma pack(push, before_imagehlp, 8)
55
#include <imagehlp.h>
56
#pragma pack(pop, before_imagehlp)
57
58
struct module_data {
59
std::string image_name;
60
std::string module_name;
61
void *base_address = nullptr;
62
DWORD load_size;
63
};
64
65
class symbol {
66
typedef IMAGEHLP_SYMBOL64 sym_type;
67
sym_type *sym;
68
static const int max_name_len = 1024;
69
70
public:
71
symbol(HANDLE process, DWORD64 address) :
72
sym((sym_type *)::operator new(sizeof(*sym) + max_name_len)) {
73
memset(sym, '\0', sizeof(*sym) + max_name_len);
74
sym->SizeOfStruct = sizeof(*sym);
75
sym->MaxNameLength = max_name_len;
76
DWORD64 displacement;
77
78
SymGetSymFromAddr64(process, address, &displacement, sym);
79
}
80
81
std::string name() { return std::string(sym->Name); }
82
std::string undecorated_name() {
83
if (*sym->Name == '\0') {
84
return "<couldn't map PC to fn name>";
85
}
86
std::vector<char> und_name(max_name_len);
87
UnDecorateSymbolName(sym->Name, &und_name[0], max_name_len, UNDNAME_COMPLETE);
88
return std::string(&und_name[0], strlen(&und_name[0]));
89
}
90
};
91
92
class get_mod_info {
93
HANDLE process;
94
95
public:
96
get_mod_info(HANDLE h) :
97
process(h) {}
98
99
module_data operator()(HMODULE module) {
100
module_data ret;
101
char temp[4096];
102
MODULEINFO mi;
103
104
GetModuleInformation(process, module, &mi, sizeof(mi));
105
ret.base_address = mi.lpBaseOfDll;
106
ret.load_size = mi.SizeOfImage;
107
108
GetModuleFileNameEx(process, module, temp, sizeof(temp));
109
ret.image_name = temp;
110
GetModuleBaseName(process, module, temp, sizeof(temp));
111
ret.module_name = temp;
112
std::vector<char> img(ret.image_name.begin(), ret.image_name.end());
113
std::vector<char> mod(ret.module_name.begin(), ret.module_name.end());
114
SymLoadModule64(process, nullptr, &img[0], &mod[0], (DWORD64)ret.base_address, ret.load_size);
115
return ret;
116
}
117
};
118
119
DWORD CrashHandlerException(EXCEPTION_POINTERS *ep) {
120
HANDLE process = GetCurrentProcess();
121
HANDLE hThread = GetCurrentThread();
122
DWORD offset_from_symbol = 0;
123
IMAGEHLP_LINE64 line = {};
124
std::vector<module_data> modules;
125
DWORD cbNeeded;
126
std::vector<HMODULE> module_handles(1);
127
128
if (OS::get_singleton() == nullptr || OS::get_singleton()->is_disable_crash_handler() || IsDebuggerPresent()) {
129
return EXCEPTION_CONTINUE_SEARCH;
130
}
131
132
if (OS::get_singleton()->is_crash_handler_silent()) {
133
std::_Exit(0);
134
}
135
136
String msg;
137
if (ProjectSettings::get_singleton()) {
138
msg = GLOBAL_GET("debug/settings/crash_handler/message");
139
}
140
141
// Tell MainLoop about the crash. This can be handled by users too in Node.
142
if (OS::get_singleton()->get_main_loop()) {
143
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
144
}
145
146
print_error("\n================================================================");
147
print_error(vformat("%s: Program crashed", __FUNCTION__));
148
149
// Print the engine version just before, so that people are reminded to include the version in backtrace reports.
150
if (String(GODOT_VERSION_HASH).is_empty()) {
151
print_error(vformat("Engine version: %s", GODOT_VERSION_FULL_NAME));
152
} else {
153
print_error(vformat("Engine version: %s (%s)", GODOT_VERSION_FULL_NAME, GODOT_VERSION_HASH));
154
}
155
print_error(vformat("Dumping the backtrace. %s", msg));
156
157
// Load the symbols:
158
if (!SymInitialize(process, nullptr, false)) {
159
return EXCEPTION_CONTINUE_SEARCH;
160
}
161
162
SymSetOptions(SymGetOptions() | SYMOPT_LOAD_LINES | SYMOPT_UNDNAME | SYMOPT_EXACT_SYMBOLS);
163
EnumProcessModules(process, &module_handles[0], module_handles.size() * sizeof(HMODULE), &cbNeeded);
164
module_handles.resize(cbNeeded / sizeof(HMODULE));
165
EnumProcessModules(process, &module_handles[0], module_handles.size() * sizeof(HMODULE), &cbNeeded);
166
std::transform(module_handles.begin(), module_handles.end(), std::back_inserter(modules), get_mod_info(process));
167
void *base = modules[0].base_address;
168
169
// Setup stuff:
170
CONTEXT *context = ep->ContextRecord;
171
STACKFRAME64 frame;
172
bool skip_first = false;
173
174
frame.AddrPC.Mode = AddrModeFlat;
175
frame.AddrStack.Mode = AddrModeFlat;
176
frame.AddrFrame.Mode = AddrModeFlat;
177
178
#if defined(_M_X64)
179
frame.AddrPC.Offset = context->Rip;
180
frame.AddrStack.Offset = context->Rsp;
181
frame.AddrFrame.Offset = context->Rbp;
182
#elif defined(_M_ARM64) || defined(_M_ARM64EC)
183
frame.AddrPC.Offset = context->Pc;
184
frame.AddrStack.Offset = context->Sp;
185
frame.AddrFrame.Offset = context->Fp;
186
#elif defined(_M_ARM)
187
frame.AddrPC.Offset = context->Pc;
188
frame.AddrStack.Offset = context->Sp;
189
frame.AddrFrame.Offset = context->R11;
190
#else
191
frame.AddrPC.Offset = context->Eip;
192
frame.AddrStack.Offset = context->Esp;
193
frame.AddrFrame.Offset = context->Ebp;
194
195
// Skip the first one to avoid a duplicate on 32-bit mode
196
skip_first = true;
197
#endif
198
199
line.SizeOfStruct = sizeof(line);
200
IMAGE_NT_HEADERS *h = ImageNtHeader(base);
201
DWORD image_type = h->FileHeader.Machine;
202
203
int n = 0;
204
do {
205
if (skip_first) {
206
skip_first = false;
207
} else {
208
if (frame.AddrPC.Offset != 0) {
209
std::string fnName = symbol(process, frame.AddrPC.Offset).undecorated_name();
210
211
if (SymGetLineFromAddr64(process, frame.AddrPC.Offset, &offset_from_symbol, &line)) {
212
print_error(vformat("[%d] %s (%s:%d)", n, fnName.c_str(), (char *)line.FileName, (int)line.LineNumber));
213
} else {
214
print_error(vformat("[%d] %s", n, fnName.c_str()));
215
}
216
} else {
217
print_error(vformat("[%d] ???", n));
218
}
219
220
n++;
221
}
222
223
if (!StackWalk64(image_type, process, hThread, &frame, context, nullptr, SymFunctionTableAccess64, SymGetModuleBase64, nullptr)) {
224
break;
225
}
226
} while (frame.AddrReturn.Offset != 0 && n < 256);
227
228
print_error("-- END OF C++ BACKTRACE --");
229
print_error("================================================================");
230
231
SymCleanup(process);
232
233
for (const Ref<ScriptBacktrace> &backtrace : ScriptServer::capture_script_backtraces(false)) {
234
if (!backtrace->is_empty()) {
235
print_error(backtrace->format());
236
print_error(vformat("-- END OF %s BACKTRACE --", backtrace->get_language_name().to_upper()));
237
print_error("================================================================");
238
}
239
}
240
241
// Pass the exception to the OS
242
return EXCEPTION_CONTINUE_SEARCH;
243
}
244
#endif
245
246
CrashHandler::CrashHandler() {
247
disabled = false;
248
}
249
250
CrashHandler::~CrashHandler() {
251
}
252
253
void CrashHandler::disable() {
254
if (disabled) {
255
return;
256
}
257
258
disabled = true;
259
}
260
261
void CrashHandler::initialize() {
262
}
263
264