使用GDB调试Android手机中的程序

Windows PC通过USB连接Android手机,而GDB在Linux PC上,如何调试手机上的程序? 可以使用GDB远程调试。 

 

(1)adb shell可以正常连接手机。将待调试的test.bin可执行文件push到手机。

 

(2)Windows PC和Android手机的端口映射

> adb forward  tcp:5040  tcp:5040

其中第一个tcp为windows的本地端口号,第二个tcp的端口号则为手机上的端口号。如果端口号设置失败则更换其他端口号,可能是端口被windows其他程序占用。

 

(3)启动手机上的gdbserver去监听gdb的远程连接

> adb shell gdbserver  :5040    test.bin

如果手机中没有gdbserver程序,需要从源码编译目录中获取该程序,然后adb push到手机的system/bin目录下。gdbserver在源码编译目录prebuilt\android-arm\gdbserver中。

执行成功,会有如下打印,test.bin启动,处于待调试状态:

Process  /system/bin/test.bin   created;  pid = 666

Listening on port 5040

如果监听失败,重启手机试试。

 

如果是调试一个系统中已经在运行的程序, 可以先ps查pid,然后执行命令:

> adb shell gdbserver  :5040  --attach 777

 

(4)Putty中设置Windows PC和Linux PC的端口映射

在PuTTY -> Connection -> SSH-> Tunnels中设置:

Source port 填隧道的入口端口,即 5040;

Destination 填隧道的出口端口,要添加“localhost:”,即 localhost:5040;

下面的两项分别选Remote和Auto类型;设置完成后之后,选“Add”保存。

最后注意选中Session -> Default Settings,点击“Save”,将上述设置保存为默认配置,否则下次启动Putty又要重新设置,个人感觉这是Putty比较奇葩的地方。


(5)通过Putty连接Linux PC


(6)在Linux PC中启动GDB进行远程调试

启动GDB。

在GDB中输入命令,设置符号文件目录:

set  solib-absolute-prefix   /home/Android/out/target/product/msm/symbols

set  solib-search-path       /home/Androi/out/target/product/msm/symbols/system/lib


在GDB中输入命令,远程调试Android手机:

target remote :5040


然后就可以正常调试了。


说明:

GDB的保存路径:prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin

Android程序的保存路径:

1、带调试信息 out/target/product/msm/symbols/system/bin  out/target/product/msm/symbols/system/lib

2、不带调试信息 out/target/product/msm/system/bin    out/target/product/msm/system/lib

 

你可能感兴趣的:(Android系统)