PortablePdb.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. //
  2. // Author:
  3. // Jb Evain (jbevain@gmail.com)
  4. //
  5. // Copyright (c) 2008 - 2015 Jb Evain
  6. // Copyright (c) 2008 - 2011 Novell, Inc.
  7. //
  8. // Licensed under the MIT/X11 license.
  9. //
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.IO.Compression;
  14. using ILRuntime.Mono.Cecil.Metadata;
  15. using ILRuntime.Mono.Cecil.PE;
  16. namespace ILRuntime.Mono.Cecil.Cil {
  17. public sealed class PortablePdbReaderProvider : ISymbolReaderProvider {
  18. public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName)
  19. {
  20. Mixin.CheckModule (module);
  21. Mixin.CheckFileName (fileName);
  22. var file = File.OpenRead (Mixin.GetPdbFileName (fileName));
  23. return GetSymbolReader (module, Disposable.Owned (file as Stream), file.Name);
  24. }
  25. public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream)
  26. {
  27. Mixin.CheckModule (module);
  28. Mixin.CheckStream (symbolStream);
  29. return GetSymbolReader (module, Disposable.NotOwned (symbolStream), symbolStream.GetFileName ());
  30. }
  31. ISymbolReader GetSymbolReader (ModuleDefinition module, Disposable<Stream> symbolStream, string fileName)
  32. {
  33. return new PortablePdbReader (ImageReader.ReadPortablePdb (symbolStream, fileName), module);
  34. }
  35. }
  36. public sealed class PortablePdbReader : ISymbolReader {
  37. readonly Image image;
  38. readonly ModuleDefinition module;
  39. readonly MetadataReader reader;
  40. readonly MetadataReader debug_reader;
  41. bool IsEmbedded { get { return reader.image == debug_reader.image; } }
  42. internal PortablePdbReader (Image image, ModuleDefinition module)
  43. {
  44. this.image = image;
  45. this.module = module;
  46. this.reader = module.reader;
  47. this.debug_reader = new MetadataReader (image, module, this.reader);
  48. }
  49. public ISymbolWriterProvider GetWriterProvider ()
  50. {
  51. return new PortablePdbWriterProvider ();
  52. }
  53. public bool ProcessDebugHeader (ImageDebugHeader header)
  54. {
  55. if (image == module.Image)
  56. return true;
  57. var entry = header.GetCodeViewEntry ();
  58. if (entry == null)
  59. return false;
  60. var data = entry.Data;
  61. if (data.Length < 24)
  62. return false;
  63. var magic = ReadInt32 (data, 0);
  64. if (magic != 0x53445352)
  65. return false;
  66. var buffer = new byte [16];
  67. Buffer.BlockCopy (data, 4, buffer, 0, 16);
  68. var module_guid = new Guid (buffer);
  69. Buffer.BlockCopy (image.PdbHeap.Id, 0, buffer, 0, 16);
  70. var pdb_guid = new Guid (buffer);
  71. if (module_guid != pdb_guid)
  72. return false;
  73. ReadModule ();
  74. return true;
  75. }
  76. static int ReadInt32 (byte [] bytes, int start)
  77. {
  78. return (bytes [start]
  79. | (bytes [start + 1] << 8)
  80. | (bytes [start + 2] << 16)
  81. | (bytes [start + 3] << 24));
  82. }
  83. void ReadModule ()
  84. {
  85. module.custom_infos = debug_reader.GetCustomDebugInformation (module);
  86. }
  87. public MethodDebugInformation Read (MethodDefinition method)
  88. {
  89. var info = new MethodDebugInformation (method);
  90. ReadSequencePoints (info);
  91. ReadScope (info);
  92. ReadStateMachineKickOffMethod (info);
  93. ReadCustomDebugInformations (info);
  94. return info;
  95. }
  96. void ReadSequencePoints (MethodDebugInformation method_info)
  97. {
  98. method_info.sequence_points = debug_reader.ReadSequencePoints (method_info.method);
  99. }
  100. void ReadScope (MethodDebugInformation method_info)
  101. {
  102. method_info.scope = debug_reader.ReadScope (method_info.method);
  103. }
  104. void ReadStateMachineKickOffMethod (MethodDebugInformation method_info)
  105. {
  106. method_info.kickoff_method = debug_reader.ReadStateMachineKickoffMethod (method_info.method);
  107. }
  108. void ReadCustomDebugInformations (MethodDebugInformation info)
  109. {
  110. info.method.custom_infos = debug_reader.GetCustomDebugInformation (info.method);
  111. }
  112. public void Dispose ()
  113. {
  114. if (IsEmbedded)
  115. return;
  116. image.Dispose ();
  117. }
  118. }
  119. public sealed class EmbeddedPortablePdbReaderProvider : ISymbolReaderProvider {
  120. public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName)
  121. {
  122. Mixin.CheckModule (module);
  123. var header = module.GetDebugHeader ();
  124. var entry = header.GetEmbeddedPortablePdbEntry ();
  125. if (entry == null)
  126. throw new InvalidOperationException ();
  127. return new EmbeddedPortablePdbReader (
  128. (PortablePdbReader) new PortablePdbReaderProvider ().GetSymbolReader (module, GetPortablePdbStream (entry)));
  129. }
  130. static Stream GetPortablePdbStream (ImageDebugHeaderEntry entry)
  131. {
  132. var compressed_stream = new MemoryStream (entry.Data);
  133. var reader = new BinaryStreamReader (compressed_stream);
  134. reader.ReadInt32 (); // signature
  135. var length = reader.ReadInt32 ();
  136. var decompressed_stream = new MemoryStream (length);
  137. using (var deflate_stream = new DeflateStream (compressed_stream, CompressionMode.Decompress, leaveOpen: true))
  138. deflate_stream.CopyTo (decompressed_stream);
  139. return decompressed_stream;
  140. }
  141. public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream)
  142. {
  143. throw new NotSupportedException ();
  144. }
  145. }
  146. public sealed class EmbeddedPortablePdbReader : ISymbolReader
  147. {
  148. private readonly PortablePdbReader reader;
  149. internal EmbeddedPortablePdbReader (PortablePdbReader reader)
  150. {
  151. if (reader == null)
  152. throw new ArgumentNullException ();
  153. this.reader = reader;
  154. }
  155. public ISymbolWriterProvider GetWriterProvider ()
  156. {
  157. return new EmbeddedPortablePdbWriterProvider ();
  158. }
  159. public bool ProcessDebugHeader (ImageDebugHeader header)
  160. {
  161. return reader.ProcessDebugHeader (header);
  162. }
  163. public MethodDebugInformation Read (MethodDefinition method)
  164. {
  165. return reader.Read (method);
  166. }
  167. public void Dispose ()
  168. {
  169. reader.Dispose ();
  170. }
  171. }
  172. public sealed class PortablePdbWriterProvider : ISymbolWriterProvider
  173. {
  174. public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName)
  175. {
  176. Mixin.CheckModule (module);
  177. Mixin.CheckFileName (fileName);
  178. var file = File.OpenWrite (Mixin.GetPdbFileName (fileName));
  179. return GetSymbolWriter (module, Disposable.Owned (file as Stream));
  180. }
  181. public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream)
  182. {
  183. Mixin.CheckModule (module);
  184. Mixin.CheckStream (symbolStream);
  185. return GetSymbolWriter (module, Disposable.NotOwned (symbolStream));
  186. }
  187. ISymbolWriter GetSymbolWriter (ModuleDefinition module, Disposable<Stream> stream)
  188. {
  189. var metadata = new MetadataBuilder (module, this);
  190. var writer = ImageWriter.CreateDebugWriter (module, metadata, stream);
  191. return new PortablePdbWriter (metadata, module, writer);
  192. }
  193. }
  194. public sealed class PortablePdbWriter : ISymbolWriter {
  195. readonly MetadataBuilder pdb_metadata;
  196. readonly ModuleDefinition module;
  197. readonly ImageWriter writer;
  198. MetadataBuilder module_metadata;
  199. bool IsEmbedded { get { return writer == null; } }
  200. internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition module)
  201. {
  202. this.pdb_metadata = pdb_metadata;
  203. this.module = module;
  204. this.module_metadata = module.metadata_builder;
  205. if (module_metadata != pdb_metadata)
  206. this.pdb_metadata.metadata_builder = this.module_metadata;
  207. pdb_metadata.AddCustomDebugInformations (module);
  208. }
  209. internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition module, ImageWriter writer)
  210. : this (pdb_metadata, module)
  211. {
  212. this.writer = writer;
  213. }
  214. public ISymbolReaderProvider GetReaderProvider ()
  215. {
  216. return new PortablePdbReaderProvider ();
  217. }
  218. public ImageDebugHeader GetDebugHeader ()
  219. {
  220. if (IsEmbedded)
  221. return new ImageDebugHeader ();
  222. var directory = new ImageDebugDirectory () {
  223. MajorVersion = 256,
  224. MinorVersion = 20557,
  225. Type = ImageDebugType.CodeView,
  226. TimeDateStamp = (int) module.timestamp,
  227. };
  228. var buffer = new ByteBuffer ();
  229. // RSDS
  230. buffer.WriteUInt32 (0x53445352);
  231. // Module ID
  232. buffer.WriteBytes (module.Mvid.ToByteArray ());
  233. // PDB Age
  234. buffer.WriteUInt32 (1);
  235. // PDB Path
  236. buffer.WriteBytes (System.Text.Encoding.UTF8.GetBytes (writer.BaseStream.GetFileName ()));
  237. buffer.WriteByte (0);
  238. var data = new byte [buffer.length];
  239. Buffer.BlockCopy (buffer.buffer, 0, data, 0, buffer.length);
  240. directory.SizeOfData = data.Length;
  241. return new ImageDebugHeader (new ImageDebugHeaderEntry (directory, data));
  242. }
  243. public void Write (MethodDebugInformation info)
  244. {
  245. CheckMethodDebugInformationTable ();
  246. pdb_metadata.AddMethodDebugInformation (info);
  247. }
  248. void CheckMethodDebugInformationTable ()
  249. {
  250. var mdi = pdb_metadata.table_heap.GetTable<MethodDebugInformationTable> (Table.MethodDebugInformation);
  251. if (mdi.length > 0)
  252. return;
  253. // The MethodDebugInformation table has the same length as the Method table
  254. mdi.rows = new Row<uint, uint> [module_metadata.method_rid - 1];
  255. mdi.length = mdi.rows.Length;
  256. }
  257. public void Dispose ()
  258. {
  259. if (IsEmbedded)
  260. return;
  261. WritePdbFile ();
  262. }
  263. void WritePdbFile ()
  264. {
  265. WritePdbHeap ();
  266. WriteTableHeap ();
  267. writer.BuildMetadataTextMap ();
  268. writer.WriteMetadataHeader ();
  269. writer.WriteMetadata ();
  270. writer.Flush ();
  271. writer.stream.Dispose ();
  272. }
  273. void WritePdbHeap ()
  274. {
  275. var pdb_heap = pdb_metadata.pdb_heap;
  276. pdb_heap.WriteBytes (module.Mvid.ToByteArray ());
  277. pdb_heap.WriteUInt32 (module_metadata.timestamp);
  278. pdb_heap.WriteUInt32 (module_metadata.entry_point.ToUInt32 ());
  279. var table_heap = module_metadata.table_heap;
  280. var tables = table_heap.tables;
  281. ulong valid = 0;
  282. for (int i = 0; i < tables.Length; i++) {
  283. if (tables [i] == null || tables [i].Length == 0)
  284. continue;
  285. valid |= (1UL << i);
  286. }
  287. pdb_heap.WriteUInt64 (valid);
  288. for (int i = 0; i < tables.Length; i++) {
  289. if (tables [i] == null || tables [i].Length == 0)
  290. continue;
  291. pdb_heap.WriteUInt32 ((uint) tables [i].Length);
  292. }
  293. }
  294. void WriteTableHeap ()
  295. {
  296. pdb_metadata.table_heap.string_offsets = pdb_metadata.string_heap.WriteStrings ();
  297. pdb_metadata.table_heap.ComputeTableInformations ();
  298. pdb_metadata.table_heap.WriteTableHeap ();
  299. }
  300. }
  301. public sealed class EmbeddedPortablePdbWriterProvider : ISymbolWriterProvider {
  302. public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName)
  303. {
  304. Mixin.CheckModule (module);
  305. Mixin.CheckFileName (fileName);
  306. var stream = new MemoryStream ();
  307. var pdb_writer = (PortablePdbWriter) new PortablePdbWriterProvider ().GetSymbolWriter (module, stream);
  308. return new EmbeddedPortablePdbWriter (stream, pdb_writer);
  309. }
  310. public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream)
  311. {
  312. throw new NotSupportedException ();
  313. }
  314. }
  315. public sealed class EmbeddedPortablePdbWriter : ISymbolWriter {
  316. readonly Stream stream;
  317. readonly PortablePdbWriter writer;
  318. internal EmbeddedPortablePdbWriter (Stream stream, PortablePdbWriter writer)
  319. {
  320. this.stream = stream;
  321. this.writer = writer;
  322. }
  323. public ISymbolReaderProvider GetReaderProvider ()
  324. {
  325. return new EmbeddedPortablePdbReaderProvider ();
  326. }
  327. public ImageDebugHeader GetDebugHeader ()
  328. {
  329. writer.Dispose ();
  330. var directory = new ImageDebugDirectory {
  331. Type = ImageDebugType.EmbeddedPortablePdb,
  332. MajorVersion = 0x0100,
  333. MinorVersion = 0x0100,
  334. };
  335. var data = new MemoryStream ();
  336. var w = new BinaryStreamWriter (data);
  337. w.WriteByte (0x4d);
  338. w.WriteByte (0x50);
  339. w.WriteByte (0x44);
  340. w.WriteByte (0x42);
  341. w.WriteInt32 ((int) stream.Length);
  342. stream.Position = 0;
  343. using (var compress_stream = new DeflateStream (data, CompressionMode.Compress, leaveOpen: true))
  344. stream.CopyTo (compress_stream);
  345. directory.SizeOfData = (int) data.Length;
  346. return new ImageDebugHeader (new [] {
  347. writer.GetDebugHeader ().Entries [0],
  348. new ImageDebugHeaderEntry (directory, data.ToArray ())
  349. });
  350. }
  351. public void Write (MethodDebugInformation info)
  352. {
  353. writer.Write (info);
  354. }
  355. public void Dispose ()
  356. {
  357. }
  358. }
  359. static class PdbGuidMapping {
  360. static readonly Dictionary<Guid, DocumentLanguage> guid_language = new Dictionary<Guid, DocumentLanguage> ();
  361. static readonly Dictionary<DocumentLanguage, Guid> language_guid = new Dictionary<DocumentLanguage, Guid> ();
  362. static PdbGuidMapping ()
  363. {
  364. AddMapping (DocumentLanguage.C, new Guid ("63a08714-fc37-11d2-904c-00c04fa302a1"));
  365. AddMapping (DocumentLanguage.Cpp, new Guid ("3a12d0b7-c26c-11d0-b442-00a0244a1dd2"));
  366. AddMapping (DocumentLanguage.CSharp, new Guid ("3f5162f8-07c6-11d3-9053-00c04fa302a1"));
  367. AddMapping (DocumentLanguage.Basic, new Guid ("3a12d0b8-c26c-11d0-b442-00a0244a1dd2"));
  368. AddMapping (DocumentLanguage.Java, new Guid ("3a12d0b4-c26c-11d0-b442-00a0244a1dd2"));
  369. AddMapping (DocumentLanguage.Cobol, new Guid ("af046cd1-d0e1-11d2-977c-00a0c9b4d50c"));
  370. AddMapping (DocumentLanguage.Pascal, new Guid ("af046cd2-d0e1-11d2-977c-00a0c9b4d50c"));
  371. AddMapping (DocumentLanguage.Cil, new Guid ("af046cd3-d0e1-11d2-977c-00a0c9b4d50c"));
  372. AddMapping (DocumentLanguage.JScript, new Guid ("3a12d0b6-c26c-11d0-b442-00a0244a1dd2"));
  373. AddMapping (DocumentLanguage.Smc, new Guid ("0d9b9f7b-6611-11d3-bd2a-0000f80849bd"));
  374. AddMapping (DocumentLanguage.MCpp, new Guid ("4b35fde8-07c6-11d3-9053-00c04fa302a1"));
  375. AddMapping (DocumentLanguage.FSharp, new Guid ("ab4f38c9-b6e6-43ba-be3b-58080b2ccce3"));
  376. }
  377. static void AddMapping (DocumentLanguage language, Guid guid)
  378. {
  379. guid_language.Add (guid, language);
  380. language_guid.Add (language, guid);
  381. }
  382. static readonly Guid type_text = new Guid ("5a869d0b-6611-11d3-bd2a-0000f80849bd");
  383. public static DocumentType ToType (this Guid guid)
  384. {
  385. if (guid == type_text)
  386. return DocumentType.Text;
  387. return DocumentType.Other;
  388. }
  389. public static Guid ToGuid (this DocumentType type)
  390. {
  391. if (type == DocumentType.Text)
  392. return type_text;
  393. return new Guid ();
  394. }
  395. static readonly Guid hash_md5 = new Guid ("406ea660-64cf-4c82-b6f0-42d48172a799");
  396. static readonly Guid hash_sha1 = new Guid ("ff1816ec-aa5e-4d10-87f7-6f4963833460");
  397. static readonly Guid hash_sha256 = new Guid ("8829d00f-11b8-4213-878b-770e8597ac16");
  398. public static DocumentHashAlgorithm ToHashAlgorithm (this Guid guid)
  399. {
  400. if (guid == hash_md5)
  401. return DocumentHashAlgorithm.MD5;
  402. if (guid == hash_sha1)
  403. return DocumentHashAlgorithm.SHA1;
  404. if (guid == hash_sha256)
  405. return DocumentHashAlgorithm.SHA256;
  406. return DocumentHashAlgorithm.None;
  407. }
  408. public static Guid ToGuid (this DocumentHashAlgorithm hash_algo)
  409. {
  410. if (hash_algo == DocumentHashAlgorithm.MD5)
  411. return hash_md5;
  412. if (hash_algo == DocumentHashAlgorithm.SHA1)
  413. return hash_sha1;
  414. if (hash_algo == DocumentHashAlgorithm.SHA256)
  415. return hash_sha256;
  416. return new Guid ();
  417. }
  418. public static DocumentLanguage ToLanguage (this Guid guid)
  419. {
  420. DocumentLanguage language;
  421. if (!guid_language.TryGetValue (guid, out language))
  422. return DocumentLanguage.Other;
  423. return language;
  424. }
  425. public static Guid ToGuid (this DocumentLanguage language)
  426. {
  427. Guid guid;
  428. if (!language_guid.TryGetValue (language, out guid))
  429. return new Guid ();
  430. return guid;
  431. }
  432. static readonly Guid vendor_ms = new Guid ("994b45c4-e6e9-11d2-903f-00c04fa302a1");
  433. public static DocumentLanguageVendor ToVendor (this Guid guid)
  434. {
  435. if (guid == vendor_ms)
  436. return DocumentLanguageVendor.Microsoft;
  437. return DocumentLanguageVendor.Other;
  438. }
  439. public static Guid ToGuid (this DocumentLanguageVendor vendor)
  440. {
  441. if (vendor == DocumentLanguageVendor.Microsoft)
  442. return vendor_ms;
  443. return new Guid ();
  444. }
  445. }
  446. }