GDB + gdbserver 远程调试android native code

http://blog.csdn.net/chen_chun_guang/article/details/6080570

 

 以调试模拟器中的native library code为例。

Host: ubuntu
Target: Android ICS

1.将gdbserver放入设备。

确保系统有arm-*-gdb,及存在设备端将要运行的gdbserver,gdbserver可以由google ndk中获取,在ndk的如下目录可以找到这个文件:

 

点击(此处)折叠或打开

  1. android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt

 

然后,通过adb shell进入设备,在/data下创建bin 目录:

点击(此处)折叠或打开

  1. hanpfei@hanpfei-F6Ve:/mnt/android_proj/android_src$ adb shell
  2. # mkdir /data/bin/

 

再通过adb push命令将gdbserver 放入设备:

 

点击(此处)折叠或打开

  1. hanpfei@hanpfei-F6Ve:/mnt/android_proj/softwares/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt$ ls
  2. gdbserver linux-x86
  3. hanpfei@hanpfei-F6Ve:/mnt/android_proj/softwares/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt$ adb remount
  4. remount succeeded
  5. hanpfei@hanpfei-F6Ve:/mnt/android_proj/softwares/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt$ adb push gdbserver /data/bin/
  6. 543 KB/(125208 bytes in 0.225s)

2、在设备端启动gdbserver并attach到所要debug的进程

 

点击(此处)折叠或打开

  1. system 1168 37 156620 31924 ffffffff 40011384 S com.android.keychain
  2. app_34 2758 37 158360 34572 ffffffff 40011384 S com.hanpfei.text
  3. root 2774 617 900 348 00000000 40010438 R ps
  4. gdbserver :1234 --attach 2758
  5. Attached; pid = 2758
  6. Listening on port 1234

 

gdbserver 后面跟的第一个参数为端口号,之后的--atach 为gdbserver的选项,之后的数字为所要调试的进程的PID。

3、在Host端启动arm-*-gdb

网上有高手build的各种交叉编译工具链来方便大家的嵌入式开发,单这个工具链中的gdb命令实际文件名称的前缀和后缀都会是相同的,中间的那个*可以见到各种各样的形式,比如linux,linux-androideabi,none-linux-gnueabi等等。

 

点击(此处)折叠或打开

  1. hanpfei@hanpfei-F6Ve:/mnt/android_proj/softwares/android-ndk-r8/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin$ ./arm-linux-androideabi-gdb
  2. GNU gdb 6.6
  3. Copyright (C) 2006 Free Software Foundation, Inc.
  4. GDB is free software, covered by the GNU General Public License, and you are
  5. welcome to change it and/or distribute copies of it under certain conditions.
  6. Type "show copying" to see the conditions.
  7. There is absolutely no warranty for GDB. Type "show warranty" for details.
  8. This GDB was configured as "--host=i386-linux-gnu --target=arm-elf-linux".
  9. (gdb) shell adb forward tcp:1234 tcp:1234
  10. (gdb) target remote localhost:1234
  11. Remote debugging using localhost:1234
  12. [New Thread 2758]
  13. Malformed packet(b) (missing colon): re:0;
  14. Packet: 'T050b:7476b6be;0d:b874b6be;0f:84130140;thread:ac6;core:0;'
  15.  
  16. (gdb)

使用ndk中所带的交叉编译工具链,会出现报错信息,意为畸形数据包:

 

 

点击(此处)折叠或打开

  1. [New Thread 2758]
  2. Malformed packet(b) (missing colon): re:0;
  3. Packet: 'T050b:7476b6be;0d:b874b6be;0f:84130140;thread:ac6;core:0;'

同时,在设备端也可以看到有如下的输出,来显示着这server端与client端难以协调的步调:

 

点击(此处)折叠或打开

  1. # gdbserver :1234 --attach 2758
  2. Attached; pid = 2758
  3. Listening on port 1234
  4. Remote debugging from host 127.0.0.1
  5. readchar: Got EOF
  6. Remote side has terminated connection. GDBserver will reopen the connection.
  7. Listening on port 1234

 

出现这样的状况,是由于gdbserver和arm-*-gdb版本不对应所致。由上面我们启动arm-linux-androideabi-gdb时的输出可以看到,其版本为GNU gdb 6.6。在设备上,我们可以查看gdbserver的版本信息:

 

 

点击(此处)折叠或打开

  1. gdbserver --version
  2. GNU gdbserver (GDB) 7.1-android-gg2
  3. Copyright (C) 2010 Free Software Foundation, Inc.
  4. gdbserver is free software, covered by the GNU General Public License.
  5. This gdbserver was configured as "arm-elf-linux"
  6. #

我们可以看到gdbserver版本为GNU gdbserver (GDB) 7.1-android-gg2。这也就是症结所在。

 

我们可以换用和gdbserver版本较为接近的arm-*-gdb。此时在host端所需输入的命令及相应的输出如下:

 

点击(此处)折叠或打开

  1. hanpfei@hanpfei-F6Ve:~/develop_tool_chain/arm-2011.03/bin$ ./arm-none-linux-gnueabi-gdb
  2. GNU gdb (Sourcery G++ Lite 2011.03-41) 7.2.50.20100908-cvs
  3. Copyright (C) 2010 Free Software Foundation, Inc.
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  5. This is free software: you are free to change and redistribute it.
  6. There is NO WARRANTY, to the extent permitted by law. Type "show copying"
  7. and "show warranty" for details.
  8. This GDB was configured as "--host=i686-pc-linux-gnu --target=arm-none-linux-gnueabi".
  9. For bug reporting instructions, please see:
  10. <https://support.codesourcery.com/GNUToolchain/>.
  11. (gdb) shell adb forward tcp:1234 tcp:1234
  12. (gdb) target remote localhost:1234
  13. Remote debugging using localhost:1234
  14. 0x40011384 in ?? ()
  15. (gdb)

启动gdb,然后是执行shell命令( shell adb forward tcp:1234 tcp:1234) , 以完成端口映射的任务,靠前的那个为local端的端口,靠后的那个为remote端的。
 
于此同时,在设备端也将看到有如下的输出:

 

 

点击(此处)折叠或打开

  1. Listening on port 1234
  2. Remote debugging from host 127.0.0.1

这即是表明,gdb client端和server端有成功的链接。


你可能感兴趣的:(android,gdb,gdbserver)