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

MdbReader.cs « Mono.Cecil.Mdb « mdb « symbols - github.com/mono/cecil.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 59e933a3708f148543969719b435e2b8ddad4975 (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
//
// 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 {

	public sealed class MdbReaderProvider : ISymbolReaderProvider {

		public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName)
		{
			Mixin.CheckModule (module);
			Mixin.CheckFileName (fileName);

			return new MdbReader (module, MonoSymbolFile.ReadSymbolFile (Mixin.GetMdbFileName (fileName), module.Mvid));
		}

		public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream)
		{
			Mixin.CheckModule (module);
			Mixin.CheckStream (symbolStream);

			var file = MonoSymbolFile.ReadSymbolFile (symbolStream);
			if (module.Mvid != file.Guid) {
				var file_stream = symbolStream as FileStream;
				if (file_stream != null)
					throw new MonoSymbolFileException ("Symbol file `{0}' does not match assembly", file_stream.Name);

				throw new MonoSymbolFileException ("Symbol file from stream does not match assembly");
			}
			return new MdbReader (module, file);
		}
	}

	public sealed class MdbReader : ISymbolReader {

		readonly ModuleDefinition module;
		readonly MonoSymbolFile symbol_file;
		readonly Dictionary<string, Document> documents;

		public MdbReader (ModuleDefinition module, MonoSymbolFile symFile)
		{
			this.module = module;
			this.symbol_file = symFile;
			this.documents = new Dictionary<string, Document> ();
		}

#if !READ_ONLY
		public ISymbolWriterProvider GetWriterProvider ()
		{
			return new MdbWriterProvider ();
		}
#endif

		public bool ProcessDebugHeader (ImageDebugHeader header)
		{
			return symbol_file.Guid == module.Mvid;
		}

		public MethodDebugInformation Read (MethodDefinition method)
		{
			var method_token = method.MetadataToken;
			var entry = symbol_file.GetMethodByToken (method_token.ToInt32	());
			if (entry == null)
				return null;

			var info = new MethodDebugInformation (method);
			info.code_size = ReadCodeSize (method);

			var scopes = ReadScopes (entry, info);
			ReadLineNumbers (entry, info);
			ReadLocalVariables (entry, scopes);

			return info;
		}

		static int ReadCodeSize (MethodDefinition method)
		{
			return method.Module.Read (method, (m, reader) => reader.ReadCodeSize (m));
		}

		static void ReadLocalVariables (MethodEntry entry, ScopeDebugInformation [] scopes)
		{
			var locals = entry.GetLocals ();

			foreach (var local in locals) {
				var variable = new VariableDebugInformation (local.Index, local.Name);

				var index = local.BlockIndex;
				if (index < 0 || index >= scopes.Length)
					continue;

				var scope = scopes [index];
				if (scope == null)
					continue;

				scope.Variables.Add (variable);
			}
		}

		void ReadLineNumbers (MethodEntry entry, MethodDebugInformation info)
		{
			var table = entry.GetLineNumberTable ();

			info.sequence_points = new Collection<SequencePoint> (table.LineNumbers.Length);

			for (var i = 0; i < table.LineNumbers.Length; i++) {
				var line = table.LineNumbers [i];
				if (i > 0 && table.LineNumbers [i - 1].Offset == line.Offset)
					continue;

				info.sequence_points.Add (LineToSequencePoint (line));
			}
		}

		Document GetDocument (SourceFileEntry file)
		{
			var file_name = file.FileName;

			Document document;
			if (documents.TryGetValue (file_name, out document))
				return document;

			document = new Document (file_name) {
				Hash = file.Checksum,
			};

			documents.Add (file_name, document);

			return document;
		}

		static ScopeDebugInformation [] ReadScopes (MethodEntry entry, MethodDebugInformation info)
		{
			var blocks = entry.GetCodeBlocks ();
			var scopes = new ScopeDebugInformation [blocks.Length + 1];

			info.scope = scopes [0] = new ScopeDebugInformation {
				Start = new InstructionOffset (0),
				End = new InstructionOffset (info.code_size),
			};

			foreach (var block in blocks) {
				if (block.BlockType != CodeBlockEntry.Type.Lexical && block.BlockType != CodeBlockEntry.Type.CompilerGenerated)
					continue;

				var scope = new ScopeDebugInformation ();
				scope.Start = new InstructionOffset (block.StartOffset);
				scope.End = new InstructionOffset (block.EndOffset);

				scopes [block.Index + 1] = scope;

				if (!AddScope (info.scope.Scopes, scope))
					info.scope.Scopes.Add (scope);
			}

			return scopes;
		}

		static bool AddScope (Collection<ScopeDebugInformation> scopes, ScopeDebugInformation scope)
		{
			foreach (var sub_scope in scopes) {
				if (sub_scope.HasScopes && AddScope (sub_scope.Scopes, scope))
					return true;

				if (scope.Start.Offset >= sub_scope.Start.Offset && scope.End.Offset <= sub_scope.End.Offset) {
					sub_scope.Scopes.Add (scope);
					return true;
				}
			}

			return false;
		}

		SequencePoint LineToSequencePoint (LineNumberEntry line)
		{
			var source = symbol_file.GetSourceFile (line.File);
			return new SequencePoint (line.Offset, GetDocument (source)) {
				StartLine = line.Row,
				EndLine = line.EndRow,
				StartColumn = line.Column,
				EndColumn = line.EndColumn,
			};
		}

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

	static class MethodEntryExtensions {

		public static bool HasColumnInfo (this MethodEntry entry)
		{
			return (entry.MethodFlags & MethodEntry.Flags.ColumnsInfoIncluded) != 0;
		}

		public static bool HasEndInfo (this MethodEntry entry)
		{
			return (entry.MethodFlags & MethodEntry.Flags.EndInfoIncluded) != 0;
		}
	}
}