我尝试使用gdb调试python C扩展。 特别是按照这个步骤,我运行gdb-ex r-args bash python mycode.py
。 然而,在运行此代码时,我无法控制调试。 在gdb内部,执行并完成python代码。 我想设置一个断点,并在里面执行一些next
,step
和print
命令。 你知道怎么做吗?
假设你有一个非常基本的代码。 示例如下:
https://github.com/mkowsiak/cython_hello_world
设置好所有内容后:
> 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);
}
你现在要做的就是打电话:
> gdb -ex r --args python script.py
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7
Copyright (C) 2013 Free Software Foundation, Inc.
...
...
(gdb) break f
Breakpoint 1 at 0x2aaab22ade80: file /usr/include/bits/stdio2.h, line 104.
(gdb) run
Starting program: .../cython_hello_world/venv/bin/python script.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Breakpoint 1, f (str=0x2aaaaabd9924 "Hello world") at hello.c:5
5 printf("str: %s\n", str);
(gdb)
而你就在那里。 您可以查看整个堆栈跟踪。 如您所见,位于顶部的是您的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 ()