VSCode远程图形化GDB

打印 上一主题 下一主题

主题 1535|帖子 1535|积分 4605

摘要

本文详细介绍了如何在 Visual Studio Code (VSCode) 中实现远程图形化调试,使用 GDB 和 gdbserver 工具进行嵌入式开发中的程序调试。文章分为四个部门:起首介绍了 VSCode 的安装和插件配置;其次解说了如何通过 core dump 文件定位程序中的错误;接着介绍了如何在嵌入式设备上使用 gdbserver 进行远程调试;末了通过客户端命令行展示了 gdbserver 的调试功能。通过本文,读者可以掌握在 VSCode 中进行远程调试的完备流程,包罗环境搭建、调试配置和常见题目办理。
一、安装VSCode

1、使用.exe安装包安装VSCode

2、VSCode 插件安装

添加Remote SSH、Remote Development等插件
  1. 1)、C/C++,这个肯定是必须的。
  2. 2)、C/C++ Snippets,即 C/C++重用代码块。
  3. 3)、C/C++ Advanced Lint,即 C/C++静态检测 。
  4. 4)、Code Runner,即代码运行。
  5. 5)、Include AutoComplete,即自动头文件包含。
  6. 6)、Rainbow Brackets,彩虹花括号,有助于阅读代码。
  7. 7)、One Dark Pro,VSCode 的主题。
  8. 8)、GBKtoUTF8,将 GBK 转换为 UTF8。
  9. 9)、ARM,即支持 ARM 汇编语法高亮显示。Arm Assembly
  10. 10)、Chinese(Simplified),即中文环境。
  11. 11)、vscode-icons,VSCode 图标插件,主要是资源管理器下各个文件夹的图标。
  12. 12)、compareit,比较插件,可以用于比较两个文件的差异。
  13. 13)、DeviceTree,设备树语法插件。
  14. 14)、TabNine,一款 AI 自动补全插件,强烈推荐,谁用谁知道!
  15. 15)、Remote SSH
  16. 16)、Remote Development
复制代码
(ARM替代,Arm Assembly更全)
设置语言环境:中文环境,安装Chinese(Simplified)插件后会提示更换并重启VSCode,或者去setting去设置locale.jsons设置"zh_cn"并重启VSCode。
  1. {
  2.         // 定义 VS Code 的显示语言。
  3.         // 请参阅 https://go.microsoft.com/fwlink/?LinkId=761051,了解支持的语言列表。
  4.        
  5.         "locale":"zh-cn" // 更改将在重新启动 VS Code 之后生效。
  6. }
复制代码
3、VSCode创建远程毗连

使用当地vscode的remote ssh 远程链接服务器
二、core dump找bug

1、开启core文件

  1. root@ATK-IMX6U:/opt# ulimit -a
  2. core file size          (blocks, -c) 0
  3. data seg size           (kbytes, -d) unlimited
  4. scheduling priority             (-e) 0
  5. file size               (blocks, -f) unlimited
  6. pending signals                 (-i) 2931
  7. max locked memory       (kbytes, -l) 64
  8. max memory size         (kbytes, -m) unlimited
  9. open files                      (-n) 1024
  10. pipe size            (512 bytes, -p) 8
  11. POSIX message queues     (bytes, -q) 819200
  12. real-time priority              (-r) 0
  13. stack size              (kbytes, -s) 8192
  14. cpu time               (seconds, -t) unlimited
  15. max user processes              (-u) 2931
  16. virtual memory          (kbytes, -v) unlimited
  17. file locks                      (-x) unlimited
