使用gdb调试ros程序

问题描述

首先需要明白catkin_make生成的可执行文件在什么地方。一般${catkin_ws}/devel/lib文件夹下。因此,对于直接rosrun,不需要launch文件的节点程序,我们可以按照下面的步骤进行调试:

  1. 运行roscore
  2. 使用gdb打开${catkin_ws}/devel/lib文件夹下的节点程序
  3. 使用gdb指令进行调试

但是对于需要launch文件配置参数的节点程序,采用上述方法就不太方便。不过ROS已经想到了这一点,只需我们在launch文件中设置一个参数,ROS就可自动完成上述三个步骤。

配置catkin_make

使用如下指令编译ros节点:

1
catkin_make -DCMAKE_BUILD_TYPE=Debug

配置launch文件

在launch文件中,根据以下需要,在该节点的<node>标签中添加对应指令:

  • launch-prefix="xterm -e gdb --args" : run your node in a gdb in a separate xterm window, manually type run to start it
  • launch-prefix="gdb -ex run --args" : run your node in gdb in the same xterm as your launch without having to type run to start it
  • launch-prefix="stterm -g 200x60 -e gdb -ex run --args" : run your node in gdb in a new stterm window without having to type run to start it
  • launch-prefix="valgrind" : run your node in valgrind
  • launch-prefix="xterm -e" : run your node in a separate xterm window

比如说,原本的节点写法如下:

1
<node pkg="bdxt" type="bdxt" name="bdxt" output="screen">

现在需要改为:

1
<node pkg="bdxt" type="bdxt" name="bdxt" output="screen" launch-prefix="xterm -e gdb --args">

另外,如果是python脚本,需要修改为pdb,launch-prefix="xterm -e python -m pdb "

使用GDB调试ROS程序

配置好launch文件后,roslaunch运行ros节点,会弹出一个gdb窗口,接下来的操作和普通的cpp程序一致。GDB命令可以参考:

https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/gdb.html

参考文献

  1. https://bluesat.com.au/a-dummys-guide-to-debugging-ros-systems/
  2. [http://wiki.ros.org/roslaunch/Tutorials/Roslaunch%20Nodes%20in%20Valgrind%20or%20GDB](http://wiki.ros.org/roslaunch/Tutorials/Roslaunch Nodes in Valgrind or GDB)