This repository was archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 805
Expand file tree
/
Copy pathModule.cs
More file actions
378 lines (333 loc) · 9.99 KB
/
Module.cs
File metadata and controls
378 lines (333 loc) · 9.99 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
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Linq;
using Debugger.Interop;
using Debugger.Interop.CorDebug;
using Debugger.Interop.CorSym;
using Debugger.Interop.MetaData;
using Debugger.MetaData;
using ICSharpCode.NRefactory.TypeSystem;
namespace Debugger
{
public class Module: DebuggerObject, IDisposable
{
AppDomain appDomain;
Process process;
bool unloaded = false;
string name;
string fullPath = string.Empty;
int orderOfLoading = 0;
ICorDebugModule corModule;
ISymUnmanagedReader symReader;
MetaDataImport metaData;
Task<IUnresolvedAssembly> unresolvedAssembly;
public IUnresolvedAssembly UnresolvedAssembly {
get { return unresolvedAssembly.Result; }
}
public IAssembly Assembly {
get { return this.UnresolvedAssembly.Resolve(appDomain.Compilation.TypeResolveContext); }
}
public AppDomain AppDomain {
get { return appDomain; }
}
public Process Process {
get { return process; }
}
NDebugger Debugger {
get { return this.AppDomain.Process.Debugger; }
}
[Debugger.Tests.Ignore]
public MetaDataImport MetaData {
get {
return metaData;
}
}
public bool Unloaded {
get {
return unloaded;
}
}
[Debugger.Tests.Ignore]
public uint GetEntryPoint()
{
try {
if (symReader != null)
return symReader.GetUserEntryPoint();
var info = TypeSystemExtensions.GetInfo(Assembly);
var cecilModule = info.CecilModule;
if (cecilModule == null)
return 0;
var ep = cecilModule.EntryPoint;
if (ep != null)
return ep.MetadataToken.ToUInt32();
return 0;
} catch {
return 0;
}
}
[Debugger.Tests.Ignore]
public ISymUnmanagedReader SymReader {
get {
return symReader;
}
}
[Debugger.Tests.Ignore]
public ISymUnmanagedDocument[] SymDocuments {
get {
ISymUnmanagedDocument[] docs;
uint maxCount = 2;
uint fetched;
do {
maxCount *= 8;
docs = new ISymUnmanagedDocument[maxCount];
symReader.GetDocuments(maxCount, out fetched, docs);
} while (fetched == maxCount);
Array.Resize(ref docs, (int)fetched);
return docs;
}
}
[Debugger.Tests.Ignore]
public ICorDebugModule CorModule {
get { return corModule; }
}
[Debugger.Tests.Ignore]
public ICorDebugModule2 CorModule2 {
get { return (ICorDebugModule2)corModule; }
}
[Debugger.Tests.Ignore]
public ulong BaseAdress {
get {
return this.CorModule.GetBaseAddress();
}
}
public bool IsDynamic {
get {
return this.CorModule.IsDynamic() == 1;
}
}
public bool IsInMemory {
get {
return this.CorModule.IsInMemory() == 1;
}
}
internal uint AppDomainID {
get {
return this.CorModule.GetAssembly().GetAppDomain().GetID();
}
}
public string Name {
get {
return name;
}
}
[Debugger.Tests.Ignore]
public string FullPath {
get {
return fullPath;
}
}
public bool HasSymbols {
get {
return symReader != null;
}
}
public int OrderOfLoading {
get {
return orderOfLoading;
}
set {
orderOfLoading = value;
}
}
[Debugger.Tests.Ignore]
public CorDebugJITCompilerFlags JITCompilerFlags
{
get
{
uint retval = ((ICorDebugModule2)corModule).GetJITCompilerFlags();
return (CorDebugJITCompilerFlags)retval;
}
set
{
// ICorDebugModule2.SetJITCompilerFlags can return successful HRESULTS other than S_OK.
// Since we have asked the COMInterop layer to preservesig, we need to marshal any failing HRESULTS.
((ICorDebugModule2)corModule).SetJITCompilerFlags((uint)value);
}
}
internal Module(AppDomain appDomain, ICorDebugModule corModule)
{
this.appDomain = appDomain;
this.process = appDomain.Process;
this.corModule = corModule;
unresolvedAssembly = TypeSystemExtensions.LoadModuleAsync(this, corModule);
metaData = new MetaDataImport(corModule);
if (IsDynamic || IsInMemory) {
name = corModule.GetName();
} else {
fullPath = corModule.GetName();
name = System.IO.Path.GetFileName(FullPath);
}
SetJITCompilerFlags();
LoadSymbolsFromDisk(process.Options.SymbolsSearchPaths);
ResetJustMyCode();
LoadSymbolsDynamic();
}
public void UnloadSymbols()
{
if (symReader != null) {
// The interface is not always supported, I did not manage to reproduce it, but the
// last callbacks in the user's log were UnloadClass and UnloadModule so I guess
// it has something to do with dynamic modules.
if (symReader is ISymUnmanagedDispose) {
((ISymUnmanagedDispose)symReader).Destroy();
}
symReader = null;
}
}
/// <summary>
/// Load symblos for on-disk module
/// </summary>
public void LoadSymbolsFromDisk(IEnumerable<string> symbolsSearchPaths)
{
if (!IsDynamic && !IsInMemory) {
if (symReader == null) {
symReader = metaData.GetSymReader(fullPath, string.Join("; ", symbolsSearchPaths ?? new string[0]));
if (symReader != null) {
process.TraceMessage("Loaded symbols from disk for " + this.Name);
OnSymbolsUpdated();
}
}
}
}
/// <summary>
/// Load symbols for in-memory module
/// </summary>
public void LoadSymbolsFromMemory(IStream pSymbolStream)
{
if (this.IsInMemory) {
UnloadSymbols();
symReader = metaData.GetSymReader(pSymbolStream);
if (symReader != null) {
process.TraceMessage("Loaded symbols from memory for " + this.Name);
} else {
process.TraceMessage("Failed to load symbols from memory");
}
OnSymbolsUpdated();
}
}
/// <summary>
/// Load symbols for dynamic module
/// (as of .NET 4.0)
/// </summary>
public void LoadSymbolsDynamic()
{
if (this.CorModule is ICorDebugModule3 && this.IsDynamic) {
Guid guid = new Guid(0, 0, 0, 0xc0, 0, 0, 0, 0, 0, 0, 70);
try {
symReader = (ISymUnmanagedReader)((ICorDebugModule3)this.CorModule).CreateReaderForInMemorySymbols(guid);
} catch (COMException e) {
// 0x80131C3B The application did not supply symbols when it loaded or created this module, or they are not yet available.
if ((uint)e.ErrorCode == 0x80131C3B) {
process.TraceMessage("Failed to load dynamic symbols for " + this.Name);
return;
}
throw;
}
TrackedComObjects.Track(symReader);
process.TraceMessage("Loaded dynamic symbols for " + this.Name);
OnSymbolsUpdated();
}
}
void OnSymbolsUpdated()
{
foreach (Breakpoint b in this.Debugger.Breakpoints) {
b.SetBreakpoint(this);
}
ResetJustMyCode();
}
void SetJITCompilerFlags()
{
// ad CORDEBUG_JIT_ENABLE_ENC:
// see issue #553 on GitHub: "ExecutionEngineException or AccessViolationException (CLR 2.0)
// when debugging a program that uses System.Data.OleDb"
// System.Data.dll (partially) is a C++/CLI assembly and EnC is not supported there.
// trying to set CORDEBUG_JIT_ENABLE_ENC succeeds at first, but leads to strange exceptions afterwards.
// CORDEBUG_JIT_ENABLE_ENC should only be set for managed-only modules with User Code.
CorDebugJITCompilerFlags flags;
if (this.Process.Options.SuppressJITOptimization) {
flags = CorDebugJITCompilerFlags.CORDEBUG_JIT_DISABLE_OPTIMIZATION;
} else {
flags = CorDebugJITCompilerFlags.CORDEBUG_JIT_DEFAULT;
}
try
{
this.JITCompilerFlags = flags;
}
catch (COMException ex)
{
Console.WriteLine(string.Format("Failed to set flags with hr=0x{0:x}", ex.ErrorCode));
}
CorDebugJITCompilerFlags actual = this.JITCompilerFlags;
if (flags != actual)
this.Process.TraceMessage("Couldn't set JIT flags to {0}. Actual flags: {1}", flags, actual);
else
this.Process.TraceMessage("JIT flags: {0}", actual);
}
/// <summary>
/// Sets all code as being 'my code'. The code will be gradually switch
/// to not-user-code as encountered according to stepping options.
/// </summary>
public void ResetJustMyCode()
{
uint unused = 0;
if (this.Process.Debugger.SymbolSources.All(s => s is PdbSymbolSource) && this.SymReader == null) {
// Optimization - set the code as non-user right away
this.CorModule2.SetJMCStatus(0, 0, ref unused);
return;
}
try {
// Reqires the process to be synchronized!
this.CorModule2.SetJMCStatus(process.Options.EnableJustMyCode ? 1 : 0, 0, ref unused);
} catch (COMException e) {
// Cannot use JMC on this code (likely wrong JIT settings).
if ((uint)e.ErrorCode == 0x80131323) {
process.TraceMessage("Cannot use JMC on this code. Release build?");
return;
}
throw;
}
}
public void ApplyChanges(byte[] metadata, byte[] il)
{
this.CorModule2.ApplyChanges((uint)metadata.Length, metadata, (uint)il.Length, il);
}
public void Dispose()
{
UnloadSymbols();
unloaded = true;
}
public override string ToString()
{
return string.Format("{0}", this.Name);
}
}
}