How to Set Up Android Platform Development and Debugging

By Larry Schiefer  |  Posted 2010-03-23

How to Set Up Android Platform Development and Debugging






Setting up the platform



Native Code Debug Setup}

Native code debug setup

Eclipse needs to be configured to use a remote C/C++ target. For every service or application that uses


Insert DBG_HALT(); in the native code you would like to debug. Currently, there is no way to embed a hard-coded breakpoint in native code.

Native Debugging

Native debugging

Debugging native code on a platform can be done using Eclipse by following these nine steps:

Step No. 1: Start the target. Using Eclipse, start debugging the Java application (or service) containing your native code. Stop the Java-side debugger in the entry point of the Activity or Service.

Step No. 2: Using the Android plug-in in Eclipse (or adb logcat), determine the Linux process ID for the application/service.

Step No. 3: Configure the machine to port forward to the target by executing:

 ~> adb forward tcp:10000 tcp:10000

Run gdbserver on the target, attaching it to the process ID found above (PID below):

~> adb shell /system/bin/gdbserver tcp:10000 --attach PID

Step No. 4: In Eclipse, go back to C/C++ or Java mode, but do not disconnect the Java debugger if you want to debug the Java side as well.

Step No. 5: Select your C/C++ project, right-click and select Debug Configurations.

Step No. 6: Select the Android C/C++ debug configuration you made earlier and click Debug. Eclipse will connect to the emulator and bring up the Debug perspective filled with target information, along with the remote Java debug information. It is normal to see warnings from gdb about dynamic sections. Ignore them. You can switch between the Java and C/C++ debug sessions as needed, though Java debugging will be unavailable while halted in native code.

Step No. 7: Select the C/C++ target in the Debug perspective, and click Suspend to stop the native process to see the current call stack. Examine each thread within the process and find which one is executing the DBG_HALT() in your code.

Step No. 8: Look at the disassembly of the DBG_HALT() code to find the memory location on the stack where it is pulling the value of "i." Change the memory location 1.

Step No. 9: Changing the memory location prevents you from single-stepping. To work around this, set a breakpoint at the next valid C/C++ line of code after the macro and click Resume. The breakpoint will get hit immediately and the ability to single step through the code.

Android platform and native development documentation may be sparse, but source code access and an understanding of how to set up your native code projects for debugging should give you a head start.


Rocket Fuel