-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathExceptionReplayProcessor.cs
More file actions
310 lines (260 loc) · 14.1 KB
/
ExceptionReplayProcessor.cs
File metadata and controls
310 lines (260 loc) · 14.1 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
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
// <copyright file="ExceptionReplayProcessor.cs" company="Datadog">
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
// </copyright>
using System;
using Datadog.Trace.Debugger.Configurations.Models;
using Datadog.Trace.Debugger.Expressions;
using Datadog.Trace.Debugger.Instrumentation.Collections;
using Datadog.Trace.Debugger.Snapshots;
using Datadog.Trace.Logging;
#nullable enable
namespace Datadog.Trace.Debugger.ExceptionAutoInstrumentation
{
internal sealed class ExceptionReplayProcessor : IProbeProcessor
{
private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(ExceptionReplayProcessor));
private readonly object _lock = new();
private readonly int _maxFramesToCapture;
private readonly bool _isMisleadingMethod;
private ExceptionProbeProcessor[] _processors;
internal ExceptionReplayProcessor(string probeId, MethodUniqueIdentifier method, int maxFramesToCapture)
{
_processors = Array.Empty<ExceptionProbeProcessor>();
ProbeId = probeId;
Method = method;
_maxFramesToCapture = maxFramesToCapture;
_isMisleadingMethod = method.IsMisleadMethod();
}
public string ProbeId { get; }
public MethodUniqueIdentifier Method { get; }
public bool ShouldProcess(in ProbeData probeData)
{
if (!ShadowStackHolder.IsShadowStackTrackingEnabled)
{
// In Exception Debugging V1, we only care about exceptions propagating up the call stack in webservice apps, when there's a request in-flight.
// When we will support Caught & Logged Exceptions, we will need to adjust this.
// `IsShadowStackTrackingEnabled` is equivalent to using `ShadowStackTree.IsInRequestContext`, as of now.
return false;
}
var shadowStack = ShadowStackHolder.EnsureShadowStackEnabled();
if (shadowStack.CurrentStackFrameNode?.IsInvalidPath == true)
{
return false;
}
return true;
}
public bool Process<TCapture>(ref CaptureInfo<TCapture> info, IDebuggerSnapshotCreator inSnapshotCreator, in ProbeData probeData)
{
var snapshotCreator = (ExceptionSnapshotCreator)inSnapshotCreator;
ShadowStackTree shadowStack;
try
{
switch (info.MethodState)
{
case MethodState.BeginLine:
case MethodState.BeginLineAsync:
break;
case MethodState.EntryStart:
case MethodState.EntryAsync:
shadowStack = ShadowStackHolder.EnsureShadowStackEnabled();
var currentFrame = shadowStack.CurrentStackFrameNode;
snapshotCreator.EnterHash = SimpleHash.Combine(info.Method.MetadataToken, shadowStack.CurrentStackFrameNode?.EnterSequenceHash ?? SimpleHash.FnvOffsetBias);
if (currentFrame?.Method == info.Method && _isMisleadingMethod)
{
// Methods marked as `misleading` are methods we tolerate being in the shadow stack multiple times.
// We flatten those methods due to `ExceptionDispatchInfo.Capture(X).Throw()` API causing frames to appear twice
// while in reality they were involved only once.
snapshotCreator.TrackedStackFrameNode = shadowStack.EnterFake(info.Method);
return true;
}
var shouldProcess = false;
foreach (var processor in snapshotCreator.Processors)
{
if (processor.ShouldProcess(snapshotCreator.EnterHash))
{
// TODO consider if the processors that successfully entered, should be used solely on Leave without any noise.
// TODO this requirement arises due to Invalidation that could happen to EnterHash if a new method pops in the call path.
shouldProcess = true;
}
}
var enteredNode = shadowStack.Enter(info.Method, isInvalidPath: !shouldProcess);
snapshotCreator.TrackedStackFrameNode = enteredNode;
return true;
case MethodState.ExitStart:
case MethodState.ExitStartAsync:
if (snapshotCreator.TrackedStackFrameNode == null)
{
throw new InvalidOperationException("Encountered invalid state of snapshotCreator in ExitState/ExitStateAsync. `snapshotCreator.TrackedStackFrameNode` should not be null.");
}
if (snapshotCreator.TrackedStackFrameNode.IsFrameUnwound)
{
Log.Warning("ExceptionReplayProcessor: Frame is already unwound. Probe Id: {ProbeId}", ProbeId);
return false;
}
shadowStack = ShadowStackHolder.EnsureShadowStackEnabled();
if (snapshotCreator.TrackedStackFrameNode.IsInvalidPath)
{
shadowStack.Leave(snapshotCreator.TrackedStackFrameNode);
return false;
}
if (info.MemberKind != ScopeMemberKind.Exception || info.Value == null)
{
shadowStack.Leave(snapshotCreator.TrackedStackFrameNode);
return false;
}
var exception = info.Value as Exception;
snapshotCreator.TrackedStackFrameNode.LeavingException = exception;
snapshotCreator.LeaveHash = shadowStack.CurrentStackFrameNode!.LeaveSequenceHash;
if (snapshotCreator.TrackedStackFrameNode is FakeTrackedStackFrameNode)
{
shadowStack.Leave(snapshotCreator.TrackedStackFrameNode, exception);
snapshotCreator.TrackedStackFrameNode.CapturingStrategy = SnapshotCapturingStrategy.FullSnapshot;
snapshotCreator.TrackedStackFrameNode.AddScopeMember(info.Name, info.Type, info.Value, info.MemberKind);
return true;
}
var leavingExceptionType = info.Value.GetType();
foreach (var processor in snapshotCreator.Processors)
{
if (processor.Leave(leavingExceptionType, snapshotCreator))
{
var isRoot = shadowStack.Leave(snapshotCreator.TrackedStackFrameNode, exception);
if (!isRoot && snapshotCreator.TrackedStackFrameNode.NumOfChildren > _maxFramesToCapture)
{
// Capture no more.
snapshotCreator.TrackedStackFrameNode.CapturingStrategy = SnapshotCapturingStrategy.None;
return true;
}
// Full Snapshot / Lightweight
var sequenceHash = snapshotCreator.TrackedStackFrameNode.SequenceHash;
if (!shadowStack.ContainsUniqueId(sequenceHash))
{
shadowStack.AddUniqueId(sequenceHash);
snapshotCreator.TrackedStackFrameNode.CapturingStrategy = SnapshotCapturingStrategy.FullSnapshot;
}
else
{
snapshotCreator.TrackedStackFrameNode.CapturingStrategy = SnapshotCapturingStrategy.LightweightSnapshot;
}
snapshotCreator.TrackedStackFrameNode.AddScopeMember(info.Name, info.Type, info.Value, info.MemberKind);
return true;
}
}
shadowStack.Leave(snapshotCreator.TrackedStackFrameNode);
return false;
case MethodState.EntryEnd:
case MethodState.EndLine:
case MethodState.EndLineAsync:
break;
case MethodState.ExitEnd:
case MethodState.ExitEndAsync:
if (snapshotCreator.TrackedStackFrameNode == null)
{
throw new InvalidOperationException("Encountered invalid state of snapshotCreator in ExitEnd/ExitEndAsync. `snapshotCreator.TrackedStackFrameNode` should not be null.");
}
if (snapshotCreator.TrackedStackFrameNode.CapturingStrategy != SnapshotCapturingStrategy.None)
{
snapshotCreator.TrackedStackFrameNode.AddScopeMember(info.Name, info.Type, info.Value, info.MemberKind);
snapshotCreator.TrackedStackFrameNode.MethodMetadataIndex = info.MethodMetadataIndex;
snapshotCreator.TrackedStackFrameNode.ProbeId = ProbeId;
snapshotCreator.TrackedStackFrameNode.HasArgumentsOrLocals = info.HasLocalOrArgument;
if (info.IsAsyncCapture())
{
snapshotCreator.TrackedStackFrameNode.IsAsyncMethod = true;
snapshotCreator.TrackedStackFrameNode.MoveNextInvocationTarget = info.AsyncCaptureInfo.MoveNextInvocationTarget;
snapshotCreator.TrackedStackFrameNode.KickoffInvocationTarget = info.AsyncCaptureInfo.KickoffInvocationTarget;
}
if (snapshotCreator.TrackedStackFrameNode.CapturingStrategy == SnapshotCapturingStrategy.FullSnapshot)
{
_ = snapshotCreator.TrackedStackFrameNode.Snapshot;
}
}
return true;
case MethodState.LogLocal:
case MethodState.LogArg:
if (snapshotCreator.TrackedStackFrameNode == null)
{
throw new InvalidOperationException("Encountered invalid state of snapshotCreator in LogLocal/LogArg. `snapshotCreator.TrackedStackFrameNode` should not be null.");
}
if (snapshotCreator.TrackedStackFrameNode.CapturingStrategy != SnapshotCapturingStrategy.None)
{
snapshotCreator.TrackedStackFrameNode.AddScopeMember(info.Name, info.Type, info.Value, info.MemberKind);
}
break;
}
}
catch (Exception e)
{
Log.Error(e, "ExceptionReplayProcessor: Failed to process probe. Probe Id: {ProbeId}", ProbeId);
return false;
}
return true;
}
public void LogException(Exception ex, IDebuggerSnapshotCreator inSnapshotCreator)
{
var snapshotCreator = (ExceptionSnapshotCreator)inSnapshotCreator;
if (snapshotCreator.TrackedStackFrameNode == null)
{
return;
}
var shadowStack = ShadowStackHolder.EnsureShadowStackEnabled();
shadowStack.Leave(snapshotCreator.TrackedStackFrameNode);
}
public IProbeProcessor UpdateProbeProcessor(ProbeDefinition probe)
{
return this;
}
public IDebuggerSnapshotCreator CreateSnapshotCreator()
{
// ReSharper disable once InconsistentlySynchronizedField
return new ExceptionSnapshotCreator(_processors, ProbeId);
}
public void AddProbeProcessor(ExceptionProbeProcessor processor)
{
lock (_lock)
{
var newProcessors = new ExceptionProbeProcessor[_processors.Length + 1];
Array.Copy(_processors, newProcessors, _processors.Length);
newProcessors[newProcessors.Length - 1] = processor;
_processors = newProcessors;
}
}
public int RemoveProbeProcessor(ExceptionProbeProcessor processor)
{
lock (_lock)
{
var index = Array.IndexOf(_processors, processor);
if (index < 0)
{
return -1;
}
if (_processors.Length == 1)
{
_processors = Array.Empty<ExceptionProbeProcessor>();
return 0;
}
var newProcessors = new ExceptionProbeProcessor[_processors.Length - 1];
if (index > 0)
{
Array.Copy(_processors, 0, newProcessors, 0, index);
}
if (index < _processors.Length - 1)
{
Array.Copy(_processors, index + 1, newProcessors, index, _processors.Length - index - 1);
}
_processors = newProcessors;
return _processors.Length;
}
}
public void InvalidateEnterLeave()
{
lock (_lock)
{
foreach (var processor in _processors)
{
processor.InvalidateEnterLeave();
}
}
}
}
}