Over a million developers have joined DZone.

Debugging OpenJDK

· DevOps Zone

The DevOps Zone is brought to you in partnership with Sonatype Nexus. The Nexus Suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

knowyourmeme.com/photos/531557 thx to @mihn
Sometimes debugging Java code is not enough and we need to step over the
native part of Java. I spent some time to achieve proper state of my JDK to do that, so short description probably will be useful for ones starting their trip. I'll use the brand new OpenJDK 9!

At first you have to obtain main repository by typing:
hg clone http://hg.openjdk.java.net/jdk9/jdk9 openjdk9
Then in the openjdk9 directory type:
bash get_source.sh
That will download all sources to you local filesystem.

Theoretically compiling openjdk is not a big deal, but there are some (hmmm....) strange behaviours if you want to use it for debugging.

At first of course we need to call ./configure to prepare specific makefiles for our system. We can read in documentation that we have to add --enable-debug flag to prepare fastdebug build. If you don't have proper libs or tools installed in your system it's right moment to install dependencies (configure output will clearly point out any lacks). After configuring and invoking make command you can face with such problem:
warning _FORTIFY_SOURCE requires compiling with optimization (-O)
Generating buffer classes
Generating exceptions classes
cc1plus: all warnings being treated as errors
Cool :) It happens only on some specific linux instalations (unfortunately including Fedora 20 :)). To solve it we have to remove _FORTIFY_SOURCE flag. Just comment (#) lines containing _FORTIFY_SOURCE in the following files:
  • hotspot/make/linux/makefiles/gcc.make
  • common/autoconf/flags.m4
Then you can move on with making jdk project and after dozen minutes you should see.
Finished building OpenJDK for target 'default'
Now it's time to import project to IDE Since we're still waiting for good C++ IDE from JetBrains we have to use NetBeans or even Eclipse. After finishing few steps needed to setup debugging commands (for example even for java -version). Start debugging, and... SIGSEGV received. Let's solve it by creating .gdbinit file in user home directory containing following lines:
handle SIGSEGV pass noprint nostop
handle SIGUSR1 pass noprint nostop
handle SIGUSR2 pass noprint nostop
Start debugging one more time - now it's better! Let's continue by adding line breakpoint. Start debugging, and... not working... :( I've extended .gdbinit by adding.
set logging on
One more debug try and in gdb.txt I saw such line:

No source file named hotspot/src/share/vm/memory/genCollectedHeap.cpp
I was pretty sure that --enable-debug will add -g flag to gcc compiler, but it seems I was wrong. I spent few hours googling and trying to solve it by changing gdb configurations, NetBeans config, etc. Still no effect. Fortunately Michal Warecki pointed me that probably OpenJDK during debug builds zips all debuginfo and if you want to debug (of course prepared debug build due to some other purposes?). After grepping makefiles I've found promising disable-zip-debug-info flag. So let's include it into our configure invocation. Also believe me it's hard to debug optimized code in C++ (you can try, but you will encounter strange thing happening, like debugger stepping lines in incorrect order (like starting method from line 4, going back to 2, then to 5 and to 3 :)). So we'll choose slowdebug option to avoid code optimization. Whole proper configure command is:
bash ./configure --with-debug-level=slowdebug --with-target-bits=64 --disable-zip-debug-info
Now we can invoke:
make
and wait for compilation to finish. Now you can check if everything works correctly by invoking ./java -version in 
build/linux-x86_64-normal-server-slowdebug/jdk/bin directory

You should see:
openjdk version "1.9.0-internal-debug"
OpenJDK Runtime Environment (build 1.9.0-internal-debug-kuba_2014_08_20_14_02-b00)
OpenJDK 64-Bit Server VM (build 1.9.0-internal-debug-kuba_2014_08_20_14_02-b00, mixed mode)
Let's try debugging. Add line breakpoint, start debugging, and... finally it's green! Have fun!

The DevOps Zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today

Topics:

Published at DZone with permission of Jakub Kubrynski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}