Debugging the JVM

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 :-)

Reference: Debugging the JVM from our JCG partner Attila-Mihaly Balazs at the Transylvania JUG blog.

Related Articles :

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

One Response to "Debugging the JVM"

  1. Toma Porti says:

    That’s cool. I always thought that if your JVM crashes, that’s it / there’s no way out. gdb doesn’t seem such a friendly option though. I bet the Oracle’s JVM developer team has some custom tools for that, that they might want to make public.
    But other than that, a very nice read.

Leave a Reply


+ eight = 9



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books