Is there a way to print a stack trace on demand?

I am looking for a way to get a stack trace when I am at a specific breakpoint. Is it possible? Ideally, without having to reset the application and change the code. I tried to play with the Android debugger, but could not find anything very useful.

The reason is that sometimes I’m not sure how the application reached a point in the code, so I am open to other suggestions that will help me track method calls.

+7
source share
3 answers

This can be done in Java:

new Throwable().printStackTrace(); 

In Eclipse, if you create an β€œexpression” with this code in the Expressions view of the Debug perspective, it will print the current stack trace (i.e. the stacktrace of the breakpoint your code stopped at) in the Console view.

+12
source
 Log.e("AppName", "Debug exception", new Exception()); 
+4
source

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

You can also try Thread.currentThread().getStackTrace() , which gives you StackTraceElement[] if you want anything else besides the text view that printStackTrace() does.

+2
source

All Articles