vscode+gdbserver遠端除錯ARM環境搭建

云的边界發表於2024-04-18

一、編譯gdbserver

1. 下載gdb

http://ftp.gnu.org/gnu/gdb/

2.解壓縮

tar xf gdb-8.0.tar.xz

3. 交叉編譯

cd gdb/gdbserver/
./configure --host=arm-none-linux-gnueabihf --target=arm-none-linux-gnueabihf --program-prefix=arm-none-linux-gnueabihf- --prefix=/home/xman/gdb-8.0/binsvr
make
make install

將/home/xman/gdb-8.0/binsvr/gdbserver 複製到ARM開發板/bin

【錯誤及解決】
(1)linux-ptrace 編譯錯誤:

In file included from ../../gdb/nat/linux-ptrace.c:20:
../../gdb/nat/linux-ptrace.h:180:22: 錯誤:expected identifier before numeric constant 180 | # define TRAP_HWBKPT 4
解決: gdb/nat/linux-ptrace.c 刪除 #include "gdb_wait.h" #include "gdb_ptrace.h"
gdb/nat/linux-ptrace.h 中在 #include "nat/gdb_ptrace.h" 下增加 #include "gdb_wait.h"
參見 https://github.com/foss-for-synopsys-dwc-arc-processors/binutils-gdb/commit/e6600bbefd667810744fbe8eed3830bf5644c354

(2)makeinfo: not found

解決:sudo apt-get install texinfo

二、vscode配置

1. 安裝外掛:

vscode ssh連線到交叉編譯環境主機(如VMware中ubuntu),安裝c/c++外掛

2. 配置launch.json

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name":"(gdb)launch",
"type":"cppdbg",
"request": "launch",
"program": "/home/xman/demo/test_main",
"args": [],
"stopAtEntry": false,
"miDebuggerPath": "/usr/.../bin/arm-none-linux-gnueabihf-gdb",
"miDebuggerServerAddress": "192.168.0.45:8889",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"cwd": ".",
"setupCommands": [
{
"description": "sysroot",
"text": "set sysroot target:/"
},
{
"description": "prettyprint",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "follow-fork-mode_child",
"text": "set follow-fork-mode child"
}
]
}
]
}

三、開始除錯

1. ARM開發板上啟動gdbserver,除錯守護程序pid(如32101, 可執行檔案為 test_main)

# gdbserver :8889 --attach 32101

2. gdb客戶端(PC 命令列)

$ arm-none-linux-gnueabihf-gdb test_main
……(省略命令輸出)

(gdb) target remote 192.168.0.45:8889
(gdb) set sysroot target:/
(gdb) set follow-fork-mode child
(gdb) b x_function
(gdb) c

【問題與解決】

(1) PC上執行arm-none-linux-gnueabihf-gdb時報錯:

Could not find platform independent libraries <prefix>

Could not find platform dependent libraries <exec_prefix>

Consider setting $PYTHONHOME to [:<exec_prefix>])

ImportError: No module named site

原因:可能沒有安裝 arm-none-linux-gnueabihf-gdb 依賴的 python版本

解決:readelf -d arm-none-linux-gnueabihf-gdb 檢視gdb依賴的python庫版本,如依賴2.7

安裝python2.7: sudo apt-get install python2.7

sudo apt-get install libpython2.7

(2)安裝python2.7後 undefined symbol: PyUnicodeUCS4_FromEncodedObject

原因: 在PATH某個交叉工具鏈路徑下也包含了libpython2.7.so.1.0但其內沒有定義函式PyUnicodeUCS4_FromoEncodedObject

解決:刪除該路徑下的libpython2.7.so.1.0,使用安裝的libpython2.7

3 gdb客戶端(vscode)

可以在程式碼中新增斷點,發起gdb除錯了。

相關文章