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

[亡魂溺海] 提交于 2020-06-29 04:00:14

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/62325655/how-to-start-gdb-for-c-called-from-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!