1:弄一个和平台相关的程序放入手机
#adb push memtest /data/bin
2:启动gdbserver
我们编译出来的系统都已经自带了gdbserver,如果没有,例如真机,可以在prebuilt里面找到编译好的安装上去
这里我们直接在adbshell中启动gdbserver
$adb shell
#gdbserver :1234 /data/bin/memtest
正常的话应该显示:
Process/data/bin/memtest created; pid = 571
Listeningon port 1234
3:启动arm-eabi-gdb进行调试
然后在另一个终端里面启动gdb客户端
A.首先设置模拟器端口转发:
$adb shell
#adbforward tcp:1234 tcp:1234
B.启动arm-eabi-gdb:
在Android源码的prebuilt/linux-86/toolchain/arm-eabi-4.4.3/bin下面有这个程序,当然你也可以选择其他版本的gdb,argc为程序参数
#~/source_code/prebuilt/linux-86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-gdb ~/source_code/out/target/product/generic/obj/EXECUTABLES/memtest_intermediates/LINKED/memtest[args]
正常启动后会显示:
GNUgdb (GDB) 7.1-android-gg2
Copyright(C) 2010 Free Software Foundation, Inc.
LicenseGPLv3+: GNU GPL version 3 or later<http://gnu.org/licenses/gpl.html>
Thisis free software: you are free to change and redistribute it.
Thereis NO WARRANTY, to the extent permitted by law. Type "showcopying"
and"show warranty" for details.
ThisGDB was configured as "--host=x86_64-linux-gnu--target=arm-elf-linux".
Forbug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Readingsymbols from/work_dir/android4.0.3/out/target/product/generic/obj/EXECUTABLES/memtest_intermediates/LINKED/memtest...done.
(gdb)
C.执行setsolib-xxx 这两个命令,建立符号链接
(gdb)set solib-absolute-prefix ~/out/target/product/generic/symbols/
(gdb)set solib-search-path ~/out/target/product/generic/symbols/system/lib/
D.连接gdbserver进行调试:
(gdb)target remote :1234
成功的话显示:
Remotedebugging using :1234
Readingsymbols from/work_dir/android4.0.3/out/target/product/generic/symbols/system/bin/linker...done.
Loadedsymbols for/work_dir/android4.0.3/out/target/product/generic/symbols/system/bin/linker
__dl__start() at bionic/linker/arch/arm/begin.S:35
35 mov r0, sp
(gdb)
另一个控制台会显示:Remotedebugging form host 127.0.0.1
如果没执行setsolib-×××的两个命令会显示如下信息:
warning:Unable to find dynamic linker breakpoint function.
GDBwill be unable to debug shared library initializers
andtrack explicitly loaded dynamic code.
0xb0000100in ?? ()
如果list查看不了,说源文件不在,我们可以查看源文件路径,并且设置文件路径
(gdb)show directories
(gdb)set directories xxxx/android
4.最后终于可以进行gdb调试了
Gdb调试工具是基于命令行的,调试命令可以参考如下连接:
http://blog.csdn.net/dadalan/article/details/3758025
设置断点命令:breakpointn或bn,n为程序行号或是函数名称,例如在main函数出打上断点
(gdb)b main
Breakpoint1 at 0xa504: file system/extras/tests/memtest/memtest.cpp, line 107.
从断点开始继续执行:continue或c,例如:
(gdb)c
Continuing.
Breakpoint1, main (argc=1, argv=0xbea51c84)
atsystem/extras/tests/memtest/memtest.cpp:107
107 if (argc == 1) {
单步执行:next或 n,例如:
(gdb)n
106 {
(gdb)n
107 if (argc == 1) {
(gdb)n
108 usage(argv[0]);
(gdb)n
109 return 0;
打印变量值:printparam 或 pparam,param为变量名,例如打印argc的值:
(gdb)p argc
$1= 1
含有很多有用的命令,查看当前运行程序的源码:list或l,查看函数堆栈:bt,查看断点信息:infobreak,设置观察点:watch,退出gdb:q,终止程序:kill。
程序调试运行完后,启动gdbserver的客户端会打印程序的运行结果,并停止server,如果要重新开始调试,不要忘了先启动gdbserver,再启动gdbclient。
这里只介绍了Androidgdb简单的使用方法,还有很多东西可以研究,例如调试动态库so,当然gdb本身还是有很多研究的地方的,甚至有着图形化工具不能完成的功能。有机会继续研究~
参考:
http://sourceware.org/gdb/onlinedocs/gdb/
http://maricoliu.iteye.com/blog/1517675