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