HiroIshida
HiroIshida

Reputation: 1603

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. Specifically by following this, I run 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. I want to set a breakpoint and execute some next, step and print commands inside. Do you know how to do this?

Upvotes: 1

Views: 968

Answers (1)

Oo.oO
Oo.oO

Reputation: 13435

Let's say you have a very basic code. Sample like this one:

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

#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.

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

Upvotes: 2

Related Questions