Flags to debug python C extensions.

I often find myself debugging python C extensions from gdb, but usually some variables are hidden because aggressive optimizations that distutils sets by default. What I did not know, is that you can prevent those optimizations by passing flags -O0 -fno-inline to gcc in keyword extra_compile_args (note: this will only work in GCC). A complete example would look like:

config.add_extension('foo',
                         sources=['a.c'],
                         # add this for gdb debug
                         extra_compile_args=['-O0 -fno-inline'])

and your extension becomes much easier to debug from gdb.

5 Responses to “Flags to debug python C extensions.”

  1. Lorenzo
    August 18th, 2010 17:38
    1

    Nice tip!

  2. Matthieu Brucher
    August 19th, 2010 12:16
    2

    You may also want to add ‘-g’ even in testing release builds ;)

  3. ross
    August 19th, 2010 16:59
    3

    -O0 can cause some other issues. -O1 is typically a better choice if the code is moderately or more complex.

  4. Åsmund Hjulstad
    August 24th, 2010 13:34
    4

    What about fortran extensions, is there any way of sending these options (or -pg) to gfortran?

  5. Joyelle
    May 10th, 2011 14:20
    5

    You’re the gretaset! JMHO