forked from southpolenator/SharpPdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdbFileReader.cs
More file actions
298 lines (260 loc) · 12.8 KB
/
Copy pathPdbFileReader.cs
File metadata and controls
298 lines (260 loc) · 12.8 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
using SharpPdb.Native.Types;
using SharpPdb.Windows;
using SharpPdb.Windows.SymbolRecords;
using SharpPdb.Windows.TypeRecords;
using SharpUtilities;
using System;
using System.Collections.Generic;
using System.Linq;
namespace SharpPdb.Native
{
/// <summary>
/// Represents Native Windows PDB file reader.
/// </summary>
public class PdbFileReader : IDisposable
{
/// <summary>
/// Cache of types by type index.
/// </summary>
private DictionaryCache<TypeIndex, PdbType> typesByIndex;
/// <summary>
/// Cache for <see cref="UserDefinedTypes"/> property.
/// </summary>
private SimpleCacheStruct<IReadOnlyList<PdbType>> userDefinedTypesCache;
/// <summary>
/// Cache for <see cref="GlobalVariables"/> property.
/// </summary>
private SimpleCacheStruct<PdbGlobalVariable[]> globalVarablesCache;
/// <summary>
/// Cache for <see cref="PublicSymbols"/> property.
/// </summary>
private SimpleCacheStruct<PdbPublicSymbol[]> publicSymbolsCache;
/// <summary>
/// Cache for <see cref="Functions"/> property.
/// </summary>
private SimpleCacheStruct<List<PdbFunction>> functionsCache;
/// <summary>
/// Initializes a new instance of the <see cref="PdbFileReader"/> class.
/// </summary>
/// <param name="pdbPath">Path to PDB file.</param>
public PdbFileReader(string pdbPath)
: this(new PdbFile(pdbPath))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PdbFileReader"/> class.
/// </summary>
/// <param name="file">File loaded into memory. Note that file will be closed when instance of this type is disposed.</param>
public PdbFileReader(MemoryLoadedFile file)
: this(new PdbFile(file))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PdbFileReader"/> class.
/// </summary>
/// <param name="reader">Binary stream reader of the PDB file.</param>
public PdbFileReader(IBinaryReader reader)
: this(new PdbFile(reader))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PdbFileReader"/> class.
/// </summary>
/// <param name="pdbFile">Opened PDB file.</param>
private PdbFileReader(PdbFile pdbFile)
{
PdbFile = pdbFile;
typesByIndex = new DictionaryCache<TypeIndex, PdbType>(CreateType);
userDefinedTypesCache = SimpleCache.CreateStruct(() =>
{
List<PdbType> types = new List<PdbType>();
var references = PdbFile.TpiStream.References;
TypeLeafKind[] allowedKinds = ClassRecord.Kinds.Concat(UnionRecord.Kinds).Concat(EnumRecord.Kinds).ToArray();
for (int i = 0; i < references.Count; i++)
if (allowedKinds.Contains(references[i].Kind))
{
TypeIndex typeIndex = TypeIndex.FromArrayIndex(i);
TypeRecord typeRecord = PdbFile.TpiStream[typeIndex];
PdbType pdbType = typesByIndex[typeIndex];
if (typeRecord is TagRecord tagRecord)
{
// Check if it is forward reference and if it has been resolved.
PdbUserDefinedType pdbUserDefinedType = (PdbUserDefinedType)pdbType;
if (pdbUserDefinedType.TagRecord != tagRecord)
continue;
}
types.Add(pdbType);
}
return (IReadOnlyList<PdbType>)types;
});
globalVarablesCache = SimpleCache.CreateStruct(() =>
{
var data = PdbFile.GlobalsStream.Data;
PdbGlobalVariable[] globalVariables = new PdbGlobalVariable[data.Count];
for (int i = 0; i < data.Count; i++)
globalVariables[i] = new PdbGlobalVariable(this, data[i]);
return globalVariables;
});
publicSymbolsCache = SimpleCache.CreateStruct(() =>
{
PdbPublicSymbol[] publicSymbols = new PdbPublicSymbol[PdbFile.PublicsStream.PublicSymbols.Count];
for (int i = 0; i < publicSymbols.Length; i++)
publicSymbols[i] = new PdbPublicSymbol(this, PdbFile.PublicsStream.PublicSymbols[i]);
return publicSymbols;
});
functionsCache = SimpleCache.CreateStruct(() =>
{
List<PdbFunction> functions = new List<PdbFunction>();
var references = PdbFile.PdbSymbolStream?.References;
var modules = PdbFile.DbiStream?.Modules;
if (references != null && modules != null)
{
HashSet<uint>[] selectedFunctions = new HashSet<uint>[modules.Count];
for (int i = 0; i < references.Count; i++)
{
ProcedureSymbol procedure = null;
switch (references[i].Kind)
{
// ProcedureSymbol
case SymbolRecordKind.S_GPROC32:
case SymbolRecordKind.S_LPROC32:
case SymbolRecordKind.S_GPROC32_ID:
case SymbolRecordKind.S_LPROC32_ID:
case SymbolRecordKind.S_LPROC32_DPC:
case SymbolRecordKind.S_LPROC32_DPC_ID:
procedure = PdbFile.PdbSymbolStream[i] as ProcedureSymbol;
break;
// ProcedureReferenceSymbol
case SymbolRecordKind.S_PROCREF:
case SymbolRecordKind.S_LPROCREF:
{
ProcedureReferenceSymbol procedureReference = PdbFile.PdbSymbolStream[i] as ProcedureReferenceSymbol;
int moduleIndex = procedureReference.Module - 1;
if (moduleIndex >= 0 && moduleIndex < modules.Count)
{
var module = modules[moduleIndex];
if (selectedFunctions[moduleIndex] == null)
selectedFunctions[moduleIndex] = new HashSet<uint>();
if (!selectedFunctions[moduleIndex].Contains(procedureReference.Offset)
&& module.LocalSymbolStream.TryGetSymbolRecordByOffset(procedureReference.Offset, out SymbolRecord procedureSymbol))
{
procedure = procedureSymbol as ProcedureSymbol;
selectedFunctions[moduleIndex].Add(procedureReference.Offset);
}
}
}
break;
}
if (procedure != null)
functions.Add(new PdbFunction(this, procedure));
}
}
return functions;
});
}
/// <summary>
/// Gets the Native Windows PDB reader.
/// </summary>
public PdbFile PdbFile { get; private set; }
/// <summary>
/// Gets the user defined types from PDB file.
/// </summary>
public IReadOnlyList<PdbType> UserDefinedTypes => userDefinedTypesCache.Value;
/// <summary>
/// Gets the global variables from PDB file.
/// </summary>
public PdbGlobalVariable[] GlobalVariables => globalVarablesCache.Value;
/// <summary>
/// Gets the public symbols from PDB file.
/// </summary>
public PdbPublicSymbol[] PublicSymbols => publicSymbolsCache.Value;
/// <summary>
/// Gets the functions from PDB file.
/// </summary>
public IReadOnlyList<PdbFunction> Functions => functionsCache.Value;
/// <summary>
/// Gets the <see cref="PdbType"/> at the specified index.
/// </summary>
/// <param name="typeIndex">The type index.</param>
internal PdbType this[TypeIndex typeIndex] => typesByIndex[typeIndex];
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
PdbFile?.Dispose();
}
/// <summary>
/// Creates <see cref="PdbType"/> read from <see cref="PdbFile"/> for the specified type index.
/// </summary>
/// <param name="typeIndex">Type index.</param>
private PdbType CreateType(TypeIndex typeIndex)
{
return CreateType(typeIndex, ModifierOptions.None, typeIndex);
}
/// <summary>
/// Creates <see cref="PdbType"/> read from <see cref="PdbFile"/> for the specified type index.
/// </summary>
/// <param name="typeIndex">Type index.</param>
/// <param name="modifierOptions">Modifier options for new type.</param>
/// <param name="originalTypeIndex">Original type index of the PDB type.</param>
private PdbType CreateType(TypeIndex typeIndex, ModifierOptions modifierOptions, TypeIndex originalTypeIndex)
{
if (typeIndex.IsSimple)
{
if (typeIndex.SimpleMode != SimpleTypeMode.Direct)
return new PdbSimplePointerType(this, typeIndex, modifierOptions);
return new PdbSimpleType(this, typeIndex, modifierOptions);
}
TypeRecord typeRecord = PdbFile.TpiStream[typeIndex];
if (typeRecord is ModifierRecord modifierRecord)
return CreateType(modifierRecord.ModifiedType, modifierRecord.Modifiers, originalTypeIndex);
if (typeRecord is TagRecord tagRecord && tagRecord.IsForwardReference)
{
// Resolve forward references
TagRecord resolvedRecord = null;
if (PdbFile.TpiStream.HashTable != null)
{
TagRecord FindUdtByHash(uint hash)
{
var bucket = PdbFile.TpiStream.HashTable[hash % PdbFile.TpiStream.Header.HashBucketsCount];
while (bucket != null)
{
TypeRecord record = PdbFile.TpiStream[bucket.TypeIndex];
if (record is TagRecord tag && tagRecord.Name.String == tag.Name.String)
return tag;
bucket = bucket.Next;
}
return null;
}
resolvedRecord = FindUdtByHash(HashTable.HashStringV1(tagRecord.Name.String));
if (resolvedRecord == null && tagRecord.HasUniqueName)
resolvedRecord = FindUdtByHash(HashTable.HashStringV1(tagRecord.UniqueName.String));
}
else
resolvedRecord = PdbFile.TpiStream[typeRecord.Kind].OfType<TagRecord>().LastOrDefault(r => !r.IsForwardReference && r.UniqueName.String == tagRecord.UniqueName.String);
if (resolvedRecord != null)
typeRecord = resolvedRecord;
}
if (typeRecord is ClassRecord classRecord)
return new PdbClassType(this, originalTypeIndex, classRecord, modifierOptions);
if (typeRecord is UnionRecord unionRecord)
return new PdbUnionType(this, originalTypeIndex, unionRecord, modifierOptions);
if (typeRecord is EnumRecord enumRecord)
return new PdbEnumType(this, originalTypeIndex, enumRecord, modifierOptions);
if (typeRecord is ArrayRecord arrayRecord)
return new PdbArrayType(this, originalTypeIndex, arrayRecord, modifierOptions);
if (typeRecord is PointerRecord pointerRecord)
return new PdbComplexPointerType(this, originalTypeIndex, pointerRecord, modifierOptions);
if (typeRecord is ProcedureRecord procedureRecord)
return new PdbFunctionType(this, originalTypeIndex, procedureRecord, modifierOptions);
if (typeRecord is MemberFunctionRecord memberFunctionRecord)
return new PdbMemberFunctionType(this, originalTypeIndex, memberFunctionRecord, modifierOptions);
#if DEBUG
throw new NotImplementedException();
#else
return null;
#endif
}
}
}