2014-08-13 10:39:27 +01:00
|
|
|
//
|
|
|
|
// Author:
|
|
|
|
// Jb Evain (jbevain@gmail.com)
|
|
|
|
//
|
2016-11-10 13:04:39 +00:00
|
|
|
// Copyright (c) 2008 - 2015 Jb Evain
|
|
|
|
// Copyright (c) 2008 - 2011 Novell, Inc.
|
2014-08-13 10:39:27 +01:00
|
|
|
//
|
2016-11-10 13:04:39 +00:00
|
|
|
// Licensed under the MIT/X11 license.
|
2014-08-13 10:39:27 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
using Mono.Collections.Generic;
|
|
|
|
|
2014-08-13 10:39:27 +01:00
|
|
|
using Microsoft.Cci.Pdb;
|
|
|
|
|
|
|
|
using Mono.Cecil.Cil;
|
|
|
|
|
|
|
|
namespace Mono.Cecil.Pdb {
|
|
|
|
|
|
|
|
public class PdbReader : ISymbolReader {
|
|
|
|
|
|
|
|
int age;
|
|
|
|
Guid guid;
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
readonly Disposable<Stream> pdb_file;
|
2014-08-13 10:39:27 +01:00
|
|
|
readonly Dictionary<string, Document> documents = new Dictionary<string, Document> ();
|
|
|
|
readonly Dictionary<uint, PdbFunction> functions = new Dictionary<uint, PdbFunction> ();
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
internal PdbReader (Disposable<Stream> file)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
|
|
|
this.pdb_file = file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
uint Magic = 0x53445352;
|
|
|
|
Guid Signature;
|
|
|
|
uint Age;
|
|
|
|
string FileName;
|
|
|
|
*/
|
|
|
|
|
|
|
|
public bool ProcessDebugHeader (ImageDebugDirectory directory, byte [] header)
|
|
|
|
{
|
2016-11-10 13:04:39 +00:00
|
|
|
if (directory.Type != 2) //IMAGE_DEBUG_TYPE_CODEVIEW
|
|
|
|
return false;
|
|
|
|
if (directory.MajorVersion != 0 || directory.MinorVersion != 0)
|
|
|
|
return false;
|
|
|
|
|
2014-08-13 10:39:27 +01:00
|
|
|
if (header.Length < 24)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
var magic = ReadInt32 (header, 0);
|
|
|
|
if (magic != 0x53445352)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
var guid_bytes = new byte [16];
|
|
|
|
Buffer.BlockCopy (header, 4, guid_bytes, 0, 16);
|
|
|
|
|
|
|
|
this.guid = new Guid (guid_bytes);
|
|
|
|
this.age = ReadInt32 (header, 20);
|
|
|
|
|
|
|
|
return PopulateFunctions ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ReadInt32 (byte [] bytes, int start)
|
|
|
|
{
|
|
|
|
return (bytes [start]
|
|
|
|
| (bytes [start + 1] << 8)
|
|
|
|
| (bytes [start + 2] << 16)
|
|
|
|
| (bytes [start + 3] << 24));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PopulateFunctions ()
|
|
|
|
{
|
|
|
|
using (pdb_file) {
|
|
|
|
Dictionary<uint, PdbTokenLine> tokenToSourceMapping;
|
|
|
|
string sourceServerData;
|
|
|
|
int age;
|
|
|
|
Guid guid;
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
var funcs = PdbFile.LoadFunctions (pdb_file.value, out tokenToSourceMapping, out sourceServerData, out age, out guid);
|
2014-08-13 10:39:27 +01:00
|
|
|
|
|
|
|
if (this.guid != guid)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
foreach (PdbFunction function in funcs)
|
|
|
|
functions.Add (function.token, function);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
public MethodDebugInformation Read (MethodDefinition method)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
2016-11-10 13:04:39 +00:00
|
|
|
var method_token = method.MetadataToken;
|
2014-08-13 10:39:27 +01:00
|
|
|
|
|
|
|
PdbFunction function;
|
|
|
|
if (!functions.TryGetValue (method_token.ToUInt32 (), out function))
|
2016-11-10 13:04:39 +00:00
|
|
|
return null;
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
var symbol = new MethodDebugInformation (method);
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
ReadSequencePoints (function, symbol);
|
|
|
|
|
|
|
|
if (function.scopes.Length > 1)
|
|
|
|
throw new NotSupportedException ();
|
|
|
|
else if (function.scopes.Length == 1)
|
|
|
|
symbol.scope = ReadScopeAndLocals (function.scopes [0], symbol);
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
return symbol;
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
static Collection<ScopeDebugInformation> ReadScopeAndLocals (PdbScope [] scopes, MethodDebugInformation info)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
2016-11-10 13:04:39 +00:00
|
|
|
var symbols = new Collection<ScopeDebugInformation> (scopes.Length);
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
foreach (PdbScope scope in scopes)
|
|
|
|
if (scope != null)
|
|
|
|
symbols.Add (ReadScopeAndLocals (scope, info));
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
return symbols;
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
static ScopeDebugInformation ReadScopeAndLocals (PdbScope scope, MethodDebugInformation info)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
2016-11-10 13:04:39 +00:00
|
|
|
var parent = new ScopeDebugInformation ();
|
|
|
|
parent.Start = new InstructionOffset ((int) scope.offset);
|
|
|
|
parent.End = new InstructionOffset ((int) (scope.offset + scope.length));
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
if (!scope.slots.IsNullOrEmpty()) {
|
|
|
|
parent.variables = new Collection<VariableDebugInformation> (scope.slots.Length);
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
foreach (PdbSlot slot in scope.slots) {
|
|
|
|
var index = (int) slot.slot;
|
|
|
|
var variable = new VariableDebugInformation (index, slot.name);
|
|
|
|
if (slot.flags == 4)
|
|
|
|
variable.IsDebuggerHidden = true;
|
|
|
|
parent.variables.Add (variable);
|
|
|
|
}
|
|
|
|
}
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
if (!scope.constants.IsNullOrEmpty ()) {
|
|
|
|
parent.constants = new Collection<ConstantDebugInformation> (scope.constants.Length);
|
2014-08-13 10:39:27 +01:00
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
foreach (var constant in scope.constants) {
|
|
|
|
parent.constants.Add (new ConstantDebugInformation (
|
|
|
|
constant.name,
|
|
|
|
(TypeReference) info.method.Module.LookupToken ((int) constant.token),
|
|
|
|
constant.value));
|
|
|
|
}
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
parent.scopes = ReadScopeAndLocals (scope.scopes, info);
|
|
|
|
|
|
|
|
return parent;
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
void ReadSequencePoints (PdbFunction function, MethodDebugInformation info)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
|
|
|
if (function.lines == null)
|
|
|
|
return;
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
info.sequence_points = new Collection<SequencePoint> ();
|
|
|
|
|
2014-08-13 10:39:27 +01:00
|
|
|
foreach (PdbLines lines in function.lines)
|
2016-11-10 13:04:39 +00:00
|
|
|
ReadLines (lines, info);
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
void ReadLines (PdbLines lines, MethodDebugInformation info)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
|
|
|
var document = GetDocument (lines.file);
|
|
|
|
|
|
|
|
foreach (var line in lines.lines)
|
2016-11-10 13:04:39 +00:00
|
|
|
ReadLine (line, document, info);
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
static void ReadLine (PdbLine line, Document document, MethodDebugInformation info)
|
2014-08-13 10:39:27 +01:00
|
|
|
{
|
2016-11-10 13:04:39 +00:00
|
|
|
var sequence_point = new SequencePoint ((int) line.offset, document);
|
2014-08-13 10:39:27 +01:00
|
|
|
sequence_point.StartLine = (int) line.lineBegin;
|
|
|
|
sequence_point.StartColumn = (int) line.colBegin;
|
|
|
|
sequence_point.EndLine = (int) line.lineEnd;
|
|
|
|
sequence_point.EndColumn = (int) line.colEnd;
|
|
|
|
|
2016-11-10 13:04:39 +00:00
|
|
|
info.sequence_points.Add (sequence_point);
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Document GetDocument (PdbSource source)
|
|
|
|
{
|
|
|
|
string name = source.name;
|
|
|
|
Document document;
|
|
|
|
if (documents.TryGetValue (name, out document))
|
|
|
|
return document;
|
|
|
|
|
|
|
|
document = new Document (name) {
|
|
|
|
Language = source.language.ToLanguage (),
|
|
|
|
LanguageVendor = source.vendor.ToVendor (),
|
|
|
|
Type = source.doctype.ToType (),
|
|
|
|
};
|
|
|
|
documents.Add (name, document);
|
|
|
|
return document;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose ()
|
|
|
|
{
|
2016-11-10 13:04:39 +00:00
|
|
|
pdb_file.Dispose ();
|
2014-08-13 10:39:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|