30 November 2008

Overcome Java JNI gdb errors on GNU/Linux

If you happen to try to debug any Java JNI programs using gdb on GNU/Linux you will soon come across error messages.
For example :

$ gdb --args java MyJavaProgramUsingJNI

cannot find user-level thread for LWP nnn: generic error

warning: Cannot initialize thread debugging library: versions of libpthread and libthread_db do not match


The reason for this is that the java program mucks around with LD_LIBRARY_PATH and that prevents gdb from running correctly.

To overcome this, you have to run your Java program and place a pause in the Java code, such as reading a key, and then in another shell, run gdb to attach to the running Java process.

You can then set your breakpoint on your JNI code and debug it.

Here is an example :

Compile your Java program:
$ javac JNITest.java

Generate the JNI header file:
$ javah -jni JNITest.java

Create and compile your C code library:
$ cat jnilib.c
#include "JNITest.h"

JNIEXPORT jint JNICALL Java_JNITest_addValues( JNIEnv *env, jobject jobj, jint a, jint b )
{
int res = a + b;
return res;
}

$ gcc -o libjnilib.so -shared -I /opt/jdk1.5.0_16/include -I /opt/jdk1.5.0_16/include/linux jnilib.c

Run it:
$ java JNITest
Result = 75

Run Java program in shell A and attach debugger in session B:
A$ java JNITest
Find running Java process ...
B$ ps -ef
...
user 17809 16492 7 21:28 pts/1 00:00:00 java JNITest
user 17821 14042 4 21:28 pts/2 00:00:00 bash
user 17845 17821 0 21:28 pts/2 00:00:00 ps -ef

Attach debugger:
B$ gdb -p 17809
...
(gdb) break Java_JNITest_addValues
Breakpoint 1 at 0x8ca02402: file jnilib.c, line 5.
(gdb) cont
Continuing.
[Switching to Thread 0xb7e866b0 (LWP 17809)]

Breakpoint 1, Java_JNITest_addValues (env=0x84258b8, jobj=0xbfd2e6d8, a=25,
b=50) at jnilib.c:5
5 int res = a + b;
(gdb)

(gdb) print a
$1 = 25
(gdb) print b
$2 = 50
(gdb) cont
Continuing.

6 comments:

Wright Petter said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.

gkr ragini said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
Best Devops training in sholinganallur
Devops training in velachery
Devops training in annanagar
Devops training in tambaram

tamilsasi said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training

Aliya Manasa said...

I would like to thank you for your nicely written post, its informative and your writing style encouraged me to read it till end. Thanks
python Course in Pune
python Course institute in Chennai
python Training institute in Bangalore

jefrin adams said...

Wow nice to read the topic keep on posting
Best blue prism training in chennai

service care said...

More informative,thanks for sharing with us.
this blog makes the readers more enjoyable.keep add more info on your page
samsung mobile service center in velachery
samsung mobile service center in porur
samsung mobile service center in vadapalani