1、Valgrind的安裝
a、直接使用sudo apt-get install valgrind
b、valgrind官網下載http://valgrind.org/
linux
#解壓 sudo tar xvf valgrind-3.8.1.tar.bz2 -C /home/worspace/ #安裝 ./configure make sudo make install
運行:
$valgrind ls -l
出現如下問題:
shell
==18092== Memcheck, a memory error detector ==18092== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. ==18092== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info ==18092== Command: ls -l ==18092== valgrind: Fatal error at startup: a function redirection valgrind: which is mandatory for this platform-tool combination valgrind: cannot be set up. Details of the redirection are: valgrind: valgrind: A must-be-redirected function valgrind: whose name matches the pattern: index valgrind: in an object with soname matching: ld-linux.so.2 valgrind: was not found whilst processing valgrind: symbols from the object with soname: ld-linux.so.2 valgrind: valgrind: Possible fixes: (1, short term): install glibc's debuginfo valgrind: package on this machine. (2, longer term): ask the packagers valgrind: for your Linux distribution to please in future ship a non- valgrind: stripped ld.so (or whatever the dynamic linker .so is called) valgrind: that exports the above-named function using the standard valgrind: calling conventions for this platform. The package you need valgrind: to install for fix (1) is called valgrind: valgrind: On Debian, Ubuntu: libc6-dbg valgrind: On SuSE, openSuSE, Fedora, RHEL: glibc-debuginfo valgrind: valgrind: Cannot continue -- exiting now. Sorry.
2、解決辦法
仔細閱讀上邊的log信息,
Possible fixes: (1, short term): install glibc's debuginfo
On Debian, Ubuntu: libc6-dbg
ok,執行命令sudo apt-get install libc6-dbg
安裝完成後,問題解決 app
3、使用方法(valgrind --help)
valgrind --leak-check=yes --show-reachable=yes ls -l
如下是截取的結果分析:
this
==11283== LEAK SUMMARY: ==11283== definitely lost: 80 bytes in 2 blocks ==11283== indirectly lost: 240 bytes in 20 blocks ==11283== possibly lost: 0 bytes in 0 blocks ==11283== still reachable: 13,405 bytes in 23 blocks ==11283== suppressed: 0 bytes in 0 blocks運行本身的app:
==11295== HEAP SUMMARY: ==11295== in use at exit: 0 bytes in 0 blocks ==11295== total heap usage: 29 allocs, 29 frees, 70,080,768 bytes allocated能夠看到堆棧heap分配了29次,釋放了29次。沒有reachable的block 4、遺留的問題 上述關於ld-linux.so.2,non stripped ld.so,應該是關於編譯連接的問題,須要好好看看《編譯原理》了。