[Documentation] [TitleIndex] [WordIndex

(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Debugging Gazebo plugins

Description: You can run gazebo with gdb to debug real time controllers and plugins.

Tutorial Level: ADVANCED

  Show EOL distros: 

Gazebo will segfault if any of the components (mechanism controller stack, dynamic plug-ins, etc) fail. To make PR2 simulator a useful tool, you will want to run it in debug mode sometimes. Assuming user is familiar with gdb, here are 3 ways to obtain a backtrace.

Launching GDB with roslaunch

  • Append the following line to your .bashrc:

    source `rospack find gazebo`/setup.bash
  • Add launch-prefix="gdb --args" and output="screen" to the gazebo node in the launch script. For example, modify the file gazebo_worlds/launch/empty_world.launch:

    <node pkg="gazebo" launch-prefix="gdb --args " type="gazebo" args="-n $(find gazebo_worlds)/worlds/empty.world" respawn="false" output="screen">
      <env name="LD_LIBRARY_PATH" value="$(find pr2_gazebo_plugins)/lib:$(find gazebo)/gazebo/lib:$(optenv LD_LIBRARY_PATH)" />
      <env name="GAZEBO_RESOURCE_PATH" value="$(find gazebo_worlds):$(find gazebo)/gazebo/share/gazebo" />
      <env name="OGRE_RESOURCE_PATH" value="$(find ogre)/ogre/lib/OGRE" />
    </node>

    In the terminal after roslaunching the modified empty_world.launch, you will have to type run and press enter at the (gdb)-prompt to start gazebo.

Manual

  • Terminal 1: Startup ROS core
    roscore
  • Terminal 2: Create a launch script, save as upload.launch:

    <launch>
      <param name="robot_description" command="$(find xacro)/xacro.py '$(find pr2_description)/robots/pr2.urdf.xacro'" />
    </launch>
    then send robot description XML to the parameter server,
    roslaunch upload.launch
  • Terminal 3: Start Gazebo with an empty world and run gdb on gazebo to catch segfaults:
    export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`rospack find pr2_gazebo_plugins`/lib:`rospack find gazebo`/gazebo/lib
    export GAZEBO_RESOURCE_PATH=$GAZEBO_RESOURCE_PATH:`rospack find pr2_ogre`:`rospack find pr2_defs`:`rospack find pr2_description`:`rospack find gazebo_worlds`:`rospack find gazebo`/gazebo/share/gazebo
    export OGRE_RESOURCE_PATH=$OGRE_RESOURCE_PATH:`rospack find ogre`/ogre/lib/OGRE
    gdb --args `rospack find gazebo`/gazebo/bin/gazebo `rospack find gazebo_worlds`/worlds/empty.world
  • Terminal 4: Spawn PR2 in the empty world
    rosrun gazebo spawn_model -urdf -param robot_description -model pr2
  • Terminal 5: Optional - load default set of controllers for PR2
    roslaunch `rospack find pr2_gazebo`/controllers/pr2_default_controllers.launch

Attach GDB to a Running Process

  • Bring up Gazebo normally, find the process id for gazebo, then attach gdb to the gazebo process. For example, if you know gazebo is running with process id 1234,
    localhost:~/ros/ros-pkg> gdb --pid 1234


Gazebo will segfault if any of the components (mechanism controller stack, dynamic plug-ins, etc) fail. To make PR2 simulator a useful tool, you will want to run it in debug mode sometimes. Assuming user is familiar with gdb, here are multiple ways to obtain a backtrace.

Launching GDB with roslaunch

Add launch-prefix="$(find gazebo_worlds)/scripts/gdbrun" to the gazebo node in the launch script. For reference, take a look at debug.launch.

Attach GDB to a Running Process

  • Bring up Gazebo normally, find the process id for gazebo, e.g.

$ ps -ef | grep 'gazebo/bin/gazebo'
user       1234  2345 12 12:00 ?        00:00:01 /home/user/projects/electric_release/simulator_gazebo/gazebo/bin/gazebo -s -1 -u /home/user/projects/electric_release/simulator_gazebo/gazebo_worlds/worlds/empty.world __name:=gazebo __log:=/home/user/.ros/electric_release/log/012304e0-c778-11e0-ae1a-f46d1234a4c9/gazebo-2.log

Next, attach gdb to the gazebo process. For example, given the above result from ps,

localhost:~/ros/ros-pkg> gdb --pid 1234

When gdb stops loading, type cont at the gdb prompt to continue,

(gdb) cont

When gazebo segfaults, you can return to the gdb prompt and use your favorite gdb commands to debug.



2019-09-14 13:04