Debian Codegraph @

external/cecil/symbols/pdb/Mono.Cecil.Pdb/NativePdbWriter.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 using System.Linq;
15 using System.Text;
16
17 using Mono.Cecil.Cil;
18 using Mono.Cecil.PE;
19 using Mono.Collections.Generic;
20
21 namespace Mono.Cecil.Pdb {
22
23 public class NativePdbWriter : ISymbolWriter {
24
25 readonly ModuleDefinition module;
26 readonly MetadataBuilder metadata;
27 readonly SymWriter writer;
28 readonly Dictionary<string, SymDocumentWriter> documents;
29 readonly Dictionary<ImportDebugInformation, MetadataToken> import_info_to_parent;
30
31 internal NativePdbWriter (ModuleDefinition module, SymWriter writer)
32 {
33 this.module = module;
34 this.metadata = module.metadata_builder;
35 this.writer = writer;
36 this.documents = new Dictionary<string, SymDocumentWriter> ();
37 this.import_info_to_parent = new Dictionary<ImportDebugInformation, MetadataToken> ();
38 }
39
40 public ISymbolReaderProvider GetReaderProvider ()
41 {
42 return new NativePdbReaderProvider ();
43 }
44
45 public ImageDebugHeader GetDebugHeader ()
46 {
47 ImageDebugDirectory directory;
48 var data = writer.GetDebugInfo (out directory);
49 directory.TimeDateStamp = (int) module.timestamp;
50 return new ImageDebugHeader (new ImageDebugHeaderEntry (directory, data));
51 }
52
53 public void Write (MethodDebugInformation info)
54 {
55 var method_token = info.method.MetadataToken;
56 var sym_token = method_token.ToInt32 ();
57
58 if (!info.HasSequencePoints && info.scope == null && !info.HasCustomDebugInformations && info.StateMachineKickOffMethod == null)
59 return;
60
61 writer.OpenMethod (sym_token);
62
63 if (!info.sequence_points.IsNullOrEmpty ())
64 DefineSequencePoints (info.sequence_points);
65
66 var import_parent = new MetadataToken ();
67
68 if (info.scope != null)
69 DefineScope (info.scope, info, out import_parent);
70
71 DefineCustomMetadata (info, import_parent);
72
73 writer.CloseMethod ();
74 }
75
76 void DefineCustomMetadata (MethodDebugInformation info, MetadataToken import_parent)
77 {
78 var metadata = new CustomMetadataWriter (this.writer);
79
80 if (import_parent.RID != 0) {
81 metadata.WriteForwardInfo (import_parent);
82 } else if (info.scope != null && info.scope.Import != null && info.scope.Import.HasTargets) {
83 metadata.WriteUsingInfo (info.scope.Import);
84 }
85
86 if (info.Method.HasCustomAttributes) {
87 foreach (var attribute in info.Method.CustomAttributes) {
88 const string compiler_services = "System.Runtime.CompilerServices";
89 var attribute_type = attribute.AttributeType;
90
91 if (!attribute_type.IsTypeOf (compiler_services, "IteratorStateMachineAttribute") && !attribute_type.IsTypeOf (compiler_services, "AsyncStateMachineAttribute"))
92 continue;
93
94 var type = attribute.ConstructorArguments [0].Value as TypeReference;
95 if (type == null)
96 continue;
97
98 metadata.WriteForwardIterator (type);
99 }
100 }
101
102 if (info.HasCustomDebugInformations) {
103 var state_machine = info.CustomDebugInformations.FirstOrDefault (cdi => cdi.Kind == CustomDebugInformationKind.StateMachineScope) as StateMachineScopeDebugInformation;
104
105 if (state_machine != null)
106 metadata.WriteIteratorScopes (state_machine, info);
107 }
108
109 metadata.WriteCustomMetadata ();
110
111 DefineAsyncCustomMetadata (info);
112 }
113
114 void DefineAsyncCustomMetadata (MethodDebugInformation info)
115 {
116 if (!info.HasCustomDebugInformations)
117 return;
118
119 foreach (var custom_info in info.CustomDebugInformations) {
120 var async_debug_info = custom_info as AsyncMethodBodyDebugInformation;
121 if (async_debug_info == null)
122 continue;
123
124 using (var stream = new MemoryStream ()) {
125 var async_metadata = new BinaryStreamWriter (stream);
126 async_metadata.WriteUInt32 (info.StateMachineKickOffMethod != null ? info.StateMachineKickOffMethod.MetadataToken.ToUInt32 () : 0);
127 async_metadata.WriteUInt32 ((uint) async_debug_info.CatchHandler.Offset);
128 async_metadata.WriteUInt32 ((uint) async_debug_info.Resumes.Count);
129 for (int i = 0; i < async_debug_info.Resumes.Count; ++i) {
130 async_metadata.WriteUInt32 ((uint) async_debug_info.Yields [i].Offset);
131 async_metadata.WriteUInt32 (async_debug_info.resume_methods [i].MetadataToken.ToUInt32 ());
132 async_metadata.WriteUInt32 ((uint) async_debug_info.Resumes [i].Offset);
133 }
134
135 writer.DefineCustomMetadata ("asyncMethodInfo", stream.ToArray ());
136 }
137 }
138 }
139
140 void DefineScope (ScopeDebugInformation scope, MethodDebugInformation info, out MetadataToken import_parent)
141 {
142 var start_offset = scope.Start.Offset;
143 var end_offset = scope.End.IsEndOfMethod
144 ? info.code_size
145 : scope.End.Offset;
146
147 import_parent = new MetadataToken (0u);
148
149 writer.OpenScope (start_offset);
150
151 if (scope.Import != null && scope.Import.HasTargets && !import_info_to_parent.TryGetValue (info.scope.Import, out import_parent)) {
152 foreach (var target in scope.Import.Targets) {
153 switch (target.Kind) {
154 case ImportTargetKind.ImportNamespace:
155 writer.UsingNamespace ("U" + target.@namespace);
156 break;
157 case ImportTargetKind.ImportType:
158 writer.UsingNamespace ("T" + TypeParser.ToParseable (target.type));
159 break;
160 case ImportTargetKind.DefineNamespaceAlias:
161 writer.UsingNamespace ("A" + target.Alias + " U" + target.@namespace);
162 break;
163 case ImportTargetKind.DefineTypeAlias:
164 writer.UsingNamespace ("A" + target.Alias + " T" + TypeParser.ToParseable (target.type));
165 break;
166 }
167 }
168
169 import_info_to_parent.Add (info.scope.Import, info.method.MetadataToken);
170 }
171
172 var sym_token = info.local_var_token.ToInt32 ();
173
174 if (!scope.variables.IsNullOrEmpty ()) {
175 for (int i = 0; i < scope.variables.Count; i++) {
176 var variable = scope.variables [i];
177 DefineLocalVariable (variable, sym_token, start_offset, end_offset);
178 }
179 }
180
181 if (!scope.constants.IsNullOrEmpty ()) {
182 for (int i = 0; i < scope.constants.Count; i++) {
183 var constant = scope.constants [i];
184 DefineConstant (constant);
185 }
186 }
187
188 if (!scope.scopes.IsNullOrEmpty ()) {
189 for (int i = 0; i < scope.scopes.Count; i++) {
190 MetadataToken _;
191 DefineScope (scope.scopes [i], info, out _);
192 }
193 }
194
195 writer.CloseScope (end_offset);
196 }
197
198 void DefineSequencePoints (Collection<SequencePoint> sequence_points)
199 {
200 for (int i = 0; i < sequence_points.Count; i++) {
201 var sequence_point = sequence_points [i];
202
203 writer.DefineSequencePoints (
204 GetDocument (sequence_point.Document),
205 new [] { sequence_point.Offset },
206 new [] { sequence_point.StartLine },
207 new [] { sequence_point.StartColumn },
208 new [] { sequence_point.EndLine },
209 new [] { sequence_point.EndColumn });
210 }
211 }
212
213 void DefineLocalVariable (VariableDebugInformation variable, int local_var_token, int start_offset, int end_offset)
214 {
215 writer.DefineLocalVariable2 (
216 variable.Name,
217 variable.Attributes,
218 local_var_token,
219 variable.Index,
220 0,
221 0,
222 start_offset,
223 end_offset);
224 }
225
226 void DefineConstant (ConstantDebugInformation constant)
227 {
228 var row = metadata.AddStandAloneSignature (metadata.GetConstantTypeBlobIndex (constant.ConstantType));
229 var token = new MetadataToken (TokenType.Signature, row);
230
231 writer.DefineConstant2 (constant.Name, constant.Value, token.ToInt32 ());
232 }
233
234 SymDocumentWriter GetDocument (Document document)
235 {
236 if (document == null)
237 return null;
238
239 SymDocumentWriter doc_writer;
240 if (documents.TryGetValue (document.Url, out doc_writer))
241 return doc_writer;
242
243 doc_writer = writer.DefineDocument (
244 document.Url,
245 document.LanguageGuid,
246 document.LanguageVendorGuid,
247 document.TypeGuid);
248
249 if (!document.Hash.IsNullOrEmpty ())
250 doc_writer.SetCheckSum (document.HashAlgorithmGuid, document.Hash);
251
252 documents [document.Url] = doc_writer;
253 return doc_writer;
254 }
255
256 public void Dispose ()
257 {
258 var entry_point = module.EntryPoint;
259 if (entry_point != null)
260 writer.SetUserEntryPoint (entry_point.MetadataToken.ToInt32 ());
261
262 writer.Close ();
263 }
264 }
265
266 enum CustomMetadataType : byte {
267 UsingInfo = 0,
268 ForwardInfo = 1,
269 IteratorScopes = 3,
270 ForwardIterator = 4,
271 }
272
273 class CustomMetadataWriter : IDisposable {
274
275 readonly SymWriter sym_writer;
276 readonly MemoryStream stream;
277 readonly BinaryStreamWriter writer;
278
279 int count;
280
281 const byte version = 4;
282
283 public CustomMetadataWriter (SymWriter sym_writer)
284 {
285 this.sym_writer = sym_writer;
286 this.stream = new MemoryStream ();
287 this.writer = new BinaryStreamWriter (stream);
288
289 writer.WriteByte (version);
290 writer.WriteByte (0); // count
291 writer.Align (4);
292 }
293
294 public void WriteUsingInfo (ImportDebugInformation import_info)
295 {
296 Write (CustomMetadataType.UsingInfo, () => {
297 writer.WriteUInt16 ((ushort) 1);
298 writer.WriteUInt16 ((ushort) import_info.Targets.Count);
299 });
300 }
301
302 public void WriteForwardInfo (MetadataToken import_parent)
303 {
304 Write (CustomMetadataType.ForwardInfo, () => writer.WriteUInt32 (import_parent.ToUInt32 ()));
305 }
306
307 public void WriteIteratorScopes (StateMachineScopeDebugInformation state_machine, MethodDebugInformation debug_info)
308 {
309 Write (CustomMetadataType.IteratorScopes, () => {
310 var scopes = state_machine.Scopes;
311 writer.WriteInt32 (scopes.Count);
312 foreach (var scope in scopes) {
313 var start = scope.Start.Offset;
314 var end = scope.End.IsEndOfMethod ? debug_info.code_size : scope.End.Offset;
315 writer.WriteInt32 (start);
316 writer.WriteInt32 (end - 1);
317 }
318 });
319 }
320
321 public void WriteForwardIterator (TypeReference type)
322 {
323 Write (CustomMetadataType.ForwardIterator, () => writer.WriteBytes(Encoding.Unicode.GetBytes(type.Name)));
324 }
325
326 void Write (CustomMetadataType type, Action write)
327 {
328 count++;
329 writer.WriteByte (version);
330 writer.WriteByte ((byte) type);
331 writer.Align (4);
332
333 var length_position = writer.Position;
334 writer.WriteUInt32 (0);
335
336 write ();
337 writer.Align (4);
338
339 var end = writer.Position;
340 var length = end - length_position + 4; // header is 4 bytes long
341
342 writer.Position = length_position;
343 writer.WriteInt32 (length);
344
345 writer.Position = end;
346 }
347
348 public void WriteCustomMetadata ()
349 {
350 if (count == 0)
351 return;
352
353 writer.BaseStream.Position = 1;
354 writer.WriteByte ((byte) count);
355 writer.Flush ();
356
357 sym_writer.DefineCustomMetadata ("MD2", stream.ToArray ());
358 }
359
360 public void Dispose ()
361 {
362 stream.Dispose ();
363 }
364 }
365 }