-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathAD7Thread.cs
More file actions
335 lines (300 loc) · 13.2 KB
/
AD7Thread.cs
File metadata and controls
335 lines (300 loc) · 13.2 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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.Debugger.Interop;
using System.Diagnostics;
using MICore;
namespace Microsoft.MIDebugEngine
{
// This class implements IDebugThread2 which represents a thread running in a program.
internal sealed class AD7Thread : IDebugThread2
{
private readonly AD7Engine _engine;
private readonly DebuggedThread _debuggedThread;
public int Id
{
get
{
return _debuggedThread.Id;
}
}
public AD7Thread(AD7Engine engine, DebuggedThread debuggedThread)
{
_engine = engine;
_debuggedThread = debuggedThread;
}
private ThreadContext GetThreadContext()
{
ThreadContext threadContext = null;
_engine.DebuggedProcess.WorkerThread.RunOperation(async () => threadContext = await _engine.DebuggedProcess.ThreadCache.GetThreadContext(_debuggedThread));
return threadContext;
}
private string GetCurrentLocation(bool fIncludeModuleName)
{
ThreadContext cxt = GetThreadContext();
string location = null;
if (cxt != null)
{
location = "";
if (fIncludeModuleName)
{
if (cxt.From != null)
{
location = cxt.From + '!';
}
else
{
DebuggedModule module = cxt.FindModule(_engine.DebuggedProcess);
if (module != null)
{
location = module.Name + '!';
}
}
}
if (cxt.Function == null)
{
location += _engine.GetAddressDescription(cxt.pc.Value);
}
else
{
location += cxt.Function;
}
}
return location;
}
internal DebuggedThread GetDebuggedThread()
{
return _debuggedThread;
}
#region IDebugThread2 Members
// Determines whether the next statement can be set to the given stack frame and code context.
int IDebugThread2.CanSetNextStatement(IDebugStackFrame2 stackFrame, IDebugCodeContext2 codeContext)
{
ulong addr = ((AD7MemoryAddress)codeContext).Address;
AD7StackFrame frame = ((AD7StackFrame)stackFrame);
if (frame.ThreadContext.Level != 0 || frame.Thread != this || !frame.ThreadContext.pc.HasValue)
{
return Constants.S_FALSE;
}
if (addr == frame.ThreadContext.pc)
{
return Constants.S_OK;
}
string toFunc = EngineUtils.GetAddressDescription(_engine.DebuggedProcess, addr);
string fromFunc = EngineUtils.GetAddressDescription(_engine.DebuggedProcess, frame.ThreadContext.pc.Value);
if (toFunc != fromFunc)
{
return Constants.S_FALSE;
}
return Constants.S_OK;
}
// Retrieves a list of the stack frames for this thread.
// For the sample engine, enumerating the stack frames requires walking the callstack in the debuggee for this thread
// and coverting that to an implementation of IEnumDebugFrameInfo2.
// Real engines will most likely want to cache this information to avoid recomputing it each time it is asked for,
// and or construct it on demand instead of walking the entire stack.
int IDebugThread2.EnumFrameInfo(enum_FRAMEINFO_FLAGS dwFieldSpec, uint nRadix, out IEnumDebugFrameInfo2 enumObject)
{
enumObject = null;
try
{
uint radix = _engine.CurrentRadix();
if (radix != _engine.DebuggedProcess.MICommandFactory.Radix)
{
_engine.DebuggedProcess.WorkerThread.RunOperation(async () =>
{
await _engine.UpdateRadixAsync(radix);
});
}
// get the thread's stack frames
System.Collections.Generic.List<ThreadContext> stackFrames = null;
_engine.DebuggedProcess.WorkerThread.RunOperation(async () => stackFrames = await _engine.DebuggedProcess.ThreadCache.StackFrames(_debuggedThread));
int numStackFrames = stackFrames != null ? stackFrames.Count : 0;
FRAMEINFO[] frameInfoArray;
if (numStackFrames == 0)
{
// failed to walk any frames. Return an empty stack.
frameInfoArray = new FRAMEINFO[0];
}
else
{
uint low = stackFrames[0].Level;
uint high = stackFrames[stackFrames.Count - 1].Level;
FilterUnknownFrames(stackFrames);
numStackFrames = stackFrames.Count;
frameInfoArray = new FRAMEINFO[numStackFrames];
List<ArgumentList> parameters = null;
if ((dwFieldSpec & enum_FRAMEINFO_FLAGS.FIF_FUNCNAME_ARGS) != 0 && !_engine.DebuggedProcess.MICommandFactory.SupportsFrameFormatting)
{
_engine.DebuggedProcess.WorkerThread.RunOperation(async () => parameters = await _engine.DebuggedProcess.GetParameterInfoOnly(this, (dwFieldSpec & enum_FRAMEINFO_FLAGS.FIF_FUNCNAME_ARGS_VALUES) != 0,
(dwFieldSpec & enum_FRAMEINFO_FLAGS.FIF_FUNCNAME_ARGS_TYPES) != 0, low, high));
}
for (int i = 0; i < numStackFrames; i++)
{
var p = parameters != null ? parameters.Find((ArgumentList t) => t.Item1 == stackFrames[i].Level) : null;
AD7StackFrame frame = new AD7StackFrame(_engine, this, stackFrames[i]);
frame.SetFrameInfo(dwFieldSpec, out frameInfoArray[i], p != null ? p.Item2 : null);
}
}
enumObject = new AD7FrameInfoEnum(frameInfoArray);
return Constants.S_OK;
}
catch (MIException e)
{
return e.HResult;
}
catch (Exception e)
{
return EngineUtils.UnexpectedException(e);
}
}
private void FilterUnknownFrames(System.Collections.Generic.List<ThreadContext> stackFrames)
{
bool lastWasQuestion = false;
for (int i = 0; i < stackFrames.Count;)
{
// replace sequences of "??" with one UnknownCode frame
if (stackFrames[i].Function == null || stackFrames[i].Function.Equals("??", StringComparison.Ordinal))
{
if (lastWasQuestion)
{
stackFrames.RemoveAt(i);
continue;
}
lastWasQuestion = true;
stackFrames[i] = new ThreadContext(stackFrames[i].pc, stackFrames[i].TextPosition, ResourceStrings.UnknownCode, stackFrames[i].Level, null);
}
else
{
lastWasQuestion = false;
}
i++;
}
}
// Get the name of the thread. For the sample engine, the name of the thread is always "Sample Engine Thread"
int IDebugThread2.GetName(out string threadName)
{
threadName = _debuggedThread.Name;
return Constants.S_OK;
}
// Return the program that this thread belongs to.
int IDebugThread2.GetProgram(out IDebugProgram2 program)
{
program = _engine;
return Constants.S_OK;
}
// Gets the system thread identifier.
int IDebugThread2.GetThreadId(out uint threadId)
{
threadId = _debuggedThread.TargetId;
return Constants.S_OK;
}
// Gets properties that describe a thread.
int IDebugThread2.GetThreadProperties(enum_THREADPROPERTY_FIELDS dwFields, THREADPROPERTIES[] ptp)
{
try
{
THREADPROPERTIES props = new THREADPROPERTIES();
if ((dwFields & enum_THREADPROPERTY_FIELDS.TPF_ID) != 0)
{
props.dwThreadId = _debuggedThread.TargetId;
props.dwFields |= enum_THREADPROPERTY_FIELDS.TPF_ID;
}
if ((dwFields & enum_THREADPROPERTY_FIELDS.TPF_SUSPENDCOUNT) != 0)
{
// sample debug engine doesn't support suspending threads
props.dwFields |= enum_THREADPROPERTY_FIELDS.TPF_SUSPENDCOUNT;
}
if ((dwFields & enum_THREADPROPERTY_FIELDS.TPF_STATE) != 0)
{
props.dwThreadState = (uint)enum_THREADSTATE.THREADSTATE_RUNNING;
props.dwFields |= enum_THREADPROPERTY_FIELDS.TPF_STATE;
}
if ((dwFields & enum_THREADPROPERTY_FIELDS.TPF_PRIORITY) != 0)
{
props.bstrPriority = "Normal";
props.dwFields |= enum_THREADPROPERTY_FIELDS.TPF_PRIORITY;
}
if ((dwFields & enum_THREADPROPERTY_FIELDS.TPF_NAME) != 0)
{
props.bstrName = _debuggedThread.Name;
props.dwFields |= enum_THREADPROPERTY_FIELDS.TPF_NAME;
}
if ((dwFields & enum_THREADPROPERTY_FIELDS.TPF_LOCATION) != 0)
{
props.bstrLocation = GetCurrentLocation(true);
props.dwFields |= enum_THREADPROPERTY_FIELDS.TPF_LOCATION;
if (props.bstrLocation == null)
{
// Thread deletion events may be delayed, in which case the thread object may still be present in the cache
// but the engine is unable to retrieve new data for it. So handle failure to get info for a dead thread.
props.dwThreadState = (uint)enum_THREADSTATE.THREADSTATE_DEAD;
props.dwFields |= enum_THREADPROPERTY_FIELDS.TPF_STATE;
props.bstrLocation = ResourceStrings.ThreadExited;
}
}
ptp[0] = props;
return Constants.S_OK;
}
catch (MIException e)
{
return e.HResult;
}
catch (Exception e)
{
return EngineUtils.UnexpectedException(e);
}
}
// Resume a thread.
// This is called when the user chooses "Unfreeze" from the threads window when a thread has previously been frozen.
int IDebugThread2.Resume(out uint suspendCount)
{
// The sample debug engine doesn't support suspending/resuming threads
suspendCount = 0;
return Constants.E_NOTIMPL;
}
// Sets the next statement to the given stack frame and code context.
// https://docs.microsoft.com/en-us/visualstudio/extensibility/debugger/reference/idebugthread2-setnextstatement
int IDebugThread2.SetNextStatement(IDebugStackFrame2 stackFrame, IDebugCodeContext2 codeContext)
{
// VS does provide a frame so at least do some sanity checks
AD7StackFrame frame = stackFrame as AD7StackFrame;
if (frame != null && (frame.ThreadContext.Level != 0 || frame.Thread != this))
return Constants.S_FALSE;
try
{
ulong addr = ((AD7MemoryAddress)codeContext).Address;
return _engine.Jump(addr);
}
catch (Exception)
{
return Constants.S_FALSE;
}
}
// suspend a thread.
// This is called when the user chooses "Freeze" from the threads window
int IDebugThread2.Suspend(out uint suspendCount)
{
// The sample debug engine doesn't support suspending/resuming threads
suspendCount = 0;
return Constants.E_NOTIMPL;
}
#endregion
#region Uncalled interface methods
// These methods are not currently called by the Visual Studio debugger, so they don't need to be implemented
int IDebugThread2.GetLogicalThread(IDebugStackFrame2 stackFrame, out IDebugLogicalThread2 logicalThread)
{
Debug.Fail("This function is not called by the debugger");
logicalThread = null;
return Constants.E_NOTIMPL;
}
int IDebugThread2.SetThreadName(string name)
{
Debug.Fail("This function is not called by the debugger");
return Constants.E_NOTIMPL;
}
#endregion
}
}