valgrind 是一个检查内存泄漏的工具。

安装 valgrind

  1. https://sourceware.org/pub/valgrind/valgrind-3.17.0.tar.bz2
  2. tar -jxvf valgrind-3.17.0.tar.bz2
  3. cd valgrind-3.17.0
  4. sudo ./configure --prefix=/usr/local/bin/valgrind
  5. sudo make && sudo make install
  6. sudo vim /etc/profile.d/valgrind.sh # 写入以下内容
  1. export PATH="$PATH:/usr/local/bin/valgrind/bin"
  2. export VALGRIND_LIB="/usr/local/bin/valgrind/libexec/valgrind

使用 valgrind

使用方式:valgrind 可执行文件名
比如:valgrind ./target/debug/adder 正常显示内容

  1. ubuntu 19:54:~/s/adder $ valgrind ./target/debug/adder
  2. ==2378651== Memcheck, a memory error detector
  3. ==2378651== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
  4. ==2378651== Using Valgrind-3.17.0 and LibVEX; rerun with -h for copyright info
  5. ==2378651== Command: ./target/debug/adder
  6. ==2378651==
  7. run Closure
  8. ==2378651==
  9. ==2378651== HEAP SUMMARY:
  10. ==2378651== in use at exit: 0 bytes in 0 blocks
  11. ==2378651== total heap usage: 11 allocs, 11 frees, 3,008 bytes allocated
  12. ==2378651==
  13. ==2378651== All heap blocks were freed -- no leaks are possible
  14. ==2378651==
  15. ==2378651== For lists of detected and suppressed errors, rerun with: -s
  16. ==2378651== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

如果显示以下错误,则需要安装 libc6-dbg:

  1. valgrind: Fatal error at startup: a function redirection
  2. valgrind: which is mandatory for this platform-tool combination
  3. valgrind: cannot be set up. Details of the redirection are:
  4. valgrind:
  5. valgrind: A must-be-redirected function
  6. valgrind: whose name matches the pattern: strlen
  7. valgrind: in an object with soname matching: ld-linux.so.2
  8. valgrind: was not found whilst processing
  9. valgrind: symbols from the object with soname: ld-linux.so.2
  10. valgrind:
  11. valgrind: Possible fixes: (1, short term): install glibc's debuginfo
  12. valgrind: package on this machine. (2, longer term): ask the packagers
  13. valgrind: for your Linux distribution to please in future ship a non-
  14. valgrind: stripped ld.so (or whatever the dynamic linker .so is called)
  15. valgrind: that exports the above-named function using the standard
  16. valgrind: calling conventions for this platform. The package you need
  17. valgrind: to install for fix (1) is called
  18. valgrind:
  19. valgrind: On Debian, Ubuntu: libc6-dbg
  20. valgrind: On SuSE, openSuSE, Fedora, RHEL: glibc-debuginfo
  21. valgrind:
  22. valgrind: Cannot continue -- exiting now. Sorry.

安装 libc6-dbg

sudo apt install libc6-dbg 安装 libc6-dbg 时遇到 libc6 版本不匹配的问题(libc6-dbg 的 2.31-0ubuntu9.3 版本半个月前发布,但是目前没在 apt 源里面):

  1. libc6-dbg : Depends: libc6 (= 2.31-0ubuntu9.2) but 2.31-0ubuntu9.3 is to be installed

所以降级安装 libc6 的 2.31-0ubuntu9.2 版本:

  1. sudo apt --fix-broken # 我的本地已经安装了 libc6-dbg 的 2.31-0ubuntu9.2 版本,所以需要解决与 libc6 2.31-0ubuntu9.3 的冲突
  2. sudo apt install libc6=2.31-0ubuntu9.2 libc6-dbg

网上找了很久都没发现正确的解决办法。。。