Imported Upstream version 5.2.0.175

Former-commit-id: bb0468d0f257ff100aa895eb5fe583fb5dfbf900
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2017-06-07 13:16:24 +00:00
parent 4bdbaf4a88
commit 966bba02bb
8776 changed files with 346420 additions and 149650 deletions

View File

@@ -8,7 +8,6 @@
//
using System;
using System.Diagnostics.SymbolStore;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
@@ -29,8 +28,8 @@ namespace Mono.Cecil.Pdb {
[In] ref Guid languageVendor,
[In] ref Guid documentType,
[Out, MarshalAs (UnmanagedType.Interface)] out ISymUnmanagedDocumentWriter pRetVal);
void SetUserEntryPoint ([In] SymbolToken method);
void OpenMethod ([In] SymbolToken method);
void SetUserEntryPoint ([In] int methodToken);
void OpenMethod ([In] int methodToken);
void CloseMethod ();
void OpenScope ([In] int startOffset, [Out] out int pRetVal);
void CloseScope ([In] int endOffset);
@@ -40,7 +39,7 @@ namespace Mono.Cecil.Pdb {
void DefineField_Placeholder ();
void DefineGlobalVariable_Placeholder ();
void Close ();
void SetSymAttribute_Placeholder ();
void SetSymAttribute (uint parent, string name, uint data, IntPtr signature);
void OpenNamespace ([In, MarshalAs (UnmanagedType.LPWStr)] string name);
void CloseNamespace ();
void UsingNamespace ([In, MarshalAs (UnmanagedType.LPWStr)] string fullName);
@@ -71,13 +70,20 @@ namespace Mono.Cecil.Pdb {
void DefineLocalVariable2 (
[In, MarshalAs (UnmanagedType.LPWStr)] string name,
[In] int attributes,
[In] SymbolToken sigToken,
[In] int sigToken,
[In] int addrKind,
[In] int addr1,
[In] int addr2,
[In] int addr3,
[In] int startOffset,
[In] int endOffset);
void DefineGlobalVariable2_Placeholder ();
void DefineConstant2 (
[In, MarshalAs (UnmanagedType.LPWStr)] string name,
[In, MarshalAs (UnmanagedType.Struct)] object variant,
[In] int sigToken);
}
}

View File

