Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript backtrace

How to I get a backtrace in Javascript?

Ideal features:

  • entry function name, or some meaningful identifier for anonymous functions,
  • argument list at each level,
  • line numbers.

Can this be done in standard ECMAScript?

If not, can it be done in the common web browser dialects?

Thanks.

Edit --

Thanks for your suggestions.

My dialect doesnot support arguments.caller or arguments.callee.

I can do this:

try {
    let x = null;
    x .foo ();
}
catch (e) {
        debug (dump (e.stack));
}

Which gets me the information as a string, which is okay for at-a-glance, but it would be a great help to walk e.stack. Does it have a standard form?

Thanks again.

like image 251
spraff Avatar asked Jul 22 '11 08:07

spraff


People also ask

What is a JavaScript stack trace?

A stack trace is a list of the functions, in order, that lead to a given point in a software program. A stack trace is essentially a breadcrumb trail for your software. You can easily see the stack trace in JavaScript by adding the following into your code: console. trace();

What is trace in JavaScript?

Definition and Usage The trace() method displays a trace that show how the code ended up at a certain point.

What is the call stack in JavaScript?

A call stack is a mechanism for an interpreter (like the JavaScript interpreter in a web browser) to keep track of its place in a script that calls multiple functions — what function is currently being run and what functions are called from within that function, etc.

Is console log () added to execution stack?

It's a function call. It goes to the call stack.


2 Answers

In my debugging experience, I always use this

(function () { console.log(new Error().stack); })();

As the comments below suggested, the readers should prefer: console.log(new Error().stack);

like image 63
linzuojian Avatar answered Oct 24 '22 05:10

linzuojian


Another way is to use console.trace();

source: https://developer.mozilla.org/en-US/docs/Web/API/Console/trace

like image 29
Lafif Astahdziq Avatar answered Oct 24 '22 05:10

Lafif Astahdziq