-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathDebugging.cpp
More file actions
104 lines (88 loc) · 3.78 KB
/
Debugging.cpp
File metadata and controls
104 lines (88 loc) · 3.78 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "Public/winget/Debugging.h"
#include "Public/AppInstallerRuntime.h"
#include "Public/AppInstallerDateTime.h"
namespace AppInstaller::Debugging
{
namespace
{
constexpr std::string_view c_minidumpPrefix = "Minidump";
constexpr std::string_view c_minidumpExtension = ".mdmp";
struct SelfInitiatedMinidumpHelper
{
SelfInitiatedMinidumpHelper() = default;
~SelfInitiatedMinidumpHelper()
{
if (!m_keepFile)
{
m_file.reset();
DeleteFile(m_filePath.wstring().c_str());
}
}
static SelfInitiatedMinidumpHelper& Instance()
{
static SelfInitiatedMinidumpHelper instance;
return instance;
}
static LONG WINAPI UnhandledExceptionCallback(EXCEPTION_POINTERS* ExceptionInfo)
{
MINIDUMP_EXCEPTION_INFORMATION exceptionInformation{};
// The unhandled exception filter is executed in the context of the failing thread.
exceptionInformation.ThreadId = GetCurrentThreadId();
exceptionInformation.ExceptionPointers = ExceptionInfo;
exceptionInformation.ClientPointers = FALSE;
std::thread([&]() {
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), Instance().m_file.get(), MiniDumpNormal, &exceptionInformation, nullptr, nullptr);
Instance().m_keepFile = true;
}).join();
return EXCEPTION_CONTINUE_SEARCH;
}
SelfInitiatedMinidumpHelper& Enable(const std::filesystem::path& filePath = {})
{
std::call_once(m_enableFlag, [&]()
{
if (filePath.empty())
{
m_filePath = Runtime::GetPathTo(Runtime::PathName::DefaultLogLocation);
m_filePath /= c_minidumpPrefix.data() + ('-' + Utility::GetCurrentTimeForFilename() + c_minidumpExtension.data());
}
else
{
m_filePath = filePath;
}
m_file.reset(CreateFile(m_filePath.wstring().c_str(), GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr));
THROW_LAST_ERROR_IF(!m_file);
SetUnhandledExceptionFilter(UnhandledExceptionCallback);
});
return *this;
}
void WriteMinidump()
{
std::thread([&]() {
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), Instance().m_file.get(), MiniDumpNormal, nullptr, nullptr, nullptr);
Instance().m_keepFile = true;
}).join();
}
private:
std::once_flag m_enableFlag;
std::filesystem::path m_filePath;
wil::unique_handle m_file;
std::atomic_bool m_keepFile{ false };
};
}
void EnableSelfInitiatedMinidump()
{
SelfInitiatedMinidumpHelper::Instance().Enable();
}
void EnableSelfInitiatedMinidump(const std::filesystem::path& filePath)
{
SelfInitiatedMinidumpHelper::Instance().Enable(filePath);
}
void WriteMinidump()
{
SelfInitiatedMinidumpHelper::Instance().Enable().WriteMinidump();
}
}