如何在调试PythonC扩展时设置断点并执行其他gdb命令

如何在调试PythonC扩展时设置断点并执行其他gdb命令,python,c++,debugging,gdb,breakpoints,Python,C++,Debugging,Gdb,Breakpoints,我尝试使用gdb调试PythonC扩展。具体地说,我运行了gdb-exr--args bash python mycode.py。但是,在运行此代码时,我无法控制调试。在gdb内部,python代码被执行并完成。我想设置一个断点并执行一些next、step和print命令。你知道怎么做吗?假设你有一个非常基本的代码。像这样的例子: 设置好所有内容后: > virtualenv venv > source venv/bin/activate > pip install Cyth

我尝试使用gdb调试PythonC扩展。具体地说,我运行了
gdb-exr--args bash python mycode.py
。但是,在运行此代码时,我无法控制调试。在gdb内部,python代码被执行并完成。我想设置一个断点并执行一些
next
step
print
命令。你知道怎么做吗?

假设你有一个非常基本的代码。像这样的例子:

设置好所有内容后:

> virtualenv venv
> source venv/bin/activate
> pip install Cython
> python setup.py build_ext --inplace
> python ./script.py
您将能够像这样运行代码:

> python ./script.py
str: Hello world
它将从本机代码调用您的函数

#include <stdio.h>

void f(char *str) {
      printf("str: %s\n", str);
}
你在那里。您可以查看整个堆栈跟踪。如您所见,顶部是您的
C
函数

(gdb) bt
#0  f (str=0x2aaaaabd9924 "Hello world") at hello.c:5
#1  0x00002aaab22adeed in __pyx_f_12hello_caller_hello_world (__pyx_skip_dispatch=0, __pyx_v_str=<optimized out>) at hello_caller.c:1050
#2  __pyx_pf_12hello_caller_hello_world (__pyx_self=<optimized out>, __pyx_v_str=<optimized out>) at hello_caller.c:1094
#3  __pyx_pw_12hello_caller_1hello_world (__pyx_self=<optimized out>, __pyx_v_str=0x2aaaaabd9900) at hello_caller.c:1078
#4  0x00002aaaaadb3b22 in PyEval_EvalFrameEx () from /lib64/libpython2.7.so.1.0
#5  0x00002aaaaadb5efd in PyEval_EvalCodeEx () from /lib64/libpython2.7.so.1.0
#6  0x00002aaaaadb6002 in PyEval_EvalCode () from /lib64/libpython2.7.so.1.0
#7  0x00002aaaaadcf43f in run_mod () from /lib64/libpython2.7.so.1.0
#8  0x00002aaaaadd05fe in PyRun_FileExFlags () from /lib64/libpython2.7.so.1.0
#9  0x00002aaaaadd1889 in PyRun_SimpleFileExFlags () from /lib64/libpython2.7.so.1.0
#10 0x00002aaaaade2a3f in Py_Main () from /lib64/libpython2.7.so.1.0
#11 0x00002aaaab9e1c05 in __libc_start_main () from /lib64/libc.so.6
#12 0x000000000040071e in _start ()
(gdb)bt
#0 f(str=0x2aaaabd9924“Hello world”)在Hello.c:5
#1 0x00002AAB22在hello_-pyx_f_12hello_-caller_-hello_世界(hello_-pyx_-skip_-dispatch=0,uuu-pyx_v_-str=)的hello_-caller.c:1050
#2 uuPyx pyx pf 12;你好caller uHello world(uuuPyx self=,uuPyx v str=)在你好caller。c:1094
#3 uuPyx_pw_12;hello_1;caller_1;hello_1;world(uuuuPyx_2;self=,uuuPyx_5;v_0str=0x2aaaabd9900)在hello_caller.c:1078
#4 0x00002aaaadb3b22,位于/lib64/libpython2.7.so.1.0中的PyEval_EvalFrameEx()中
#来自/lib64/libpython2.7.so.1.0的PyEval_evalcodex()中的5 0x00002aaaaadb5efd
#6/lib64/libpython2.7.so.1.0中PyEval_EvalCode()中的0x00002aaaaadb6002
#从/lib64/libpython2.7.so.1.0运行_mod()中的7 0x00002aaaaadcf43f
#8/lib64/libpython2.7.so.1.0中PyRun_FileExFlags()中的0x00002aaaadd05fe
#9/lib64/libpython2.7.so.1.0中的PyRun_SimpleFileExFlags()中的0x00002aaaadd1889
#来自/lib64/libpython2.7.so.1.0的Py_Main()中的10 0x00002aaaade2a3f
#11 0x00002AAAB9E1C05,位于/lib64/libc.so.6中的
#12 0x000000000040071e in_启动()
(gdb) bt
#0  f (str=0x2aaaaabd9924 "Hello world") at hello.c:5
#1  0x00002aaab22adeed in __pyx_f_12hello_caller_hello_world (__pyx_skip_dispatch=0, __pyx_v_str=<optimized out>) at hello_caller.c:1050
#2  __pyx_pf_12hello_caller_hello_world (__pyx_self=<optimized out>, __pyx_v_str=<optimized out>) at hello_caller.c:1094
#3  __pyx_pw_12hello_caller_1hello_world (__pyx_self=<optimized out>, __pyx_v_str=0x2aaaaabd9900) at hello_caller.c:1078
#4  0x00002aaaaadb3b22 in PyEval_EvalFrameEx () from /lib64/libpython2.7.so.1.0
#5  0x00002aaaaadb5efd in PyEval_EvalCodeEx () from /lib64/libpython2.7.so.1.0
#6  0x00002aaaaadb6002 in PyEval_EvalCode () from /lib64/libpython2.7.so.1.0
#7  0x00002aaaaadcf43f in run_mod () from /lib64/libpython2.7.so.1.0
#8  0x00002aaaaadd05fe in PyRun_FileExFlags () from /lib64/libpython2.7.so.1.0
#9  0x00002aaaaadd1889 in PyRun_SimpleFileExFlags () from /lib64/libpython2.7.so.1.0
#10 0x00002aaaaade2a3f in Py_Main () from /lib64/libpython2.7.so.1.0
#11 0x00002aaaab9e1c05 in __libc_start_main () from /lib64/libc.so.6
#12 0x000000000040071e in _start ()