http://mhandroid.wordpress.com/2011/01/25/how-cc-debugging-works-on-android/

How debugging of C/C++ code works on Android? Nothing special actually.Gdbitself has a feature for remote debugging.

Note: For this article I am using Android NDK, r5. The behavior ofndk-buildandndk-gdbcommands can be different in other versions.

Basic setup

You simply rungdbserveron the device and let it attach to some process.Gdbserveracts as a remote debugger which is commanded bygdbitself. This step is pretty easy and is done by some variant of

gdbserver :5039 --attach 123

where5039is a TCP port number to whichgdbwill connect and123is aPIDof process we want to debug.

The next step is to rungdbclient and connect it to thegdbserveron the device. You need to startgdbwith command

gdb app_process

whereapp_processis Android binary which you need to copy from Android device to your PC. This binary is some kind of loader and lies in/system/bin/app_processfile on the device (or in the emulator).

After you startgdbyou connect it with thegdbserverby this command (ran from gdb shell)

target remote :5039

Because you are usually running Android device connected through USB and not normal TCP/IP network (or you are running Android emulator) you have to set up TCP port forwarding in advance. This is done by command

adb forward tcp:5039 tcp:5039

which you have to execute before startinggdb. This command will handle all connection requests to port 5039 on PC to the device on the same port.

And this is all.

Debug symbols

You typically compile your .so library with debug symbols. But it makes good sense to strip those symbols off before uploading your binary to the Android device. Binary will be smaller (which means faster installation time of the apk package), will occupy less memory and will run faster. So you upload stripped version of your libraries to the Android device and keep unstripped versions on your PC.

To tellgdbthe path to the unstripped libraries run this command from gdb shell

set solib-search-path obj/local/armeabi

You can enter more paths and separate them by colon (:) character.

There is a difference between debug symbols and code optimization. Code optimization allows changing the code flow or removing some dead-code and unused variables. While debug symbols bind machine code together with appropriate lines in the source code. Usually you build a binary with debug symbols and no code optimization for debugging and another binary without debug symbols and with some code optimization for release. But you can use debug symbols together with code optimization. In this case some variables will be missing and some lines of code will be probably jumped over but in general you can debug this way.

Androidndk-buildcommand always produce binaries (your libnative.so files) with debug symbols and place them inobj/local/armeabisubdirectory of your project. Then it strips off debug symbols and place stripped version in the final .apk (and thus in the device).ndk-gdbscript uses thisset solib-search-pathcommand to pointgdbto the unstripped binary so you can see your position in the source code. You can find theset solib-search-pathcommand inobj/local/armeabi/gdb.setupfile.

If you haveandroid:debuggable=”true”in yourAndroidManifest.xmlor if you runndk-buildwithNDK_DEBUG=1argument thenndk-buildwill produce binaries with no code optimization. Otherwise code optimization will be used.

Sources

Gdb needs to have access to your source files. Otherwise it cannot show you the source code as it is executed. You can configure directory with your source files by command

directory jni

You can enter more paths and separate them by space. Take a look intoobj/local/armeabi/gdb.setupfile for example.

Howndk-gdband friends works

If you are using Android NDK, r5 and Android 2.3 device you can usendk-gdbscript which does everything for you.

If you haveandroid:debuggable=”true”in yourAndroidManifest.xmlthenndk-buildwill add thegdbserverinto your .apk package. Thisgdbserverwill be started byndk-gdband also all the other steps will be set up by this script.

Important caveat

Becausegdbserveris attached to the already running process (as opposed to situation where process would be started bygdbserver) it can miss some code execution which take place soon after the application start. There is no easy solution to this.

I usually write some endless while loop and then change the control variable aftergdbis fully started. For example

1 inti = 0
2 while(!i) {
3 a++;
4 }

Afterndk-gdbstartsgdbsession I can set breakpoints appropriately and change value ofivariable by command

set var i=1

and then continue in application execution bygdbcommandc.

Multithread debug problem

It is well-known problem thatndk-gdbshipped with Android NDK, r4b used on Android 2.2 device was only able to hit the breakpoint on the main thread. If you set breakpoint on any other thread gdb session will crash.

Every time new thread is createdlibcwill call_thread_created_hook()function (which has empty body). The purpose of this function is thatgdbwill set breakpoint on this function and thus will know that new thread was created.Gdbwill then keep database of existing threads. Because of thisgdbneeds to have access to thelibc.sofile to figure out address of the_thread_created_hook()function.

Android NDK, r5 containsndk-gdbwhich copies the filelibc.sofrom the device (or emulator) and stores it inobj/local/armeabisubdirectory of your project. Alsogdbserverincluded in this ndk is compiled withlibthread_dbsupport. Both those two things are missing in Android NDK, r4b.

Links

Description of multithreading debug bug.
http://code.google.com/p/android/issues/detail?id=9713

Android NDK, r5
http://dl.google.com/android/ndk/android-ndk-r5-linux-x86.tar.bz2

Android ndk main page
http://developer.android.com/sdk/ndk/index.html

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android 属性系统使用的小问题
  2. Android 3.0,将为对战iPad归来
  3. Android 系列 5.9使用Inkscape创建Androi
  4. android 2.3 从零开始学习—环境的搭建以
  5. Android面试系列2018总结(全方面覆盖Andr
  6. Android和设计模式:模板模式
  7. [Android] Android进程和线程模型
  8. android就一定比java差?
  9. 安卓(android)开发应该怎么学?需要哪些基础
  10. Android/OPhone开发完全讲义(china-pub首