【发布时间】:2012-11-19 03:51:30
【问题描述】:
我正在尝试调试位于共享库中的 c/c++ 代码,这些代码由 python 中的 ctypes.cdll.LoadLibrary() 加载,然后从 python 调用特定函数。 python代码分叉子进程,所以我需要能够打破c函数是从python父进程还是子进程调用的。 一个非常简单的例子: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]
您可以从我的终端日志中看到,当 python 加载库时,gdb 没有看到断点。我的应用程序出现了相同的行为。
【问题讨论】:
-
更新:进一步挖掘,我注意到“(gdb) break test.c:6”确实有效。为什么一个有效而另一个无效?
标签: python gdb shared-libraries ctypes breakpoints