复制代码
第一行core文件巨细为0,没有开启。
使用#ulimit -c [kbytes]可以设置系统允许生成的core文件巨细;
  1. ulimit -c 0 不产生core文件
  2. ulimit -c 100 设置core文件最大为100k
  3. ulimit -c unlimited 不限制core文件大小
  4. root@ATK-IMX6U:/opt# ulimit -c unlimited
  5. root@ATK-IMX6U:/opt# ulimit -a
  6. core file size          (blocks, -c) unlimited
  7. data seg size           (kbytes, -d) unlimited
  8. scheduling priority             (-e) 0
  9. file size               (blocks, -f) unlimited
  10. pending signals                 (-i) 2931
  11. max locked memory       (kbytes, -l) 64
  12. max memory size         (kbytes, -m) unlimited
  13. open files                      (-n) 1024
  14. pipe size            (512 bytes, -p) 8
  15. POSIX message queues     (bytes, -q) 819200
  16. real-time priority              (-r) 0
  17. stack size              (kbytes, -s) 8192
  18. cpu time               (seconds, -t) unlimited
  19. max user processes              (-u) 2931
  20. virtual memory          (kbytes, -v) unlimited
  21. file locks                      (-x) unlimited
复制代码
2、永世生效的方法

如许进程奔溃就可以生成core文件了,这种方法只能在shell中生效,下面说一下永世生效的方法
vi /etc/profile 进入编辑模式在文件末了加入:ulimit -c unlimited
  1. root@ATK-IMX6U:/opt# vi /etc/profile
  2. root@ATK-IMX6U:/opt# sync
复制代码
3、编写测试程序

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. int main(int argc, char *argv[])
  4. {
  5.     unsigned int timerCnt = 0;
  6.     while(1)
  7.     {
  8.         printf("system runing times:%d\r\n", timerCnt);
  9.         timerCnt++;
  10.         int *p = NULL;
  11.         int num = *p;
  12.         sleep(1);
  13.     }
  14. }
复制代码
4、运行结果

  1. root@ATK-IMX6U:/opt# ./gdbApp
  2. system runing times:0
  3. Segmentation fault (core dumped)
  4. root@ATK-IMX6U:/opt# ls
  5. core  download.sh  gdbApp  GW_EVSE  IBG_AWS_Conn  logs  QDesktop  sqlite  src  volume.tmp
  6. root@ATK-IMX6U:/opt# ls -l core
  7. -rw------- 1 root root 344K Jul 21 23:14 core
  8. root@ATK-IMX6U:/opt#
复制代码
产生的core文件即为异常中止调试文件。
5、查看core段错误位置

将core文件拷贝到Ubuntu平台上,和可自行文件放在同一个文件夹下,然后运行下面的命令:
  1. /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
