-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDebugger.cs
More file actions
435 lines (369 loc) · 11.5 KB
/
Copy pathDebugger.cs
File metadata and controls
435 lines (369 loc) · 11.5 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Diagnostics.Runtime.Interop;
using static PSExt.ErrorHelper;
namespace PSExt
{
// ReSharper disable SuspiciousTypeConversion.Global
internal class Debugger : IDebugger
{
private readonly IDebugClient6 _client;
private readonly IDebugControl5 _control5;
private readonly IDebugAdvanced2 _advanced2;
private SystemObjects _systemObjects;
private BreakpointManager _breakpointManager;
private Symbols _symbols;
private DebuggerThread _thread;
public BreakpointManager BreakpointManager => _breakpointManager ?? (_breakpointManager = new BreakpointManager(_client));
public Symbols Symbols => _symbols ?? (_symbols = new Symbols((IDebugSymbols3)_client));
public SystemObjects SystemObjects => _systemObjects ?? (_systemObjects = new SystemObjects((IDebugSystemObjects2)_client));
public DebuggerThread DebuggerThread => _thread ?? (_thread = new DebuggerThread(_client, Symbols));
public Debugger(IDebugClient client)
{
_client = (IDebugClient6)client;
_control5 = (IDebugControl5)client;
_advanced2 = (IDebugAdvanced2)client;
//_client.SetOutputCallbacksWide(new NullDebugOutput());
}
public string ExecuteCommand(string command)
{
IDebugOutputCallbacksWide oldCallbacks;
_client.GetOutputCallbacksWide(out oldCallbacks);
var output = new DebugOutput();
try
{
_client.SetOutputCallbacksWide(output);
_control5.Execute(DEBUG_OUTCTL.THIS_CLIENT, command, DEBUG_EXECUTE.DEFAULT);
return output.Text;
}
finally
{
_client.SetOutputCallbacksWide(oldCallbacks);
}
}
class InputCallbacks : IDebugInputCallbacks
{
private readonly IDebugControl5 _control;
public InputCallbacks(IDebugControl5 control)
{
_control = control;
}
public int StartInput(uint bufferSize)
{
return 0;
}
public int EndInput()
{
return 0;
}
}
public string ReadLine()
{
var builder = new StringBuilder(1024);
uint inputSize;
var res =_control5.InputWide(builder, builder.Capacity, out inputSize);
if (res != 0)
{
ThrowDebuggerException(res, "IDebugControl5.InputWide");
}
return builder.ToString();
}
public void Write(string value)
{
_control5.Output(DEBUG_OUTPUT.NORMAL, value);
}
public IList<BreakpointData> GetBreakpoints()
{
return BreakpointManager.GetBreakpoints();
}
public IList<BreakpointData> AddBreakpoints(BreakpointData data)
{
return BreakpointManager.AddBreakpoints(data);
}
public IList<ModuleData> GetModules()
{
var modules = new List<ModuleData>(50);
UInt32 loaded;
UInt32 unloaded;
var symbols = (IDebugSymbols)_client;
int res = symbols.GetNumberModules(out loaded, out unloaded);
if (res != 0)
{
ThrowDebuggerException(res, "IDebugSymbols.GetNumberModules");
}
for (UInt32 i = 0; i < loaded; ++i)
{
ulong moduleBase;
int status;
if ((status = symbols.GetModuleByIndex(i, out moduleBase)) != 0)
{
ThrowDebuggerException(status, "IDebugSymbols.GetModuleByIndex");
}
var buffer = new byte[Marshal.SizeOf<IMAGEHLP_MODULEW64>()];
var sizeAsBytes = BitConverter.GetBytes(buffer.Length);
Array.Copy(sizeAsBytes, buffer, sizeAsBytes.Length);
int infoSize;
var bufferSize = buffer.Length;
if ((status = _advanced2.GetSymbolInformation(DEBUG_SYMINFO.IMAGEHLP_MODULEW64, moduleBase, 0, buffer, bufferSize, out infoSize, null, 0, IntPtr.Zero)) != 0)
{
ThrowDebuggerException(status, "IDebugSymbols.GetSymbolInformation");
}
var versionInfo = Symbols.GetModuleVersionInfo(i);
var moduleParams = Symbols.GetModuleParameters(i);
var md = ToModuleData(buffer, versionInfo, moduleParams);
modules.Add(md);
}
return modules;
}
private unsafe ModuleData ToModuleData(byte[] moduleInfoBuffer, ModuleVersionInfo versionInfo, DEBUG_MODULE_PARAMETERS moduleParams)
{
fixed (byte* buf = moduleInfoBuffer)
{
IMAGEHLP_MODULEW64 mi = Marshal.PtrToStructure<IMAGEHLP_MODULEW64>(new IntPtr(buf));
return new ModuleData(mi.ModuleName, mi.ImageName, mi.LoadedImageName, mi.LoadedPdbName, mi.BaseOfImage, mi.ImageSize,
moduleParams.TimeDateStamp, moduleParams.Checksum, mi.NumSyms, (uint)mi.SymType, mi.PdbSig70, mi.PdbAge, mi.PdbUnmatched, mi.LineNumbers, mi.GlobalSymbols,
mi.TypeInfo, mi.SourceIndexed, mi.Publics, mi.MachineType, versionInfo);
}
}
public IList<DebugThread> GetCallstack(bool all)
{
IList<DebugThread> retVal;
if (all)
{
var threadInfos= SystemObjects.ThreadInfos;
retVal = new List<DebugThread>(threadInfos.Length);
foreach (var threadId in threadInfos)
{
retVal.Add(DebuggerThread.GetCallstack(threadId));
}
}
else
{
var threadInfo = SystemObjects.CurrentThreadInfo;
retVal = new List<DebugThread>(1) {DebuggerThread.GetCallstack(threadInfo)};
}
return retVal;
}
class DebugOutput : IDebugOutputCallbacksWide
{
private readonly StringBuilder _builder = new StringBuilder(1000);
public int Output(DEBUG_OUTPUT mask, string text)
{
_builder.Append(text);
return 0;
}
public string Text => _builder.ToString();
}
public IList<SymbolValue> GetVariables(StackFrame frame, int levels)
{
_systemObjects.SetCurrentThreadId(frame.Thread.ThreadNumber);
_symbols.SetScopeFrameByIndex(frame.FrameNumber);
var scope =_symbols.GetScopeSymbolGroup();
return scope.GetSymbolGroup(levels).Symbols;
}
}
class ThreadContext : IDisposable
{
public IMAGE_FILE_MACHINE Machine { get; }
private IntPtr _context;
public IntPtr Context => _context;
public ThreadContext(IMAGE_FILE_MACHINE machine)
{
Machine = machine;
_context = Marshal.AllocHGlobal((int) Size);
}
public uint Size => GetSize(Machine);
public static uint GetSize(IMAGE_FILE_MACHINE machine)
{
switch (machine)
{
case IMAGE_FILE_MACHINE.AMD64: return (uint)Marshal.SizeOf(typeof(Extension.x64.CONTEXT));
case IMAGE_FILE_MACHINE.I386: return (uint)Marshal.SizeOf(typeof(Extension.x86.CONTEXT));
default:
throw new DebuggerException(-1, "Unsupported machine type");
}
}
public static explicit operator IntPtr(ThreadContext context)
{
return context.Context;
}
public Extension.x86.CONTEXT AsX86Context => Marshal.PtrToStructure<Extension.x86.CONTEXT>(Context);
public Extension.x64.CONTEXT AsX64Context => Marshal.PtrToStructure<Extension.x64.CONTEXT>(Context);
public void Dispose()
{
Marshal.FreeHGlobal(Context);
_context = IntPtr.Zero;
}
~ThreadContext()
{
Dispose();
}
}
class DebuggerThread
{
private readonly Symbols _symbols;
private readonly IDebugControl5 _control5;
private readonly IDebugAdvanced2 _advanced2;
private readonly IDebugSystemObjects _systemObjects;
IMAGE_FILE_MACHINE GetEffectiveMachine()
{
IMAGE_FILE_MACHINE procType;
_control5.GetEffectiveProcessorType(out procType);
return procType;
}
public DebuggerThread(IDebugClient client, Symbols symbols)
{
_symbols = symbols;
_control5 = (IDebugControl5) client;
_advanced2 = (IDebugAdvanced2)client;
_systemObjects = (IDebugSystemObjects)client;
}
public ThreadContext GetThreadContext(ThreadInfo threadInfo)
{
var res = _systemObjects.SetCurrentThreadId(threadInfo.ThreadId);
if (res != 0)
{
ThrowDebuggerException(res, "IDebugSystemObjects.SetCurrentThreadId");
}
var imageFileMachine = GetEffectiveMachine();
var context = new ThreadContext(imageFileMachine);
res = _advanced2.GetThreadContext((IntPtr) context, context.Size);
if (res != 0)
{
ThrowDebuggerException(res, "IDebugAdvanced2.GetThreadContext");
}
return context;
}
private unsafe List<FrameInfo> GetCallstackFrames(ThreadContext threadContext)
{
const uint maxFrames = 1024;
var frames = new DEBUG_STACK_FRAME_EX[maxFrames];
var contextSize = threadContext.Size;
byte[] framesBuf = new byte[maxFrames * contextSize];
var machine = GetEffectiveMachine();
uint filled;
List<FrameInfo> retVal;
fixed (byte* framesContext = framesBuf)
{
int res = _control5.GetContextStackTraceEx((IntPtr) threadContext, contextSize, frames, frames.Length,
(IntPtr)framesContext, (uint)framesBuf.Length, contextSize, out filled);
if (res != 0)
{
ThrowDebuggerException(res, "IDebugControl5.GetContextStackTraceEx");
}
retVal = new List<FrameInfo>((int)filled);
}
var intContextSize = (int)contextSize;
for (var i = 0; i < filled; ++i)
{
var frameCtx = new ThreadContext(machine);
Marshal.Copy(framesBuf,i * intContextSize, (IntPtr) frameCtx, intContextSize);
retVal.Add(new FrameInfo(frames[i], frameCtx));
}
return retVal;
}
public DebugThread GetCallstack(ThreadInfo threadInfo)
{
var context = GetThreadContext(threadInfo);
var frames = GetCallstackFrames(context);
var f = new List<StackFrame>(frames.Count);
var retVal = new DebugThread(f, threadInfo);
f.AddRange(frames.Select(frame => ToStackFrame(frame, retVal)));
return retVal;
}
private StackFrame ToStackFrame(FrameInfo frameInfo, DebugThread thread)
{
ulong displacement;
var builder = new StringBuilder(256);
var frame = frameInfo.StackFrame;
_symbols.GetSymbolNameByOffset(frame.InstructionOffset, ref builder, out displacement);
var name = builder.ToString();
return new StackFrame(frame.ReturnOffset, frame.InstructionOffset, frame.FrameOffset, (ushort)frame.FrameNumber, string.Intern(name), displacement, thread);
}
}
internal struct ThreadInfo
{
public uint ThreadId;
public uint SystemThreadId;
public ThreadInfo(uint threadId, uint sysId)
{
ThreadId = threadId;
SystemThreadId = sysId;
}
}
internal class FrameInfo
{
public DEBUG_STACK_FRAME_EX StackFrame { get; }
public ThreadContext FrameContext { get; }
public FrameInfo(DEBUG_STACK_FRAME_EX stackFrame, ThreadContext frameContext)
{
StackFrame = stackFrame;
FrameContext = frameContext;
}
}
class Scope
{
//GetScopeSymbolGroup
}
internal class SystemObjects
{
private readonly IDebugSystemObjects2 _systemObjects2;
public SystemObjects(IDebugSystemObjects2 systemObjects2)
{
_systemObjects2 = systemObjects2;
}
public uint ThreadCount
{
get
{
uint retVal;
var res = _systemObjects2.GetNumberThreads(out retVal);
if (res != 0)
{
ThrowDebuggerException(res, "IDebugSystemObjects2.GetNumberThreads");
}
return retVal;
}
}
public ThreadInfo[] ThreadInfos
{
get
{
var threadCount = ThreadCount;
var debuggerThreadId = new uint[threadCount];
var threadSysIds = new uint[threadCount];
_systemObjects2.GetThreadIdsByIndex(0, threadCount, debuggerThreadId, threadSysIds);
var retVal = new ThreadInfo[threadCount];
for (uint i = 0; i < threadCount; ++i)
{
retVal[i] = new ThreadInfo(debuggerThreadId[i], threadSysIds[i]);
}
return retVal;
}
}
public ThreadInfo CurrentThreadInfo
{
get
{
uint threadId;
_systemObjects2.GetCurrentThreadId(out threadId);
uint sysId;
_systemObjects2.GetCurrentThreadSystemId(out sysId);
return new ThreadInfo(threadId, sysId);
}
}
public void SetCurrentThreadId(uint threadNumber)
{
int res = _systemObjects2.SetCurrentThreadId(threadNumber);
if (res != 0)
{
ThrowDebuggerException(res, "IDebugSystemObjects.SetCurrentThreadId");
}
}
}
}