Capture Stack Trace during execution

2,178 views
Skip to first unread message

kst...@gmail.com

unread,
Sep 9, 2011, 2:13:55 PM9/9/11
to google-chrome-...@googlegroups.com
Is there a way to capture stack trace during execution of code?  Ideally, I'd like to output the stack trace to the console to help debug and understand the code at various execution times.

Thanks,

Karl..

pfeldman

unread,
Sep 9, 2011, 2:16:16 PM9/9/11
to Google Chrome Developer Tools
You can use console.trace(). See http://code.google.com/chrome/devtools/docs/scripts-exceptions.html
for more info.

kst...@gmail.com

unread,
Sep 9, 2011, 2:54:23 PM9/9/11
to google-chrome-...@googlegroups.com
That was a huge help, and so obvious!  Thanks.

Tim Meadowcroft

unread,
Sep 9, 2011, 3:04:46 PM9/9/11
to google-chrome-...@googlegroups.com
You can also capture the trace as a string in case you want to log it elsewhere

  var getTrace = function(top) {
            var obj = {};
            // in Chrome (or V8) this will return a string of the current call stack up to
            // function top or this getTrace call itself
            if ((typeof Error !== "undefined") && Error.captureStackTrace) {
                Error.captureStackTrace(obj, top || getTrace);
            }
            return obj.stack;
  };

This will return a string of the stack up to the call to getTrace itself (or up to another function if you pass in a parameter for top).

I tried putting this into a window.onError handler for this chrome extension I wrote that gives you a toolbar button to indicate javascript errors when the dev tools are shut


but you only get the stack trace of the engine calling the onError handler itself.. :(

But the routine can still be handy in your own code to get the trace as a string so you can pop it up in an message when the dev tools are closed.

--
T



Reply all
Reply to author
Forward
0 new messages