/Mono.Cecil/Mono.Cecil.Cil/Symbols.cs

http://github.com/icsharpcode/ILSpy · C# · 277 lines · 186 code · 64 blank · 27 comment · 23 complexity · 678b957aad071c42e3315da853be20f3 MD5 · raw file

  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 !scopes.IsNullOrEmpty (); }
  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 !variables.IsNullOrEmpty (); }
  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. internal Collection<InstructionSymbol> instructions;
  95. public bool HasVariables {
  96. get { return !variables.IsNullOrEmpty (); }
  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. #if !CF
  170. } catch (FileLoadException) {
  171. #endif
  172. }
  173. return null;
  174. }
  175. static ISymbolReaderProvider reader_provider;
  176. public static ISymbolReaderProvider GetPlatformReaderProvider ()
  177. {
  178. if (reader_provider != null)
  179. return reader_provider;
  180. var type = GetPlatformType (GetProviderTypeName ("ReaderProvider"));
  181. if (type == null)
  182. return null;
  183. return reader_provider = (ISymbolReaderProvider) Activator.CreateInstance (type);
  184. }
  185. static string GetProviderTypeName (string name)
  186. {
  187. return "Mono.Cecil." + symbol_kind + "." + symbol_kind + name;
  188. }
  189. #if !READ_ONLY
  190. static ISymbolWriterProvider writer_provider;
  191. public static ISymbolWriterProvider GetPlatformWriterProvider ()
  192. {
  193. if (writer_provider != null)
  194. return writer_provider;
  195. var type = GetPlatformType (GetProviderTypeName ("WriterProvider"));
  196. if (type == null)
  197. return null;
  198. return writer_provider = (ISymbolWriterProvider) Activator.CreateInstance (type);
  199. }
  200. #endif
  201. }
  202. #if !READ_ONLY
  203. public interface ISymbolWriter : IDisposable {
  204. bool GetDebugHeader (out ImageDebugDirectory directory, out byte [] header);
  205. void Write (MethodBody body);
  206. void Write (MethodSymbols symbols);
  207. }
  208. public interface ISymbolWriterProvider {
  209. ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName);
  210. ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream);
  211. }
  212. #endif
  213. }