Debugging Native Android Code

NDK

Contains gdb and gdbserver.

Download

Latest

Older versions: r7c - r12b

GDB

gdbserver should already be on the device, if not:

$ adb push ${ndk_path}/toolchains/${android_platform}/prebuilt/gdbserver /data/local/tmp

On the device:

$ gdbserver :5039 /path/to/executable

or if the program is already running:

$ gdbserver :5039 --attach pid

On your workstation:

$ adb forward tcp:5039 tcp:5039
$ ${ndk_path}/toolchains/${android_platform}/prebuilt/${host_platform}/bin/${android_platform}-gdb /path/to/executable

Connect to the device within gdb

target remote :5039

gdb options

For library symbols:

set solib-absolute-prefix /path/to/libs

Might be needed to load symbols:

shared

non-PIE support

https://source.android.com/security/enhancements/enhancements50

non-PIE linker support removed. Android now requires all dynamically linked executables to support PIE (position-independent executables).


Other resources