OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library trace; |
| 6 |
| 7 import 'dart:collection'; |
| 8 import 'dart:math' as math; |
| 9 |
| 10 import 'chain.dart'; |
| 11 import 'frame.dart'; |
| 12 import 'lazy_trace.dart'; |
| 13 import 'utils.dart'; |
| 14 import 'vm_trace.dart'; |
| 15 |
| 16 final _terseRegExp = new RegExp(r"(-patch)?(/.*)?$"); |
| 17 |
| 18 /// A RegExp to match V8's stack traces. |
| 19 /// |
| 20 /// V8's traces start with a line that's either just "Error" or else is a |
| 21 /// description of the exception that occurred. That description can be multiple |
| 22 /// lines, so we just look for any line other than the first that begins with |
| 23 /// three or four spaces and "at". |
| 24 final _v8Trace = new RegExp(r"\n ?at "); |
| 25 |
| 26 /// A RegExp to match indidual lines of V8's stack traces. |
| 27 /// |
| 28 /// This is intended to filter out the leading exception details of the trace |
| 29 /// though it is possible for the message to match this as well. |
| 30 final _v8TraceLine = new RegExp(r" ?at "); |
| 31 |
| 32 /// A RegExp to match Firefox and Safari's stack traces. |
| 33 /// |
| 34 /// Firefox and Safari have very similar stack trace formats, so we use the same |
| 35 /// logic for parsing them. |
| 36 /// |
| 37 /// Firefox's trace frames start with the name of the function in which the |
| 38 /// error occurred, possibly including its parameters inside `()`. For example, |
| 39 /// `.VW.call$0("arg")@http://pub.dartlang.org/stuff.dart.js:560`. |
| 40 /// |
| 41 /// Safari traces occasionally don't include the initial method name followed by |
| 42 /// "@", and they always have both the line and column number (or just a |
| 43 /// trailing colon if no column number is available). They can also contain |
| 44 /// empty lines or lines consisting only of `[native code]`. |
| 45 final _firefoxSafariTrace = new RegExp( |
| 46 r"^" |
| 47 r"(" // Member description. Not present in some Safari frames. |
| 48 r"([.0-9A-Za-z_$/<]|\(.*\))*" // Member name and arguments. |
| 49 r"@" |
| 50 r")?" |
| 51 r"[^\s]*" // Frame URL. |
| 52 r":\d*" // Line or column number. Some older frames only have a line number. |
| 53 r"$", multiLine: true); |
| 54 |
| 55 /// A RegExp to match this package's stack traces. |
| 56 final _friendlyTrace = new RegExp(r"^[^\s]+( \d+(:\d+)?)?[ \t]+[^\s]+$", |
| 57 multiLine: true); |
| 58 |
| 59 /// A stack trace, comprised of a list of stack frames. |
| 60 class Trace implements StackTrace { |
| 61 /// The stack frames that comprise this stack trace. |
| 62 final List<Frame> frames; |
| 63 |
| 64 /// Returns a human-readable representation of [stackTrace]. If [terse] is |
| 65 /// set, this folds together multiple stack frames from the Dart core |
| 66 /// libraries, so that only the core library method directly called from user |
| 67 /// code is visible (see [Trace.terse]). |
| 68 static String format(StackTrace stackTrace, {bool terse: true}) { |
| 69 var trace = new Trace.from(stackTrace); |
| 70 if (terse) trace = trace.terse; |
| 71 return trace.toString(); |
| 72 } |
| 73 |
| 74 /// Returns the current stack trace. |
| 75 /// |
| 76 /// By default, the first frame of this trace will be the line where |
| 77 /// [Trace.current] is called. If [level] is passed, the trace will start that |
| 78 /// many frames up instead. |
| 79 factory Trace.current([int level=0]) { |
| 80 if (level < 0) { |
| 81 throw new ArgumentError("Argument [level] must be greater than or equal " |
| 82 "to 0."); |
| 83 } |
| 84 |
| 85 try { |
| 86 throw ''; |
| 87 } catch (_, nativeTrace) { |
| 88 var trace = new Trace.from(nativeTrace); |
| 89 return new LazyTrace(() => new Trace(trace.frames.skip(level + 1))); |
| 90 } |
| 91 } |
| 92 |
| 93 /// Returns a new stack trace containing the same data as [trace]. |
| 94 /// |
| 95 /// If [trace] is a native [StackTrace], its data will be parsed out; if it's |
| 96 /// a [Trace], it will be returned as-is. |
| 97 factory Trace.from(StackTrace trace) { |
| 98 // Normally explicitly validating null arguments is bad Dart style, but here |
| 99 // the natural failure will only occur when the LazyTrace is materialized, |
| 100 // and we want to provide an error that's more local to the actual problem. |
| 101 if (trace == null) { |
| 102 throw new ArgumentError("Cannot create a Trace from null."); |
| 103 } |
| 104 |
| 105 if (trace is Trace) return trace; |
| 106 if (trace is Chain) return trace.toTrace(); |
| 107 return new LazyTrace(() => new Trace.parse(trace.toString())); |
| 108 } |
| 109 |
| 110 /// Parses a string representation of a stack trace. |
| 111 /// |
| 112 /// [trace] should be formatted in the same way as a Dart VM or browser stack |
| 113 /// trace. |
| 114 factory Trace.parse(String trace) { |
| 115 try { |
| 116 if (trace.isEmpty) return new Trace(<Frame>[]); |
| 117 if (trace.contains(_v8Trace)) return new Trace.parseV8(trace); |
| 118 if (trace.contains(_firefoxSafariTrace)) { |
| 119 return new Trace.parseFirefox(trace); |
| 120 } |
| 121 if (trace.contains(_friendlyTrace)) { |
| 122 return new Trace.parseFriendly(trace); |
| 123 } |
| 124 |
| 125 // Default to parsing the stack trace as a VM trace. This is also hit on |
| 126 // IE and Safari, where the stack trace is just an empty string (issue |
| 127 // 11257). |
| 128 return new Trace.parseVM(trace); |
| 129 } on FormatException catch (error) { |
| 130 throw new FormatException('${error.message}\nStack trace:\n$trace'); |
| 131 } |
| 132 } |
| 133 |
| 134 /// Parses a string representation of a Dart VM stack trace. |
| 135 Trace.parseVM(String trace) |
| 136 : this(trace.trim().split("\n"). |
| 137 // TODO(nweiz): remove this when issue 15920 is fixed. |
| 138 where((line) => line.isNotEmpty). |
| 139 map((line) => new Frame.parseVM(line))); |
| 140 |
| 141 /// Parses a string representation of a Chrome/V8 stack trace. |
| 142 Trace.parseV8(String trace) |
| 143 : this(trace.split("\n").skip(1) |
| 144 // It's possible that an Exception's description contains a line that |
| 145 // looks like a V8 trace line, which will screw this up. |
| 146 // Unfortunately, that's impossible to detect. |
| 147 .skipWhile((line) => !line.startsWith(_v8TraceLine)) |
| 148 .map((line) => new Frame.parseV8(line))); |
| 149 |
| 150 /// Parses a string representation of an Internet Explorer stack trace. |
| 151 /// |
| 152 /// IE10+ traces look just like V8 traces. Prior to IE10, stack traces can't |
| 153 /// be retrieved. |
| 154 Trace.parseIE(String trace) |
| 155 : this.parseV8(trace); |
| 156 |
| 157 /// Parses a string representation of a Firefox stack trace. |
| 158 Trace.parseFirefox(String trace) |
| 159 : this(trace.trim().split("\n") |
| 160 .where((line) => line.isNotEmpty && line != '[native code]') |
| 161 .map((line) => new Frame.parseFirefox(line))); |
| 162 |
| 163 /// Parses a string representation of a Safari stack trace. |
| 164 Trace.parseSafari(String trace) |
| 165 : this.parseFirefox(trace); |
| 166 |
| 167 /// Parses a string representation of a Safari 6.1+ stack trace. |
| 168 @Deprecated("Use Trace.parseSafari instead.") |
| 169 Trace.parseSafari6_1(String trace) |
| 170 : this.parseSafari(trace); |
| 171 |
| 172 /// Parses a string representation of a Safari 6.0 stack trace. |
| 173 @Deprecated("Use Trace.parseSafari instead.") |
| 174 Trace.parseSafari6_0(String trace) |
| 175 : this(trace.trim().split("\n") |
| 176 .where((line) => line != '[native code]') |
| 177 .map((line) => new Frame.parseFirefox(line))); |
| 178 |
| 179 /// Parses this package's string representation of a stack trace. |
| 180 /// |
| 181 /// This also parses string representations of [Chain]s. They parse to the |
| 182 /// same trace that [Chain.toTrace] would return. |
| 183 Trace.parseFriendly(String trace) |
| 184 : this(trace.trim().split("\n") |
| 185 // Filter out asynchronous gaps from [Chain]s. |
| 186 .where((line) => !line.startsWith('=====')) |
| 187 .map((line) => new Frame.parseFriendly(line))); |
| 188 |
| 189 /// Returns a new [Trace] comprised of [frames]. |
| 190 Trace(Iterable<Frame> frames) |
| 191 : frames = new UnmodifiableListView<Frame>(frames.toList()); |
| 192 |
| 193 /// Returns a VM-style [StackTrace] object. |
| 194 /// |
| 195 /// The return value's [toString] method will always return a string |
| 196 /// representation in the Dart VM's stack trace format, regardless of what |
| 197 /// platform is being used. |
| 198 StackTrace get vmTrace => new VMTrace(frames); |
| 199 |
| 200 /// Returns a terser version of [this]. |
| 201 /// |
| 202 /// This is accomplished by folding together multiple stack frames from the |
| 203 /// core library or from this package, as in [foldFrames]. Remaining core |
| 204 /// library frames have their libraries, "-patch" suffixes, and line numbers |
| 205 /// removed. |
| 206 Trace get terse { |
| 207 return new Trace(foldFrames((frame) { |
| 208 return frame.isCore || frame.package == 'stack_trace'; |
| 209 }).frames.map((frame) { |
| 210 if (!frame.isCore) return frame; |
| 211 var library = frame.library.replaceAll(_terseRegExp, ''); |
| 212 return new Frame(Uri.parse(library), null, null, frame.member); |
| 213 })); |
| 214 } |
| 215 |
| 216 /// Returns a new [Trace] based on [this] where multiple stack frames matching |
| 217 /// [predicate] are folded together. This means that whenever there are |
| 218 /// multiple frames in a row that match [predicate], only the last one is |
| 219 /// kept. |
| 220 /// |
| 221 /// This is useful for limiting the amount of library code that appears in a |
| 222 /// stack trace by only showing user code and code that's called by user code. |
| 223 Trace foldFrames(bool predicate(Frame frame)) { |
| 224 var newFrames = <Frame>[]; |
| 225 for (var frame in frames.reversed) { |
| 226 if (!predicate(frame)) { |
| 227 newFrames.add(frame); |
| 228 } else if (newFrames.isEmpty || !predicate(newFrames.last)) { |
| 229 newFrames.add(new Frame( |
| 230 frame.uri, frame.line, frame.column, frame.member)); |
| 231 } |
| 232 } |
| 233 |
| 234 return new Trace(newFrames.reversed); |
| 235 } |
| 236 |
| 237 /// Returns a human-readable string representation of [this]. |
| 238 String toString() { |
| 239 // Figure out the longest path so we know how much to pad. |
| 240 var longest = frames.map((frame) => frame.location.length) |
| 241 .fold(0, math.max); |
| 242 |
| 243 // Print out the stack trace nicely formatted. |
| 244 return frames.map((frame) { |
| 245 return '${padRight(frame.location, longest)} ${frame.member}\n'; |
| 246 }).join(); |
| 247 } |
| 248 } |
OLD | NEW |