-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathframe.dart
458 lines (400 loc) · 16.9 KB
/
frame.dart
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:path/path.dart' as path;
import 'trace.dart';
import 'unparsed_frame.dart';
// #1 Foo._bar (file:///home/nweiz/code/stuff.dart:42:21)
// #1 Foo._bar (file:///home/nweiz/code/stuff.dart:42)
// #1 Foo._bar (file:///home/nweiz/code/stuff.dart)
final _vmFrame = RegExp(r'^#\d+\s+(\S.*) \((.+?)((?::\d+){0,2})\)$');
// at Object.stringify (native)
// at VW.call$0 (https://example.com/stuff.dart.js:560:28)
// at VW.call$0 (eval as fn
// (https://example.com/stuff.dart.js:560:28), efn:3:28)
// at https://example.com/stuff.dart.js:560:28
final _v8JsFrame =
RegExp(r'^\s*at (?:(\S.*?)(?: \[as [^\]]+\])? \((.*)\)|(.*))$');
// https://example.com/stuff.dart.js:560:28
// https://example.com/stuff.dart.js:560
//
// Group 1: URI, required
// Group 2: line number, required
// Group 3: column number, optional
final _v8JsUrlLocation = RegExp(r'^(.*?):(\d+)(?::(\d+))?$|native$');
// With names:
//
// at Error.f (wasm://wasm/0006d966:wasm-function[119]:0xbb13)
// at g (wasm://wasm/0006d966:wasm-function[796]:0x143b4)
//
// Without names:
//
// at wasm://wasm/0005168a:wasm-function[119]:0xbb13
// at wasm://wasm/0005168a:wasm-function[796]:0x143b4
//
// Matches named groups:
//
// - "member": optional, `Error.f` in the first example, NA in the second.
// - "uri": `wasm://wasm/0006d966`.
// - "index": `119`.
// - "offset": (hex number) `bb13`.
//
// To avoid having multiple groups for the same part of the frame, this regex
// matches unmatched parentheses after the member name.
final _v8WasmFrame = RegExp(r'^\s*at (?:(?<member>.+) )?'
r'(?:\(?(?:(?<uri>\S+):wasm-function\[(?<index>\d+)\]'
r'\:0x(?<offset>[0-9a-fA-F]+))\)?)$');
// eval as function (https://example.com/stuff.dart.js:560:28), efn:3:28
// eval as function (https://example.com/stuff.dart.js:560:28)
// eval as function (eval as otherFunction
// (https://example.com/stuff.dart.js:560:28))
final _v8EvalLocation =
RegExp(r'^eval at (?:\S.*?) \((.*)\)(?:, .*?:\d+:\d+)?$');
// anonymous/<@https://example.com/stuff.js line 693 > Function:3:40
// anonymous/<@https://example.com/stuff.js line 693 > eval:3:40
final _firefoxEvalLocation =
RegExp(r'(\S+)@(\S+) line (\d+) >.* (Function|eval):\d+:\d+');
// .VW.call$0@https://example.com/stuff.dart.js:560
// .VW.call$0("arg")@https://example.com/stuff.dart.js:560
// .VW.call$0/name<@https://example.com/stuff.dart.js:560
// .VW.call$0@https://example.com/stuff.dart.js:560:36
// https://example.com/stuff.dart.js:560
final _firefoxSafariJSFrame = RegExp(r'^'
r'(?:' // Member description. Not present in some Safari frames.
r'([^@(/]*)' // The actual name of the member.
r'(?:\(.*\))?' // Arguments to the member, sometimes captured by Firefox.
r'((?:/[^/]*)*)' // Extra characters indicating a nested closure.
r'(?:\(.*\))?' // Arguments to the closure.
r'@'
r')?'
r'(.*?)' // The frame's URL.
r':'
r'(\d*)' // The line number. Empty in Safari if it's unknown.
r'(?::(\d*))?' // The column number. Not present in older browsers and
// empty in Safari if it's unknown.
r'$');
// With names:
//
// g@http://localhost:8080/test.wasm:wasm-function[796]:0x143b4
// f@http://localhost:8080/test.wasm:wasm-function[795]:0x143a8
// main@http://localhost:8080/test.wasm:wasm-function[792]:0x14390
//
// Without names:
//
// @http://localhost:8080/test.wasm:wasm-function[796]:0x143b4
// @http://localhost:8080/test.wasm:wasm-function[795]:0x143a8
// @http://localhost:8080/test.wasm:wasm-function[792]:0x14390
//
// JSShell in the command line uses a different format, which this regex also
// parses.
//
// With names:
//
// main@/home/user/test.mjs line 29 > WebAssembly.compile:wasm-function[792]:0x14378
//
// Without names:
//
// @/home/user/test.mjs line 29 > WebAssembly.compile:wasm-function[792]:0x14378
//
// Matches named groups:
//
// - "member": Function name, may be empty: `g`.
// - "uri": `http://localhost:8080/test.wasm`.
// - "index": `796`.
// - "offset": (in hex) `143b4`.
final _firefoxWasmFrame =
RegExp(r'^(?<member>.*?)@(?:(?<uri>\S+).*?:wasm-function'
r'\[(?<index>\d+)\]:0x(?<offset>[0-9a-fA-F]+))$');
// With names:
//
// (Note: Lines below are literal text, e.g. <?> is not a placeholder, it's a
// part of the stack frame.)
//
// <?>.wasm-function[g]@[wasm code]
// <?>.wasm-function[f]@[wasm code]
// <?>.wasm-function[main]@[wasm code]
//
// Without names:
//
// <?>.wasm-function[796]@[wasm code]
// <?>.wasm-function[795]@[wasm code]
// <?>.wasm-function[792]@[wasm code]
//
// Matches named group "member": `g` or `796`.
final _safariWasmFrame =
RegExp(r'^.*?wasm-function\[(?<member>.*)\]@\[wasm code\]$');
// foo/bar.dart 10:11 Foo._bar
// foo/bar.dart 10:11 (anonymous function).dart.fn
// https://dart.dev/foo/bar.dart Foo._bar
// data:... 10:11 Foo._bar
final _friendlyFrame = RegExp(r'^(\S+)(?: (\d+)(?::(\d+))?)?\s+([^\d].*)$');
/// A regular expression that matches asynchronous member names generated by the
/// VM.
final _asyncBody = RegExp(r'<(<anonymous closure>|[^>]+)_async_body>');
final _initialDot = RegExp(r'^\.');
/// A single stack frame. Each frame points to a precise location in Dart code.
class Frame {
/// The URI of the file in which the code is located.
///
/// This URI will usually have the scheme `dart`, `file`, `http`, or `https`.
final Uri uri;
/// The line number on which the code location is located.
///
/// This can be null, indicating that the line number is unknown or
/// unimportant.
final int? line;
/// The column number of the code location.
///
/// This can be null, indicating that the column number is unknown or
/// unimportant.
final int? column;
/// The name of the member in which the code location occurs.
///
/// Anonymous closures are represented as `<fn>` in this member string.
final String? member;
/// Whether this stack frame comes from the Dart core libraries.
bool get isCore => uri.scheme == 'dart';
/// Returns a human-friendly description of the library that this stack frame
/// comes from.
///
/// This will usually be the string form of [uri], but a relative URI will be
/// used if possible. Data URIs will be truncated.
String get library {
if (uri.scheme == 'data') return 'data:...';
return path.prettyUri(uri);
}
/// Returns the name of the package this stack frame comes from, or `null` if
/// this stack frame doesn't come from a `package:` URL.
String? get package {
if (uri.scheme != 'package') return null;
return uri.path.split('/').first;
}
/// A human-friendly description of the code location.
String get location {
if (line == null) return library;
if (column == null) return '$library $line';
return '$library $line:$column';
}
/// Returns a single frame of the current stack.
///
/// By default, this will return the frame above the current method. If
/// [level] is `0`, it will return the current method's frame; if [level] is
/// higher than `1`, it will return higher frames.
factory Frame.caller([int level = 1]) {
if (level < 0) {
throw ArgumentError('Argument [level] must be greater than or equal '
'to 0.');
}
return Trace.current(level + 1).frames.first;
}
/// Parses a string representation of a Dart VM stack frame.
factory Frame.parseVM(String frame) => _catchFormatException(frame, () {
// The VM sometimes folds multiple stack frames together and replaces
// them with "...".
if (frame == '...') {
return Frame(Uri(), null, null, '...');
}
var match = _vmFrame.firstMatch(frame);
if (match == null) return UnparsedFrame(frame);
// Get the pieces out of the regexp match. Function, URI and line should
// always be found. The column is optional.
var member = match[1]!
.replaceAll(_asyncBody, '<async>')
.replaceAll('<anonymous closure>', '<fn>');
var uri = match[2]!.startsWith('<data:')
? Uri.dataFromString('')
: Uri.parse(match[2]!);
var lineAndColumn = match[3]!.split(':');
var line =
lineAndColumn.length > 1 ? int.parse(lineAndColumn[1]) : null;
var column =
lineAndColumn.length > 2 ? int.parse(lineAndColumn[2]) : null;
return Frame(uri, line, column, member);
});
/// Parses a string representation of a Chrome/V8 stack frame.
factory Frame.parseV8(String frame) => _catchFormatException(frame, () {
// Try to match a Wasm frame first: the Wasm frame regex won't match a
// JS frame but the JS frame regex may match a Wasm frame.
var match = _v8WasmFrame.firstMatch(frame);
if (match != null) {
final member = match.namedGroup('member');
final uri = _uriOrPathToUri(match.namedGroup('uri')!);
final functionIndex = match.namedGroup('index')!;
final functionOffset =
int.parse(match.namedGroup('offset')!, radix: 16);
return Frame(uri, 1, functionOffset + 1, member ?? functionIndex);
}
match = _v8JsFrame.firstMatch(frame);
if (match != null) {
// v8 location strings can be arbitrarily-nested, since it adds a
// layer of nesting for each eval performed on that line.
Frame parseJsLocation(String location, String member) {
var evalMatch = _v8EvalLocation.firstMatch(location);
while (evalMatch != null) {
location = evalMatch[1]!;
evalMatch = _v8EvalLocation.firstMatch(location);
}
if (location == 'native') {
return Frame(Uri.parse('native'), null, null, member);
}
var urlMatch = _v8JsUrlLocation.firstMatch(location);
if (urlMatch == null) return UnparsedFrame(frame);
final uri = _uriOrPathToUri(urlMatch[1]!);
final line = int.parse(urlMatch[2]!);
final columnMatch = urlMatch[3];
final column = columnMatch != null ? int.parse(columnMatch) : null;
return Frame(uri, line, column, member);
}
// V8 stack frames can be in two forms.
if (match[2] != null) {
// The first form looks like " at FUNCTION (LOCATION)". V8 proper
// lists anonymous functions within eval as "<anonymous>", while
// IE10 lists them as "Anonymous function".
return parseJsLocation(
match[2]!,
match[1]!
.replaceAll('<anonymous>', '<fn>')
.replaceAll('Anonymous function', '<fn>')
.replaceAll('(anonymous function)', '<fn>'));
} else {
// The second form looks like " at LOCATION", and is used for
// anonymous functions.
return parseJsLocation(match[3]!, '<fn>');
}
}
return UnparsedFrame(frame);
});
/// Parses a string representation of a JavaScriptCore stack trace.
factory Frame.parseJSCore(String frame) => Frame.parseV8(frame);
/// Parses a string representation of an IE stack frame.
///
/// IE10+ frames look just like V8 frames. Prior to IE10, stack traces can't
/// be retrieved.
factory Frame.parseIE(String frame) => Frame.parseV8(frame);
/// Parses a Firefox 'eval' or 'function' stack frame.
///
/// For example:
///
/// ```
/// anonymous/<@https://example.com/stuff.js line 693 > Function:3:40
/// anonymous/<@https://example.com/stuff.js line 693 > eval:3:40
/// ```
factory Frame._parseFirefoxEval(String frame) =>
_catchFormatException(frame, () {
final match = _firefoxEvalLocation.firstMatch(frame);
if (match == null) return UnparsedFrame(frame);
var member = match[1]!.replaceAll('/<', '');
final uri = _uriOrPathToUri(match[2]!);
final line = int.parse(match[3]!);
if (member.isEmpty || member == 'anonymous') {
member = '<fn>';
}
return Frame(uri, line, null, member);
});
/// Parses a string representation of a Firefox or Safari stack frame.
factory Frame.parseFirefox(String frame) => _catchFormatException(frame, () {
var match = _firefoxSafariJSFrame.firstMatch(frame);
if (match != null) {
if (match[3]!.contains(' line ')) {
return Frame._parseFirefoxEval(frame);
}
// Normally this is a URI, but in a jsshell trace it can be a path.
var uri = _uriOrPathToUri(match[3]!);
var member = match[1];
if (member != null) {
member +=
List.filled('/'.allMatches(match[2]!).length, '.<fn>').join();
if (member == '') member = '<fn>';
// Some Firefox members have initial dots. We remove them for
// consistency with other platforms.
member = member.replaceFirst(_initialDot, '');
} else {
member = '<fn>';
}
var line = match[4] == '' ? null : int.parse(match[4]!);
var column =
match[5] == null || match[5] == '' ? null : int.parse(match[5]!);
return Frame(uri, line, column, member);
}
match = _firefoxWasmFrame.firstMatch(frame);
if (match != null) {
final member = match.namedGroup('member')!;
final uri = _uriOrPathToUri(match.namedGroup('uri')!);
final functionIndex = match.namedGroup('index')!;
final functionOffset =
int.parse(match.namedGroup('offset')!, radix: 16);
return Frame(uri, 1, functionOffset + 1,
member.isNotEmpty ? member : functionIndex);
}
match = _safariWasmFrame.firstMatch(frame);
if (match != null) {
final member = match.namedGroup('member')!;
return Frame(Uri(path: 'wasm code'), null, null, member);
}
return UnparsedFrame(frame);
});
/// Parses a string representation of a Safari 6.0 stack frame.
@Deprecated('Use Frame.parseSafari instead.')
factory Frame.parseSafari6_0(String frame) => Frame.parseFirefox(frame);
/// Parses a string representation of a Safari 6.1+ stack frame.
@Deprecated('Use Frame.parseSafari instead.')
factory Frame.parseSafari6_1(String frame) => Frame.parseFirefox(frame);
/// Parses a string representation of a Safari stack frame.
factory Frame.parseSafari(String frame) => Frame.parseFirefox(frame);
/// Parses this package's string representation of a stack frame.
factory Frame.parseFriendly(String frame) => _catchFormatException(frame, () {
var match = _friendlyFrame.firstMatch(frame);
if (match == null) {
throw FormatException(
"Couldn't parse package:stack_trace stack trace line '$frame'.");
}
// Fake truncated data urls generated by the friendly stack trace format
// cause Uri.parse to throw an exception so we have to special case
// them.
var uri = match[1] == 'data:...'
? Uri.dataFromString('')
: Uri.parse(match[1]!);
// If there's no scheme, this is a relative URI. We should interpret it
// as relative to the current working directory.
if (uri.scheme == '') {
uri = path.toUri(path.absolute(path.fromUri(uri)));
}
var line = match[2] == null ? null : int.parse(match[2]!);
var column = match[3] == null ? null : int.parse(match[3]!);
return Frame(uri, line, column, match[4]);
});
/// A regular expression matching an absolute URI.
static final _uriRegExp = RegExp(r'^[a-zA-Z][-+.a-zA-Z\d]*://');
/// A regular expression matching a Windows path.
static final _windowsRegExp = RegExp(r'^([a-zA-Z]:[\\/]|\\\\)');
/// Converts [uriOrPath], which can be a URI, a Windows path, or a Posix path,
/// to a URI (absolute if possible).
static Uri _uriOrPathToUri(String uriOrPath) {
if (uriOrPath.contains(_uriRegExp)) {
return Uri.parse(uriOrPath);
} else if (uriOrPath.contains(_windowsRegExp)) {
return Uri.file(uriOrPath, windows: true);
} else if (uriOrPath.startsWith('/')) {
return Uri.file(uriOrPath, windows: false);
}
// As far as I've seen, Firefox and V8 both always report absolute paths in
// their stack frames. However, if we do get a relative path, we should
// handle it gracefully.
if (uriOrPath.contains('\\')) return path.windows.toUri(uriOrPath);
return Uri.parse(uriOrPath);
}
/// Runs [body] and returns its result.
///
/// If [body] throws a [FormatException], returns an [UnparsedFrame] with
/// [text] instead.
static Frame _catchFormatException(String text, Frame Function() body) {
try {
return body();
} on FormatException catch (_) {
return UnparsedFrame(text);
}
}
Frame(this.uri, this.line, this.column, this.member);
@override
String toString() => '$location in $member';
}