小马的世界

How to Debug ROS2 C++ Node

2023-10-20 · 3 min read
自动驾驶 英语English英語

This implementation was found by Alejandro Duarte and Jeison Garcia.

Description

This is a small tutorial on how to debug a ROS2 C++ node usign VSCode.

Debug

Once you have your C++ code correctly implemented (at least compile), the First thing to do is to compile the package exporting the symbols (allow the breakpoints where you want to stop the code):

cd 
cd autoware
colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo
source install/setup.bash

Second, we have to launch the GDB Server for debbuging the CPP Code. Here, we will use a localhost:port for creating the server. Choose any free port that you want.

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

Third, we have to create a launch.json on VSCode. In other words, we will create a custom debugging configuration. In our case, create a GDB client and connect to the server.

1) Open VSCode on your workspace.
2) Go to your side bar, 'Run and Debug' section.
3) Add a new configuration (Select C++ enviroment or any other)
4) On your launch.json file, put the following information

Launch.json file:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "C++ Debugger",
                "request": "launch",
                "type": "cppdbg",s
                "miDebuggerServerAddress": "localhost:3000",
                "cwd": "/",
                "program": "[build-path-executable]"
            }
        ]
    }
  • name - Custom name of your debugger configuration
  • request - In this case we want to launch the client
  • type - cppdbg for c++ debugging
  • miDebuggerServerAddress - path_server:port
  • cwd - Where to find all the required files. We use root because ROS, the package, and other required files are distributed along the entire PC.
  • program - Change [build-path-executable] by your executable build file. You can find this path on the console when you launch the server.

Lastly, use the VSCode buttons and panels option to debug correctly your code.