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

MdbWriter.cs « Mono.Cecil.Mdb « mdb « symbols - github.com/mono/cecil.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 66c5b34ba5df606abb3eb82af8f6aa9e23a5d2bd (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
//
// 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 Mono.Cecil.Cil;
using Mono.Collections.Generic;
using Mono.CompilerServices.SymbolWriter;

namespace Mono.Cecil.Mdb {

#if !READ_ONLY
	public class MdbWriterProvider : ISymbolWriterProvider {

		public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName)
		{
			return new MdbWriter (module.Mvid, fileName);
		}

		public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream)
		{
			throw new NotImplementedException ();
		}
	}

	public class MdbWriter : ISymbolWriter {

		readonly Guid mvid;
		readonly MonoSymbolWriter writer;
		readonly Dictionary<string, SourceFile> source_files;

		public MdbWriter (Guid mvid, string assembly)
		{
			this.mvid = mvid;
			this.writer = new MonoSymbolWriter (assembly);
			this.source_files = new Dictionary<string, SourceFile> ();
		}

		static Collection<Instruction> GetInstructions (MethodBody body)
		{
			var instructions = new Collection<Instruction> ();
			foreach (var instruction in body.Instructions)
				if (instruction.SequencePoint != null)
					instructions.Add (instruction);

			return instructions;
		}

		SourceFile GetSourceFile (Document document)
		{
			var url = document.Url;

			SourceFile source_file;
			if (source_files.TryGetValue (url, out source_file))
				return source_file;

			var entry = writer.DefineDocument (url);
			var compile_unit = writer.DefineCompilationUnit (entry);

			source_file = new SourceFile (compile_unit, entry);
			source_files.Add (url, source_file);
			return source_file;
		}

		void Populate (Collection<Instruction> instructions, int [] offsets,
			int [] startRows, int [] endRows, int [] startCols, int [] endCols, out SourceFile file)
		{
			SourceFile source_file = null;

			for (int i = 0; i < instructions.Count; i++) {
				var instruction = instructions [i];
				offsets [i] = instruction.Offset;

				var sequence_point = instruction.SequencePoint;
				if (source_file == null)
					source_file = GetSourceFile (sequence_point.Document);

				startRows [i] = sequence_point.StartLine;
				endRows [i] = sequence_point.EndLine;
				startCols [i] = sequence_point.StartColumn;
				endCols [i] = sequence_point.EndColumn;
			}

			file = source_file;
		}

		public void Write (MethodBody body)
		{
			var method = new SourceMethod (body.Method);

			var instructions = GetInstructions (body);
			int count = instructions.Count;
			if (count == 0)
				return;

			var offsets = new int [count];
			var start_rows = new int [count];
			var end_rows = new int [count];
			var start_cols = new int [count];
			var end_cols = new int [count];

			SourceFile file;
			Populate (instructions, offsets, start_rows, end_rows, start_cols, end_cols, out file);

			var builder = writer.OpenMethod (file.CompilationUnit, 0, method);

			for (int i = 0; i < count; i++) {
				builder.MarkSequencePoint (
					offsets [i],
					file.CompilationUnit.SourceFile,
					start_rows [i],
					start_cols [i],
					end_rows [i],
					end_cols [i],
					false);
			}

			if (body.Scope != null && body.Scope.HasScopes)
				WriteScope (body.Scope, true);
			else 
				if (body.HasVariables)
					AddVariables (body.Variables);

			writer.CloseMethod ();
		}

		private void WriteScope (Scope scope, bool root)
		{
			if (scope.Start.Offset == scope.End.Offset) return;
			writer.OpenScope (scope.Start.Offset);


			if (scope.HasVariables)
			{
				foreach (var el in scope.Variables)
				{
					if (!String.IsNullOrEmpty (el.Name))
						writer.DefineLocalVariable (el.Index, el.Name);
				}
			}

			if (scope.HasScopes)
			{
				foreach (var el in scope.Scopes)
					WriteScope (el, false);
			}

			writer.CloseScope (scope.End.Offset + scope.End.GetSize());
		}

		readonly static byte [] empty_header = new byte [0];

		public bool GetDebugHeader (out ImageDebugDirectory directory, out byte [] header)
		{
			directory = new ImageDebugDirectory ();
			header = empty_header;
			return false;
		}

		void AddVariables (IList<VariableDefinition> variables)
		{
			for (int i = 0; i < variables.Count; i++) {
				var variable = variables [i];
				writer.DefineLocalVariable (i, variable.Name);
			}
		}

		public void Write (MethodSymbols symbols)
		{
			var method = new SourceMethodSymbol (symbols);

			var file = GetSourceFile (symbols.Instructions [0].SequencePoint.Document);
			var builder = writer.OpenMethod (file.CompilationUnit, 0, method);
			var count = symbols.Instructions.Count;

			for (int i = 0; i < count; i++) {
				var instruction = symbols.Instructions [i];
				var sequence_point = instruction.SequencePoint;

				builder.MarkSequencePoint (
					instruction.Offset,
					GetSourceFile (sequence_point.Document).CompilationUnit.SourceFile,
					sequence_point.StartLine,
					sequence_point.StartColumn,
					sequence_point.EndLine,
					sequence_point.EndColumn,
					false);
			}

			if (symbols.HasVariables)
				AddVariables (symbols.Variables);

			writer.CloseMethod ();
		}

		public void Dispose ()
		{
			writer.WriteSymbolFile (mvid);
		}

		class SourceFile : ISourceFile {

			readonly CompileUnitEntry compilation_unit;
			readonly SourceFileEntry entry;

			public SourceFileEntry Entry {
				get { return entry; }
			}

			public CompileUnitEntry CompilationUnit {
				get { return compilation_unit; }
			}

			public SourceFile (CompileUnitEntry comp_unit, SourceFileEntry entry)
			{
				this.compilation_unit = comp_unit;
				this.entry = entry;
			}
		}

		class SourceMethodSymbol : IMethodDef {

			readonly string name;
			readonly int token;

			public string Name {
				get { return name;}
			}

			public int Token {
				get { return token; }
			}

			public SourceMethodSymbol (MethodSymbols symbols)
			{
				name = symbols.MethodName;
				token = symbols.MethodToken.ToInt32 ();
			}
		}

		class SourceMethod : IMethodDef {

			readonly MethodDefinition method;

			public string Name {
				get { return method.Name; }
			}

			public int Token {
				get { return method.MetadataToken.ToInt32 (); }
			}

			public SourceMethod (MethodDefinition method)
			{
				this.method = method;
			}
		}
	}
#endif
}