Javascript 异常堆栈跟踪

在 Firefox 中,我可以通过使用 exception.stack获得异常的堆栈跟踪。

有没有办法在其他浏览器中也能实现这一点呢?

编辑: 我实际上想要自动保存堆栈跟踪(如果可能的话) ,而不是当时调试(例如,我知道如何在调试器中获取堆栈跟踪)。

87340 次浏览

Not really, at least not easily.

In IE, you can debug the browser process with MS Script Debugger (which for some reason is an Office component) or Visual Studio, and then you can see the stack on breakpoints.

Webkit now has functionality that provides stack traces:

Web Inspector: Understanding Stack Traces, posted by Yury Semikhatsky on Wednesday, April 20th, 2011 at 7:32 am (webkit.org)

From that post:

Place this line where you want to print the stack trace:

console.log(new Error().stack);

Note: tested by me on Chrome 24 and Firefox 18

May be worth taking a look at this tool as well.

If you want the string stack trace, I'd go with insin's answer: stacktrace.js. If you want to access the pieces of a stacktrace (line numbers, file names, etc) stackinfo, which actually uses stacktrace.js under the hood.