首页 > 代码库 > gdb debug android executable

gdb debug android executable

For convenience of discussion, assume our 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 are 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, pull app_process from 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 be PID of hello-exe. 

adb shell busybox ps aux | busybox grep hello-exe

Busybox includes a lot of useful commands, which make debugging easier. It is my strong recommandation to have 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:5039
arm-linux-androideabi-gdb app_process

arm-linux-androideabi-gdb is included in 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 will 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 be in running state. Often, this makes toggling breakpoints impossible.

Fortunately, there is simple method to work around. First, add the code

int endless_cnt = 0, useless_cnt;
while(endless_cnt) {
    useless_cnt ++;
}

 

at entrance of the program. After starting hello-exe, it will be in endless loop, waiting for debugging.

Next, before any debugging actions, use the following command to quit endless loop.

(gdb) set var endless_cnt = 1

Then you can add breakpoints, and do debugging as usual.