Firebug has the ability to log calls to a particular function name. I'm looking for a bug that sometimes stops a page from rendering, but doesn't cause any errors or warnings. The bug only appears about half the time. So how do I get a list of all the function calls for the entire program, or some kind of stack trace for the execution of the entire program?
5 Answers
Firefox provides console.trace()
which is very handy to print the call stack. It is also available in Chrome and IE 11.
Alternatively try something like this:
function print_call_stack() {
var stack = new Error().stack;
console.log("PRINTING CALL STACK");
console.log( stack );
}

- 3,374
- 2
- 20
- 15
-
4Is there a way to increase the length of the stack? That would be very helpful. – Ravi Teja Apr 04 '17 at 11:30
-
✚1 console.warn('[WARN] CALL STACK:', new Error().stack); – user1742529 Jul 06 '18 at 13:23
When i need a stack trace i do the following, maybe you can draw some inspiration from it:
function logStackTrace(levels) {
var callstack = [];
var isCallstackPopulated = false;
try {
i.dont.exist += 0; //doesn't exist- that's the point
} catch (e) {
if (e.stack) { //Firefox / chrome
var lines = e.stack.split('\n');
for (var i = 0, len = lines.length; i < len; i++) {
callstack.push(lines[i]);
}
//Remove call to logStackTrace()
callstack.shift();
isCallstackPopulated = true;
}
else if (window.opera && e.message) { //Opera
var lines = e.message.split('\n');
for (var i = 0, len = lines.length; i < len; i++) {
if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
var entry = lines[i];
//Append next line also since it has the file info
if (lines[i + 1]) {
entry += " at " + lines[i + 1];
i++;
}
callstack.push(entry);
}
}
//Remove call to logStackTrace()
callstack.shift();
isCallstackPopulated = true;
}
}
if (!isCallstackPopulated) { //IE and Safari
var currentFunction = arguments.callee.caller;
while (currentFunction) {
var fn = currentFunction.toString();
var fname = fn.substring(fn.indexOf("function") + 8, fn.indexOf("(")) || "anonymous";
callstack.push(fname);
currentFunction = currentFunction.caller;
}
}
if (levels) {
console.log(callstack.slice(0, levels).join('\n'));
}
else {
console.log(callstack.join('\n'));
}
};
Moderator's note: The code in this answer seems to also appear in this post from Eric Wenderlin's blog. The author of this answer claims it as his own code, though, written prior to the blog post linked here. Just for purposes of good-faith, I've added the link to the post and this note.

- 39,603
- 20
- 94
- 123

- 25,743
- 8
- 56
- 68
-
2There is a console.trace() call you can make in Firebug that does this. – amccormack Jan 12 '11 at 19:19
-
This is brilliant. Firebug has troubles with minified files, this script does it! – pstadler Jan 16 '13 at 17:40
-
1FWIW @andrew-barber, author of the answer never claimed as his own. Just didn't attribute. Your edit should be a comment. – Ascherer Aug 30 '15 at 01:04
I accomplished this without firebug. Tested in both chrome and firefox:
console.error("I'm debugging this code.");
Once your program prints that to the console, you can click the little arrow to it to expand the call stack.

- 10,719
- 5
- 32
- 42
Try stepping through your code one line or one function at a time to determine where it stops working correctly. Or make some reasonable guesses and scatter logging statements through your code.

- 69,683
- 7
- 133
- 150
-
2This. Definitely add a load of `console.log('something')` statements to your functions to see which ones are (and aren't) being called – Gareth Jan 12 '11 at 16:46
-
1The program is huge, so I am looking for a way to compare function logs for when the program ran correctly vs when it did not. – amccormack Jan 12 '11 at 19:22
-
1I concur that this would be useful. I am stepping in to take ownership of a large code base and something that can generate a running trace of all function calls would certainly help get a sense of the flow/shape of the code and detect dead code. – Matthew Nichols Mar 07 '11 at 22:12
Try this:
console.trace()
I don't know if it's supported on all browsers, so I would check if it exists first.

- 2,917
- 23
- 46
- 68

- 11
- 1