Debugging the JVM
Join the DZone community and get the full member experience.Join For Free
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
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
Opinions expressed by DZone contributors are their own.