0

I'm pretty familiar with writing C code and I'm comfortable in writing python code. I'm trying to learn how to write modules in C that can be called from Python-3.9.X on OSX 10.15.7. I've gotten a couple 'hello world' type of examples to work, but for complex examples I'm struggling to figure out how I would debug the C-extensions that I write.

MWE:

src/add.c

// The C function that actually does the work
static PyObject * add_c_func(PyObject *self, PyObject *args)
{
    int a=0;
    int b=0;
    int c=0;

    // This determines the number arguments used by add_c_func
    if (!PyArg_ParseTuple(args, "iii", &a, &b, &c))
    {
        return NULL;
    }

    printf("%i\n", a+b+c);

    Py_RETURN_NONE;
}

// This defines the function used by
static PyMethodDef AddMethods[] = {
    {"add_py_func", add_c_func, METH_VARARGS, "Add three numbers."},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef addpymod =
{
    PyModuleDef_HEAD_INIT,
    "addpymod",     /* name of module */
    "",          /* module documentation, may be NULL */
    -1,          /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
    AddMethods
};

PyMODINIT_FUNC PyInit_addpymod(void)
{
    return PyModule_Create(&addpymod);
}

setup.py :

from setuptools import setup, Extension
setup(
    name='addpymod',
    version='1.0',
    description='Python Package with Hello World C Extension',
    ext_modules=[
        Extension(
            'addpymod',
            sources=['src/add.c'],
            py_limited_api=True)
    ],
)

Compiling / installing (by default it uses clang):

python setup.py install

Trying to debug :

(py-ext-test) local: understand-python-c-ext $ gdb
GNU gdb (GDB) 10.1
.
.
.
(gdb) b add.c : 20
No symbol table is loaded.  Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (add.c : 20) pending.
(gdb) python
>import addpymod
>addpymod.add_py_func(10,10,10)   # Why didn't my breakpoint get hit?
>Quit
# This clearly failed, I'm not even sure where my stdout is

There are multiple levels of complexity here and I'm sure that I'm being tripped by more than one.

Question :

  1. How do I debug my add.c using gdb (preferred) or possibly lldb since it was compiled by default with clang's -g option?
irritable_phd_syndrome
  • 4,631
  • 3
  • 32
  • 60
  • You misunderstand what the `python` command inside gdb means. If you want to debug python, you need tell gdb that it should debug the python executable, and then run it, not use the `python` command of gdb itself. – ssbssa Apr 14 '22 at 09:47
  • That is a fine point. I included that section to show that I had at least tried something. I'd also tried putting the commands in a file and then running it via `gdb somefile.py`, which did not work. It is still unclear how I can debug my C-extension. – irritable_phd_syndrome Apr 15 '22 at 03:32
  • As I said, you have to debug the python executable. Try `gdb --args python somefile.py`. – ssbssa Apr 15 '22 at 05:36

0 Answers0