Python: Running Valgrind on a C extension

This is how I use Valgrind to check for memory bugs in pygit2.

Glibc with debug symbols

The first issue I run into is that Valgrind refused to work if Glibc was not compiled with debug symbols.

This is how I did in my Gentoo notebook, I edited the /etc/make.conf file to enable the splitdebug feature:

/etc/make.conf
FEATURES="splitdebug"

Then re-emerged the glibc:

$ sudo emerge glibc

Then commented out the splitdebug feature to avoid emerging other packages with debug symbols (there is likely a better way to do this).

Valgrind 3.7

The big problem is that running Valgrind with a Python C extension raises tons of false positives. There are a number of things you need to do to avoid all these false positives.

The first one is to use latest version of Valgrind 3.7, because of some bug I forgot about present in version 3.6

Python, compiling for a memory debugger

Now, you need to install a version of Python to be used with Valgrind. There is one little change to do regarding an standard Python, edit file Objects/obmalloc.c and define Py_USING_MEMORY_DEBUGGER :

Objects/obmalloc.c
#define Py_USING_MEMORY_DEBUGGER

Now install as usual:

$ ./configure --prefix=~/Python-2.7.3-mem-debug
$ make
$ make install

The suppression file

Now you need to use the suppression file that you will find in the Python sources, at Misc/valgrind-python.supp. This is how I run the unit tests in pygit2 to find issues:

$ valgrind --trace-children=yes --suppressions=valgrind-python.supp \
~/Python-2.7.3-mem-debug/bin/python setup.py test

That’s it.

Leave a Reply