Debian Codegraph @

external/cecil-legacy/symbols/mdb/Mono.Cecil.Mdb/MdbReader.cs

raw symbols SCIP
1 //
2 // MdbReader.cs
3 //
4 // Author:
5 // Jb Evain ([email protected])
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
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) // Mono 2.6 emits wrong local infos for iterators
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) // Mono 2.6 emits wrong local infos for iterators
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 }