参考文章:Debug Native c/c++ Application for Android(Step by Step)
和Google Android Debugging Utilities
首先,我们需要一个含有调试信息的exe文件,一般这个文件存在于out/target/product/generic/obj/EXECUTABLES/yourexefile_intermediates/LINKED/libomstts目录下,这里以我们前面文章中生成的helloworld为例。
1. 下载Android Debugging Utilities
2. 启动Android Emulator模拟器
$ emulator @1.5_R2
3. 把gdb和调试文件以及源代码放到模拟器上
$ adb push gdb /data/bin
$ adb push helloworld /data/bin
以helloworld为例,源代码要放在/data/bin/development/hello目录下。
有的文章说将以上文件放在/system/bin目录下,但后果是一旦关掉模拟器,这些的文件就消失了,下次又要重传~所以推荐放在/data/bin目录下。
4. 启动shell
$adb shell
如果提示文件系统不可写的话,执行:
#mount -o remount
5. 因为Android文件系统没有/bin/sh, 所以这里有两种解决方法(推荐第二种方法):
(1). 拷贝一个bash文件(点击下载)到/bin目录下:
#mkdir /bin
$adb push bash /bin
(2). 或者设置SHELL的路径:
#export SHELL=/system/bin/sh
6. 进入/data/bin目录,运行gdb,可以开始调试程序了
#cd /data/bin
#./gdb helloworld
转载请注明: 转载自不然你要我怎么样 本文链接地址: 使用gdb在Android Emulator中进行调试c程序

