Debian Codegraph @

external/cecil/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 public sealed class MdbWriterProvider : ISymbolWriterProvider {
22
23 public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName)
24 {
25 Mixin.CheckModule (module);
26 Mixin.CheckFileName (fileName);
27
28 return new MdbWriter (module.Mvid, fileName);
29 }
30
31 public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream)
32 {
33 throw new NotImplementedException ();
34 }
35 }
36
37 public sealed class MdbWriter : ISymbolWriter {
38
39 readonly Guid mvid;
40 readonly MonoSymbolWriter writer;
41 readonly Dictionary<string, SourceFile> source_files;
42
43 public MdbWriter (Guid mvid, string assembly)
44 {
45 this.mvid = mvid;
46 this.writer = new MonoSymbolWriter (assembly);
47 this.source_files = new Dictionary<string, SourceFile> ();
48 }
49
50 public ISymbolReaderProvider GetReaderProvider ()
51 {
52 return new MdbReaderProvider ();
53 }
54
55 SourceFile GetSourceFile (Document document)
56 {
57 var url = document.Url;
58
59 SourceFile source_file;
60 if (source_files.TryGetValue (url, out source_file))
61 return source_file;
62
63 var entry = writer.DefineDocument (url, null, document.Hash != null && document.Hash.Length == 16 ? document.Hash : null);
64 var compile_unit = writer.DefineCompilationUnit (entry);
65
66 source_file = new SourceFile (compile_unit, entry);
67 source_files.Add (url, source_file);
68 return source_file;
69 }
70
71 void Populate (Collection<SequencePoint> sequencePoints, int [] offsets,
72 int [] startRows, int [] endRows, int [] startCols, int [] endCols, out SourceFile file)
73 {
74 SourceFile source_file = null;
75
76 for (int i = 0; i < sequencePoints.Count; i++) {
77 var sequence_point = sequencePoints [i];
78 offsets [i] = sequence_point.Offset;
79
80 if (source_file == null)
81 source_file = GetSourceFile (sequence_point.Document);
82
83 startRows [i] = sequence_point.StartLine;
84 endRows [i] = sequence_point.EndLine;
85 startCols [i] = sequence_point.StartColumn;
86 endCols [i] = sequence_point.EndColumn;
87 }
88
89 file = source_file;
90 }
91
92 public void Write (MethodDebugInformation info)
93 {
94 var method = new SourceMethod (info.method);
95
96 var sequence_points = info.SequencePoints;
97 int count = sequence_points.Count;
98 if (count == 0)
99 return;
100
101 var offsets = new int [count];
102 var start_rows = new int [count];
103 var end_rows = new int [count];
104 var start_cols = new int [count];
105 var end_cols = new int [count];
106
107 SourceFile file;
108 Populate (sequence_points, offsets, start_rows, end_rows, start_cols, end_cols, out file);
109
110 var builder = writer.OpenMethod (file.CompilationUnit, 0, method);
111
112 for (int i = 0; i < count; i++) {
113 builder.MarkSequencePoint (
114 offsets [i],
115 file.CompilationUnit.SourceFile,
116 start_rows [i],
117 start_cols [i],
118 end_rows [i],
119 end_cols [i],
120 false);
121 }
122
123 if (info.scope != null)
124 WriteRootScope (info.scope, info);
125
126 writer.CloseMethod ();
127 }
128
129 void WriteRootScope (ScopeDebugInformation scope, MethodDebugInformation info)
130 {
131 WriteScopeVariables (scope);
132
133 if (scope.HasScopes)
134 WriteScopes (scope.Scopes, info);
135 }
136
137 void WriteScope (ScopeDebugInformation scope, MethodDebugInformation info)
138 {
139 writer.OpenScope (scope.Start.Offset);
140
141 WriteScopeVariables (scope);
142
143 if (scope.HasScopes)
144 WriteScopes (scope.Scopes, info);
145
146 writer.CloseScope (scope.End.IsEndOfMethod ? info.code_size : scope.End.Offset);
147 }
148
149 void WriteScopes (Collection<ScopeDebugInformation> scopes, MethodDebugInformation info)
150 {
151 for (int i = 0; i < scopes.Count; i++)
152 WriteScope (scopes [i], info);
153 }
154
155 void WriteScopeVariables (ScopeDebugInformation scope)
156 {
157 if (!scope.HasVariables)
158 return;
159
160 foreach (var variable in scope.variables)
161 if (!string.IsNullOrEmpty (variable.Name))
162 writer.DefineLocalVariable (variable.Index, variable.Name);
163 }
164
165 public ImageDebugHeader GetDebugHeader ()
166 {
167 return new ImageDebugHeader ();
168 }
169
170 public void Dispose ()
171 {
172 writer.WriteSymbolFile (mvid);
173 }
174
175 class SourceFile : ISourceFile {
176
177 readonly CompileUnitEntry compilation_unit;
178 readonly SourceFileEntry entry;
179
180 public SourceFileEntry Entry {
181 get { return entry; }
182 }
183
184 public CompileUnitEntry CompilationUnit {
185 get { return compilation_unit; }
186 }
187
188 public SourceFile (CompileUnitEntry comp_unit, SourceFileEntry entry)
189 {
190 this.compilation_unit = comp_unit;
191 this.entry = entry;
192 }
193 }
194
195 class SourceMethod : IMethodDef {
196
197 readonly MethodDefinition method;
198
199 public string Name {
200 get { return method.Name; }
201 }
202
203 public int Token {
204 get { return method.MetadataToken.ToInt32 (); }
205 }
206
207 public SourceMethod (MethodDefinition method)
208 {
209 this.method = method;
210 }
211 }
212 }
213 }