| 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.Diagnostics.SymbolStore; |
| 14 |
|
| 15 |
using Mono.Cecil.Cil; |
| 16 |
using Mono.Collections.Generic; |
| 17 |
|
| 18 |
#if !READ_ONLY |
| 19 |
|
| 20 |
namespace Mono.Cecil.Pdb { |
| 21 |
|
| 22 |
public class PdbWriter : Cil.ISymbolWriter { |
| 23 |
|
| 24 |
readonly ModuleDefinition module; |
| 25 |
readonly SymWriter writer; |
| 26 |
readonly Dictionary<string, SymDocumentWriter> documents; |
| 27 |
|
| 28 |
internal PdbWriter (ModuleDefinition module, SymWriter writer) |
| 29 |
{ |
| 30 |
this.module = module; |
| 31 |
this.writer = writer; |
| 32 |
this.documents = new Dictionary<string, SymDocumentWriter> (); |
| 33 |
} |
| 34 |
|
| 35 |
public bool GetDebugHeader (out ImageDebugDirectory directory, out byte [] header) |
| 36 |
{ |
| 37 |
header = writer.GetDebugInfo (out directory); |
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
public void Write (MethodBody body) |
| 42 |
{ |
| 43 |
var method_token = body.Method.MetadataToken; |
| 44 |
var sym_token = new SymbolToken (method_token.ToInt32 ()); |
| 45 |
|
| 46 |
var instructions = CollectInstructions (body); |
| 47 |
if (instructions.Count == 0) |
| 48 |
return; |
| 49 |
|
| 50 |
var start_offset = 0; |
| 51 |
var end_offset = body.CodeSize; |
| 52 |
|
| 53 |
writer.OpenMethod (sym_token); |
| 54 |
writer.OpenScope (start_offset); |
| 55 |
|
| 56 |
DefineSequencePoints (instructions); |
| 57 |
DefineVariables (body, start_offset, end_offset); |
| 58 |
|
| 59 |
writer.CloseScope (end_offset); |
| 60 |
writer.CloseMethod (); |
| 61 |
} |
| 62 |
|
| 63 |
Collection<Instruction> CollectInstructions (MethodBody body) |
| 64 |
{ |
| 65 |
var collection = new Collection<Instruction> (); |
| 66 |
var instructions = body.Instructions; |
| 67 |
|
| 68 |
for (int i = 0; i < instructions.Count; i++) { |
| 69 |
var instruction = instructions [i]; |
| 70 |
var sequence_point = instruction.SequencePoint; |
| 71 |
if (sequence_point == null) |
| 72 |
continue; |
| 73 |
|
| 74 |
GetDocument (sequence_point.Document); |
| 75 |
collection.Add (instruction); |
| 76 |
} |
| 77 |
|
| 78 |
return collection; |
| 79 |
} |
| 80 |
|
| 81 |
void DefineVariables (MethodBody body, int start_offset, int end_offset) |
| 82 |
{ |
| 83 |
if (!body.HasVariables) |
| 84 |
return; |
| 85 |
|
| 86 |
var sym_token = new SymbolToken (body.LocalVarToken.ToInt32 ()); |
| 87 |
|
| 88 |
var variables = body.Variables; |
| 89 |
for (int i = 0; i < variables.Count; i++) { |
| 90 |
var variable = variables [i]; |
| 91 |
CreateLocalVariable (variable, sym_token, start_offset, end_offset); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void DefineSequencePoints (Collection<Instruction> instructions) |
| 96 |
{ |
| 97 |
for (int i = 0; i < instructions.Count; i++) { |
| 98 |
var instruction = instructions [i]; |
| 99 |
var sequence_point = instruction.SequencePoint; |
| 100 |
|
| 101 |
writer.DefineSequencePoints ( |
| 102 |
GetDocument (sequence_point.Document), |
| 103 |
new [] { instruction.Offset }, |
| 104 |
new [] { sequence_point.StartLine }, |
| 105 |
new [] { sequence_point.StartColumn }, |
| 106 |
new [] { sequence_point.EndLine }, |
| 107 |
new [] { sequence_point.EndColumn }); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void CreateLocalVariable (VariableDefinition variable, SymbolToken local_var_token, int start_offset, int end_offset) |
| 112 |
{ |
| 113 |
writer.DefineLocalVariable2 ( |
| 114 |
variable.Name, |
| 115 |
0, |
| 116 |
local_var_token, |
| 117 |
SymAddressKind.ILOffset, |
| 118 |
variable.Index, |
| 119 |
0, |
| 120 |
0, |
| 121 |
start_offset, |
| 122 |
end_offset); |
| 123 |
} |
| 124 |
|
| 125 |
SymDocumentWriter GetDocument (Document document) |
| 126 |
{ |
| 127 |
if (document == null) |
| 128 |
return null; |
| 129 |
|
| 130 |
SymDocumentWriter doc_writer; |
| 131 |
if (documents.TryGetValue (document.Url, out doc_writer)) |
| 132 |
return doc_writer; |
| 133 |
|
| 134 |
doc_writer = writer.DefineDocument ( |
| 135 |
document.Url, |
| 136 |
document.Language.ToGuid (), |
| 137 |
document.LanguageVendor.ToGuid (), |
| 138 |
document.Type.ToGuid ()); |
| 139 |
|
| 140 |
documents [document.Url] = doc_writer; |
| 141 |
return doc_writer; |
| 142 |
} |
| 143 |
|
| 144 |
public void Write (MethodSymbols symbols) |
| 145 |
{ |
| 146 |
var sym_token = new SymbolToken (symbols.MethodToken.ToInt32 ()); |
| 147 |
|
| 148 |
var start_offset = 0; |
| 149 |
var end_offset = symbols.CodeSize; |
| 150 |
|
| 151 |
writer.OpenMethod (sym_token); |
| 152 |
writer.OpenScope (start_offset); |
| 153 |
|
| 154 |
DefineSequencePoints (symbols); |
| 155 |
DefineVariables (symbols, start_offset, end_offset); |
| 156 |
|
| 157 |
writer.CloseScope (end_offset); |
| 158 |
writer.CloseMethod (); |
| 159 |
} |
| 160 |
|
| 161 |
void DefineSequencePoints (MethodSymbols symbols) |
| 162 |
{ |
| 163 |
var instructions = symbols.instructions; |
| 164 |
|
| 165 |
for (int i = 0; i < instructions.Count; i++) { |
| 166 |
var instruction = instructions [i]; |
| 167 |
var sequence_point = instruction.SequencePoint; |
| 168 |
|
| 169 |
writer.DefineSequencePoints ( |
| 170 |
GetDocument (sequence_point.Document), |
| 171 |
new [] { instruction.Offset }, |
| 172 |
new [] { sequence_point.StartLine }, |
| 173 |
new [] { sequence_point.StartColumn }, |
| 174 |
new [] { sequence_point.EndLine }, |
| 175 |
new [] { sequence_point.EndColumn }); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
void DefineVariables (MethodSymbols symbols, int start_offset, int end_offset) |
| 180 |
{ |
| 181 |
if (!symbols.HasVariables) |
| 182 |
return; |
| 183 |
|
| 184 |
var sym_token = new SymbolToken (symbols.LocalVarToken.ToInt32 ()); |
| 185 |
|
| 186 |
var variables = symbols.Variables; |
| 187 |
for (int i = 0; i < variables.Count; i++) { |
| 188 |
var variable = variables [i]; |
| 189 |
CreateLocalVariable (variable, sym_token, start_offset, end_offset); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
public void Dispose () |
| 194 |
{ |
| 195 |
var entry_point = module.EntryPoint; |
| 196 |
if (entry_point != null) |
| 197 |
writer.SetUserEntryPoint (new SymbolToken (entry_point.MetadataToken.ToInt32 ())); |
| 198 |
|
| 199 |
writer.Close (); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
#endif |