背景
在跨平台开发场景中,常常会交叉编译的场景,比如从x86交叉编译到arm。
那么在目标平台调试,就成为了比较困难的一件事。需要手动配置源码路径,替换源码set substitute-path等操作,十分繁琐,并且无法像native调试一样,直接使用vscode来使用快捷键进行调试。
gdbserver
当你使用gdbserver 和 默认的gdb进行调试时,你可能遇到以下问题:
目标机执行
gdbserver <target_ip>:<port> <your_app>
Process /mnt/share/hgl/sdk_out/bin/Demo created; pid = 1519126
Listening on port 10086
调试机执行
$ gdb
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.2) 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 172.20.117.167:10086
Remote debugging using 172.20.117.167:10086
warning: while parsing target description (at line 4): Target description specified unknown architecture "aarch64"
warning: Could not load XML target description; ignoring
Reading /mnt/share/hgl/sdk_out/bin/Demo from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /mnt/share/hgl/sdk_out/bin/Demo from remote target...
Reading symbols from target:/mnt/share/hgl/sdk_out/bin/Demo...
Reading /mnt/share/hgl/sdk_out/bin/Demo.debug from remote target...
Reading /mnt/share/hgl/sdk_out/bin/Demo.debug from remote target...
Reading symbols from target:/mnt/share/hgl/sdk_out/bin/Demo.debug...
Remote register badly formatted: T051d:0000000000000000;1f:10f1ffff7f000000;20:c0d0fcf77f000000;thread:p172e16.172e16;core:1;
here: 00000000;1f:10f1ffff7f000000;20:c0d0fcf77f000000;thread:p172e16.172e16;core:1;
(gdb)
gdb会提示错误的format,这是因为gdb要求目标目标gdbserver架构要与调试gdb架构相同
gdb-multiarch
gdb-multiarch 是 GNU Debugger(GDB)的一个扩展版本,专门用于支持多架构调试。它允许开发者在不同处理器架构之间进行跨平台调试,例如 ARM、x86、MIPS 等,而无需手动更改调试器设置或重新加载插件。
安装
sudo apt install gdb-multiarch
接下来,你只需要执行
gdb-multiarch
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.2) 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 172.20.117.167:10086
Remote debugging using 172.20.117.167:10086
Reading /mnt/share/hgl/sdk_out/bin/Demo from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /mnt/share/hgl/sdk_out/bin/Demo from remote target...
Reading symbols from target:/mnt/share/hgl/sdk_out/bin/Demo...
Reading /mnt/share/hgl/sdk_out/bin/Demo.debug from remote target...
Reading /mnt/share/hgl/sdk_out/bin/Demo.debug from remote target...
Reading symbols from target:/mnt/share/hgl/sdk_out/bin/Demo.debug...
Reading /lib/ld-linux-aarch64.so.1 from remote target...
Reading /lib/ld-linux-aarch64.so.1 from remote target...
Reading symbols from target:/lib/ld-linux-aarch64.so.1...
Reading /lib/ld-2.31.so from remote target...
Reading /lib/.debug/ld-2.31.so from remote target...
Reading /usr/lib/debug//lib/ld-2.31.so from remote target...
Reading /usr/lib/debug/lib//ld-2.31.so from remote target...
Reading target:/usr/lib/debug/lib//ld-2.31.so from remote target...
(No debugging symbols found in target:/lib/ld-linux-aarch64.so.1)
0x0000007ff7fcd0c0 in ?? () from target:/lib/ld-linux-aarch64.so.1
(gdb)
现在你就可以愉快的进行远程跨架构调试了
vscode 配置
直接看launch.json
{
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "you app with debug info",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "gdb-multiarch",
"miDebuggerServerAddress": "172.20.117.167:10086",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"logging": {
"engineLogging": true,
"trace": true,
"traceResponse": true
}
}
],
"version": "2.0.0"
}
关键配置:
- program:本地程序,需要带符号
- miDebuggerPath: 配置为
gdb-multiarch - miDebuggerServerAddress: 配置为目标调试地址
接下来按下F5
开始愉快的调试吧。