-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
eventredirectionpipeline.cpp
349 lines (280 loc) · 9.8 KB
/
eventredirectionpipeline.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//*****************************************************************************
// File: EventRedirectionPipeline.cpp
//
//
// Implement a native pipeline that redirects events.
//*****************************************************************************
#include "stdafx.h"
#include "nativepipeline.h"
#include "sstring.h"
#if defined(ENABLE_EVENT_REDIRECTION_PIPELINE)
#include "eventredirection.h"
#include "eventredirectionpipeline.h"
// Constructor
EventRedirectionPipeline::EventRedirectionPipeline()
{
m_pBlock = NULL;
m_dwProcessId = 0;
InitConfiguration();
}
// Dtor
EventRedirectionPipeline::~EventRedirectionPipeline()
{
CloseBlock();
}
// Call to free up the pipeline.
void EventRedirectionPipeline::Delete()
{
delete this;
}
//---------------------------------------------------------------------------------------
//
// Returns true if the Redirection is enabled.
//
// Arguments:
// szOptions - specific Create/attach options to include in the overal format string
// pidTarget - pid of real debuggeee.
//
// Return Value:
// S_OK on success.
//
//
// Notes:
// This will spin up an auxiliary debugger (windbg) and attach it to the existing
// process. If this is a create case, then we're attaching to a create-suspended process.
//
//---------------------------------------------------------------------------------------
void EventRedirectionPipeline::InitConfiguration()
{
// We need some config strings. See header for possible values.
m_DebuggerCmd = CLRConfig::GetConfigValue(CLRConfig::EXTERNAL_DbgRedirectApplication);
m_AttachParams = CLRConfig::GetConfigValue(CLRConfig::EXTERNAL_DbgRedirectAttachCmd);
m_CreateParams = CLRConfig::GetConfigValue(CLRConfig::EXTERNAL_DbgRedirectCreateCmd);
m_CommonParams = CLRConfig::GetConfigValue(CLRConfig::EXTERNAL_DbgRedirectCommonCmd);
}
// Implement INativeEventPipeline::DebugSetProcessKillOnExit
BOOL EventRedirectionPipeline::DebugSetProcessKillOnExit(bool fKillOnExit)
{
// Not implemented for redirection pipeline. That's ok. Redirection pipeline doesn't care.
// Return success so caller can assert for other pipeline types.
return TRUE;
}
//---------------------------------------------------------------------------------------
//
// Attach a real debugger to the target.
//
// Arguments:
// szOptions - specific Create/attach options to include in the overal format string
// pidTarget - pid of real debuggeee.
//
// Return Value:
// S_OK on success.
//
//
// Notes:
// This will spin up an auxiliary debugger (windbg) and attach it to the existing
// process. If this is a create case, then we're attaching to a create-suspended process.
//
//---------------------------------------------------------------------------------------
HRESULT EventRedirectionPipeline::AttachDebuggerToTarget(LPCWSTR szOptions, DWORD pidTarget)
{
SString s;
BOOL fRemap = false;
LPCWSTR lpApplicationName = NULL;
LPCWSTR lpCommandLine = NULL;
EX_TRY
{
m_pBlock = new (nothrow) RedirectionBlock(); // $$ make throwing
ZeroMemory(m_pBlock, sizeof(RedirectionBlock));
// Initialize
m_pBlock->m_versionCookie = EVENT_REDIRECTION_CURRENT_VERSION;
s.Printf(m_CommonParams, GetCurrentProcessId(), m_pBlock, szOptions, pidTarget);
lpCommandLine = s.GetUnicode();
lpApplicationName = m_DebuggerCmd; // eg, something like L"c:\\debuggers_amd64\\windbg.exe";
// Initialize events.
const BOOL kManualResetEvent = TRUE;
const BOOL kAutoResetEvent = FALSE;
m_pBlock->m_hEventAvailable = WszCreateEvent(NULL, kAutoResetEvent, FALSE, NULL);
m_pBlock->m_hEventConsumed = WszCreateEvent(NULL, kAutoResetEvent, FALSE, NULL);
m_pBlock->m_hDetachEvent = WszCreateEvent(NULL, kManualResetEvent, FALSE, NULL);
fRemap = true;
}EX_CATCH {}
EX_END_CATCH(SwallowAllExceptions)
if (!fRemap)
{
return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
}
STARTUPINFO startupInfo = {0};
startupInfo.cb = sizeof (STARTUPINFOW);
PROCESS_INFORMATION procInfo = {0};
// Now create the debugger
BOOL fStatus = WszCreateProcess(
lpApplicationName,
lpCommandLine,
NULL,
NULL,
FALSE,
0, // flags
NULL,
NULL,
&startupInfo,
&procInfo);
if (!fStatus)
{
return HRESULT_FROM_GetLastError();
}
CloseHandle(procInfo.hProcess);
CloseHandle(procInfo.hThread);
return S_OK;
}
//---------------------------------------------------------------------------------------
//
// Close the event block
//
// Notes:
// This can be called multiple times.
//---------------------------------------------------------------------------------------
void EventRedirectionPipeline::CloseBlock()
{
if (m_pBlock == NULL)
{
return;
}
// Close our handle to the IPC events. When server closes its handles, OS will free the events.
// Setting the detach event signals the Server that this block is closing.
if (m_pBlock->m_hDetachEvent != NULL)
{
SetEvent(m_pBlock->m_hDetachEvent);
CloseHandle(m_pBlock->m_hDetachEvent);
}
if (m_pBlock->m_hEventAvailable != NULL)
{
CloseHandle(m_pBlock->m_hEventAvailable);
}
if (m_pBlock->m_hEventConsumed != NULL)
{
CloseHandle(m_pBlock->m_hEventConsumed);
}
delete m_pBlock;
m_pBlock = NULL;
}
// Wait for a debug event
BOOL EventRedirectionPipeline::WaitForDebugEvent(DEBUG_EVENT * pEvent, DWORD dwTimeout, CordbProcess * pProcess)
{
// Get debug event via Redirection from control block
DWORD res = WaitForSingleObject(m_pBlock->m_hEventAvailable, dwTimeout);
if (res == WAIT_TIMEOUT)
{
// No event is available.
return FALSE;
}
pEvent->dwDebugEventCode = EXCEPTION_DEBUG_EVENT;
pEvent->dwProcessId = m_pBlock->m_dwProcessId;
pEvent->dwThreadId = m_pBlock->m_dwThreadId;
pEvent->u.Exception.dwFirstChance = m_pBlock->m_dwFirstChance;
_ASSERTE(sizeof(m_pBlock->m_record) == sizeof(pEvent->u.Exception.ExceptionRecord));
memcpy(&pEvent->u.Exception.ExceptionRecord, &m_pBlock->m_record, sizeof(m_pBlock->m_record));
// We've got an event!
return TRUE;
}
// Continue a debug event
BOOL EventRedirectionPipeline::ContinueDebugEvent(
DWORD dwProcessId,
DWORD dwThreadId,
DWORD dwContinueStatus
)
{
m_pBlock->m_ContinuationStatus = dwContinueStatus;
m_pBlock->m_counterConsumed++;
// Sanity check the block. If these checks fail, then the block is corrupted (perhaps a issue in the
// extension dll feeding us the events?).
_ASSERTE(dwProcessId == m_pBlock->m_dwProcessId);
_ASSERTE(dwThreadId == m_pBlock->m_dwThreadId);
_ASSERTE(m_pBlock->m_counterAvailable == m_pBlock->m_counterConsumed);
SetEvent(m_pBlock->m_hEventConsumed);
return TRUE;
}
// Create
HRESULT EventRedirectionPipeline::CreateProcessUnderDebugger(
MachineInfo machineInfo,
LPCWSTR lpApplicationName,
LPCWSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCWSTR lpCurrentDirectory,
LPSTARTUPINFOW lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation)
{
DWORD dwRealCreationFlags = dwCreationFlags;
dwRealCreationFlags |= CREATE_SUSPENDED;
dwRealCreationFlags &= ~(DEBUG_ONLY_THIS_PROCESS | DEBUG_PROCESS);
// We must create the real process so that startup info and process information are correct.
BOOL fStatus = WszCreateProcess(
lpApplicationName,
lpCommandLine,
lpProcessAttributes,
lpThreadAttributes,
bInheritHandles,
dwRealCreationFlags,
lpEnvironment,
lpCurrentDirectory,
lpStartupInfo,
lpProcessInformation);
if (!fStatus)
{
return HRESULT_FROM_GetLastError();
}
// Attach the real debugger.
AttachDebuggerToTarget(m_CreateParams, lpProcessInformation->dwProcessId);
m_dwProcessId = lpProcessInformation->dwProcessId;
return S_OK;
}
// Attach
HRESULT EventRedirectionPipeline::DebugActiveProcess(MachineInfo machineInfo, const ProcessDescriptor& processDescriptor)
{
m_dwProcessId = processDescriptor.m_Pid;
// Use redirected pipeline
// Spin up debugger to attach to target.
return AttachDebuggerToTarget(m_AttachParams, processDescriptor.m_Pid);
}
// Detach
HRESULT EventRedirectionPipeline::DebugActiveProcessStop(DWORD processId)
{
// Use redirected pipeline
SetEvent(m_pBlock->m_hDetachEvent);
CloseBlock();
// Assume detach can't fail (true on WinXP and above)
return S_OK;
}
// Return a handle for the debuggee process.
HANDLE EventRedirectionPipeline::GetProcessHandle()
{
_ASSERTE(m_dwProcessId != 0);
return ::OpenProcess(PROCESS_DUP_HANDLE |
PROCESS_QUERY_INFORMATION |
PROCESS_TERMINATE |
PROCESS_VM_OPERATION |
PROCESS_VM_READ |
PROCESS_VM_WRITE |
SYNCHRONIZE,
FALSE,
m_dwProcessId);
}
// Terminate the debuggee process.
BOOL EventRedirectionPipeline::TerminateProcess(UINT32 exitCode)
{
_ASSERTE(m_dwProcessId != 0);
// Get a process handle for the process ID.
HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, m_dwProcessId);
if (hProc == NULL)
{
return FALSE;
}
return ::TerminateProcess(hProc, exitCode);
}
#endif // ENABLE_EVENT_REDIRECTION_PIPELINE