gdb配置调试python3.8

系统: ubuntu20.04

python版本: Python 3.8.10

gdb版本: GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2

安装Python的调试符号信息

参考文档: https://juejin.cn/s/ubuntu%20install%20python-debuginfo

安装Python调试符号信息:

apt install python3-dbg

安装版本:

python3-dbg --version
Python 3.8.10

安装后gdb就可以查看python进程和python的coredump的调用栈信息。

并可以在gdb中使用py-bt等命令。

效果

未配置gdb的python调试时,调用栈信息为: gdb python core.python3.1687680206.11.92525.0.0

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `python3 <python file>'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000005894be in PyUnicode_AsUTF8AndSize ()
[Current thread is 1 (Thread 0x7f61bcdf7700 (LWP 92528))]
(gdb) bt
#0  0x00000000005894be in PyUnicode_AsUTF8AndSize ()
...
#2  0x00007f61bf94c076 in std::function<int (generic::FieldType const&, generic::Variable const&)>::operator()(generic::FieldType const&, generic::Variable const&) const (
    __args#1=..., __args#0=..., this=<optimized out>) at /usr/include/c++/9/bits/std_function.h:683
...
#5  0x00007f61bf94c076 in std::function<int (generic::FieldType const&, generic::Variable const&)>::operator()(generic::FieldType const&, generic::Variable const&) const (
    __args#1=..., __args#0=..., this=<optimized out>) at /usr/include/c++/9/bits/std_function.h:683
...
#8  0x00000000005f652a in PyCFunction_Call ()
#9  0x00000000005f7056 in _PyObject_MakeTpCall ()
#10 0x00000000006a22fe in ?? ()
#11 0x00000000005c5080 in ?? ()
#12 0x00000000005f3c41 in ?? ()
#13 0x00000000005f3f05 in PyObject_CallFunctionObjArgs ()
#14 0x0000000000660c95 in ?? ()
#15 0x0000000000539905 in ?? ()
#16 0x000000000065f8fd in ?? ()
#17 0x0000000000662baf in ?? ()
#18 0x00000000005c51d7 in ?? ()
#19 0x0000000000570ac2 in _PyEval_EvalFrameDefault ()
#20 0x0000000000569cea in _PyEval_EvalCodeWithName ()
#21 0x000000000050b2b0 in ?? ()

配置python-dbg后,调用栈信息:

[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `python3 <python file path>'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000005894be in PyUnicode_AsUTF8AndSize (unicode=0x0, psize=psize@entry=0x7f61bcdf4fd0) at ../Objects/unicodeobject.c:3940
3940    ../Objects/unicodeobject.c: No such file or directory.
[Current thread is 1 (Thread 0x7f61bcdf7700 (LWP 92528))]
(gdb) 
(gdb) 
(gdb) bt
#0  0x00000000005894be in PyUnicode_AsUTF8AndSize (unicode=0x0, psize=psize@entry=0x7f61bcdf4fd0) at ../Objects/unicodeobject.c:3940
...
#2  0x00007f61bf94c076 in std::function<int (generic::FieldType const&, generic::Variable const&)>::operator()(generic::FieldType const&, generic::Variable const&) const (
    __args#1=..., __args#0=..., this=<optimized out>) at /usr/include/c++/9/bits/std_function.h:683
...
#5  0x00007f61bf94c076 in std::function<int (generic::FieldType const&, generic::Variable const&)>::operator()(generic::FieldType const&, generic::Variable const&) const (
    __args#1=..., __args#0=..., this=<optimized out>) at /usr/include/c++/9/bits/std_function.h:683
...
#8  0x00000000005f652a in cfunction_call_varargs (kwargs=<optimized out>, args=<optimized out>, 
    func=<built-in method __getstate__ of <Your Python Class> object at remote 0x7f61bc399960>) at ../Include/internal/pycore_pyerrors.h:13
#9  PyCFunction_Call (func=<built-in method __getstate__ of <Your Python Class> object at remote 0x7f61bc399960>, args=<optimized out>, 
    kwargs=<optimized out>) at ../Objects/call.c:773
#10 0x00000000005f7056 in _PyObject_MakeTpCall (callable=<built-in method __getstate__ of <Your Python Class> object at remote 0x7f61bc399960>, 
    args=<optimized out>, nargs=<optimized out>, keywords=<optimized out>) at ../Include/internal/pycore_pyerrors.h:13
