-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathModuleManager.cs
More file actions
304 lines (243 loc) · 10.3 KB
/
Copy pathModuleManager.cs
File metadata and controls
304 lines (243 loc) · 10.3 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shared.VSCodeDebugProtocol.Messages;
using Ookii.CommandLine;
using static System.FormattableString;
namespace SampleDebugAdapter
{
internal class ModuleManager
{
#region Private Fields
private SampleDebugAdapter adapter;
private IList<SampleModule> loadedModules;
#endregion
#region Constructor
internal ModuleManager(SampleDebugAdapter adapter)
{
this.adapter = adapter;
this.loadedModules = new List<SampleModule>();
this.adapter.RegisterDirective<LoadModuleArgs>("LoadModule", this.DoLoadModule);
this.adapter.RegisterDirective<UnloadModuleArgs>("UnloadModule", this.DoUnloadModule);
}
#endregion
#region Internal API
internal SampleModule GetModuleById(string moduleId)
{
return this.loadedModules.FirstOrDefault(m => String.Equals(m.Id, moduleId, StringComparison.Ordinal));
}
#endregion
#region Protocol Members
internal ModulesResponse HandleModulesRequest(ModulesArguments arguments)
{
IEnumerable<Module> modules = this.loadedModules.Select(m => m.GetProtocolModule());
int startModule = arguments.StartModule ?? 0;
if (startModule != 0)
{
modules = modules.Skip(startModule);
}
int moduleCount = arguments.ModuleCount ?? 0;
if (moduleCount != 0)
{
modules = modules.Take(moduleCount);
}
return new ModulesResponse(
modules: modules.ToList(),
totalModules: this.loadedModules.Count);
}
#endregion
#region LoadModule Directive
private class LoadModuleArgs
{
[CommandLineArgument("name", IsRequired = true, Position = 0, ValueDescription = "module name")]
public string ModuleName { get; set; }
[CommandLineArgument("id", IsRequired = false, ValueDescription = "module id")]
public string Id { get; set; }
[CommandLineArgument("version", IsRequired = false, ValueDescription = "version")]
public string Version { get; set; }
[CommandLineArgument("symbolstatus", IsRequired = false, ValueDescription = "symbol status")]
public string SymbolStatus { get; set; }
[CommandLineArgument("loadaddress", IsRequired = false, ValueDescription = "load address")]
public string LoadAddress { get; set; }
[CommandLineArgument("preferredloadaddress", IsRequired = false, ValueDescription = "preferred load address")]
public string PreferredLoadAddress { get; set; }
[CommandLineArgument("size", IsRequired = false, ValueDescription = "module size")]
public string Size { get; set; }
[CommandLineArgument("timestamp", IsRequired = false, ValueDescription = "symbol timestamp")]
public string Timestamp { get; set; }
[CommandLineArgument("symbolfile", IsRequired = false, ValueDescription = "symbol file")]
public string SymbolFile { get; set; }
[CommandLineArgument("is64bit", IsRequired = false, ValueDescription = "is 64-bit")]
public bool? Is64Bit { get; set; }
[CommandLineArgument("optimized", IsRequired = false, ValueDescription = "is optimized")]
public bool? IsOptimized { get; set; }
[CommandLineArgument("usercode", IsRequired = false, ValueDescription = "is user code")]
public bool? IsUserCode { get; set; }
[CommandLineArgument("appdomain", IsRequired = false, ValueDescription = "app domain")]
public string AppDomain { get; set; }
}
private bool DoLoadModule(LoadModuleArgs args, StringBuilder output)
{
SampleModule module;
if (String.IsNullOrEmpty(args.Id))
{
module = new SampleModule(args.ModuleName);
}
else
{
module = new SampleModule(args.Id, args.ModuleName);
}
bool shouldPopulateRandom = true;
if (!String.IsNullOrEmpty(args.Version))
{
module.Version = args.Version;
shouldPopulateRandom = false;
}
if (!String.IsNullOrEmpty(args.SymbolStatus))
{
module.SymbolStatus = args.SymbolStatus;
shouldPopulateRandom = false;
}
if (!String.IsNullOrEmpty(args.LoadAddress))
{
NumberStyles styles = NumberStyles.Number;
if (args.LoadAddress.StartsWith("0x", StringComparison.Ordinal))
{
args.LoadAddress = args.LoadAddress.Substring(2);
styles = NumberStyles.HexNumber;
}
ulong addr;
if (UInt64.TryParse(args.LoadAddress, styles, CultureInfo.InvariantCulture, out addr))
{
module.LoadAddress = addr;
shouldPopulateRandom = false;
}
else
{
output.AppendLine(Invariant($"Could not parse '{args.LoadAddress}' as an address!"));
return false;
}
}
if (!String.IsNullOrEmpty(args.PreferredLoadAddress))
{
NumberStyles styles = NumberStyles.Number;
if (args.PreferredLoadAddress.StartsWith("0x", StringComparison.Ordinal))
{
args.PreferredLoadAddress = args.PreferredLoadAddress.Substring(2);
styles = NumberStyles.HexNumber;
}
ulong addr;
if (UInt64.TryParse(args.PreferredLoadAddress, styles, CultureInfo.InvariantCulture, out addr))
{
module.PreferredLoadAddress = addr;
shouldPopulateRandom = false;
}
else
{
output.AppendLine(Invariant($"Could not parse '{args.PreferredLoadAddress}' as an address!"));
return false;
}
}
if (!String.IsNullOrEmpty(args.Size))
{
NumberStyles styles = NumberStyles.Number;
if (args.Size.StartsWith("0x", StringComparison.Ordinal))
{
args.Size = args.Size.Substring(2);
styles = NumberStyles.HexNumber;
}
int size;
if (Int32.TryParse(args.Size, styles, CultureInfo.InvariantCulture, out size))
{
module.Size = size;
shouldPopulateRandom = false;
}
else
{
output.AppendLine(Invariant($"Could not parse '{args.Size}' as an integer!"));
return false;
}
}
if (!String.IsNullOrEmpty(args.Timestamp))
{
DateTime timestamp;
if (DateTime.TryParse(args.Timestamp, out timestamp))
{
// We assume the provided date is in the local timezone, so convert to UTC
module.TimestampUTC = timestamp.ToUniversalTime();
shouldPopulateRandom = false;
}
else
{
output.AppendLine(Invariant($"Could not parse '{args.Timestamp}' as a date / time!"));
return false;
}
}
if (!String.IsNullOrEmpty(args.SymbolFile))
{
module.SymbolFile = args.SymbolFile;
shouldPopulateRandom = false;
}
if (args.Is64Bit.HasValue)
{
module.Is64Bit = args.Is64Bit.Value;
shouldPopulateRandom = false;
}
if (args.IsOptimized.HasValue)
{
module.IsOptimized = args.IsOptimized.Value;
shouldPopulateRandom = false;
}
if (args.IsUserCode.HasValue)
{
module.IsUserCode = args.IsUserCode.Value;
shouldPopulateRandom = false;
}
if (!String.IsNullOrEmpty(args.AppDomain))
{
module.AppDomain = args.AppDomain;
shouldPopulateRandom = false;
}
if (shouldPopulateRandom)
{
module.PopulateRandom();
}
output.AppendLine(Invariant($"Loading module '{args.ModuleName}'"));
this.loadedModules.Add(module);
this.adapter.Protocol.SendEvent(
new ModuleEvent(
reason: ModuleEvent.ReasonValue.New,
module: module.GetProtocolModule()));
return true;
}
#endregion
#region UnloadModule Directive
private class UnloadModuleArgs
{
[CommandLineArgument("name", IsRequired = true, Position = 0, ValueDescription = "module name")]
public string ModuleName { get; set; }
}
private bool DoUnloadModule(UnloadModuleArgs args, StringBuilder output)
{
SampleModule module = this.loadedModules.FirstOrDefault(m => String.Equals(m.Name, args.ModuleName, StringComparison.OrdinalIgnoreCase));
if (module == null)
{
output.AppendLine(Invariant($"Error: Unknown module '{args.ModuleName}'!"));
return false;
}
output.AppendLine(Invariant($"Unloading module '{args.ModuleName}'"));
this.adapter.Protocol.SendEvent(
new ModuleEvent(
reason: ModuleEvent.ReasonValue.Removed,
module: new Module(
id: module.Id,
name: null)));
return true;
}
#endregion
}
}