@@ -0,0 +1,352 @@
//
// 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.Collections.Generic;
using Microsoft.Cci.Pdb;
using Mono.Cecil.Cil;
namespace Mono.Cecil.Pdb {
public class NativePdbReader : ISymbolReader {
int age;
Guid guid;
readonly Disposable<Stream> pdb_file;
readonly Dictionary<string, Document> documents = new Dictionary<string, Document> ();
readonly Dictionary<uint, PdbFunction> functions = new Dictionary<uint, PdbFunction> ();
readonly Dictionary<PdbScope, ImportDebugInformation> imports = new Dictionary<PdbScope, ImportDebugInformation> ();
internal NativePdbReader (Disposable<Stream> file)
{
this.pdb_file = file;
}
public ISymbolWriterProvider GetWriterProvider ()
{
return new NativePdbWriterProvider ();
}
/*
uint Magic = 0x53445352;
Guid Signature;
uint Age;
string FileName;
*/
public bool ProcessDebugHeader (ImageDebugHeader header)
{
if (!header.HasEntries)
return false;
var entry = header.GetCodeViewEntry ();
if (entry == null)
return false;
var directory = entry.Directory;
if (directory.Type != ImageDebugType.CodeView)
return false;
if (directory.MajorVersion != 0 || directory.MinorVersion != 0)
return false;
var data = entry.Data;
if (data.Length < 24)
return false;
var magic = ReadInt32 (data, 0);
if (magic != 0x53445352)
return false;
var guid_bytes = new byte [16];
Buffer.BlockCopy (data, 4, guid_bytes, 0, 16);
this.guid = new Guid (guid_bytes);
this.age = ReadInt32 (data, 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;
var funcs = PdbFile.LoadFunctions (pdb_file.value, out tokenToSourceMapping, out sourceServerData, out age, out guid);
if (this.guid != guid)
return false;
foreach (PdbFunction function in funcs)
functions.Add (function.token, function);
}
return true;
}
public MethodDebugInformation Read (MethodDefinition method)
{
var method_token = method.MetadataToken;
PdbFunction function;
if (!functions.TryGetValue (method_token.ToUInt32 (), out function))
return null;
var symbol = new MethodDebugInformation (method);
ReadSequencePoints (function, symbol);
symbol.scope = !function.scopes.IsNullOrEmpty ()
? ReadScopeAndLocals (function.scopes [0], symbol)
: new ScopeDebugInformation { Start = new InstructionOffset (0), End = new InstructionOffset ((int) function.length) };
if (function.tokenOfMethodWhoseUsingInfoAppliesToThisMethod != method.MetadataToken.ToUInt32 () && function.tokenOfMethodWhoseUsingInfoAppliesToThisMethod != 0)
symbol.scope.import = GetImport (function.tokenOfMethodWhoseUsingInfoAppliesToThisMethod, method.Module);
if (function.scopes.Length > 1) {
for (int i = 1; i < function.scopes.Length; i++) {
var s = ReadScopeAndLocals (function.scopes [i], symbol);
if (!AddScope (symbol.scope.Scopes, s))
symbol.scope.Scopes.Add (s);
}
}
if (function.iteratorScopes != null)
foreach (var iterator_scope in function.iteratorScopes)
symbol.CustomDebugInformations.Add (new StateMachineScopeDebugInformation ((int) iterator_scope.Offset, (int) (iterator_scope.Offset + iterator_scope.Length + 1)));
if (function.synchronizationInformation != null) {
var async_debug_info = new AsyncMethodBodyDebugInformation ((int) function.synchronizationInformation.GeneratedCatchHandlerOffset);
foreach (var synchronization_point in function.synchronizationInformation.synchronizationPoints) {
async_debug_info.Yields.Add (new InstructionOffset ((int) synchronization_point.SynchronizeOffset));
async_debug_info.Resumes.Add (new InstructionOffset ((int) synchronization_point.ContinuationOffset));
}
symbol.CustomDebugInformations.Add (async_debug_info);
async_debug_info.MoveNextMethod = method;
symbol.StateMachineKickOffMethod = (MethodDefinition) method.Module.LookupToken ((int) function.synchronizationInformation.kickoffMethodToken);
}
return symbol;
}
Collection<ScopeDebugInformation> ReadScopeAndLocals (PdbScope [] scopes, MethodDebugInformation info)
{
var symbols = new Collection<ScopeDebugInformation> (scopes.Length);
foreach (PdbScope scope in scopes)
if (scope != null)
symbols.Add (ReadScopeAndLocals (scope, info));
return symbols;
}
ScopeDebugInformation ReadScopeAndLocals (PdbScope scope, MethodDebugInformation info)
{
var parent = new ScopeDebugInformation ();
parent.Start = new InstructionOffset ((int) scope.offset);
parent.End = new InstructionOffset ((int) (scope.offset + scope.length));
if (!scope.slots.IsNullOrEmpty ()) {
parent.variables = new Collection<VariableDebugInformation> (scope.slots.Length);
foreach (PdbSlot slot in scope.slots) {
if (slot.flags == 1) // parameter names
continue;
var index = (int) slot.slot;
var variable = new VariableDebugInformation (index, slot.name);
if (slot.flags == 4)
variable.IsDebuggerHidden = true;
parent.variables.Add (variable);
}
}
if (!scope.constants.IsNullOrEmpty ()) {
parent.constants = new Collection<ConstantDebugInformation> (scope.constants.Length);
foreach (var constant in scope.constants) {
var type = info.Method.Module.Read (constant, (c, r) => r.ReadConstantSignature (new MetadataToken (c.token)));
var value = constant.value;
// Object "null" is encoded as integer
if (type != null && !type.IsValueType && value is int && (int) value == 0)
value = null;
parent.constants.Add (new ConstantDebugInformation (constant.name, type, value));
}
}
if (!scope.usedNamespaces.IsNullOrEmpty ()) {
ImportDebugInformation import;
if (imports.TryGetValue (scope, out import)) {
parent.import = import;
} else {
import = GetImport (scope, info.Method.Module);
imports.Add (scope, import);
}
}
parent.scopes = ReadScopeAndLocals (scope.scopes, info);
return parent;
}
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;
}
ImportDebugInformation GetImport (uint token, ModuleDefinition module)
{
PdbFunction function;
if (!functions.TryGetValue (token, out function))
return null;
if (function.scopes.Length != 1)
return null;
var scope = function.scopes [0];
ImportDebugInformation import;
if (imports.TryGetValue (scope, out import))
return import;
import = GetImport (scope, module);
imports.Add (scope, import);
return import;
}
static ImportDebugInformation GetImport (PdbScope scope, ModuleDefinition module)
{
if (scope.usedNamespaces.IsNullOrEmpty ())
return null;
var import = new ImportDebugInformation ();
foreach (var used_namespace in scope.usedNamespaces) {
ImportTarget target = null;
var value = used_namespace.Substring (1);
switch (used_namespace [0]) {
case 'U':
target = new ImportTarget (ImportTargetKind.ImportNamespace) { @namespace = value };
break;
case 'T': {
var type = module.GetType (value, runtimeName: true);
if (type != null)
target = new ImportTarget (ImportTargetKind.ImportType) { type = type };
break;
}
case 'A':
var index = used_namespace.IndexOf(' ');
var alias_value = used_namespace.Substring (1, index - 1);
var alias_target_value = used_namespace.Substring (index + 2);
switch (used_namespace [index + 1]) {
case 'U':
target = new ImportTarget (ImportTargetKind.DefineNamespaceAlias) { alias = alias_value, @namespace = alias_target_value };
break;
case 'T':
var type = module.GetType (alias_target_value, runtimeName: true);
if (type != null)
target = new ImportTarget (ImportTargetKind.DefineTypeAlias) { alias = alias_value, type = type };
break;
}
break;
}
if (target != null)
import.Targets.Add (target);
}
return import;
}
void ReadSequencePoints (PdbFunction function, MethodDebugInformation info)
{
if (function.lines == null)
return;
info.sequence_points = new Collection<SequencePoint> ();
foreach (PdbLines lines in function.lines)
ReadLines (lines, info);
}
void ReadLines (PdbLines lines, MethodDebugInformation info)
{
var document = GetDocument (lines.file);
foreach (var line in lines.lines)
ReadLine (line, document, info);
}
static void ReadLine (PdbLine line, Document document, MethodDebugInformation info)
{
var sequence_point = new SequencePoint ((int) line.offset, document);
sequence_point.StartLine = (int) line.lineBegin;
sequence_point.StartColumn = (int) line.colBegin;
sequence_point.EndLine = (int) line.lineEnd;
sequence_point.EndColumn = (int) line.colEnd;
info.sequence_points.Add (sequence_point);
}
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 ()
{
pdb_file.Dispose ();
}
}
}

