I recently encountered my first need for on-device NDK debugging. While the setup is kind of a pain, its super cool to be able to attach to a remote process over a network socket and have access to the full power of the GNU Debugger. Hopefully this quick guide takes some pain out of the initial setup (root required!).
First, let’s load the ARM64 build of
gdbserver on to the phone. This can be found wherever you’ve placed the Android NDK (in my case, the latest version is hanging out in my downloads folder)
Now let’s jump into the phone, gain root access, and have a look at the PATH. For the sake of convenience, we’ll want to move
gdbserver to somewhere the system is looking for executables.
Looks like there are some options for where to put it.
/system/bin are read-only locations, but
/su/bin is writable. Let’s move
gdbserver there and make it executable.
With the debug server in place and executable, let’s check that it can execute.
Great! Now for something more cool. Find the Android Talk process and attach the debugger. The
ps command lists processes and we’ll use
grep to filter. The second column in the result is the PID of the
com.google.android.talk process, let’s start the debug server and attach it to that process.
Now in another terminal from your desktop, we’ll forward the debug ports over the Android USB bridge and try connecting.
The system symbols download and we’re presented with an entry point for the debugger. At this point, you can use GDB in exactly the same way as if you were using it locally or from within an IDE. The Quick Reference is your friend, especially if you’ve spent the last few years in Xcode living the lldb high life.