VSCode遠程圖形化GDB
- 摘要
- 一、安裝VSCode
- 1、使用.exe安裝包安裝VSCode
- 2、VSCode 插件安裝
- 3、VSCode建立遠程連接
- 二、core dump找bug
- 1、開啟core文件
- 2、永久生效的方法
- 3、編寫測試程序
- 4、運行結果
- 5、查看core段錯誤位置
- 6、在程序中開啟core dump并二者core文件大小
- 三、gdbserver
- 1、[GDB移植](https://blog.csdn.net/weixin_38426553/article/details/126633280)
- 2、VSCode遠程連接
- 3、代碼編譯
- 4、代碼調試
- 4.1、修改launch.json文件
- 4.2、gdbserver啟動
- 四、客戶端命令行gdbserver調試
- 1、在arm開發板啟動gdbserver
- 2、在客戶端輸入命令
- 3、開啟遠程連接
- 4、打斷點
- 5、c繼續運行,在斷點處停止
- 6、打印參數
- 7、其他功能
- 總結
摘要
本文詳細介紹了如何在 Visual Studio Code (VSCode) 中實現遠程圖形化調試,使用 GDB 和 gdbserver 工具進行嵌入式開發中的程序調試。文章分為四個部分:首先介紹了 VSCode 的安裝和插件配置;其次講解了如何通過 core dump 文件定位程序中的錯誤;接著介紹了如何在嵌入式設備上使用 gdbserver 進行遠程調試;最后通過客戶端命令行展示了 gdbserver 的調試功能。通過本文,讀者可以掌握在 VSCode 中進行遠程調試的完整流程,包括環境搭建、調試配置和常見問題解決。
一、安裝VSCode
1、使用.exe安裝包安裝VSCode
2、VSCode 插件安裝
添加Remote SSH、Remote Development等插件
1)、C/C++,這個肯定是必須的。
2)、C/C++ Snippets,即 C/C++重用代碼塊。
3)、C/C++ Advanced Lint,即 C/C++靜態檢測 。
4)、Code Runner,即代碼運行。
5)、Include AutoComplete,即自動頭文件包含。
6)、Rainbow Brackets,彩虹花括號,有助于閱讀代碼。
7)、One Dark Pro,VSCode 的主題。
8)、GBKtoUTF8,將 GBK 轉換為 UTF8。
9)、ARM,即支持 ARM 匯編語法高亮顯示。Arm Assembly
10)、Chinese(Simplified),即中文環境。
11)、vscode-icons,VSCode 圖標插件,主要是資源管理器下各個文件夾的圖標。
12)、compareit,比較插件,可以用于比較兩個文件的差異。
13)、DeviceTree,設備樹語法插件。
14)、TabNine,一款 AI 自動補全插件,強烈推薦,誰用誰知道!
15)、Remote SSH
16)、Remote Development
(ARM替代,Arm Assembly更全)
設置語言環境:中文環境,安裝Chinese(Simplified)插件后會提示更換并重啟VSCode,或者去setting去設置locale.jsons設置"zh_cn"并重啟VSCode。
{// 定義 VS Code 的顯示語言。// 請參閱 https://go.microsoft.com/fwlink/?LinkId=761051,了解支持的語言列表。"locale":"zh-cn" // 更改將在重新啟動 VS Code 之后生效。
}
3、VSCode建立遠程連接
使用本地vscode的remote ssh 遠程鏈接服務器
二、core dump找bug
1、開啟core文件
root@ATK-IMX6U:/opt# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 2931
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 2931
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
第一行core文件大小為0,沒有開啟。
使用#ulimit -c [kbytes]可以設置系統允許生成的core文件大小;
ulimit -c 0 不產生core文件
ulimit -c 100 設置core文件最大為100k
ulimit -c unlimited 不限制core文件大小
root@ATK-IMX6U:/opt# ulimit -c unlimited
root@ATK-IMX6U:/opt# ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 2931
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 2931
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
2、永久生效的方法
這樣進程奔潰就可以生成core文件了,這種方法只能在shell中生效,下面說一下永久生效的方法
vi /etc/profile
進入編輯模式在文件最后加入:ulimit -c unlimited
root@ATK-IMX6U:/opt# vi /etc/profile
root@ATK-IMX6U:/opt# sync
3、編寫測試程序
#include <stdio.h>#include <unistd.h>int main(int argc, char *argv[]){unsigned int timerCnt = 0;while(1) {printf("system runing times:%d\r\n", timerCnt);timerCnt++;int *p = NULL;int num = *p;sleep(1);}
}
4、運行結果
root@ATK-IMX6U:/opt# ./gdbApp
system runing times:0
Segmentation fault (core dumped)
root@ATK-IMX6U:/opt# ls
core download.sh gdbApp GW_EVSE IBG_AWS_Conn logs QDesktop sqlite src volume.tmp
root@ATK-IMX6U:/opt# ls -l core
-rw------- 1 root root 344K Jul 21 23:14 core
root@ATK-IMX6U:/opt#
產生的core文件即為異常中止調試文件。
5、查看core段錯誤位置
將core文件拷貝到Ubuntu平臺上,和可自行文件放在同一個文件夾下,然后運行下面的命令:
/opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gdb gdbApp core
運行結果:
leo@leo-virtual-machine:/mnt/hgfs/VMShare/TESTVSCODE$ /opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gdb gdbApp core
GNU gdb (GDB) 7.10.1
Copyright (C) 2015 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-pokysdk-linux --target=arm-poky-linux-gnueabi".
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:
---Type <return> to continue, or q <return> to quit---y
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from gdbApp...done.
[New LWP 1577]warning: Could not load shared library symbols for 3 libraries, e.g. linux-vdso.so.1.
Use the "info sharedlibrary" command to see the complete listing.
Do you need "set solib-search-path" or "set sysroot"?
Core was generated by `./gdbApp'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x00010494 in main (argc=1, argv=0x7edf5cd4) at gdbApp.c:14
14 int num = *p;
(gdb)
運行結果來看,錯誤產生在14行,將空指針*p賦值給了num。
6、在程序中開啟core dump并二者core文件大小
#define CORE_DUMP_SIZE 1024*1024*500 //500M
#define CORE_DUMP_DEBUG 1#if CORE_DUMP_DEBUG/*設置core文件存儲上限,系統默認為0.用于段錯誤時堆棧信息記錄的操作,可以產生一個core文件*/struct rlimit rlmt;if (getrlimit(RLIMIT_CORE, &rlmt) == -1) {return -1; } rlmt.rlim_cur = (rlim_t)CORE_DUMP_SIZE;rlmt.rlim_max = (rlim_t)CORE_DUMP_SIZE;if (setrlimit(RLIMIT_CORE, &rlmt) == -1){return -1; }
#endif
三、gdbserver
1、GDB移植
將Ubuntu上的交叉編譯器里面的gdbserver拷貝到開發板文件系統指定目錄/bin/
leo@leo-virtual-machine:~$ whereis gdbserver
gdbserver: /usr/bin/gdbserver /usr/local/arm_gcc/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/gdbserver /usr/share/man/man1/gdbserver.1.gz
leo@leo-virtual-machine:~$
2、VSCode遠程連接
具體步驟可以百度:
設置.ssh config
設置config文件
Host 192.168.3.116HostName 192.168.3.116Host 192.168.3.143HostName 192.168.3.143Port 22User rootHost 192.168.3.116HostName 192.168.3.116Port 22User leo
server設置:
輸入密碼連接
3、代碼編譯
編譯代碼:修改Makefile,在gcc 后面加上 -ggdb選項
# -------------[ CFLAGS ] -------------
CFLAGS += -g
CFLAGS += -ggdb
CFLAGS += -Wall
CFLAGS += -I.
CFLAGS += -I $(TOPDIR)/api
CFLAGS += -I $(TOPDIR)/daemon
......
然后和以前一樣編譯代碼,這樣代碼編譯出來的可執行文件會更大一些,保留了調試信息在可執行文件中。
4、代碼調試
4.1、修改launch.json文件
點擊運行->啟動調試(或者按F5)。新的代碼未創建launch.json文件,選擇C++(GDB/LLDB),選擇默認配置會自動生成一個launch.json文件。
或者
或者點擊調試,選擇創建愛你launch.json文件。,選擇C++(GDB/LLDB)生成一個launch.json文件。
修改launch.json文件
{// 使用 IntelliSense 了解相關屬性。 // 懸停以查看現有屬性的描述。// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"type": "cppdbg","request": "launch","name": "(gdb)調試","program": "${workspaceFolder}/GW_EVSE","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath":"/opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gdb",//"/usr/local/arm_gcc/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb","miDebuggerServerAddress": "192.168.3.143:2001","setupCommands": [{"description": "為 gdb 啟用整齊打印","text": "-enable-pretty-printing","ignoreFailures": true}]}]
}
注意交叉編譯器和可執行文件名稱需要修改。上面交叉編譯器有兩個,需要和編譯代碼的交叉編譯器對應上,否則程序不可執行,會段錯誤。
4.2、gdbserver啟動
將程序可執行文件下載到arm linux開發板后,執行
root@ATK-IMX6U:/opt# gdbserver 192.168.3.143:2001 GW_EVSE
Process gdbApp created; pid = 1629
Listening on port 2001
或者本機地址可以省略
root@ATK-IMX6U:/opt# gdbserver 3:2001 GW_EVSE
Process gdbApp created; pid = 1629
Listening on port 2001
然后在PC端VSCode啟動調試,就可以打斷點進行調試了
四、客戶端命令行gdbserver調試
1、在arm開發板啟動gdbserver
root@ATK-IMX6U:/opt# gdbserver 192.168.3.143:2001 GW_EVSE
Process gdbApp created; pid = 1629
Listening on port 2001
2、在客戶端輸入命令
leo@leo-virtual-machine:/mnt/hgfs/VMShare/VSCodeWorkSpace/GW_EVSE$ /opt/fsl-imx-x11/4.1.15-2.1.0/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gdb GW_EVSE
GNU gdb (GDB) 7.10.1
Copyright (C) 2015 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-pokysdk-linux --target=arm-poky-linux-gnueabi".
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"...
Reading symbols from GW_EVSE...done.
(gdb)
3、開啟遠程連接
(gdb) target remote 192.168.3.143:2001
Remote debugging using 192.168.3.143:2001
Reading /lib/ld-linux-armhf.so.3 from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /lib/ld-linux-armhf.so.3 from remote target...
Reading symbols from target:/lib/ld-linux-armhf.so.3...Reading /lib/ld-2.23.so from remote target...
Reading /lib/.debug/ld-2.23.so from remote target...
(no debugging symbols found)...done.
0x76fcfac0 in ?? () from target:/lib/ld-linux-armhf.so.3
(gdb)
4、打斷點
(gdb) b 140
Breakpoint 1 at 0x1efac: file main/main_pro.c, line 140.
(gdb)
5、c繼續運行,在斷點處停止
(gdb) c
Continuing.
Reading /lib/librt.so.1 from remote target...
Reading /lib/libpthread.so.0 from remote target...
Reading /lib/libm.so.6 from remote target...
Reading /lib/libdl.so.2 from remote target...
Reading /lib/libc.so.6 from remote target...
Reading /lib/librt-2.23.so from remote target...
Reading /lib/.debug/librt-2.23.so from remote target...
Reading /lib/libpthread-2.23.so from remote target...
Reading /lib/.debug/libpthread-2.23.so from remote target...
Reading /lib/libm-2.23.so from remote target...
Reading /lib/.debug/libm-2.23.so from remote target...
Reading /lib/libdl-2.23.so from remote target...
Reading /lib/.debug/libdl-2.23.so from remote target...
Reading /lib/libc-2.23.so from remote target...
Reading /lib/.debug/libc-2.23.so from remote target...
[New Thread 1725]
[New Thread 1726]
[New Thread 1727]
[New Thread 1728]
[New Thread 1729]
[New Thread 1731]
[New Thread 1732]
[New Thread 1733]
[New Thread 1734]
[New Thread 1736]
[New Thread 1738]
[New Thread 1739]
[New Thread 1740]Breakpoint 1, main () at main/main_pro.c:140
warning: Source file is more recent than executable.
140 if(cnt > 30)
(gdb)
6、打印參數
(gdb) r
The "remote" target does not support "run". Try "help target" or "continue".
(gdb) c
Continuing.Breakpoint 1, main () at main/main_pro.c:140
140 if(cnt > 30)
(gdb) c
Continuing.Breakpoint 1, main () at main/main_pro.c:140
140 if(cnt > 30)
(gdb) print cnt
$1 = 3
(gdb)
從上面看出,遠程模式下,不支持全速運行 r 命令。
7、其他功能
這里不再贅述,可以查看操作手冊。
總結
本文為嵌入式開發人員提供了一套完整的 VSCode 遠程調試解決方案。通過安裝必要的插件和配置遠程連接,開發者可以在 VSCode 中方便地進行代碼編寫和調試。core dump 功能可以幫助快速定位程序崩潰的原因,而 gdbserver 則實現了遠程設備與本地調試環境的無縫連接。通過修改 launch.json 文件和正確配置交叉編譯器路徑,開發者可以在 VSCode 中輕松啟動和管理遠程調試會話。此外,文章還介紹了如何通過命令行使用 gdbserver 進行調試,進一步擴展了調試功能的靈活性。總之,本文為希望在 VSCode 中實現高效遠程調試的開發者提供了實用的指導和參考。