#include <Python.h>
static PyObject *
vloop_f1(PyObject *self, PyObject *args)
{
char *st1;
int i=0;
if (!PyArg_ParseTuple(args, "s", &st1))
return NULL;
while(*(st1++))
i++;
return Py_BuildValue("l", i);
}
static PyMethodDef FooMethods[] = {
{"vloop_f1", vloop_f1, METH_VARARGS, "help 1"},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initvloop()
{
Py_InitModule("vloop", FooMethods);
}
建立模块vloop,vloop_f1为一个计算字符串长度的函数。
gcc vloop.c -shared -I /usr/include/python2.7 -fPIC -g -o vloop.so
编译模块,加-g参数以供gdb调试。
gdb python进入gdb
(gdb) b vloop.c:1对模块打断点
(gdb) run -c "import vloop;vloop.vloop_f1('123456')"进行调试
(gdb) b vloop.c:1
No symbol table is loaded. Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (vloop.c:1) pending.
(gdb) run -c "import vloop;vloop.vloop_f1('123456')"
Starting program: /usr/bin/python -c "import vloop;vloop.vloop_f1('123456')"
(gdb) n
8 if (!PyArg_ParseTuple(args, "s", &st1))
(gdb) n
10 while(*(st1++))
(gdb) p st1
$1 = 0x7ffff7e923e4 "123456"
12 return Py_BuildValue("l", i);
(gdb)
13 }
(gdb) p i
$3 = 6