Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to dump a stacktrace when a specific Java method is called?

We need to debug a Java application that randomly does stuff it should not do.

To understand the root cause of the issue, we would need to get a stacktrace whenever a specific method is called. This sounds easy (add Thread.dumpStack() in this specific method), however this is a code class from the JDK, hence we do not really want to mess with it (even if we indeed could).

Does anybody know if we can implement some kind of "listener" in the JVM that would detect when a specific method is called and dump the stack trace?

This is a production system, so if the solution is a debugger we would need to have something that has very limited impact on performance.

Thank you for your input.

like image 661
V-Turn Avatar asked Aug 27 '26 01:08

V-Turn


2 Answers

Use Byteman. It can inject tracing code into a running server pretty easily. Our support team love it. http://www.jboss.org/byteman

like image 119
Jonathan Halliday Avatar answered Aug 28 '26 15:08

Jonathan Halliday


"Does anybody know if we can implement some kind of "listener" in the JVM that would detect when a specific method is called and dump the stack trace?

This is a production system, so if the solution is a debugger we would need to have something that has very limited impact on performance."

These requirements are contradictory. Dumping the stack is expensive, no matter how you do it. IMO, what you really need to do is set up a test server and run your investigations there.

FWIW, the simplest and probably least expensive way to do this kind of thing is to insert one of these as the first statement of the method:

    log.info("Method called", new Throwable());

    new Throwable("method called").printStackTrace(System.out);
like image 27
Stephen C Avatar answered Aug 28 '26 15:08

Stephen C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!