Skip to content

Instantly share code, notes, and snippets.

@ZhenshengLee
Forked from JADC362/ROS2DebugVSCode.md
Created June 10, 2022 04:39
Show Gist options
  • Save ZhenshengLee/dd1cce9085a29455c0c136d9be618eac to your computer and use it in GitHub Desktop.
Save ZhenshengLee/dd1cce9085a29455c0c136d9be618eac to your computer and use it in GitHub Desktop.
Debug ROS2 C++ node on VSCode (Ubuntu)

Debug ROS2 C++ node on VSCode (Ubuntu)

Description

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

Requeriments

This implementation was done using:

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 ros_ws
 - 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",
                "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.

This implementation was found by Alejandro Duarte and Jeison Garcia.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment