-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCDM.cpp
More file actions
152 lines (122 loc) · 3.47 KB
/
CDM.cpp
File metadata and controls
152 lines (122 loc) · 3.47 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
// rStatus.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "EuroScopePlugIn.h"
#include "CDMSingle.hpp"
extern "C" IMAGE_DOS_HEADER __ImageBase;
CDM* gpMyPlugin = NULL;
static PVOID g_vectoredHandle = nullptr;
LONG WINAPI VectoredHandler(EXCEPTION_POINTERS* pExceptionInfo);
static std::string GetTimestamp();
static std::string GetPluginFolder();
static HANDLE CreateDumpFile(std::string& outPath);
void __declspec (dllexport) EuroScopePlugInInit(EuroScopePlugIn::CPlugIn** ppPlugInInstance)
{
g_vectoredHandle = AddVectoredExceptionHandler(1, VectoredHandler);
// create the instance
*ppPlugInInstance = gpMyPlugin = new CDM();
}
//---EuroScopePlugInExit-----------------------------------------------
void __declspec (dllexport) EuroScopePlugInExit(void)
{
if (g_vectoredHandle)
{
RemoveVectoredExceptionHandler(g_vectoredHandle);
g_vectoredHandle = nullptr;
}
// delete the instance
delete gpMyPlugin;
}
LONG WINAPI VectoredHandler(EXCEPTION_POINTERS* pExceptionInfo)
{
DWORD code = pExceptionInfo->ExceptionRecord->ExceptionCode;
if (code == 0xE06D7363 || // C++ throw
code == 0x40010006 || // OutputDebugString
code == 0x406D1388 || // Thread naming
code == 0x80000003 || // Breakpoint
code == 0x80000004) // Single step
return EXCEPTION_CONTINUE_SEARCH;
static bool handled = false;
if (handled)
return EXCEPTION_CONTINUE_SEARCH;
handled = true;
std::string dumpPath;
HANDLE hFile = CreateDumpFile(dumpPath);
if (hFile != INVALID_HANDLE_VALUE)
{
MINIDUMP_EXCEPTION_INFORMATION mdei{};
mdei.ThreadId = GetCurrentThreadId();
mdei.ExceptionPointers = pExceptionInfo;
mdei.ClientPointers = FALSE;
BOOL ok = MiniDumpWriteDump(
GetCurrentProcess(),
GetCurrentProcessId(),
hFile,
static_cast<MINIDUMP_TYPE>(
MiniDumpWithDataSegs | // global variables
MiniDumpWithProcessThreadData | // thread stacks
MiniDumpWithHandleData | // open handles
MiniDumpWithThreadInfo // thread timing info
),
&mdei,
nullptr,
nullptr
);
CloseHandle(hFile);
}
return EXCEPTION_CONTINUE_SEARCH;
}
static std::string GetTimestamp()
{
std::time_t t = std::time(nullptr);
std::tm tm{};
localtime_s(&tm, &t);
std::ostringstream oss;
oss << std::put_time(&tm, "%Y%m%d_%H%M%S");
return oss.str();
}
static std::string GetPluginFolder()
{
char path[MAX_PATH];
GetModuleFileNameA((HMODULE)&__ImageBase, path, MAX_PATH);
std::string folder(path);
size_t pos = folder.find_last_of("\\/");
if (pos != std::string::npos)
folder = folder.substr(0, pos);
return folder;
}
static HANDLE CreateDumpFile(std::string& outPath)
{
std::string timestamp = GetTimestamp();
// 1. Try plugin folder
char dllPath[_MAX_PATH];
GetModuleFileNameA(HINSTANCE(&__ImageBase), dllPath, sizeof(dllPath));
string fld = dllPath;
fld.resize(fld.size() - strlen("CDM.dll"));
std::string file = fld + "\\CDM_" + timestamp + ".dmp";
HANDLE hFile = CreateFileA(
file.c_str(),
GENERIC_WRITE, 0, nullptr,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr
);
if (hFile != INVALID_HANDLE_VALUE)
{
outPath = file;
return hFile;
}
// 2. Fallback → TEMP
char tempPath[MAX_PATH];
GetTempPathA(MAX_PATH, tempPath);
file = std::string(tempPath) + "VCP_" + timestamp + ".dmp";
hFile = CreateFileA(
file.c_str(),
GENERIC_WRITE, 0, nullptr,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr
);
if (hFile != INVALID_HANDLE_VALUE)
{
outPath = file;
return hFile;
}
return INVALID_HANDLE_VALUE;
}