how can I debug an android native executable and library not directly integrated into the APK application

2.9k Views Asked by At

I have an android application that consists of a Java based APK, native executable, and native library. The apk talks to the native (root NDK c/c++) executable and library over a socket.

I'm not sure if it matters but the executable and library are compiled via cmake, and copied to be executable and then run as root. I need to get some type of debugging going with breakpoints and such, regardless of if it's directly in android studio or via command line.

1

There are 1 best solutions below

0
On

You would need to run gdbserver on the device and let it attach to your executable

gdbserver comes prebuilt with ndk, usually under <ndk>/prebuilt/android-arm/gdbserver/

  1. Copy gdbserver binary to your device, for instance to /data/local/tmp and give it executable permissions with chmod

  2. If your executable is already running, find its PID with ps command and attach gdb to it:

    gdbserver :5039 --attach <PID>

Note that 5039 is port number that is usually used for debugging with gdb, you can use your own if you like

  1. set up a port forwarding from device to pc with

    adb forward tcp:5039 tcp:5039

  2. Run gdb locally, note that you need arm targeted gdb that comes with ndk too, usually at

    <ndk>toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gdb

  3. Attach gdb to your process

    target remote :5039

And from here you need to use gdb commands that match your debugging expectations (set breakpoints, load symbols, step through etc), for examples use cheatsheet or ask in comments