Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NativePdbWriter.cs « Mono.Cecil.Pdb « pdb « symbols - github.com/mono/cecil.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 01b6c7a510c150c78e46f6e6fd4ace9561e1fbbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//
// Author:
//   Jb Evain (jbevain@gmail.com)
//
// Copyright (c) 2008 - 2015 Jb Evain
// Copyright (c) 2008 - 2011 Novell, Inc.
//
// Licensed under the MIT/X11 license.
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using Mono.Cecil.Cil;
using Mono.Cecil.PE;
using Mono.Collections.Generic;

namespace Mono.Cecil.Pdb {

	public class NativePdbWriter : ISymbolWriter {

		readonly ModuleDefinition module;
		readonly MetadataBuilder metadata;
		readonly SymWriter writer;
		readonly Dictionary<string, SymDocumentWriter> documents;
		readonly Dictionary<ImportDebugInformation, MetadataToken> import_info_to_parent;

		internal NativePdbWriter (ModuleDefinition module, SymWriter writer)
		{
			this.module = module;
			this.metadata = module.metadata_builder;
			this.writer = writer;
			this.documents = new Dictionary<string, SymDocumentWriter> ();
			this.import_info_to_parent = new Dictionary<ImportDebugInformation, MetadataToken> ();
		}

		public ISymbolReaderProvider GetReaderProvider ()
		{
			return new NativePdbReaderProvider ();
		}

		public ImageDebugHeader GetDebugHeader ()
		{
			ImageDebugDirectory directory;
			var data = writer.GetDebugInfo (out directory);
			directory.TimeDateStamp = (int) module.timestamp;
			return new ImageDebugHeader (new ImageDebugHeaderEntry (directory, data));
		}

		public void Write (MethodDebugInformation info)
		{
			var method_token = info.method.MetadataToken;
			var sym_token = method_token.ToInt32 ();

			if (!info.HasSequencePoints && info.scope == null && !info.HasCustomDebugInformations && info.StateMachineKickOffMethod == null)
				return;

			writer.OpenMethod (sym_token);

			if (!info.sequence_points.IsNullOrEmpty ())
				DefineSequencePoints (info.sequence_points);

			var import_parent = new MetadataToken ();

			if (info.scope != null)
				DefineScope (info.scope, info, out import_parent);

			DefineCustomMetadata (info, import_parent);

			writer.CloseMethod ();
		}

		void DefineCustomMetadata (MethodDebugInformation info, MetadataToken import_parent)
		{
			var metadata = new CustomMetadataWriter (this.writer);

			if (import_parent.RID != 0) {
				metadata.WriteForwardInfo (import_parent);
			} else if (info.scope != null && info.scope.Import != null && info.scope.Import.HasTargets) {
				metadata.WriteUsingInfo (info.scope.Import);
			}

			if (info.Method.HasCustomAttributes) {
				foreach (var attribute in info.Method.CustomAttributes) {
					const string compiler_services = "System.Runtime.CompilerServices";
					var attribute_type = attribute.AttributeType;

					if (!attribute_type.IsTypeOf (compiler_services, "IteratorStateMachineAttribute") && !attribute_type.IsTypeOf (compiler_services, "AsyncStateMachineAttribute"))
						continue;

					var type = attribute.ConstructorArguments [0].Value as TypeReference;
					if (type == null)
						continue;

					metadata.WriteForwardIterator (type);
				}
			}

			if (info.HasCustomDebugInformations) {
				var state_machine = info.CustomDebugInformations.FirstOrDefault (cdi => cdi.Kind == CustomDebugInformationKind.StateMachineScope) as StateMachineScopeDebugInformation;

				if (state_machine != null)
					metadata.WriteIteratorScopes (state_machine, info);
			}

			metadata.WriteCustomMetadata ();

			DefineAsyncCustomMetadata (info);
		}

		void DefineAsyncCustomMetadata (MethodDebugInformation info)
		{
			if (!info.HasCustomDebugInformations)
				return;

			foreach (var custom_info in info.CustomDebugInformations) {
				var async_debug_info = custom_info as AsyncMethodBodyDebugInformation;
				if (async_debug_info == null)
					continue;

				using (var stream = new MemoryStream ()) {
					var async_metadata = new BinaryStreamWriter (stream);
					async_metadata.WriteUInt32 (info.StateMachineKickOffMethod != null ? info.StateMachineKickOffMethod.MetadataToken.ToUInt32 () : 0);
					async_metadata.WriteUInt32 ((uint) async_debug_info.CatchHandler.Offset);
					async_metadata.WriteUInt32 ((uint) async_debug_info.Resumes.Count);
					for (int i = 0; i < async_debug_info.Resumes.Count; ++i) {
						async_metadata.WriteUInt32 ((uint) async_debug_info.Yields [i].Offset);
						async_metadata.WriteUInt32 (async_debug_info.resume_methods [i].MetadataToken.ToUInt32 ());
						async_metadata.WriteUInt32 ((uint) async_debug_info.Resumes [i].Offset);
					}

					writer.DefineCustomMetadata ("asyncMethodInfo", stream.ToArray ());
				}
			}
		}

		void DefineScope (ScopeDebugInformation scope, MethodDebugInformation info, out MetadataToken import_parent)
		{
			var start_offset = scope.Start.Offset;
			var end_offset = scope.End.IsEndOfMethod
				? info.code_size
				: scope.End.Offset;

			import_parent = new MetadataToken (0u);

			writer.OpenScope (start_offset);

			if (scope.Import != null && scope.Import.HasTargets && !import_info_to_parent.TryGetValue (info.scope.Import, out import_parent)) {
				foreach (var target in scope.Import.Targets) {
					switch (target.Kind) {
					case ImportTargetKind.ImportNamespace:
						writer.UsingNamespace ("U" + target.@namespace);
						break;
					case ImportTargetKind.ImportType:
						writer.UsingNamespace ("T" + TypeParser.ToParseable (target.type));
						break;
					case ImportTargetKind.DefineNamespaceAlias:
						writer.UsingNamespace ("A" + target.Alias + " U" + target.@namespace);
						break;
					case ImportTargetKind.DefineTypeAlias:
						writer.UsingNamespace ("A" + target.Alias + " T" + TypeParser.ToParseable (target.type));
						break;
					}
				}

				import_info_to_parent.Add (info.scope.Import, info.method.MetadataToken);
			}

			var sym_token = info.local_var_token.ToInt32 ();

			if (!scope.variables.IsNullOrEmpty ()) {
				for (int i = 0; i < scope.variables.Count; i++) {
					var variable = scope.variables [i];
					DefineLocalVariable (variable, sym_token, start_offset, end_offset);
				}
			}

			if (!scope.constants.IsNullOrEmpty ()) {
				for (int i = 0; i < scope.constants.Count; i++) {
					var constant = scope.constants [i];
					DefineConstant (constant);
				}
			}

			if (!scope.scopes.IsNullOrEmpty ()) {
				for (int i = 0; i < scope.scopes.Count; i++) {
					MetadataToken _;
					DefineScope (scope.scopes [i], info, out _);
				}
			}

			writer.CloseScope (end_offset);
		}

		void DefineSequencePoints (Collection<SequencePoint> sequence_points)
		{
			for (int i = 0; i < sequence_points.Count; i++) {
				var sequence_point = sequence_points [i];

				writer.DefineSequencePoints (
					GetDocument (sequence_point.Document),
					new [] { sequence_point.Offset },
					new [] { sequence_point.StartLine },
					new [] { sequence_point.StartColumn },
					new [] { sequence_point.EndLine },
					new [] { sequence_point.EndColumn });
			}
		}

		void DefineLocalVariable (VariableDebugInformation variable, int local_var_token, int start_offset, int end_offset)
		{
			writer.DefineLocalVariable2 (
				variable.Name,
				variable.Attributes,
				local_var_token,
				variable.Index,
				0,
				0,
				start_offset,
				end_offset);
		}

