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!");


Secure Android Browser said...

Using ddd on top of gdb?

Pete Cole said...

Many thanks for the suggestion - that is a great idea!

It turns out that it is really easy to use ddd instead...

1. install ddd!
2. modify your ndk-gdb shells script, simply comment-out the exit(1) call when an unexpected argument is passed
3. Change to your project's jni folder
4. Run like this:
ddd --debugger ndk-gdb

Thanks again!


roopa said...

ey, just wanted to say that this was a very well written post and I enjoyed reading it. It's great to see someone sharing interesting information on the developer

Pete Cole said...

Glad you found it useful!