View File

@@ -0,0 +1,370 @@
//
// 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 System.Linq;
using System.Text;
using Mono.Cecil.Cil;
using Mono.Cecil.PE;
using Mono.Collections.Generic;
#if !READ_ONLY
namespace Mono.Cecil.Pdb {
public class NativePdbWriter : ISymbolWriter, IMetadataSymbolWriter {
readonly ModuleDefinition module;
readonly SymWriter writer;
readonly Dictionary<string, SymDocumentWriter> documents;
readonly Dictionary<ImportDebugInformation, MetadataToken> import_info_to_parent;
MetadataBuilder metadata;
internal NativePdbWriter (ModuleDefinition module, SymWriter writer)
{
this.module = module;
this.writer = writer;
this.documents = new Dictionary<string, SymDocumentWriter> ();
this.import_info_to_parent = new Dictionary<ImportDebugInformation, MetadataToken> ();
}
public ISymbolReaderProvider GetReaderProvider ()
{
return new NativePdbReaderProvider ();
}
public ImageDebugHeader GetDebugHeader ()
{
ImageDebugDirectory directory;
var data = writer.GetDebugInfo (out directory);
directory.TimeDateStamp = (int) module.timestamp;
return new ImageDebugHeader (new ImageDebugHeaderEntry (directory, data));
}
public void Write (MethodDebugInformation info)
{
var method_token = info.method.MetadataToken;
var sym_token = method_token.ToInt32 ();
if (!info.HasSequencePoints && info.scope == null && !info.HasCustomDebugInformations && info.StateMachineKickOffMethod == null)
return;
writer.OpenMethod (sym_token);
if (!info.sequence_points.IsNullOrEmpty ())
DefineSequencePoints (info.sequence_points);
var import_parent = new MetadataToken ();
if (info.scope != null)
DefineScope (info.scope, info, out import_parent);
DefineCustomMetadata (info, import_parent);
writer.CloseMethod ();
}
void IMetadataSymbolWriter.SetMetadata (MetadataBuilder metadata)
{
this.metadata = metadata;
}
void DefineCustomMetadata (MethodDebugInformation info, MetadataToken import_parent)
{
var metadata = new CustomMetadataWriter (this.writer);
if (import_parent.RID != 0) {
metadata.WriteForwardInfo (import_parent);
} else if (info.scope != null && info.scope.Import != null && info.scope.Import.HasTargets) {
metadata.WriteUsingInfo (info.scope.Import);
}
if (info.Method.HasCustomAttributes) {
foreach (var attribute in info.Method.CustomAttributes) {
const string compiler_services = "System.Runtime.CompilerServices";
var attribute_type = attribute.AttributeType;
if (!attribute_type.IsTypeOf (compiler_services, "IteratorStateMachineAttribute") && !attribute_type.IsTypeOf (compiler_services, "AsyncStateMachineAttribute"))
continue;
var type = attribute.ConstructorArguments [0].Value as TypeReference;
if (type == null)
continue;
metadata.WriteForwardIterator (type);
}
}
if (info.HasCustomDebugInformations) {
var scopes = info.CustomDebugInformations.OfType<StateMachineScopeDebugInformation> ().ToArray ();
if (scopes.Length > 0)
metadata.WriteIteratorScopes (scopes, info);
}
metadata.WriteCustomMetadata ();
DefineAsyncCustomMetadata (info);
}
void DefineAsyncCustomMetadata (MethodDebugInformation info)
{
if (!info.HasCustomDebugInformations)
return;
foreach (var custom_info in info.CustomDebugInformations) {
var async_debug_info = custom_info as AsyncMethodBodyDebugInformation;
if (async_debug_info == null)
continue;
using (var stream = new MemoryStream ()) {
var async_metadata = new BinaryStreamWriter (stream);
async_metadata.WriteUInt32 (info.StateMachineKickOffMethod != null ? info.StateMachineKickOffMethod.MetadataToken.ToUInt32 () : 0);
async_metadata.WriteUInt32 ((uint) async_debug_info.CatchHandler.Offset);
async_metadata.WriteUInt32 ((uint) async_debug_info.Resumes.Count);
for (int i = 0; i < async_debug_info.Resumes.Count; ++i) {
async_metadata.WriteUInt32 ((uint) async_debug_info.Yields [i].Offset);
async_metadata.WriteUInt32 (async_debug_info.MoveNextMethod != null ? async_debug_info.MoveNextMethod.MetadataToken.ToUInt32 () : 0);
async_metadata.WriteUInt32 ((uint) async_debug_info.Resumes [i].Offset);
}
writer.DefineCustomMetadata ("asyncMethodInfo", stream.ToArray ());
}
}
}
void DefineScope (ScopeDebugInformation scope, MethodDebugInformation info, out MetadataToken import_parent)
{
var start_offset = scope.Start.Offset;
var end_offset = scope.End.IsEndOfMethod
? info.code_size
: scope.End.Offset;
import_parent = new MetadataToken (0u);
writer.OpenScope (start_offset);
if (scope.Import != null && scope.Import.HasTargets && !import_info_to_parent.TryGetValue (info.scope.Import, out import_parent)) {
foreach (var target in scope.Import.Targets) {
switch (target.Kind) {
case ImportTargetKind.ImportNamespace:
writer.UsingNamespace ("U" + target.@namespace);
break;
case ImportTargetKind.ImportType:
writer.UsingNamespace ("T" + TypeParser.ToParseable (target.type));
break;
case ImportTargetKind.DefineNamespaceAlias:
writer.UsingNamespace ("A" + target.Alias + " U" + target.@namespace);
break;
case ImportTargetKind.DefineTypeAlias:
writer.UsingNamespace ("A" + target.Alias + " T" + TypeParser.ToParseable (target.type));
break;
}
}
import_info_to_parent.Add (info.scope.Import, info.method.MetadataToken);
}
var sym_token = info.local_var_token.ToInt32 ();
if (!scope.variables.IsNullOrEmpty ()) {
for (int i = 0; i < scope.variables.Count; i++) {
var variable = scope.variables [i];
DefineLocalVariable (variable, sym_token, start_offset, end_offset);
}
}
if (!scope.constants.IsNullOrEmpty ()) {
for (int i = 0; i < scope.constants.Count; i++) {
var constant = scope.constants [i];
DefineConstant (constant);
}
}
if (!scope.scopes.IsNullOrEmpty ()) {
for (int i = 0; i < scope.scopes.Count; i++) {
MetadataToken _;
DefineScope (scope.scopes [i], info, out _);
}
}
writer.CloseScope (end_offset);
}
void DefineSequencePoints (Collection<SequencePoint> sequence_points)
{
for (int i = 0; i < sequence_points.Count; i++) {
var sequence_point = sequence_points [i];
writer.DefineSequencePoints (
GetDocument (sequence_point.Document),
new [] { sequence_point.Offset },
new [] { sequence_point.StartLine },
new [] { sequence_point.StartColumn },
new [] { sequence_point.EndLine },
new [] { sequence_point.EndColumn });
}
}
void DefineLocalVariable (VariableDebugInformation variable, int local_var_token, int start_offset, int end_offset)
{
writer.DefineLocalVariable2 (
variable.Name,
variable.Attributes,
local_var_token,
variable.Index,
0,
0,
start_offset,
end_offset);
}
void DefineConstant (ConstantDebugInformation constant)
{
var row = metadata.AddStandAloneSignature (metadata.GetConstantTypeBlobIndex (constant.ConstantType));
var token = new MetadataToken (TokenType.Signature, row);
writer.DefineConstant2 (constant.Name, constant.Value, token.ToInt32 ());
}
SymDocumentWriter GetDocument (Document document)
{
if (document == null)
return null;
SymDocumentWriter doc_writer;
if (documents.TryGetValue (document.Url, out doc_writer))
return doc_writer;
doc_writer = writer.DefineDocument (
document.Url,
document.Language.ToGuid (),
document.LanguageVendor.ToGuid (),
document.Type.ToGuid ());
documents [document.Url] = doc_writer;
return doc_writer;
}
public void Dispose ()
{
var entry_point = module.EntryPoint;
if (entry_point != null)
writer.SetUserEntryPoint (entry_point.MetadataToken.ToInt32 ());
writer.Close ();
}
}
enum CustomMetadataType : byte {
UsingInfo = 0,
ForwardInfo = 1,
IteratorScopes = 3,
ForwardIterator = 4,
}
class CustomMetadataWriter : IDisposable {
readonly SymWriter sym_writer;
readonly MemoryStream stream;
readonly BinaryStreamWriter writer;
int count;
const byte version = 4;
public CustomMetadataWriter (SymWriter sym_writer)
{
this.sym_writer = sym_writer;
this.stream = new MemoryStream ();
this.writer = new BinaryStreamWriter (stream);
writer.WriteByte (version);
writer.WriteByte (0); // count
writer.Align (4);
}
public void WriteUsingInfo (ImportDebugInformation import_info)
{
Write (CustomMetadataType.UsingInfo, () => {
writer.WriteUInt16 ((ushort) 1);
writer.WriteUInt16 ((ushort) import_info.Targets.Count);
});
}
public void WriteForwardInfo (MetadataToken import_parent)
{
Write (CustomMetadataType.ForwardInfo, () => writer.WriteUInt32 (import_parent.ToUInt32 ()));
}
public void WriteIteratorScopes (StateMachineScopeDebugInformation [] scopes, MethodDebugInformation debug_info)
{
Write (CustomMetadataType.IteratorScopes, () => {
writer.WriteInt32 (scopes.Length);
foreach (var scope in scopes) {
var start = scope.Start.Offset;
var end = scope.End.IsEndOfMethod ? debug_info.code_size : scope.End.Offset;
writer.WriteInt32 (start);
writer.WriteInt32 (end - 1);
}
});
}
public void WriteForwardIterator (TypeReference type)
{
Write (CustomMetadataType.ForwardIterator, () => writer.WriteBytes(Encoding.Unicode.GetBytes(type.Name)));
}
void Write (CustomMetadataType type, Action write)
{
count++;
writer.WriteByte (version);
writer.WriteByte ((byte) type);
writer.Align (4);
var length_position = writer.Position;
writer.WriteUInt32 (0);
write ();
writer.Align (4);
var end = writer.Position;
var length = end - length_position + 4; // header is 4 bytes long
writer.Position = length_position;
writer.WriteInt32 (length);
writer.Position = end;
}
public void WriteCustomMetadata ()
{
if (count == 0)
return;
writer.BaseStream.Position = 1;
writer.WriteByte ((byte) count);
writer.Flush ();
sym_writer.DefineCustomMetadata ("MD2", stream.ToArray ());
}
public void Dispose ()
{
stream.Dispose ();
}
}
}
#endif

