1.使能gdb与调试信息
1.程序CMakeLists.txt使能debug
SET(CMAKE_BUILD_TYPE Debug)
# SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
# SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
2.catkin_make指定debug版
catkin_make -DCMAKE_BUILD_TYPE=Debug
3.catkin_make调试指定的包
catkin_make -DCMAKE_BUILD_TYPE=Debug -DCATKIN_WHITELIST_PACKAGES="package1;package2"
2.vscode c++程序调试
vscode 安装c++插件,如图
创建调试的launch.json配置如下3种
launch 运行启动程序
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
// 注意更改的程序位置
"program": "${workspaceFolder}/ros1/catkin_ws/devel/lib/beginner_tutorials/talker",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
],
}
attach 调试已运行的程序
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 附加",
"type": "cppdbg",
"request": "attach",
// 注意更改的程序位置
"program": "${workspaceFolder}/ros1/catkin_ws/devel/lib/beginner_tutorials/talker",
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
],
}
F5运行之后,选择调试的程序即可
方法3: 远程调试-万能通用的方法
解决vscode 无root权限调试问题
配置.vscode/launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
// "preLaunchTask": "C/C++: g++ 生成活动文件",
"miDebuggerPath": "/usr/bin/gdb",
"miDebuggerServerAddress":"127.0.0.1:6688"
}
]
}
miDebuggerServerAddress 本机调试使用127.0.0.1, 6688是gdbserver调试端口
使用方法:
1.gdbserver启动程序
gdbserver 127.0.0.1:6688 ./a.out
2.gdbserver 调试已经运行程序
./a.out # 启动程序
ps -ef|grep a.out # 获取已运行的程序pid
gdbserver --attach 127.0.0.1:6688 a.out_pid
2.安装ros插件
.vscode/launch.json添加调试配置,多出ROS: Launch,ROS: Attach选项
方法同c++ , ROS: Attach指定程序即可
总结:
目前使用gdbserver --attach 已经运行程序 方法最稳定, 没有root权限问题, 没有参数设置问题.
推荐使用此方法.