gdb调试native程序--Android环境搭建记录

adtxl
2021-12-22 / 0 评论 / 685 阅读 / 正在检测是否收录...

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到目标进程(设备端)
gdbserver  :1234 --attach <PID> 或者 gdbserver  :1234 ./<program name>

实例:

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/xxx/symbols/
(gdb) set solib-search-path out/target/product/xxx/symbols/
(gdb) target remote :1234

或者:

① 源码目录执行:./prebuilts/gdb/linux-x86/bin/gdb
dir /Android_src #设置Android源码目录的路径,替换为自己的路径即可
set solib-absolute-prefix /Android_src/out/target/product/xxx/symbols #设置带debug符号的symbols文件路径 ,替换为自己的路径即可
target remote <device ip>:1234 #连接开发板
b <file name >:<line number> #设置断点
continue #运行程序
listctrl+x+a查看源码

0

评论 (0)

取消