vscode + gdb 调试ros2程序

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

ps -ef | grep executable_name
进程pid的为 6324

  • 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 附加也无法调试
知道的小伙伴, 评论留言告知下,想了解如何调试.


总结

本质上还是

  1. 熟悉gdb, gdbserver调试命令,方法
  2. 编译debug版本软件
  3. vscode gdb调试配置

参考

ros2 gdbserver 调试论坛总结

签名
一个嵌入式-机器人-网络-系统 软件开发工程师
微信: liuj1637664504
在这里插入图片描述


<think>好的,用户的问题是在VSCode中对ROS2进行单步调试的方法或教程。首先,我需要回忆一下ROS2调试流程和VSCode的相关配置。 首先,用户可能已经安装了VSCodeROS2,但需要配置调试环境。VSCode常用的调试工具是C++和Python的调试扩展。对于ROS2来说,主要涉及C++节点和Python节点,所以需要分别考虑。 接下来,安装必要的扩展是必须的。比如C/C++扩展和Python扩展,可能还需要ROS扩展,但ROS2官方是否有专门的VSCode插件呢?记得微软有一个ROS扩展,支持ROSROS2,所以需要推荐用户安装这个。 然后是创建工作区。通常ROS2项目使用colcon构建,所以需要用户打开工作空间根目录。接着配置tasks.json来构建项目,使用colcon build命令,并启用符号链接,这样构建后的文件可以直接运行。 调试配置方面,C++节点需要使用GDB,而Python节点可能需要使用debugpy。对于C++,在launch.json中添加配置,指定目标程序,比如安装路径下的节点可执行文件。可能需要设置args参数传递节点名和其他参数。Python节点则需要配置脚本路径,并设置环境变量,比如source ROS2的setup文件,这样才能找到必要的库。 另外,混合调试的情况,比如同时调试C++和Python,可能需要更复杂的配置,或者分开调试。用户可能需要分步骤进行,先调试C++部分,再处理Python。 还需要注意环境变量的加载,比如在调试配置中通过env或使用脚本来设置ROS2的环境。否则可能会出现找不到库的情况。比如在launch.json中,使用"setup"脚本来source install/setup.bash。 可能的问题点包括:路径是否正确,节点名称是否正确,是否构建时带有调试信息(比如CMake中的Debug模式),以及权限问题,比如节点是否可执行。此外,如果使用Python,可能需要安装debugpy模块,并在代码中插入断点。 用户可能需要具体的步骤示例,比如如何编写launch.json和tasks.json,以及如何启动调试会话。同时,提供常见错误的解决方案,比如环境变量未加载导致的库找不到,或者构建配置不正确导致的符号缺失。 最后,生成相关问题,帮助用户进一步解决可能遇到的其他问题,比如性能分析、优化调试配置、处理依赖问题等。</think>### 如何在VSCode中单步调试ROS2 #### 1. **环境准备** - 安装VSCode及必要插件: - **C/C++**(Microsoft官方插件,用于C++调试) - **Python**(Microsoft官方插件,用于Python调试) - **ROS**(Microsoft官方ROS插件,支持ROS2)[^1] - 确保ROS2环境已正确配置(通过`source /opt/ros/<distro>/setup.bash`)。 #### 2. **配置构建任务** 1. 打开ROS2工作区根目录。 2. 创建/编辑`.vscode/tasks.json`,添加以下内容: ```json { "version": "2.0.0", "tasks": [{ "label": "colcon build", "type": "shell", "command": "colcon build --symlink-install", "group": "build", "problemMatcher": [] }] } ``` > 使用`--symlink-install`避免重复构建[^2]。 #### 3. **调试C++节点** 1. 创建/编辑`.vscode/launch.json`,添加配置: ```json { "name": "Debug C++ Node", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/install/<package_name>/lib/<package_name>/<node_name>", "args": [], "environment": [{"name": "ROS_DOMAIN_ID", "value": "0"}], "cwd": "${workspaceFolder}", "setupCommands": [{ "description": "Load ROS2环境", "text": "source /opt/ros/<distro>/setup.bash" }] } ``` 2. 设置断点后按`F5`启动调试。 #### 4. **调试Python节点** 1. 安装Python调试器: ```bash pip install debugpy ``` 2. 在Python节点代码中插入断点: ```python import debugpy debugpy.listen(5678) debugpy.wait_for_client() # 阻塞直到连接调试器 ``` 3. 配置`launch.json`: ```json { "name": "Debug Python Node", "type": "python", "request": "attach", "port": 5678, "host": "localhost", "pathMappings": [{ "localRoot": "${workspaceFolder}", "remoteRoot": "." }] } ``` #### 5. **常见问题** - **依赖缺失**:通过`rosdep install`安装缺失依赖。 - **环境未加载**:在`launch.json`中显式调用`source`命令加载ROS2环境。 - **权限问题**:使用`--allow-root`或调整文件权限。 ```python # 示例:Python节点调试启动脚本 import rclpy from rclpy.node import Node import debugpy class DebugNode(Node): def __init__(self): super().__init__('debug_node') debugpy.listen(5678) debugpy.wait_for_client() self.get_logger().info("Debugger attached") def main(): rclpy.init() node = DebugNode() rclpy.spin(node) rclpy.shutdown() if __name__ == '__main__': main() ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值