Danilo's Tech Blog

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Thursday, 22 August 2013

How to debug Android Native Code with Eclipse

Posted on 06:29 by Unknown
This blog summarizes the steps needed to set-up your Eclipse environment to support the debugging of an Android native application written in C/C++. It's taken from Carlos Suoto's web page at http://www.eclipse.org/sequoyah/documentation/native_debug.php.

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


  1. In Eclipse go to Help -> Install New Software
  2. Click on the Add button
  3. Enter 'Sequoyah Metadata Repository' in the Name field
  4. Enter 'http://download.eclipse.org/sequoyah/updates/2.0/' in the Location field
  5. If you still see the "There are no categorized items" message, uncheck the "Group items by category" radio button
  6. 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.
  1. In Eclipse select the Android Java project you need to convert
  2. Right click with the mouse and select Android Tools -> Add Native Support

1.3. Other Pre-Requisites


  1. The platform must be Android 2.2 (android-8) or later
  2. The ndk version must be r4b (it contains bugfixes to ndk-gdb that are necessary) or later
  3. Eclipse CDT 7.0 or newer must be installed
  4. The AndroidManifest.xml must have the property of the application node android:debuggable="true"
  5. 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
  • 03) Set the following properties:

    http://www.eclipse.org/sequoyah/images/native_debug_2.png
  • 04) The process launcher must be the Standard Process Launcher. This is selected at the bottom of the Main tab:

    http://www.eclipse.org/sequoyah/images/native_debug_3.png
  • 05) On the "Main" tab:
    the Field C/C++ Application: $PROJECT_PATH/obj/local/armeabi/app_process
  • 06) On the "Debugger" tab:
    • field Debugger: gdbserver
    • On the "Main" subtab:

      http://www.eclipse.org/sequoyah/images/native_debug_4.png
    • 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
    • On the "Connection" subtab:

      http://www.eclipse.org/sequoyah/images/native_debug_5.png
    • 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.
After following these steps, one can continue to debug the application as usual, using the "continue" option to let the execution flow until the next breakpoint is hit or by using the usual "step-in" to execute each statement individually. Setting a breakpoint on a Java statement that calls a native function through JNI and stepping into will place the user at the beginning of the native code.
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.
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in Android, ARM | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • How to build Python-4-Android for the ARM Neon
    Currently the Py4A project does not compile for the ARM Neon architecture. If you try to run ndk-build on the project by setting the APP_A...
  • Problems with new version of rpmbuild
    The Problem With the new version of rpmbuild installed on CentOS 6.x, if you try to use an old RPM spec file, you will get an error like the...
  • How to build the gcc Fortran cross-compiler for Android (ARM and x86)
    If you need to cross-compile for Android a program written in Fortran, you know already that the official Android NDK does not come with the...
  • Upgrading mid-2007 24in iMac with an SSD and a 2.5in HDD in the optical bay
    I own a mid-2007 24in iMac with a 2.4 GHz Intel Core 2 Duo processor, 4GB of DDR2 DRAM, running Mac OS Mountain Lion, and for the past few m...
  • Porting your Legacy C/C++ project to Android
    This is a recurring problem people have often: trying to port a big C/C++ project to the Android platform. You have thousands of lines of te...
  • How to inspect expanded C macros with gcc/gcc+
    Sometimes you get compilation errors in gcc/g++ and you'd like to inspect the output from the C compiler pre-processor to figure out why...
  • Android: Trying to load native library results in a process terminated by signal (11)
    Symptoms You are trying to load your native C/C++ library in an Android application and when at runtime your app calls the System.loadLibrar...
  • Arduino and 7-segment LED counter driven by two tactile switches
    I have posted on YouTube a couple of videos about a project I made with the Arduino prototype board. The circuit uses an Arduino mini and ...
  • How to build Python-4-Android for the x86
    Currently the Py4A project only compiles for the ARM architecture. The following patch for the python-build sub-directory allows you to cros...
  • How to port Mozilla SpiderMonkey 1.7 to Android
    Another third-party package I needed to cross-compile for Android was Mozilla's SpiderMonkey 1.7 Javascript engine. I found two issues h...

Categories

  • Android
  • Apple
  • Arduino
  • ARM
  • busybox
  • CentOS
  • DHCP
  • Fortran
  • GNU
  • GPON
  • iMac
  • Javascript
  • ksh
  • Linux
  • MacOSX
  • Mips
  • Network
  • Python
  • Router
  • UNIX
  • Windows
  • x86

Blog Archive

  • ▼  2013 (12)
    • ►  October (1)
    • ▼  August (1)
      • How to debug Android Native Code with Eclipse
    • ►  May (1)
    • ►  February (3)
    • ►  January (6)
  • ►  2012 (11)
    • ►  December (2)
    • ►  November (2)
    • ►  October (7)
Powered by Blogger.

About Me

Unknown
View my complete profile