`
poower
  • 浏览: 18016 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

转:Debug Native c/c++ Application for Android(Step by Step)

阅读更多

    调试Android上的c/c++程序一直是个难题,以前我经常靠输出log来解决问题,对于稍复杂一些的工程,这几乎是个不可能完成的任务,尤其有些错误,在wincewindows下都没事,只在android上出现,就更难找了。后来看了些资料,知道可以用gdbserver来调试,今天决定必须把这个先弄清楚,不然以后干活效率实在是太低了,找了很多网站,终于成功了。这里把整个过程整理一下,以备以后查阅。

    1. 准备gdbserver。
    android 1.0 代码刚开放到时候,里面并没有带gdbserver,有些强人就自己编译了gdbserver来使用。不过现在好了,android的新源码里已经包含了gdbserver,就在prebuilt目录下。如果想在android 1.0里使用,可以到如下地址下载:http://android.git.kernel.org/?p=platform/prebuilt.git;a=tree。gdbserver的二进制文件就在android-arm/gdbserver/gdbserver,我们只需要把gdbserver这个可执行文件放到模拟器上即可。
    准备把它放在/system/bin,这样它就在默认的PATH里了。启动模拟器后:
    adb push gdbserver /system/bin
    如果提示文件系统不可写的话,执行一下“adb remount”,因为默认是用只读模式挂载的。

    2. 编译程序。
    默认情况下,android的编译系统在编译程序时已经使用了“-g”选项,即已经生成了调试信息。但是在生成最终的文件时,是经过strip的,去除了所有到调试信息。所以最终我们到调试目标要使用strip之前的文件。用make showcommands查看详细的命令信息,下面是其中某次的输出:
target Executable: libomstts (out/target/product/generic/obj/EXECUTABLES/libomstts_intermediates/LINKED/libomstts)
prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin/arm-eabi-g++ -nostdlib -Bdynamic -Wl,-T,build/core/armelf.x -Wl,-dynamic-linker,/system/bin/linker -Wl,--gc-sections -Wl,-z,nocopyreloc -o out/target/product/generic/obj/EXECUTABLES/libomstts_intermediates/LINKED/libomstts -Lout/target/product/generic/obj/lib -Wl,-rpath-link=out/target/product/generic/obj/lib -llog -lcutils -lutils -landroid_runtime -lnativehelper -lstdc++ -lz -lc -lstdc++ -lm  out/target/product/generic/obj/lib/crtbegin_dynamic.o         out/target/product/generic/obj/EXECUTABLES/libomstts_intermediates/src/tts.o out/target/product/generic/obj/EXECUTABLES/libomstts_intermediates/src/TTSWrapper.o out/target/product/generic/obj/lib/crtend_android.o
target Non-prelinked: libomstts (out/target/product/generic/symbols/system/bin/libomstts)
out/host/linux-x86/bin/acp -fpt out/target/product/generic/obj/EXECUTABLES/libomstts_intermediates/LINKED/libomstts out/target/product/generic/symbols/system/bin/libomstts
target Strip: libomstts (out/target/product/generic/obj/EXECUTABLES/libomstts_intermediates/libomstts)
out/host/linux-x86/bin/soslim --strip --shady --quiet out/target/product/generic/symbols/system/bin/libomstts --outfile out/target/product/generic/obj/EXECUTABLES/libomstts_intermediates/libomstts
Install: out/target/product/generic/system/bin/libomstts
out/host/linux-x86/bin/acp -fpt out/target/product/generic/obj/EXECUTABLES/libomstts_intermediates/libomstts out/target/product/generic/system/bin/libomstts
    生成的可执行文件是libomstts,可以看到,初次链接的目标文件是“out/target/product/generic/obj/EXECUTABLES/libomstts_intermediates/LINKED/libomstts”,然后拷贝到“out/target/product/generic/symbols/system/bin/libomstts”,strip后的文件是“out/target/product/generic/obj/EXECUTABLES/libomstts_intermediates/libomstts”和“out/target/product/generic/system/bin/libomstts”。调试只能使用前两个文件。
    把带调试信息到可执行文件放到模拟器上,我用到是“out/target/product/generic/symbols/system/bin/libomstts”:
    adb push out/target/product/generic/symbols/system/bin/libomstts /system/bin

    3. 启动调试器
    首先在模拟器上启动gdbserver:
    adb shell
    进入模拟器的控制台后
    gdbserver 10.0.2.2:1234 /system/bin/libomstts
    10.0.2.2是模拟器的默认ip地址,让gdbserver在模拟器上监听1234端口。如果启动成功会显示以下信息:
Process /system/bin/libomstts created; pid = 1025
Listening on port 1234
    为来让gdb能连接到模拟器上到gdbserver,必须进行数据转发:
telnet localhost 5554
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Android Console: type 'help' for a list of commands
OK
redir add tcp:1234:1234
OK
exit
    上面的telnet localhost 5554,redir add tcp:1234:1234,exit是自己输入的命令,其他的是输出信息。5554是模拟器控制台的监听端口,这些命令是将所有到localhost:1234的数据转发到模拟器的1234端口。
    最后在本机启动gdb:
    arm-eabi-gdb out/target/product/generic/symbols/system/bin/libomstts
    arm-eabi-gdb是android自带的toolchain里的,注意后面的可执行文件是strip之前的。
    gdb启动后,在gdb里输入命令连接gdbserver:
    target remote localhost:1234
    连接到gdbserver成功后,就可以使用所有的gdb调试命令啦
    
    现在的这个gdbserver还不能调试动态链接库,只能先编译成可执行文件调试。

分享到:
评论

相关推荐

    C/C++ DevTools Support (DWARF)

    C/C++ DevTools Support (DWARF)离线安装包,里面是crx文件,如果不知道goole crx文件怎么安装的请自行搜索

    Android代码-Wifi局域网聊天

    This repository contains the source code for the WifiChat. Please see the issues section to report any bugs or feature requests and to see the list of known issues. Debug mode: // src/szu/wifichat/...

    安卓设备读取USB外设信息

    可以通过AS调试读出 , 过滤关键字 "Li_Debug" , 一次显示使用USB外设 格式举例 厂商/VendorId: 0x10C4 产品/ProductId: 0xEA60 设备/DeviceId: 0x7D2 接口数/InterfaceCount: 1 描述/describeContents = 0 设备类/...

    Microsoft Visual C++/CLI Step by Step Aug 2013 PDF ePub

    Microsoft Visual C++/CLI Step by Step (pdf + ePub) Publisher: Microsoft Press (August 2, 2013) Language: English ISBN-10: 0735675171 ISBN-13: 978-0735675179 Your hands-on guide to Visual C++/CLI ...

    c++ 连接 mysql 官方文档

    Contents ?MySQL C++ Driver Based on JDBC 4.0 Specification ...Debug Tracing with MySQL Connector/C++ ?For More Information ?About the author ?Appendix I: Installing MySQL Connector/C++ from Source

    SQLite数据库 sqlitedll库文件 sqlite驱动JAR包 sqlite工具

    一定要保证在类路径ClassPath中有该jar包,并且保证在JAVA库路径JAVA Library Path中有本地库Native Library(\workspace\"Web应用"\WebRoot\WEB-INF\lib\下最好也要加入该jar包)。"SQLite.JDBCDriver"作为JDBC的...

    基于C语言功能模块备用:配置文件读取 /debug日志记录 等

    c语言文件读写操作代码 基于C语言功能模块备用:配置文件读取 /debug日志记录 ...等等

    微信小程序 wechat-app-demo.zip

    webApp-demo 微信小程序 demo / 开发工具 / 文档资源 ...开发工具: https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html?t=1474644089359 标签:小程序 wechat demo

    C语言功能模块备用:配置文件读取 /debug日志记录 ...等等

    c语言文件读写操作代码 C语言功能模块备用:配置文件读取 /debug日志记录 ...等等

    app-debug.apk

    [{"url":"http://192.168.0.104:8080/app-debug.apk","versionCode":5,"versionName":"1.4.20161008","updateMessage":"版本更新为4"}]

    Debug c/c++program with eclipse

    Debug c/c++program with eclipse

    C++自动生成数字地面模型dem

    Debug 2017/10/21 20:50 文件夹 res 2017/10/21 20:50 文件夹 CLine.h 2008/12/3 13:28 C/C++ Header 0 KB contour.aps 2008/12/8 13:48 APS 文件 29 KB contour.clw 2008/12/8 15:18 CLW 文件 3 KB contour.cpp ...

    浅谈Android Studio如何Debug对应so文件C/C++代码

    在C/C++跨平台开发中,我们知道在Windows上可以通过VS,进行单步断点...1、The Android Native Development Kit (NDK) : 让你能在 Android 上面使用 C 和 C++ 代码的工具集。 2、CMake 外部构建工具。如果你准备只使用

    基于X86的C/C++ 反编译器

    熬夜一年多,终于把C/C++语言反编译器的演示版本搞出来了! 目前C语言的基本反编译功能已完成,C++部分刚刚开了个头,正在开发中。此版本非常不稳定,仅做演示用!它能很好的反编译自带的56个测试例子,但对于大家...

    DebugLog Log调试工具

    Android DebugLog调试工具,此资源对应的博客地址为:http://blog.csdn.net/bbld_/article/details/39757099

    《魔法门之英雄无敌III》的开源引擎-C/C++开发

    VCMI项目VCMI是正在进行的尝试,旨在为Heroes III重新创建引擎,从而为其提供了新的和扩展的可能性。...持续集成和CMake配置广告不断测试Android Linux macOS Windows Source from Source平台支持的持续测试

    vscode_c_debug_set_launch_task.zip

    使用vscode-1.61.2,c/c++插件1.7.1。...缺点:由于c/c++版本匹配的问题,配置好之后进行debug断点调试,不会在断点处停留,而是直接生成结果,尝试了很多版本都没有解决这个问题。 所以,请谨慎下载该文件。

    Pyhton 2.6 debug file

    python26_d.lib c 调用Python的 debug 文件, 一定要自己build ,check out 出来 看看 有 30M,压缩代码

    android调试sdcard文件加载,解决不能传入文件问题

    android调试sdcard文件加载,解决不能传入文件问题: 只需要执行4个命令,win和mac下都好使。

    NTGraph测试程序(有问题)

    for infomation on how your program can cause an assertion failure, see the visual c++ documentation on asserts 最后跳出: Unhandled exception in controltest2.exe(mfc42d.dll):0xc0000005:access ...

Global site tag (gtag.js) - Google Analytics