1 // Domain Public by Eric Wendelin http://eriwen.com/ (2008)
2 // Luke Smith http://lucassmith.name/ (2008)
3 // Loic Dachary <loic@dachary.org> (2008)
4 // Johan Euphrosine <proppy@aminche.com> (2008)
5 // Oyvind Sean Kinsey http://kinsey.no/blog (2010)
6 // Victor Homyakov <victor-homyakov@users.sourceforge.net> (2010)
9 * Main function giving a function stack trace with a forced or passed in Error
11 * @cfg {Error} e The error to create a stacktrace from (optional)
12 * @cfg {Boolean} guess If we should try to resolve the names of anonymous functions
13 * @return {Array} of Strings with functions, lines, files, and arguments where possible
15 function printStackTrace(options) {
16 options = options || {guess: true};
17 var ex = options.e || null, guess = !!options.guess;
18 var p = new printStackTrace.implementation(), result = p.run(ex);
19 return (guess) ? p.guessAnonymousFunctions(result) : result;
22 printStackTrace.implementation = function() {
25 printStackTrace.implementation.prototype = {
27 * @param {Error} ex The error to create a stacktrace from (optional)
28 * @param {String} mode Forced mode (optional, mostly for unit tests)
30 run: function(ex, mode) {
31 ex = ex || this.createException();
32 // examine exception properties w/o debugger
33 //for (var prop in ex) {alert("Ex['" + prop + "']=" + ex[prop]);}
34 mode = mode || this.mode(ex);
35 if (mode === 'other') {
36 return this.other(arguments.callee);
38 return this[mode](ex);
42 createException: function() {
51 * Mode could differ for different exception, e.g.
52 * exceptions in Chrome may or may not have arguments or stack.
54 * @return {String} mode of operation for the exception
57 if (e['arguments'] && e.stack) {
59 } else if (typeof e.message === 'string' && typeof window !== 'undefined' && window.opera) {
60 // e.message.indexOf("Backtrace:") > -1 -> opera
61 // !e.stacktrace -> opera
63 return 'opera9'; // use e.message
65 // 'opera#sourceloc' in e -> opera9, opera10a
66 if (e.message.indexOf('\n') > -1 && e.message.split('\n').length > e.stacktrace.split('\n').length) {
67 return 'opera9'; // use e.message
69 // e.stacktrace && !e.stack -> opera10a
71 return 'opera10a'; // use e.stacktrace
73 // e.stacktrace && e.stack -> opera10b
74 if (e.stacktrace.indexOf("called from line") < 0) {
75 return 'opera10b'; // use e.stacktrace, format differs from 'opera10a'
77 // e.stacktrace && e.stack -> opera11
78 return 'opera11'; // use e.stacktrace, format differs from 'opera10a', 'opera10b'
86 * Given a context, function name, and callback function, overwrite it so that it calls
87 * printStackTrace() first with a callback and then runs the rest of the body.
89 * @param {Object} context of execution (e.g. window)
90 * @param {String} functionName to instrument
91 * @param {Function} function to call with a stack trace on invocation
93 instrumentFunction: function(context, functionName, callback) {
94 context = context || window;
95 var original = context[functionName];
96 context[functionName] = function instrumented() {
97 callback.call(this, printStackTrace().slice(4));
98 return context[functionName]._instrumented.apply(this, arguments);
100 context[functionName]._instrumented = original;
104 * Given a context and function name of a function that has been
105 * instrumented, revert the function to it's original (non-instrumented)
108 * @param {Object} context of execution (e.g. window)
109 * @param {String} functionName to de-instrument
111 deinstrumentFunction: function(context, functionName) {
112 if (context[functionName].constructor === Function &&
113 context[functionName]._instrumented &&
114 context[functionName]._instrumented.constructor === Function) {
115 context[functionName] = context[functionName]._instrumented;
120 * Given an Error object, return a formatted Array based on Chrome's stack string.
122 * @param e - Error object to inspect
123 * @return Array<String> of function calls, files and line numbers
125 chrome: function(e) {
126 var stack = (e.stack + '\n').replace(/^\S[^\(]+?[\n$]/gm, '').
127 replace(/^\s+at\s+/gm, '').
128 replace(/^([^\(]+?)([\n$])/gm, '{anonymous}()@$1$2').
129 replace(/^Object.<anonymous>\s*\(([^\)]+)\)/gm, '{anonymous}()@$1').split('\n');
135 * Given an Error object, return a formatted Array based on Firefox's stack string.
137 * @param e - Error object to inspect
138 * @return Array<String> of function calls, files and line numbers
140 firefox: function(e) {
141 return e.stack.replace(/(?:\n@:0)?\s+$/m, '').replace(/^\(/gm, '{anonymous}(').split('\n');
144 opera11: function(e) {
145 // "Error thrown at line 42, column 12 in <anonymous function>() in file://localhost/G:/js/stacktrace.js:\n"
146 // "Error thrown at line 42, column 12 in <anonymous function: createException>() in file://localhost/G:/js/stacktrace.js:\n"
147 // "called from line 7, column 4 in bar(n) in file://localhost/G:/js/test/functional/testcase1.html:\n"
148 // "called from line 15, column 3 in file://localhost/G:/js/test/functional/testcase1.html:\n"
149 var ANON = '{anonymous}', lineRE = /^.*line (\d+), column (\d+)(?: in (.+))? in (\S+):$/;
150 var lines = e.stacktrace.split('\n'), result = [];
152 for (var i = 0, len = lines.length; i < len; i += 2) {
153 var match = lineRE.exec(lines[i]);
155 var location = match[4] + ':' + match[1] + ':' + match[2];
156 var fnName = match[3] || "global code";
157 fnName = fnName.replace(/<anonymous function: (\S+)>/, "$1").replace(/<anonymous function>/, ANON);
158 result.push(fnName + '@' + location + ' -- ' + lines[i + 1].replace(/^\s+/, ''));
165 opera10b: function(e) {
166 // "<anonymous function: run>([arguments not available])@file://localhost/G:/js/stacktrace.js:27\n" +
167 // "printStackTrace([arguments not available])@file://localhost/G:/js/stacktrace.js:18\n" +
168 // "@file://localhost/G:/js/test/functional/testcase1.html:15"
169 var ANON = '{anonymous}', lineRE = /^(.*)@(.+):(\d+)$/;
170 var lines = e.stacktrace.split('\n'), result = [];
172 for (var i = 0, len = lines.length; i < len; i++) {
173 var match = lineRE.exec(lines[i]);
175 var fnName = match[1]? (match[1] + '()') : "global code";
176 result.push(fnName + '@' + match[2] + ':' + match[3]);
184 * Given an Error object, return a formatted Array based on Opera 10's stacktrace string.
186 * @param e - Error object to inspect
187 * @return Array<String> of function calls, files and line numbers
189 opera10a: function(e) {
190 // " Line 27 of linked script file://localhost/G:/js/stacktrace.js\n"
191 // " Line 11 of inline#1 script in file://localhost/G:/js/test/functional/testcase1.html: In function foo\n"
192 var ANON = '{anonymous}', lineRE = /Line (\d+).*script (?:in )?(\S+)(?:: In function (\S+))?$/i;
193 var lines = e.stacktrace.split('\n'), result = [];
195 for (var i = 0, len = lines.length; i < len; i += 2) {
196 var match = lineRE.exec(lines[i]);
198 var fnName = match[3] || ANON;
199 result.push(fnName + '()@' + match[2] + ':' + match[1] + ' -- ' + lines[i + 1].replace(/^\s+/, ''));
206 // Opera 7.x-9.2x only!
207 opera9: function(e) {
208 // " Line 43 of linked script file://localhost/G:/js/stacktrace.js\n"
209 // " Line 7 of inline#1 script in file://localhost/G:/js/test/functional/testcase1.html\n"
210 var ANON = '{anonymous}', lineRE = /Line (\d+).*script (?:in )?(\S+)/i;
211 var lines = e.message.split('\n'), result = [];
213 for (var i = 2, len = lines.length; i < len; i += 2) {
214 var match = lineRE.exec(lines[i]);
216 result.push(ANON + '()@' + match[2] + ':' + match[1] + ' -- ' + lines[i + 1].replace(/^\s+/, ''));
223 // Safari, IE, and others
224 other: function(curr) {
225 var ANON = '{anonymous}', fnRE = /function\s*([\w\-$]+)?\s*\(/i, stack = [], fn, args, maxStackSize = 10;
226 while (curr && stack.length < maxStackSize) {
227 fn = fnRE.test(curr.toString()) ? RegExp.$1 || ANON : ANON;
228 args = Array.prototype.slice.call(curr['arguments'] || []);
229 stack[stack.length] = fn + '(' + this.stringifyArguments(args) + ')';
236 * Given arguments array as a String, subsituting type names for non-string types.
238 * @param {Arguments} object
239 * @return {Array} of Strings with stringified arguments
241 stringifyArguments: function(args) {
243 var slice = Array.prototype.slice;
244 for (var i = 0; i < args.length; ++i) {
246 if (arg === undefined) {
247 result[i] = 'undefined';
248 } else if (arg === null) {
250 } else if (arg.constructor) {
251 if (arg.constructor === Array) {
252 if (arg.length < 3) {
253 result[i] = '[' + this.stringifyArguments(arg) + ']';
255 result[i] = '[' + this.stringifyArguments(slice.call(arg, 0, 1)) + '...' + this.stringifyArguments(slice.call(arg, -1)) + ']';
257 } else if (arg.constructor === Object) {
258 result[i] = '#object';
259 } else if (arg.constructor === Function) {
260 result[i] = '#function';
261 } else if (arg.constructor === String) {
262 result[i] = '"' + arg + '"';
263 } else if (arg.constructor === Number) {
268 return result.join(',');
274 * @return the text from a given URL
276 ajax: function(url) {
277 var req = this.createXMLHTTPObject();
280 req.open('GET', url, false);
282 return req.responseText;
290 * Try XHR methods in order and store XHR factory.
292 * @return <Function> XHR function or equivalent
294 createXMLHTTPObject: function() {
295 var xmlhttp, XMLHttpFactories = [
297 return new XMLHttpRequest();
299 return new ActiveXObject('Msxml2.XMLHTTP');
301 return new ActiveXObject('Msxml3.XMLHTTP');
303 return new ActiveXObject('Microsoft.XMLHTTP');
306 for (var i = 0; i < XMLHttpFactories.length; i++) {
308 xmlhttp = XMLHttpFactories[i]();
309 // Use memoization to cache the factory
310 this.createXMLHTTPObject = XMLHttpFactories[i];
318 * Given a URL, check if it is in the same domain (so we can get the source
321 * @param url <String> source url
322 * @return False if we need a cross-domain request
324 isSameDomain: function(url) {
325 return url.indexOf(location.hostname) !== -1;
329 * Get source code from given URL if in the same domain.
331 * @param url <String> JS source URL
332 * @return <Array> Array of source code lines
334 getSource: function(url) {
335 // TODO reuse source from script tags?
336 if (!(url in this.sourceCache)) {
337 this.sourceCache[url] = this.ajax(url).split('\n');
339 return this.sourceCache[url];
342 guessAnonymousFunctions: function(stack) {
343 for (var i = 0; i < stack.length; ++i) {
344 var reStack = /\{anonymous\}\(.*\)@(.*)/,
345 reRef = /^(.*?)(?::(\d+))(?::(\d+))?(?: -- .+)?$/,
346 frame = stack[i], ref = reStack.exec(frame);
349 var m = reRef.exec(ref[1]), file = m[1],
350 lineno = m[2], charno = m[3] || 0;
351 if (file && this.isSameDomain(file) && lineno) {
352 var functionName = this.guessAnonymousFunction(file, lineno, charno);
353 stack[i] = frame.replace('{anonymous}', functionName);
360 guessAnonymousFunction: function(url, lineNo, charNo) {
363 ret = this.findFunctionName(this.getSource(url), lineNo);
365 ret = 'getSource failed with url: ' + url + ', exception: ' + e.toString();
370 findFunctionName: function(source, lineNo) {
371 // FIXME findFunctionName fails for compressed source
372 // (more than one function on the same line)
373 // TODO use captured args
374 // function {name}({args}) m[1]=name m[2]=args
375 var reFunctionDeclaration = /function\s+([^(]*?)\s*\(([^)]*)\)/;
376 // {name} = function ({args}) TODO args capture
377 // /['"]?([0-9A-Za-z_]+)['"]?\s*[:=]\s*function(?:[^(]*)/
378 var reFunctionExpression = /['"]?([0-9A-Za-z_]+)['"]?\s*[:=]\s*function\b/;
380 var reFunctionEvaluation = /['"]?([0-9A-Za-z_]+)['"]?\s*[:=]\s*(?:eval|new Function)\b/;
381 // Walk backwards in the source lines until we find
382 // the line which matches one of the patterns above
383 var code = "", line, maxLines = Math.min(lineNo, 20), m, commentPos;
384 for (var i = 0; i < maxLines; ++i) {
385 // lineNo is 1-based, source[] is 0-based
386 line = source[lineNo - i - 1];
387 commentPos = line.indexOf('//');
388 if (commentPos >= 0) {
389 line = line.substr(0, commentPos);
391 // TODO check other types of comments? Commented code may lead to false positive
394 m = reFunctionExpression.exec(code);
398 m = reFunctionDeclaration.exec(code);
400 //return m[1] + "(" + (m[2] || "") + ")";
403 m = reFunctionEvaluation.exec(code);