复制代码
运行结果:
  1. 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
  2. GNU gdb (GDB) 7.10.1Copyright (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:1414              int num = *p;(gdb)
复制代码
运行结果来看,错误产生在14行,将空指针*p赋值给了num。
6、在程序中开启core dump并二者core文件巨细

  1. #define CORE_DUMP_SIZE        1024*1024*500 //500M
  2. #define CORE_DUMP_DEBUG 1
  3. #if CORE_DUMP_DEBUG
  4.         /*设置core文件存储上限,系统默认为0.用于段错误时堆栈信息记录的操作,可以产生一个core文件*/
  5.         struct rlimit rlmt;
  6.         if (getrlimit(RLIMIT_CORE, &rlmt) == -1)
  7.     {
  8.                 return -1;
  9.         }   
  10.         rlmt.rlim_cur        = (rlim_t)CORE_DUMP_SIZE;
  11.         rlmt.rlim_max        = (rlim_t)CORE_DUMP_SIZE;
  12.         if (setrlimit(RLIMIT_CORE, &rlmt) == -1)
  13.     {
  14.         return -1;
  15.     }
  16. #endif       
复制代码
三、gdbserver

1、GDB移植

将Ubuntu上的交错编译器里面的gdbserver拷贝到开发板文件系统指定目录/bin/
  1. leo@leo-virtual-machine:~$ whereis gdbserver
  2. 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
  3. leo@leo-virtual-machine:~$
复制代码

2、VSCode远程毗连



详细步骤可以百度:
设置.ssh config

设置config文件
  1. Host 192.168.3.116
  2.   HostName 192.168.3.116
  3. Host 192.168.3.143
  4.   HostName 192.168.3.143
  5.   Port 22
  6.   User root
  7.   
  8. Host 192.168.3.116
  9.   HostName 192.168.3.116
  10.   Port 22
  11.   User leo
复制代码

server设置:

输入密码毗连

3、代码编译

编译代码:修改Makefile,在gcc 后面加上 -ggdb选项
  1. # -------------[ CFLAGS ] -------------
  2. CFLAGS += -g
  3. CFLAGS += -ggdb
  4. CFLAGS += -Wall
  5. CFLAGS += -I.
  6. CFLAGS += -I $(TOPDIR)/api
  7. CFLAGS += -I $(TOPDIR)/daemon
  8. ......
复制代码
然后和从前一样编译代码,如许代码编译出来的可执行文件会更大一些,保留了调试信息在可执行文件中。
4、代码调试

4.1、修改launch.json文件

点击运行->启动调试(或者按F5)。新的代码未创建launch.json文件,选择C++(GDB/LLDB),选择默认配置会主动生成一个launch.json文件。


或者
或者点击调试,选择创建爱你launch.json文件。,选择C++(GDB/LLDB)生成一个launch.json文件。

修改launch.json文件
  1. {
  2.     // 使用 IntelliSense 了解相关属性。
  3.     // 悬停以查看现有属性的描述。
  4.     // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  5.     "version": "0.2.0",
  6.     "configurations": [
  7.         {
  8.             "type": "cppdbg",
  9.             "request": "launch",
  10.             "name": "(gdb)调试",
  11.             "program": "${workspaceFolder}/GW_EVSE",
  12.             "args": [],
  13.             "stopAtEntry": false,
  14.             "cwd": "${workspaceFolder}",
  15.             "environment": [],
  16.             "externalConsole": false,
  17.             "MIMode": "gdb",
  18.             "miDebuggerPath":
  19.             "/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",
  20.             //"/usr/local/arm_gcc/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gdb",
  21.             "miDebuggerServerAddress": "192.168.3.143:2001",
  22.             "setupCommands": [
  23.                 {
  24.                     "description": "为 gdb 启用整齐打印",
  25.                     "text": "-enable-pretty-printing",
  26.                     "ignoreFailures": true
  27.                 }
  28.             ]
  29.         }
  30.     ]
  31. }
复制代码
注意交错编译器和可执行文件名称必要修改。上面交错编译器有两个,必要和编译代码的交错编译器对应上,否则程序不可执行,会段错误。
4.2、gdbserver启动

将程序可执行文件下载到arm linux开发板后,执行
  1. root@ATK-IMX6U:/opt# gdbserver 192.168.3.143:2001 GW_EVSE
  2. Process gdbApp created; pid = 1629
  3. Listening on port 2001
复制代码
或者本机地址可以省略
  1. root@ATK-IMX6U:/opt# gdbserver 3:2001 GW_EVSE
  2. Process gdbApp created; pid = 1629
  3. Listening on port 2001
复制代码
然后在PC端VSCode启动调试,就可以打断点进行调试了

四、客户端命令行gdbserver调试

1、在arm开发板启动gdbserver

  1. root@ATK-IMX6U:/opt# gdbserver 192.168.3.143:2001 GW_EVSE
  2. Process gdbApp created; pid = 1629
  3. Listening on port 2001
复制代码
2、在客户端输入命令

  1. 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
  2. GNU gdb (GDB) 7.10.1
  3. Copyright (C) 2015 Free Software Foundation, Inc.
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  5. This is free software: you are free to change and redistribute it.
  6. There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  7. and "show warranty" for details.
  8. This GDB was configured as "--host=x86_64-pokysdk-linux --target=arm-poky-linux-gnueabi".
  9. Type "show configuration" for configuration details.
  10. For bug reporting instructions, please see:
  11. <http://www.gnu.org/software/gdb/bugs/>.
  12. Find the GDB manual and other documentation resources online at:
  13. <http://www.gnu.org/software/gdb/documentation/>.
  14. For help, type "help".
  15. Type "apropos word" to search for commands related to "word"...
  16. Reading symbols from GW_EVSE...done.
  17. (gdb)
复制代码
3、开启远程毗连

  1. (gdb) target remote 192.168.3.143:2001
  2. Remote debugging using 192.168.3.143:2001
  3. Reading /lib/ld-linux-armhf.so.3 from remote target...
  4. warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
  5. Reading /lib/ld-linux-armhf.so.3 from remote target...
  6. Reading symbols from target:/lib/ld-linux-armhf.so.3...Reading /lib/ld-2.23.so from remote target...
  7. Reading /lib/.debug/ld-2.23.so from remote target...
  8. (no debugging symbols found)...done.
  9. 0x76fcfac0 in ?? () from target:/lib/ld-linux-armhf.so.3
  10. (gdb)
复制代码
4、打断点

  1. (gdb) b 140
  2. Breakpoint 1 at 0x1efac: file main/main_pro.c, line 140.
  3. (gdb)
复制代码
5、c继续运行,在断点处停止

  1. (gdb) c
  2. Continuing.
  3. Reading /lib/librt.so.1 from remote target...
  4. Reading /lib/libpthread.so.0 from remote target...
  5. Reading /lib/libm.so.6 from remote target...
  6. Reading /lib/libdl.so.2 from remote target...
  7. Reading /lib/libc.so.6 from remote target...
  8. Reading /lib/librt-2.23.so from remote target...
  9. Reading /lib/.debug/librt-2.23.so from remote target...
  10. Reading /lib/libpthread-2.23.so from remote target...
  11. Reading /lib/.debug/libpthread-2.23.so from remote target...
  12. Reading /lib/libm-2.23.so from remote target...
  13. Reading /lib/.debug/libm-2.23.so from remote target...
  14. Reading /lib/libdl-2.23.so from remote target...
  15. Reading /lib/.debug/libdl-2.23.so from remote target...
  16. Reading /lib/libc-2.23.so from remote target...
  17. Reading /lib/.debug/libc-2.23.so from remote target...
  18. [New Thread 1725]
  19. [New Thread 1726]
  20. [New Thread 1727]
  21. [New Thread 1728]
  22. [New Thread 1729]
  23. [New Thread 1731]
  24. [New Thread 1732]
  25. [New Thread 1733]
  26. [New Thread 1734]
  27. [New Thread 1736]
  28. [New Thread 1738]
  29. [New Thread 1739]
  30. [New Thread 1740]
  31. Breakpoint 1, main () at main/main_pro.c:140
  32. warning: Source file is more recent than executable.
  33. 140                     if(cnt > 30)
  34. (gdb)
复制代码
6、打印参数

  1. (gdb) r
  2. The "remote" target does not support "run".  Try "help target" or "continue".
  3. (gdb) c
  4. Continuing.
  5. Breakpoint 1, main () at main/main_pro.c:140
  6. 140                     if(cnt > 30)
  7. (gdb) c
  8. Continuing.
  9. Breakpoint 1, main () at main/main_pro.c:140
  10. 140                     if(cnt > 30)
  11. (gdb) print cnt
  12. $1 = 3
  13. (gdb)
复制代码
从上面看出,远程模式下,不支持全速运行 r 命令。
7、其他功能

这里不再赘述,可以查看操纵手册。
总结

本文为嵌入式开发职员提供了一套完备的 VSCode 远程调试办理方案。通过安装必要的插件和配置远程毗连,开发者可以在 VSCode 中方便地进行代码编写和调试。core dump 功能可以帮助快速定位程序崩溃的原因,而 gdbserver 则实现了远程设备与当地调试环境的无缝毗连。通过修改 launch.json 文件和精确配置交错编译器路径,开发者可以在 VSCode 中轻松启动和管理远程调试会话。别的,文章还介绍了如何通过命令行使用 gdbserver 进行调试,进一步扩展了调试功能的机动性。总之,本文为希望在 VSCode 中实现高效远程调试的开发者提供了实用的指导和参考。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

前进之路

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表