Debian Codegraph @

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