Home | Tutorials | Wiki | Issues
Ask Your Question

How to enable debugging for a plugin?

asked 2017-04-04 04:53:06 -0500

lubiluk gravatar image

I've created a plugin by following a tutorial from Gazebo website. Now I'd like to take it further but it is difficult to work without being able to set breakpoints and inspect variables. What steps do I have to do in order to be able to set breakpoints in my code?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-04-04 18:56:12 -0500

Carlos Agüero gravatar image

updated 2017-04-04 19:20:47 -0500

I'll try to answer with an example:

Let's assume that you want to debug some code in the LinearBatteryPlugin that is shipped with Gazebo. You can launch Gazebo with the linear_battery_demo.world for loading the plugin. Let's also assume that your goal is to inspect the member variable LinearBatteryPlugin::q within the function LinearBatteryPlugin::OnUpdateVoltage(). Here are the steps for doing it using gdb:

  • Make sure that you compiled Gazebo (or your plugin) with debug symbols. See this reference tutorial.
  • Start gdb specifying gzserver as the executable program:

    gdb gzserver
  • Start gzserver with (assuming that your current directory is the top level directory of the Gazebo repository):

    run worlds/linear_battery_demo.world
  • You should see some gdb messages indicating that the program has started.

  • Interrupt the execution of gzserver by pressing CTRL-C.
  • We're going to create a breakpoint on a function. We can double check that gdb has the definition of the function (note that we have to indicate the entire namespace):

    list gazebo::LinearBatteryPlugin::OnUpdateVoltage(const gazebo::common::BatteryPtr&)
  • gdb should show a partial view of the source code:

    123  this->Init();
    124 }
    126 /////////////////////////////////////////////////
    127 double LinearBatteryPlugin::OnUpdateVoltage(const common::BatteryPtr &_battery)
    128 {
    129   double dt = this->world->GetPhysicsEngine()->GetMaxStepSize();
    130   double totalpower = 0.0;
    131   double k = dt / this->tau;
  • Now, you can create your breakpoint:

    break gazebo::LinearBatteryPlugin::OnUpdateVoltage(const gazebo::common::BatteryPtr&)
  • gdb should confirm that the breakpoint was created. It's time to resume the gzserver execution:

  • Your execution should be interrupted when the code entered OnUpdateVoltage() and gdb should tell you that your breakpoint was hit:

    [Switching to Thread 0x7fff797f7700 (LWP 20446)]
    Thread 31 "gzserver" hit Breakpoint 1, gazebo::LinearBatteryPlugin::OnUpdateVoltage (this=0x7fff44005a40, 
    std::shared_ptr (count 4, weak 1) 0x180ed20) at 
    128 {
  • Now you can inspect your q variable:

    print q
  • And gdb should tell you the value:

    (gdb) print q
    $1 = 1.1660311540299606

Hopefully this is what you're looking for. Alternatively, if you're using Gazebo 8 we put in place an introspection system that is described in this tutorial (currently under review).

edit flag offensive delete link more


Thank you for your exhaustive answer. I could only add a clarification for future readers that if we want to debug only the plugin we're working on, we don't need to compile whole gazebo with debug symbols. Just a plugin is enough. We do this by using `cmake -DCMAKE_BUILD_TYPE=Debug ..` I have managed to set breakpoints in my plugin on standard, binary gazebo installation. Graphical debugging through an IDE also works well.

lubiluk gravatar imagelubiluk ( 2017-04-07 03:11:30 -0500 )edit

Hi lubiluk , could you please show how to set the breakpoint in a IDE? I managed to launch gzserver in both VSCode and Qt and compiled my plugin with Debug flag. But seems like the IDE doesn't associate the cpp file with the executable so the breakpoint I set in the code doesn't work. Thanks, Malcolm

Malcolm gravatar imageMalcolm ( 2017-04-09 17:39:52 -0500 )edit
Login/Signup to Answer

Question Tools



Asked: 2017-04-04 04:53:06 -0500

Seen: 1,219 times

Last updated: Apr 04 '17