{"id":1766,"date":"2012-12-23T08:50:54","date_gmt":"2012-12-23T08:50:54","guid":{"rendered":"https:\/\/lineo.es\/python-valgrind-extension-c\/"},"modified":"2026-04-01T07:17:16","modified_gmt":"2026-04-01T07:17:16","slug":"python-valgrind","status":"publish","type":"post","link":"https:\/\/lineo.es\/en\/python-valgrind\/","title":{"rendered":"Python: Running Valgrind on a C extension"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"1766\" class=\"elementor elementor-1766\" data-elementor-post-type=\"post\">\n\t\t\t\t<div class=\"elementor-element elementor-element-5c40420 e-flex e-con-boxed e-con e-parent\" data-id=\"5c40420\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t<div class=\"elementor-element elementor-element-521f0d2 e-con-full e-flex e-con e-child\" data-id=\"521f0d2\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-b30a4fb elementor-widget elementor-widget-theme-post-title elementor-page-title elementor-widget-heading\" data-id=\"b30a4fb\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"theme-post-title.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h3 class=\"elementor-heading-title elementor-size-default\">Python: Running Valgrind on a C extension<\/h3>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-3e31f46 e-con-full e-flex e-con e-child\" data-id=\"3e31f46\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t<div class=\"elementor-element elementor-element-722e48c elementor-widget elementor-widget-text-editor\" data-id=\"722e48c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>This is how I use\u00a0<a href=\"http:\/\/valgrind.org\/\">Valgrind<\/a>\u00a0to check for memory bugs in\u00a0<a href=\"https:\/\/github.com\/libgit2\/pygit2\">pygit2<\/a>.<\/p><h4>Glibc with debug symbols<\/h4><p>The first issue I run into is that Valgrind refused to work if\u00a0<a href=\"http:\/\/www.gnu.org\/software\/libc\/\">Glibc<\/a>\u00a0was not compiled with debug symbols.<\/p><p>This is how I did in my\u00a0<a href=\"http:\/\/www.gentoo.org\/\">Gentoo<\/a>\u00a0notebook, I edited the \/etc\/portage\/make.conf file to enable the\u00a0<i>splitdebug<\/i>\u00a0feature:<\/p><pre style=\"padding-left: 40px;\">\/etc\/portage\/make.conf\nFEATURES=\"${FEATURES} splitdebug compressdebug -nostrip\"<\/pre><p>Then re-emerged the glibc:<\/p><pre style=\"padding-left: 40px;\">$ sudo emerge glibc<\/pre><p>Then commented out the splitdebug feature to avoid emerging other packages with debug symbols (there is likely a better way to do this).<\/p><h4>Valgrind 3.7<\/h4><p>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.<\/p><p>The first one is to use latest version of Valgrind 3.7, because of some bug I forgot about present in version 3.6<\/p><h4>Python, compiling for a memory debugger<\/h4><p>Now, you need to install a version of Python to be used with Valgrind:<\/p><pre style=\"padding-left: 40px;\">$ .\/configure --prefix=\/...\/Python-3.7.4-valgrind <code><span class=\"pun\">--<\/span><span class=\"pln\">without<\/span><span class=\"pun\">-<\/span><span class=\"pln\">pymalloc <\/span><span class=\"pun\">--<\/span><span class=\"kwd\">with<\/span><span class=\"pun\">-<\/span><span class=\"pln\">pydebug <\/span><span class=\"pun\">--<\/span><span class=\"kwd\">with<\/span><span class=\"pun\">-<\/span><span class=\"pln\">valgrind<\/span><\/code> $ make $ make install<\/pre><h4>Install pytest and pygit2<\/h4><pre style=\"padding-left: 40px;\">$ curl https:\/\/bootstrap.pypa.io\/get-pip.py -o get-pip.py\n$ \/...\/Python-3.7.4-valgrind\/bin\/python3 get-pip.py\n$ \/...\/Python-3.7.4-valgrind\/bin\/pip install pytest\n[...]\n$ \/...\/Python-3.7.4-valgrind\/bin\/python3 setup.py install<\/pre><h4>The suppression file<\/h4><p>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:<\/p><pre style=\"padding-left: 40px;\">$ valgrind --trace-children=yes --suppressions=valgrind-python.supp \\\n    \/...\/Python-3.7.4-valgrind\/bin\/pytest<\/pre><p>That\u2019s it.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>This is how I use\u00a0Valgrind\u00a0to check for memory bugs in\u00a0pygit2. Glibc with debug symbols The first issue I run into is that Valgrind refused to work if\u00a0Glibc\u00a0was not compiled with debug symbols. This is how I did in my\u00a0Gentoo\u00a0notebook, I edited the \/etc\/portage\/make.conf file to enable the\u00a0splitdebug\u00a0feature: \/etc\/portage\/make.conf FEATURES=&#8221;${FEATURES} splitdebug compressdebug -nostrip&#8221; Then re-emerged the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"elementor_header_footer","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1766","post","type-post","status-publish","format-standard","hentry","category-sin-categoria"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/lineo.es\/en\/wp-json\/wp\/v2\/posts\/1766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lineo.es\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lineo.es\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lineo.es\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lineo.es\/en\/wp-json\/wp\/v2\/comments?post=1766"}],"version-history":[{"count":5,"href":"https:\/\/lineo.es\/en\/wp-json\/wp\/v2\/posts\/1766\/revisions"}],"predecessor-version":[{"id":2141,"href":"https:\/\/lineo.es\/en\/wp-json\/wp\/v2\/posts\/1766\/revisions\/2141"}],"wp:attachment":[{"href":"https:\/\/lineo.es\/en\/wp-json\/wp\/v2\/media?parent=1766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lineo.es\/en\/wp-json\/wp\/v2\/categories?post=1766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lineo.es\/en\/wp-json\/wp\/v2\/tags?post=1766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}