-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbgeng.cs
624 lines (458 loc) · 14.5 KB
/
dbgeng.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
using System;
using System.Runtime.InteropServices;
using System.Text;
// ReSharper disable InterpolatedStringExpressionIsNotIFormattable
// ReSharper disable InconsistentNaming
// ReSharper disable UnusedMember.Global
// ReSharper disable IdentifierTypo
namespace WindbgEx
{
#region enum
[Flags]
public enum DEBUG_OUTCTL : uint
{
THIS_CLIENT = 0,
ALL_CLIENTS = 1,
ALL_OTHER_CLIENTS = 2,
IGNORE = 3,
LOG_ONLY = 4,
SEND_MASK = 7,
NOT_LOGGED = 8,
OVERRIDE_MASK = 0x10,
DML = 0x20,
AMBIENT_DML = 0xfffffffe,
AMBIENT_TEXT = 0xffffffff
}
[Flags]
public enum DEBUG_EXECUTE : uint
{
DEFAULT = 0,
ECHO = 1,
NOT_LOGGED = 2,
NO_REPEAT = 4
}
[Flags]
public enum DEBUG_OUTPUT : uint
{
NORMAL = 1,
ERROR = 2,
WARNING = 4,
VERBOSE = 8,
PROMPT = 0x10,
PROMPT_REGISTERS = 0x20,
EXTENSION_WARNING = 0x40,
DEBUGGEE = 0x80,
DEBUGGEE_PROMPT = 0x100,
SYMBOLS = 0x200
}
[Flags]
public enum DEBUG_WAIT : uint
{
DEFAULT = 0
}
public struct HResult
{
public const int S_OK = 0;
public const int S_FALSE = 1;
public const int E_FAIL = unchecked((int) 0x80004005);
public const int E_INVALIDARG = unchecked((int) 0x80070057);
public const int E_NOTIMPL = unchecked((int) 0x80004001);
public const int E_NOINTERFACE = unchecked((int) 0x80004002);
public bool IsOK
{
get { return Value == S_OK; }
}
public int Value;
public HResult(int hr)
{
Value = hr;
}
public static implicit operator HResult(int hr)
{
return new HResult(hr);
}
public static implicit operator int(HResult hr)
{
return hr.Value;
}
public static implicit operator bool(HResult hr)
{
return hr.Value >= 0;
}
public override string ToString()
{
switch (Value)
{
case S_OK: return "S_OK";
case S_FALSE: return "S_FALSE";
case E_FAIL: return "E_FAIL";
case E_INVALIDARG: return "E_INVALIDARG";
case E_NOTIMPL: return "E_NOTIMPL";
case E_NOINTERFACE: return "E_NOINTERFACE";
default: return string.Format("{0:x8}", Value);
}
}
}
#endregion
#region comimport
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("4bf58045-d654-4c40-b0af-683090f356dc")]
public interface IDebugOutputCallbacks
{
[PreserveSig]
int Output(
DEBUG_OUTPUT Mask,
[In] [MarshalAs(UnmanagedType.LPStr)] string Text);
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("27fe5639-8407-4f47-8364-ee118fb08ac8")]
public interface IDebugClient
{
/* IDebugClient */
[PreserveSig]
int AttachKernel();
[PreserveSig]
int GetKernelConnectionOptions();
[PreserveSig]
int SetKernelConnectionOptions();
[PreserveSig]
int StartProcessServer();
[PreserveSig]
int ConnectProcessServer();
[PreserveSig]
int DisconnectProcessServer();
[PreserveSig]
int GetRunningProcessSystemIds();
[PreserveSig]
int GetRunningProcessSystemIdByExecutableName();
[PreserveSig]
int GetRunningProcessDescription();
[PreserveSig]
int AttachProcess();
[PreserveSig]
int CreateProcess();
[PreserveSig]
int CreateProcessAndAttach();
[PreserveSig]
int GetProcessOptions();
[PreserveSig]
int AddProcessOptions();
[PreserveSig]
int RemoveProcessOptions();
[PreserveSig]
int SetProcessOptions();
[PreserveSig]
int OpenDumpFile(
[In] [MarshalAs(UnmanagedType.LPStr)] string DumpFile);
[PreserveSig]
int WriteDumpFile();
[PreserveSig]
int ConnectSession();
[PreserveSig]
int StartServer();
[PreserveSig]
int OutputServer();
[PreserveSig]
int TerminateProcesses();
[PreserveSig]
int DetachProcesses();
[PreserveSig]
int EndSession();
[PreserveSig]
int GetExitCode(out uint Code);
[PreserveSig]
int DispatchCallbacks();
[PreserveSig]
int ExitDispatch();
[PreserveSig]
int CreateClient();
[PreserveSig]
int GetInputCallbacks();
[PreserveSig]
int SetInputCallbacks();
/* GetOutputCallbacks could a conversion thunk from the debugger engine so we can't specify a specific interface */
[PreserveSig]
int GetOutputCallbacks(
out IDebugOutputCallbacks Callbacks);
/* We may have to pass a debugger engine conversion thunk back in so we can't specify a specific interface */
[PreserveSig]
int SetOutputCallbacks(
[In] IDebugOutputCallbacks Callbacks);
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("5182e668-105e-416e-ad92-24ef800424ba")]
public interface IDebugControl
{
/* IDebugControl */
[PreserveSig]
int GetInterrupt();
[PreserveSig]
int SetInterrupt();
[PreserveSig]
int GetInterruptTimeout(
out uint Seconds);
[PreserveSig]
int SetInterruptTimeout(
uint Seconds);
[PreserveSig]
int GetLogFile();
[PreserveSig]
int OpenLogFile();
[PreserveSig]
int CloseLogFile();
[PreserveSig]
int GetLogMask();
[PreserveSig]
int SetLogMask();
[PreserveSig]
int Input();
[PreserveSig]
int ReturnInput();
[PreserveSig]
int Output();
[PreserveSig]
int OutputVaList();
[PreserveSig]
int ControlledOutput();
[PreserveSig]
int ControlledOutputVaList();
[PreserveSig]
int OutputPrompt();
[PreserveSig]
int OutputPromptVaList();
[PreserveSig]
int GetPromptText();
[PreserveSig]
int OutputCurrentState();
[PreserveSig]
int OutputVersionInformation();
[PreserveSig]
int GetNotifyEventHandle();
[PreserveSig]
int SetNotifyEventHandle();
[PreserveSig]
int Assemble();
[PreserveSig]
int Disassemble();
[PreserveSig]
int GetDisassembleEffectiveOffset();
[PreserveSig]
int OutputDisassembly();
[PreserveSig]
int OutputDisassemblyLines();
[PreserveSig]
int GetNearInstruction();
[PreserveSig]
int GetStackTrace();
[PreserveSig]
int GetReturnOffset();
[PreserveSig]
int OutputStackTrace();
[PreserveSig]
int GetDebuggeeType();
[PreserveSig]
int GetActualProcessorType();
[PreserveSig]
int GetExecutingProcessorType();
[PreserveSig]
int GetNumberPossibleExecutingProcessorTypes();
[PreserveSig]
int GetPossibleExecutingProcessorTypes();
[PreserveSig]
int GetNumberProcessors();
[PreserveSig]
int GetSystemVersion();
[PreserveSig]
int GetPageSize();
[PreserveSig]
int IsPointer64Bit();
[PreserveSig]
int ReadBugCheckData();
[PreserveSig]
int GetNumberSupportedProcessorTypes();
[PreserveSig]
int GetSupportedProcessorTypes();
[PreserveSig]
int GetProcessorTypeNames();
[PreserveSig]
int GetEffectiveProcessorType();
[PreserveSig]
int SetEffectiveProcessorType();
[PreserveSig]
int GetExecutionStatus();
[PreserveSig]
int SetExecutionStatus();
[PreserveSig]
int GetCodeLevel();
[PreserveSig]
int SetCodeLevel();
[PreserveSig]
int GetEngineOptions();
[PreserveSig]
int AddEngineOptions();
[PreserveSig]
int RemoveEngineOptions();
[PreserveSig]
int SetEngineOptions();
[PreserveSig]
int GetSystemErrorControl();
[PreserveSig]
int SetSystemErrorControl();
[PreserveSig]
int GetTextMacro();
[PreserveSig]
int SetTextMacro();
[PreserveSig]
int GetRadix();
[PreserveSig]
int SetRadix();
[PreserveSig]
int Evaluate();
[PreserveSig]
int CoerceValue();
[PreserveSig]
int CoerceValues();
[PreserveSig]
int Execute(
DEBUG_OUTCTL OutputControl,
[In] [MarshalAs(UnmanagedType.LPStr)] string Command,
DEBUG_EXECUTE Flags);
[PreserveSig]
int ExecuteCommandFile();
[PreserveSig]
int GetNumberBreakpoints();
[PreserveSig]
int GetBreakpointByIndex();
[PreserveSig]
int GetBreakpointById();
[PreserveSig]
int GetBreakpointParameters();
[PreserveSig]
int AddBreakpoint();
[PreserveSig]
int RemoveBreakpoint();
[PreserveSig]
int AddExtension();
[PreserveSig]
int RemoveExtension();
[PreserveSig]
int GetExtensionByPath();
[PreserveSig]
int CallExtension();
[PreserveSig]
int GetExtensionFunction();
[PreserveSig]
int GetWindbgExtensionApis32();
[PreserveSig]
int GetWindbgExtensionApis64();
[PreserveSig]
int GetNumberEventFilters();
[PreserveSig]
int GetEventFilterText();
[PreserveSig]
int GetEventFilterCommand();
[PreserveSig]
int SetEventFilterCommand();
[PreserveSig]
int GetSpecificFilterParameters();
[PreserveSig]
int SetSpecificFilterParameters();
[PreserveSig]
int GetSpecificEventFilterArgument();
[PreserveSig]
int SetSpecificEventFilterArgument();
[PreserveSig]
int GetExceptionFilterParameters();
[PreserveSig]
int SetExceptionFilterParameters();
[PreserveSig]
int GetExceptionFilterSecondCommand();
[PreserveSig]
int SetExceptionFilterSecondCommand();
[PreserveSig]
int WaitForEvent(
DEBUG_WAIT Flags,
uint Timeout);
}
#endregion
public delegate int DebugOutputCallback(DEBUG_OUTPUT Mask, string Text);
public class DebugOutputCallbacks : IDebugOutputCallbacks
{
private readonly DebugOutputCallback callback;
public DebugOutputCallbacks(DebugOutputCallback callback)
{
this.callback = callback;
}
public int Output(DEBUG_OUTPUT Mask, string Text)
{
return callback(Mask, Text);
}
}
public class dbgeng : IDisposable
{
private readonly IDebugClient client;
private readonly IDebugControl control;
private readonly object executeLock = new object();
private readonly IntPtr pDebugClient;
public dbgeng()
{
var guid = new Guid("27fe5639-8407-4f47-8364-ee118fb08ac8");
HResult hr = DebugCreate(guid, out pDebugClient);
if (!hr.IsOK)
throw new Exception(string.Format("Failed to create DebugClient, hr={0:x}.", hr));
client = (IDebugClient) Marshal.GetTypedObjectForIUnknown(pDebugClient, typeof(IDebugClient));
control = (IDebugControl) Marshal.GetTypedObjectForIUnknown(pDebugClient, typeof(IDebugControl));
}
public void Dispose()
{
Marshal.Release(pDebugClient);
}
[DllImport("dbgeng.dll")]
public static extern int DebugCreate(Guid InterfaceId, out IntPtr Interface);
public string OpenDumpFile(string DumpFile)
{
HResult hr;
hr = client.OpenDumpFile(DumpFile);
if (!hr.IsOK)
return string.Format("Failed to OpenDumpFile, hr={0:x}.", hr);
hr = control.WaitForEvent(DEBUG_WAIT.DEFAULT, 60000);
if (!hr.IsOK)
return string.Format("Failed to attach to dump file, hr={0:x}.", hr);
return null;
}
public string Execute(string cmd, DebugOutputCallback callback = null)
{
HResult hr;
IDebugOutputCallbacks origCallback;
var sb = new StringBuilder();
lock (executeLock)
{
hr = client.GetOutputCallbacks(out origCallback);
if (!hr.IsOK)
origCallback = null;
if (null == callback)
callback = (m, t) =>
{
sb.Append(t);
return 0;
};
hr = client.SetOutputCallbacks(new DebugOutputCallbacks(callback));
if (!hr.IsOK)
{
sb.AppendLine(string.Format("SetOutputCallbacks failed. HRESULT={0:x}.", hr));
}
else
{
hr = control.Execute(DEBUG_OUTCTL.THIS_CLIENT, cmd, DEBUG_EXECUTE.DEFAULT);
if (!hr.IsOK) sb.AppendLine(string.Format("Command encountered an error. HRESULT={0:x}.", hr));
}
if (origCallback != null)
client.SetOutputCallbacks(origCallback);
}
return sb.ToString();
}
}
}