View File

@@ -22,7 +22,7 @@ namespace Mono.Cecil.Pdb {
Mixin.CheckModule (module);
Mixin.CheckFileName (fileName);
return new PdbReader (Disposable.Owned (File.OpenRead (Mixin.GetPdbFileName (fileName)) as Stream));
return new NativePdbReader (Disposable.Owned (File.OpenRead (Mixin.GetPdbFileName (fileName)) as Stream));
}
public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream)
@@ -30,7 +30,7 @@ namespace Mono.Cecil.Pdb {
Mixin.CheckModule (module);
Mixin.CheckStream (symbolStream);
return new PdbReader (Disposable.NotOwned (symbolStream));
return new NativePdbReader (Disposable.NotOwned (symbolStream));
}
}
@@ -41,7 +41,14 @@ namespace Mono.Cecil.Pdb {
Mixin.CheckModule (module);
Mixin.CheckFileName (fileName);
return IsPortablePdb (Mixin.GetPdbFileName (fileName))
if (module.HasDebugHeader) {
var header = module.GetDebugHeader ();
var entry = header.GetEmbeddedPortablePdbEntry ();
if (entry != null)
return new EmbeddedPortablePdbReaderProvider ().GetSymbolReader (module, fileName);
}
return Mixin.IsPortablePdb (Mixin.GetPdbFileName (fileName))
? new PortablePdbReaderProvider ().GetSymbolReader (module, fileName)
: new NativePdbReaderProvider ().GetSymbolReader (module, fileName);
}
@@ -52,29 +59,10 @@ namespace Mono.Cecil.Pdb {
Mixin.CheckStream (symbolStream);
Mixin.CheckReadSeek (symbolStream);
return IsPortablePdb (symbolStream)
return Mixin.IsPortablePdb (symbolStream)
? new PortablePdbReaderProvider ().GetSymbolReader (module, symbolStream)
: new NativePdbReaderProvider ().GetSymbolReader (module, symbolStream);
}
static bool IsPortablePdb (string fileName)
{
using (var file = new FileStream (fileName, FileMode.Open, FileAccess.Read, FileShare.None))
return IsPortablePdb (file);
}
static bool IsPortablePdb (Stream stream)
{
const uint ppdb_signature = 0x424a5342;
var position = stream.Position;
try {
var reader = new BinaryReader (stream);
return reader.ReadUInt32 () == ppdb_signature;
} finally {
stream.Position = position;
}
}
}
#if !READ_ONLY
@@ -86,7 +74,7 @@ namespace Mono.Cecil.Pdb {
Mixin.CheckModule (module);
Mixin.CheckFileName (fileName);
return new PdbWriter (module, CreateWriter (module, Mixin.GetPdbFileName (fileName)));
return new NativePdbWriter (module, CreateWriter (module, Mixin.GetPdbFileName (fileName)));
}
static SymWriter CreateWriter (ModuleDefinition module, string pdb)

