ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Debugging ROS 2 cpp node with vs code starting the node with a launch file

asked 2021-08-27 11:55:15 -0500

__Jeremy__ gravatar image

updated 2021-08-27 11:57:15 -0500

Hello everyone,

I'm using vs code and ROS 2 foxy. If I start the node executable directly, it's easy to connect it to the vs code debugger. Unfortunately I couldn't figure out connecting the debugger to the node executable if I start it using a launch file. Is this possible?

The reason I want to start it with the launch file is that the launch file loads the parameters from a config.yaml file. And if the parameters are not available the node won't work properly. If there is an option to load the parameters without a launch, will also help. Any help is appreciated :-).

Regards, Jeremy

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2021-08-28 03:02:16 -0500

Per Edwardsson gravatar image

Ok, I haven't tried this myself, but I think it should work. Check the second answer here. It explains how you attach a ROS2 node to VS code. Then, in order to use gdb from a launch file, navigation2 has you covered. I _think_ you can combine these two ideas to debug VS code from a launch file. If you get it to work, do get back to me. :)

edit flag offensive delete link more


Thanks Per for your answer. It worked. I used this in my launch file:

        # prefix=['xterm -e gdb -ex run --args'],
        prefix=['gdbserver localhost:3000'],

and started the node with: ros2 launch test_package test_package_node. Afterwards I started the debug session with this launch description:

  "name": "C++ Debugger",
  "request": "launch",
  "type": "cppdbg",
  "miDebuggerServerAddress": "localhost:3000",
  "cwd": "/",
  "program": "${workspaceFolder}/install/test_package/lib/test_package/test_package_node"
__Jeremy__ gravatar image __Jeremy__  ( 2021-09-10 11:11:17 -0500 )edit

The only thing which still grinds my gears is that I wasn't able to start everything with a single launch command. So what I did is, I put the ros2 launch command into task and then I set this task as pre-launch task in launch task. Unfortunately this doesn't work since the ros2 launch command doesn't return and thus the launch task gets stuck in the terminal where the ros2 command was executed. Any ideas on this?

__Jeremy__ gravatar image __Jeremy__  ( 2021-09-10 11:21:51 -0500 )edit

Okay I found a solution by myself. Maybe I should put more effort in finding a solution before asking questions :-D:

__Jeremy__ gravatar image __Jeremy__  ( 2021-09-10 11:40:45 -0500 )edit

How can we use this method to debug Python based ROS2 packages?

Raza Rizvi gravatar image Raza Rizvi  ( 2022-02-01 09:41:41 -0500 )edit

Any idea how to do this for a ComposableContainer, which consists of composableNodes? The program: attribute can't tie to an executable since CCs are formed on the fly

jd2548 gravatar image jd2548  ( 2022-09-29 13:42:32 -0500 )edit

answered 2021-08-31 04:57:03 -0500

You can use this configuration (this can be generated using ROS extension for VSCode). You can then debug all nodes that are present in the launch file. Also remember to build with debug & have workspace as a project root directory in VSCode.

colcon build --cmake-args -DCMAKE_BUILD_TYPE=RelWithDebInfo


"launch": {
        "version": "0.2.0",
        "configurations": [
            "name": "ROS: Launch",
            "type": "ros",
            "request": "launch",
            "target": "absolute_path_to_the_launch_file",
            "args": ["argument1:=arg1", "argument2:=arg2"]
edit flag offensive delete link more

answered 2021-08-28 02:56:10 -0500

updated 2021-08-28 06:34:14 -0500

Add prefix command to your node declaration in file.

node = Node(
    prefix=['xterm -e gdb -ex run --args'],

This will start your node in gdb in a seperate xterm Terminal. So you need have that installed with sudo apt install xterm.

edit flag offensive delete link more

Question Tools


Asked: 2021-08-27 11:55:15 -0500

Seen: 2,450 times

Last updated: Aug 31 '21