lig*_*dee 7 python ctypes gdb breakpoints shared-libraries
我正在尝试调试位于共享库中的c/c ++代码,这些代码由python中的ctypes.cdll.LoadLibrary()加载,然后从python调用特定的函数.python代码分叉子进程,所以我需要能够破解是否从python父进程或子进程调用c函数.一个简单的例子:test.c
// j = clib.call1(i)
int call1(int i)
{
return i*2;
}
Run Code Online (Sandbox Code Playgroud)
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]
Run Code Online (Sandbox Code Playgroud)
您可以从我的终端日志中看到,当python加载库时,gdb没有看到断点.我看到了与我的应用程序相同的行为.
打破call1
代替
(gdb) break call1
Run Code Online (Sandbox Code Playgroud)
这应该也有效
(gdb) break test.c:call1
Run Code Online (Sandbox Code Playgroud)