For convenience of discussion, assume we Android executable name is Hello-exe.
1. Compile Debug version of Android executable file, use following command under Project path.
Ndk-build ndk_debug=1
When compiling finished, make sure there is Gdbserver, Gdb.setup and Hello-exe in libs/armeabi-v7a/.
2. Push Gdbserver, Hello-exe to Android device.
ADB push libs/armeabi-v7a/gdbserver/data/local/tmp/adb push Libs/armeabi-v7a/hello-exe/data/local/tmp/
After that, the pull app_process from the Android device to PC.
ADB pull/system/bin/app_process./
3. Prepare for debugging
1) Run Hello-exe as background process.
ADB Shell/data/local/hello-exe &
And get PID of Hello-exe, for convenience, let $HPID is PID of Hello-exe.
PS grep Hello-exe
Busybox includes a lot of useful commands, which make debugging easier. It is my strong recommandation to has busybox in your Android devices.
2) Start Gdbserver to listen debugging requests.
ADB shell/data/local/tmp/gdbserver tcp:5039 --attach $HPID
4 Start Another terminal to perform remote debug
1) Start GDB
ADB forward TCP:5039 tcp:5039arm-linux-androideabi-gdb app_process
Arm-linux-androideabi-gdb is included in the Android NDK package.
2) Perform Debugging
(GDB) Target remote:5039
Load symbols from Obj/local/armeabi-v7a/hello-exe
(gdb) symbol-file obj/local/armeabi-v7a/hello-exe
Setup source path. Gdb.setup contains information of Solib-search-path and source directory.
Typically, the commands would be
(gdb) directory JNI
(GDB) Set Solib-search-path obj/local/armeabi-v7a
3) do debugging as PC applications
5 Other issues
1) How to add breakpoints.
Before remote debugging, Android executable must is in running state. Often, this makes toggling breakpoints impossible.
Fortunately, there is the simple method to work around. First, add the code
int 0 , useless_cnt; while (endless_cnt) { + +;}
At entrance of the program. After starting Hello-exe, it'll be in the endless loop, waiting for debugging.
Next, before any debugging actions with the following command to quit endless loop.
1
Then you can add breakpoints, and do debugging as usual.