#11 0x00000000006a22fe in _PyObject_CallNoArg (func=<optimized out>) at ../Include/cpython/abstract.h:153
#12 _PyObject_GetState (required=1, obj=<PythonType at remote 0x7f61bc399960>) at ../Objects/typeobject.c:4304
#13 reduce_newobj (obj=<<Your Python Class> at remote 0x7f61bc399960>) at ../Objects/typeobject.c:4533
#14 _common_reduce (proto=<optimized out>, self=<PythonType at remote 0x7f61bc399960>) at ../Objects/typeobject.c:4577
#15 object___reduce_ex___impl (protocol=<optimized out>, self=<<Your Python Class> at remote 0x7f61bc399960>) at ../Objects/typeobject.c:4648
#16 object___reduce_ex__ (self=<PythonType at remote 0x7f61bc399960>, arg=<optimized out>) at ../Objects/clinic/typeobject.c.h:178
#17 0x00000000005c5080 in cfunction_vectorcall_O (func=<built-in method __reduce_ex__ of <Your Python Class> object at remote 0x7f61bc399960>, 
    args=<optimized out>, nargsf=<optimized out>, kwnames=<optimized out>) at ../Objects/methodobject.c:482
#18 0x00000000005f3c41 in _PyObject_Vectorcall (kwnames=0x0, nargsf=1, args=0x7f61bcdf5250, 
    callable=<built-in method __reduce_ex__ of PythonType object at remote 0x7f61bc399960>) at ../Include/cpython/abstract.h:127
#19 _PyObject_FastCall (nargs=1, args=0x7f61bcdf5250, func=<built-in method __reduce_ex__ of <Your Python Class> object at remote 0x7f61bc399960>)
    at ../Include/cpython/abstract.h:147
#20 object_vacall (base=<optimized out>, callable=<built-in method __reduce_ex__ of PythonType object at remote 0x7f61bc399960>, 
    vargs=<optimized out>) at ../Objects/call.c:1187
#21 0x00000000005f3f05 in PyObject_CallFunctionObjArgs (callable=<optimized out>) at ../Objects/call.c:1260
#22 0x0000000000660c95 in _Pickle_FastCall (func=<optimized out>, obj=4) at ../Modules/_pickle.c:362
#23 0x0000000000539905 in save (self=0x7f61b41ab040, obj=<PythonType at remote 0x7f61bc399960>, pers_save=<optimized out>)
    at ../Modules/_pickle.c:4435
#24 0x000000000065f8fd in dump (self=0x7f61b41ab040, obj=<PythonType at remote 0x7f61bc399960>) at ../Modules/_pickle.c:4519
#25 0x0000000000662baf in _pickle_dumps_impl (module=<optimized out>, buffer_callback=None, fix_imports=1, protocol=4, 
    obj=<PythonType at remote 0x7f61bc399960>) at ../Modules/_pickle.c:7800
#26 _pickle_dumps (module=<optimized out>, args=0x7f61bc3235c8, nargs=<optimized out>, kwnames=<optimized out>) at ../Modules/clinic/_pickle.c.h:619
#27 0x00000000005c51d7 in cfunction_vectorcall_FASTCALL_KEYWORDS (func=<built-in method dumps of module object at remote 0x7f61be3fd040>, args=<optimized out>, 
    nargsf=<optimized out>, kwnames=<optimized out>) at ../Objects/methodobject.c:437
#28 0x0000000000570ac2 in _PyObject_Vectorcall (kwnames=0x0, nargsf=<optimized out>, args=0x7f61bc3235c8, 
    callable=<built-in method dumps of module object at remote 0x7f61be3fd040>) at ../Include/cpython/abstract.h:127
#29 call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, tstate=0x2477eb0) at ../Python/ceval.c:4963

还可以看到python程序的调用栈: py-bt

(gdb) py-bt
Traceback (most recent call first):
  <built-in method __getstate__ of <Your Python Class> object at remote 0x7f61bc399960>
  <built-in method __reduce_ex__ of <Your Python Class> object at remote 0x7f61bc399960>
  <built-in method dumps of module object at remote 0x7f61be3fd040>
  File "/usr/local/lib/python3.8/dist-packages/zmq/sugar/socket.py", line 946, in send_pyobj
    msg = pickle.dumps(obj, protocol)
  File "<your python file path>", line 19, in <your method>
    self._sock.send_pyobj(data)
  File "<your python file path>", line 72, in <your method>
    ...

遇到问题

直接使用 gdb/libpython.py报错: unable to read python frame information

不安装python3-dbg

直接使用: https://github.com/python/cpython/blob/v3.8.10/Tools/gdb/libpython.py

操作:

gdb python3 core.python3.1687680206.11.92525.0.0 

(gdb) py-bt
Undefined command: "py-bt".  Try "help".

(gdb) source libpython.py 
(gdb) py
py-bt               py-down             py-locals           py-up               python-interactive  
py-bt-full          py-list             py-print            python        
(gdb) py-bt
Traceback (most recent call first):
  (unable to read python frame information)
  (unable to read python frame information)
  (unable to read python frame information)
  (unable to read python frame information)
  (unable to read python frame information)
  (unable to read python frame information)
  (unable to read python frame information)
  (unable to read python frame information)
  (unable to read python frame information)
  (unable to read python frame information)
  (unable to read python frame information)
  (unable to read python frame information)
  (unable to read python frame information)

说明:

  • source libpython.py后gdb就会有py-bt命令。