1. Pre-Requisites
- Make sure you compile your C/C++ with the "-g" option (or use -DCMAKE_BUILD_TYPE:STRING="Debug" if you use cmake).
- Make sure APP_OPTIM is set to "APP_OPTIM:=debug" in Android.mk and Application.mk.
- Make sure build/core/build-binary.mk in Android NDK doesn't strip executables. For example patch android-ndk-r8e/build/core/build-binary.mk to the following:
--- build/core/build-binary.mk.orig 2013-08-21 11:06:39.818329442 -0400
+++ build/core/build-binary.mk 2013-08-21 11:13:11.877214361 -0400
@@ -485,10 +485,16 @@ $(LOCAL_INSTALLED): PRIVATE_DST :=
$(LOCAL_INSTALLED): PRIVATE_STRIP := $(TARGET_STRIP)
$(LOCAL_INSTALLED): PRIVATE_STRIP_CMD := $(call cmd-strip, $(PRIVATE_DST))
+ifeq ($(APP_OPTIM),debug)
+$(LOCAL_INSTALLED): $(LOCAL_BUILT_MODULE) clean-installed-binaries
+ @$(HOST_ECHO) "Install : $(PRIVATE_NAME) => $(call pretty-dir,$(PRIVATE_DST))"
+ $(hide) $(call host-install,$(PRIVATE_SRC),$(PRIVATE_DST))
+else
$(LOCAL_INSTALLED): $(LOCAL_BUILT_MODULE) clean-installed-binaries
@$(HOST_ECHO) "Install : $(PRIVATE_NAME) => $(call pretty-dir,$(PRIVATE_DST))"
$(hide) $(call host-install,$(PRIVATE_SRC),$(PRIVATE_DST))
$(hide) $(PRIVATE_STRIP_CMD)
+endif
1.1. Install the Eclipse Sequoyah plugin
- In Eclipse go to Help -> Install New Software
- Click on the Add button
- Enter 'Sequoyah Metadata Repository' in the Name field
- Enter 'http://download.eclipse.org/sequoyah/updates/2.0/' in the Location field
- If you still see the "There are no categorized items" message, uncheck the "Group items by category" radio button
- Select "Sequoyah Android Native Code Support" and install the plugin
1.2. Convert The Android Java Application into C/C++ Project
You must
convert your Java project to C/C++ using the Sequoyah plugin or else you
won't be able to see the configuration options to set the proper
debugger settings.
- In Eclipse select the Android Java project you need to convert
- Right click with the mouse and select Android Tools -> Add Native Support
1.3. Other Pre-Requisites
- The platform must be Android 2.2 (android-8) or later
- The ndk version must be r4b (it contains bugfixes to ndk-gdb that are necessary) or later
- Eclipse CDT 7.0 or newer must be installed
- The AndroidManifest.xml must have the property of the application node android:debuggable="true"
- The build must have been done with the ndk-build (if using the Sequoyah Android components, it will be automatic)
2. Configurations
- 01) Create a debug configuration for an Android application (can be done with Eclipse or MOTODEV Studio)
- 02) Create a debug configuration for a C/C++ application
- 04) The process launcher must be the Standard Process Launcher. This is selected at the bottom of the Main tab:
- 06) On the "Debugger" tab:
- field Debugger: gdbserver
-
On the "Main" subtab:
- 07) GDB debugger: $NDK_PATH/build/prebuilt/$ARCH/arm-eabi-$GCC_VERSION/bin/arm-eabi-gdb
- 08) GDB command file: $PROJECT_PATH/obj/local/armeabi/gdb2.setup
[Windows users] Uncheck the "Use full file path to set breakpoints" option - 09) Type: TCP
- 10) Hostname or IP address: localhost
- 11) Port number: 5039
3. Instructions
- Open the
ndk-gdb script that came with the android NDK and comment the last line
(we are not calling the usual gdb client, but we will attach an Eclipse
gdb session instead):
# $GDBCLIENT -x $GDBSETUP -e $APP_PROCESS
- Insert a breakpoint in your Java code, preferably after all System.loadLibrary() calls. (To make sure that the debugger is correctly attached to the Java process)
- Launch the android debug and wait for it to reach the breakpoint
- From a Terminal session, in the project folder, run the modified ndk-gdb command. It should not attach to an gdb client, but call the gdbserver on the emulator and open a TCP port for connection (or in alternative if you have an Android device connected to your USB port, the ndk-gdb script will run gdbserver on the device itself).
- In the $PROJECT_PATH/obj/local/armeabi/, modify the gdb.setup file, removing the target remote:5039 statement. (For some reason, the Eclipse GDB session does not like this statement being done in the commands file). Rename this new file to gdb2.setup. This step need to be run just once, on the first debug session.
- Launch the C/C++ Application debug and wait for the Eclipse GDB session to fully connect to the emulator's gdbserver instance.
Another way to set breakpoints in the C/C++ code is from Eclipse to click on File -> Open File and browse to the location of your source code. Then double-click on the line where you want to set the breakpoint.