本文介绍了如何在python中使用valgrind?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对正在编写的C python扩展进行memcheck,但是我无法设置valgrind与python一起使用.我真的很感谢一些建议.仅出于上下文考虑,这是Ubuntu 13.10,python 2.7.5+和valgrind 3.8.1.

I am trying to memcheck a C python extension I am writing, but I'm having trouble setting up valgrind to work with python. I would really appreciate some advice. Just for context, this is Ubuntu 13.10, python 2.7.5+, and valgrind 3.8.1.

根据 Readme.valgrind 的建议,我执行了以下操作.

As per recommendation from Readme.valgrind I did the following.

1)使用以下命令下载了python源代码

1) Downloaded the python source with

sudo apt-get build-dep python2.7
apt-get source python2.7

2)应用了代码补丁,即取消注释Objects/obmalloc.c中的Py_USING_MEMORY_DEBUGGER".

2) Applied the code patch, i.e. "Uncomment Py_USING_MEMORY_DEBUGGER in Objects/obmalloc.c".

3)应用了抑制补丁,即取消对Misc/valgrind-python.supp中抑制PyObject_Free和PyObject_Realloc警告的行的注释"

3) Applied the suppression patch, i.e. "Uncomment the lines in Misc/valgrind-python.supp that suppress the warnings for PyObject_Free and PyObject_Realloc"

4)使用

./configure --prefix=/home/dejan/workspace/python --without-pymalloc
make -j4 install

请注意,我同时执行了2和3,而README.valgrind则说要执行2或3.

Note that I did both 2 and 3, while README.valgrind says to do 2 or 3... more can't hurt.

现在,让我们在test.py

print "Test"

让我们使用此脚本在python上运行valgrind

Let's run valgrind on python with this script

valgrind --tool=memcheck --leak-check=full --suppressions=python2.7-2.7.5/Misc/valgrind-python.supp bin/python test.py

出乎意料的是,仍然有来自valgrind的大量报告,其中第一个是(随后还有更多)

Unexpectedly, there is still loads of reports from valgrind, with the first one being (and many more following)

==27944== HEAP SUMMARY:
==27944==     in use at exit: 857,932 bytes in 5,144 blocks  
==27944==   total heap usage: 22,766 allocs, 17,622 frees, 4,276,934 bytes allocated
==27944== 
==27944== 38 bytes in 1 blocks are possibly lost in loss record 24 of 1,343
==27944==    at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==27944==    by 0x46B8DD: PyString_FromString (stringobject.c:143)
==27944==    by 0x439631: PyFile_FromFile (fileobject.c:157)
==27944==    by 0x4E9B4A: _PySys_Init (sysmodule.c:1383)
==27944==    by 0x4E29E9: Py_InitializeEx (pythonrun.c:222)
==27944==    by 0x4154B4: Py_Main (main.c:546)
==27944==    by 0x577DDE4: (below main) (libc-start.c:260)

我做错什么了吗?有没有办法让valgrind一个不会泄漏的python脚本并获得干净的valgrind输出?

Am I doing something wrong? Is there a way to valgrind a python script that doesn't leak and get clean valgrind output?

推荐答案

我在此处找到了答案

Python还需要以调试模式进行编译,即

Python also needs to be compiled in debug mode, i.e.

./configure --prefix=/home/dejan/workspace/python --without-pymalloc --with-pydebug --with-valgrind

此外,numpy还有一个禁止文件消除了多余的valgrind警告.

In addition, numpy has a suppresion file that gets rid of the extra valgrind warnings.

这篇关于如何在python中使用valgrind?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 04:09