Skip to main content
 首页 » 编程设计

python之当我使用调试版本的 Python 时如何使用 gdb

2024年11月01日10cmt

我的 gdb 版本链接到我的系统 python,但我目前正在使用一个特殊的 python 调试版本。因此,gdb 无法正确启动,出现如下错误:

$ gdb 
gdb: Symbol `_Py_ZeroStruct' has different size in shared object, consider re-linking 
gdb: Symbol `PyBool_Type' has different size in shared object, consider re-linking 
gdb: Symbol `_Py_NotImplementedStruct' has different size in shared object, consider re-linking 
gdb: Symbol `PyFloat_Type' has different size in shared object, consider re-linking 
gdb: Symbol `_Py_TrueStruct' has different size in shared object, consider re-linking 
gdb: Symbol `_Py_NoneStruct' has different size in shared object, consider re-linking 
Segmentation fault 

...或者可能是这样的错误:

gdb: symbol lookup error: gdb: undefined symbol: PyUnicodeUCS4_FromEncodedObject 

即使 LD_LIBRARY_PATH 上存在非系统版本的 Python,我如何使用 gdb?

请您参考如下方法:

我在 rootpy Documentation 中找到了答案:

The way around this is to preload the correct library by setting LD_PRELOAD, and then unsetting it before your program is executed. For example, this will debug my-program-to-debug:

LD_PRELOAD=/usr/lib/libpython2.7.so gdb -ex 'set environ LD_PRELOAD' --args my-program-to-debug 

Note that you need to set LD_PRELOAD to the right version of python that gdb was compiled against, which you can find with ldd $(which gdb) from a fresh environment.