Debian Codegraph @

symbols/mdb/Mono.Cecil.Mdb/MdbWriter.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 #if !READ_ONLY
22 public class MdbWriterProvider : ISymbolWriterProvider {
23
24 public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName)
25 {
26 return new MdbWriter (module.Mvid, fileName);
27 }
28
29 public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream)
30 {
31 throw new NotImplementedException ();
32 }
33 }
34
35 public class MdbWriter : ISymbolWriter {
36
37 readonly Guid mvid;
38 readonly MonoSymbolWriter writer;
39 readonly Dictionary<string, SourceFile> source_files;
40
41 public MdbWriter (Guid mvid, string assembly)
42 {
43 this.mvid = mvid;
44 this.writer = new MonoSymbolWriter (assembly);
45 this.source_files = new Dictionary<string, SourceFile> ();
46 }
47
48 static Collection<Instruction> GetInstructions (MethodBody body)
49 {
50 var instructions = new Collection<Instruction> ();
51 foreach (var instruction in body.Instructions)
52 if (instruction.SequencePoint != null)
53 instructions.Add (instruction);
54
55 return instructions;
56 }
57
58 SourceFile GetSourceFile (Document document)
59 {
60 var url = document.Url;
61
62 SourceFile source_file;
63 if (source_files.TryGetValue (url, out source_file))
64 return source_file;
65
66 var entry = writer.DefineDocument (url);
67 var compile_unit = writer.DefineCompilationUnit (entry);
68
69 source_file = new SourceFile (compile_unit, entry);
70 source_files.Add (url, source_file);
71 return source_file;
72 }
73
74 void Populate (Collection<Instruction> instructions, int [] offsets,
75 int [] startRows, int [] endRows, int [] startCols, int [] endCols, out SourceFile file)
76 {
77 SourceFile source_file = null;
78
79 for (int i = 0; i < instructions.Count; i++) {
80 var instruction = instructions [i];
81 offsets [i] = instruction.Offset;
82
83 var sequence_point = instruction.SequencePoint;
84 if (source_file == null)
85 source_file = GetSourceFile (sequence_point.Document);
86
87 startRows [i] = sequence_point.StartLine;
88 endRows [i] = sequence_point.EndLine;
89 startCols [i] = sequence_point.StartColumn;
90 endCols [i] = sequence_point.EndColumn;
91 }
92
93 file = source_file;
94 }
95
96 public void Write (MethodBody body)
97 {
98 var method = new SourceMethod (body.Method);
99
100 var instructions = GetInstructions (body);
101 int count = instructions.Count;
102 if (count == 0)
103 return;
104
105 var offsets = new int [count];
106 var start_rows = new int [count];
107 var end_rows = new int [count];
108 var start_cols = new int [count];
109 var end_cols = new int [count];
110
111 SourceFile file;
112 Populate (instructions, offsets, start_rows, end_rows, start_cols, end_cols, out file);
113
114 var builder = writer.OpenMethod (file.CompilationUnit, 0, method);
115
116 for (int i = 0; i < count; i++) {
117 builder.MarkSequencePoint (
118 offsets [i],
119 file.CompilationUnit.SourceFile,
120 start_rows [i],
121 end_rows [i],
122 start_cols [i],
123 end_cols [i],
124 false);
125 }
126
127 if (body.Scope != null && body.Scope.HasScopes)
128 WriteScope (body.Scope, true);
129 else
130 if (body.HasVariables)
131 AddVariables (body.Variables);
132
133 writer.CloseMethod ();
134 }
135
136 private void WriteScope (Scope scope, bool root)
137 {
138 if (scope.Start.Offset == scope.End.Offset) return;
139 writer.OpenScope (scope.Start.Offset);
140
141
142 if (scope.HasVariables)
143 {
144 foreach (var el in scope.Variables)
145 {
146 if (!String.IsNullOrEmpty (el.Name))
147 writer.DefineLocalVariable (el.Index, el.Name);
148 }
149 }
150
151 if (scope.HasScopes)
152 {
153 foreach (var el in scope.Scopes)
154 WriteScope (el, false);
155 }
156
157 writer.CloseScope (scope.End.Offset + scope.End.GetSize());
158 }
159
160 readonly static byte [] empty_header = new byte [0];
161
162 public bool GetDebugHeader (out ImageDebugDirectory directory, out byte [] header)
163 {
164 directory = new ImageDebugDirectory ();
165 header = empty_header;
166 return false;
167 }
168
169 void AddVariables (IList<VariableDefinition> variables)
170 {
171 for (int i = 0; i < variables.Count; i++) {
172 var variable = variables [i];
173 writer.DefineLocalVariable (i, variable.Name);
174 }
175 }
176
177 public void Write (MethodSymbols symbols)
178 {
179 var method = new SourceMethodSymbol (symbols);
180
181 var file = GetSourceFile (symbols.Instructions [0].SequencePoint.Document);
182 var builder = writer.OpenMethod (file.CompilationUnit, 0, method);
183 var count = symbols.Instructions.Count;
184
185 for (int i = 0; i < count; i++) {
186 var instruction = symbols.Instructions [i];
187 var sequence_point = instruction.SequencePoint;
188
189 builder.MarkSequencePoint (
190 instruction.Offset,
191 GetSourceFile (sequence_point.Document).CompilationUnit.SourceFile,
192 sequence_point.StartLine,
193 sequence_point.EndLine,
194 sequence_point.StartColumn,
195 sequence_point.EndColumn,
196 false);
197 }
198
199 if (symbols.HasVariables)
200 AddVariables (symbols.Variables);
201
202 writer.CloseMethod ();
203 }
204
205 public void Dispose ()
206 {
207 writer.WriteSymbolFile (mvid);
208 }
209
210 class SourceFile : ISourceFile {
211
212 readonly CompileUnitEntry compilation_unit;
213 readonly SourceFileEntry entry;
214
215 public SourceFileEntry Entry {
216 get { return entry; }
217 }
218
219 public CompileUnitEntry CompilationUnit {
220 get { return compilation_unit; }
221 }
222
223 public SourceFile (CompileUnitEntry comp_unit, SourceFileEntry entry)
224 {
225 this.compilation_unit = comp_unit;
226 this.entry = entry;
227 }
228 }
229
230 class SourceMethodSymbol : IMethodDef {
231
232 readonly string name;
233 readonly int token;
234
235 public string Name {
236 get { return name;}
237 }
238
239 public int Token {
240 get { return token; }
241 }
242
243 public SourceMethodSymbol (MethodSymbols symbols)
244 {
245 name = symbols.MethodName;
246 token = symbols.MethodToken.ToInt32 ();
247 }
248 }
249
250 class SourceMethod : IMethodDef {
251
252 readonly MethodDefinition method;
253
254 public string Name {
255 get { return method.Name; }
256 }
257
258 public int Token {
259 get { return method.MetadataToken.ToInt32 (); }
260 }
261
262 public SourceMethod (MethodDefinition method)
263 {
264 this.method = method;
265 }
266 }
267 }
268 #endif
269 }