GdB Debug Android Executable

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.