-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathLoader_1.cs
268 lines (216 loc) · 10.4 KB
/
Loader_1.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using Microsoft.Diagnostics.DataContractReader.Data;
namespace Microsoft.Diagnostics.DataContractReader.Contracts;
internal readonly struct Loader_1 : ILoader
{
private readonly Target _target;
internal Loader_1(Target target)
{
_target = target;
}
ModuleHandle ILoader.GetModuleHandle(TargetPointer modulePointer)
{
if (modulePointer == TargetPointer.Null)
throw new ArgumentNullException(nameof(modulePointer));
return new ModuleHandle(modulePointer);
}
List<ModuleHandle> ILoader.GetAssemblies(TargetPointer appDomain, AssemblyIterationFlags iterationFlags)
{
if (appDomain == TargetPointer.Null)
throw new ArgumentNullException(nameof(appDomain));
Data.AppDomain domain = _target.ProcessedData.GetOrAdd<Data.AppDomain>(appDomain);
ArrayListBase arrayList = _target.ProcessedData.GetOrAdd<ArrayListBase>(domain.DomainAssemblyList);
List<ModuleHandle> handles = [];
foreach (TargetPointer pAssembly in arrayList.Elements)
{
TargetPointer assemblyAddr = _target.ReadPointer(pAssembly);
Data.Assembly assembly = _target.ProcessedData.GetOrAdd<Data.Assembly>(assemblyAddr);
// following logic is based on AppDomain::AssemblyIterator::Next_Unlocked in appdomain.cpp
if (assembly.IsError && !iterationFlags.HasFlag(AssemblyIterationFlags.IncludeFailedToLoad))
continue; // skip assemblies with errors
if ((assembly.NotifyFlags & 0x1 /*PROFILER_NOTIFIED*/) != 0 && !iterationFlags.HasFlag(AssemblyIterationFlags.IncludeAvailableToProfilers))
{
// The assembly has reached the state at which we would notify profilers,
// and we're supposed to include such assemblies in the enumeration. So
// don't reject it (i.e., noop here, and don't bother with the rest of
// the load status checks). Check for this first, since
// IncludeAvailableToProfilers contains some loaded AND loading
// assemblies.
}
else if (assembly.IsLoaded)
{
if (!iterationFlags.HasFlag(AssemblyIterationFlags.IncludeLoaded))
continue; // skip loaded assemblies
}
else
{
if (!iterationFlags.HasFlag(AssemblyIterationFlags.IncludeLoading))
continue; // skip loading assemblies
}
// Next, reject assemblies whose execution status is
// not to be included in the enumeration
if (!iterationFlags.HasFlag(AssemblyIterationFlags.IncludeExecution))
continue; // skip assemblies with execution status
if (assembly.IsCollectible != 0)
{
if (iterationFlags.HasFlag(AssemblyIterationFlags.ExcludeCollectible))
continue; // skip collectible assemblies
Module module = _target.ProcessedData.GetOrAdd<Data.Module>(assembly.Module);
if (((ModuleFlags)module.Flags).HasFlag(ModuleFlags.Tenured))
continue; // skip tenured modules
LoaderAllocator loaderAllocator = _target.ProcessedData.GetOrAdd<Data.LoaderAllocator>(module.LoaderAllocator);
if (!loaderAllocator.IsAlive && !iterationFlags.HasFlag(AssemblyIterationFlags.IncludeCollected))
continue; // skip collected assemblies
}
handles.Add(new(assembly.Module));
}
return handles;
}
TargetPointer ILoader.GetRootAssembly()
{
TargetPointer appDomainPointer = _target.ReadGlobalPointer(Constants.Globals.AppDomain);
Data.AppDomain appDomain = _target.ProcessedData.GetOrAdd<Data.AppDomain>(_target.ReadPointer(appDomainPointer));
return appDomain.RootAssembly;
}
TargetPointer ILoader.GetAssembly(ModuleHandle handle)
{
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
return module.Assembly;
}
TargetPointer ILoader.GetPEAssembly(ModuleHandle handle)
{
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
return module.PEAssembly;
}
bool ILoader.TryGetLoadedImageContents(ModuleHandle handle, out TargetPointer baseAddress, out uint size, out uint imageFlags)
{
baseAddress = TargetPointer.Null;
size = 0;
imageFlags = 0;
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
if (module.PEAssembly == TargetPointer.Null)
return false; // no loaded PEAssembly
Data.PEAssembly peAssembly = _target.ProcessedData.GetOrAdd<Data.PEAssembly>(module.PEAssembly);
if (peAssembly.PEImage == TargetPointer.Null)
return false; // no loaded PEImage
Data.PEImage peImage = _target.ProcessedData.GetOrAdd<Data.PEImage>(peAssembly.PEImage);
if (peImage.LoadedImageLayout == TargetPointer.Null)
return false; // no loaded image layout
Data.PEImageLayout peImageLayout = _target.ProcessedData.GetOrAdd<Data.PEImageLayout>(peImage.LoadedImageLayout);
baseAddress = peImageLayout.Base;
size = peImageLayout.Size;
imageFlags = peImageLayout.Flags;
return true;
}
bool ILoader.TryGetSymbolStream(ModuleHandle handle, out TargetPointer buffer, out uint size)
{
buffer = TargetPointer.Null;
size = 0;
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
if (module.GrowableSymbolStream == TargetPointer.Null)
return false;
Data.CGrowableSymbolStream growableSymbolStream = _target.ProcessedData.GetOrAdd<CGrowableSymbolStream>(module.GrowableSymbolStream);
buffer = growableSymbolStream.Buffer;
size = growableSymbolStream.Size;
return true;
}
bool ILoader.IsProbeExtensionResultValid(ModuleHandle handle)
{
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
if (module.PEAssembly == TargetPointer.Null)
return false; // no loaded PEAssembly
Data.PEAssembly peAssembly = _target.ProcessedData.GetOrAdd<Data.PEAssembly>(module.PEAssembly);
if (peAssembly.PEImage == TargetPointer.Null)
return false; // no loaded PEImage
Data.PEImage peImage = _target.ProcessedData.GetOrAdd<Data.PEImage>(peAssembly.PEImage);
// 0 is the invalid type. See assemblyprobeextension.h for details
return peImage.ProbeExtensionResult.Type != 0;
}
ModuleFlags ILoader.GetFlags(ModuleHandle handle)
{
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
return (ModuleFlags)module.Flags;
}
string ILoader.GetPath(ModuleHandle handle)
{
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
return module.Path != TargetPointer.Null
? _target.ReadUtf16String(module.Path)
: string.Empty;
}
string ILoader.GetFileName(ModuleHandle handle)
{
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
return module.FileName != TargetPointer.Null
? _target.ReadUtf16String(module.FileName)
: string.Empty;
}
TargetPointer ILoader.GetLoaderAllocator(ModuleHandle handle)
{
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
return module.LoaderAllocator;
}
TargetPointer ILoader.GetILBase(ModuleHandle handle)
{
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
return module.Base;
}
ModuleLookupTables ILoader.GetLookupTables(ModuleHandle handle)
{
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
return new ModuleLookupTables(
module.FieldDefToDescMap,
module.ManifestModuleReferencesMap,
module.MemberRefToDescMap,
module.MethodDefToDescMap,
module.TypeDefToMethodTableMap,
module.TypeRefToMethodTableMap,
module.MethodDefToILCodeVersioningStateMap);
}
TargetPointer ILoader.GetModuleLookupMapElement(TargetPointer table, uint token, out TargetNUInt flags)
{
uint rid = EcmaMetadataUtils.GetRowId(token);
ArgumentOutOfRangeException.ThrowIfZero(rid);
flags = new TargetNUInt(0);
if (table == TargetPointer.Null)
return TargetPointer.Null;
uint index = rid;
Data.ModuleLookupMap lookupMap = _target.ProcessedData.GetOrAdd<Data.ModuleLookupMap>(table);
// have to read lookupMap an extra time upfront because only the first map
// has valid supportedFlagsMask
TargetNUInt supportedFlagsMask = lookupMap.SupportedFlagsMask;
do
{
lookupMap = _target.ProcessedData.GetOrAdd<Data.ModuleLookupMap>(table);
if (index < lookupMap.Count)
{
TargetPointer entryAddress = lookupMap.TableData + (ulong)(index * _target.PointerSize);
TargetPointer rawValue = _target.ReadPointer(entryAddress);
flags = new TargetNUInt(rawValue & supportedFlagsMask.Value);
return rawValue & ~(supportedFlagsMask.Value);
}
else
{
table = lookupMap.Next;
index -= lookupMap.Count;
}
} while (table != TargetPointer.Null);
return TargetPointer.Null;
}
bool ILoader.IsCollectible(ModuleHandle handle)
{
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
TargetPointer assembly = module.Assembly;
Data.Assembly la = _target.ProcessedData.GetOrAdd<Data.Assembly>(assembly);
return la.IsCollectible != 0;
}
bool ILoader.IsAssemblyLoaded(ModuleHandle handle)
{
Data.Module module = _target.ProcessedData.GetOrAdd<Data.Module>(handle.Address);
Data.Assembly assembly = _target.ProcessedData.GetOrAdd<Data.Assembly>(module.Assembly);
return assembly.IsLoaded;
}
}