Use GDB to debug Android NDK native (c + +) programs

Source: Internet
Author: User

use GDB to debug Android NDK native (c + +) programs

First of all, here the so-called NDK native program is not related to Android upper Java applications, and does not need to involve jni to encapsulate the native interface, in general, is to compile a pure C + + program, push to the Android device or emulator, Then run the program on the device. Debugging is done by attach to Gdbserver. Recommended in Ubuntu or Mac OSX down, Windows to install Cygwin to simulate the POSIX environment, very slow to say. Here's how:


0. Preparatory work
First make sure that the Android development environment is already on the machine, such as downloading the Android SDK and the NDK package, configuring the relevant environment variables, launching the Android device or emulator, and so on. Of course you also need to compile and build a native program with debug symbol, I am using the NDK under the GCC compiler to configure the compilation environment to compile, of course, you can also use the ndk-build directly.


1. Deploy Gdbserver on the device
The so-called deployment is actually the Android NDK under the Gdbserver copy to the device, you can use the following command
ADB push $ANDROID _ndk_root/prebuilt/android-arm/gdbserver/gdbserver/data


2. Deploy your native program on the device
Need to deploy your compiled program and related so library to the device, note that so library to be placed under/system/lib,/system path default is read-only, can be reset by the ADB remount.
ADB push./myapp/data/data
ADB push./libmylib.so/system/lib


3. Copy the relevant debugging environment on the device to the local
Because remote debugging requires a library of some target machines, copy the following files to a local folder
ADB pull/system/lib./debugging/lib
ADB pull/system/bin/linker./debugging/lib


4. Run your program through Gdbserver on the device
ADB Shell Gdbserver:12345/data/data/myapp


5. Locally forward the local TCP port to the TCP port on the device
ADB forward tcp:12345 tcp:12345


6. GDB program running locally on the Android NDK path
$ANDROID _ndk/toolchains/arm-linux-androideabi-4.4.3/prebuild/darwin-x86/bin/arm-linux-androideabi-gdb
Note here that if you are using the Linux NDK package under Ubuntu, the path will be a bit different, and the darwin-x86 should be linux-x86. The most insured thing is to search under the NDK.


7. After starting GDB, set the Solib search path under GDB
is for GDB to be able to find the debug-related lib, which is the file that was pulled down from the device in the third step.
(GDB) Set Solib-search-path./debugging/lib


8. Under GDB, set the native program you wish to debug.
(gdb) file./myapp


9. Gdbserver connected to the device
(GDB) Target remote:12345
The 6~9 step above can also be executed with the following command
$ANDROID _ndk/toolchains/arm-linux-androideabi-4.4.3/prebuild/darwin-x86/bin/arm-linux-androideabi-gdb-- Eval-command= "set Solib-search-path./debugging/lib"--eval-command= "file/myapp"--eval-command= "Target remote : 12345 "


10. Start Debugging
Run the program through continue or C. Note that it is not run, because the program is actually started on the target machine, just break at the program entrance.

Use GDB to debug Android NDK native (c + +) programs

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.