- 编译选项
SET(CMAKE_CXX_FLAGS "-std=c++11 -g")
- 遇到optimize-out variable
增加参数 -O0
- gdb 启动
gdb ./YOU_EXEC_NAME
## bt
## f 0 # 根据 实际和自己代码最有关的进入
## print var / p var
## list
- gdb coredump
gdb ./YOUR_EXEC_NAME coredump_file
- gdb 连接在线软件
## 1
gdb -p PID
gdb --program PID
gdb attach PID
#0 unlimit -c 查询
ulimit -c unlimited
#1 设置core dump 输出, 这样设置是临时的,开始重启会丢失
sudo bash -c "echo /tmp/core.%e.%p.%h.%t > /proc/sys/kernel/core_pattern"
#2 系统设置
vim /etc/sysctl.conf
kernel.core_pattern = /tmp/core.%e.%p.%h.%t
最后sudo sysctl -p 来应用修改后的参数
"pattern"类似我们C语言打印字符串的格式,相关标识如下:
%%: 相当于%
%p: 相当于<pid>
%u: 相当于<uid>
%g: 相当于<gid>
%s: 相当于导致dump的信号的数字
%t: 相当于dump的时间
%h: 相当于hostname
%e: 相当于执行文件的名称
#3 每次开机生效
- gdb 与vscode
{
// 推荐方式1和方式3
// stopAtEntry 进入默认打断点
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
// "program": "/data/path/work/YOUR_EXEC_NAME",
"program": "/data/path/work/YOUR_EXEC_NAME",
"args": ["/data/path/world/xxx.sqlite", "49", "/data/path/work/xx.file", "1", "112.57501644,26.82793034"],
"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
}
]
},
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "/data/path/work/YOUR_EXEC_NAME",
"args": [""/data/path/world/xxx.sqlite", "49", "/data/path/work/xx.file", "1", "112.57501644,26.82793034"],
"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
}
]
},
{
"name": "ROS Launch",
"type": "ros",
"request": "launch",
"target": "/home/xxxxx/catkin_ws/src/xxx_pkg/launch/xxx.launch"
}
]
}
设置条件断点
watch 监听
call stack 0 是最近的
鼠标悬停打印变量
- ros launch 添加标签
<node name="xxx" pkg="xxx" type="xxx" output="screen" launch-prefix="gdb -ex run --args"/>
preLaunchTask
### task.json
{
"tasks": [
{
"type": "shell",
"label": "catkin build",
"command": "cd /home/xxx/xxx/ && source install/setup.bash && catkin_make -DROS=1 install",
// "args": [
// "-DCMAKE_BUILD_TYPE=Debug",
// "-DCATKIN_WHITELIST_PACKAGES=hello_world_node"
// ],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "ros build"
},
{
"label": "runscript",
"type": "shell",
"command": "cd /home/xxx/xxx/install/ && source set_envxxx.sh && env | head -n -1 > /home/xxx/xxx/install/temp_launch.env",
// "args": ["/home/xxx/install/set_env.sh"],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "change_dir",
"type": "shell",
"command": "cd /home/xxx/xxx/install",
// "args": ["/home/xxx/install/set_env.sh"],
"group": {
"kind": "build",
"isDefault": false
}
},
],
"version": "2.0.0"
}
{
// stopAtEntry 进入默认打断点
"version": "0.2.0",
"configurations": [
{
"name": "gdb_xxx_planner",
"type": "cppdbg",
"request": "launch",
// "program": "/home/xxx/xxx/install/bin/xxx",
"program": "${workspaceRoot}/../install/bin/xxx",
"args": [],
"stopAtEntry": false,
"cwd": "/home/xxx/xxx/install",
"environment": [
{
"name": "LD_LIBRARY_PATH",
"value": "${LD_LIBRARY_PATH}:/home/xxx/xxx/install/lib:/home/xxx/xxx/install/lib/xxx/opengl/lib"
// "value": "${LD_LIBRARY_PATH}:/home/xxx/xxx/install/lib:/home/xxx/xxx/install/lib/xxx/opencv33/lib:/home/xxx/xxx/install/lib/xxx/xxx/lib"
}
],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "change_dir"
// "envFile": "/home/xxx/xx/install/temp_launch.env",
}
]
}