Debugging JNI C and Java code in Eclipse

    Working with both C and Java in the same project brings its own particular challenges which are often overcome in a number of interesting manners many of which prove to be unworkable at larger scale

     

    Environment

    Java 6

    Eclipse 3.7.1

    CDT 8.0.1

     

    Purpose

    Debug a single Java maven project that contains both java and JNI code

     

    Process

     

    1. Create a new Eclipse project using "File -> Import -> Existing Maven Projects"
    2. Right click on the project "New -> Other" "C/C++ -> Convert to a C++Project (Adds C/C++ Nature)" (I choose the Linux toolchain)
    3. Go to the "C/C++ Perspective"
    4. I add the folders containing the c++ source to the build path so I can set break points
      1. Ensure you can see "/usr/includes" under the "Includes" element of the project tree
    5. Put some breakpoints in your code:
      1. Put a breakpoint in the Java code before your C code starts (for example imeadiately after the System.loadLibrary call (you must ensure that this Run Configuration has its source tab including the C code you wish to debug)
      2. Put a breakpoint in the C code wherever you like
    6. Create a Eclipse "Debug Configuration" for the wrapping Java process (I have attached my config)
      1. Under "Arguments -> VM arguments", set the -Djava.library.path=/home/tom/projects/jbosstm/blacktie/integration-tests/target/ (where this is the path to your .so containing JNI)
      2. Under "Environment" I set LD_LIBRARY_PATH to /home/tom/projects/jbosstm/blacktie/integration-tests/target/cxx/test/lib/ (path for any dependencies of your JNI .so)
    7. Create a new Eclipse "C/C++ Attach to a Local Application" Debug Configuration within one of your Eclipse C++ projects
      1. Point the "C/C++ application" at the actual java executable that your Java debug configuration process launches (for example /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/bin/java), no other changes should be required (for example ps -eaf)
    8. Start the Java application in the Eclipse Java Debugger
    9. When the java breakpoint fires, start the "C++ Attach to a Local Application" debug configuration
    10. You will be asked to connect to a process, choose the correct "java" one, for example, filter the processes by "java"
    11. Gotcha:
      1. Eclipse will stop the GDB process at one of its own breakpoints (which I can't seem to override), you need to press Play (F8) on the GDB debugger to move this on
    12. Watch in awe as both your java and C breakpoints are hit

     

    Thats all there is to it! Hope this has helped!