-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathSymUnmanagedExtensions.Reader.cs
More file actions
203 lines (169 loc) · 7.48 KB
/
Copy pathSymUnmanagedExtensions.Reader.cs
File metadata and controls
203 lines (169 loc) · 7.48 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the License.txt file in the project root for more information.
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace Microsoft.DiaSymReader
{
using static InteropUtilities;
partial class SymUnmanagedExtensions
{
// The name of the attribute containing the byte array of custom debug info.
// MSCUSTOMDEBUGINFO in Dev10.
private const string CdiAttributeName = "MD2";
public static void UpdateSymbolStore(this ISymUnmanagedReader reader, Stream stream, string? fileName = null)
{
if (reader == null)
{
throw new ArgumentNullException(nameof(reader));
}
ThrowExceptionForHR(reader.UpdateSymbolStore(fileName, SymUnmanagedStreamFactory.CreateStream(stream)));
}
public static void Initialize(this ISymUnmanagedReader3 reader, Stream stream, object metadataImporter, string? fileName = null, string? searchPath = null)
{
if (reader == null)
{
throw new ArgumentNullException(nameof(reader));
}
ThrowExceptionForHR(reader.Initialize(metadataImporter, fileName, searchPath, SymUnmanagedStreamFactory.CreateStream(stream)));
}
/// <summary>
/// Get the blob of binary custom debug info for a given method.
/// </summary>
public static byte[]? GetCustomDebugInfo(this ISymUnmanagedReader3 reader, int methodToken, int methodVersion)
{
if (reader == null)
{
throw new ArgumentNullException(nameof(reader));
}
return GetItems(
reader,
methodToken,
methodVersion,
(ISymUnmanagedReader3 pReader, int pMethodToken, int pMethodVersion, int pBufferLength, out int pCount, byte[] pCustomDebugInfo) =>
// Note: Here, we are assuming that the sym reader implementation we're using implements ISymUnmanagedReader3. This is
// necessary so that we get custom debug info for the correct method version in EnC scenarios. However, some sym reader
// implementations do not support this interface (for example, the mscordbi dynamic sym reader). If we need to fall back
// and call ISymUnmanagedReader.GetSymAttribute in those cases (assuming EnC is not supported), then we'll need to ensure
// that incorrect or missing custom debug info will not cause problems for any callers of this method.
pReader.GetSymAttributeByVersion(pMethodToken, pMethodVersion, CdiAttributeName, pBufferLength, out pCount, pCustomDebugInfo));
}
public static int GetUserEntryPoint(this ISymUnmanagedReader reader)
{
if (reader == null)
{
throw new ArgumentNullException(nameof(reader));
}
int entryPoint;
int hr = reader.GetUserEntryPoint(out entryPoint);
if (hr == HResult.E_FAIL)
{
// Not all assemblies have entry points
// dlls for example...
return 0;
}
ThrowExceptionForHR(hr);
return entryPoint;
}
public static ISymUnmanagedDocument GetDocument(this ISymUnmanagedReader reader, string name)
{
if (reader == null)
{
throw new ArgumentNullException(nameof(reader));
}
ISymUnmanagedDocument document;
ThrowExceptionForHR(reader.GetDocument(name, language: default, languageVendor: default, documentType: default, out document));
return document;
}
public static ISymUnmanagedDocument[] GetDocuments(this ISymUnmanagedReader reader)
{
if (reader == null)
{
throw new ArgumentNullException(nameof(reader));
}
return NullToEmpty(GetItems(reader,
(ISymUnmanagedReader a, int b, out int c, ISymUnmanagedDocument[] d) => a.GetDocuments(b, out c, d)));
}
public static ISymUnmanagedMethod[] GetMethodsInDocument(this ISymUnmanagedReader reader, ISymUnmanagedDocument symDocument)
{
if (reader == null)
{
throw new ArgumentNullException(nameof(reader));
}
return NullToEmpty(GetItems((ISymUnmanagedReader2)reader, symDocument,
(ISymUnmanagedReader2 a, ISymUnmanagedDocument b, int c, out int d, ISymUnmanagedMethod[] e) => a.GetMethodsInDocument(b, c, out d, e)));
}
public static ISymUnmanagedMethod? GetMethod(this ISymUnmanagedReader reader, int methodToken)
{
if (reader == null)
{
throw new ArgumentNullException(nameof(reader));
}
int hr = reader.GetMethod(methodToken, out var method);
ThrowExceptionForHR(hr);
if (hr < 0)
{
// method has no symbol info
return null;
}
if (method == null)
{
throw new InvalidOperationException();
}
return method;
}
public static ISymUnmanagedMethod? GetMethodByVersion(this ISymUnmanagedReader reader, int methodToken, int methodVersion)
{
if (reader == null)
{
throw new ArgumentNullException(nameof(reader));
}
ISymUnmanagedMethod method;
int hr = reader.GetMethodByVersion(methodToken, methodVersion, out method);
ThrowExceptionForHR(hr);
if (hr < 0)
{
// method has no symbol info
return null;
}
if (method == null)
{
throw new InvalidOperationException();
}
return method;
}
public static int GetMethodVersion(this ISymUnmanagedReader reader, ISymUnmanagedMethod method)
{
if (reader == null)
{
throw new ArgumentNullException(nameof(reader));
}
ThrowExceptionForHR(reader.GetMethodVersion(method, out var version));
return version;
}
/// <summary>
/// Returns compiler version number and name.
/// </summary>
public static bool TryGetCompilerInfo(this ISymUnmanagedCompilerInfoReader reader, out Version? version, out string? name)
{
if (reader == null)
{
throw new ArgumentNullException(nameof(reader));
}
ThrowExceptionForHR(reader.GetCompilerInfo(out var _, out var _, out var _, out var _, bufferLength: 0, out var bufferLength, name: null));
if (bufferLength == 0)
{
version = null;
name = null;
return false;
}
var nameBuffer = new char[bufferLength];
ThrowExceptionForHR(reader.GetCompilerInfo(out var major, out var minor, out var build, out var revision, bufferLength, out var actualLength, nameBuffer));
ValidateItems(actualLength, nameBuffer.Length);
name = BufferToString(nameBuffer);
version = new Version(major, minor, build, revision);
return true;
}
}
}