Friday, 21 October 2011

gdb with the Android NDK - now easy with ddd wrapping ndk-gdb!

You might already know this, but android debugging from gdb is now really, really easy - if you can stomach gdb!

See Android/ndk6/docs/NDK-GDB.html in your NDK documentation.

Basically, the app's libs/armeabi folder must have a gdbserver in it... which is put there automatically by the ndk-build command (remember not to ship your app with gdbserver in it!):

So...: build your app, install with e.g.:

adb install -r myfile.apk

Start your app directly from the Android UI, and in a terminal, change to your product's jni folder, and you're ready to debug!

Using command-line gdb

If you prefer command-line gdb, type this:

ndk-gdb com.example.myproject

You'll find that ndk-gdb will break as your app attaches. Set a breakpoint if you want (optional!), e.g.:

b Java_com_example_hellojni_HelloJni_stringFromJNI

Enter "cont" to continue...

And the debugger will now hit your breakpoints - use normal gdb commands to control it!

Using the graphical ddd debugger

If, like me, you prefer to use a graphical debugger, it is really easy to use ddd as a wrapper around gdb!

Many thanks to a blog reader for this suggestion!

It is really easy to use ddd instead... here are the steps!


1. Install ddd ...!
2. Modify your ndk-gdb shells script, to simply comment-out the "exit 1" call when an unexpected argument is passed

Running ddd 

Start your app from Eclipse

In a terminal window, change to your project's jni folder (if you're not there already!)
Run ddd like this - and that is all there is to it!

ddd --debugger ndk-gdb


Using gdb for JNI/NDK from Eclipse is way too painful - I wouldn't bother. :)

Run-time breakpoints

On a final note, you can use this code to force a breakpoint at runtime...

    #define BREAKPOINT __asm__ ("bkpt 0")
    printf ("Hitting breakpoint!");

    printf ("Gone past breakpoint!");