大家好,我是林译川,今天我们来聊聊Android开发中一个实用的小技巧——如何在Android模拟器中使用Gdb进行程序调试。
在Android系统的emulator里,我们可以通过两种方式使用Gdb进行调试:直接利用Gdb调试和GdbServer进行调试。
1. 利用Gdb Server进行调试
- A. 从这里下载针对Android的Gdb Server。
- B. 将下载的Gdb Server安装到你的emulator中。
- 1) 启动emulator。
- 2) 下载Gdb Server到你的emulator,打开一个终端,运行:
- $ adb shell
- #cd data
- #mkdir bin
- 在另外一个终端里运行:
- $ adb push gdbserver /data/bin
- C. 运行gdbserver:
- # /data/bin/gdbserver 10.0.2.2:1234 [args...]
- D. 回到另一终端,映射端口:
- $telnet localhost 5554
- telnet到emulator中,运行下面的命令:
- redir add tcp:1234:1234
- E. 在第三个终端中运行gdb:
- $gdb test
- test 是你要调试的程序,然后在gdb命令行运行:
- (gdb) target remote localhost:1234
- 然后你就可以像以前用gdb那样进行程序调试了。
2. 直接用本地Gdb进行调试
- A. 从这里下载Android调试包,它是用Android的toolchain编译好的二进制文件。
- B. 解压调试包,然后把gdb下载到你的emulator中,当然如果你有真的手机,也可以这样调试:
- $adb push gdb /data/bin
- C. 利用adb连接到emulator:
- $adb shell
- 运行gdb,会出现下面的信息:
- # /data/bin/gdb
- 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)
- D. 然后你就可以像以前用gdb那样对程序进行调试了。
以上就是Android模拟器中使用Gdb进行程序调试的两种方法,希望对大家有所帮助。
——林译川,新派编程网
想要了解更多编程知识,请访问新派编程网。
