close
The Wayback Machine - https://web.archive.org/web/20150907055955/https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
My favorites | Sign in
v8
Project Home Downloads Wiki Issues Source
Search
for
JavaScriptStackTraceApi  
Updated Nov 11, 2014 by machenb...@chromium.org
Comment by n...@znscode.net, May 18, 2011

Is there any way to get exact source line for each stack frame? This would greatly simplify implementation of real C-like ASSERT function.

Comment by tjholowayhuk@gmail.com, Feb 8, 2012

you can implement line etc with this, and C-style assert()s: https://github.com/joyent/node/commit/007b7181e307a8d52970947d2b6432a2addd9ef0

Comment by char...@isomorphic.com, Jun 7, 2012

I'm trying to use an override of prepareStackTrace() to provide more information than Chrome does by default, specifically, the value of the arguments passed to functions in the stack. This works for manually creating an error in order to get the current stack, but not when catching a thrown error and trying to look at the stack where it came from.

Sample code below. The trace alert()d with prefix "trace from before crash" can get arguments for all functions in the stack, the trace alert()d with prefix "trace from catch()" can only get them for functions that are still on the stack at the time the catch() block executes. A third trace is captured inside prepareStackTrace() just in case - this also cannot get arguments.

Is there any way to get these arguments from a catch() block?

If not, could V8 be enhanced to simply call prepareStackTrace() sooner, that is, before the stack frames have been destroyed?

Note I'm aware that if there are recursive calls the best I can get are the arguments to the deepest frame for a given function. The ideal enhancement is really that CallSite? objects support a getArguments() call (and I'd love a getLocalVariables() call as well).

Error.prepareStackTrace = function(error, stack) {
    window.traceInsidePrepare = getTrace({ stack : stack }); 
    return stack;
};

var someObj = {
    someMethod : function () { 
        alert("trace from before crash: " + getTrace(new Error())); 
        crash();
    }
}
function bar(barArg) { someObj.someMethod(); };
function foo(fooArg) { bar("barArgString"); };

function getTrace(e) {
    var stack = e.stack;
    var trace = "";
    for (var i = 0; i < stack.length; i++) {
        var frame = stack[i],
            func = frame.getFunction();
        
        trace += "\r" + frame.getThis() + "." + frame.getFunctionName() +
                "\r    function first arg: " + (func.arguments != null ? func.arguments[0] : "<missing>");
    }
    return trace;
}

try {
    foo("fooArgString");
} catch (e) {
    alert("trace from catch(): " + getTrace(e));
    alert("trace inside prepare: " + window.traceInsidePrepare);
}
Comment by tjholowayhuk@gmail.com, Oct 17, 2012

it's really annoying that you can't get the callsite objects after the error is created... this would be fantastic for logging, IMO this should be part of the ecmascript spec, string stacks are pretty weak

Comment by putramandiri47@gmail.com, Mar 6, 2013

IMO this should be part of the ecmascript spec,string stacks are pretty week

Comment by karl.wes...@gmail.com, Mar 19, 2013

here's a link to how the default StackTrace? formatter looks. I think someone could find it useful: https://github.com/v8/v8/blob/master/src/messages.js#L1050

Comment by deniskem...@gmail.com, Oct 8, 2013
--allow-running-insecure-content
Comment by jayarama...@gmail.com, Nov 18, 2013

PLEASE CORRECT JAVA

Comment by autumnfi...@gmail.com, Jan 27, 2014

Great info keep it coming

Comment by leah070...@gmail.com, Apr 10, 2014

Error.prepareStackTrace = function(error, stack) {

window.traceInsidePrepare = getTrace({ stack : stack }); return stack;
};

var someObj = {

someMethod : function () {
alert("trace from before crash: " + getTrace(new Error())); crash();
}
} function bar(barArg) { someObj.someMethod(); }; function foo(fooArg) { bar("barArgString"); };

function getTrace(e) {

var stack = e.stack; var trace = ""; for (var i = 0; i < stack.length; i++) {
var frame = stacki?,
func = frame.getFunction();
trace += "\r" + frame.getThis() + "." + frame.getFunctionName() +
"\r function first arg: " + (func.arguments != null ? func.arguments0? : "<missing>");
} return trace;
}

try {

foo("fooArgString");
} catch (e) {
alert("trace from catch(): " + getTrace(e)); alert("trace inside prepare: " + window.traceInsidePrepare);
}

Comment by javier...@gmail.com, Sep 23, 2014

I've noticed that getFunction for node 0.11 and above or even the func property on the callsites. Anyone knows something about this?

Comment by kangt...@gmail.com, Apr 16, 2015

Allow me to upload my script here. But still in the process of making

http://www.kangtips.com/2015/04/minuman-berenergi-aman-tidak-berbahaya.html

Comment by glrd...@gmail.com, Jul 3, 2015

I do accept as true with all the ideas you've introduced in your post. They are very convincing and can definitely work. ignou.ac.in Nonetheless, the posts are very quick for newbies.

Comment by masterba...@gmail.com, Aug 30, 2015

Thanks for your information

http://evomovie.com

Comment by anil.sin...@gmail.com, Sep 2 (4 days ago)

Nice and thanks http://www.code-sample.com/


Sign in to add a comment
Powered by Google Project Hosting