MdbReader.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using Mono.Cecil.Cil;
  2. using Mono.Collections.Generic;
  3. using Mono.CompilerServices.SymbolWriter;
  4. using System;
  5. using System.Collections.Generic;
  6. namespace Mono.Cecil.Mdb
  7. {
  8. public class MdbReader : ISymbolReader, IDisposable
  9. {
  10. private readonly MonoSymbolFile symbol_file;
  11. private readonly Dictionary<string, Document> documents;
  12. public MdbReader(MonoSymbolFile symFile)
  13. {
  14. this.symbol_file = symFile;
  15. this.documents = new Dictionary<string, Document>();
  16. }
  17. public bool ProcessDebugHeader(ImageDebugDirectory directory, byte[] header)
  18. {
  19. return true;
  20. }
  21. public void Read(MethodBody body, InstructionMapper mapper)
  22. {
  23. MetadataToken method_token = body.Method.MetadataToken;
  24. MethodEntry entry = this.symbol_file.GetMethodByToken(method_token.ToInt32());
  25. if (entry != null)
  26. {
  27. Scope[] scopes = MdbReader.ReadScopes(entry, body, mapper);
  28. this.ReadLineNumbers(entry, mapper);
  29. MdbReader.ReadLocalVariables(entry, body, scopes);
  30. }
  31. }
  32. private static void ReadLocalVariables(MethodEntry entry, MethodBody body, Scope[] scopes)
  33. {
  34. LocalVariableEntry[] locals = entry.GetLocals();
  35. LocalVariableEntry[] array = locals;
  36. for (int i = 0; i < array.Length; i++)
  37. {
  38. LocalVariableEntry local = array[i];
  39. VariableDefinition variable = body.Variables[local.Index];
  40. variable.Name = local.Name;
  41. int index = local.BlockIndex;
  42. if (index >= 0 && index < scopes.Length)
  43. {
  44. Scope scope = scopes[index];
  45. if (scope != null)
  46. {
  47. scope.Variables.Add(variable);
  48. }
  49. }
  50. }
  51. }
  52. private void ReadLineNumbers(MethodEntry entry, InstructionMapper mapper)
  53. {
  54. Document document = null;
  55. LineNumberTable table = entry.GetLineNumberTable();
  56. LineNumberEntry[] lineNumbers = table.LineNumbers;
  57. for (int i = 0; i < lineNumbers.Length; i++)
  58. {
  59. LineNumberEntry line = lineNumbers[i];
  60. Instruction instruction = mapper(line.Offset);
  61. if (instruction != null)
  62. {
  63. if (document == null)
  64. {
  65. document = this.GetDocument(entry.CompileUnit.SourceFile);
  66. }
  67. instruction.SequencePoint = new SequencePoint(document)
  68. {
  69. StartLine = line.Row,
  70. EndLine = line.Row
  71. };
  72. }
  73. }
  74. }
  75. private Document GetDocument(SourceFileEntry file)
  76. {
  77. string file_name = file.FileName;
  78. Document document;
  79. Document result;
  80. if (this.documents.TryGetValue(file_name, out document))
  81. {
  82. result = document;
  83. }
  84. else
  85. {
  86. document = new Document(file_name);
  87. this.documents.Add(file_name, document);
  88. result = document;
  89. }
  90. return result;
  91. }
  92. private static Scope[] ReadScopes(MethodEntry entry, MethodBody body, InstructionMapper mapper)
  93. {
  94. CodeBlockEntry[] blocks = entry.GetCodeBlocks();
  95. Scope[] scopes = new Scope[blocks.Length];
  96. CodeBlockEntry[] array = blocks;
  97. for (int i = 0; i < array.Length; i++)
  98. {
  99. CodeBlockEntry block = array[i];
  100. if (block.BlockType == CodeBlockEntry.Type.Lexical)
  101. {
  102. Scope scope = new Scope();
  103. scope.Start = mapper(block.StartOffset);
  104. scope.End = mapper(block.EndOffset);
  105. scopes[block.Index] = scope;
  106. if (body.Scope == null)
  107. {
  108. body.Scope = scope;
  109. }
  110. if (!MdbReader.AddScope(body.Scope, scope))
  111. {
  112. body.Scope = scope;
  113. }
  114. }
  115. }
  116. return scopes;
  117. }
  118. private static bool AddScope(Scope provider, Scope scope)
  119. {
  120. bool result;
  121. foreach (Scope sub_scope in provider.Scopes)
  122. {
  123. if (MdbReader.AddScope(sub_scope, scope))
  124. {
  125. result = true;
  126. return result;
  127. }
  128. if (scope.Start.Offset >= sub_scope.Start.Offset && scope.End.Offset <= sub_scope.End.Offset)
  129. {
  130. sub_scope.Scopes.Add(scope);
  131. result = true;
  132. return result;
  133. }
  134. }
  135. result = false;
  136. return result;
  137. }
  138. public void Read(MethodSymbols symbols)
  139. {
  140. MethodEntry entry = this.symbol_file.GetMethodByToken(symbols.MethodToken.ToInt32());
  141. if (entry != null)
  142. {
  143. this.ReadLineNumbers(entry, symbols);
  144. MdbReader.ReadLocalVariables(entry, symbols);
  145. }
  146. }
  147. private void ReadLineNumbers(MethodEntry entry, MethodSymbols symbols)
  148. {
  149. LineNumberTable table = entry.GetLineNumberTable();
  150. LineNumberEntry[] lines = table.LineNumbers;
  151. Collection<InstructionSymbol> instructions = symbols.instructions = new Collection<InstructionSymbol>(lines.Length);
  152. for (int i = 0; i < lines.Length; i++)
  153. {
  154. LineNumberEntry line = lines[i];
  155. instructions.Add(new InstructionSymbol(line.Offset, new SequencePoint(this.GetDocument(entry.CompileUnit.SourceFile))
  156. {
  157. StartLine = line.Row,
  158. EndLine = line.Row
  159. }));
  160. }
  161. }
  162. private static void ReadLocalVariables(MethodEntry entry, MethodSymbols symbols)
  163. {
  164. LocalVariableEntry[] locals = entry.GetLocals();
  165. for (int i = 0; i < locals.Length; i++)
  166. {
  167. LocalVariableEntry local = locals[i];
  168. VariableDefinition variable = symbols.Variables[local.Index];
  169. variable.Name = local.Name;
  170. }
  171. }
  172. public void Dispose()
  173. {
  174. this.symbol_file.Dispose();
  175. }
  176. }
  177. }