-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
FrameBuilder.php
213 lines (180 loc) · 7.58 KB
/
FrameBuilder.php
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
<?php
declare(strict_types=1);
namespace Sentry;
use Sentry\Serializer\RepresentationSerializerInterface;
use Sentry\Util\PrefixStripper;
/**
* This class builds a {@see Frame} object out of a backtrace's raw frame.
*
* @internal
*
* @psalm-type StacktraceFrame array{
* function?: string,
* line?: int,
* file?: string,
* class?: class-string,
* type?: string,
* args?: mixed[]
* }
*/
final class FrameBuilder
{
use PrefixStripper;
/**
* @var Options The SDK client options
*/
private $options;
/**
* @var RepresentationSerializerInterface The representation serializer
*/
private $representationSerializer;
/**
* Constructor.
*
* @param Options $options The SDK client options
* @param RepresentationSerializerInterface $representationSerializer The representation serializer
*/
public function __construct(Options $options, RepresentationSerializerInterface $representationSerializer)
{
$this->options = $options;
$this->representationSerializer = $representationSerializer;
}
/**
* Builds a {@see Frame} object from the given backtrace's raw frame.
*
* @param string $file The file where the frame originated
* @param int $line The line at which the frame originated
* @param array<string, mixed> $backtraceFrame The raw frame
*
* @psalm-param StacktraceFrame $backtraceFrame
*/
public function buildFromBacktraceFrame(string $file, int $line, array $backtraceFrame): Frame
{
// The filename can be in any of these formats:
// - </path/to/filename>
// - </path/to/filename>(<line number>) : eval()'d code
// - </path/to/filename>(<line number>) : runtime-created function
if (preg_match('/^(.*)\((\d+)\) : (?:eval\(\)\'d code|runtime-created function)$/', $file, $matches)) {
$file = $matches[1];
$line = (int) $matches[2];
}
$functionName = null;
$rawFunctionName = null;
$strippedFilePath = $this->stripPrefixFromFilePath($this->options, $file);
if (isset($backtraceFrame['class']) && isset($backtraceFrame['function'])) {
$functionName = $backtraceFrame['class'];
if (mb_substr($functionName, 0, mb_strlen(Frame::ANONYMOUS_CLASS_PREFIX)) === Frame::ANONYMOUS_CLASS_PREFIX) {
$functionName = Frame::ANONYMOUS_CLASS_PREFIX . $this->stripPrefixFromFilePath($this->options, substr($backtraceFrame['class'], \strlen(Frame::ANONYMOUS_CLASS_PREFIX)));
}
$rawFunctionName = sprintf('%s::%s', $backtraceFrame['class'], $backtraceFrame['function']);
$functionName = sprintf('%s::%s', preg_replace('/(?::\d+\$|0x)[a-fA-F0-9]+$/', '', $functionName), $backtraceFrame['function']);
} elseif (isset($backtraceFrame['function'])) {
$functionName = $backtraceFrame['function'];
}
return new Frame(
$functionName,
$strippedFilePath,
$line,
$rawFunctionName,
$file !== Frame::INTERNAL_FRAME_FILENAME ? $file : null,
$this->getFunctionArguments($backtraceFrame),
$this->isFrameInApp($file, $functionName)
);
}
/**
* Checks whether a certain frame should be marked as "in app" or not.
*
* @param string $file The file to check
* @param string|null $functionName The name of the function
*/
private function isFrameInApp(string $file, ?string $functionName): bool
{
if ($file === Frame::INTERNAL_FRAME_FILENAME) {
return false;
}
if ($functionName !== null && substr($functionName, 0, \strlen('Sentry\\')) === 'Sentry\\') {
return false;
}
$excludedAppPaths = $this->options->getInAppExcludedPaths();
$includedAppPaths = $this->options->getInAppIncludedPaths();
$absoluteFilePath = @realpath($file) ?: $file;
$isInApp = true;
foreach ($excludedAppPaths as $excludedAppPath) {
if (mb_substr($absoluteFilePath, 0, mb_strlen($excludedAppPath)) === $excludedAppPath) {
$isInApp = false;
break;
}
}
foreach ($includedAppPaths as $includedAppPath) {
if (mb_substr($absoluteFilePath, 0, mb_strlen($includedAppPath)) === $includedAppPath) {
$isInApp = true;
break;
}
}
return $isInApp;
}
/**
* Gets the arguments of the function called in the given frame.
*
* @param array<string, mixed> $backtraceFrame The frame data
*
* @return array<string, mixed>
*
* @psalm-param StacktraceFrame $backtraceFrame
*/
private function getFunctionArguments(array $backtraceFrame): array
{
if (!isset($backtraceFrame['function'], $backtraceFrame['args'])) {
return [];
}
$reflectionFunction = null;
try {
if (isset($backtraceFrame['class'])) {
if (method_exists($backtraceFrame['class'], $backtraceFrame['function'])) {
$reflectionFunction = new \ReflectionMethod($backtraceFrame['class'], $backtraceFrame['function']);
} elseif (isset($backtraceFrame['type']) && $backtraceFrame['type'] === '::') {
$reflectionFunction = new \ReflectionMethod($backtraceFrame['class'], '__callStatic');
} else {
$reflectionFunction = new \ReflectionMethod($backtraceFrame['class'], '__call');
}
} elseif (!\in_array($backtraceFrame['function'], ['{closure}', '__lambda_func'], true) && \function_exists($backtraceFrame['function'])) {
$reflectionFunction = new \ReflectionFunction($backtraceFrame['function']);
}
} catch (\ReflectionException $e) {
// Reflection failed, we do nothing instead
}
$argumentValues = [];
if ($reflectionFunction !== null) {
$argumentValues = $this->getFunctionArgumentValues($reflectionFunction, $backtraceFrame['args']);
} else {
foreach ($backtraceFrame['args'] as $parameterPosition => $parameterValue) {
$argumentValues['param' . $parameterPosition] = $parameterValue;
}
}
foreach ($argumentValues as $argumentName => $argumentValue) {
$argumentValues[$argumentName] = $this->representationSerializer->representationSerialize($argumentValue);
}
return $argumentValues;
}
/**
* Gets an hashmap indexed by argument name containing all the arguments
* passed to the function called in the given frame of the stacktrace.
*
* @param \ReflectionFunctionAbstract $reflectionFunction A reflection object
* @param mixed[] $backtraceFrameArgs The arguments of the frame
*
* @return array<string, mixed>
*/
private function getFunctionArgumentValues(\ReflectionFunctionAbstract $reflectionFunction, array $backtraceFrameArgs): array
{
$argumentValues = [];
foreach ($reflectionFunction->getParameters() as $reflectionParameter) {
$parameterPosition = $reflectionParameter->getPosition();
if (!isset($backtraceFrameArgs[$parameterPosition])) {
continue;
}
$argumentValues[$reflectionParameter->getName()] = $backtraceFrameArgs[$parameterPosition];
}
return $argumentValues;
}
}