forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrameIterator.cs
168 lines (140 loc) · 6.08 KB
/
FrameIterator.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
namespace Microsoft.Diagnostics.DataContractReader.Contracts.StackWalkHelpers;
internal sealed class FrameIterator
{
private enum FrameType
{
Unknown,
InlinedCallFrame,
SoftwareExceptionFrame,
/* TransitionFrame Types */
FramedMethodFrame,
CLRToCOMMethodFrame,
PInvokeCalliFrame,
PrestubMethodFrame,
StubDispatchFrame,
CallCountingHelperFrame,
ExternalMethodFrame,
DynamicHelperFrame,
FuncEvalFrame,
/* ResumableFrame Types */
ResumableFrame,
RedirectedThreadFrame,
FaultingExceptionFrame,
HijackFrame,
}
private readonly Target target;
private readonly TargetPointer terminator;
private TargetPointer currentFramePointer;
internal Data.Frame CurrentFrame => target.ProcessedData.GetOrAdd<Data.Frame>(currentFramePointer);
public TargetPointer CurrentFrameAddress => currentFramePointer;
public FrameIterator(Target target, ThreadData threadData)
{
this.target = target;
terminator = new TargetPointer(target.PointerSize == 8 ? ulong.MaxValue : uint.MaxValue);
currentFramePointer = threadData.Frame;
}
public bool IsValid()
{
return currentFramePointer != terminator;
}
public bool Next()
{
if (currentFramePointer == terminator)
return false;
currentFramePointer = CurrentFrame.Next;
return currentFramePointer != terminator;
}
public void UpdateContextFromFrame(IPlatformAgnosticContext context)
{
switch (GetFrameType(target, CurrentFrame.Identifier))
{
case FrameType.InlinedCallFrame:
Data.InlinedCallFrame inlinedCallFrame = target.ProcessedData.GetOrAdd<Data.InlinedCallFrame>(CurrentFrame.Address);
GetFrameHandler(context).HandleInlinedCallFrame(inlinedCallFrame);
return;
case FrameType.SoftwareExceptionFrame:
Data.SoftwareExceptionFrame softwareExceptionFrame = target.ProcessedData.GetOrAdd<Data.SoftwareExceptionFrame>(CurrentFrame.Address);
GetFrameHandler(context).HandleSoftwareExceptionFrame(softwareExceptionFrame);
return;
// TransitionFrame type frames
case FrameType.FramedMethodFrame:
case FrameType.CLRToCOMMethodFrame:
case FrameType.PInvokeCalliFrame:
case FrameType.PrestubMethodFrame:
case FrameType.StubDispatchFrame:
case FrameType.CallCountingHelperFrame:
case FrameType.ExternalMethodFrame:
case FrameType.DynamicHelperFrame:
// FrameMethodFrame is the base type for all transition Frames
Data.FramedMethodFrame framedMethodFrame = target.ProcessedData.GetOrAdd<Data.FramedMethodFrame>(CurrentFrame.Address);
GetFrameHandler(context).HandleTransitionFrame(framedMethodFrame);
return;
case FrameType.FuncEvalFrame:
Data.FuncEvalFrame funcEvalFrame = target.ProcessedData.GetOrAdd<Data.FuncEvalFrame>(CurrentFrame.Address);
GetFrameHandler(context).HandleFuncEvalFrame(funcEvalFrame);
return;
// ResumableFrame type frames
case FrameType.ResumableFrame:
case FrameType.RedirectedThreadFrame:
Data.ResumableFrame resumableFrame = target.ProcessedData.GetOrAdd<Data.ResumableFrame>(CurrentFrame.Address);
GetFrameHandler(context).HandleResumableFrame(resumableFrame);
return;
case FrameType.FaultingExceptionFrame:
Data.FaultingExceptionFrame faultingExceptionFrame = target.ProcessedData.GetOrAdd<Data.FaultingExceptionFrame>(CurrentFrame.Address);
GetFrameHandler(context).HandleFaultingExceptionFrame(faultingExceptionFrame);
return;
case FrameType.HijackFrame:
Data.HijackFrame hijackFrame = target.ProcessedData.GetOrAdd<Data.HijackFrame>(CurrentFrame.Address);
GetFrameHandler(context).HandleHijackFrame(hijackFrame);
return;
default:
// Unknown Frame type. This could either be a Frame that we don't know how to handle,
// or a Frame that does not update the context.
return;
}
}
public bool IsInlineCallFrameWithActiveCall()
{
if (GetFrameType(target, CurrentFrame.Identifier) != FrameType.InlinedCallFrame)
{
return false;
}
Data.InlinedCallFrame inlinedCallFrame = target.ProcessedData.GetOrAdd<Data.InlinedCallFrame>(currentFramePointer);
return inlinedCallFrame.CallerReturnAddress != 0;
}
public static string GetFrameName(Target target, TargetPointer frameIdentifier)
{
FrameType frameType = GetFrameType(target, frameIdentifier);
if (frameType == FrameType.Unknown)
{
return string.Empty;
}
return frameType.ToString();
}
private static FrameType GetFrameType(Target target, TargetPointer frameIdentifier)
{
foreach (FrameType frameType in Enum.GetValues<FrameType>())
{
if (target.TryReadGlobal(frameType.ToString() + "Identifier", out ulong? id))
{
if (frameIdentifier == new TargetPointer(id.Value))
{
return frameType;
}
}
}
return FrameType.Unknown;
}
private IPlatformFrameHandler GetFrameHandler(IPlatformAgnosticContext context)
{
return context switch
{
ContextHolder<AMD64Context> contextHolder => new AMD64FrameHandler(target, contextHolder),
ContextHolder<ARM64Context> contextHolder => new ARM64FrameHandler(target, contextHolder),
_ => throw new InvalidOperationException("Unsupported context type"),
};
}
}