Debian Codegraph @

external/cecil-legacy/symbols/pdb/Mono.Cecil.Pdb/PdbWriter.cs

raw symbols SCIP
1 //
2 // PdbWriter.cs
3 //
4 // Author:
5 // Jb Evain ([email protected])
6 //
7 // Copyright (c) 2008 - 2011 Jb Evain
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Collections.Generic;
31 using System.Diagnostics.SymbolStore;
32
33 using Mono.Cecil.Cil;
34 using Mono.Collections.Generic;
35
36 #if !READ_ONLY
37
38 namespace Mono.Cecil.Pdb {
39
40 public class PdbWriter : Cil.ISymbolWriter {
41
42 readonly ModuleDefinition module;
43 readonly SymWriter writer;
44 readonly Dictionary<string, SymDocumentWriter> documents;
45
46 internal PdbWriter (ModuleDefinition module, SymWriter writer)
47 {
48 this.module = module;
49 this.writer = writer;
50 this.documents = new Dictionary<string, SymDocumentWriter> ();
51 }
52
53 public bool GetDebugHeader (out ImageDebugDirectory directory, out byte [] header)
54 {
55 header = writer.GetDebugInfo (out directory);
56 return true;
57 }
58
59 public void Write (MethodBody body)
60 {
61 var method_token = body.Method.MetadataToken;
62 var sym_token = new SymbolToken (method_token.ToInt32 ());
63
64 var instructions = CollectInstructions (body);
65 if (instructions.Count == 0)
66 return;
67
68 var start_offset = 0;
69 var end_offset = body.CodeSize;
70
71 writer.OpenMethod (sym_token);
72 writer.OpenScope (start_offset);
73
74 DefineSequencePoints (instructions);
75 DefineVariables (body, start_offset, end_offset);
76
77 writer.CloseScope (end_offset);
78 writer.CloseMethod ();
79 }
80
81 Collection<Instruction> CollectInstructions (MethodBody body)
82 {
83 var collection = new Collection<Instruction> ();
84 var instructions = body.Instructions;
85
86 for (int i = 0; i < instructions.Count; i++) {
87 var instruction = instructions [i];
88 var sequence_point = instruction.SequencePoint;
89 if (sequence_point == null)
90 continue;
91
92 GetDocument (sequence_point.Document);
93 collection.Add (instruction);
94 }
95
96 return collection;
97 }
98
99 void DefineVariables (MethodBody body, int start_offset, int end_offset)
100 {
101 if (!body.HasVariables)
102 return;
103
104 var sym_token = new SymbolToken (body.LocalVarToken.ToInt32 ());
105
106 var variables = body.Variables;
107 for (int i = 0; i < variables.Count; i++) {
108 var variable = variables [i];
109 CreateLocalVariable (variable, sym_token, start_offset, end_offset);
110 }
111 }
112
113 void DefineSequencePoints (Collection<Instruction> instructions)
114 {
115 for (int i = 0; i < instructions.Count; i++) {
116 var instruction = instructions [i];
117 var sequence_point = instruction.SequencePoint;
118
119 writer.DefineSequencePoints (
120 GetDocument (sequence_point.Document),
121 new [] { instruction.Offset },
122 new [] { sequence_point.StartLine },
123 new [] { sequence_point.StartColumn },
124 new [] { sequence_point.EndLine },
125 new [] { sequence_point.EndColumn });
126 }
127 }
128
129 void CreateLocalVariable (VariableDefinition variable, SymbolToken local_var_token, int start_offset, int end_offset)
130 {
131 writer.DefineLocalVariable2 (
132 variable.Name,
133 0,
134 local_var_token,
135 SymAddressKind.ILOffset,
136 variable.Index,
137 0,
138 0,
139 start_offset,
140 end_offset);
141 }
142
143 SymDocumentWriter GetDocument (Document document)
144 {
145 if (document == null)
146 return null;
147
148 SymDocumentWriter doc_writer;
149 if (documents.TryGetValue (document.Url, out doc_writer))
150 return doc_writer;
151
152 doc_writer = writer.DefineDocument (
153 document.Url,
154 document.Language.ToGuid (),
155 document.LanguageVendor.ToGuid (),
156 document.Type.ToGuid ());
157
158 documents [document.Url] = doc_writer;
159 return doc_writer;
160 }
161
162 public void Write (MethodSymbols symbols)
163 {
164 var sym_token = new SymbolToken (symbols.MethodToken.ToInt32 ());
165
166 var start_offset = 0;
167 var end_offset = symbols.CodeSize;
168
169 writer.OpenMethod (sym_token);
170 writer.OpenScope (start_offset);
171
172 DefineSequencePoints (symbols);
173 DefineVariables (symbols, start_offset, end_offset);
174
175 writer.CloseScope (end_offset);
176 writer.CloseMethod ();
177 }
178
179 void DefineSequencePoints (MethodSymbols symbols)
180 {
181 var instructions = symbols.instructions;
182
183 for (int i = 0; i < instructions.Count; i++) {
184 var instruction = instructions [i];
185 var sequence_point = instruction.SequencePoint;
186
187 writer.DefineSequencePoints (
188 GetDocument (sequence_point.Document),
189 new [] { instruction.Offset },
190 new [] { sequence_point.StartLine },
191 new [] { sequence_point.StartColumn },
192 new [] { sequence_point.EndLine },
193 new [] { sequence_point.EndColumn });
194 }
195 }
196
197 void DefineVariables (MethodSymbols symbols, int start_offset, int end_offset)
198 {
199 if (!symbols.HasVariables)
200 return;
201
202 var sym_token = new SymbolToken (symbols.LocalVarToken.ToInt32 ());
203
204 var variables = symbols.Variables;
205 for (int i = 0; i < variables.Count; i++) {
206 var variable = variables [i];
207 CreateLocalVariable (variable, sym_token, start_offset, end_offset);
208 }
209 }
210
211 public void Dispose ()
212 {
213 var entry_point = module.EntryPoint;
214 if (entry_point != null)
215 writer.SetUserEntryPoint (new SymbolToken (entry_point.MetadataToken.ToInt32 ()));
216
217 writer.Close ();
218 }
219 }
220 }
221
222 #endif