Repository navigation
Expand file tree
/
Copy pathCrashHandler.cpp
More file actions
266 lines (226 loc) · 9.81 KB
/
Copy pathCrashHandler.cpp
File metadata and controls
266 lines (226 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <BranchInfo.h>
#include "CrashHandler.h"
#include <DbgHelp.h>
#include <Windows.h>
#include <chrono>
#include <filesystem>
#include <exception>
#include <intrin.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <strsafe.h>
#ifndef STATUS_HEAP_CORRUPTION
#define STATUS_HEAP_CORRUPTION 0xC0000374L
#endif
#ifndef STATUS_STACK_BUFFER_OVERRUN
#define STATUS_STACK_BUFFER_OVERRUN 0xC0000409L
#endif
#ifndef STATUS_FATAL_APP_EXIT
#define STATUS_FATAL_APP_EXIT 0x40000015L
#endif
using time_point = std::chrono::system_clock::time_point;
std::string SerializeTimePoint(const time_point& time, const std::string& format)
{
std::time_t tt = std::chrono::system_clock::to_time_t(time);
std::tm tm = *std::gmtime(&tt); // GMT (UTC)
// std::tm tm = *std::localtime(&tt); //Locale time-zone, usually UTC by default.
std::stringstream ss;
ss << std::put_time(&tm, format.c_str());
return ss.str();
}
static void WriteMiniDump(PEXCEPTION_POINTERS pExceptionInfo)
{
#if (IS_MASTER)
volatile static bool bMiniDump = false;
#else
volatile static bool bMiniDump = true;
#endif
if (!bMiniDump)
return;
HANDLE hDumpFile = NULL;
try
{
MINIDUMP_EXCEPTION_INFORMATION M;
char dumpPath[MAX_PATH];
M.ThreadId = GetCurrentThreadId();
M.ExceptionPointers = pExceptionInfo;
M.ClientPointers = 0;
std::ostringstream oss;
oss << "crash_" << SerializeTimePoint(std::chrono::system_clock::now(), "UTC_%Y-%m-%d_%H-%M-%S")
<< ".dmp";
GetModuleFileNameA(NULL, dumpPath, sizeof(dumpPath));
std::filesystem::path modulePath(dumpPath);
auto subPath = modulePath.parent_path();
CrashHandler::RemovePreviousDump(subPath);
subPath /= oss.str();
hDumpFile = CreateFileA(subPath.string().c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
// baseline settings from https://stackoverflow.com/a/63123214/5273909
//
// MiniDumpWithIndirectlyReferencedMemory captures a small window around every pointer-like
// value on the stack. Without it a dump holds the module data segments and the stacks but no
// heap, so a crash that hands the game a bad object can be traced to the call but not to the
// object: Tools/vr_addresses/dumpmem.mjs reads a game object's vtable and form id straight
// out of a dump, and that only works if the object was captured. It costs a few MB, against
// the ~1 GB MiniDumpWithDataSegs already writes.
auto dumpSettings = MiniDumpWithDataSegs | MiniDumpWithProcessThreadData | MiniDumpWithHandleData |
MiniDumpWithThreadInfo | MiniDumpWithIndirectlyReferencedMemory |
/*
//MiniDumpWithPrivateReadWriteMemory | // this one gens bad dump
MiniDumpWithUnloadedModules |
MiniDumpWithFullMemoryInfo |
MiniDumpWithTokenInformation |
MiniDumpWithPrivateWriteCopyMemory |
*/
0;
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, (MINIDUMP_TYPE)dumpSettings,
(pExceptionInfo) ? &M : NULL, NULL, NULL);
}
catch (...) // Mini-dump is best effort only.
{
}
if (!hDumpFile)
spdlog::critical(__FUNCTION__ ": coredump may have failed.");
else
{
CloseHandle(hDumpFile);
spdlog::critical(__FUNCTION__ ": coredump created -> flush logs.");
}
}
// An unhandled C++ exception, std::bad_alloc being the one that actually shows up, ends in
// std::terminate. The vectored handler below never sees it, because it only answers to access
// violations, so the process aborts with "Fatal program exit requested", no log line and no
// coredump. That is the one failure shape there is no way to diagnose. Log what was thrown and
// write a dump with a real exception stream, so the usual tooling can read the stack out of it.
static void TerminateHandler()
{
static int alreadyTerminating = 0;
const char* pWhat = "not a std::exception";
try
{
if (auto current = std::current_exception())
std::rethrow_exception(current);
}
catch (const std::exception& e)
{
pWhat = e.what();
}
catch (...)
{
}
if (alreadyTerminating++ == 0)
{
spdlog::critical(__FUNCTION__ ": unhandled exception, terminating: {}", pWhat);
CONTEXT context{};
RtlCaptureContext(&context);
EXCEPTION_RECORD record{};
record.ExceptionCode = STATUS_FATAL_APP_EXIT;
record.ExceptionAddress = _ReturnAddress();
EXCEPTION_POINTERS pointers{&record, &context};
WriteMiniDump(&pointers);
spdlog::shutdown();
}
abort();
}
/**
* @brief Whether an exception code means the process is going down.
*
* This used to be access violations and abort only, and a crash that was neither produced nothing at all: no
* log line, no coredump, nothing to read. That happened for real on 2026-08-17, an illegal instruction at
* `0x1416D788A` where the game had jumped into a vtable in `.rdata` and executed it, and the only evidence
* that remained was the message box the player saw.
*
* Every code below already means the process is dead, so writing a dump for them costs nothing that was not
* already lost. Anything continuable or expected is deliberately absent: C++ exceptions (`0xE06D7363`), the
* debugger's thread naming exception (`0x406D1388`) and breakpoints all pass through untouched.
*/
bool IsFatalException(const DWORD aCode) noexcept
{
switch (aCode)
{
case EXCEPTION_ACCESS_VIOLATION:
case EXCEPTION_ILLEGAL_INSTRUCTION:
case EXCEPTION_PRIV_INSTRUCTION:
case EXCEPTION_IN_PAGE_ERROR:
case EXCEPTION_STACK_OVERFLOW:
case EXCEPTION_INT_DIVIDE_BY_ZERO:
case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
case EXCEPTION_DATATYPE_MISALIGNMENT:
case EXCEPTION_NONCONTINUABLE_EXCEPTION:
case EXCEPTION_INVALID_DISPOSITION:
case STATUS_HEAP_CORRUPTION:
case STATUS_STACK_BUFFER_OVERRUN:
case STATUS_FATAL_APP_EXIT: return true;
default: return false;
}
}
LONG WINAPI VectoredExceptionHandler(PEXCEPTION_POINTERS pExceptionInfo)
{
static int alreadyCrashed = 0;
auto retval = EXCEPTION_CONTINUE_SEARCH;
// Serialize
static std::mutex singleThreaded;
const std::lock_guard lock{singleThreaded};
// Check for severe, not continuable and not software-originated exception.
//
// STATUS_FATAL_APP_EXIT is what abort() raises, which is where an unhandled C++ exception ends
// up. The terminate handler above cannot cover that on its own, because the Microsoft CRT's
// set_terminate installs per thread: a handler set on the main thread never runs for a worker,
// and the game runs plenty of workers. A vectored handler is process wide, so answering to the
// abort here catches those wherever they happen.
const auto cExceptionCode = pExceptionInfo->ExceptionRecord->ExceptionCode;
if (IsFatalException(cExceptionCode) && alreadyCrashed++ == 0)
{
spdlog::critical (__FUNCTION__ ": crash occurred!");
spdlog::error(__FUNCTION__ ": exception code is {:x}, at address {}, flags {:x} ",
pExceptionInfo->ExceptionRecord->ExceptionCode,
pExceptionInfo->ExceptionRecord->ExceptionAddress,
pExceptionInfo->ExceptionRecord->ExceptionFlags);
WriteMiniDump(pExceptionInfo);
// Something in STR breaks top-level unhandled exception filters.
// The Win API for them is pretty clunky (non-atomic, not chainable),
// but they can do some important things. If someone actually set one
// they probably meant it; make sure it actually runs.
// This will make more CrashLogger mods work with STR.
// Get the current unhandled exception filter. If it has changed
// from when STR started up, invoke it here.
LPTOP_LEVEL_EXCEPTION_FILTER pCurrentUnhandledExceptionFilter = SetUnhandledExceptionFilter(CrashHandler::GetOriginalUnhandledExceptionFilter());
SetUnhandledExceptionFilter(pCurrentUnhandledExceptionFilter);
if (pCurrentUnhandledExceptionFilter != CrashHandler::GetOriginalUnhandledExceptionFilter())
{
spdlog::critical(__FUNCTION__ ": UnhandledExceptionFilter() workaround triggered.");
singleThreaded.unlock(); // Might reenter, but is safe at this point.
if ((*pCurrentUnhandledExceptionFilter)(pExceptionInfo) == EXCEPTION_CONTINUE_EXECUTION)
retval = EXCEPTION_CONTINUE_EXECUTION;
singleThreaded.lock();
}
spdlog::shutdown();
}
return retval;
}
LPTOP_LEVEL_EXCEPTION_FILTER CrashHandler::m_pUnhandled;
CrashHandler::CrashHandler()
{
// Record the original (or as close as we can get) top-level unhandled exception handler.
// We grab this so we can see if it is changed, presumably by a mod or even graphics drivers.
// Something in STR breaks unhandled exception handling, so we'll fake it if necessary.
// This is the only way to get the current setting, but the race is small.
m_pUnhandled = SetUnhandledExceptionFilter(NULL);
SetUnhandledExceptionFilter(m_pUnhandled);
m_handler = AddVectoredExceptionHandler(1, &VectoredExceptionHandler);
std::set_terminate(&TerminateHandler);
}
CrashHandler::~CrashHandler()
{
}
void CrashHandler::RemovePreviousDump(std::filesystem::path path)
{
for (auto& entry : std::filesystem::directory_iterator(path))
{
if (entry.path().string().find("crash") != std::string::npos)
{
DeleteFileA(entry.path().string().c_str());
}
}
}