GDB - To debug your C++ ROS code more efficient

post-thumb

Published on Jan 18, 2022 by Doan Nguyen

In general, gbd is a very powerful tool for debugging C++ at runtime. In order to use it as you are developing your C++ ROS code, you need to do some extra steps.

I actually learned this trick from a colleague, and want to save it for future use by writing this post.

Step 1: Compile your code in debug mode.

catkin build your_ros_pkg -DCMAKE_BUILD_TYPE=Debug
Step 2: Run your node with gdb. The easiest way is to create a launch file for your node, similar to the below sample
<launch>
    <!-- <arg name="launch_prefix"         default=""/> -->
    <arg name="launch_prefix"         default="gdb -ex run -args"/>
    <node name="mpc_controller_node" 
            pkg="mpc_controller_ros" 
            type="mpc_controller_node" 
            output="screen"  launch-prefix="$(arg launch_prefix)">
        <rosparam file="$(find mpc_controller_ros)/params/mpc_controller_params.yaml" command="load" />
    </node>
</launch>

Step 3: There NO step 3. So simple, right?

Update 03/31/2022: Some common gdp commands I often use:

  • bt - backtrace
  • where - where the execution currently at (the error occurs)

I don’t use gdb extensively, so explore yourself and have fun learning