PdbFile.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. namespace Microsoft.Cci.Pdb {
  6. internal class PdbFile
  7. {
  8. /// <summary>
  9. /// GUID of the Basic source language.
  10. /// </summary>
  11. private static readonly Guid BasicLanguageGuid = new Guid(974311608, -15764, 4560, 180, 66, 0, 160, 36, 74, 29, 210);
  12. private PdbFile() // This class can't be instantiated.
  13. {
  14. }
  15. static void LoadInjectedSourceInformation(BitAccess bits, out Guid doctype, out Guid language, out Guid vendor, out Guid checksumAlgo, out byte[] checksum)
  16. {
  17. int checksumSize;
  18. int injectedSourceSize;
  19. checksum = null;
  20. bits.ReadGuid(out language);
  21. bits.ReadGuid(out vendor);
  22. bits.ReadGuid(out doctype);
  23. bits.ReadGuid(out checksumAlgo);
  24. bits.ReadInt32(out checksumSize);
  25. bits.ReadInt32(out injectedSourceSize);
  26. if (checksumSize > 0)
  27. {
  28. checksum = new byte[checksumSize];
  29. bits.ReadBytes(checksum);
  30. }
  31. }
  32. static Dictionary<string, int> LoadNameIndex(BitAccess bits, out int age, out Guid guid) {
  33. Dictionary<string, int> result = new Dictionary<string, int>();
  34. int ver;
  35. int sig;
  36. bits.ReadInt32(out ver); // 0..3 Version
  37. bits.ReadInt32(out sig); // 4..7 Signature
  38. bits.ReadInt32(out age); // 8..11 Age
  39. bits.ReadGuid(out guid); // 12..27 GUID
  40. //if (ver != 20000404) {
  41. // throw new PdbDebugException("Unsupported PDB Stream version {0}", ver);
  42. //}
  43. // Read string buffer.
  44. int buf;
  45. bits.ReadInt32(out buf); // 28..31 Bytes of Strings
  46. int beg = bits.Position;
  47. int nxt = bits.Position + buf;
  48. bits.Position = nxt;
  49. // Read map index.
  50. int cnt; // n+0..3 hash size.
  51. int max; // n+4..7 maximum ni.
  52. bits.ReadInt32(out cnt);
  53. bits.ReadInt32(out max);
  54. BitSet present = new BitSet(bits);
  55. BitSet deleted = new BitSet(bits);
  56. if (!deleted.IsEmpty) {
  57. throw new PdbDebugException("Unsupported PDB deleted bitset is not empty.");
  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. string name;
  67. int saved = bits.Position;
  68. bits.Position = beg + ns;
  69. bits.ReadCString(out name);
  70. bits.Position = saved;
  71. result.Add(name.ToUpperInvariant(), ni);
  72. j++;
  73. }
  74. }
  75. if (j != cnt) {
  76. throw new PdbDebugException("Count mismatch. ({0} != {1})", j, cnt);
  77. }
  78. return result;
  79. }
  80. static IntHashTable LoadNameStream(BitAccess bits) {
  81. IntHashTable ht = new IntHashTable();
  82. uint sig;
  83. int ver;
  84. bits.ReadUInt32(out sig); // 0..3 Signature
  85. bits.ReadInt32(out ver); // 4..7 Version
  86. // Read (or skip) string buffer.
  87. int buf;
  88. bits.ReadInt32(out buf); // 8..11 Bytes of Strings
  89. if (sig != 0xeffeeffe || ver != 1) {
  90. throw new PdbDebugException("Unsupported Name Stream version. "+
  91. "(sig={0:x8}, ver={1})",
  92. sig, ver);
  93. }
  94. int beg = bits.Position;
  95. int nxt = bits.Position + buf;
  96. bits.Position = nxt;
  97. // Read hash table.
  98. int siz;
  99. bits.ReadInt32(out siz); // n+0..3 Number of hash buckets.
  100. nxt = bits.Position;
  101. for (int i = 0; i < siz; i++) {
  102. int ni;
  103. string name;
  104. bits.ReadInt32(out ni);
  105. if (ni != 0) {
  106. int saved = bits.Position;
  107. bits.Position = beg + ni;
  108. bits.ReadCString(out name);
  109. bits.Position = saved;
  110. ht.Add(ni, name);
  111. }
  112. }
  113. bits.Position = nxt;
  114. return ht;
  115. }
  116. private static PdbFunction match = new PdbFunction();
  117. private static int FindFunction(PdbFunction[] funcs, ushort sec, uint off) {
  118. match.segment = sec;
  119. match.address = off;
  120. return Array.BinarySearch(funcs, match, PdbFunction.byAddress);
  121. }
  122. static void LoadManagedLines(PdbFunction[] funcs,
  123. IntHashTable names,
  124. BitAccess bits,
  125. MsfDirectory dir,
  126. Dictionary<string, int> nameIndex,
  127. PdbReader reader,
  128. uint limit,
  129. Dictionary<string, PdbSource> sourceCache)
  130. {
  131. Array.Sort(funcs, PdbFunction.byAddressAndToken);
  132. int begin = bits.Position;
  133. IntHashTable checks = ReadSourceFileInfo(bits, limit, names, dir, nameIndex, reader, sourceCache);
  134. // Read the lines next.
  135. bits.Position = begin;
  136. while (bits.Position < limit) {
  137. int sig;
  138. int siz;
  139. bits.ReadInt32(out sig);
  140. bits.ReadInt32(out siz);
  141. int endSym = bits.Position + siz;
  142. switch ((DEBUG_S_SUBSECTION)sig) {
  143. case DEBUG_S_SUBSECTION.LINES: {
  144. CV_LineSection sec;
  145. bits.ReadUInt32(out sec.off);
  146. bits.ReadUInt16(out sec.sec);
  147. bits.ReadUInt16(out sec.flags);
  148. bits.ReadUInt32(out sec.cod);
  149. int funcIndex = FindFunction(funcs, sec.sec, sec.off);
  150. if (funcIndex < 0) break;
  151. var func = funcs[funcIndex];
  152. if (func.lines == null) {
  153. while (funcIndex > 0) {
  154. var f = funcs[funcIndex-1];
  155. if (f.lines != null || f.segment != sec.sec || f.address != sec.off) break;
  156. func = f;
  157. funcIndex--;
  158. }
  159. } else {
  160. while (funcIndex < funcs.Length-1 && func.lines != null) {
  161. var f = funcs[funcIndex+1];
  162. if (f.segment != sec.sec || f.address != sec.off) break;
  163. func = f;
  164. funcIndex++;
  165. }
  166. }
  167. if (func.lines != null) break;
  168. // Count the line blocks.
  169. int begSym = bits.Position;
  170. int blocks = 0;
  171. while (bits.Position < endSym) {
  172. CV_SourceFile file;
  173. bits.ReadUInt32(out file.index);
  174. bits.ReadUInt32(out file.count);
  175. bits.ReadUInt32(out file.linsiz); // Size of payload.
  176. int linsiz = (int)file.count * (8 + ((sec.flags & 1) != 0 ? 4 : 0));
  177. bits.Position += linsiz;
  178. blocks++;
  179. }
  180. func.lines = new PdbLines[blocks];
  181. int block = 0;
  182. bits.Position = begSym;
  183. while (bits.Position < endSym) {
  184. CV_SourceFile file;
  185. bits.ReadUInt32(out file.index);
  186. bits.ReadUInt32(out file.count);
  187. bits.ReadUInt32(out file.linsiz); // Size of payload.
  188. PdbSource src = (PdbSource)checks[(int)file.index];
  189. if (src.language.Equals(BasicLanguageGuid))
  190. {
  191. func.AdjustVisualBasicScopes();
  192. }
  193. PdbLines tmp = new PdbLines(src, file.count);
  194. func.lines[block++] = tmp;
  195. PdbLine[] lines = tmp.lines;
  196. int plin = bits.Position;
  197. int pcol = bits.Position + 8 * (int)file.count;
  198. for (int i = 0; i < file.count; i++) {
  199. CV_Line line;
  200. CV_Column column = new CV_Column();
  201. bits.Position = plin + 8 * i;
  202. bits.ReadUInt32(out line.offset);
  203. bits.ReadUInt32(out line.flags);
  204. uint lineBegin = line.flags & (uint)CV_Line_Flags.linenumStart;
  205. uint delta = (line.flags & (uint)CV_Line_Flags.deltaLineEnd) >> 24;
  206. //bool statement = ((line.flags & (uint)CV_Line_Flags.fStatement) == 0);
  207. if ((sec.flags & 1) != 0) {
  208. bits.Position = pcol + 4 * i;
  209. bits.ReadUInt16(out column.offColumnStart);
  210. bits.ReadUInt16(out column.offColumnEnd);
  211. }
  212. lines[i] = new PdbLine(line.offset,
  213. lineBegin,
  214. column.offColumnStart,
  215. lineBegin+delta,
  216. column.offColumnEnd);
  217. }
  218. }
  219. break;
  220. }
  221. }
  222. bits.Position = endSym;
  223. }
  224. }
  225. static void LoadFuncsFromDbiModule(BitAccess bits,
  226. DbiModuleInfo info,
  227. IntHashTable names,
  228. List<PdbFunction> funcList,
  229. bool readStrings,
  230. MsfDirectory dir,
  231. Dictionary<string, int> nameIndex,
  232. PdbReader reader,
  233. Dictionary<string, PdbSource> sourceCache)
  234. {
  235. PdbFunction[] funcs = null;
  236. bits.Position = 0;
  237. int sig;
  238. bits.ReadInt32(out sig);
  239. if (sig != 4) {
  240. throw new PdbDebugException("Invalid signature. (sig={0})", sig);
  241. }
  242. bits.Position = 4;
  243. // Console.WriteLine("{0}:", info.moduleName);
  244. funcs = PdbFunction.LoadManagedFunctions(/*info.moduleName,*/
  245. bits, (uint)info.cbSyms,
  246. readStrings);
  247. if (funcs != null) {
  248. bits.Position = info.cbSyms + info.cbOldLines;
  249. LoadManagedLines(funcs, names, bits, dir, nameIndex, reader,
  250. (uint)(info.cbSyms + info.cbOldLines + info.cbLines),
  251. sourceCache);
  252. for (int i = 0; i < funcs.Length; i++) {
  253. funcList.Add(funcs[i]);
  254. }
  255. }
  256. }
  257. static void LoadDbiStream(BitAccess bits,
  258. out DbiModuleInfo[] modules,
  259. out DbiDbgHdr header,
  260. bool readStrings) {
  261. DbiHeader dh = new DbiHeader(bits);
  262. header = new DbiDbgHdr();
  263. //if (dh.sig != -1 || dh.ver != 19990903) {
  264. // throw new PdbException("Unsupported DBI Stream version, sig={0}, ver={1}",
  265. // dh.sig, dh.ver);
  266. //}
  267. // Read gpmod section.
  268. List<DbiModuleInfo> modList = new List<DbiModuleInfo>();
  269. int end = bits.Position + dh.gpmodiSize;
  270. while (bits.Position < end) {
  271. DbiModuleInfo mod = new DbiModuleInfo(bits, readStrings);
  272. modList.Add(mod);
  273. }
  274. if (bits.Position != end) {
  275. throw new PdbDebugException("Error reading DBI stream, pos={0} != {1}",
  276. bits.Position, end);
  277. }
  278. if (modList.Count > 0) {
  279. modules = modList.ToArray();
  280. } else {
  281. modules = null;
  282. }
  283. // Skip the Section Contribution substream.
  284. bits.Position += dh.secconSize;
  285. // Skip the Section Map substream.
  286. bits.Position += dh.secmapSize;
  287. // Skip the File Info substream.
  288. bits.Position += dh.filinfSize;
  289. // Skip the TSM substream.
  290. bits.Position += dh.tsmapSize;
  291. // Skip the EC substream.
  292. bits.Position += dh.ecinfoSize;
  293. // Read the optional header.
  294. end = bits.Position + dh.dbghdrSize;
  295. if (dh.dbghdrSize > 0) {
  296. header = new DbiDbgHdr(bits);
  297. }
  298. bits.Position = end;
  299. }
  300. internal static PdbInfo LoadFunctions(Stream read) {
  301. PdbInfo pdbInfo = new PdbInfo();
  302. pdbInfo.TokenToSourceMapping = new Dictionary<uint, PdbTokenLine>();
  303. BitAccess bits = new BitAccess(64 * 1024);
  304. PdbFileHeader head = new PdbFileHeader(read, bits);
  305. PdbReader reader = new PdbReader(read, head.pageSize);
  306. MsfDirectory dir = new MsfDirectory(reader, head, bits);
  307. DbiModuleInfo[] modules = null;
  308. DbiDbgHdr header;
  309. Dictionary<string, PdbSource> sourceCache = new Dictionary<string, PdbSource>();
  310. dir.streams[1].Read(reader, bits);
  311. Dictionary<string, int> nameIndex = LoadNameIndex(bits, out pdbInfo.Age, out pdbInfo.Guid);
  312. int nameStream;
  313. if (!nameIndex.TryGetValue("/NAMES", out nameStream)) {
  314. throw new PdbException("Could not find the '/NAMES' stream: the PDB file may be a public symbol file instead of a private symbol file");
  315. }
  316. dir.streams[nameStream].Read(reader, bits);
  317. IntHashTable names = LoadNameStream(bits);
  318. int srcsrvStream;
  319. if (!nameIndex.TryGetValue("SRCSRV", out srcsrvStream))
  320. pdbInfo.SourceServerData = string.Empty;
  321. else {
  322. DataStream dataStream = dir.streams[srcsrvStream];
  323. byte[] bytes = new byte[dataStream.contentSize];
  324. dataStream.Read(reader, bits);
  325. pdbInfo.SourceServerData = bits.ReadBString(bytes.Length);
  326. }
  327. int sourceLinkStream;
  328. if (nameIndex.TryGetValue("SOURCELINK", out sourceLinkStream)) {
  329. DataStream dataStream = dir.streams[sourceLinkStream];
  330. pdbInfo.SourceLinkData = new byte[dataStream.contentSize];
  331. dataStream.Read(reader, bits);
  332. bits.ReadBytes(pdbInfo.SourceLinkData);
  333. }
  334. dir.streams[3].Read(reader, bits);
  335. LoadDbiStream(bits, out modules, out header, true);
  336. List<PdbFunction> funcList = new List<PdbFunction>();
  337. if (modules != null) {
  338. for (int m = 0; m < modules.Length; m++) {
  339. var module = modules[m];
  340. if (module.stream > 0) {
  341. dir.streams[module.stream].Read(reader, bits);
  342. if (module.moduleName == "TokenSourceLineInfo") {
  343. LoadTokenToSourceInfo(bits, module, names, dir, nameIndex, reader, pdbInfo.TokenToSourceMapping, sourceCache);
  344. continue;
  345. }
  346. LoadFuncsFromDbiModule(bits, module, names, funcList, true, dir, nameIndex, reader, sourceCache);
  347. }
  348. }
  349. }
  350. PdbFunction[] funcs = funcList.ToArray();
  351. // After reading the functions, apply the token remapping table if it exists.
  352. if (header.snTokenRidMap != 0 && header.snTokenRidMap != 0xffff) {
  353. dir.streams[header.snTokenRidMap].Read(reader, bits);
  354. uint[] ridMap = new uint[dir.streams[header.snTokenRidMap].Length / 4];
  355. bits.ReadUInt32(ridMap);
  356. foreach (PdbFunction func in funcs) {
  357. func.token = 0x06000000 | ridMap[func.token & 0xffffff];
  358. }
  359. }
  360. //
  361. Array.Sort(funcs, PdbFunction.byAddressAndToken);
  362. //Array.Sort(funcs, PdbFunction.byToken);
  363. pdbInfo.Functions = funcs;
  364. return pdbInfo;
  365. }
  366. private static void LoadTokenToSourceInfo(BitAccess bits, DbiModuleInfo module, IntHashTable names, MsfDirectory dir,
  367. Dictionary<string, int> nameIndex, PdbReader reader, Dictionary<uint, PdbTokenLine> tokenToSourceMapping, Dictionary<string,PdbSource> sourceCache) {
  368. bits.Position = 0;
  369. int sig;
  370. bits.ReadInt32(out sig);
  371. if (sig != 4) {
  372. throw new PdbDebugException("Invalid signature. (sig={0})", sig);
  373. }
  374. bits.Position = 4;
  375. while (bits.Position < module.cbSyms) {
  376. ushort siz;
  377. ushort rec;
  378. bits.ReadUInt16(out siz);
  379. int star = bits.Position;
  380. int stop = bits.Position + siz;
  381. bits.Position = star;
  382. bits.ReadUInt16(out rec);
  383. switch ((SYM)rec) {
  384. case SYM.S_OEM:
  385. OemSymbol oem;
  386. bits.ReadGuid(out oem.idOem);
  387. bits.ReadUInt32(out oem.typind);
  388. // internal byte[] rgl; // user data, force 4-byte alignment
  389. if (oem.idOem == PdbFunction.msilMetaData) {
  390. string name = bits.ReadString();
  391. if (name == "TSLI") {
  392. uint token;
  393. uint file_id;
  394. uint line;
  395. uint column;
  396. uint endLine;
  397. uint endColumn;
  398. bits.ReadUInt32(out token);
  399. bits.ReadUInt32(out file_id);
  400. bits.ReadUInt32(out line);
  401. bits.ReadUInt32(out column);
  402. bits.ReadUInt32(out endLine);
  403. bits.ReadUInt32(out endColumn);
  404. PdbTokenLine tokenLine;
  405. if (!tokenToSourceMapping.TryGetValue(token, out tokenLine))
  406. tokenToSourceMapping.Add(token, new PdbTokenLine(token, file_id, line, column, endLine, endColumn));
  407. else {
  408. while (tokenLine.nextLine != null) tokenLine = tokenLine.nextLine;
  409. tokenLine.nextLine = new PdbTokenLine(token, file_id, line, column, endLine, endColumn);
  410. }
  411. }
  412. bits.Position = stop;
  413. break;
  414. } else {
  415. throw new PdbDebugException("OEM section: guid={0} ti={1}",
  416. oem.idOem, oem.typind);
  417. // bits.Position = stop;
  418. }
  419. case SYM.S_END:
  420. bits.Position = stop;
  421. break;
  422. default:
  423. //Console.WriteLine("{0,6}: {1:x2} {2}",
  424. // bits.Position, rec, (SYM)rec);
  425. bits.Position = stop;
  426. break;
  427. }
  428. }
  429. bits.Position = module.cbSyms + module.cbOldLines;
  430. int limit = module.cbSyms + module.cbOldLines + module.cbLines;
  431. IntHashTable sourceFiles = ReadSourceFileInfo(bits, (uint)limit, names, dir, nameIndex, reader, sourceCache);
  432. foreach (var tokenLine in tokenToSourceMapping.Values) {
  433. tokenLine.sourceFile = (PdbSource)sourceFiles[(int)tokenLine.file_id];
  434. }
  435. }
  436. public static readonly Guid SymDocumentType_Text = new Guid(1518771467, 26129, 4563, 189, 42, 0, 0, 248, 8, 73, 189);
  437. private static IntHashTable ReadSourceFileInfo(BitAccess bits, uint limit, IntHashTable names, MsfDirectory dir,
  438. Dictionary<string, int> nameIndex, PdbReader reader, Dictionary<string, PdbSource> sourceCache)
  439. {
  440. IntHashTable checks = new IntHashTable();
  441. int begin = bits.Position;
  442. while (bits.Position < limit) {
  443. int sig;
  444. int siz;
  445. bits.ReadInt32(out sig);
  446. bits.ReadInt32(out siz);
  447. int place = bits.Position;
  448. int endSym = bits.Position + siz;
  449. switch ((DEBUG_S_SUBSECTION)sig) {
  450. case DEBUG_S_SUBSECTION.FILECHKSMS:
  451. while (bits.Position < endSym) {
  452. CV_FileCheckSum chk;
  453. int ni = bits.Position - place;
  454. bits.ReadUInt32(out chk.name);
  455. bits.ReadUInt8(out chk.len);
  456. bits.ReadUInt8(out chk.type);
  457. string name = (string)names[(int)chk.name];
  458. PdbSource src;
  459. if (!sourceCache.TryGetValue(name, out src))
  460. {
  461. int guidStream;
  462. Guid doctypeGuid = SymDocumentType_Text;
  463. Guid languageGuid = Guid.Empty;
  464. Guid vendorGuid = Guid.Empty;
  465. Guid checksumAlgoGuid = Guid.Empty;
  466. byte[] checksum = null;
  467. if (nameIndex.TryGetValue("/SRC/FILES/" + name.ToUpperInvariant(), out guidStream))
  468. {
  469. var guidBits = new BitAccess(0x100);
  470. dir.streams[guidStream].Read(reader, guidBits);
  471. LoadInjectedSourceInformation(guidBits, out doctypeGuid, out languageGuid, out vendorGuid, out checksumAlgoGuid, out checksum);
  472. }
  473. src = new PdbSource(name, doctypeGuid, languageGuid, vendorGuid, checksumAlgoGuid, checksum);
  474. sourceCache.Add(name, src);
  475. }
  476. checks.Add(ni, src);
  477. bits.Position += chk.len;
  478. bits.Align(4);
  479. }
  480. bits.Position = endSym;
  481. break;
  482. default:
  483. bits.Position = endSym;
  484. break;
  485. }
  486. }
  487. return checks;
  488. }
  489. }
  490. }