Symbols.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // Symbols.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Runtime.InteropServices;
  31. using SR = System.Reflection;
  32. using Mono.Collections.Generic;
  33. namespace Mono.Cecil.Cil {
  34. [StructLayout (LayoutKind.Sequential)]
  35. public struct ImageDebugDirectory {
  36. public int Characteristics;
  37. public int TimeDateStamp;
  38. public short MajorVersion;
  39. public short MinorVersion;
  40. public int Type;
  41. public int SizeOfData;
  42. public int AddressOfRawData;
  43. public int PointerToRawData;
  44. }
  45. public sealed class Scope : IVariableDefinitionProvider {
  46. Instruction start;
  47. Instruction end;
  48. Collection<Scope> scopes;
  49. Collection<VariableDefinition> variables;
  50. public Instruction Start {
  51. get { return start; }
  52. set { start = value; }
  53. }
  54. public Instruction End {
  55. get { return end; }
  56. set { end = value; }
  57. }
  58. public bool HasScopes {
  59. get { return !Mixin.IsNullOrEmpty (scopes); }
  60. }
  61. public Collection<Scope> Scopes {
  62. get {
  63. if (scopes == null)
  64. scopes = new Collection<Scope> ();
  65. return scopes;
  66. }
  67. }
  68. public bool HasVariables {
  69. get { return !Mixin.IsNullOrEmpty (variables); }
  70. }
  71. public Collection<VariableDefinition> Variables {
  72. get {
  73. if (variables == null)
  74. variables = new Collection<VariableDefinition> ();
  75. return variables;
  76. }
  77. }
  78. }
  79. public struct InstructionSymbol {
  80. public readonly int Offset;
  81. public readonly SequencePoint SequencePoint;
  82. public InstructionSymbol (int offset, SequencePoint sequencePoint)
  83. {
  84. this.Offset = offset;
  85. this.SequencePoint = sequencePoint;
  86. }
  87. }
  88. public sealed class MethodSymbols {
  89. internal int code_size;
  90. internal string method_name;
  91. internal MetadataToken method_token;
  92. internal MetadataToken local_var_token;
  93. internal Collection<VariableDefinition> variables;
  94. public Collection<InstructionSymbol> instructions;
  95. public bool HasVariables {
  96. get { return !Mixin.IsNullOrEmpty (variables); }
  97. }
  98. public Collection<VariableDefinition> Variables {
  99. get {
  100. if (variables == null)
  101. variables = new Collection<VariableDefinition> ();
  102. return variables;
  103. }
  104. }
  105. public Collection<InstructionSymbol> Instructions {
  106. get {
  107. if (instructions == null)
  108. instructions = new Collection<InstructionSymbol> ();
  109. return instructions;
  110. }
  111. }
  112. public int CodeSize {
  113. get { return code_size; }
  114. }
  115. public string MethodName {
  116. get { return method_name; }
  117. }
  118. public MetadataToken MethodToken {
  119. get { return method_token; }
  120. }
  121. public MetadataToken LocalVarToken {
  122. get { return local_var_token; }
  123. }
  124. internal MethodSymbols (string methodName)
  125. {
  126. this.method_name = methodName;
  127. }
  128. public MethodSymbols (MetadataToken methodToken)
  129. {
  130. this.method_token = methodToken;
  131. }
  132. }
  133. public delegate Instruction InstructionMapper (int offset);
  134. public interface ISymbolReader : IDisposable {
  135. bool ProcessDebugHeader (ImageDebugDirectory directory, byte [] header);
  136. void Read (MethodBody body, InstructionMapper mapper);
  137. void Read (MethodSymbols symbols);
  138. }
  139. public interface ISymbolReaderProvider {
  140. ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName);
  141. ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream);
  142. }
  143. static class SymbolProvider {
  144. static readonly string symbol_kind = Type.GetType ("Mono.Runtime") != null ? "Mdb" : "Pdb";
  145. static SR.AssemblyName GetPlatformSymbolAssemblyName ()
  146. {
  147. var cecil_name = typeof (SymbolProvider).Assembly.GetName ();
  148. var name = new SR.AssemblyName {
  149. Name = "Mono.Cecil." + symbol_kind,
  150. Version = cecil_name.Version,
  151. };
  152. name.SetPublicKeyToken (cecil_name.GetPublicKeyToken ());
  153. return name;
  154. }
  155. static Type GetPlatformType (string fullname)
  156. {
  157. var type = Type.GetType (fullname);
  158. if (type != null)
  159. return type;
  160. var assembly_name = GetPlatformSymbolAssemblyName ();
  161. type = Type.GetType (fullname + ", " + assembly_name.FullName);
  162. if (type != null)
  163. return type;
  164. try {
  165. var assembly = SR.Assembly.Load (assembly_name);
  166. if (assembly != null)
  167. return assembly.GetType (fullname);
  168. } catch (FileNotFoundException) {
  169. }
  170. return null;
  171. }
  172. static ISymbolReaderProvider reader_provider;
  173. public static ISymbolReaderProvider GetPlatformReaderProvider ()
  174. {
  175. if (reader_provider != null)
  176. return reader_provider;
  177. var type = GetPlatformType (GetProviderTypeName ("ReaderProvider"));
  178. if (type == null)
  179. return null;
  180. return reader_provider = (ISymbolReaderProvider) Activator.CreateInstance (type);
  181. }
  182. static string GetProviderTypeName (string name)
  183. {
  184. return "Mono.Cecil." + symbol_kind + "." + symbol_kind + name;
  185. }
  186. }
  187. }