| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library trace; | 5 library trace; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:math' as math; | 8 import 'dart:math' as math; |
| 9 | 9 |
| 10 import 'frame.dart'; | 10 import 'frame.dart'; |
| 11 import 'lazy_trace.dart'; | 11 import 'lazy_trace.dart'; |
| 12 import 'utils.dart'; | 12 import 'utils.dart'; |
| 13 import 'vm_trace.dart'; | 13 import 'vm_trace.dart'; |
| 14 | 14 |
| 15 final _terseRegExp = new RegExp(r"(-patch)?(/.*)?$"); | 15 final _terseRegExp = new RegExp(r"(-patch)?(/.*)?$"); |
| 16 | 16 |
| 17 /// A RegExp to match V8's stack traces. |
| 18 /// |
| 19 /// V8's traces start with a line that's either just "Error" or else is a |
| 20 /// description of the exception that occurred. That description can be multiple |
| 21 /// lines, so we just look for any line other than the first that begins with |
| 22 /// four spaces and "at". |
| 23 final _v8Trace = new RegExp(r"\n at "); |
| 24 |
| 17 /// A RegExp to match Firefox's stack traces. | 25 /// A RegExp to match Firefox's stack traces. |
| 18 /// | 26 /// |
| 19 /// Firefox's trace frames start with the name of the function in which the | 27 /// Firefox's trace frames start with the name of the function in which the |
| 20 /// error occurred, possibly including its parameters inside `()`. For example, | 28 /// error occurred, possibly including its parameters inside `()`. For example, |
| 21 /// `.VW.call$0("arg")@http://pub.dartlang.org/stuff.dart.js:560`. | 29 /// `.VW.call$0("arg")@http://pub.dartlang.org/stuff.dart.js:560`. |
| 22 final _firefoxTrace = new RegExp(r"^([.0-9A-Za-z_$/<]|\(.*\))*@"); | 30 final _firefoxTrace = new RegExp(r"^([.0-9A-Za-z_$/<]|\(.*\))*@"); |
| 23 | 31 |
| 24 /// A RegExp to match this package's stack traces. | 32 /// A RegExp to match this package's stack traces. |
| 25 final _friendlyTrace = new RegExp(r"^[^\s]+( \d+:\d+)?\s+[^\s]+($|\n)"); | 33 final _friendlyTrace = new RegExp(r"^[^\s]+( \d+:\d+)?\s+[^\s]+($|\n)"); |
| 26 | 34 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 return new LazyTrace(() => new Trace.parse(trace.toString())); | 75 return new LazyTrace(() => new Trace.parse(trace.toString())); |
| 68 } | 76 } |
| 69 | 77 |
| 70 /// Parses a string representation of a stack trace. | 78 /// Parses a string representation of a stack trace. |
| 71 /// | 79 /// |
| 72 /// [trace] should be formatted in the same way as a Dart VM or browser stack | 80 /// [trace] should be formatted in the same way as a Dart VM or browser stack |
| 73 /// trace. | 81 /// trace. |
| 74 factory Trace.parse(String trace) { | 82 factory Trace.parse(String trace) { |
| 75 try { | 83 try { |
| 76 if (trace.isEmpty) return new Trace(<Frame>[]); | 84 if (trace.isEmpty) return new Trace(<Frame>[]); |
| 77 if (trace.startsWith("Error\n")) return new Trace.parseV8(trace); | 85 if (trace.contains(_v8Trace)) return new Trace.parseV8(trace); |
| 78 if (trace.contains(_firefoxTrace)) return new Trace.parseFirefox(trace); | 86 if (trace.contains(_firefoxTrace)) return new Trace.parseFirefox(trace); |
| 79 if (trace.contains(_friendlyTrace)) return new Trace.parseFriendly(trace); | 87 if (trace.contains(_friendlyTrace)) return new Trace.parseFriendly(trace); |
| 80 | 88 |
| 81 // Default to parsing the stack trace as a VM trace. This is also hit on | 89 // Default to parsing the stack trace as a VM trace. This is also hit on |
| 82 // IE and Safari, where the stack trace is just an empty string (issue | 90 // IE and Safari, where the stack trace is just an empty string (issue |
| 83 // 11257). | 91 // 11257). |
| 84 return new Trace.parseVM(trace); | 92 return new Trace.parseVM(trace); |
| 85 } on FormatException catch (error) { | 93 } on FormatException catch (error) { |
| 86 throw new FormatException('${error.message}\nStack trace:\n$trace'); | 94 throw new FormatException('${error.message}\nStack trace:\n$trace'); |
| 87 } | 95 } |
| 88 } | 96 } |
| 89 | 97 |
| 90 /// Parses a string representation of a Dart VM stack trace. | 98 /// Parses a string representation of a Dart VM stack trace. |
| 91 Trace.parseVM(String trace) | 99 Trace.parseVM(String trace) |
| 92 : this(trace.trim().split("\n").map((line) => new Frame.parseVM(line))); | 100 : this(trace.trim().split("\n").map((line) => new Frame.parseVM(line))); |
| 93 | 101 |
| 94 /// Parses a string representation of a Chrome/V8 stack trace. | 102 /// Parses a string representation of a Chrome/V8 stack trace. |
| 95 Trace.parseV8(String trace) | 103 Trace.parseV8(String trace) |
| 96 : this(trace.split("\n").skip(1).map((line) => new Frame.parseV8(line))); | 104 : this(trace.split("\n").skip(1) |
| 105 // It's possible that an Exception's description contains a line that |
| 106 // looks like a V8 trace line, which will screw this up. |
| 107 // Unfortunately, that's impossible to detect. |
| 108 .skipWhile((line) => !line.startsWith(" at ")) |
| 109 .map((line) => new Frame.parseV8(line))); |
| 97 | 110 |
| 98 /// Parses a string representation of a Firefox stack trace. | 111 /// Parses a string representation of a Firefox stack trace. |
| 99 Trace.parseFirefox(String trace) | 112 Trace.parseFirefox(String trace) |
| 100 : this(trace.trim().split("\n") | 113 : this(trace.trim().split("\n") |
| 101 .map((line) => new Frame.parseFirefox(line))); | 114 .map((line) => new Frame.parseFirefox(line))); |
| 102 | 115 |
| 103 /// Parses this package's a string representation of a stack trace. | 116 /// Parses this package's a string representation of a stack trace. |
| 104 Trace.parseFriendly(String trace) | 117 Trace.parseFriendly(String trace) |
| 105 : this(trace.trim().split("\n") | 118 : this(trace.trim().split("\n") |
| 106 .map((line) => new Frame.parseFriendly(line))); | 119 .map((line) => new Frame.parseFriendly(line))); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 // Figure out the longest path so we know how much to pad. | 168 // Figure out the longest path so we know how much to pad. |
| 156 var longest = frames.map((frame) => frame.location.length) | 169 var longest = frames.map((frame) => frame.location.length) |
| 157 .fold(0, math.max); | 170 .fold(0, math.max); |
| 158 | 171 |
| 159 // Print out the stack trace nicely formatted. | 172 // Print out the stack trace nicely formatted. |
| 160 return frames.map((frame) { | 173 return frames.map((frame) { |
| 161 return '${padRight(frame.location, longest)} ${frame.member}\n'; | 174 return '${padRight(frame.location, longest)} ${frame.member}\n'; |
| 162 }).join(); | 175 }).join(); |
| 163 } | 176 } |
| 164 } | 177 } |
| OLD | NEW |