Skip to content

Instantly share code, notes, and snippets.

@JADC362
Last active April 7, 2025 14:07
Show Gist options
  • Save JADC362/a4425c2d05cdaadaaa71b697b674425f to your computer and use it in GitHub Desktop.
Save JADC362/a4425c2d05cdaadaaa71b697b674425f 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.

@iampaulidrobo
Copy link

I can run gdb for a single executable.But he there a way to attach gdb for multiple executable which are called through launch file.
Currently I want to run the nav2 stack and understand the flow of all the nodes.

Can someone help me here,please?

@iampaulidrobo
Copy link

iampaulidrobo commented Oct 23, 2024

I found it out what I was missing.
Thank you so much ,me ,myself and I.

@juansuerorobotics
Copy link

juansuerorobotics commented Dec 27, 2024

Hi, has anyone made a debug configuration of 2 or more nodes(e.g. A_node started and publishes a a.msg, B_node subscirbes it and publishes another b.msg)? what should I do with launch.json to make it work?

you can run vscode twice over the same code base. its something like right click on File >> Duplicate Workspace
Then you can run two vscodes and just run two different launch configurations from the same launch.json

Other than that you could run both nodes from the same launch file.. the xml ros launch file
and it will step through your breakpoints one at a time only
so youll be bouncing from publisher to subscriber cpp files you might not want that.

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