1 | //----------------------------------------------------------------------------- |
---|
2 | // |
---|
3 | // Copyright (C) Microsoft Corporation. All Rights Reserved. |
---|
4 | // |
---|
5 | //----------------------------------------------------------------------------- |
---|
6 | using System; |
---|
7 | using System.Collections; |
---|
8 | using System.Collections.Generic; |
---|
9 | using System.IO; |
---|
10 | using System.Diagnostics.SymbolStore; |
---|
11 | |
---|
12 | namespace Microsoft.Cci.Pdb { |
---|
13 | internal class PdbFile { |
---|
14 | private PdbFile() // This class can't be instantiated. |
---|
15 | { |
---|
16 | } |
---|
17 | |
---|
18 | static void LoadGuidStream(BitAccess bits, out Guid doctype, out Guid language, out Guid vendor) { |
---|
19 | bits.ReadGuid(out language); |
---|
20 | bits.ReadGuid(out vendor); |
---|
21 | bits.ReadGuid(out doctype); |
---|
22 | } |
---|
23 | |
---|
24 | static Dictionary<string,int> LoadNameIndex(BitAccess bits, out int age, out Guid guid) { |
---|
25 | Dictionary<string, int> result = new Dictionary<string, int>(); |
---|
26 | int ver; |
---|
27 | int sig; |
---|
28 | bits.ReadInt32(out ver); // 0..3 Version |
---|
29 | bits.ReadInt32(out sig); // 4..7 Signature |
---|
30 | bits.ReadInt32(out age); // 8..11 Age |
---|
31 | bits.ReadGuid(out guid); // 12..27 GUID |
---|
32 | |
---|
33 | if (ver != 20000404) { |
---|
34 | throw new PdbDebugException("Unsupported PDB Stream version {0}", ver); |
---|
35 | } |
---|
36 | |
---|
37 | // Read string buffer. |
---|
38 | int buf; |
---|
39 | bits.ReadInt32(out buf); // 28..31 Bytes of Strings |
---|
40 | |
---|
41 | int beg = bits.Position; |
---|
42 | int nxt = bits.Position + buf; |
---|
43 | |
---|
44 | bits.Position = nxt; |
---|
45 | |
---|
46 | // Read map index. |
---|
47 | int cnt; // n+0..3 hash size. |
---|
48 | int max; // n+4..7 maximum ni. |
---|
49 | |
---|
50 | bits.ReadInt32(out cnt); |
---|
51 | bits.ReadInt32(out max); |
---|
52 | |
---|
53 | BitSet present = new BitSet(bits); |
---|
54 | BitSet deleted = new BitSet(bits); |
---|
55 | if (!deleted.IsEmpty) { |
---|
56 | throw new PdbDebugException("Unsupported PDB deleted bitset is not empty."); |
---|
57 | } |
---|
58 | |
---|
59 | int j = 0; |
---|
60 | for (int i = 0; i < max; i++) { |
---|
61 | if (present.IsSet(i)) { |
---|
62 | int ns; |
---|
63 | int ni; |
---|
64 | bits.ReadInt32(out ns); |
---|
65 | bits.ReadInt32(out ni); |
---|
66 | |
---|
67 | string name; |
---|
68 | int saved = bits.Position; |
---|
69 | bits.Position = beg + ns; |
---|
70 | bits.ReadCString(out name); |
---|
71 | bits.Position = saved; |
---|
72 | |
---|
73 | result.Add(name, ni); |
---|
74 | j++; |
---|
75 | } |
---|
76 | } |
---|
77 | if (j != cnt) { |
---|
78 | throw new PdbDebugException("Count mismatch. ({0} != {1})", j, cnt); |
---|
79 | } |
---|
80 | return result; |
---|
81 | } |
---|
82 | |
---|
83 | static IntHashTable LoadNameStream(BitAccess bits) { |
---|
84 | IntHashTable ht = new IntHashTable(); |
---|
85 | |
---|
86 | uint sig; |
---|
87 | int ver; |
---|
88 | bits.ReadUInt32(out sig); // 0..3 Signature |
---|
89 | bits.ReadInt32(out ver); // 4..7 Version |
---|
90 | |
---|
91 | // Read (or skip) string buffer. |
---|
92 | int buf; |
---|
93 | bits.ReadInt32(out buf); // 8..11 Bytes of Strings |
---|
94 | |
---|
95 | if (sig != 0xeffeeffe || ver != 1) { |
---|
96 | throw new PdbDebugException("Unsupported Name Stream version. "+ |
---|
97 | "(sig={0:x8}, ver={1})", |
---|
98 | sig, ver); |
---|
99 | } |
---|
100 | int beg = bits.Position; |
---|
101 | int nxt = bits.Position + buf; |
---|
102 | bits.Position = nxt; |
---|
103 | |
---|
104 | // Read hash table. |
---|
105 | int siz; |
---|
106 | bits.ReadInt32(out siz); // n+0..3 Number of hash buckets. |
---|
107 | nxt = bits.Position; |
---|
108 | |
---|
109 | for (int i = 0; i < siz; i++) { |
---|
110 | int ni; |
---|
111 | string name; |
---|
112 | |
---|
113 | bits.ReadInt32(out ni); |
---|
114 | |
---|
115 | if (ni != 0) { |
---|
116 | int saved = bits.Position; |
---|
117 | bits.Position = beg + ni; |
---|
118 | bits.ReadCString(out name); |
---|
119 | bits.Position = saved; |
---|
120 | |
---|
121 | ht.Add(ni, name); |
---|
122 | } |
---|
123 | } |
---|
124 | bits.Position = nxt; |
---|
125 | |
---|
126 | return ht; |
---|
127 | } |
---|
128 | |
---|
129 | private static PdbFunction match = new PdbFunction(); |
---|
130 | |
---|
131 | private static PdbFunction FindFunction(PdbFunction[] funcs, ushort sec, uint off) { |
---|
132 | match.segment = sec; |
---|
133 | match.address = off; |
---|
134 | |
---|
135 | int item = Array.BinarySearch(funcs, match, PdbFunction.byAddress); |
---|
136 | if (item >= 0) { |
---|
137 | return funcs[item]; |
---|
138 | } |
---|
139 | return null; |
---|
140 | } |
---|
141 | |
---|
142 | static void LoadManagedLines(PdbFunction[] funcs, |
---|
143 | IntHashTable names, |
---|
144 | BitAccess bits, |
---|
145 | MsfDirectory dir, |
---|
146 | Dictionary<string, int> nameIndex, |
---|
147 | PdbReader reader, |
---|
148 | uint limit) { |
---|
149 | Array.Sort(funcs, PdbFunction.byAddress); |
---|
150 | IntHashTable checks = new IntHashTable(); |
---|
151 | |
---|
152 | // Read the files first |
---|
153 | int begin = bits.Position; |
---|
154 | while (bits.Position < limit) { |
---|
155 | int sig; |
---|
156 | int siz; |
---|
157 | bits.ReadInt32(out sig); |
---|
158 | bits.ReadInt32(out siz); |
---|
159 | int place = bits.Position; |
---|
160 | int endSym = bits.Position + siz; |
---|
161 | |
---|
162 | switch ((DEBUG_S_SUBSECTION)sig) { |
---|
163 | case DEBUG_S_SUBSECTION.FILECHKSMS: |
---|
164 | while (bits.Position < endSym) { |
---|
165 | CV_FileCheckSum chk; |
---|
166 | |
---|
167 | int ni = bits.Position - place; |
---|
168 | bits.ReadUInt32(out chk.name); |
---|
169 | bits.ReadUInt8(out chk.len); |
---|
170 | bits.ReadUInt8(out chk.type); |
---|
171 | |
---|
172 | string name = (string)names[(int)chk.name]; |
---|
173 | int guidStream; |
---|
174 | Guid doctypeGuid = SymDocumentType.Text; |
---|
175 | Guid languageGuid = SymLanguageType.CSharp; |
---|
176 | Guid vendorGuid = SymLanguageVendor.Microsoft; |
---|
177 | if (nameIndex.TryGetValue("/src/files/"+name, out guidStream)) { |
---|
178 | var guidBits = new BitAccess(0x100); |
---|
179 | dir.streams[guidStream].Read(reader, guidBits); |
---|
180 | LoadGuidStream(guidBits, out doctypeGuid, out languageGuid, out vendorGuid); |
---|
181 | } |
---|
182 | |
---|
183 | PdbSource src = new PdbSource((uint)ni, name, doctypeGuid, languageGuid, vendorGuid); |
---|
184 | checks.Add(ni, src); |
---|
185 | bits.Position += chk.len; |
---|
186 | bits.Align(4); |
---|
187 | } |
---|
188 | bits.Position = endSym; |
---|
189 | break; |
---|
190 | |
---|
191 | default: |
---|
192 | bits.Position = endSym; |
---|
193 | break; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | // Read the lines next. |
---|
198 | bits.Position = begin; |
---|
199 | while (bits.Position < limit) { |
---|
200 | int sig; |
---|
201 | int siz; |
---|
202 | bits.ReadInt32(out sig); |
---|
203 | bits.ReadInt32(out siz); |
---|
204 | int endSym = bits.Position + siz; |
---|
205 | |
---|
206 | switch ((DEBUG_S_SUBSECTION)sig) { |
---|
207 | case DEBUG_S_SUBSECTION.LINES: { |
---|
208 | CV_LineSection sec; |
---|
209 | |
---|
210 | bits.ReadUInt32(out sec.off); |
---|
211 | bits.ReadUInt16(out sec.sec); |
---|
212 | bits.ReadUInt16(out sec.flags); |
---|
213 | bits.ReadUInt32(out sec.cod); |
---|
214 | PdbFunction func = FindFunction(funcs, sec.sec, sec.off); |
---|
215 | if (func == null) break; |
---|
216 | |
---|
217 | // Count the line blocks. |
---|
218 | int begSym = bits.Position; |
---|
219 | int blocks = 0; |
---|
220 | while (bits.Position < endSym) { |
---|
221 | CV_SourceFile file; |
---|
222 | bits.ReadUInt32(out file.index); |
---|
223 | bits.ReadUInt32(out file.count); |
---|
224 | bits.ReadUInt32(out file.linsiz); // Size of payload. |
---|
225 | int linsiz = (int)file.count * (8 + ((sec.flags & 1) != 0 ? 4 : 0)); |
---|
226 | bits.Position += linsiz; |
---|
227 | blocks++; |
---|
228 | } |
---|
229 | |
---|
230 | func.lines = new PdbLines[blocks]; |
---|
231 | int block = 0; |
---|
232 | |
---|
233 | bits.Position = begSym; |
---|
234 | while (bits.Position < endSym) { |
---|
235 | CV_SourceFile file; |
---|
236 | bits.ReadUInt32(out file.index); |
---|
237 | bits.ReadUInt32(out file.count); |
---|
238 | bits.ReadUInt32(out file.linsiz); // Size of payload. |
---|
239 | |
---|
240 | PdbSource src = (PdbSource)checks[(int)file.index]; |
---|
241 | PdbLines tmp = new PdbLines(src, file.count); |
---|
242 | func.lines[block++] = tmp; |
---|
243 | PdbLine[] lines = tmp.lines; |
---|
244 | |
---|
245 | int plin = bits.Position; |
---|
246 | int pcol = bits.Position + 8 * (int)file.count; |
---|
247 | |
---|
248 | for (int i = 0; i < file.count; i++) { |
---|
249 | CV_Line line; |
---|
250 | CV_Column column = new CV_Column(); |
---|
251 | |
---|
252 | bits.Position = plin + 8 * i; |
---|
253 | bits.ReadUInt32(out line.offset); |
---|
254 | bits.ReadUInt32(out line.flags); |
---|
255 | |
---|
256 | uint lineBegin = line.flags & (uint)CV_Line_Flags.linenumStart; |
---|
257 | uint delta = (line.flags & (uint)CV_Line_Flags.deltaLineEnd) >> 24; |
---|
258 | bool statement = ((line.flags & (uint)CV_Line_Flags.fStatement) == 0); |
---|
259 | if ((sec.flags & 1) != 0) { |
---|
260 | bits.Position = pcol + 4 * i; |
---|
261 | bits.ReadUInt16(out column.offColumnStart); |
---|
262 | bits.ReadUInt16(out column.offColumnEnd); |
---|
263 | } |
---|
264 | |
---|
265 | lines[i] = new PdbLine(line.offset, |
---|
266 | lineBegin, |
---|
267 | column.offColumnStart, |
---|
268 | lineBegin+delta, |
---|
269 | column.offColumnEnd); |
---|
270 | } |
---|
271 | } |
---|
272 | break; |
---|
273 | } |
---|
274 | } |
---|
275 | bits.Position = endSym; |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | static void LoadFuncsFromDbiModule(BitAccess bits, |
---|
280 | DbiModuleInfo info, |
---|
281 | IntHashTable names, |
---|
282 | ArrayList funcList, |
---|
283 | bool readStrings, |
---|
284 | MsfDirectory dir, |
---|
285 | Dictionary<string, int> nameIndex, |
---|
286 | PdbReader reader) { |
---|
287 | PdbFunction[] funcs = null; |
---|
288 | |
---|
289 | bits.Position = 0; |
---|
290 | int sig; |
---|
291 | bits.ReadInt32(out sig); |
---|
292 | if (sig != 4) { |
---|
293 | throw new PdbDebugException("Invalid signature. (sig={0})", sig); |
---|
294 | } |
---|
295 | |
---|
296 | bits.Position = 4; |
---|
297 | // Console.WriteLine("{0}:", info.moduleName); |
---|
298 | funcs = PdbFunction.LoadManagedFunctions(info.moduleName, |
---|
299 | bits, (uint)info.cbSyms, |
---|
300 | readStrings); |
---|
301 | if (funcs != null) { |
---|
302 | bits.Position = info.cbSyms + info.cbOldLines; |
---|
303 | LoadManagedLines(funcs, names, bits, dir, nameIndex, reader, |
---|
304 | (uint)(info.cbSyms + info.cbOldLines + info.cbLines)); |
---|
305 | |
---|
306 | for (int i = 0; i < funcs.Length; i++) { |
---|
307 | funcList.Add(funcs[i]); |
---|
308 | } |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | static void LoadDbiStream(BitAccess bits, |
---|
313 | out DbiModuleInfo[] modules, |
---|
314 | out DbiDbgHdr header, |
---|
315 | bool readStrings) { |
---|
316 | DbiHeader dh = new DbiHeader(bits); |
---|
317 | header = new DbiDbgHdr(); |
---|
318 | |
---|
319 | if (dh.sig != -1 || dh.ver != 19990903) { |
---|
320 | throw new PdbException("Unsupported DBI Stream version, sig={0}, ver={1}", |
---|
321 | dh.sig, dh.ver); |
---|
322 | } |
---|
323 | |
---|
324 | // Read gpmod section. |
---|
325 | ArrayList modList = new ArrayList(); |
---|
326 | int end = bits.Position + dh.gpmodiSize; |
---|
327 | while (bits.Position < end) { |
---|
328 | DbiModuleInfo mod = new DbiModuleInfo(bits, readStrings); |
---|
329 | modList.Add(mod); |
---|
330 | } |
---|
331 | if (bits.Position != end) { |
---|
332 | throw new PdbDebugException("Error reading DBI stream, pos={0} != {1}", |
---|
333 | bits.Position, end); |
---|
334 | } |
---|
335 | |
---|
336 | if (modList.Count > 0) { |
---|
337 | modules = (DbiModuleInfo[])modList.ToArray(typeof(DbiModuleInfo)); |
---|
338 | } else { |
---|
339 | modules = null; |
---|
340 | } |
---|
341 | |
---|
342 | // Skip the Section Contribution substream. |
---|
343 | bits.Position += dh.secconSize; |
---|
344 | |
---|
345 | // Skip the Section Map substream. |
---|
346 | bits.Position += dh.secmapSize; |
---|
347 | |
---|
348 | // Skip the File Info substream. |
---|
349 | bits.Position += dh.filinfSize; |
---|
350 | |
---|
351 | // Skip the TSM substream. |
---|
352 | bits.Position += dh.tsmapSize; |
---|
353 | |
---|
354 | // Skip the EC substream. |
---|
355 | bits.Position += dh.ecinfoSize; |
---|
356 | |
---|
357 | // Read the optional header. |
---|
358 | end = bits.Position + dh.dbghdrSize; |
---|
359 | if (dh.dbghdrSize > 0) { |
---|
360 | header = new DbiDbgHdr(bits); |
---|
361 | } |
---|
362 | bits.Position = end; |
---|
363 | } |
---|
364 | |
---|
365 | internal static PdbFunction[] LoadFunctions(Stream read, bool readAllStrings, out int age, out Guid guid) { |
---|
366 | BitAccess bits = new BitAccess(512 * 1024); |
---|
367 | return LoadFunctions(read, bits, readAllStrings, out age, out guid); |
---|
368 | } |
---|
369 | |
---|
370 | internal static PdbFunction[] LoadFunctions(Stream read, BitAccess bits, bool readAllStrings, out int age, out Guid guid) { |
---|
371 | PdbFileHeader head = new PdbFileHeader(read, bits); |
---|
372 | PdbReader reader = new PdbReader(read, head.pageSize); |
---|
373 | MsfDirectory dir = new MsfDirectory(reader, head, bits); |
---|
374 | DbiModuleInfo[] modules = null; |
---|
375 | DbiDbgHdr header; |
---|
376 | |
---|
377 | dir.streams[1].Read(reader, bits); |
---|
378 | Dictionary<string, int> nameIndex = LoadNameIndex(bits, out age, out guid); |
---|
379 | int nameStream; |
---|
380 | if (!nameIndex.TryGetValue("/names", out nameStream)) { |
---|
381 | throw new PdbException("No `name' stream"); |
---|
382 | } |
---|
383 | |
---|
384 | dir.streams[nameStream].Read(reader, bits); |
---|
385 | IntHashTable names = LoadNameStream(bits); |
---|
386 | |
---|
387 | dir.streams[3].Read(reader, bits); |
---|
388 | LoadDbiStream(bits, out modules, out header, readAllStrings); |
---|
389 | |
---|
390 | ArrayList funcList = new ArrayList(); |
---|
391 | |
---|
392 | if (modules != null) { |
---|
393 | for (int m = 0; m < modules.Length; m++) { |
---|
394 | if (modules[m].stream > 0) { |
---|
395 | dir.streams[modules[m].stream].Read(reader, bits); |
---|
396 | LoadFuncsFromDbiModule(bits, modules[m], names, funcList, |
---|
397 | readAllStrings, dir, nameIndex, reader); |
---|
398 | } |
---|
399 | } |
---|
400 | } |
---|
401 | |
---|
402 | PdbFunction[] funcs = (PdbFunction[])funcList.ToArray(typeof(PdbFunction)); |
---|
403 | |
---|
404 | // After reading the functions, apply the token remapping table if it exists. |
---|
405 | if (header.snTokenRidMap != 0 && header.snTokenRidMap != 0xffff) { |
---|
406 | dir.streams[header.snTokenRidMap].Read(reader, bits); |
---|
407 | uint[] ridMap = new uint[dir.streams[header.snTokenRidMap].Length / 4]; |
---|
408 | bits.ReadUInt32(ridMap); |
---|
409 | |
---|
410 | foreach (PdbFunction func in funcs) { |
---|
411 | func.token = 0x06000000 | ridMap[func.token & 0xffffff]; |
---|
412 | } |
---|
413 | } |
---|
414 | |
---|
415 | // |
---|
416 | Array.Sort(funcs, PdbFunction.byAddress); |
---|
417 | //Array.Sort(funcs, PdbFunction.byToken); |
---|
418 | return funcs; |
---|
419 | } |
---|
420 | } |
---|
421 | } |
---|