PdbFile.cs 15 KB

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