安装 valgrind
https://sourceware.org/pub/valgrind/valgrind-3.17.0.tar.bz2
tar -jxvf valgrind-3.17.0.tar.bz2
cd valgrind-3.17.0
sudo ./configure --prefix=/usr/local/bin/valgrind
sudo make && sudo make install
sudo vim /etc/profile.d/valgrind.sh # 写入以下内容
export PATH="$PATH:/usr/local/bin/valgrind/bin"
export VALGRIND_LIB="/usr/local/bin/valgrind/libexec/valgrind
使用 valgrind
使用方式:valgrind 可执行文件名
比如:valgrind ./target/debug/adder
正常显示内容
ubuntu 19:54:~/s/adder $ valgrind ./target/debug/adder
==2378651== Memcheck, a memory error detector
==2378651== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2378651== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
==2378651== Command: ./target/debug/adder
==2378651==
run Closure
==2378651==
==2378651== HEAP SUMMARY:
==2378651== in use at exit: 0 bytes in 0 blocks
==2378651== total heap usage: 11 allocs, 11 frees, 3,008 bytes allocated
==2378651==
==2378651== All heap blocks were freed -- no leaks are possible
==2378651==
==2378651== For lists of detected and suppressed errors, rerun with: -s
==2378651== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
如果显示以下错误,则需要安装 libc6-dbg:
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: strlen
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.
安装 libc6-dbg
sudo apt install libc6-dbg
安装 libc6-dbg 时遇到 libc6 版本不匹配的问题(libc6-dbg 的 2.31-0ubuntu9.3 版本半个月前发布,但是目前没在 apt 源里面):
libc6-dbg : Depends: libc6 (= 2.31-0ubuntu9.2) but 2.31-0ubuntu9.3 is to be installed
所以降级安装 libc6 的 2.31-0ubuntu9.2 版本:
sudo apt --fix-broken # 我的本地已经安装了 libc6-dbg 的 2.31-0ubuntu9.2 版本,所以需要解决与 libc6 2.31-0ubuntu9.3 的冲突
sudo apt install libc6=2.31-0ubuntu9.2 libc6-dbg
网上找了很久都没发现正确的解决办法。。。