4d84a934
曹明
初始代码提交
|
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
|
<?php
/**
* Small helper class to inspect the stacktrace
*
* @package raven
*/
class Raven_Stacktrace
{
public static $statements = array(
'include',
'include_once',
'require',
'require_once',
);
public static function get_stack_info($frames,
$trace = false,
$errcontext = null,
$frame_var_limit = Raven_Client::MESSAGE_LIMIT,
$strip_prefixes = null,
$app_path = null,
$excluded_app_paths = null,
Raven_Serializer $serializer = null,
Raven_ReprSerializer $reprSerializer = null)
{
$serializer = $serializer ?: new Raven_Serializer();
$reprSerializer = $reprSerializer ?: new Raven_ReprSerializer();
/**
* PHP stores calls in the stacktrace, rather than executing context. Sentry
* wants to know "when Im calling this code, where am I", and PHP says "I'm
* calling this function" not "I'm in this function". Due to that, we shift
* the context for a frame up one, meaning the variables (which are the calling
* args) come from the previous frame.
*/
$result = array();
for ($i = 0; $i < count($frames); $i++) {
$frame = isset($frames[$i]) ? $frames[$i] : array();
$nextframe = isset($frames[$i + 1]) ? $frames[$i + 1] : array();
if (!array_key_exists('file', $frame)) {
$context = array();
if (!empty($frame['class'])) {
$context['line'] = sprintf('%s%s%s', $frame['class'], $frame['type'], $frame['function']);
try {
$reflect = new ReflectionClass($frame['class']);
$context['filename'] = $filename = $reflect->getFileName();
} catch (ReflectionException $e) {
// Forget it if we run into errors, it's not worth it.
}
} elseif (!empty($frame['function'])) {
$context['line'] = sprintf('%s(anonymous)', $frame['function']);
} else {
$context['line'] = sprintf('(anonymous)');
}
if (empty($context['filename'])) {
$context['filename'] = $filename = '[Anonymous function]';
}
$abs_path = '';
$context['prefix'] = '';
$context['suffix'] = '';
$context['lineno'] = 0;
} else {
$context = self::read_source_file($frame['file'], $frame['line']);
$abs_path = $frame['file'];
}
// strip base path if present
$context['filename'] = self::strip_prefixes($context['filename'], $strip_prefixes);
if ($i === 0 && isset($errcontext)) {
// If we've been given an error context that can be used as the vars for the first frame.
$vars = $errcontext;
} else {
if ($trace) {
$vars = self::get_frame_context($nextframe, $frame_var_limit);
} else {
$vars = array();
}
}
$data = array(
'filename' => $context['filename'],
'lineno' => (int) $context['lineno'],
'function' => isset($nextframe['function']) ? $nextframe['function'] : null,
'pre_context' => $serializer->serialize($context['prefix']),
'context_line' => $serializer->serialize($context['line']),
'post_context' => $serializer->serialize($context['suffix']),
);
// detect in_app based on app path
if ($app_path) {
$norm_abs_path = @realpath($abs_path) ?: $abs_path;
if (!$abs_path) {
$in_app = false;
} else {
$in_app = (bool)(substr($norm_abs_path, 0, strlen($app_path)) === $app_path);
}
if ($in_app && $excluded_app_paths) {
foreach ($excluded_app_paths as $path) {
if (substr($norm_abs_path, 0, strlen($path)) === $path) {
$in_app = false;
break;
}
}
}
$data['in_app'] = $in_app;
}
// dont set this as an empty array as PHP will treat it as a numeric array
// instead of a mapping which goes against the defined Sentry spec
if (!empty($vars)) {
$cleanVars = array();
foreach ($vars as $key => $value) {
$value = $reprSerializer->serialize($value);
if (is_string($value) || is_numeric($value)) {
$cleanVars[(string)$key] = substr($value, 0, $frame_var_limit);
} else {
$cleanVars[(string)$key] = $value;
}
}
$data['vars'] = $cleanVars;
}
$result[] = $data;
}
return array_reverse($result);
}
public static function get_default_context($frame, $frame_arg_limit = Raven_Client::MESSAGE_LIMIT)
{
if (!isset($frame['args'])) {
return array();
}
$i = 1;
$args = array();
foreach ($frame['args'] as $arg) {
$args['param'.$i] = self::serialize_argument($arg, $frame_arg_limit);
$i++;
}
return $args;
}
public static function get_frame_context($frame, $frame_arg_limit = Raven_Client::MESSAGE_LIMIT)
{
if (!isset($frame['args'])) {
return array();
}
// The reflection API seems more appropriate if we associate it with the frame
// where the function is actually called (since we're treating them as function context)
if (!isset($frame['function'])) {
return self::get_default_context($frame, $frame_arg_limit);
}
if (strpos($frame['function'], '__lambda_func') !== false) {
return self::get_default_context($frame, $frame_arg_limit);
}
if (isset($frame['class']) && $frame['class'] == 'Closure') {
return self::get_default_context($frame, $frame_arg_limit);
}
if (strpos($frame['function'], '{closure}') !== false) {
return self::get_default_context($frame, $frame_arg_limit);
}
if (in_array($frame['function'], self::$statements)) {
if (empty($frame['args'])) {
// No arguments
return array();
} else {
// Sanitize the file path
return array(
'param1' => self::serialize_argument($frame['args'][0], $frame_arg_limit),
);
}
}
try {
if (isset($frame['class'])) {
if (method_exists($frame['class'], $frame['function'])) {
$reflection = new ReflectionMethod($frame['class'], $frame['function']);
} elseif ($frame['type'] === '::') {
$reflection = new ReflectionMethod($frame['class'], '__callStatic');
} else {
$reflection = new ReflectionMethod($frame['class'], '__call');
}
} elseif (function_exists($frame['function'])) {
$reflection = new ReflectionFunction($frame['function']);
} else {
return self::get_default_context($frame, $frame_arg_limit);
}
} catch (ReflectionException $e) {
return self::get_default_context($frame, $frame_arg_limit);
}
$params = $reflection->getParameters();
$args = array();
foreach ($frame['args'] as $i => $arg) {
$arg = self::serialize_argument($arg, $frame_arg_limit);
if (isset($params[$i])) {
// Assign the argument by the parameter name
$args[$params[$i]->name] = $arg;
} else {
$args['param'.$i] = $arg;
}
}
return $args;
}
private static function serialize_argument($arg, $frame_arg_limit)
{
if (is_array($arg)) {
$_arg = array();
foreach ($arg as $key => $value) {
if (is_string($value) || is_numeric($value)) {
$_arg[$key] = substr($value, 0, $frame_arg_limit);
} else {
$_arg[$key] = $value;
}
}
return $_arg;
} elseif (is_string($arg) || is_numeric($arg)) {
return substr($arg, 0, $frame_arg_limit);
} else {
return $arg;
}
}
private static function strip_prefixes($filename, $prefixes)
{
if ($prefixes === null) {
return $filename;
}
foreach ($prefixes as $prefix) {
if (substr($filename, 0, strlen($prefix)) === $prefix) {
return substr($filename, strlen($prefix));
}
}
return $filename;
}
private static function read_source_file($filename, $lineno, $context_lines = 5)
{
$frame = array(
'prefix' => array(),
'line' => '',
'suffix' => array(),
'filename' => $filename,
'lineno' => $lineno,
);
if ($filename === null || $lineno === null) {
return $frame;
}
// Code which is eval'ed have a modified filename.. Extract the
// correct filename + linenumber from the string.
$matches = array();
$matched = preg_match("/^(.*?)\\((\\d+)\\) : eval\\(\\)'d code$/",
$filename, $matches);
if ($matched) {
$frame['filename'] = $filename = $matches[1];
$frame['lineno'] = $lineno = $matches[2];
}
// In the case of an anonymous function, the filename is sent as:
// "</path/to/filename>(<lineno>) : runtime-created function"
// Extract the correct filename + linenumber from the string.
$matches = array();
$matched = preg_match("/^(.*?)\\((\\d+)\\) : runtime-created function$/",
$filename, $matches);
if ($matched) {
$frame['filename'] = $filename = $matches[1];
$frame['lineno'] = $lineno = $matches[2];
}
if (!file_exists($filename)) {
return $frame;
}
try {
$file = new SplFileObject($filename);
$target = max(0, ($lineno - ($context_lines + 1)));
$file->seek($target);
$cur_lineno = $target+1;
while (!$file->eof()) {
$line = rtrim($file->current(), "\r\n");
if ($cur_lineno == $lineno) {
$frame['line'] = $line;
} elseif ($cur_lineno < $lineno) {
$frame['prefix'][] = $line;
} elseif ($cur_lineno > $lineno) {
$frame['suffix'][] = $line;
}
$cur_lineno++;
if ($cur_lineno > $lineno + $context_lines) {
break;
}
$file->next();
}
} catch (RuntimeException $exc) {
return $frame;
}
return $frame;
}
}
|