I'm a member of Transylvania JUG, a community from Cluj-Napoca/Romania interested in exchange of Java knowledge between members and keeping in touch with the latest Java technologies, frameworks and development trends. Attila-mihaly is a DZone MVB and is not an employee of DZone and has posted 20 posts at DZone. You can read more from them at their website. View Full User Profile

Debugging the JVM

01.11.2012
| 4476 views |
  • submit to reddit

In some (rare) cases you might find yourself in the situation that you managed to crash the JVM itself. I most recently managed this by setting the name of a ThreadGroup to null. In these cases it is useful to debug the JVM itself so that the crash can be located more precisely. Here are the steps to do it (they are Linux specific since there is no readily available debugger under Windows):

  • Install gdb (under Ubuntu this would be something like: sudo apt-get install build-essential)
  • If you are using OpenJDK, install the debugging symbols for it so that the debugger can give a more readable output (again, under Ubuntu this would be sudo apt-get install openjdk-6-dbg – substitute 6 with a 7 if you are using the latest OpenJDK)

Now just prefix your java command with gdb --args:

gdb --args java Foo

When the gdb prompt comes up (“(gdb)”), type “run” (without the quotes) to start the actual running of the program. After the crash happens you should a message like the following:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x6b195b70 (LWP 30273)]
(gdb)

Here you can use the commands “backtrace” and “backtrace full” to get an approximate idea of the crashsite. To continue running (although it will just exit most probably) input “c”. To exit (killing the JVM in the process) type “quit”. Consult the GDB tutorials available on the Internet for more commands and their parameters.

If you are debugging from inside Eclipse, you can do the following: in the configuration properties set the JRE to “Alternate JRE” and specify the Java executable as “javag” (also, make sure that you have “Allocate console” checked in the Common tab).

Now go to your JDK run directory (/usr/lib/jvm/java-7-openjdk-i386/bin in my case) and create a javag file (sudo vim javag) with the following content:

 

#!/bin/bash
gdb -x '/usr/lib/jvm/java-7-openjdk-i386/bin/javag-commands' --args '/usr/lib/jvm/java-7-openjdk-i386/bin/java' $*

 

Also create the javag-commands file with the following content

run

Finally, make javag executable (sudo +x chmod javag) and you’re good to go! This workaround is necessary because Eclipse doesn’t accept absolute paths in the configuration tab. The second file is used to automatically pass the “run” command to gdb rather than the user having to type it themselves on each start. Also, keep in mind that while GDB has suspended the process Java debuggers (like Eclipse) can’t communicate with it so it is normal for them to throw all kind of errors (like “target not responding”).

Have a bugfree year, but if you find bugs, let them be at least reproducible :-)

 

From http://www.transylvania-jug.org/archives/331

Published at DZone with permission of Attila-mihaly Balazs, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Wojciech Kudla replied on Wed, 2012/01/11 - 4:06am

Thank you for a nice post. Unfortunately from my perspective this has more academic than real-world applications.
I have never seen OpenJDK being used in serious production deployments as it almost always is either HotSpot or JRockit or IBM's JVM.
As an alternative (in case of HotSpot) I would suggest either using fastdebug builds or hsdis (HotSpot Disassembler). It's not the same but may turn out to be helpful.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.