Skip to content

Debugging C/C++ code in ROS

When figuring out issues with your code, it is always nice to have some tools available at hand!

Documentation index — full table of contents.

Visual debugging inside the development container

If you run a development container in VS Code, you can use the ROS extension to debug a node!

Follow the instructions on how to debug using the msiot/vscode-ros extension.

Caveats:

We found that using the current setup, it is only possible to debug small launch files that don't include other launch files. A workaround to debug a single node is to launch them separately:

  1. For example, let's say you want to debug the ros_wifi_com package. Comment out the ros_wifi_com entry in run_single_car.launch.
  2. Launch the reset of the nodes through roslaunch crs_launch run_single_car.launch.
  3. Use the ROS extension as shown in the tutorial above to launch the wifi_com_default.launch file separately. You can now set breakpoints inside wifi_com.cpp and enjoy hassle-free debugging!

GDB debugging

It is possible to debug C and C++ ROS code using gdb.

To do so it is necessary to recompile CRS in debugging mode:

catkin config --cmake-args -DCMAKE_BUILD_TYPE=Debug

catkin clean

catkin build

It is then necessary to use gdb in the roslaunch file. Read here here for more information.

Debugging C++ using GDB for a certain Node

To debug e.g. segfaults you can use GDB. It will output where the program died. By typing bt in the terminal, the last lines that were executed before the program failed will be shown (backtrace). In order to debug a given node, e.g. the estimator node you need to add the following argument to the launch file: launch-prefix="gdb -ex run --args"

Example Estimator Node: In src/ros4crs/ros_estimators/launch/default.launch set the following

<node pkg="ros_estimators" name="estimation_node" type="ros_estimation_node" output="screen" launch-prefix="gdb -ex run --args" >

Debugging with gdb and tmux

Tmux allows you to run a certain node in a different terminal, so you can control the debugging process and set breakpoints. By typing tmux in the docker container terminal, a tmux terminal is started. You need to have a tmux session started before running your ros code. You can leave the tmux session (have it running in the background) by pressing ctrl+B D. In order to debug a given node, e.g. the estimator node using tmux and gdb you need to add the following argument to the launch file: launch-prefix="/code/.debug/tmux_debug.sh"

Example Estimator Node: In src/ros4crs/ros_estimators/launch/default.launch set the following

<node pkg="ros_estimators" name="estimation_node" type="ros_estimation_node" output="screen" launch-prefix="/code/.debug/tmux_gdb_debug.sh" >

Once you started the code (roslaunch ....) you will need to open a second terminal (also in the Docker) and connect to the tmux session using tmux a. This allows you to run any gdb command (CheatSheet) in interactive mode.

Some additional nice tmux commands are:

  • ctrl+b d to (d)etach from a session
  • ctrl+b c to open a new (c)onsole.
  • ctrl+b x to e(x)it the tmux session.