跳到主要内容

调试利器GDB

阅读量: 101
阅读人次: 102

使用GDB Server和VS Code

在嵌入式板子上,直接使用gdb,需要记住各种gdb命令,时间久了就忘的差不多了。在条件许可的情况下,GDB Server和VS Code配合使用,会非常方便调试。

这里加上了useExtendedRemotesetupCommands两个字段,用到了GDB Server的extended-remote模式,使得我们可以不用每次在板子上通过GDB Server运行程序或者附加到已经运行的程序,然后再使用VS Code + GDB调试。而是直接通过VSCode集成方便的启动调试程序。

.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "AppDebug",
"type": "cppdbg",
"request": "launch",
"miDebuggerPath": "arm-linux-gnueabihf-gdb",
"miDebuggerServerAddress": "192.168.8.127:8080",
"program": "${workspaceFolder}/build/GateFace",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"logging": {
"engineLogging": false
},
"MIMode": "gdb",
"useExtendedRemote": true,
"setupCommands": [
{
"text": "set remote exec-file /sdcard/GateFace",
"description": "设置嵌入式单板加载的程序",
"ignoreFailures": false
}
]
}
]
}

然后在板子上执行:

./gdbserver --multi 192.168.8.127:8080

就可以很方便的使用VS Code的 运行和调试 功能了。

如果希望手动在单板上运行调试程序或附加到已经运行的调试程序,useExtendedRemotesetupCommands两个字段去除即可。

target extended-remote 192.168.8.127:8080
set remote exec-file /sdcard/GateFace

源码编译

下载GDB源码包,这里以 gdb-14.1.tar.xz 为例。下载后进行解压然后进入源码目录:

tar xvf ./gdb-14.1.tar.xz
cd gdb-14.1/

首先构建gdb(构建出来的 aarch64-linux-gnu-gdb 是运行在 PC 环境下的,所以使用的编译器不是交叉编译工具,而是本地 gcc ):

sudo apt install libgmp-dev libmpfr-dev texinfo
./configure --enable-static=yes --target=aarch64-linux-gnu --prefix=/opt/aarch64-v01c01-linux-gnu-gcc/lib/gdb-14.1
make -j6 && make install

然后构建运行于目标调试板的 gdbserver(可以删除 gdb-14.1 目录,重新解压源码以保持环境干净):

export CC=aarch64-linux-gnu-gcc
./configure --enable-static=yes --host aarch64-linux-gnu --target=aarch64-linux-gnu --disable-gdb --prefix=/opt/aarch64-v01c01-linux-gnu-gcc/lib/gdb-14.1
make all-gdbserver -j6 && make install-gdbserver

在使用时 gdb 会报错:

Remote 'g' packet reply is too long (expected 788 bytes, got 796 bytes): ......

如果出现此报错,则降低gdb版本尝试。推荐使用和交叉编译工具链gcc版本相同的gdb源码。