10.3. Valgrind¶
This is about how to build Valgrind, a Valgrind friendly version of Python and finally how to use and interpret Valgrind.
Note
These instructions have been tested on Mac OS X 10.9 (Mavericks). They may or may not work on other OS’s
10.3.1. Building Valgrind¶
This should be fairly straightforward:
svn co svn://svn.valgrind.org/valgrind
cd valgrind
./autogen.sh
./configure
make
make install
10.3.2. Building Python for Valgrind¶
Prepare the source by uncommenting Py_USING_MEMORY_DEBUGGER
in Objects/obmalloc.c around line 1082 or so.
10.3.2.1. Configuring¶
configure
takes the following aguments:
Argument |
|
---|---|
|
Installs it in /Library/Frameworks/Python.framework/Versions/ |
|
Debug build of Python. See Misc/SpecialBuilds.txt |
|
With Valgrind support Misc/README.valgrind |
To make a framework install:
./configure --enable-framework --with-pydebug --without-pymalloc --with-valgrind
sudo make frameworkinstall
To make a local version cd to the source tree and we will build a Valgrind version of Python in the valgrind/
directory:
mkdir valgrind
cd valgrind
../configure --with-pydebug --without-pymalloc --with-valgrind
make
10.3.2.1.1. Check debug build¶
$ python3 -X showrefcount
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Python 3.4.3 (default, May 26 2015, 19:54:01)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.51)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 23
23
[54793 refs, 0 blocks]
>>> import sys
[54795 refs, 0 blocks]
>>> sys.gettotalrefcount()
54817
[54795 refs, 0 blocks]
>>> import sysconfig
>>> sysconfig.get_config_var('Py_DEBUG')
1
|
10.3.3. Using Valgrind¶
In the <Python source>/Misc
directory there is a valgrind-python.supp
file that supresses some Valgrind spurious warnings. I find that this needs editing so:
cp <Python source>/Misc/valgrind-python.supp ~/valgrind-python.supp
vi ~/valgrind-python.supp
Uncomment PyObject_Free
and PyObject_Realloc
in the valgrind suppression file.
Invoking the Python interpreter with Valgrind:
valgrind --tool=memcheck --dsymutil=yes --track-origins=yes --show-leak-kinds=all --trace-children=yes --suppressions=$HOME/.valgrind-python.supp <Python source>/valgrind/python.exe -X showrefcount
An example of using Valgrind to detect leaks is here: Finding Where the Leak is With Valgrind.