4

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.

Brett Thomas
  • 161
  • 3
  • 7
  • As long as you have sources of native part and can build it in debug mode, you can debug it with recent version of Android Studio or with command-line lldb or gdb. Have you tried anything like [these instructions](https://developer3.oculus.com/documentation/mobilesdk/latest/concepts/mobile-studio-debug) ? – Oleg Bogdanov Nov 08 '16 at 17:04
  • I've tried following those however I can't seem to find the obj directory that used to get created when i was using ndk-build. Now that I'm using cmake that directory does not seem to exist. – Brett Thomas Nov 09 '16 at 18:32
  • I'm not particularly familiar with cmake usage for Android but it all boils down to finding your .so file with unstripped symbols, just look for bigger .so in size with same name – Oleg Bogdanov Nov 09 '16 at 18:33
  • 1
    so I'm actually trying to debug a native executable, that gets copied and run as root on app launch. The library gets used by that, and all communication is done over a socket. I've added the paths to the .so files, and followed all the steps in that link you posted but still not able to get it to break. – Brett Thomas Nov 09 '16 at 19:37
  • Android studio is capable of attaching and running java apps + debugging .so they use. I'm not sure if its easy possible to use it for debugging of standalone non-java application. you need to make it attach to gdbserver running on the device, I can give you instructions how to do command line debugging though – Oleg Bogdanov Nov 09 '16 at 19:49
  • even that would be better than the nothing I have now. I'll update the question to accept command line debugging as well. – Brett Thomas Nov 09 '16 at 22:04

1 Answers1

3

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

Community
  • 1
  • 1
Oleg Bogdanov
  • 1,712
  • 13
  • 19