您的当前位置:首页正文

vscode + gdb 调试ros2程序

2024-11-10 来源:个人技术集锦

1.c++ 调试方法

方法1.vscode + gdbserver + ros2 run 启动程序调试

1.编译 gdb版本

colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo

2.运行 + 前缀gdbserver

ros2 run --prefix 'gdbserver localhost:6688' package_name executable_name

3.vscode配置
launch.json

        {
            "name": "gdbserver debug",
            "request": "launch",
            "type": "cppdbg",
            "miDebuggerServerAddress": "localhost:6688",
            "cwd": "/",
            "program": "${workspaceFolder}/install/lib/package/xxx_node", # 程序可执行文件位置
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        },

等价于gdb命令
gdb install/lib/package/xxx_node
(gdb) target remote localhost:6688

方法2: gdbserver调试已运行的程序

1.编译gdb版本
同方法1

2.gdbserver 粘附调试已运行的程序

  • 运行程序
    ros2 run package_name executable_name

  • 获取进程pid

  • gdbserver粘附上已运行程序的pid
    gdbserver --attach :6688 6324

3.vscode配置
同方法1

3.ros2 launch 开启gdbserver调试选项

python launch启动

def generate_launch_description():
    return LaunchDescription([
        Node(
            package='package',
            executable='executable',
            name='name',
            prefix=['gdbserver localhost:6688'],
            output='screen'
        ),
    ])

xml启动

<launch>
  <node pkg="pkg" exec="node" name="name" launch-prefix="gdbserver localhost:6688">
  </node>
</launch>

4.gdb 运行ros2 程序

gdb调试ros2程序\

. install/setup.bash # 不执行此步骤, 运行时会提示 libcommon__rosidl_typesupport_cpp.so 找不到
gdb ./install/2_cpp_base/lib/2_cpp_base/31_log_output

vscode + gdb调试ros2程序
1.ros2 环境变量添加~/.bashrc中
echo source /opt/ros/humble/setup.bash >> ~/.bashrc

2.vscode 通用的c++调试方法
launch.json

        {
            "name": "c++ 调试",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/install/2_cpp_base/lib/2_cpp_base/5_ros2_service_server",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            //"preLaunchTask": "colcon",
            "miDebuggerPath": "/usr/bin/gdb"
        }

5.官方调试方法 --验证不通过

ros2 run --prefix 'gdb -ex run --args' 2_cpp_base 5_ros2_service_server

Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/liuj/3_work/7_ros-robot-example/ros2/install/2_cpp_base/lib/2_cpp_base/5_ros2_service_server...
Starting program: /home/liuj/3_work/7_ros-robot-example/ros2/install/2_cpp_base/lib/2_cpp_base/5_ros2_service_server 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff688e640 (LWP 10250)]
[New Thread 0x7ffff608d640 (LWP 10251)]
[New Thread 0x7ffff5805640 (LWP 10252)]
[New Thread 0x7ffff5004640 (LWP 10253)]
[New Thread 0x7ffff4803640 (LWP 10254)]
[New Thread 0x7ffff4002640 (LWP 10255)]
[New Thread 0x7ffff3801640 (LWP 10256)]
[New Thread 0x7ffff2f40640 (LWP 10257)]
[New Thread 0x7ffff2737640 (LWP 10258)]

进入这个模式之后, 使用gdb 附加也无法调试
知道的小伙伴, 评论留言告知下,想了解如何调试.


总结

本质上还是

参考

签名
一个嵌入式-机器人-网络-系统 软件开发工程师
微信: liuj1637664504


Top