-
Notifications
You must be signed in to change notification settings - Fork 4k
/
EditAndContinueMethodDebugInformation.cs
369 lines (302 loc) · 15.1 KB
/
EditAndContinueMethodDebugInformation.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// 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 file in the project root for more information.
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using Microsoft.CodeAnalysis.CodeGen;
using Microsoft.CodeAnalysis.PooledObjects;
namespace Microsoft.CodeAnalysis.Emit
{
/// <summary>
/// Debugging information associated with the specified method that is emitted by the compiler to support Edit and Continue.
/// </summary>
public readonly struct EditAndContinueMethodDebugInformation
{
internal readonly int MethodOrdinal;
internal readonly ImmutableArray<LocalSlotDebugInfo> LocalSlots;
internal readonly ImmutableArray<LambdaDebugInfo> Lambdas;
internal readonly ImmutableArray<ClosureDebugInfo> Closures;
internal readonly ImmutableArray<StateMachineStateDebugInfo> StateMachineStates;
internal EditAndContinueMethodDebugInformation(
int methodOrdinal,
ImmutableArray<LocalSlotDebugInfo> localSlots,
ImmutableArray<ClosureDebugInfo> closures,
ImmutableArray<LambdaDebugInfo> lambdas,
ImmutableArray<StateMachineStateDebugInfo> stateMachineStates)
{
Debug.Assert(methodOrdinal >= -1);
MethodOrdinal = methodOrdinal;
LocalSlots = localSlots;
Lambdas = lambdas;
Closures = closures;
StateMachineStates = stateMachineStates;
}
/// <summary>
/// Deserializes Edit and Continue method debug information from specified blobs.
/// </summary>
/// <param name="compressedSlotMap">Local variable slot map.</param>
/// <param name="compressedLambdaMap">Lambda and closure map.</param>
/// <exception cref="InvalidDataException">Invalid data.</exception>
public static EditAndContinueMethodDebugInformation Create(ImmutableArray<byte> compressedSlotMap, ImmutableArray<byte> compressedLambdaMap)
=> Create(compressedSlotMap, compressedLambdaMap, compressedStateMachineStateMap: default);
/// <summary>
/// Deserializes Edit and Continue method debug information from specified blobs.
/// </summary>
/// <param name="compressedSlotMap">Local variable slot map.</param>
/// <param name="compressedLambdaMap">Lambda and closure map.</param>
/// <param name="compressedStateMachineStateMap">State machine suspension points, if the method is the MoveNext method of the state machine.</param>
/// <exception cref="InvalidDataException">Invalid data.</exception>
public static EditAndContinueMethodDebugInformation Create(ImmutableArray<byte> compressedSlotMap, ImmutableArray<byte> compressedLambdaMap, ImmutableArray<byte> compressedStateMachineStateMap)
{
UncompressLambdaMap(compressedLambdaMap, out var methodOrdinal, out var closures, out var lambdas);
return new EditAndContinueMethodDebugInformation(
methodOrdinal,
UncompressSlotMap(compressedSlotMap),
closures,
lambdas,
UncompressStateMachineStates(compressedStateMachineStateMap));
}
private static InvalidDataException CreateInvalidDataException(ImmutableArray<byte> data, int offset)
{
const int maxReportedLength = 1024;
int start = Math.Max(0, offset - maxReportedLength / 2);
int end = Math.Min(data.Length, offset + maxReportedLength / 2);
byte[] left = new byte[offset - start];
data.CopyTo(start, left, 0, left.Length);
byte[] right = new byte[end - offset];
data.CopyTo(offset, right, 0, right.Length);
throw new InvalidDataException(string.Format(CodeAnalysisResources.InvalidDataAtOffset,
offset, (start != 0) ? "..." : "", BitConverter.ToString(left), BitConverter.ToString(right), (end != data.Length) ? "..." : ""));
}
#region Local Slots
private const byte SyntaxOffsetBaseline = 0xff;
/// <exception cref="InvalidDataException">Invalid data.</exception>
private static unsafe ImmutableArray<LocalSlotDebugInfo> UncompressSlotMap(ImmutableArray<byte> compressedSlotMap)
{
if (compressedSlotMap.IsDefaultOrEmpty)
{
return default;
}
var mapBuilder = ArrayBuilder<LocalSlotDebugInfo>.GetInstance();
int syntaxOffsetBaseline = -1;
fixed (byte* compressedSlotMapPtr = &compressedSlotMap.ToArray()[0])
{
var blobReader = new BlobReader(compressedSlotMapPtr, compressedSlotMap.Length);
while (blobReader.RemainingBytes > 0)
{
try
{
// Note: integer operations below can't overflow since compressed integers are in range [0, 0x20000000)
byte b = blobReader.ReadByte();
if (b == SyntaxOffsetBaseline)
{
syntaxOffsetBaseline = -blobReader.ReadCompressedInteger();
continue;
}
if (b == 0)
{
// short-lived temp, no info
mapBuilder.Add(new LocalSlotDebugInfo(SynthesizedLocalKind.LoweringTemp, default));
continue;
}
var kind = (SynthesizedLocalKind)((b & 0x3f) - 1);
bool hasOrdinal = (b & (1 << 7)) != 0;
int syntaxOffset = blobReader.ReadCompressedInteger() + syntaxOffsetBaseline;
int ordinal = hasOrdinal ? blobReader.ReadCompressedInteger() : 0;
mapBuilder.Add(new LocalSlotDebugInfo(kind, new LocalDebugId(syntaxOffset, ordinal)));
}
catch (BadImageFormatException)
{
throw CreateInvalidDataException(compressedSlotMap, blobReader.Offset);
}
}
}
return mapBuilder.ToImmutableAndFree();
}
internal void SerializeLocalSlots(BlobBuilder writer)
{
int syntaxOffsetBaseline = -1;
foreach (LocalSlotDebugInfo localSlot in LocalSlots)
{
if (localSlot.Id.SyntaxOffset < syntaxOffsetBaseline)
{
syntaxOffsetBaseline = localSlot.Id.SyntaxOffset;
}
}
if (syntaxOffsetBaseline != -1)
{
writer.WriteByte(SyntaxOffsetBaseline);
writer.WriteCompressedInteger(-syntaxOffsetBaseline);
}
foreach (LocalSlotDebugInfo localSlot in LocalSlots)
{
SynthesizedLocalKind kind = localSlot.SynthesizedKind;
Debug.Assert(kind <= SynthesizedLocalKind.MaxValidValueForLocalVariableSerializedToDebugInformation);
if (!kind.IsLongLived())
{
writer.WriteByte(0);
continue;
}
byte b = (byte)(kind + 1);
Debug.Assert((b & (1 << 7)) == 0);
bool hasOrdinal = localSlot.Id.Ordinal > 0;
if (hasOrdinal)
{
b |= 1 << 7;
}
writer.WriteByte(b);
writer.WriteCompressedInteger(localSlot.Id.SyntaxOffset - syntaxOffsetBaseline);
if (hasOrdinal)
{
writer.WriteCompressedInteger(localSlot.Id.Ordinal);
}
}
}
#endregion
#region Lambdas
private static unsafe void UncompressLambdaMap(
ImmutableArray<byte> compressedLambdaMap,
out int methodOrdinal,
out ImmutableArray<ClosureDebugInfo> closures,
out ImmutableArray<LambdaDebugInfo> lambdas)
{
methodOrdinal = DebugId.UndefinedOrdinal;
closures = default;
lambdas = default;
if (compressedLambdaMap.IsDefaultOrEmpty)
{
return;
}
var closuresBuilder = ArrayBuilder<ClosureDebugInfo>.GetInstance();
var lambdasBuilder = ArrayBuilder<LambdaDebugInfo>.GetInstance();
fixed (byte* blobPtr = &compressedLambdaMap.ToArray()[0])
{
var blobReader = new BlobReader(blobPtr, compressedLambdaMap.Length);
try
{
// Note: integer operations below can't overflow since compressed integers are in range [0, 0x20000000)
// [-1, inf)
methodOrdinal = blobReader.ReadCompressedInteger() - 1;
int syntaxOffsetBaseline = -blobReader.ReadCompressedInteger();
int closureCount = blobReader.ReadCompressedInteger();
for (int i = 0; i < closureCount; i++)
{
int syntaxOffset = blobReader.ReadCompressedInteger();
var closureId = new DebugId(closuresBuilder.Count, generation: 0);
closuresBuilder.Add(new ClosureDebugInfo(syntaxOffset + syntaxOffsetBaseline, closureId));
}
while (blobReader.RemainingBytes > 0)
{
int syntaxOffset = blobReader.ReadCompressedInteger();
int closureOrdinal = blobReader.ReadCompressedInteger() + LambdaDebugInfo.MinClosureOrdinal;
if (closureOrdinal >= closureCount)
{
throw CreateInvalidDataException(compressedLambdaMap, blobReader.Offset);
}
var lambdaId = new DebugId(lambdasBuilder.Count, generation: 0);
lambdasBuilder.Add(new LambdaDebugInfo(syntaxOffset + syntaxOffsetBaseline, lambdaId, closureOrdinal));
}
}
catch (BadImageFormatException)
{
throw CreateInvalidDataException(compressedLambdaMap, blobReader.Offset);
}
}
closures = closuresBuilder.ToImmutableAndFree();
lambdas = lambdasBuilder.ToImmutableAndFree();
}
internal void SerializeLambdaMap(BlobBuilder writer)
{
Debug.Assert(MethodOrdinal >= -1);
writer.WriteCompressedInteger(MethodOrdinal + 1);
// Negative syntax offsets are rare - only when the syntax node is in an initializer of a field or property.
// To optimize for size calculate the base offset and adds it to all syntax offsets. In common cases (no negative offsets)
// this base offset will be 0. Otherwise it will be the lowest negative offset.
int syntaxOffsetBaseline = -1;
foreach (ClosureDebugInfo info in Closures)
{
if (info.SyntaxOffset < syntaxOffsetBaseline)
{
syntaxOffsetBaseline = info.SyntaxOffset;
}
}
foreach (LambdaDebugInfo info in Lambdas)
{
if (info.SyntaxOffset < syntaxOffsetBaseline)
{
syntaxOffsetBaseline = info.SyntaxOffset;
}
}
writer.WriteCompressedInteger(-syntaxOffsetBaseline);
writer.WriteCompressedInteger(Closures.Length);
foreach (ClosureDebugInfo info in Closures)
{
writer.WriteCompressedInteger(info.SyntaxOffset - syntaxOffsetBaseline);
}
foreach (LambdaDebugInfo info in Lambdas)
{
Debug.Assert(info.ClosureOrdinal >= LambdaDebugInfo.MinClosureOrdinal);
Debug.Assert(info.LambdaId.Generation == 0);
writer.WriteCompressedInteger(info.SyntaxOffset - syntaxOffsetBaseline);
writer.WriteCompressedInteger(info.ClosureOrdinal - LambdaDebugInfo.MinClosureOrdinal);
}
}
#endregion
#region State Machine States
/// <exception cref="InvalidDataException">Invalid data.</exception>
private static unsafe ImmutableArray<StateMachineStateDebugInfo> UncompressStateMachineStates(ImmutableArray<byte> compressedStateMachineStates)
{
if (compressedStateMachineStates.IsDefaultOrEmpty)
{
return default;
}
var mapBuilder = ArrayBuilder<StateMachineStateDebugInfo>.GetInstance();
fixed (byte* ptr = &compressedStateMachineStates.ToArray()[0])
{
var blobReader = new BlobReader(ptr, compressedStateMachineStates.Length);
try
{
int count = blobReader.ReadCompressedInteger();
if (count > 0)
{
int syntaxOffsetBaseline = -blobReader.ReadCompressedInteger();
while (count > 0)
{
int stateNumber = blobReader.ReadCompressedSignedInteger();
int syntaxOffset = syntaxOffsetBaseline + blobReader.ReadCompressedInteger();
mapBuilder.Add(new StateMachineStateDebugInfo(syntaxOffset, (StateMachineState)stateNumber));
count--;
}
}
}
catch (BadImageFormatException)
{
throw CreateInvalidDataException(compressedStateMachineStates, blobReader.Offset);
}
}
return mapBuilder.ToImmutableAndFree();
}
internal void SerializeStateMachineStates(BlobBuilder writer)
{
writer.WriteCompressedInteger(StateMachineStates.Length);
if (StateMachineStates.Length > 0)
{
// Negative syntax offsets are rare - only when the syntax node is in an initializer of a field or property.
// To optimize for size calculate the base offset and adds it to all syntax offsets. In common cases (no negative offsets)
// this base offset will be 0. Otherwise it will be the lowest negative offset.
int syntaxOffsetBaseline = Math.Min(StateMachineStates.Min(state => state.SyntaxOffset), 0);
writer.WriteCompressedInteger(-syntaxOffsetBaseline);
foreach (StateMachineStateDebugInfo state in StateMachineStates)
{
writer.WriteCompressedSignedInteger((int)state.StateNumber);
writer.WriteCompressedInteger(state.SyntaxOffset - syntaxOffsetBaseline);
}
}
}
#endregion
}
}