gdb调试--Android环境搭建记录

作者 by adtxl / 2021-12-22 / 暂无评论 / 433 个足迹

1. 调试准备

1.1 检查gdb和gdbserver

远程调试需要gdb和gdbserver,程调试需要在目标手机上有个类似调试客户端的东西来给gdb传送数据什么的,然后在linux上接受这些信息,进行调试。

需要注意的是,gdbserver和gdb的版本要匹配,否则会出错

使用如下命令可查看开发板中gdbserver的version

user@user-600-G5:~$ adb shell gdbserver --version
GNU gdbserver (GDB) 8.3
Copyright (C) 2019 Free Software Foundation, Inc.
gdbserver is free software, covered by the GNU General Public License.
This gdbserver was configured as "arm-eabi-linux"

可以直接使用AOSP中prebuilts目录下自带的gdb,版本一般是匹配的。

$ ./prebuilts/gdb/linux-x86/bin/gdb --version
GNU gdb (GDB) 8.3
Copyright (C) 2019 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.

1.2 设置selinux为permissive模式

使用getenforce命令可查看当前的selinux状态,如果是Enforcing模式,使用setenforce 0命令设置为Permissive模式。

xxx_evb:/ # getenforce
Enforcing
xxx_evb:/ # setenforce 0
xxx_evb:/ # getenforce
Permissive

2. 使用

2.1 连接

  1. 首先要启动调试的程序,可使用ps命令获取其进程号

  2. 启动gdbserver attach到目标进程

console:/ # gdbserver :1234 --attach 2928
[  753.203989] init: Untracked pid 4054 received signal 9
Attached; pid = 2928
Listening on port 123

:1234 表示映射成tcp的1234端口

可以看到,进程状态已经变成t了,表示attach成功了

user@user-600-G5:~$ adb shell ps -A | grep launcher
system         2928   1817 1072284 105568 0                   0 t com.xxx.tv.launcher.lite
  1. 启动gdb来进行调试(PC环境)
user@user-600-G5:~/project/$ adb forward tcp:1234 tcp:1234
1234

设置adb tcp端口转发,前一个tcp:1234是指PC端的端口,后一个是Target,也就是手机端的.

运行gdb

user@user-600-G5:~/txl/project/LD60_419$ ./prebuilts/gdb/linux-x86/bin/gdb
GNU gdb (GDB) 8.3
Copyright (C) 2019 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 "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb)
(gdb) set solib-absolute-prefix out/target/product/ld60_evb/symbols/
(gdb) set solib-search-path out/target/product/ld60_evb/symbols/
(gdb) target remote :1234

独特见解