Gtest test fixture debug with gdb

Since ros2 uses gtest framework for the unit tests, but how do you debug your internal code of the test fixture ?

for example
$ gdb test_node__rmw_fastrtps_cpp

and when I list the code, it can merely show the gtest trigger code but nothing about the real test content, this results in no breakpoint which you can set in your test content. as follows:

Reading symbols from test_node__rmw_fastrtps_cpp...done.
(gdb) l
21	// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22	// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23	// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24	// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25	// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26	// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27	// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28	// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29	
30	#include <stdio.h>
(gdb) l
31	
32	#include "gtest/gtest.h"
33	
34	GTEST_API_ int main(int argc, char **argv) {
35	  printf("Running main() from gtest_main.cc\n");
36	  testing::InitGoogleTest(&argc, argv);
37	  return RUN_ALL_TESTS();
38	}

and the test_node__rmw_fastrtps_cpp actually contains the following 4 test fixtures

(gdb) run --gtest_list_tests
 Starting program: /home/ethan/Myspace/sandbox/ros2_overlay/build/rcl/test/test_node__rmw_fastrtps_cpp --gtest_list_tests
 [Thread debugging using libthread_db enabled]
 Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
 Running main() from gtest_main.cc
 TestNodeFixture__rmw_fastrtps_cpp.
      test_rcl_node_accessors
      test_rcl_node_life_cycle
      test_rcl_node_name_restrictions
      test_rcl_node_namespace_restrictions
 [Inferior 1 (process 12853) exited normally]

So how to set breakpoint to debug or track the code for the test fixture (e.g test_rcl_node_life_cycle above) ?

1 Like

I found the answer and it’s really easy to achieve it by set break points directly with the format b test_file: line (omg :star_struck:). anyway, just mark it here and hopefully it benefits those not very familiar with google test debugging in ROS2, thank you !