Skip to article frontmatterSkip to article content

Debugging C++ with GDB

Authors: Tiago P. Peixoto1
Affiliations: 1Inverse Complexity Lab
License: CC-BY

GDB stands for GNU Debugger, and it provides a more robust debugging infrastructure than using print statements in your code.

A simple introduction to using GDB can be read here. Please take a look there.

To debug some C++ code that is called from python, you should call gdb with the python interpreter:

$ gdb `which python`
GNU gdb (GDB) 16.3
Copyright (C) 2024 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /sbin/python...
Downloading separate debug info for /usr/bin/python3.13
Reading symbols from /home/count0/.cache/debuginfod_client/76195980c4ccf58a63406c313d47a475eabc0bbe/debuginfo...
(gdb)

You can then load your script my_script.py with:

(gdb) set args ./my_script.py
(gdb) run