/
Debugger.cs
457 lines (368 loc) · 15.2 KB
/
Debugger.cs
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Diagnostics.Runtime.Interop;
namespace Microsoft.Diagnostics.Runtime.Tests.Tasks
{
internal enum ExceptionTypes : uint
{
AV = 0xC0000005,
StackOverflow = 0xC00000FD,
Cpp = 0xe06d7363,
Clr = 0xe0434352,
Break = 0x80000003
}
internal class DebuggerStartInfo
{
private readonly Dictionary<string, string> _environment = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
public void SetEnvironmentVariable(string variable, string value)
{
_environment[variable] = value;
}
public unsafe Debugger LaunchProcess(string commandLine, string workingDirectory)
{
IDebugClient5 client = CreateIDebugClient();
IDebugControl control = (IDebugControl)client;
if (string.IsNullOrEmpty(workingDirectory))
workingDirectory = Environment.CurrentDirectory;
string env = GetEnvironment();
DEBUG_CREATE_PROCESS_OPTIONS options = new DEBUG_CREATE_PROCESS_OPTIONS();
options.CreateFlags = (DEBUG_CREATE_PROCESS)1;
int hr = client.CreateProcessAndAttach2(0, commandLine, options, (uint)sizeof(DEBUG_CREATE_PROCESS_OPTIONS), workingDirectory, env, 0, DEBUG_ATTACH.DEFAULT);
if (hr < 0)
throw new Exception(Debugger.GetExceptionString("IDebugClient::CreateProcessAndAttach2", hr));
Debugger debugger = new Debugger(client, control);
hr = client.SetEventCallbacks(debugger);
if (hr < 0)
throw new Exception(Debugger.GetExceptionString("IDebugClient::SetEventCallbacks", hr));
hr = client.SetOutputCallbacks(debugger);
if (hr < 0)
throw new Exception(Debugger.GetExceptionString("IDebugClient::SetOutputCallbacks", hr));
return debugger;
}
#region Private Helpers
[DllImport("dbgeng.dll")]
private static extern int DebugCreate(in Guid InterfaceId, [MarshalAs(UnmanagedType.IUnknown)] out object Interface);
private static IDebugClient5 CreateIDebugClient()
{
Guid guid = new Guid("27fe5639-8407-4f47-8364-ee118fb08ac8");
int hr = DebugCreate(guid, out object obj);
if (hr < 0)
throw new Exception(Debugger.GetExceptionString("DebugCreate", hr));
return (IDebugClient5)obj;
}
private string GetEnvironment()
{
if (_environment.Count == 0)
return null;
StringBuilder sb = new StringBuilder();
foreach (string key in _environment.Keys)
{
sb.Append(key);
sb.Append("=");
string value = _environment[key];
if (value.Length > 0 && value.Contains(' ') && value[0] != '"' && value[^1] != '"')
value = '"' + value + '"';
sb.Append(value);
sb.Append('\0');
}
sb.Append('\0');
return sb.ToString();
}
#endregion
}
internal class Debugger : IDebugOutputCallbacks, IDebugEventCallbacks, IDisposable
{
#region Fields
private DEBUG_OUTPUT _outputMask;
private readonly StringBuilder _output = new StringBuilder();
private bool _exited;
private bool _processing;
private readonly IDebugClient5 _client;
private readonly IDebugControl _control;
#endregion
#region Events
public delegate void ModuleEventHandler(Debugger debugger, ModuleEventArgs args);
public event ModuleEventHandler ModuleLoadEvent;
public event ModuleEventHandler ModuleUnloadEvent;
public delegate void CreateThreadEventHandler(Debugger debugger, CreateThreadArgs args);
public event CreateThreadEventHandler ThreadCreateEvent;
public delegate void ExitThreadEventHandler(Debugger debugger, int exitCode);
public event ExitThreadEventHandler ExitThreadEvent;
public delegate void ExceptionEventHandler(Debugger debugger, EXCEPTION_RECORD64 ex);
public event ExceptionEventHandler FirstChanceExceptionEvent;
public event ExceptionEventHandler SecondChanceExceptionEvent;
public delegate void CreateProcessEventHandler(Debugger debugger, CreateProcessArgs args);
public event CreateProcessEventHandler CreateProcessEvent;
public delegate void ExitProcessEventHandler(Debugger debugger, int exitCode);
public event ExitProcessEventHandler ExitProcessEvent;
#endregion
public IDebugClient5 Client => _client;
public Debugger(IDebugClient5 client, IDebugControl control)
{
_client = client;
_control = control;
client.SetOutputCallbacks(this);
}
public DEBUG_STATUS ProcessEvents(uint timeout)
{
if (_processing)
throw new InvalidOperationException("Cannot call ProcessEvents reentrantly.");
if (_exited)
return DEBUG_STATUS.NO_DEBUGGEE;
_processing = true;
int hr = _control.WaitForEvent(0, timeout);
_processing = false;
if (hr < 0 && (uint)hr != 0x8000000A)
throw new Exception(GetExceptionString("IDebugControl::WaitForEvent", hr));
return GetDebugStatus();
}
public void TerminateProcess()
{
_exited = true;
_client.EndSession(DEBUG_END.ACTIVE_TERMINATE);
}
public string Execute(ulong handle, string command, string args)
{
DEBUG_OUTPUT mask = _outputMask;
_output.Clear();
_outputMask = DEBUG_OUTPUT.NORMAL | DEBUG_OUTPUT.SYMBOLS
| DEBUG_OUTPUT.ERROR | DEBUG_OUTPUT.WARNING | DEBUG_OUTPUT.DEBUGGEE;
string result = null;
try
{
int hr = _control.CallExtension(handle, command, args);
if (hr < 0)
_output.Append($"Command encountered an error. HRESULT={hr:X8}");
result = _output.ToString();
}
finally
{
_outputMask = mask;
_output.Clear();
}
return result;
}
public string Execute(string cmd)
{
DEBUG_OUTPUT mask = _outputMask;
_output.Clear();
_outputMask = DEBUG_OUTPUT.NORMAL | DEBUG_OUTPUT.SYMBOLS | DEBUG_OUTPUT.ERROR | DEBUG_OUTPUT.WARNING | DEBUG_OUTPUT.DEBUGGEE;
string result = null;
try
{
int hr = _control.Execute(DEBUG_OUTCTL.ALL_CLIENTS, cmd, DEBUG_EXECUTE.DEFAULT);
if (hr < 0)
_output.Append($"Command encountered an error. HRESULT={hr:X8}");
result = _output.ToString();
}
finally
{
_outputMask = mask;
_output.Clear();
}
return result;
}
public string ExecuteScript(string script)
{
DEBUG_OUTPUT mask = _outputMask;
_output.Clear();
_outputMask = DEBUG_OUTPUT.NORMAL | DEBUG_OUTPUT.SYMBOLS | DEBUG_OUTPUT.ERROR | DEBUG_OUTPUT.WARNING | DEBUG_OUTPUT.DEBUGGEE;
string result = null;
try
{
int hr = _control.ExecuteCommandFile(DEBUG_OUTCTL.ALL_CLIENTS, script, DEBUG_EXECUTE.DEFAULT);
if (hr < 0)
_output.Append($"Script encountered an error. HRESULT={hr:X8}");
result = _output.ToString();
}
finally
{
_outputMask = mask;
_output.Clear();
}
return result;
}
public int WriteDumpFile(string dump, DEBUG_DUMP type)
{
// DEBUG_DUMP.DEFAULT emits an older "USERDU64" format dump which conflicts with our minidump reader.
if (type == DEBUG_DUMP.DEFAULT)
return _control.Execute(DEBUG_OUTCTL.NOT_LOGGED, $".dump /ma {dump}", DEBUG_EXECUTE.DEFAULT);
return _control.Execute(DEBUG_OUTCTL.NOT_LOGGED, $".dump /m {dump}", DEBUG_EXECUTE.DEFAULT);
}
#region Helpers
private void SetDebugStatus(DEBUG_STATUS status)
{
int hr = _control.SetExecutionStatus(status);
if (hr < 0)
throw new Exception(GetExceptionString("IDebugControl::SetExecutionStatus", hr));
}
private DEBUG_STATUS GetDebugStatus()
{
DEBUG_STATUS status;
int hr = _control.GetExecutionStatus(out status);
if (hr < 0)
throw new Exception(GetExceptionString("IDebugControl::GetExecutionStatus", hr));
return status;
}
internal static string GetExceptionString(string name, int hr)
{
return $"{name} failed with hresult={hr:X8}";
}
#endregion
#region IDebugOutputCallbacks
public int Output(DEBUG_OUTPUT Mask, string Text)
{
if (_output != null && (_outputMask & Mask) != 0)
_output.Append(Text);
return 0;
}
#endregion
#region IDebugEventCallbacks
public int GetInterestMask(out DEBUG_EVENT Mask)
{
Mask = DEBUG_EVENT.BREAKPOINT | DEBUG_EVENT.CREATE_PROCESS
| DEBUG_EVENT.EXCEPTION | DEBUG_EVENT.EXIT_PROCESS
| DEBUG_EVENT.CREATE_THREAD | DEBUG_EVENT.EXIT_THREAD
| DEBUG_EVENT.LOAD_MODULE | DEBUG_EVENT.UNLOAD_MODULE;
return 0;
}
public int Breakpoint(IDebugBreakpoint Bp)
{
return (int)DEBUG_STATUS.GO;
}
public int CreateProcess(ulong ImageFileHandle, ulong Handle, ulong BaseOffset, uint ModuleSize, string ModuleName, string ImageName,
uint CheckSum, uint TimeDateStamp, ulong InitialThreadHandle, ulong ThreadDataOffset, ulong StartOffset)
{
CreateProcessEvent?.Invoke(this, new CreateProcessArgs(ImageFileHandle, Handle, BaseOffset, ModuleSize, ModuleName, ImageName, CheckSum, TimeDateStamp, InitialThreadHandle, ThreadDataOffset, StartOffset));
return 0;
}
public int ExitProcess(uint ExitCode)
{
ExitProcessEvent?.Invoke(this, (int)ExitCode);
_exited = true;
return (int)DEBUG_STATUS.BREAK;
}
public int CreateThread(ulong Handle, ulong DataOffset, ulong StartOffset)
{
ThreadCreateEvent?.Invoke(this, new CreateThreadArgs(Handle, DataOffset, StartOffset));
return 0;
}
public int ExitThread(uint ExitCode)
{
ExitThreadEvent?.Invoke(this, (int)ExitCode);
return 0;
}
public int Exception(in EXCEPTION_RECORD64 Exception, uint FirstChance)
{
((FirstChance == 1) ? FirstChanceExceptionEvent : SecondChanceExceptionEvent)?.Invoke(this, Exception);
return (int)DEBUG_STATUS.BREAK;
}
public int LoadModule(ulong ImageFileHandle, ulong BaseOffset, uint ModuleSize, string ModuleName, string ImageName, uint CheckSum, uint TimeDateStamp)
{
ModuleLoadEvent?.Invoke(this, new ModuleEventArgs(ImageFileHandle, BaseOffset, ModuleSize, ModuleName, ImageName, CheckSum, TimeDateStamp));
return 0;
}
public int UnloadModule(string ImageBaseName, ulong BaseOffset)
{
ModuleUnloadEvent?.Invoke(this, new ModuleEventArgs(ImageBaseName, BaseOffset));
return 0;
}
public int SessionStatus(DEBUG_SESSION Status)
{
throw new NotImplementedException();
}
public int SystemError(uint Error, uint Level)
{
throw new NotImplementedException();
}
public int ChangeDebuggeeState(DEBUG_CDS Flags, ulong Argument)
{
throw new NotImplementedException();
}
public int ChangeEngineState(DEBUG_CES Flags, ulong Argument)
{
throw new NotImplementedException();
}
public int ChangeSymbolState(DEBUG_CSS Flags, ulong Argument)
{
throw new NotImplementedException();
}
public void Dispose()
{
_client.SetEventCallbacks(null);
_client.SetOutputCallbacks(null);
}
#endregion
}
internal class ModuleEventArgs
{
public ulong ImageFileHandle;
public ulong BaseOffset;
public uint ModuleSize;
public string ModuleName;
public string ImageName;
public uint CheckSum;
public uint TimeDateStamp;
public ModuleEventArgs(string imageBaseName, ulong baseOffset)
{
ImageName = imageBaseName;
BaseOffset = baseOffset;
}
public ModuleEventArgs(ulong ImageFileHandle, ulong BaseOffset, uint ModuleSize, string ModuleName, string ImageName, uint CheckSum, uint TimeDateStamp)
{
this.ImageFileHandle = ImageFileHandle;
this.BaseOffset = BaseOffset;
this.ModuleSize = ModuleSize;
this.ModuleName = ModuleName;
this.ImageName = ImageName;
this.CheckSum = CheckSum;
this.TimeDateStamp = TimeDateStamp;
}
}
internal class CreateThreadArgs
{
public ulong Handle;
public ulong DataOffset;
public ulong StartOffset;
public CreateThreadArgs(ulong handle, ulong data, ulong start)
{
Handle = handle;
DataOffset = data;
StartOffset = start;
}
}
internal class CreateProcessArgs
{
public ulong ImageFileHandle;
public ulong Handle;
public ulong BaseOffset;
public uint ModuleSize;
public string ModuleName;
public string ImageName;
public uint CheckSum;
public uint TimeDateStamp;
public ulong InitialThreadHandle;
public ulong ThreadDataOffset;
public ulong StartOffset;
public CreateProcessArgs(ulong ImageFileHandle, ulong Handle, ulong BaseOffset, uint ModuleSize, string ModuleName, string ImageName,
uint CheckSum, uint TimeDateStamp, ulong InitialThreadHandle, ulong ThreadDataOffset, ulong StartOffset)
{
this.ImageFileHandle = ImageFileHandle;
this.Handle = Handle;
this.BaseOffset = BaseOffset;
this.ModuleSize = ModuleSize;
this.ModuleName = ModuleName;
this.ImageName = ImageName;
this.CheckSum = CheckSum;
this.TimeDateStamp = TimeDateStamp;
this.InitialThreadHandle = InitialThreadHandle;
this.ThreadDataOffset = ThreadDataOffset;
this.StartOffset = StartOffset;
}
}
}