-
Notifications
You must be signed in to change notification settings - Fork 9
/
windebugmonitor.cpp
234 lines (188 loc) · 5.77 KB
/
windebugmonitor.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
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
#include "windebugmonitor.h"
#include "atlbase.h"
#include "atlstr.h"
WinDebugMonitor::WinDebugMonitor() {
m_hDBWinMutex = NULL;
m_hDBMonBuffer = NULL;
m_hEventBufferReady = NULL;
m_hEventDataReady = NULL;
m_hWinDebugMonitorThread = NULL;
m_bWinDebugMonStopped = TRUE;
m_pDBBuffer = NULL;
}
WinDebugMonitor::~WinDebugMonitor() {
Unintialize();
}
DWORD WinDebugMonitor::Initialize()
{
DWORD errorCode = 0;
BOOL bSuccessful = FALSE;
SetLastError(0);
// Mutex: DBWin
// ---------------------------------------------------------
CComBSTR DBWinMutex = L"DBWinMutex";
m_hDBWinMutex = ::OpenMutex(
MUTEX_ALL_ACCESS,
FALSE,
DBWinMutex
);
if (m_hDBWinMutex == NULL) {
errorCode = GetLastError();
emit outputDebugString(-1, "Failed to create DBWinMutex");
return errorCode;
}
// Event: buffer ready
// ---------------------------------------------------------
CComBSTR DBWIN_BUFFER_READY = L"DBWIN_BUFFER_READY";
m_hEventBufferReady = ::OpenEvent(
EVENT_ALL_ACCESS,
FALSE,
DBWIN_BUFFER_READY
);
if (m_hEventBufferReady == NULL) {
m_hEventBufferReady = ::CreateEvent(
NULL,
FALSE, // auto-reset
TRUE, // initial state: signaled
DBWIN_BUFFER_READY
);
if (m_hEventBufferReady == NULL) {
errorCode = GetLastError();
emit outputDebugString(-1, "Failed to create m_hEventBufferReady");
return errorCode;
}
}
// Event: data ready
// ---------------------------------------------------------
CComBSTR DBWIN_DATA_READY = L"DBWIN_DATA_READY";
m_hEventDataReady = ::OpenEvent(
SYNCHRONIZE,
FALSE,
DBWIN_DATA_READY
);
if (m_hEventDataReady == NULL) {
m_hEventDataReady = ::CreateEvent(
NULL,
FALSE, // auto-reset
FALSE, // initial state: nonsignaled
DBWIN_DATA_READY
);
if (m_hEventDataReady == NULL) {
errorCode = GetLastError();
emit outputDebugString(-1, "Failed to create m_hEventDataReady");
return errorCode;
}
}
// Shared memory
// ---------------------------------------------------------
CComBSTR DBWIN_BUFFER = L"DBWIN_BUFFER";
m_hDBMonBuffer = ::OpenFileMapping(
FILE_MAP_READ,
FALSE,
DBWIN_BUFFER
);
if (m_hDBMonBuffer == NULL) {
m_hDBMonBuffer = ::CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
sizeof(struct dbwin_buffer),
DBWIN_BUFFER
);
if (m_hDBMonBuffer == NULL) {
errorCode = GetLastError();
emit outputDebugString(-1, "Failed to create m_hDBMonBuffer");
return errorCode;
}
}
m_pDBBuffer = (struct dbwin_buffer *)::MapViewOfFile(
m_hDBMonBuffer,
SECTION_MAP_READ,
0,
0,
0
);
if (m_pDBBuffer == NULL) {
errorCode = GetLastError();
emit outputDebugString(-1, "Failed to create m_pDBBuffer");
return errorCode;
}
// Monitoring thread
// ---------------------------------------------------------
m_bWinDebugMonStopped = FALSE;
m_hWinDebugMonitorThread = ::CreateThread(
NULL,
0,
WinDebugMonitorThread,
this,
0,
NULL
);
if (m_hWinDebugMonitorThread == NULL) {
m_bWinDebugMonStopped = TRUE;
errorCode = GetLastError();
emit outputDebugString(-1, "Failed to create m_hWinDebugMonitorThread");
return errorCode;
}
// set monitor thread's priority to highest
// ---------------------------------------------------------
bSuccessful = ::SetPriorityClass(
::GetCurrentProcess(),
REALTIME_PRIORITY_CLASS
);
bSuccessful = ::SetThreadPriority(
m_hWinDebugMonitorThread,
THREAD_PRIORITY_TIME_CRITICAL
);
//::WaitForSingleObjec(m_hWinDebugMonitorThread, INFINITE);
return errorCode;
}
void WinDebugMonitor::Unintialize()
{
if (m_hWinDebugMonitorThread != NULL) {
m_bWinDebugMonStopped = TRUE;
::WaitForSingleObject(m_hWinDebugMonitorThread, INFINITE);
}
if (m_hDBWinMutex != NULL) {
CloseHandle(m_hDBWinMutex);
m_hDBWinMutex = NULL;
}
if (m_hDBMonBuffer != NULL) {
::UnmapViewOfFile(m_pDBBuffer);
CloseHandle(m_hDBMonBuffer);
m_hDBMonBuffer = NULL;
}
if (m_hEventBufferReady != NULL) {
CloseHandle(m_hEventBufferReady);
m_hEventBufferReady = NULL;
}
if (m_hEventDataReady != NULL) {
CloseHandle(m_hEventDataReady);
m_hEventDataReady = NULL;
}
m_pDBBuffer = NULL;
}
DWORD WINAPI WinDebugMonitor::WinDebugMonitorThread(void *pData)
{
WinDebugMonitor *_this = (WinDebugMonitor *)pData;
if (_this != NULL) {
while (!_this->m_bWinDebugMonStopped) {
_this->WinDebugMonitorProcess();
}
}
return 0;
}
DWORD WinDebugMonitor::WinDebugMonitorProcess()
{
DWORD ret = 0;
// wait for data ready
ret = ::WaitForSingleObject(m_hEventDataReady, TIMEOUT_WIN_DEBUG);
if (ret == WAIT_OBJECT_0) {
//m_pDBBuffer->dwProcessI
OutputWinDebugString(m_pDBBuffer->dwProcessId, m_pDBBuffer->data);
// signal buffer ready
SetEvent(m_hEventBufferReady);
}
return ret;
}