View File

@@ -1,211 +0,0 @@
//
// 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.Collections.Generic;
using Microsoft.Cci.Pdb;
using Mono.Cecil.Cil;
namespace Mono.Cecil.Pdb {
public class PdbReader : ISymbolReader {
int age;
Guid guid;
readonly Disposable<Stream> pdb_file;
readonly Dictionary<string, Document> documents = new Dictionary<string, Document> ();
readonly Dictionary<uint, PdbFunction> functions = new Dictionary<uint, PdbFunction> ();
internal PdbReader (Disposable<Stream> file)
{
this.pdb_file = file;
}
/*
uint Magic = 0x53445352;
Guid Signature;
uint Age;
string FileName;
*/
public bool ProcessDebugHeader (ImageDebugDirectory directory, byte [] header)
{
if (directory.Type != 2) //IMAGE_DEBUG_TYPE_CODEVIEW
return false;
if (directory.MajorVersion != 0 || directory.MinorVersion != 0)
return false;
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;
var funcs = PdbFile.LoadFunctions (pdb_file.value, out tokenToSourceMapping, out sourceServerData, out age, out guid);
if (this.guid != guid)
return false;
foreach (PdbFunction function in funcs)
functions.Add (function.token, function);
}
return true;
}
public MethodDebugInformation Read (MethodDefinition method)
{
var method_token = method.MetadataToken;
PdbFunction function;
if (!functions.TryGetValue (method_token.ToUInt32 (), out function))
return null;
var symbol = new MethodDebugInformation (method);
ReadSequencePoints (function, symbol);
if (function.scopes.Length > 1)
throw new NotSupportedException ();
else if (function.scopes.Length == 1)
symbol.scope = ReadScopeAndLocals (function.scopes [0], symbol);
return symbol;
}
static Collection<ScopeDebugInformation> ReadScopeAndLocals (PdbScope [] scopes, MethodDebugInformation info)
{
var symbols = new Collection<ScopeDebugInformation> (scopes.Length);
foreach (PdbScope scope in scopes)
if (scope != null)
symbols.Add (ReadScopeAndLocals (scope, info));
return symbols;
}
static ScopeDebugInformation ReadScopeAndLocals (PdbScope scope, MethodDebugInformation info)
{
var parent = new ScopeDebugInformation ();
parent.Start = new InstructionOffset ((int) scope.offset);
parent.End = new InstructionOffset ((int) (scope.offset + scope.length));
if (!scope.slots.IsNullOrEmpty()) {
parent.variables = new Collection<VariableDebugInformation> (scope.slots.Length);
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);
}
}
if (!scope.constants.IsNullOrEmpty ()) {
parent.constants = new Collection<ConstantDebugInformation> (scope.constants.Length);
foreach (var constant in scope.constants) {
parent.constants.Add (new ConstantDebugInformation (
constant.name,
(TypeReference) info.method.Module.LookupToken ((int) constant.token),
constant.value));
}
}
parent.scopes = ReadScopeAndLocals (scope.scopes, info);
return parent;
}
void ReadSequencePoints (PdbFunction function, MethodDebugInformation info)
{
if (function.lines == null)
return;
info.sequence_points = new Collection<SequencePoint> ();
foreach (PdbLines lines in function.lines)
ReadLines (lines, info);
}
void ReadLines (PdbLines lines, MethodDebugInformation info)
{
var document = GetDocument (lines.file);
foreach (var line in lines.lines)
ReadLine (line, document, info);
}
static void ReadLine (PdbLine line, Document document, MethodDebugInformation info)
{
var sequence_point = new SequencePoint ((int) line.offset, document);
sequence_point.StartLine = (int) line.lineBegin;
sequence_point.StartColumn = (int) line.colBegin;
sequence_point.EndLine = (int) line.lineEnd;
sequence_point.EndColumn = (int) line.colEnd;
info.sequence_points.Add (sequence_point);
}
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 ()
{
pdb_file.Dispose ();
}
}
}