你好,我有几个问题想请教,在你的第五步提到“Android文件系统没有/bin/sh”,但是实际上在/system/bin/下是有sh文件的,用# mkdir /bin时无法建立sh文件夹的,我是用adb push bash /system/bin/sh将bash文件加入到sh(?奇怪为什么文件能够添加进文件,sh有什么特别的含义?)文件
另外我运行#cd /data/bin
#./gdb helloworld会提示./gdb permission denied,权限为什么不允许呢?
还有,调试的时候是否要用debug模式呢
博主 对 huangxiao0322 的回复: 2009-09-08 11:19:03
1. /system/bin/下的确是有sh文件,但GDB工具默认要求的sh路径为/bin,所以我们一是拷贝一个shell文件到bin目录,二就是手动设置SHELL为/system/bin/sh。的确mkdir bin这个命令不能用,所以最好用第二种方法。adb push bash /system/bin/sh这个命令有问题吧?不应该这么用。
2. 没有权限?你试试chmod。
3. 什么debug模式?不是太明白。
希望有帮上忙:)
huangxiao0322
7 九 09 at 18:59
1.第三步中$ adb helloworld /data/bin,helloworld 是含有调试信息的exe文件吗?你是用android源码方式编译的吧,我没有下载源码,用的是NDK的make APP方式编译的,那我在哪里找该文件呢?而且编译后的是.SO文件啊,EXE文件不是在windows下才有的吗?另外$ adb helloworld /data/bin是将helloworld上传到/data/bin目录下吧,这么不用$ adb push helloworld /data/bin?
2.我在/data/bin下面有gdb 文件与development/h264Sybian文件夹,我的源代码放在了development/ h264Sybian下面。在我执行chmod 777 development和chmod 777 gdb后,执行#cd /data/bin
#./gdb h264Sybian,
出现dlopen failed on ‘libthread_db.so.1′ – libthread_db.so.1: cannot open shared object file: No such file or directory
GDB will not be able to debug pthreads.
GNU gdb 6.7
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "–host=arm-none-linux-gnueabi –target=".
(gdb)
是否现在可以调出源代码断点调试了呢,但是这样怎么关联我已经打包好了的so包呢,我的目的是在NDK中如何跟踪调试打包好SO库,以下是我发的帖子
http://androidin.net/bbs/thread-14613-1-1.html
huangxiao0322
8 九 09 at 14:37
我原来已经用C写好了一个视频解码的算法,但是打包成SO文件后供我的Android工程调用后发现只能执行到一部分,我现在想要断点查找到底在哪里出现了问题。原来的C谢的视频解码的算法能正确解码成功的。
还有,博主的联系方式是***吧,请问QQ号码能否留下,以后多多请教
huangxiao0322
8 九 09 at 14:46
我在输入./gdb h264Sybian后出现如下命令
dlopen failed on ‘libthread_db.so.1′ – libthread_db.so.1: cannot open shared object file: No such file or directory
GDB will not be able to debug pthreads.
GNU gdb 6.7
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "–host=arm-none-linux-gnueabi –target=".
h264Sybian:no such file or directory(是否是我没有加入含有调试信息的exe文件吗?)
(gdb)
博主 对 huangxiao0322 的回复: 2009-09-08 17:25:23
1. 我用的helloworld是带有调试信息的可执行文件(我可没说它是exe文件);对我是用源码编的,没用过NDK所以这个我也不知道;写错了不好意思(包括之前的mkdir /bin部分),现在已经改过来了。
2. 可以调试了。不过你的.so文件是动态库么?目前Android对Native code动态库的调试似乎没有好的方法,目前我还没发现。你看能不能做成静态。
3. 对的,欢迎mail相互学习。我msn是xiangmocheng#hotmail.com(把#换成@)。
4. 第一次不是好好的么,文件怎么又没了?
huangxiao0322
8 九 09 at 15:47
你的helloworld是带有调试信息的可执行文件后缀名是什么呢?
你好,我按照如下步骤操作:
adb shell
cd /data/bin
./gdb libqcif.so(这个是我用NDK大宝h264Sybian的SO文件)
出现如下信息
dlopen failed on ‘libthread_db.so.1′ – libthread_db.so.1: cannot open shared object file: No such file or directory
GDB will not be able to debug pthreads.
GNU gdb 6.7
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "arm-none-linux-gnueabi –targe".
(gdb)
我输入l 命令结果提示:sources/samples/h264Sybian/main.cpp no such file or directory
in ources/samples/h264Sybian/main.cpp
这里的ources/samples/h264Sybian是我的NDK下面的sources文件夹,里面的确是有文件的啊
是否是源代码放在模拟器中的位置不对呢
我是按照你上面说的在/data/bin/development下面新建h264Sybian文件夹,然后我自己所有的源代码都放在该文件夹下面的啊。(疑问h264Sybian文件夹为什么不直接放在/data/bin下面)
我的libqcif.so放在/data/bin下面
huangxiao0322
8 九 09 at 19:54
哥们,我知道是为什么了,/data/bin/development该文件夹的建立应该是
/data/bin/sources/samples/h264Sybian,与自己的NDK根目录下面的存放h264Sybian中的文件的目录名相同,谢谢你拉
以后多多交流交流
博主 对 huangxiao0322 的回复: 2009-09-08 23:56:58
没有后缀名,就是个可执行文件。。。运行gdb时出现那么一大串是因为这个gdb工具不支持线程的调试吧。恩客气。
huangxiao0322
8 九 09 at 20:23
老兄,昨天都能够分布调试了,今天竟然从新打包SO后不成功了,我用file libqcif.so来查看是有not stripped代表含有debug信息啊。但是我用./gdb libqcif.so后提示no debugging symbol found。兄弟你有没有遇到这个问题。我的MSN是jt_tzh@hotmail.com,你上线吧,发个信息给我
huangxiao0322
9 九 09 at 15:55
我解决了上个问题了,应该将out/apps中的.SO文件导入进data/bin中,不应该该是apps中的SO文件。
但是现在又有一个问题,怎么样才能够在Android工程中进入到.C的源代码中调试呢,因为.C的入口函数是符合JNI规范的,按照楼主的方式我仅仅是通过./gdb libqcif.so调出了其对应的.C源代码。请问怎么样在Android的工程中分步调试呢。
另外楼主好像很少登录MSN哦
huangxiao0322
10 九 09 at 11:17