		void DefineConstant (ConstantDebugInformation constant)
		{
			var row = metadata.AddStandAloneSignature (metadata.GetConstantTypeBlobIndex (constant.ConstantType));
			var token = new MetadataToken (TokenType.Signature, row);

			writer.DefineConstant2 (constant.Name, constant.Value, token.ToInt32 ());
		}

		SymDocumentWriter GetDocument (Document document)
		{
			if (document == null)
				return null;

			SymDocumentWriter doc_writer;
			if (documents.TryGetValue (document.Url, out doc_writer))
				return doc_writer;

			doc_writer = writer.DefineDocument (
				document.Url,
				document.LanguageGuid,
				document.LanguageVendorGuid,
				document.TypeGuid);

			if (!document.Hash.IsNullOrEmpty ())
				doc_writer.SetCheckSum (document.HashAlgorithmGuid, document.Hash);

			documents [document.Url] = doc_writer;
			return doc_writer;
		}

		public void Dispose ()
		{
			var entry_point = module.EntryPoint;
			if (entry_point != null)
				writer.SetUserEntryPoint (entry_point.MetadataToken.ToInt32 ());

			writer.Close ();
		}
	}

	enum CustomMetadataType : byte {
		UsingInfo = 0,
		ForwardInfo = 1,
		IteratorScopes = 3,
		ForwardIterator = 4,
	}

	class CustomMetadataWriter : IDisposable {

		readonly SymWriter sym_writer;
		readonly MemoryStream stream;
		readonly BinaryStreamWriter writer;

		int count;

		const byte version = 4;

		public CustomMetadataWriter (SymWriter sym_writer)
		{
			this.sym_writer = sym_writer;
			this.stream = new MemoryStream ();
			this.writer = new BinaryStreamWriter (stream);

			writer.WriteByte (version);
			writer.WriteByte (0); // count
			writer.Align (4);
		}

		public void WriteUsingInfo (ImportDebugInformation import_info)
		{
			Write (CustomMetadataType.UsingInfo, () => {
				writer.WriteUInt16 ((ushort) 1);
				writer.WriteUInt16 ((ushort) import_info.Targets.Count);
			});
		}

		public void WriteForwardInfo (MetadataToken import_parent)
		{
			Write (CustomMetadataType.ForwardInfo, () => writer.WriteUInt32 (import_parent.ToUInt32 ()));
		}

		public void WriteIteratorScopes (StateMachineScopeDebugInformation state_machine, MethodDebugInformation debug_info)
		{
			Write (CustomMetadataType.IteratorScopes, () => {
				var scopes = state_machine.Scopes;
				writer.WriteInt32 (scopes.Count);
				foreach (var scope in scopes) {
					var start = scope.Start.Offset;
					var end = scope.End.IsEndOfMethod ? debug_info.code_size : scope.End.Offset;
					writer.WriteInt32 (start);
					writer.WriteInt32 (end - 1);
				}
			});
		}

		public void WriteForwardIterator (TypeReference type)
		{
			Write (CustomMetadataType.ForwardIterator, () => writer.WriteBytes(Encoding.Unicode.GetBytes(type.Name)));
		}

		void Write (CustomMetadataType type, Action write)
		{
			count++;
			writer.WriteByte (version);
			writer.WriteByte ((byte) type);
			writer.Align (4);

			var length_position = writer.Position;
			writer.WriteUInt32 (0);

			write ();
			writer.Align (4);

			var end = writer.Position;
			var length = end - length_position + 4; // header is 4 bytes long

			writer.Position = length_position;
			writer.WriteInt32 (length);

			writer.Position = end;
		}

		public void WriteCustomMetadata ()
		{
			if (count == 0)
				return;

			writer.BaseStream.Position = 1;
			writer.WriteByte ((byte) count);
			writer.Flush ();

			sym_writer.DefineCustomMetadata ("MD2", stream.ToArray ());
		}

		public void Dispose ()
		{
			stream.Dispose ();
		}
	}
}