-
Notifications
You must be signed in to change notification settings - Fork 718
/
Copy pathPortableSymbolModule.cs
132 lines (110 loc) · 5.24 KB
/
PortableSymbolModule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
namespace Microsoft.Diagnostics.Symbols
{
internal class PortableSymbolModule : ManagedSymbolModule
{
public PortableSymbolModule(SymbolReader reader, string pdbFileName) : this(reader, File.Open(pdbFileName, FileMode.Open, FileAccess.Read, FileShare.Read), pdbFileName) { }
public PortableSymbolModule(SymbolReader reader, Stream stream, string pdbFileName = "") : base(reader, pdbFileName)
{
_stream = stream;
_provider = MetadataReaderProvider.FromPortablePdbStream(_stream);
_metaData = _provider.GetMetadataReader();
}
public override Guid PdbGuid
{
get
{
// The first 16 bytes are the PDB Guid, the next 4 are the DLL timestamp.
var idBytes = _metaData.DebugMetadataHeader.Id;
byte[] guidBytes = new byte[16];
idBytes.CopyTo(0, guidBytes, 0, guidBytes.Length);
return new Guid(guidBytes);
}
}
public override SourceLocation SourceLocationForManagedCode(uint methodMetadataToken, int ilOffset)
{
MethodDefinitionHandle methodDefinitionHandle = (MethodDefinitionHandle)MetadataTokens.Handle((int)methodMetadataToken);
MethodDebugInformationHandle methodDebugInfoHandle = methodDefinitionHandle.ToDebugInformationHandle();
MethodDebugInformation methodDebugInfo = _metaData.GetMethodDebugInformation(methodDebugInfoHandle);
int methodToken = MetadataTokens.GetToken(methodDebugInfoHandle.ToDefinitionHandle());
SequencePoint lastSequencePoint = default(SequencePoint);
foreach (SequencePoint sequencePoint in methodDebugInfo.GetSequencePoints())
{
if (sequencePoint.Offset > ilOffset)
{
if (lastSequencePoint.Document.IsNil)
{
lastSequencePoint = sequencePoint;
}
break;
}
lastSequencePoint = sequencePoint;
}
if (lastSequencePoint.Document.IsNil)
{
return null;
}
return new SourceLocation(GetSourceFile(lastSequencePoint.Document), lastSequencePoint.StartLine);
}
#region private
protected override string GetSourceLinkJson()
{
foreach (CustomDebugInformationHandle customDebugInformationHandle in _metaData.CustomDebugInformation)
{
CustomDebugInformation customDebugInformation = _metaData.GetCustomDebugInformation(customDebugInformationHandle);
EntityHandle parent = customDebugInformation.Parent;
Guid guid = _metaData.GetGuid(customDebugInformation.Kind);
if (guid == SourceLinkKind)
{
BlobReader blobReader = _metaData.GetBlobReader(customDebugInformation.Value);
var ret = blobReader.ReadUTF8(blobReader.Length);
return ret;
}
}
return null;
}
private SourceFile GetSourceFile(DocumentHandle documentHandle)
{
return new PortablePdbSourceFile(_metaData.GetDocument(documentHandle), this);
}
private class PortablePdbSourceFile : SourceFile
{
internal PortablePdbSourceFile(Document sourceFileDocument, PortableSymbolModule portablePdb) : base(portablePdb)
{
_sourceFileDocument = sourceFileDocument;
_portablePdb = portablePdb;
Guid hashAlgorithmGuid = _portablePdb._metaData.GetGuid(_sourceFileDocument.HashAlgorithm);
if (hashAlgorithmGuid == HashAlgorithmSha1)
{
_hashAlgorithm = System.Security.Cryptography.SHA1.Create();
}
else if (hashAlgorithmGuid == HashAlgorithmSha256)
{
_hashAlgorithm = System.Security.Cryptography.SHA256.Create();
}
if (_hashAlgorithm != null)
{
_hash = _portablePdb._metaData.GetBlobBytes(_sourceFileDocument.Hash);
}
BuildTimeFilePath = _portablePdb._metaData.GetString(_sourceFileDocument.Name);
_log.WriteLine("Opened Portable Pdb Source File: {0}", BuildTimeFilePath);
}
#region private
private static Guid HashAlgorithmSha1 = Guid.Parse("ff1816ec-aa5e-4d10-87f7-6f4963833460");
private static Guid HashAlgorithmSha256 = Guid.Parse("8829d00f-11b8-4213-878b-770e8597ac16");
// Fields
private Document _sourceFileDocument;
private PortableSymbolModule _portablePdb;
#endregion
} // Class PortablePdbSourceFile
private static Guid SourceLinkKind = Guid.Parse("CC110556-A091-4D38-9FEC-25AB9A351A6A");
// Needed by other things to look up data
internal MetadataReader _metaData;
private MetadataReaderProvider _provider;
private Stream _stream;
#endregion
}
}