一、gdbserver

1.1 安装gdbserver

使用Linux远程调试目标机器上面的程序时,目标机器需要安装gdbserver,启动测试程序后与本地gdb进行通讯
Ubuntu下安装命令如下:

sduo apt-get install gdbserver

CentOS安装方式:

sudo yum install gdb-server

1.2 配置防火墙

为了能够顺利连接,可以暂时关闭防火墙。

二、gdbserver的启动与连接

2.1 gdbserver启动

在目标机器上启动gdbserver:
localhost就是本地ip
4242是未被占用的端口,可以自由指定
app.out就是可执行程序名

gdbserver localhost:4242 app.out

启动成功之后就会监听对应端口,等待连接

$> gdbserver localhost:4242 app.out 
Process /home/lhq/Desktop/EOS/14-c-asm/app.out created; pid = 31633
Listening on port 4242

如果程序正在运行可以通过查找对应进程的进程号后输入如下指令进行连接:

sudo gdbserver localhost:4242 --attach PID
$> sudo gdbserver localhost:4242 --attach 33201
[sudo] lhq 的密码: 
Attached; pid = 33201
Listening on port 4242

2.2 gdb连接

服务器监听之后,客户端便可以发起连接请求了,操作如下:

$> gdb 
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 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) target remote localhost:4242
Remote debugging using localhost:4242
Reading /home/lhq/Desktop/EOS/14-c-asm/app.out from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /home/lhq/Desktop/EOS/14-c-asm/app.out from remote target...
Reading symbols from target:/home/lhq/Desktop/EOS/14-c-asm/app.out...
0x08049000 in _start ()

gdb 启动之后输入指令:target remote 目标主机IP:端口
此处是本地演示,连接到了本地端口,连接之后就可以进行进行调试了,和本地效果完全一致。
服务端:

$> gdbserver localhost:4242 app.out         
Process /home/lhq/Desktop/EOS/14-c-asm/app.out created; pid = 32189
Listening on port 4242
Remote debugging from host 127.0.0.1, port 40230

客户端:

(gdb) b main.c:c_func 
Breakpoint 1 at 0x8049049: file main.c, line 8.
(gdb) c
Continuing.

Breakpoint 1, c_func () at main.c:8
8       {
(gdb) q
A debugging session is active.

三、gdb远程调试stm32

gdb调试stm32等单片机的过程如下:

  • server程序通过JTAG连接到电脑的设备暂停并连接,开启一个端口等待客户端连接
  • arm-none-eabi-gdb连接到服务端
  • 从本地文件中获取调试信息
  • 将可执行程序通过server端的烧录算法烧录到目标设备中
  • 然后断点调试

3.1 stlink-tools安装

服务端程序st-util需要被安装:

sudo apt-get install stlink-tools

如果没有对应的安装包可以直接用源码编译安装
克隆地址如下:

git clone https://github.com/stlink-org/stlink.git

然后进入源码目录:

 $> cd stlink
 $> make
 $> cd build/Release && make install

3.2 st-util启动与连接

st-util运行GDB服务器与STM32设备交互,使用方式如下:

运行gdb服务于4500端口,端口如果不指定默认为4242,然后等待客户端连接

$> st-util -p 4500

在另一个终端输入如下指令启动gdb

$> arm-none-eabi-gdb

进入之后进行连接

(gdb) target extended-remote localhost:4500

加载可执行程序

(gdb) load firmware.elf

此时gdb还没有对应的调试符号信息,需要进行加载

(gdb) file firmware.elf

然后开始进行调试

3.3 远程调试测试

服务端启动

$> st-util 
st-util 1.6.0
2021-11-24T22:25:22 INFO common.c: Loading device parameters....
2021-11-24T22:25:22 INFO common.c: Device connected is: F76xxx device, id 0x10006451
2021-11-24T22:25:22 INFO common.c: SRAM size: 0x80000 bytes (512 KiB), Flash: 0x100000 bytes (1024 KiB) in pages of 2048 bytes
2021-11-24T22:25:22 INFO gdb-server.c: Chip ID is 00000451, Core ID is  5ba02477.
2021-11-24T22:25:22 INFO gdb-server.c: Chip clidr: 09000003, I-Cache: off, D-Cache: off
2021-11-24T22:25:22 INFO gdb-server.c:  cache: LoUU: 1, LoC: 1, LoUIS: 0
2021-11-24T22:25:22 INFO gdb-server.c:  cache: ctr: 8303c003, DminLine: 32 bytes, IminLine: 32 bytes
2021-11-24T22:25:22 INFO gdb-server.c: D-Cache L0: 2021-11-24T22:25:22 INFO gdb-server.c: f00fe019 LineSize: 8, ways: 4, sets: 128 (width: 12)
2021-11-24T22:25:22 INFO gdb-server.c: I-Cache L0: 2021-11-24T22:25:22 INFO gdb-server.c: f01fe009 LineSize: 8, ways: 2, sets: 256 (width: 13)
2021-11-24T22:25:22 INFO gdb-server.c: Listening at *:4242...

使用另一台电脑进行远程连接:

$> arm-none-eabi-gdb 
GNU gdb (GNU Tools for Arm Embedded Processors 9-2019-q4-major) 8.3.0.20190709-git
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 "--host=x86_64-apple-darwin10 --target=arm-none-eabi".
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) target remote lihongquan.local:4242
Remote debugging using lihongquan.local:4242
warning: No executable has been specified and target does not support
determining executable automatically.  Try using the "file" command.
0x08046528 in ?? ()

烧录程序到芯片中:

(gdb) load build/TFTLCD.elf 
Loading section .isr_vector, size 0x1f8 lma 0x8000000
Loading section .text, size 0x473f8 lma 0x8000200
Loading section .rodata, size 0x4e638 lma 0x80475f8
Loading section .ARM, size 0x8 lma 0x8095c30
Loading section .init_array, size 0x4 lma 0x8095c38
Loading section .fini_array, size 0x4 lma 0x8095c3c
Loading section .data, size 0x2d0 lma 0x8095c40
Start address 0x8046528, load size 614152
Transfer rate: 24 KB/sec, 13067 bytes/write.

加载文件到gdb中进行调试:

(gdb) file build/TFTLCD.elf 
A program is being debugged already.
Are you sure you want to change the file? (y or n) y
Reading symbols from build/TFTLCD.elf...

打断点:

 (gdb) b main.c:103
Breakpoint 1 at 0x8000796: file Core/Src/main.c, line 103.
(gdb) c
Continuing.
Note: automatically using hardware breakpoints for read-only addresses.
Breakpoint 1, main () at Core/Src/main.c:103
103       MX_GPIO_Init();
(gdb) q
A debugging session is active.

        Inferior 1 [Remote target] will be killed.

3.4 总结

  1. 相比Keil图形界面进行调试,由于传输数据量小,稳定性更高。
  2. 如果在树莓派或者其他开发板中安装相对应的程序,再加上一块电池,即使设备已经用于实际环境,也可以进行方便的调试。
  3. 使用方法和gdb一致,节约学习成本。
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