Debian Codegraph @

symbols/mdb/Mono.Cecil.Mdb/MdbReader.cs

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