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