Debian Codegraph @

external/cecil-legacy/symbols/pdb/Mono.Cecil.Pdb/PdbReader.cs

raw symbols SCIP
1 //
2 // PdbReader.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 Microsoft.Cci.Pdb;
34
35 using Mono.Cecil.Cil;
36
37 namespace Mono.Cecil.Pdb {
38
39 public class PdbReader : ISymbolReader {
40
41 int age;
42 Guid guid;
43
44 readonly Stream pdb_file;
45 readonly Dictionary<string, Document> documents = new Dictionary<string, Document> ();
46 readonly Dictionary<uint, PdbFunction> functions = new Dictionary<uint, PdbFunction> ();
47
48 internal PdbReader (Stream file)
49 {
50 this.pdb_file = file;
51 }
52
53 /*
54 uint Magic = 0x53445352;
55 Guid Signature;
56 uint Age;
57 string FileName;
58 */
59
60 public bool ProcessDebugHeader (ImageDebugDirectory directory, byte [] header)
61 {
62 if (header.Length < 24)
63 return false;
64
65 var magic = ReadInt32 (header, 0);
66 if (magic != 0x53445352)
67 return false;
68
69 var guid_bytes = new byte [16];
70 Buffer.BlockCopy (header, 4, guid_bytes, 0, 16);
71
72 this.guid = new Guid (guid_bytes);
73 this.age = ReadInt32 (header, 20);
74
75 return PopulateFunctions ();
76 }
77
78 static int ReadInt32 (byte [] bytes, int start)
79 {
80 return (bytes [start]
81 | (bytes [start + 1] << 8)
82 | (bytes [start + 2] << 16)
83 | (bytes [start + 3] << 24));
84 }
85
86 bool PopulateFunctions ()
87 {
88 using (pdb_file) {
89 Dictionary<uint, PdbTokenLine> tokenToSourceMapping;
90 string sourceServerData;
91 int age;
92 Guid guid;
93
94 var funcs = PdbFile.LoadFunctions (pdb_file, out tokenToSourceMapping, out sourceServerData, out age, out guid);
95
96 if (this.guid != guid)
97 return false;
98
99 foreach (PdbFunction function in funcs)
100 functions.Add (function.token, function);
101 }
102
103 return true;
104 }
105
106 public void Read (MethodBody body, InstructionMapper mapper)
107 {
108 var method_token = body.Method.MetadataToken;
109
110 PdbFunction function;
111 if (!functions.TryGetValue (method_token.ToUInt32 (), out function))
112 return;
113
114 ReadSequencePoints (function, mapper);
115 ReadScopeAndLocals (function.scopes, null, body, mapper);
116 }
117
118 static void ReadScopeAndLocals (PdbScope [] scopes, Scope parent, MethodBody body, InstructionMapper mapper)
119 {
120 foreach (PdbScope scope in scopes)
121 ReadScopeAndLocals (scope, parent, body, mapper);
122
123 CreateRootScope (body);
124 }
125
126 static void CreateRootScope (MethodBody body)
127 {
128 if (!body.HasVariables)
129 return;
130
131 var instructions = body.Instructions;
132
133 var root = new Scope ();
134 root.Start = instructions [0];
135 root.End = instructions [instructions.Count - 1];
136
137 var variables = body.Variables;
138 for (int i = 0; i < variables.Count; i++)
139 root.Variables.Add (variables [i]);
140
141 body.Scope = root;
142 }
143
144 static void ReadScopeAndLocals (PdbScope scope, Scope parent, MethodBody body, InstructionMapper mapper)
145 {
146 //Scope s = new Scope ();
147 //s.Start = GetInstruction (body, instructions, (int) scope.address);
148 //s.End = GetInstruction (body, instructions, (int) scope.length - 1);
149
150 //if (parent != null)
151 // parent.Scopes.Add (s);
152 //else
153 // body.Scopes.Add (s);
154
155 if (scope == null)
156 return;
157
158 foreach (PdbSlot slot in scope.slots) {
159 int index = (int) slot.slot;
160 if (index < 0 || index >= body.Variables.Count)
161 continue;
162
163 VariableDefinition variable = body.Variables [index];
164 variable.Name = slot.name;
165
166 //s.Variables.Add (variable);
167 }
168
169 ReadScopeAndLocals (scope.scopes, null /* s */, body, mapper);
170 }
171
172 void ReadSequencePoints (PdbFunction function, InstructionMapper mapper)
173 {
174 if (function.lines == null)
175 return;
176
177 foreach (PdbLines lines in function.lines)
178 ReadLines (lines, mapper);
179 }
180
181 void ReadLines (PdbLines lines, InstructionMapper mapper)
182 {
183 var document = GetDocument (lines.file);
184
185 foreach (var line in lines.lines)
186 ReadLine (line, document, mapper);
187 }
188
189 static void ReadLine (PdbLine line, Document document, InstructionMapper mapper)
190 {
191 var instruction = mapper ((int) line.offset);
192 if (instruction == null)
193 return;
194
195 var sequence_point = new SequencePoint (document);
196 sequence_point.StartLine = (int) line.lineBegin;
197 sequence_point.StartColumn = (int) line.colBegin;
198 sequence_point.EndLine = (int) line.lineEnd;
199 sequence_point.EndColumn = (int) line.colEnd;
200
201 instruction.SequencePoint = sequence_point;
202 }
203
204 Document GetDocument (PdbSource source)
205 {
206 string name = source.name;
207 Document document;
208 if (documents.TryGetValue (name, out document))
209 return document;
210
211 document = new Document (name) {
212 Language = source.language.ToLanguage (),
213 LanguageVendor = source.vendor.ToVendor (),
214 Type = source.doctype.ToType (),
215 };
216 documents.Add (name, document);
217 return document;
218 }
219
220 public void Read (MethodSymbols symbols)
221 {
222 PdbFunction function;
223 if (!functions.TryGetValue (symbols.MethodToken.ToUInt32 (), out function))
224 return;
225
226 ReadSequencePoints (function, symbols);
227 ReadLocals (function.scopes, symbols);
228 }
229
230 void ReadLocals (PdbScope [] scopes, MethodSymbols symbols)
231 {
232 foreach (var scope in scopes)
233 ReadLocals (scope, symbols);
234 }
235
236 void ReadLocals (PdbScope scope, MethodSymbols symbols)
237 {
238 if (scope == null)
239 return;
240
241 foreach (var slot in scope.slots) {
242 int index = (int) slot.slot;
243 if (index < 0 || index >= symbols.Variables.Count)
244 continue;
245
246 var variable = symbols.Variables [index];
247 variable.Name = slot.name;
248 }
249
250 ReadLocals (scope.scopes, symbols);
251 }
252
253 void ReadSequencePoints (PdbFunction function, MethodSymbols symbols)
254 {
255 if (function.lines == null)
256 return;
257
258 foreach (PdbLines lines in function.lines)
259 ReadLines (lines, symbols);
260 }
261
262 void ReadLines (PdbLines lines, MethodSymbols symbols)
263 {
264 for (int i = 0; i < lines.lines.Length; i++) {
265 var line = lines.lines [i];
266
267 symbols.Instructions.Add (new InstructionSymbol ((int) line.offset, new SequencePoint (GetDocument (lines.file)) {
268 StartLine = (int) line.lineBegin,
269 StartColumn = (int) line.colBegin,
270 EndLine = (int) line.lineEnd,
271 EndColumn = (int) line.colEnd,
272 }));
273 }
274 }
275
276 public void Dispose ()
277 {
278 pdb_file.Close ();
279 }
280 }
281 }