Symbols.cs 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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.Runtime.InteropServices;
  14. using SR = System.Reflection;
  15. using Mono.Collections.Generic;
  16. using Mono.Cecil.Cil;
  17. using Mono.Cecil.PE;
  18. namespace Mono.Cecil.Cil {
  19. [StructLayout (LayoutKind.Sequential)]
  20. public struct ImageDebugDirectory {
  21. public const int Size = 28;
  22. public int Characteristics;
  23. public int TimeDateStamp;
  24. public short MajorVersion;
  25. public short MinorVersion;
  26. public ImageDebugType Type;
  27. public int SizeOfData;
  28. public int AddressOfRawData;
  29. public int PointerToRawData;
  30. }
  31. public enum ImageDebugType {
  32. CodeView = 2,
  33. Deterministic = 16,
  34. EmbeddedPortablePdb = 17,
  35. }
  36. public sealed class ImageDebugHeader {
  37. readonly ImageDebugHeaderEntry [] entries;
  38. public bool HasEntries {
  39. get { return !entries.IsNullOrEmpty (); }
  40. }
  41. public ImageDebugHeaderEntry [] Entries {
  42. get { return entries; }
  43. }
  44. public ImageDebugHeader (ImageDebugHeaderEntry [] entries)
  45. {
  46. this.entries = entries ?? Empty<ImageDebugHeaderEntry>.Array;
  47. }
  48. public ImageDebugHeader ()
  49. : this (Empty<ImageDebugHeaderEntry>.Array)
  50. {
  51. }
  52. public ImageDebugHeader (ImageDebugHeaderEntry entry)
  53. : this (new [] { entry })
  54. {
  55. }
  56. }
  57. public sealed class ImageDebugHeaderEntry {
  58. ImageDebugDirectory directory;
  59. readonly byte [] data;
  60. public ImageDebugDirectory Directory {
  61. get { return directory; }
  62. internal set { directory = value; }
  63. }
  64. public byte [] Data {
  65. get { return data; }
  66. }
  67. public ImageDebugHeaderEntry (ImageDebugDirectory directory, byte [] data)
  68. {
  69. this.directory = directory;
  70. this.data = data ?? Empty<byte>.Array;
  71. }
  72. }
  73. public sealed class ScopeDebugInformation : DebugInformation {
  74. internal InstructionOffset start;
  75. internal InstructionOffset end;
  76. internal ImportDebugInformation import;
  77. internal Collection<ScopeDebugInformation> scopes;
  78. internal Collection<VariableDebugInformation> variables;
  79. internal Collection<ConstantDebugInformation> constants;
  80. public InstructionOffset Start {
  81. get { return start; }
  82. set { start = value; }
  83. }
  84. public InstructionOffset End {
  85. get { return end; }
  86. set { end = value; }
  87. }
  88. public ImportDebugInformation Import {
  89. get { return import; }
  90. set { import = value; }
  91. }
  92. public bool HasScopes {
  93. get { return !scopes.IsNullOrEmpty (); }
  94. }
  95. public Collection<ScopeDebugInformation> Scopes {
  96. get { return scopes ?? (scopes = new Collection<ScopeDebugInformation> ()); }
  97. }
  98. public bool HasVariables {
  99. get { return !variables.IsNullOrEmpty (); }
  100. }
  101. public Collection<VariableDebugInformation> Variables {
  102. get { return variables ?? (variables = new Collection<VariableDebugInformation> ()); }
  103. }
  104. public bool HasConstants {
  105. get { return !constants.IsNullOrEmpty (); }
  106. }
  107. public Collection<ConstantDebugInformation> Constants {
  108. get { return constants ?? (constants = new Collection<ConstantDebugInformation> ()); }
  109. }
  110. internal ScopeDebugInformation ()
  111. {
  112. this.token = new MetadataToken (TokenType.LocalScope);
  113. }
  114. public ScopeDebugInformation (Instruction start, Instruction end)
  115. : this ()
  116. {
  117. if (start == null)
  118. throw new ArgumentNullException ("start");
  119. this.start = new InstructionOffset (start);
  120. if (end != null)
  121. this.end = new InstructionOffset (end);
  122. }
  123. public bool TryGetName (VariableDefinition variable, out string name)
  124. {
  125. name = null;
  126. if (variables == null || variables.Count == 0)
  127. return false;
  128. for (int i = 0; i < variables.Count; i++) {
  129. if (variables [i].Index == variable.Index) {
  130. name = variables [i].Name;
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. }
  137. public struct InstructionOffset {
  138. readonly Instruction instruction;
  139. readonly int? offset;
  140. public int Offset {
  141. get {
  142. if (instruction != null)
  143. return instruction.Offset;
  144. if (offset.HasValue)
  145. return offset.Value;
  146. throw new NotSupportedException ();
  147. }
  148. }
  149. public bool IsEndOfMethod {
  150. get { return instruction == null && !offset.HasValue; }
  151. }
  152. public InstructionOffset (Instruction instruction)
  153. {
  154. if (instruction == null)
  155. throw new ArgumentNullException ("instruction");
  156. this.instruction = instruction;
  157. this.offset = null;
  158. }
  159. public InstructionOffset (int offset)
  160. {
  161. this.instruction = null;
  162. this.offset = offset;
  163. }
  164. }
  165. [Flags]
  166. public enum VariableAttributes : ushort {
  167. None = 0,
  168. DebuggerHidden = 1,
  169. }
  170. public struct VariableIndex {
  171. readonly VariableDefinition variable;
  172. readonly int? index;
  173. public int Index {
  174. get {
  175. if (variable != null)
  176. return variable.Index;
  177. if (index.HasValue)
  178. return index.Value;
  179. throw new NotSupportedException ();
  180. }
  181. }
  182. public VariableIndex (VariableDefinition variable)
  183. {
  184. if (variable == null)
  185. throw new ArgumentNullException ("variable");
  186. this.variable = variable;
  187. this.index = null;
  188. }
  189. public VariableIndex (int index)
  190. {
  191. this.variable = null;
  192. this.index = index;
  193. }
  194. }
  195. public abstract class DebugInformation : ICustomDebugInformationProvider {
  196. internal MetadataToken token;
  197. internal Collection<CustomDebugInformation> custom_infos;
  198. public MetadataToken MetadataToken {
  199. get { return token; }
  200. set { token = value; }
  201. }
  202. public bool HasCustomDebugInformations {
  203. get { return !custom_infos.IsNullOrEmpty (); }
  204. }
  205. public Collection<CustomDebugInformation> CustomDebugInformations {
  206. get { return custom_infos ?? (custom_infos = new Collection<CustomDebugInformation> ()); }
  207. }
  208. internal DebugInformation ()
  209. {
  210. }
  211. }
  212. public sealed class VariableDebugInformation : DebugInformation {
  213. string name;
  214. ushort attributes;
  215. internal VariableIndex index;
  216. public int Index {
  217. get { return index.Index; }
  218. }
  219. public string Name {
  220. get { return name; }
  221. set { name = value; }
  222. }
  223. public VariableAttributes Attributes {
  224. get { return (VariableAttributes) attributes; }
  225. set { attributes = (ushort) value; }
  226. }
  227. public bool IsDebuggerHidden {
  228. get { return attributes.GetAttributes ((ushort) VariableAttributes.DebuggerHidden); }
  229. set { attributes = attributes.SetAttributes ((ushort) VariableAttributes.DebuggerHidden, value); }
  230. }
  231. internal VariableDebugInformation (int index, string name)
  232. {
  233. if (name == null)
  234. throw new ArgumentNullException ("name");
  235. this.index = new VariableIndex (index);
  236. this.name = name;
  237. }
  238. public VariableDebugInformation (VariableDefinition variable, string name)
  239. {
  240. if (variable == null)
  241. throw new ArgumentNullException ("variable");
  242. if (name == null)
  243. throw new ArgumentNullException ("name");
  244. this.index = new VariableIndex (variable);
  245. this.name = name;
  246. this.token = new MetadataToken (TokenType.LocalVariable);
  247. }
  248. }
  249. public sealed class ConstantDebugInformation : DebugInformation {
  250. string name;
  251. TypeReference constant_type;
  252. object value;
  253. public string Name {
  254. get { return name; }
  255. set { name = value; }
  256. }
  257. public TypeReference ConstantType {
  258. get { return constant_type; }
  259. set { constant_type = value; }
  260. }
  261. public object Value {
  262. get { return value; }
  263. set { this.value = value; }
  264. }
  265. public ConstantDebugInformation (string name, TypeReference constant_type, object value)
  266. {
  267. if (name == null)
  268. throw new ArgumentNullException ("name");
  269. this.name = name;
  270. this.constant_type = constant_type;
  271. this.value = value;
  272. this.token = new MetadataToken (TokenType.LocalConstant);
  273. }
  274. }
  275. public enum ImportTargetKind : byte {
  276. ImportNamespace = 1,
  277. ImportNamespaceInAssembly = 2,
  278. ImportType = 3,
  279. ImportXmlNamespaceWithAlias = 4,
  280. ImportAlias = 5,
  281. DefineAssemblyAlias = 6,
  282. DefineNamespaceAlias = 7,
  283. DefineNamespaceInAssemblyAlias = 8,
  284. DefineTypeAlias = 9,
  285. }
  286. public sealed class ImportTarget {
  287. internal ImportTargetKind kind;
  288. internal string @namespace;
  289. internal TypeReference type;
  290. internal AssemblyNameReference reference;
  291. internal string alias;
  292. public string Namespace {
  293. get { return @namespace; }
  294. set { @namespace = value; }
  295. }
  296. public TypeReference Type {
  297. get { return type; }
  298. set { type = value; }
  299. }
  300. public AssemblyNameReference AssemblyReference {
  301. get { return reference; }
  302. set { reference = value; }
  303. }
  304. public string Alias {
  305. get { return alias; }
  306. set { alias = value; }
  307. }
  308. public ImportTargetKind Kind {
  309. get { return kind; }
  310. set { kind = value; }
  311. }
  312. public ImportTarget (ImportTargetKind kind)
  313. {
  314. this.kind = kind;
  315. }
  316. }
  317. public sealed class ImportDebugInformation : DebugInformation {
  318. internal ImportDebugInformation parent;
  319. internal Collection<ImportTarget> targets;
  320. public bool HasTargets {
  321. get { return !targets.IsNullOrEmpty (); }
  322. }
  323. public Collection<ImportTarget> Targets {
  324. get { return targets ?? (targets = new Collection<ImportTarget> ()); }
  325. }
  326. public ImportDebugInformation Parent {
  327. get { return parent; }
  328. set { parent = value; }
  329. }
  330. public ImportDebugInformation ()
  331. {
  332. this.token = new MetadataToken (TokenType.ImportScope);
  333. }
  334. }
  335. public interface ICustomDebugInformationProvider : IMetadataTokenProvider {
  336. bool HasCustomDebugInformations { get; }
  337. Collection<CustomDebugInformation> CustomDebugInformations { get; }
  338. }
  339. public enum CustomDebugInformationKind {
  340. Binary,
  341. StateMachineScope,
  342. DynamicVariable,
  343. DefaultNamespace,
  344. AsyncMethodBody,
  345. EmbeddedSource,
  346. SourceLink,
  347. }
  348. public abstract class CustomDebugInformation : DebugInformation {
  349. Guid identifier;
  350. public Guid Identifier {
  351. get { return identifier; }
  352. }
  353. public abstract CustomDebugInformationKind Kind { get; }
  354. internal CustomDebugInformation (Guid identifier)
  355. {
  356. this.identifier = identifier;
  357. this.token = new MetadataToken (TokenType.CustomDebugInformation);
  358. }
  359. }
  360. public sealed class BinaryCustomDebugInformation : CustomDebugInformation {
  361. byte [] data;
  362. public byte [] Data {
  363. get { return data; }
  364. set { data = value; }
  365. }
  366. public override CustomDebugInformationKind Kind {
  367. get { return CustomDebugInformationKind.Binary; }
  368. }
  369. public BinaryCustomDebugInformation (Guid identifier, byte [] data)
  370. : base (identifier)
  371. {
  372. this.data = data;
  373. }
  374. }
  375. public sealed class AsyncMethodBodyDebugInformation : CustomDebugInformation {
  376. internal InstructionOffset catch_handler;
  377. internal Collection<InstructionOffset> yields;
  378. internal Collection<InstructionOffset> resumes;
  379. internal Collection<MethodDefinition> resume_methods;
  380. public InstructionOffset CatchHandler {
  381. get { return catch_handler; }
  382. set { catch_handler = value; }
  383. }
  384. public Collection<InstructionOffset> Yields {
  385. get { return yields ?? (yields = new Collection<InstructionOffset> ()); }
  386. }
  387. public Collection<InstructionOffset> Resumes {
  388. get { return resumes ?? (resumes = new Collection<InstructionOffset> ()); }
  389. }
  390. public Collection<MethodDefinition> ResumeMethods {
  391. get { return resume_methods ?? (resume_methods = new Collection<MethodDefinition> ()); }
  392. }
  393. public override CustomDebugInformationKind Kind {
  394. get { return CustomDebugInformationKind.AsyncMethodBody; }
  395. }
  396. public static Guid KindIdentifier = new Guid ("{54FD2AC5-E925-401A-9C2A-F94F171072F8}");
  397. internal AsyncMethodBodyDebugInformation (int catchHandler)
  398. : base (KindIdentifier)
  399. {
  400. this.catch_handler = new InstructionOffset (catchHandler);
  401. }
  402. public AsyncMethodBodyDebugInformation (Instruction catchHandler)
  403. : base (KindIdentifier)
  404. {
  405. this.catch_handler = new InstructionOffset (catchHandler);
  406. }
  407. public AsyncMethodBodyDebugInformation ()
  408. : base (KindIdentifier)
  409. {
  410. this.catch_handler = new InstructionOffset (-1);
  411. }
  412. }
  413. public sealed class StateMachineScope {
  414. internal InstructionOffset start;
  415. internal InstructionOffset end;
  416. public InstructionOffset Start {
  417. get { return start; }
  418. set { start = value; }
  419. }
  420. public InstructionOffset End {
  421. get { return end; }
  422. set { end = value; }
  423. }
  424. internal StateMachineScope (int start, int end)
  425. {
  426. this.start = new InstructionOffset (start);
  427. this.end = new InstructionOffset (end);
  428. }
  429. public StateMachineScope (Instruction start, Instruction end)
  430. {
  431. this.start = new InstructionOffset (start);
  432. this.end = end != null ? new InstructionOffset (end) : new InstructionOffset ();
  433. }
  434. }
  435. public sealed class StateMachineScopeDebugInformation : CustomDebugInformation {
  436. internal Collection<StateMachineScope> scopes;
  437. public Collection<StateMachineScope> Scopes {
  438. get { return scopes ?? (scopes = new Collection<StateMachineScope> ()); }
  439. }
  440. public override CustomDebugInformationKind Kind {
  441. get { return CustomDebugInformationKind.StateMachineScope; }
  442. }
  443. public static Guid KindIdentifier = new Guid ("{6DA9A61E-F8C7-4874-BE62-68BC5630DF71}");
  444. public StateMachineScopeDebugInformation ()
  445. : base (KindIdentifier)
  446. {
  447. }
  448. }
  449. public sealed class EmbeddedSourceDebugInformation : CustomDebugInformation {
  450. internal byte [] content;
  451. internal bool compress;
  452. public byte [] Content {
  453. get { return content; }
  454. set { content = value; }
  455. }
  456. public bool Compress {
  457. get { return compress; }
  458. set { compress = value; }
  459. }
  460. public override CustomDebugInformationKind Kind {
  461. get { return CustomDebugInformationKind.EmbeddedSource; }
  462. }
  463. public static Guid KindIdentifier = new Guid ("{0E8A571B-6926-466E-B4AD-8AB04611F5FE}");
  464. public EmbeddedSourceDebugInformation (byte [] content, bool compress)
  465. : base (KindIdentifier)
  466. {
  467. this.content = content;
  468. this.compress = compress;
  469. }
  470. }
  471. public sealed class SourceLinkDebugInformation : CustomDebugInformation {
  472. internal string content;
  473. public string Content {
  474. get { return content; }
  475. set { content = value; }
  476. }
  477. public override CustomDebugInformationKind Kind {
  478. get { return CustomDebugInformationKind.SourceLink; }
  479. }
  480. public static Guid KindIdentifier = new Guid ("{CC110556-A091-4D38-9FEC-25AB9A351A6A}");
  481. public SourceLinkDebugInformation (string content)
  482. : base (KindIdentifier)
  483. {
  484. this.content = content;
  485. }
  486. }
  487. public sealed class MethodDebugInformation : DebugInformation {
  488. internal MethodDefinition method;
  489. internal Collection<SequencePoint> sequence_points;
  490. internal ScopeDebugInformation scope;
  491. internal MethodDefinition kickoff_method;
  492. internal int code_size;
  493. internal MetadataToken local_var_token;
  494. public MethodDefinition Method {
  495. get { return method; }
  496. }
  497. public bool HasSequencePoints {
  498. get { return !sequence_points.IsNullOrEmpty (); }
  499. }
  500. public Collection<SequencePoint> SequencePoints {
  501. get { return sequence_points ?? (sequence_points = new Collection<SequencePoint> ()); }
  502. }
  503. public ScopeDebugInformation Scope {
  504. get { return scope; }
  505. set { scope = value; }
  506. }
  507. public MethodDefinition StateMachineKickOffMethod {
  508. get { return kickoff_method; }
  509. set { kickoff_method = value; }
  510. }
  511. internal MethodDebugInformation (MethodDefinition method)
  512. {
  513. if (method == null)
  514. throw new ArgumentNullException ("method");
  515. this.method = method;
  516. this.token = new MetadataToken (TokenType.MethodDebugInformation, method.MetadataToken.RID);
  517. }
  518. public SequencePoint GetSequencePoint (Instruction instruction)
  519. {
  520. if (!HasSequencePoints)
  521. return null;
  522. for (int i = 0; i < sequence_points.Count; i++)
  523. if (sequence_points [i].Offset == instruction.Offset)
  524. return sequence_points [i];
  525. return null;
  526. }
  527. public IDictionary<Instruction, SequencePoint> GetSequencePointMapping ()
  528. {
  529. var instruction_mapping = new Dictionary<Instruction, SequencePoint> ();
  530. if (!HasSequencePoints || !method.HasBody)
  531. return instruction_mapping;
  532. var offset_mapping = new Dictionary<int, SequencePoint> (sequence_points.Count);
  533. for (int i = 0; i < sequence_points.Count; i++) {
  534. if (!offset_mapping.ContainsKey (sequence_points [i].Offset))
  535. offset_mapping.Add (sequence_points [i].Offset, sequence_points [i]);
  536. }
  537. var instructions = method.Body.Instructions;
  538. for (int i = 0; i < instructions.Count; i++) {
  539. SequencePoint sequence_point;
  540. if (offset_mapping.TryGetValue (instructions [i].Offset, out sequence_point))
  541. instruction_mapping.Add (instructions [i], sequence_point);
  542. }
  543. return instruction_mapping;
  544. }
  545. public IEnumerable<ScopeDebugInformation> GetScopes ()
  546. {
  547. if (scope == null)
  548. return Empty<ScopeDebugInformation>.Array;
  549. return GetScopes (new[] { scope });
  550. }
  551. static IEnumerable<ScopeDebugInformation> GetScopes (IList<ScopeDebugInformation> scopes)
  552. {
  553. for (int i = 0; i < scopes.Count; i++) {
  554. var scope = scopes [i];
  555. yield return scope;
  556. if (!scope.HasScopes)
  557. continue;
  558. foreach (var sub_scope in GetScopes (scope.Scopes))
  559. yield return sub_scope;
  560. }
  561. }
  562. public bool TryGetName (VariableDefinition variable, out string name)
  563. {
  564. name = null;
  565. var has_name = false;
  566. var unique_name = "";
  567. foreach (var scope in GetScopes ()) {
  568. string slot_name;
  569. if (!scope.TryGetName (variable, out slot_name))
  570. continue;
  571. if (!has_name) {
  572. has_name = true;
  573. unique_name = slot_name;
  574. continue;
  575. }
  576. if (unique_name != slot_name)
  577. return false;
  578. }
  579. name = unique_name;
  580. return has_name;
  581. }
  582. }
  583. public interface ISymbolReader : IDisposable {
  584. #if !READ_ONLY
  585. ISymbolWriterProvider GetWriterProvider ();
  586. #endif
  587. bool ProcessDebugHeader (ImageDebugHeader header);
  588. MethodDebugInformation Read (MethodDefinition method);
  589. }
  590. public interface ISymbolReaderProvider {
  591. ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName);
  592. ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream);
  593. }
  594. #if !NET_CORE
  595. [Serializable]
  596. #endif
  597. public sealed class SymbolsNotFoundException : FileNotFoundException {
  598. public SymbolsNotFoundException (string message) : base (message)
  599. {
  600. }
  601. #if !NET_CORE
  602. SymbolsNotFoundException (
  603. System.Runtime.Serialization.SerializationInfo info,
  604. System.Runtime.Serialization.StreamingContext context)
  605. : base (info, context)
  606. {
  607. }
  608. #endif
  609. }
  610. #if !NET_CORE
  611. [Serializable]
  612. #endif
  613. public sealed class SymbolsNotMatchingException : InvalidOperationException {
  614. public SymbolsNotMatchingException (string message) : base (message)
  615. {
  616. }
  617. #if !NET_CORE
  618. SymbolsNotMatchingException (
  619. System.Runtime.Serialization.SerializationInfo info,
  620. System.Runtime.Serialization.StreamingContext context)
  621. : base (info, context)
  622. {
  623. }
  624. #endif
  625. }
  626. public class DefaultSymbolReaderProvider : ISymbolReaderProvider {
  627. readonly bool throw_if_no_symbol;
  628. public DefaultSymbolReaderProvider ()
  629. : this (throwIfNoSymbol: true)
  630. {
  631. }
  632. public DefaultSymbolReaderProvider (bool throwIfNoSymbol)
  633. {
  634. throw_if_no_symbol = throwIfNoSymbol;
  635. }
  636. public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName)
  637. {
  638. if (module.Image.HasDebugTables ())
  639. return null;
  640. if (module.HasDebugHeader) {
  641. var header = module.GetDebugHeader ();
  642. var entry = header.GetEmbeddedPortablePdbEntry ();
  643. if (entry != null)
  644. return new EmbeddedPortablePdbReaderProvider ().GetSymbolReader (module, fileName);
  645. }
  646. var pdb_file_name = Mixin.GetPdbFileName (fileName);
  647. if (File.Exists (pdb_file_name)) {
  648. if (Mixin.IsPortablePdb (Mixin.GetPdbFileName (fileName)))
  649. return new PortablePdbReaderProvider ().GetSymbolReader (module, fileName);
  650. try {
  651. return SymbolProvider.GetReaderProvider (SymbolKind.NativePdb).GetSymbolReader (module, fileName);
  652. } catch (Exception) {
  653. // We might not include support for native pdbs.
  654. }
  655. }
  656. var mdb_file_name = Mixin.GetMdbFileName (fileName);
  657. if (File.Exists (mdb_file_name)) {
  658. try {
  659. return SymbolProvider.GetReaderProvider (SymbolKind.Mdb).GetSymbolReader (module, fileName);
  660. } catch (Exception) {
  661. // We might not include support for mdbs.
  662. }
  663. }
  664. if (throw_if_no_symbol)
  665. throw new SymbolsNotFoundException (string.Format ("No symbol found for file: {0}", fileName));
  666. return null;
  667. }
  668. public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream)
  669. {
  670. if (module.Image.HasDebugTables ())
  671. return null;
  672. if (module.HasDebugHeader) {
  673. var header = module.GetDebugHeader ();
  674. var entry = header.GetEmbeddedPortablePdbEntry ();
  675. if (entry != null)
  676. return new EmbeddedPortablePdbReaderProvider ().GetSymbolReader (module, "");
  677. }
  678. Mixin.CheckStream (symbolStream);
  679. Mixin.CheckReadSeek (symbolStream);
  680. var position = symbolStream.Position;
  681. const int portablePdbHeader = 0x424a5342;
  682. var reader = new BinaryStreamReader (symbolStream);
  683. var intHeader = reader.ReadInt32 ();
  684. symbolStream.Position = position;
  685. if (intHeader == portablePdbHeader) {
  686. return new PortablePdbReaderProvider ().GetSymbolReader (module, symbolStream);
  687. }
  688. const string nativePdbHeader = "Microsoft C/C++ MSF 7.00";
  689. var bytesHeader = reader.ReadBytes (nativePdbHeader.Length);
  690. symbolStream.Position = position;
  691. var isNativePdb = true;
  692. for (var i = 0; i < bytesHeader.Length; i++) {
  693. if (bytesHeader [i] != (byte) nativePdbHeader [i]) {
  694. isNativePdb = false;
  695. break;
  696. }
  697. }
  698. if (isNativePdb) {
  699. try {
  700. return SymbolProvider.GetReaderProvider (SymbolKind.NativePdb).GetSymbolReader (module, symbolStream);
  701. } catch (Exception) {
  702. // We might not include support for native pdbs.
  703. }
  704. }
  705. const long mdbHeader = 0x45e82623fd7fa614;
  706. var longHeader = reader.ReadInt64 ();
  707. symbolStream.Position = position;
  708. if (longHeader == mdbHeader) {
  709. try {
  710. return SymbolProvider.GetReaderProvider (SymbolKind.Mdb).GetSymbolReader (module, symbolStream);
  711. } catch (Exception) {
  712. // We might not include support for mdbs.
  713. }
  714. }
  715. if (throw_if_no_symbol)
  716. throw new SymbolsNotFoundException (string.Format ("No symbols found in stream"));
  717. return null;
  718. }
  719. }
  720. enum SymbolKind {
  721. NativePdb,
  722. PortablePdb,
  723. EmbeddedPortablePdb,
  724. Mdb,
  725. }
  726. static class SymbolProvider {
  727. static SR.AssemblyName GetSymbolAssemblyName (SymbolKind kind)
  728. {
  729. if (kind == SymbolKind.PortablePdb)
  730. throw new ArgumentException ();
  731. var suffix = GetSymbolNamespace (kind);
  732. var cecil_name = typeof (SymbolProvider).Assembly ().GetName ();
  733. var name = new SR.AssemblyName {
  734. Name = cecil_name.Name + "." + suffix,
  735. Version = cecil_name.Version,
  736. CultureInfo = cecil_name.CultureInfo,
  737. };
  738. name.SetPublicKeyToken (cecil_name.GetPublicKeyToken ());
  739. return name;
  740. }
  741. static Type GetSymbolType (SymbolKind kind, string fullname)
  742. {
  743. var type = Type.GetType (fullname);
  744. if (type != null)
  745. return type;
  746. var assembly_name = GetSymbolAssemblyName (kind);
  747. type = Type.GetType (fullname + ", " + assembly_name.FullName);
  748. if (type != null)
  749. return type;
  750. try {
  751. var assembly = SR.Assembly.Load (assembly_name);
  752. if (assembly != null)
  753. return assembly.GetType (fullname);
  754. } catch (FileNotFoundException) {
  755. } catch (FileLoadException) {
  756. }
  757. return null;
  758. }
  759. public static ISymbolReaderProvider GetReaderProvider (SymbolKind kind)
  760. {
  761. if (kind == SymbolKind.PortablePdb)
  762. return new PortablePdbReaderProvider ();
  763. if (kind == SymbolKind.EmbeddedPortablePdb)
  764. return new EmbeddedPortablePdbReaderProvider ();
  765. var provider_name = GetSymbolTypeName (kind, "ReaderProvider");
  766. var type = GetSymbolType (kind, provider_name);
  767. if (type == null)
  768. throw new TypeLoadException ("Could not find symbol provider type " + provider_name);
  769. return (ISymbolReaderProvider) Activator.CreateInstance (type);
  770. }
  771. static string GetSymbolTypeName (SymbolKind kind, string name)
  772. {
  773. return "Mono.Cecil" + "." + GetSymbolNamespace (kind) + "." + kind + name;
  774. }
  775. static string GetSymbolNamespace (SymbolKind kind)
  776. {
  777. if (kind == SymbolKind.PortablePdb || kind == SymbolKind.EmbeddedPortablePdb)
  778. return "Cil";
  779. if (kind == SymbolKind.NativePdb)
  780. return "Pdb";
  781. if (kind == SymbolKind.Mdb)
  782. return "Mdb";
  783. throw new ArgumentException ();
  784. }
  785. }
  786. #if !READ_ONLY
  787. public interface ISymbolWriter : IDisposable {
  788. ISymbolReaderProvider GetReaderProvider ();
  789. ImageDebugHeader GetDebugHeader ();
  790. void Write (MethodDebugInformation info);
  791. }
  792. public interface ISymbolWriterProvider {
  793. ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName);
  794. ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream);
  795. }
  796. public class DefaultSymbolWriterProvider : ISymbolWriterProvider {
  797. public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName)
  798. {
  799. var reader = module.SymbolReader;
  800. if (reader == null)
  801. throw new InvalidOperationException ();
  802. if (module.Image != null && module.Image.HasDebugTables ())
  803. return null;
  804. return reader.GetWriterProvider ().GetSymbolWriter (module, fileName);
  805. }
  806. public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream)
  807. {
  808. throw new NotSupportedException ();
  809. }
  810. }
  811. #endif
  812. }
  813. namespace Mono.Cecil {
  814. static partial class Mixin {
  815. public static ImageDebugHeaderEntry GetCodeViewEntry (this ImageDebugHeader header)
  816. {
  817. return GetEntry (header, ImageDebugType.CodeView);
  818. }
  819. public static ImageDebugHeaderEntry GetDeterministicEntry (this ImageDebugHeader header)
  820. {
  821. return GetEntry (header, ImageDebugType.Deterministic);
  822. }
  823. public static ImageDebugHeader AddDeterministicEntry (this ImageDebugHeader header)
  824. {
  825. var entry = new ImageDebugHeaderEntry (new ImageDebugDirectory { Type = ImageDebugType.Deterministic }, Empty<byte>.Array);
  826. if (header == null)
  827. return new ImageDebugHeader (entry);
  828. var entries = new ImageDebugHeaderEntry [header.Entries.Length + 1];
  829. Array.Copy (header.Entries, entries, header.Entries.Length);
  830. entries [entries.Length - 1] = entry;
  831. return new ImageDebugHeader (entries);
  832. }
  833. public static ImageDebugHeaderEntry GetEmbeddedPortablePdbEntry (this ImageDebugHeader header)
  834. {
  835. return GetEntry (header, ImageDebugType.EmbeddedPortablePdb);
  836. }
  837. private static ImageDebugHeaderEntry GetEntry (this ImageDebugHeader header, ImageDebugType type)
  838. {
  839. if (!header.HasEntries)
  840. return null;
  841. for (var i = 0; i < header.Entries.Length; i++) {
  842. var entry = header.Entries [i];
  843. if (entry.Directory.Type == type)
  844. return entry;
  845. }
  846. return null;
  847. }
  848. public static string GetPdbFileName (string assemblyFileName)
  849. {
  850. return Path.ChangeExtension (assemblyFileName, ".pdb");
  851. }
  852. public static string GetMdbFileName (string assemblyFileName)
  853. {
  854. return assemblyFileName + ".mdb";
  855. }
  856. public static bool IsPortablePdb (string fileName)
  857. {
  858. using (var file = new FileStream (fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  859. return IsPortablePdb (file);
  860. }
  861. public static bool IsPortablePdb (Stream stream)
  862. {
  863. const uint ppdb_signature = 0x424a5342;
  864. if (stream.Length < 4) return false;
  865. var position = stream.Position;
  866. try {
  867. var reader = new BinaryReader (stream);
  868. return reader.ReadUInt32 () == ppdb_signature;
  869. } finally {
  870. stream.Position = position;
  871. }
  872. }
  873. }
  874. }