recently written about a cross-platform compatible C + + library, Android is one of the platforms where compatibility is required. Unlike the powerful IDE environment in Windows,mac, the official does not provide much support for the Android NDK. As a result, I tried to use GDB to debug the Android Ndk's C + + program under some configuration, so it was convenient and recorded to share with you. 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 , and 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 workfirst 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 deviceThe so-called deployment is actually the Android NDK under the Gdbserver copy to the device, you can use the following commandadb push $ANDROID _ndk_root/prebuilt/android-arm/gdbserver/gdbserver/data
2. Deploy your native program on the deviceneed 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/dataadb push./libmylib.so/system/lib
3. Copy the relevant debugging environment on the device to the localbecause remote debugging requires a library of some target machines, copy the following files to a local folderadb pull/system/lib./debugging/libadb pull/system/bin/linker./debugging/lib
4. Run your program through Gdbserver on the deviceadb shell Gdbserver:12345/data/data/myapp
5. Locally forward the local TCP port to the TCP port on the deviceadb 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-gdbNote 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 GDBis 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:12345The 6~9 step above can also be executed with the following command$ANDROID _ndk/toolchains/arm-linux-androideabi-4.4.3/prebuilt/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 DebuggingRun 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.
This article does not discuss the specific use of GDB debugging commands, related articles online should be a lot, perhaps follow-up will write the experience of using GDB. It is also possible to configure the compile and debug commands to be easier to use in some common Ides, such as Eclipse,codeblocks.
Use GDB to debug Android NDK native (c + +) programs-Go