| 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'; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 /// lines, so we just look for any line other than the first that begins with | 21 /// lines, so we just look for any line other than the first that begins with |
| 22 /// three or four spaces and "at". | 22 /// three or four spaces and "at". |
| 23 final _v8Trace = new RegExp(r"\n ?at "); | 23 final _v8Trace = new RegExp(r"\n ?at "); |
| 24 | 24 |
| 25 /// A RegExp to match indidual lines of V8's stack traces. | 25 /// A RegExp to match indidual lines of V8's stack traces. |
| 26 /// | 26 /// |
| 27 /// This is intended to filter out the leading exception details of the trace | 27 /// This is intended to filter out the leading exception details of the trace |
| 28 /// though it is possible for the message to match this as well. | 28 /// though it is possible for the message to match this as well. |
| 29 final _v8TraceLine = new RegExp(r" ?at "); | 29 final _v8TraceLine = new RegExp(r" ?at "); |
| 30 | 30 |
| 31 /// A RegExp to match Safari's stack traces. |
| 32 /// |
| 33 /// Prior to version 6, Safari's stack traces were uncapturable. In v6 they were |
| 34 /// almost identical to Firefox traces, and so are handled by the Firefox code. |
| 35 /// In v6.1+, they have their own format that's similar to Firefox but distinct |
| 36 /// enough to warrant handling separately. |
| 37 /// |
| 38 /// Most notably, Safari traces occasionally don't include the initial method |
| 39 /// name followed by "@", and they always have both the line and column number |
| 40 /// (or just a trailing colon if no column number is available). |
| 41 final _safariTrace = new RegExp(r"^([0-9A-Za-z_$]*@)?.*:\d*:\d*$", |
| 42 multiLine: true); |
| 43 |
| 31 /// A RegExp to match Firefox's stack traces. | 44 /// A RegExp to match Firefox's stack traces. |
| 32 /// | 45 /// |
| 33 /// Firefox's trace frames start with the name of the function in which the | 46 /// Firefox's trace frames start with the name of the function in which the |
| 34 /// error occurred, possibly including its parameters inside `()`. For example, | 47 /// error occurred, possibly including its parameters inside `()`. For example, |
| 35 /// `.VW.call$0("arg")@http://pub.dartlang.org/stuff.dart.js:560`. | 48 /// `.VW.call$0("arg")@http://pub.dartlang.org/stuff.dart.js:560`. |
| 36 final _firefoxTrace = new RegExp(r"^([.0-9A-Za-z_$/<]|\(.*\))*@"); | 49 final _firefoxTrace = new RegExp(r"^([.0-9A-Za-z_$/<]|\(.*\))*@"); |
| 37 | 50 |
| 38 /// A RegExp to match this package's stack traces. | 51 /// A RegExp to match this package's stack traces. |
| 39 final _friendlyTrace = new RegExp(r"^[^\s]+( \d+(:\d+)?)?\s+[^\s]+($|\n)"); | 52 final _friendlyTrace = new RegExp(r"^[^\s]+( \d+(:\d+)?)?[ \t]+[^\s]+$", |
| 53 multiLine: true); |
| 40 | 54 |
| 41 /// A stack trace, comprised of a list of stack frames. | 55 /// A stack trace, comprised of a list of stack frames. |
| 42 class Trace implements StackTrace { | 56 class Trace implements StackTrace { |
| 43 /// The stack frames that comprise this stack trace. | 57 /// The stack frames that comprise this stack trace. |
| 44 final List<Frame> frames; | 58 final List<Frame> frames; |
| 45 | 59 |
| 46 /// Returns a human-readable representation of [stackTrace]. If [terse] is | 60 /// Returns a human-readable representation of [stackTrace]. If [terse] is |
| 47 /// set, this folds together multiple stack frames from the Dart core | 61 /// set, this folds together multiple stack frames from the Dart core |
| 48 /// libraries, so that only the core library method directly called from user | 62 /// libraries, so that only the core library method directly called from user |
| 49 /// code is visible (see [Trace.terse]). | 63 /// code is visible (see [Trace.terse]). |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 } | 96 } |
| 83 | 97 |
| 84 /// Parses a string representation of a stack trace. | 98 /// Parses a string representation of a stack trace. |
| 85 /// | 99 /// |
| 86 /// [trace] should be formatted in the same way as a Dart VM or browser stack | 100 /// [trace] should be formatted in the same way as a Dart VM or browser stack |
| 87 /// trace. | 101 /// trace. |
| 88 factory Trace.parse(String trace) { | 102 factory Trace.parse(String trace) { |
| 89 try { | 103 try { |
| 90 if (trace.isEmpty) return new Trace(<Frame>[]); | 104 if (trace.isEmpty) return new Trace(<Frame>[]); |
| 91 if (trace.contains(_v8Trace)) return new Trace.parseV8(trace); | 105 if (trace.contains(_v8Trace)) return new Trace.parseV8(trace); |
| 92 // Valid Safari traces are a superset of valid Firefox traces. | 106 // Safari 6.1+ traces could be misinterpreted as Firefox traces, so we |
| 93 if (trace.contains(_firefoxTrace)) return new Trace.parseSafari(trace); | 107 // check for them first. |
| 94 if (trace.contains(_friendlyTrace)) return new Trace.parseFriendly(trace); | 108 if (trace.contains(_safariTrace)) return new Trace.parseSafari6_1(trace); |
| 109 // Safari 6.0 traces are a superset of Firefox traces, so we parse those |
| 110 // two together. |
| 111 if (trace.contains(_firefoxTrace)) return new Trace.parseSafari6_0(trace); |
| 112 if (trace.contains(_friendlyTrace)) { |
| 113 return new Trace.parseFriendly(trace); |
| 114 } |
| 95 | 115 |
| 96 // Default to parsing the stack trace as a VM trace. This is also hit on | 116 // Default to parsing the stack trace as a VM trace. This is also hit on |
| 97 // IE and Safari, where the stack trace is just an empty string (issue | 117 // IE and Safari, where the stack trace is just an empty string (issue |
| 98 // 11257). | 118 // 11257). |
| 99 return new Trace.parseVM(trace); | 119 return new Trace.parseVM(trace); |
| 100 } on FormatException catch (error) { | 120 } on FormatException catch (error) { |
| 101 throw new FormatException('${error.message}\nStack trace:\n$trace'); | 121 throw new FormatException('${error.message}\nStack trace:\n$trace'); |
| 102 } | 122 } |
| 103 } | 123 } |
| 104 | 124 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 122 Trace.parseIE(String trace) | 142 Trace.parseIE(String trace) |
| 123 : this.parseV8(trace); | 143 : this.parseV8(trace); |
| 124 | 144 |
| 125 /// Parses a string representation of a Firefox stack trace. | 145 /// Parses a string representation of a Firefox stack trace. |
| 126 Trace.parseFirefox(String trace) | 146 Trace.parseFirefox(String trace) |
| 127 : this(trace.trim().split("\n") | 147 : this(trace.trim().split("\n") |
| 128 .map((line) => new Frame.parseFirefox(line))); | 148 .map((line) => new Frame.parseFirefox(line))); |
| 129 | 149 |
| 130 /// Parses a string representation of a Safari stack trace. | 150 /// Parses a string representation of a Safari stack trace. |
| 131 /// | 151 /// |
| 132 /// Safari 6+ stack traces look just like Firefox traces, except that they | 152 /// This will automatically decide between [parseSafari6_0] and |
| 153 /// [parseSafari6_1] based on the contents of [trace]. |
| 154 factory Trace.parseSafari(String trace) { |
| 155 if (trace.contains(_safariTrace)) return new Trace.parseSafari6_1(trace); |
| 156 return new Trace.parseSafari6_0(trace); |
| 157 } |
| 158 |
| 159 /// Parses a string representation of a Safari 6.1+ stack trace. |
| 160 Trace.parseSafari6_1(String trace) |
| 161 : this(trace.trim().split("\n") |
| 162 .map((line) => new Frame.parseSafari6_1(line))); |
| 163 |
| 164 /// Parses a string representation of a Safari 6.0 stack trace. |
| 165 /// |
| 166 /// Safari 6.0 stack traces look just like Firefox traces, except that they |
| 133 /// sometimes (e.g. in isolates) have a "[native code]" frame. We just ignore | 167 /// sometimes (e.g. in isolates) have a "[native code]" frame. We just ignore |
| 134 /// this frame to make the stack format more consistent between browsers. | 168 /// this frame to make the stack format more consistent between browsers. |
| 135 /// Prior to Safari 6, stack traces can't be retrieved. | 169 /// Prior to Safari 6.0, stack traces can't be retrieved. |
| 136 Trace.parseSafari(String trace) | 170 Trace.parseSafari6_0(String trace) |
| 137 : this(trace.trim().split("\n") | 171 : this(trace.trim().split("\n") |
| 138 .where((line) => line != '[native code]') | 172 .where((line) => line != '[native code]') |
| 139 .map((line) => new Frame.parseFirefox(line))); | 173 .map((line) => new Frame.parseFirefox(line))); |
| 140 | 174 |
| 141 /// Parses this package's a string representation of a stack trace. | 175 /// Parses this package's a string representation of a stack trace. |
| 142 Trace.parseFriendly(String trace) | 176 Trace.parseFriendly(String trace) |
| 143 : this(trace.trim().split("\n") | 177 : this(trace.trim().split("\n") |
| 144 .map((line) => new Frame.parseFriendly(line))); | 178 .map((line) => new Frame.parseFriendly(line))); |
| 145 | 179 |
| 146 /// Returns a new [Trace] comprised of [frames]. | 180 /// Returns a new [Trace] comprised of [frames]. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 // Figure out the longest path so we know how much to pad. | 227 // Figure out the longest path so we know how much to pad. |
| 194 var longest = frames.map((frame) => frame.location.length) | 228 var longest = frames.map((frame) => frame.location.length) |
| 195 .fold(0, math.max); | 229 .fold(0, math.max); |
| 196 | 230 |
| 197 // Print out the stack trace nicely formatted. | 231 // Print out the stack trace nicely formatted. |
| 198 return frames.map((frame) { | 232 return frames.map((frame) { |
| 199 return '${padRight(frame.location, longest)} ${frame.member}\n'; | 233 return '${padRight(frame.location, longest)} ${frame.member}\n'; |
| 200 }).join(); | 234 }).join(); |
| 201 } | 235 } |
| 202 } | 236 } |
| OLD | NEW |