Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java post-mortem debugging?

Is it possible to have a post-mortem ( or post-exception ) debugging session in Java ? What would the workarounds be ( if there isn't a solution for this already ) ?

like image 781
Geo Avatar asked Nov 24 '08 17:11

Geo


2 Answers

You can attach the debugger to a java process and set a breakpoint when a specific exception is received. Is this what you need?

From http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/jdb.html

When an exception occurs for which there isn't a catch statement anywhere up a Java program's stack, the Java runtime normally dumps an exception trace and exits. When running under jdb, however, that exception is treated as a non-recoverable breakpoint, and jdb stops at the offending instruction. If that class was compiled with the -g option, instance and local variables can be printed to determine the cause of the exception.

This type of breakpoints can be set with any IDE, such as Eclipse. Using eclipse you can also set a breakpoint on a specific exception type, even if it is caught in the normal code.

If you have something like a multi-threaded server running, and one of the threads servicing a client throws an unhandled exception, then you would be able to check the debugger and see what happens. I don't think this is something for production, but definitively helps when testing.

The application does not have to run from the debugger, but it can be launched with the debug options as arguments:

-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=XXXX

The application runs normally, but switches into "interpreted" mode when a breakpoint is hit, at least in more modern versions of the JVM. So the performance is not affected.

Check the section about full-speed debugging for HotSpot VM

Full Speed Debugging

The Java HotSpot VM now uses full-speed debugging. In previous version of the VM, when debugging was enabled, the program executed using only the interpreter. Now, the full performance advantage of HotSpot technology is available to programs, even with compiled code. The improved performance allows long-running programs to be more easily debugged. It also allows testing to proceed at full speed. Once there is an exception, the debugger launches with full visibility to code sources.

like image 127
Mario Ortegón Avatar answered Oct 03 '22 19:10

Mario Ortegón


As far as I know, you'd have to wait for Java 7 for an official API - see http://tech.puredanger.com/java7#jsr326.

In the meantime, you could substitute Exception (replace base class, inject code via instrumentation, etc) to keep your data. Please note though the ClassLoader throws and catches ClassNotFoundException regularly (each new package loaded). You'd have a lot of control that way without having to modify you base code.

like image 35
Ran Biron Avatar answered Oct 03 '22 20:10

Ran Biron