简体   繁体   中英

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.

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 (ie the stacktrace of the breakpoint your code stopped on) in the Console view.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM