Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gdb: break in shared library loaded by python

I'm trying to debug c/c++ code located in shared libraries which are loaded by ctypes.cdll.LoadLibrary() in python and then specific functions are called from python. The python code forks child processes, so I need to be able to break whether the c function is called from a python parent or child process. A dead-simple example: test.c

// j = clib.call1(i)
int call1(int i)
{
    return i*2;
}

test.py

import os, sys, ctypes
path = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "test.so"))
clib = ctypes.cdll.LoadLibrary(path)
i = 20
j = clib.call1(i)
print "i=%d j=%d\n" %(i, j)


$ gcc -g -O0 test.c -shared -o test.so
$ gdb --args python-dbg test.py
(gdb) break test.c call1
Function "test.c call1" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (test.c call1) pending.
(gdb) info breakpoints
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   <PENDING>  test.c call1
(gdb) run
Starting program: /usr/bin/python-dbg test.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
i=20 j=40

[23744 refs]
[Inferior 1 (process 44079) exited normally]

You can see from my terminal log that gdb is not seeing the breakpoint when python loads the library. I am seeing the same behavior with my application.

like image 581
lightdee Avatar asked Nov 30 '12 21:11

lightdee


1 Answers

break on call1 instead

(gdb) break call1

this should work too

(gdb) break test.c:call1
like image 79
iabdalkader Avatar answered Sep 28 '22 00:09

iabdalkader