| 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.IO; |
| 14 |
|
| 15 |
using Mono.Collections.Generic; |
| 16 |
|
| 17 |
using Microsoft.Cci.Pdb; |
| 18 |
|
| 19 |
using Mono.Cecil.Cil; |
| 20 |
|
| 21 |
namespace Mono.Cecil.Pdb { |
| 22 |
|
| 23 |
public class NativePdbReader : ISymbolReader { |
| 24 |
|
| 25 |
int age; |
| 26 |
Guid guid; |
| 27 |
|
| 28 |
readonly Disposable<Stream> pdb_file; |
| 29 |
readonly Dictionary<string, Document> documents = new Dictionary<string, Document> (); |
| 30 |
readonly Dictionary<uint, PdbFunction> functions = new Dictionary<uint, PdbFunction> (); |
| 31 |
readonly Dictionary<PdbScope, ImportDebugInformation> imports = new Dictionary<PdbScope, ImportDebugInformation> (); |
| 32 |
|
| 33 |
internal NativePdbReader (Disposable<Stream> file) |
| 34 |
{ |
| 35 |
this.pdb_file = file; |
| 36 |
} |
| 37 |
|
| 38 |
public ISymbolWriterProvider GetWriterProvider () |
| 39 |
{ |
| 40 |
return new NativePdbWriterProvider (); |
| 41 |
} |
| 42 |
|
| 43 |
/* |
| 44 |
uint Magic = 0x53445352; |
| 45 |
Guid Signature; |
| 46 |
uint Age; |
| 47 |
string FileName; |
| 48 |
*/ |
| 49 |
|
| 50 |
public bool ProcessDebugHeader (ImageDebugHeader header) |
| 51 |
{ |
| 52 |
if (!header.HasEntries) |
| 53 |
return false; |
| 54 |
|
| 55 |
var entry = header.GetCodeViewEntry (); |
| 56 |
if (entry == null) |
| 57 |
return false; |
| 58 |
|
| 59 |
var directory = entry.Directory; |
| 60 |
|
| 61 |
if (directory.Type != ImageDebugType.CodeView) |
| 62 |
return false; |
| 63 |
|
| 64 |
var data = entry.Data; |
| 65 |
|
| 66 |
if (data.Length < 24) |
| 67 |
return false; |
| 68 |
|
| 69 |
var magic = ReadInt32 (data, 0); |
| 70 |
if (magic != 0x53445352) |
| 71 |
return false; |
| 72 |
|
| 73 |
var guid_bytes = new byte [16]; |
| 74 |
Buffer.BlockCopy (data, 4, guid_bytes, 0, 16); |
| 75 |
|
| 76 |
this.guid = new Guid (guid_bytes); |
| 77 |
this.age = ReadInt32 (data, 20); |
| 78 |
|
| 79 |
return PopulateFunctions (); |
| 80 |
} |
| 81 |
|
| 82 |
static int ReadInt32 (byte [] bytes, int start) |
| 83 |
{ |
| 84 |
return (bytes [start] |
| 85 |
| (bytes [start + 1] << 8) |
| 86 |
| (bytes [start + 2] << 16) |
| 87 |
| (bytes [start + 3] << 24)); |
| 88 |
} |
| 89 |
|
| 90 |
bool PopulateFunctions () |
| 91 |
{ |
| 92 |
using (pdb_file) { |
| 93 |
var info = PdbFile.LoadFunctions (pdb_file.value); |
| 94 |
|
| 95 |
if (this.guid != info.Guid) |
| 96 |
return false; |
| 97 |
|
| 98 |
foreach (PdbFunction function in info.Functions) |
| 99 |
functions.Add (function.token, function); |
| 100 |
} |
| 101 |
|
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
public MethodDebugInformation Read (MethodDefinition method) |
| 106 |
{ |
| 107 |
var method_token = method.MetadataToken; |
| 108 |
|
| 109 |
PdbFunction function; |
| 110 |
if (!functions.TryGetValue (method_token.ToUInt32 (), out function)) |
| 111 |
return null; |
| 112 |
|
| 113 |
var symbol = new MethodDebugInformation (method); |
| 114 |
|
| 115 |
ReadSequencePoints (function, symbol); |
| 116 |
|
| 117 |
symbol.scope = !function.scopes.IsNullOrEmpty () |
| 118 |
? ReadScopeAndLocals (function.scopes [0], symbol) |
| 119 |
: new ScopeDebugInformation { Start = new InstructionOffset (0), End = new InstructionOffset ((int) function.length) }; |
| 120 |
|
| 121 |
if (function.tokenOfMethodWhoseUsingInfoAppliesToThisMethod != method.MetadataToken.ToUInt32 () && function.tokenOfMethodWhoseUsingInfoAppliesToThisMethod != 0) |
| 122 |
symbol.scope.import = GetImport (function.tokenOfMethodWhoseUsingInfoAppliesToThisMethod, method.Module); |
| 123 |
|
| 124 |
if (function.scopes.Length > 1) { |
| 125 |
for (int i = 1; i < function.scopes.Length; i++) { |
| 126 |
var s = ReadScopeAndLocals (function.scopes [i], symbol); |
| 127 |
if (!AddScope (symbol.scope.Scopes, s)) |
| 128 |
symbol.scope.Scopes.Add (s); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
if (function.iteratorScopes != null) { |
| 133 |
var state_machine = new StateMachineScopeDebugInformation (); |
| 134 |
|
| 135 |
foreach (var iterator_scope in function.iteratorScopes) { |
| 136 |
state_machine.Scopes.Add (new StateMachineScope ((int) iterator_scope.Offset, (int) (iterator_scope.Offset + iterator_scope.Length + 1))); |
| 137 |
} |
| 138 |
|
| 139 |
symbol.CustomDebugInformations.Add (state_machine); |
| 140 |
} |
| 141 |
|
| 142 |
if (function.synchronizationInformation != null) { |
| 143 |
var async_debug_info = new AsyncMethodBodyDebugInformation ((int) function.synchronizationInformation.GeneratedCatchHandlerOffset); |
| 144 |
|
| 145 |
foreach (var synchronization_point in function.synchronizationInformation.synchronizationPoints) { |
| 146 |
async_debug_info.Yields.Add (new InstructionOffset ((int) synchronization_point.SynchronizeOffset)); |
| 147 |
async_debug_info.Resumes.Add (new InstructionOffset ((int) synchronization_point.ContinuationOffset)); |
| 148 |
async_debug_info.ResumeMethods.Add (method); |
| 149 |
} |
| 150 |
|
| 151 |
symbol.CustomDebugInformations.Add (async_debug_info); |
| 152 |
|
| 153 |
symbol.StateMachineKickOffMethod = (MethodDefinition) method.Module.LookupToken ((int) function.synchronizationInformation.kickoffMethodToken); |
| 154 |
} |
| 155 |
|
| 156 |
return symbol; |
| 157 |
} |
| 158 |
|
| 159 |
Collection<ScopeDebugInformation> ReadScopeAndLocals (PdbScope [] scopes, MethodDebugInformation info) |
| 160 |
{ |
| 161 |
var symbols = new Collection<ScopeDebugInformation> (scopes.Length); |
| 162 |
|
| 163 |
foreach (PdbScope scope in scopes) |
| 164 |
if (scope != null) |
| 165 |
symbols.Add (ReadScopeAndLocals (scope, info)); |
| 166 |
|
| 167 |
return symbols; |
| 168 |
} |
| 169 |
|
| 170 |
ScopeDebugInformation ReadScopeAndLocals (PdbScope scope, MethodDebugInformation info) |
| 171 |
{ |
| 172 |
var parent = new ScopeDebugInformation (); |
| 173 |
parent.Start = new InstructionOffset ((int) scope.offset); |
| 174 |
parent.End = new InstructionOffset ((int) (scope.offset + scope.length)); |
| 175 |
|
| 176 |
if (!scope.slots.IsNullOrEmpty ()) { |
| 177 |
parent.variables = new Collection<VariableDebugInformation> (scope.slots.Length); |
| 178 |
|
| 179 |
foreach (PdbSlot slot in scope.slots) { |
| 180 |
if ((slot.flags & 1) != 0) // parameter names |
| 181 |
continue; |
| 182 |
|
| 183 |
var index = (int) slot.slot; |
| 184 |
var variable = new VariableDebugInformation (index, slot.name); |
| 185 |
if ((slot.flags & 4) != 0) |
| 186 |
variable.IsDebuggerHidden = true; |
| 187 |
parent.variables.Add (variable); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
if (!scope.constants.IsNullOrEmpty ()) { |
| 192 |
parent.constants = new Collection<ConstantDebugInformation> (scope.constants.Length); |
| 193 |
|
| 194 |
foreach (var constant in scope.constants) { |
| 195 |
var type = info.Method.Module.Read (constant, (c, r) => r.ReadConstantSignature (new MetadataToken (c.token))); |
| 196 |
var value = constant.value; |
| 197 |
|
| 198 |
// Object "null" is encoded as integer |
| 199 |
if (type != null && !type.IsValueType && value is int && (int) value == 0) |
| 200 |
value = null; |
| 201 |
|
| 202 |
parent.constants.Add (new ConstantDebugInformation (constant.name, type, value)); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if (!scope.usedNamespaces.IsNullOrEmpty ()) { |
| 207 |
ImportDebugInformation import; |
| 208 |
if (imports.TryGetValue (scope, out import)) { |
| 209 |
parent.import = import; |
| 210 |
} else { |
| 211 |
import = GetImport (scope, info.Method.Module); |
| 212 |
imports.Add (scope, import); |
| 213 |
parent.import = import; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
parent.scopes = ReadScopeAndLocals (scope.scopes, info); |
| 218 |
|
| 219 |
return parent; |
| 220 |
} |
| 221 |
|
| 222 |
static bool AddScope (Collection<ScopeDebugInformation> scopes, ScopeDebugInformation scope) |
| 223 |
{ |
| 224 |
foreach (var sub_scope in scopes) { |
| 225 |
if (sub_scope.HasScopes && AddScope (sub_scope.Scopes, scope)) |
| 226 |
return true; |
| 227 |
|
| 228 |
if (scope.Start.Offset >= sub_scope.Start.Offset && scope.End.Offset <= sub_scope.End.Offset) { |
| 229 |
sub_scope.Scopes.Add (scope); |
| 230 |
return true; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
ImportDebugInformation GetImport (uint token, ModuleDefinition module) |
| 238 |
{ |
| 239 |
PdbFunction function; |
| 240 |
if (!functions.TryGetValue (token, out function)) |
| 241 |
return null; |
| 242 |
|
| 243 |
if (function.scopes.Length != 1) |
| 244 |
return null; |
| 245 |
|
| 246 |
var scope = function.scopes [0]; |
| 247 |
|
| 248 |
ImportDebugInformation import; |
| 249 |
if (imports.TryGetValue (scope, out import)) |
| 250 |
return import; |
| 251 |
|
| 252 |
import = GetImport (scope, module); |
| 253 |
imports.Add (scope, import); |
| 254 |
return import; |
| 255 |
} |
| 256 |
|
| 257 |
static ImportDebugInformation GetImport (PdbScope scope, ModuleDefinition module) |
| 258 |
{ |
| 259 |
if (scope.usedNamespaces.IsNullOrEmpty ()) |
| 260 |
return null; |
| 261 |
|
| 262 |
var import = new ImportDebugInformation (); |
| 263 |
|
| 264 |
foreach (var used_namespace in scope.usedNamespaces) { |
| 265 |
if (string.IsNullOrEmpty (used_namespace)) |
| 266 |
continue; |
| 267 |
|
| 268 |
ImportTarget target = null; |
| 269 |
var value = used_namespace.Substring (1); |
| 270 |
switch (used_namespace [0]) { |
| 271 |
case 'U': |
| 272 |
target = new ImportTarget (ImportTargetKind.ImportNamespace) { @namespace = value }; |
| 273 |
break; |
| 274 |
case 'T': { |
| 275 |
var type = TypeParser.ParseType (module, value); |
| 276 |
if (type != null) |
| 277 |
target = new ImportTarget (ImportTargetKind.ImportType) { type = type }; |
| 278 |
break; |
| 279 |
} |
| 280 |
case 'A': |
| 281 |
var index = used_namespace.IndexOf (' '); |
| 282 |
if (index < 0) { |
| 283 |
target = new ImportTarget (ImportTargetKind.ImportNamespace) { @namespace = used_namespace }; |
| 284 |
break; |
| 285 |
} |
| 286 |
var alias_value = used_namespace.Substring (1, index - 1); |
| 287 |
var alias_target_value = used_namespace.Substring (index + 2); |
| 288 |
switch (used_namespace [index + 1]) { |
| 289 |
case 'U': |
| 290 |
target = new ImportTarget (ImportTargetKind.DefineNamespaceAlias) { alias = alias_value, @namespace = alias_target_value }; |
| 291 |
break; |
| 292 |
case 'T': |
| 293 |
var type = TypeParser.ParseType (module, alias_target_value); |
| 294 |
if (type != null) |
| 295 |
target = new ImportTarget (ImportTargetKind.DefineTypeAlias) { alias = alias_value, type = type }; |
| 296 |
break; |
| 297 |
} |
| 298 |
break; |
| 299 |
case '*': |
| 300 |
target = new ImportTarget (ImportTargetKind.ImportNamespace) { @namespace = value }; |
| 301 |
break; |
| 302 |
case '@': |
| 303 |
if (!value.StartsWith ("P:")) |
| 304 |
continue; |
| 305 |
|
| 306 |
target = new ImportTarget (ImportTargetKind.ImportNamespace) { @namespace = value.Substring (2) }; |
| 307 |
break; |
| 308 |
} |
| 309 |
|
| 310 |
if (target != null) |
| 311 |
import.Targets.Add (target); |
| 312 |
} |
| 313 |
|
| 314 |
return import; |
| 315 |
} |
| 316 |
|
| 317 |
void ReadSequencePoints (PdbFunction function, MethodDebugInformation info) |
| 318 |
{ |
| 319 |
if (function.lines == null) |
| 320 |
return; |
| 321 |
|
| 322 |
info.sequence_points = new Collection<SequencePoint> (); |
| 323 |
|
| 324 |
foreach (PdbLines lines in function.lines) |
| 325 |
ReadLines (lines, info); |
| 326 |
} |
| 327 |
|
| 328 |
void ReadLines (PdbLines lines, MethodDebugInformation info) |
| 329 |
{ |
| 330 |
var document = GetDocument (lines.file); |
| 331 |
|
| 332 |
foreach (var line in lines.lines) |
| 333 |
ReadLine (line, document, info); |
| 334 |
} |
| 335 |
|
| 336 |
static void ReadLine (PdbLine line, Document document, MethodDebugInformation info) |
| 337 |
{ |
| 338 |
var sequence_point = new SequencePoint ((int) line.offset, document); |
| 339 |
sequence_point.StartLine = (int) line.lineBegin; |
| 340 |
sequence_point.StartColumn = (int) line.colBegin; |
| 341 |
sequence_point.EndLine = (int) line.lineEnd; |
| 342 |
sequence_point.EndColumn = (int) line.colEnd; |
| 343 |
|
| 344 |
info.sequence_points.Add (sequence_point); |
| 345 |
} |
| 346 |
|
| 347 |
Document GetDocument (PdbSource source) |
| 348 |
{ |
| 349 |
string name = source.name; |
| 350 |
Document document; |
| 351 |
if (documents.TryGetValue (name, out document)) |
| 352 |
return document; |
| 353 |
|
| 354 |
document = new Document (name) { |
| 355 |
LanguageGuid = source.language, |
| 356 |
LanguageVendorGuid = source.vendor, |
| 357 |
TypeGuid = source.doctype, |
| 358 |
HashAlgorithmGuid = source.checksumAlgorithm, |
| 359 |
Hash = source.checksum, |
| 360 |
}; |
| 361 |
documents.Add (name, document); |
| 362 |
return document; |
| 363 |
} |
| 364 |
|
| 365 |
public void Dispose () |
| 366 |
{ |
| 367 |
pdb_file.Dispose (); |
| 368 |
} |
| 369 |
} |
| 370 |
} |