Using GDB and Valgrind with ROS Projects
This blog post requires neither long exposé nor tedious instructions. Here’s the sitch: you need to use GDB or Valgrind with your ROS project, and you’re either using roslaunch or rosrun. I ran into this when I needed to use GDB with ORB-SLAM, where I specifically was running my program with rosrun and CMake.
You use roslaunch
If you use roslaunch, ROS publishes their own instructions for modifying your launch file to use GDB or Valgrind. Add the correct launch-prefix to your launch file, and then run your program as you would normally.
You use rosrun
If you use rosrun, it’s harder to find exactly what you need on the internet without some digging, but the answer is actually quite straightforward.
For GDB, modify the command you run to include a
--prefix argument, like below:
1 rosrun --prefix 'xterm -e gdb --args' NameOfExecutable [your project arguments]
This will start an xterm window and load your program as the executable. Then, you can set any breakpoints you want. When you’re ready to run your program, just enter
run into GDB’s prompt.
In general, you can replace
xterm -e gdb --args with any of the other commands in the previously linked document. So, if you want to run Valgrind, replace
xterm -e gdb --args with
Of course, as always, you need to compile your code with the
-g flag for breakpoints to work. If you’re compiling it using a Makefile or … painstakingly by hand, I guess … you should add this flag. If you’re compiling it using CMake (for example, if you’re compiling ORB-SLAM), then you can easily add the flag by changing
CMAKE_BUILD_TYPE to either
RelwithDebInfo. There are two ways to do this:
set(CMAKE_BUILD_TYPE Debug)in your CMakeLists.txt file or
- Run cmake with the flag
And that’s it! See, no long exposé nor tedious instructions!