JohnEye
JohnEye

Reputation: 6895

Is there a way to print stack trace on demand?

I am looking for a way to get a stack trace when I am at a certain breakpoint. Is this possible? Ideally without having to crash the application and modifying the code. I tried playing with the Android debugger but couldn't find anything very helpful.

The reason is that sometimes I am not certain how the application arrived at a point in code, so I am open to other suggestions that would help me trace the method calls.

Upvotes: 7

Views: 2054

Answers (3)

npe
npe

Reputation: 15699

This can be done in Java:

new Throwable().printStackTrace();

In Eclipse, if you create an "expression" with that code in the Expressions view of Debug perspective, it will print current stack trace (i.e. the stacktrace of the breakpoint your code stopped on) in the Console view.

Upvotes: 14

f1sh
f1sh

Reputation: 11934

The easiest way is to throw an exception, immediately catch it and use printStackTrace().

You could also try Thread.currentThread().getStackTrace() which gives you a StackTraceElement[] in case you want to to anything else besides having the textual representation that printStackTrace() does.

Upvotes: 2

Jonathon Faust
Jonathon Faust

Reputation: 12545

Log.e("AppName", "Debug exception", new Exception());

Upvotes: 4

Related Questions