View File

@@ -1,141 +0,0 @@
//
// 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.Diagnostics.SymbolStore;
using Mono.Cecil.Cil;
using Mono.Collections.Generic;
#if !READ_ONLY
namespace Mono.Cecil.Pdb {
public class PdbWriter : Cil.ISymbolWriter {
readonly ModuleDefinition module;
readonly SymWriter writer;
readonly Dictionary<string, SymDocumentWriter> documents;
internal PdbWriter (ModuleDefinition module, SymWriter writer)
{
this.module = module;
this.writer = writer;
this.documents = new Dictionary<string, SymDocumentWriter> ();
}
public bool GetDebugHeader (out ImageDebugDirectory directory, out byte [] header)
{
header = writer.GetDebugInfo (out directory);
return true;
}
public void Write (MethodDebugInformation info)
{
var method_token = info.method.MetadataToken;
var sym_token = new SymbolToken (method_token.ToInt32 ());
writer.OpenMethod (sym_token);
DefineSequencePoints (info.sequence_points);
if (info.scope != null)
DefineScope (info.scope, info);
writer.CloseMethod ();
}
void DefineScope (ScopeDebugInformation scope, MethodDebugInformation info)
{
var start_offset = scope.Start.Offset;
var end_offset = scope.End.IsEndOfMethod
? info.code_size
: scope.End.Offset;
writer.OpenScope (start_offset);
var sym_token = new SymbolToken (info.local_var_token.ToInt32 ());
if (!scope.variables.IsNullOrEmpty ()) {
for (int i = 0; i < scope.variables.Count; i++) {
var variable = scope.variables [i];
CreateLocalVariable (variable, sym_token, start_offset, end_offset);
}
}
if (!scope.scopes.IsNullOrEmpty ()) {
for (int i = 0; i < scope.scopes.Count; i++)
DefineScope (scope.scopes [i], info);
}
writer.CloseScope (end_offset);
}
void DefineSequencePoints (Collection<SequencePoint> sequence_points)
{
for (int i = 0; i < sequence_points.Count; i++) {
var sequence_point = sequence_points [i];
writer.DefineSequencePoints (
GetDocument (sequence_point.Document),
new [] { sequence_point.Offset },
new [] { sequence_point.StartLine },
new [] { sequence_point.StartColumn },
new [] { sequence_point.EndLine },
new [] { sequence_point.EndColumn });
}
}
void CreateLocalVariable (VariableDebugInformation variable, SymbolToken local_var_token, int start_offset, int end_offset)
{
writer.DefineLocalVariable2 (
variable.Name,
variable.Attributes,
local_var_token,
SymAddressKind.ILOffset,
variable.Index,
0,
0,
start_offset,
end_offset);
}
SymDocumentWriter GetDocument (Document document)
{
if (document == null)
return null;
SymDocumentWriter doc_writer;
if (documents.TryGetValue (document.Url, out doc_writer))
return doc_writer;
doc_writer = writer.DefineDocument (
document.Url,
document.Language.ToGuid (),
document.LanguageVendor.ToGuid (),
document.Type.ToGuid ());
documents [document.Url] = doc_writer;
return doc_writer;
}
public void Dispose ()
{
var entry_point = module.EntryPoint;
if (entry_point != null)
writer.SetUserEntryPoint (new SymbolToken (entry_point.MetadataToken.ToInt32 ()));
writer.Close ();
}
}
}
#endif

