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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值