安装 valgrind
https://sourceware.org/pub/valgrind/valgrind-3.17.0.tar.bz2tar -jxvf valgrind-3.17.0.tar.bz2cd valgrind-3.17.0sudo ./configure --prefix=/usr/local/bin/valgrindsudo make && sudo make installsudo 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 redirectionvalgrind: which is mandatory for this platform-tool combinationvalgrind: cannot be set up. Details of the redirection are:valgrind:valgrind: A must-be-redirected functionvalgrind: whose name matches the pattern: strlenvalgrind: in an object with soname matching: ld-linux.so.2valgrind: was not found whilst processingvalgrind: symbols from the object with soname: ld-linux.so.2valgrind:valgrind: Possible fixes: (1, short term): install glibc's debuginfovalgrind: package on this machine. (2, longer term): ask the packagersvalgrind: 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 standardvalgrind: calling conventions for this platform. The package you needvalgrind: to install for fix (1) is calledvalgrind:valgrind: On Debian, Ubuntu: libc6-dbgvalgrind: On SuSE, openSuSE, Fedora, RHEL: glibc-debuginfovalgrind: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
网上找了很久都没发现正确的解决办法。。。