View File

@@ -8,8 +8,6 @@
//
using System;
using System.Collections.Generic;
using System.Diagnostics.SymbolStore;
using System.Runtime.InteropServices;
using Mono.Cecil.Cil;
@@ -60,15 +58,24 @@ namespace Mono.Cecil.Pdb
public void DefineLocalVariable2 (
string name,
VariableAttributes attributes,
SymbolToken sigToken,
SymAddressKind addrKind,
int sigToken,
int addr1,
int addr2,
int addr3,
int startOffset,
int endOffset)
{
m_writer.DefineLocalVariable2 (name, (int)attributes, sigToken, (int)addrKind, addr1, addr2, addr3, startOffset, endOffset);
m_writer.DefineLocalVariable2 (name, (int)attributes, sigToken, 1 /* ILOffset*/, addr1, addr2, addr3, startOffset, endOffset);
}
public void DefineConstant2 (string name, object value, int sigToken)
{
if (value == null) {
m_writer.DefineConstant2 (name, 0, sigToken);
return;
}
m_writer.DefineConstant2 (name, value, sigToken);
}
public void Close ()
@@ -104,11 +111,6 @@ namespace Mono.Cecil.Pdb
return new SymDocumentWriter (unmanagedDocumentWriter);
}
public void DefineParameter (string name, ParameterAttributes attributes, int sequence, SymAddressKind addrKind, int addr1, int addr2, int addr3)
{
throw new Exception ("The method or operation is not implemented.");
}
public void DefineSequencePoints (SymDocumentWriter document, int[] offsets, int[] lines, int[] columns, int[] endLines, int[] endColumns)
{
m_writer.DefineSequencePoints (document.GetUnmanaged(), offsets.Length, offsets, lines, columns, endLines, endColumns);
@@ -119,14 +121,14 @@ namespace Mono.Cecil.Pdb
m_writer.Initialize (emitter, filename, null, fFullBuild);
}
public void SetUserEntryPoint (SymbolToken method)
public void SetUserEntryPoint (int methodToken)
{
m_writer.SetUserEntryPoint (method);
m_writer.SetUserEntryPoint (methodToken);
}
public void OpenMethod (SymbolToken method)
public void OpenMethod (int methodToken)
{
m_writer.OpenMethod (method);
m_writer.OpenMethod (methodToken);
}
public void OpenNamespace (string name)
@@ -145,6 +147,13 @@ namespace Mono.Cecil.Pdb
{
m_writer.UsingNamespace (fullName);
}
public void DefineCustomMetadata (string name, byte [] metadata)
{
var handle = GCHandle.Alloc (metadata, GCHandleType.Pinned);
m_writer.SetSymAttribute (0, name, (uint) metadata.Length, handle.AddrOfPinnedObject ());
handle.Free ();
}
}
}