вівторок, 29 березня 2011 р.

Using Eclipse for debugging Android native app

I did lots of "try-and-fail researches" to get debugging of native Android application under emulator. Unfortunately I couldn't find enough information about this topic online, so I'd like to share my achievements.
BTW, You can do debugging on real phone this way, too.

Here are some steps how I did it:


* PREREQUISITES

Basic linux experience.
Get familiar with following docs:
      http://source.android.com/source/overview.html
      http://source.android.com/source/licenses.html
      http://source.android.com/source/download.html
      http://source.android.com/source/git-repo.html
      http://source.android.com/source/using-eclipse.html
3Gb+ RAM recommended.

* LINUX


The Android build is routinely tested in house on recent versions of Ubuntu (10.04 and later),
but most distributions should have the required build tools available.

Configuring on Fedora 13 (that was my case)

Remove JDK 1.6 and install JDK 1.5 (update_22) instead (for Android 2.1 and 2.2)

Install packages:
      $ sudo yum install openssh-­clients git make gnupg flex bison gperf zip zlib-devel
             ncurses-static ncurses-­devel readline-devel gcc-c++ gcc libX11-devel
             wget meld mc patch subversion vim-X11 ccache kdesvn

Install repo:
      $ mkdir ~/bin
      $ curl http://android.git.kernel.org/repo > ~/bin/repo
      $ chmod a+x ~/bin/repo

* GETTING SOURCES

First, create directory for android files:
      $ mkdir ~/android

Basic folder layout we will create:
      ~/android/
      ~/android/src            Sources of Android
      ~/android/sdk            Android SDK
      ~/android/ndk            Android NDK
      ~/android/tools            Android toolchains from the latest repo
      ~/android/cache            ccache cache

* SETTING-UP ENVIRONMENT VARIABLES

Add following to ~/.bashrc :

[=== ... start of .bashrc =================]

############################################
# Setup Paths
############################################

export USE_CCACHE=1
export CCACHE_DIR=$HOME/android/cache/
export JAVA_HOME=/usr/java/jdk1.5.0_22/bin/

export PATH=./:$PATH
export PATH=$PATH:$HOME/bin
export PATH=$PATH:$HOME/android/sdk/tools
export PATH=$PATH:$HOME/android/sdk/platform-tools
export PATH=$PATH:$HOME/android/tools/linux-x86/toolchain/arm-eabi-4.4.3/bin/
export PATH=$PATH:$JAVA_HOME

[=== ... end of .bashrc ===================]

* BUILDING SOURCES

      $ cd ~/android/src
      $ source build/envsetup.sh
      $ lunch 1
      $ make
      ... you can relax, it can build several hours

or... $ make showcommands
to print commands that are executed
or... $ make -j2
to build using 2 threads on multi-core system
and... there are many commands, you can combine them

you can also build selected modules using:

      $ cd ~/android/src
      $ source build/envsetup.sh
      $ lunch 1
      $ mmm <module_path>
      $ mmm <module_path>
      $ mmm <module_path>
      $ make snod


* USING ECLIPSE FOR REMOTE DEBUGGING

10 simple steps to get Eclipse debugging android native app:

1. Eclipse setup
   Window -> Preferences
      General -> Content Types
      Text -> C Source Code -> C++ Source File
          Click Add "*.inl"

2. File -> New -> C++ Project
      Set project name: Android
      Uncheck "Use default location"
      Select ~/android/src/ for location
      Select Makefile Project/Empty Project in Project type
      Select Linux GCC toolchain

3. Open Project Properties
      Open C/C++ Build - Builder Settings
      Set "Build command" to "bash"
      Open C/C++ Build - Behaviour
      Uncheck "Auto build", "Clean"
      Set "Incremental build" to "fastmake.sh"
      Open C/C++ General - Path and Symbols - Source Location
      Add ./external/libmobdrm ./external/filemon ./external/testdrmcontent directories
      Open Output Location tab
      Add ./out directory
      Click OK

4. Open Debug Configuration
      Add new C/C++ Application
      Name: filemon
      On the bottom, click "Select other..." and select "GDB (DSF) Remote System Process Launcher"
      On Main tab:
      Application: /home/iev27302/android/src/out/target/product/generic/symbols/system/bin/filemon
      Select "Disable auto build"
      Select Debugger tab:
      On Main tab:
          GDB Debugger: arm-eabi-gdb
          GDB command file: gdbinit.gdb
          You may select "Force thread list update on suspend"
      On Shared Libraries tab:
          Add "out/target/product/generic/system/lib" directory
      On Connection tab:
          Select Type: TCP
          Address:     localhost
          Port number: 5039
      On Source tab:
      You may remove "Default" source lookup path, and add only source paths you need.
      (This will speed-up debugger launch significantly)
      Click Apply.

5. Build android from eclipse
6. Launch emulator
7. In terminal:
$ adb forward tcp:5039 tcp:5039
$ adb shell gdbserver :5039 <my_app_path> <app_params>
8. Click "Debug" in Eclipse
9. Wait until it launches debugger (this may take 2-4 minutes if you have "Default" source lookup path)
10. Enjoy debugging ;)

P.S. Be sure to be compiling your application with debugging info enabled (-g switch), etc...

Немає коментарів:

Дописати коментар