-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCrashDump.cpp
63 lines (50 loc) · 1.83 KB
/
CrashDump.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
// Windows crash dump support, to save a minidump image for debugging
#include <windows.h>
#include <DbgHelp.h>
#include <string>
#pragma comment(lib, "DbgHelp.lib")
BOOL CreateMiniDump (EXCEPTION_POINTERS* pep)
{
BOOL ret = false;
WCHAR szModule[MAX_PATH];
GetModuleFileName(nullptr, szModule, ARRAYSIZE(szModule));
std::wstring path = szModule;
auto dumppath = path.substr(0, path.find_last_of(L'.') + 1) + L"dmp";
HANDLE hfile = CreateFile(dumppath.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
// If the location is read-only, create in the temporary directory instead
if (hfile == INVALID_HANDLE_VALUE)
{
GetTempPath(MAX_PATH, szModule);
auto module = path.substr(path.find_last_of(L'\\') + 1);
auto dumpfile = module.substr(0, module.find_last_of(L'.') + 1) + L"dmp";
dumppath = std::wstring(szModule) + dumpfile;
hfile = CreateFile(dumppath.c_str(), GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
}
if (hfile != INVALID_HANDLE_VALUE)
{
MINIDUMP_EXCEPTION_INFORMATION mdei;
mdei.ThreadId = GetCurrentThreadId();
mdei.ExceptionPointers = pep;
mdei.ClientPointers = TRUE;
ret = MiniDumpWriteDump(GetCurrentProcess(),
GetCurrentProcessId(),
hfile,
MiniDumpWithDataSegs, //MiniDumpWithFullMemory,
pep ? &mdei : nullptr,
nullptr,
nullptr);
// Close the file
CloseHandle(hfile);
if (!ret)
DeleteFile(path.c_str());
auto message = L"Diagnostics file saved to:\n\n" + dumppath + L"\n\nPlease report the crash with this file.";
MessageBox(NULL, message.c_str(), L"Crashed", MB_ICONSTOP);
}
return ret;
}
LONG WINAPI CrashDumpUnhandledExceptionFilter (EXCEPTION_POINTERS * ExceptionInfo)
{
SetUnhandledExceptionFilter(nullptr);
CreateMiniDump(ExceptionInfo);
return EXCEPTION_EXECUTE_HANDLER;
}