pythonc++gdbddd-debuggergdb-python

How to start GDB for C++ called from Python?


I need to debug a C++ function that is called from Python code.

How to start GDB (or better DDD) in such a way that it debugs the C++ code called from a given Python command line?

The given Python command line is:

python3 -m e2e.Tests.Libs.HundredEightyOneTest

It calls a C++ code that I need to debug.


Solution

  • My recommendation: recompile your python interpreter from its source code (so it gets compiled with DWARF debug information, practically speaking with GCC invoked as gcc -Wall -O -g).

    Once you get such a python3 interpreter (with DWARF debug info), perhaps in /usr/local/bin/python3, read the documentation of Python, the documentation of GDB and run

    gdb --args /usr/local/bin/python3 -m e2e.Tests.Libs.HundredEightyOneTest
    

    Of course you have compiled your C++ code embedded by Python with e.g. g++ -Wall -Wextra -g and probably -fPIC and your C++ functions might sometimes need extern "C". See C++ dlopen mini howto since Python usually uses dlopen(3).

    Further guidance might be available on LinuxFromScratch.

    Regarding usage of DDD read its documentation. It is running gdb.

    You may want to run gdb from GNU emacs, or with its --tui option.

    You could want to recompile a recent GDB from its source code, since it is free software, to take advantage of recent features. And likewise even for GCC (for the same reasons).

    You could glance inside the source code of your Python interpreter, since it is open source.