当前位置: 首页 > news >正文

vscode中Debug c++

在vscode中Debug ros c++程序

1 Debug模式编译

如果用命令行catkin_make,在输入catkin_make时加上一个参数:

catkin_make -DCMAKE_BUILD_TYPE=Debug

或者直接修改CMakelist.txt,添加以下代码:

SET(CMAKE_BUILD_TYPE "Debug")

SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")

SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

2 编写launch文件

 rosrun package node 改为roslaunch package xxx.launch

3 vscode debug配置

"version": "0.2.0","configurations": [{"name": "ROS: Launch","type": "ros","request": "launch","target": "/home/aaa/mycompany/15weeding/infer/yolov8/yolov8_ws_4/src/instance_seg/launch/instance_seg.launch"},

4 Debug

打断点

启动roscore,点击绿色三角

调试方法

一种是调试coredump文件,另一种是在线调试。

大多使用在线调试,而在线调试又分为 roslaunch 调试和 rosrun node调试

使用gdb命令行在线调试

1安装xterm

sudo apt install xterm

2 Debug模式编译

如果用命令行catkin_make,在输入catkin_make时加上一个参数:

catkin_make -DCMAKE_BUILD_TYPE=Debug

或者直接修改CMakelist.txt,添加以下代码:

SET(CMAKE_BUILD_TYPE "Debug")

SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")

SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

launch文件添加GDB调试指令

在node标签中添加参数

launch-prefix="xterm -e gdb -ex run --args"

<launch>

    <node pkg="learning_topic" type="person_subscriber" name="talker" output="screen"  launch-prefix="xterm -e gdb -ex run --args"/>

    <node pkg="learning_topic" type="person_publisher" name="listener" output="screen" />

</launch>

The launch-prefix attribute of the tag that, among other things, makes it easy to debug a ROS node process. Here are some example launch-prefixes you might find useful:

launch-prefix=“xterm -e gdb --args” : run your node in a gdb in a separate xterm window, manually type run to start it

launch-prefix=“gdb -ex run --args” : run your node in gdb in the same xterm as your launch without having to type run to start it

launch-prefix=“stterm -g 200x60 -e gdb -ex run --args” : run your node in gdb in a new stterm window without having to type run to start it

launch-prefix=“valgrind” : run your node in valgrind

这个valgrind工具可以用于检测内存泄露,并执行性能分析

launch-prefix=“xterm -e” : run your node in a separate xterm window

launch-prefix=“nice” : nice your process to lower its CPU usage

launch-prefix=“screen -d -m gdb --args” : useful if the node is being run on another machine; you can then ssh to that machine and do screen -D -R to see the gdb session

launch-prefix=“xterm -e python -m pdb” : run your python node a separate xterm window in pdb for debugging; manually type run to start it

rosrun node 的方式进行调试

rosrun --prefix 'gdb -ex run --args'  [package_name]  [node_name]

gdb core dump 调试

        在Linux下可通过core文件来获取当程序异常退出(如异常信号SIGSEGV, SIGABRT等)时的堆栈信息。core dump叫做核心转储,当程序运行过程中发生异常的那一刻的一个内存快照,操作系统在程序发生异常而异常在进程内部又没有被捕获的情况下,会把进程此刻内存、寄存器状态、运行堆栈等信息转储保存在一个core文件里,叫core dump。core文件是程序非法执行后core dump后产生的文件,该文件是二进制文件,可以使用gdb、elfdump、objdump打开分析里面的具体内容。

产生core dump的可能原因:

(1)  内存访问越界;

(2) 多线程程序使用了线程不安全的函数;

(3) 多线程读写的数据未加锁保护;

 (4) 非法指针;

 (5) 堆栈溢出。

Debug模式编译

如果用命令行catkin_make,在输入catkin_make时加上一个参数:

catkin_make -DCMAKE_BUILD_TYPE=Debug

或者直接修改CMakelist.txt,添加以下代码:

SET(CMAKE_BUILD_TYPE "Debug")

SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")

SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

1、查看core dump是否打开,如果返回0代表没有打开,返回 unlimited 表示已经打开

ulimit -c

2、打开core dump

ulimit -c unlimited

执行这行命令之后,只针对当前终端有效。

如果想对所有终端都有效则在 .bashrc 末尾添加这行代码:

ulimit -c unlimited

3、生成可执行文件,要加-g选项,以获取到具体的行号信息,否则没有

g++ -g -o main main.c

4、执行程序后生成core文件,core文件默认放在可执行文件相同目录下,且文件名固定为core

./main

5、使用gdb调试core

gdb main core

6、查看程序堆栈信息

bt

相关文章:

  • 【言语理解】逻辑填空之逻辑对应11
  • DB-MongoDB-00002--Workload Generator for MongoDB
  • 安装和升级到devExpress23.1.7
  • 工作流介绍
  • Milvus向量数据库
  • 何首乌基因组-文献精读131
  • LearnOpenGL---着色器
  • 微服务项目->在线oj系统(Java版 - 3)
  • 大模型在胫骨平台骨折预测及治疗方案制定中的应用研究
  • 网络编程-select(二)
  • 组态王|组态王中如何添加西门子1200设备
  • 【QGIS二次开发】地图编辑-04
  • Python数据可视化 - Pyecharts绘图示例
  • 【Linux网络】五种IO模型与阻塞IO
  • 【MYSQL】笔记
  • Go 后端中双 token 的实现模板
  • 几种基于比较的排序
  • 建一个结合双向长短期记忆网络(BiLSTM)和条件随机场(CRF)的模型
  • 【java多线程】线程间通信-利用wait和notify轮流按序打印奇数和偶数
  • 什么是着色器 Shader
  • 武汉警方通报一起故意伤害案件:1人死亡,嫌疑人已被抓获
  • 十大券商看后市|A股指数有望进一步缓步推高,淡化短期波动
  • 一周观展|一批重量级考古博物馆开馆:从凌家滩看到孙吴大墓
  • 江南考古文脉探寻
  • 技术派|威胁F-35、击落“死神”,胡塞武装防空战力如何?
  • 穆迪下调美国主权信用评级