本文只讨论使用c++的 ROS程序,使用的主要工具是GDB。这是Linux诸多c/c++ debug工具中最流行的一种。只要你装了gcc(这是大多数Linux系统的默认c/c++编译器),GDB一般都捆绑安装过了。
- 步骤一 Build with debug
catkin_make 时加上一个参数:
catkin_make -DCMAKE_BUILD_TYPE=Debug
如果你想给Release版本Debug,就这样
catkin_make -DCMAKE_BUILD_TYPE=RelWithDebInfo
如果你用Rosbuild,在你的CMakeLists.txt靠近顶部的位置加上这样一句即可:
set(ROS_BUILD_TYPE Debug)
- 步骤二 在GDB里运行程序
如果你用roslaunch,在你的launch文件里,要debug的node那行加上
launch-prefix="xterm -e gdb --args"
其中xterm可以换成任何你喜欢的终端。我比较推荐terminator, 因为方便划分窗口。
用Roslaunch还有几种不同的参数来调用GDB,详见引用3 。
如果你用rosrun,直接在调用的时候加上gdb的参数:
rosrun --prefix 'gdb -ex run --args' [package_name] [node_name]
- 步骤三 开始Debug!
roslaunch或者rosrun会新弹出来一个窗口,一般第一次用会报错,解决方法看下文。
gdb窗口输入指令:
r
从头开始运行程序直到断点。在一次debug中你可以通过用 r 来多次重新运行程序,而不必重新rosrun 或 roslaunch.
接下来可以在百度一篇GDB快速上手指南即可。
- 报错解决
使用gdb调试的ROS节点时,碰到这么一个报错:
process[feature_tracker-2]: started with pid [8459]
RLException: Roslaunch got a ‘No such file or directory’ error while attempting to run:
xterm -e gdb --args /home/matthew/projects/vinsmono/devel/lib/vins_estimator/vins_estimator __name:=vins_estimator __log:=/home/matthew/.ros/log/5eeae726-84a9-11ec-ad3c-378f34e64432/vins_estimator-3.log
Please make sure that all the executables in this command exist and have
executable permission. This is often caused by a bad launch-prefix.
The traceback for the exception was written to the log file
查了一下,原来是没有安装xterm。
解决办法:
sudo apt-get install xterm



