-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathThreadManager.cs
More file actions
435 lines (345 loc) · 15 KB
/
Copy pathThreadManager.cs
File metadata and controls
435 lines (345 loc) · 15 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shared.VSCodeDebugProtocol;
using Microsoft.VisualStudio.Shared.VSCodeDebugProtocol.Messages;
using Ookii.CommandLine;
using static System.FormattableString;
namespace SampleDebugAdapter
{
internal class ThreadManager
{
private SampleDebugAdapter adapter;
private List<SampleThread> threads;
internal ThreadManager(SampleDebugAdapter adapter)
{
this.adapter = adapter;
this.threads = new List<SampleThread>();
this.adapter.RegisterDirective<StartThreadArgs>("StartThread", this.DoStartThread);
this.adapter.RegisterDirective<EndThreadArgs>("EndThread", this.DoEndThread);
this.adapter.RegisterDirective<PushStackFrameArgs>("PushStackFrame", this.DoPushStackFrame);
this.adapter.RegisterDirective<PopStackFrameArgs>("PopStackFrame", this.DoPopStackFrame);
this.adapter.RegisterDirective<DefineScopeArgs>("DefineScope", this.DoDefineScope);
this.adapter.RegisterDirective<DefineVariableArgs>("DefineVariable", this.DoDefineVariable);
}
#region Internal API
internal SampleThread StartThread(int id, string name)
{
SampleThread newThread = new SampleThread(id, name);
this.threads.Add(newThread);
this.adapter.Protocol.SendEvent(
new ThreadEvent(
reason: ThreadEvent.ReasonValue.Started,
threadId: id));
return newThread;
}
internal void EndThread(SampleThread thread)
{
this.threads.Remove(thread);
this.adapter.Protocol.SendEvent(
new ThreadEvent(
reason: ThreadEvent.ReasonValue.Exited,
threadId: thread.Id));
}
internal SampleThread GetThread(int threadId)
{
return this.threads.FirstOrDefault(t => t.Id == threadId);
}
internal SampleStackFrame GetStackFrame(int stackFrameId)
{
return this.threads.SelectMany(t => t.StackFrames).FirstOrDefault(f => f.Id == stackFrameId);
}
internal ISampleVariableContainer GetVariableContainer(int variablesReference)
{
return this.threads
.SelectMany(t => t.StackFrames)
.SelectMany(f => f.MergedScopes)
.Select(s => this.FindVariableReference(s, variablesReference))
.FirstOrDefault(c => c != null);
}
private ISampleVariableContainer FindVariableReference(ISampleVariableContainer container, int variablesReference)
{
if (container.VariableReference == variablesReference)
{
return container;
}
if (container.ChildContainers != null)
{
foreach (ISampleVariableContainer childContainer in container.ChildContainers)
{
ISampleVariableContainer found = this.FindVariableReference(childContainer, variablesReference);
if (found != null)
{
return found;
}
}
}
return null;
}
internal void Invalidate()
{
foreach (SampleThread thread in this.threads)
{
thread.Invalidate();
}
}
#endregion
#region Protocol Implementation
internal ThreadsResponse HandleThreadsRequest(ThreadsArguments args)
{
return new ThreadsResponse(threads: this.threads.Select(t => t.GetProtocolThread()).ToList());
}
internal ScopesResponse HandleScopesRequest(ScopesArguments args)
{
SampleStackFrame frame = this.GetStackFrame(args.FrameId);
if (frame == null)
{
throw new ProtocolException(Invariant($"Invalid frame id '{args.FrameId}'!"));
}
return frame.HandleScopesRequest(args);
}
internal StackTraceResponse HandleStackTraceRequest(StackTraceArguments args)
{
SampleThread thread = this.GetThread(args.ThreadId);
if (thread == null)
{
throw new ProtocolException(Invariant($"Invalid thread id '{args.ThreadId}'!"));
}
return thread.HandleStackTraceRequest(args);
}
internal VariablesResponse HandleVariablesRequest(VariablesArguments args)
{
ISampleVariableContainer container = this.GetVariableContainer(args.VariablesReference);
if (container == null)
{
throw new ProtocolException(Invariant($"Invalid variable reference '{args.VariablesReference}'!"));
}
return container.HandleVariablesRequest(args);
}
internal SetVariableResponse HandleSetVariableRequest(SetVariableArguments args)
{
ISampleVariableContainer container = this.GetVariableContainer(args.VariablesReference);
if (container == null)
{
throw new ProtocolException(Invariant($"Invalid variable reference '{args.VariablesReference}'!"));
}
return container.HandleSetVariableRequest(args);
}
internal EvaluateResponse HandleEvaluateRequest(EvaluateArguments args)
{
if (!args.FrameId.HasValue)
{
throw new ProtocolException("Evaluation without a frame id is not supported!");
}
SampleStackFrame frame = this.GetStackFrame(args.FrameId.Value);
if (frame == null)
{
throw new ProtocolException(Invariant($"Invalid frame id '{args.FrameId.Value}'!"));
}
return frame.HandleEvaluateRequest(args);
}
internal SetExpressionResponse HandleSetExpressionRequest(SetExpressionArguments args)
{
if (!args.FrameId.HasValue)
{
throw new ProtocolException("Evaluation without a frame id is not supported!");
}
SampleStackFrame frame = this.GetStackFrame(args.FrameId.Value);
if (frame == null)
{
throw new ProtocolException(Invariant($"Invalid frame id '{args.FrameId.Value}'!"));
}
return frame.HandleSetExpressionRequest(args);
}
#endregion
#region StartThread Directive
private class StartThreadArgs
{
[CommandLineArgument("id", IsRequired = true, ValueDescription = "thread id")]
public int Id { get; set; }
[CommandLineArgument("name", ValueDescription = "thread name")]
public string Name { get; set; }
}
private bool DoStartThread(StartThreadArgs args, StringBuilder output)
{
if (this.threads.Any(t => t.Id == args.Id))
{
output.AppendLine(Invariant($"Error: Thread with id '{args.Id}' already exists!"));
return false;
}
this.StartThread(args.Id, args.Name);
return true;
}
#endregion
#region EndThread Directive
private class EndThreadArgs
{
[CommandLineArgument("id", IsRequired = true, ValueDescription = "thread id")]
public int Id { get; set; }
}
private bool DoEndThread(EndThreadArgs args, StringBuilder output)
{
if (args.Id == 0)
{
output.AppendLine("Error: Main thread cannot be ended!");
return false;
}
SampleThread thread = this.threads.FirstOrDefault(t => t.Id == args.Id);
if (thread == null)
{
output.AppendLine(Invariant($"Error: No thread with id '{args.Id}'!"));
return false;
}
this.EndThread(thread);
return true;
}
#endregion
#region PushStackFrame Directive
private class PushStackFrameArgs
{
[CommandLineArgument("name", IsRequired = true, ValueDescription = "frame name")]
public string Name { get; set; }
[CommandLineArgument("moduleid", IsRequired = false, ValueDescription = "module id")]
public string ModuleId { get; set; }
[CommandLineArgument("threadid", IsRequired = false, DefaultValue = 0, ValueDescription = "thread id")]
public int ThreadId { get; set; }
[CommandLineArgument("argname", IsRequired = false, ValueDescription = "argument name")]
public string[] ArgName { get; set; }
[CommandLineArgument("argtype", IsRequired = false, ValueDescription = "argument type")]
public string[] ArgType { get; set; }
[CommandLineArgument("argvalue", IsRequired = false, ValueDescription = "argument value")]
public string[] ArgValue { get; set; }
}
private bool DoPushStackFrame(PushStackFrameArgs args, StringBuilder output)
{
List<SampleFunctionArgument> stackArgs = null;
if (args.ArgName != null && args.ArgName.Length > 0)
{
if (args.ArgType == null || args.ArgType.Length != args.ArgName.Length ||
args.ArgValue == null || args.ArgValue.Length != args.ArgName.Length)
{
output.AppendLine("Error: Must have same number of -argName, -argType, and -argValue arguments!");
return false;
}
stackArgs = new List<SampleFunctionArgument>();
for (int i = 0; i < args.ArgName.Length; i++)
{
stackArgs.Add(new SampleFunctionArgument(args.ArgType[i], args.ArgName[i], args.ArgValue[i]));
}
}
SampleModule module = null;
if (!String.IsNullOrEmpty(args.ModuleId))
{
module = this.adapter.ModuleManager.GetModuleById(args.ModuleId);
if (module == null)
{
output.AppendLine(Invariant($"Error: Unknown module id '{args.ModuleId}'!"));
return false;
}
}
SampleThread targetThread = this.GetThread(args.ThreadId);
if (targetThread == null)
{
output.AppendLine(Invariant($"Error: Unknown thread id '{args.ThreadId}'!"));
return false;
}
targetThread.PushStackFrame(new SampleStackFrame(this.adapter, module, args.Name, stackArgs, this.adapter.Source.Path, 0, 0));
return true;
}
#endregion
#region PopStackFrame Directive
private class PopStackFrameArgs
{
[CommandLineArgument("threadid", IsRequired = false, DefaultValue = 0, ValueDescription = "thread id")]
public int ThreadId { get; set; }
}
private bool DoPopStackFrame(PopStackFrameArgs args, StringBuilder output)
{
SampleThread targetThread = this.GetThread(args.ThreadId);
if (targetThread == null)
{
output.AppendLine(Invariant($"Error: Unknown thread id '{args.ThreadId}'!"));
return false;
}
targetThread.PopStackFrame();
return true;
}
#endregion
#region DefineScope Directive
private class DefineScopeArgs
{
[CommandLineArgument("name", IsRequired = true, Position = 0)]
public string Name { get; set; }
[CommandLineArgument("thread", IsRequired = false, DefaultValue = 0)]
public int ThreadId { get; set; }
[CommandLineArgument("expensive", IsRequired = false, DefaultValue = false)]
public bool Expensive { get; set; }
}
private bool DoDefineScope(DefineScopeArgs args, StringBuilder output)
{
SampleThread thread = this.GetThread(args.ThreadId);
if (thread == null)
{
output.AppendLine(Invariant($"Error: Invalid thread id '{args.ThreadId}'!"));
return false;
}
SampleStackFrame frame = thread.GetTopStackFrame();
if (frame == null)
{
output.AppendLine(Invariant($"Error: Thread '{args.ThreadId}' has no stack frames!"));
return false;
}
if (frame.AllScopes.Any(s => String.Equals(s.Name, args.Name, StringComparison.OrdinalIgnoreCase)))
{
output.AppendLine(Invariant($"Error: Selected stack frame already has a scope called '{args.Name}'!"));
return false;
}
SampleScope scope = new SampleScope(this.adapter, args.Name, args.Expensive);
frame.AddScope(scope);
return true;
}
#endregion
#region DefineVariable Directive
private class DefineVariableArgs
{
[CommandLineArgument("name", IsRequired = true, Position = 0)]
public string Name { get; set; }
[CommandLineArgument("type", IsRequired = true, Position = 1)]
public string Type { get; set; }
[CommandLineArgument("value", IsRequired = true, Position = 2)]
public string Value { get; set; }
[CommandLineArgument("scope", IsRequired = false)]
public string Scope { get; set; }
}
private bool DoDefineVariable(DefineVariableArgs args, StringBuilder output)
{
SampleScope scope;
if (String.IsNullOrEmpty(args.Scope))
{
// Use the "Locals" scope for the top stack frame on the main thread
scope = this.GetThread(0)?.GetTopStackFrame()?.ModifiableScopes?.FirstOrDefault(s => String.Equals(s.Name, "Locals", StringComparison.OrdinalIgnoreCase));
}
else
{
// Find the requested scope
scope = this.threads.SelectMany(t => t.StackFrames).SelectMany(f => f.ModifiableScopes).FirstOrDefault(s => String.Equals(s.Name, args.Scope, StringComparison.OrdinalIgnoreCase));
}
if (scope == null)
{
output.AppendLine("Error: Unable to locate target scope!");
return false;
}
if (scope.Variables != null && scope.Variables.Any(v => String.Equals(v.Name, args.Name, StringComparison.Ordinal)))
{
output.AppendLine(Invariant($"Error: Scope '{scope.Name}' already contains a variable named '{args.Name}'!"));
return false;
}
scope.AddVariable(new SimpleVariable(this.adapter, args.Name, args.Type, args.Value));
return true;
}
#endregion
}
}