-
Notifications
You must be signed in to change notification settings - Fork 226
/
CrashHandler.cpp
155 lines (131 loc) · 6.03 KB
/
CrashHandler.cpp
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
#include <BranchInfo.h>
#include "CrashHandler.h"
#include <DbgHelp.h>
#include <Windows.h>
#include <chrono>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <strsafe.h>
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();
}
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
if (pExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION &&
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);
#if (IS_MASTER)
volatile static bool bMiniDump = false;
#else
volatile static bool bMiniDump = true;
#endif
if (bMiniDump)
{
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
auto dumpSettings = MiniDumpWithDataSegs | MiniDumpWithProcessThreadData | MiniDumpWithHandleData |
MiniDumpWithThreadInfo |
/*
//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.");
}
}
spdlog::default_logger()->flush();
// 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();
}
}
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);
}
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());
}
}
}