简体   繁体   English

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

[英]how to set a breakpoint and execute other gdb commands while debugging python C extension

I tried to debug python C extension by using gdb.我尝试使用 gdb 来调试 python C 扩展。 Specifically by following this , I run gdb -ex r --args bash python mycode.py .具体来说,按照这个,我运行gdb -ex r --args bash python mycode.py However while running this code, I have no control for debug.但是,在运行此代码时,我无法控制调试。 Inside gdb, the python code is executed and finished.在 gdb 内部,执行并完成了 python 代码。 I want to set a breakpoint and execute some next , step and print commands inside.我想设置一个断点并在里面执行一些nextstepprint命令。 Do you know how to do this?你知道怎么做吗?

Let's say you have a very basic code.假设您有一个非常基本的代码。 Sample like this one:像这样的样本:

https://github.com/mkowsiak/cython_hello_world https://github.com/mkowsiak/cython_hello_world

Once you set up everything:设置完所有内容后:

> virtualenv venv
> source venv/bin/activate
> pip install Cython
> python setup.py build_ext --inplace
> python ./script.py

you will be able to run the code like this:您将能够像这样运行代码:

> python ./script.py
str: Hello world

and it will call your function from the native code它会从本机代码调用你的 function

#include <stdio.h>

void f(char *str) {
      printf("str: %s\n", str);
}

All you have to do now is to call:您现在所要做的就是致电:

> 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)

and you are there.你在那里。 You can take a look at the whole stack trace.您可以查看整个堆栈跟踪。 As you can see, at the top is yours C function.如您所见,顶部是您的C function。

(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 ()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM