本文主要是介绍valgrind-内存泄漏定位工具,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、前言
- valgirnd 是一套开放源代码的动态调试工具集合。能够检测内存管理错误,线程BUG等。valgirnd是由内核以及基于内核的其他调试工具组成。内核类似于一个框架,它模拟了一个cpu的环境,并提供服务给其他工具使用。而其他工具则类似于插件,利用内核提供的服务完成各种特定的内存调试任务。
2、介绍
valgirnd是以下工具的集合:
- 1、memcheck:这是valgrind应用最广泛的工具,一个重量级的内存检车器,能够发现开发中绝大多数内存错误使用的情况。比如:使用未初始化的内存,使用已经释放了的内存,内存访问越界等。
- 2、callgrind:主要用来检查程序中函数调用过程中出现的问题。
- 3、cachegrind:主要用来检查程序中缓存使用出现的问题。
- 4、helgrind:主要用来检查多线程中出现的竞争的问题。
- 5、massif:主要用来检车程序中堆栈使用过程出现的问题。
- 6、externsion:可以利用core提供的功能,自己编写特定的内存调试工具。
3、编译
- 源码下载:valgirnd官网下载连接
- 解压后进行valrind目录下面,执行
./autogen.sh
- 创建builsh.sh脚本:
./configure \
prefix=$(pwd)/output \
--host=armv7-linux \
CFLAGS="-fPIC -mfloat-abi=hard -mfpu=vfp" \
CC=/home/leo/sv82x/toolchain/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc \
CXX=/home/leo/sv82x/toolchain/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++ \
- 编译和安装:
make;make install
4、 运行
- 在板载的终端输入测试命令,查看valgirnd是否能正常运行,如果出现一堆解释性文字,则表示成功:
./valgrind --help
- 通过以下命令进行执行的内存泄漏检测:
./valgrind --error-limit-no --leak-check=full --tool=memcheck ./test.bin
5、遇到的问题分析
5.1 、 交叉编译期间的问题
- 在配置 configure 时遇到类似以下问题,解决方案请参考buil.sh脚本中的–host参数设置
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for arm-ca9-linux-gnueabihf-strip... no
checking for strip... strip
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking whether ln -s works... yes
checking for arm-ca9-linux-gnueabihf-gcc... /opt/arm-ca9-linux-gnueabihf-6.5/bin/arm-ca9-linux-gnueabihf-gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... yes
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether /opt/arm-ca9-linux-gnueabihf-6.5/bin/arm-ca9-linux-gnueabihf-gcc accepts -g... yes
checking for /opt/arm-ca9-linux-gnueabihf-6.5/bin/arm-ca9-linux-gnueabihf-gcc option to accept ISO C89... none needed
checking whether /opt/arm-ca9-linux-gnueabihf-6.5/bin/arm-ca9-linux-gnueabihf-gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of /opt/arm-ca9-linux-gnueabihf-6.5/bin/arm-ca9-linux-gnueabihf-gcc... gcc3
checking how to run the C preprocessor... /opt/arm-ca9-linux-gnueabihf-6.5/bin/arm-ca9-linux-gnueabihf-cpp
checking whether we are using the GNU C++ compiler... yes
checking whether /opt/arm-ca9-linux-gnueabihf-6.5/bin/arm-ca9-linux-gnueabihf-g++ accepts -g... yes
checking dependency style of /opt/arm-ca9-linux-gnueabihf-6.5/bin/arm-ca9-linux-gnueabihf-g++... gcc3
checking for arm-ca9-linux-gnueabihf-ranlib... no
checking for ranlib... ranlib
configure: WARNING: using cross tools not prefixed with host triplet
checking for a sed that does not truncate output... /bin/sed
checking for ar... /usr/bin/ar
checking for perl... /usr/bin/perl
checking for gdb... /usr/bin/gdb
checking dependency style of /opt/arm-ca9-linux-gnueabihf-6.5/bin/arm-ca9-linux-gnueabihf-gcc... gcc3
checking for diff -u... yes
checking for a supported version of gcc... ok (6.5.0)
checking build system type... x86_64-unknown-linux-gnu
checking host system type... arm-ca9-linux-gnueabihf
checking for a supported CPU... no (arm)
configure: error: Unsupported host architecture. Sorry
- 配置configure 时遇到类似以下问题,原因是交叉工具链没有找到,请排查工具链的路径
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for arm-ca9-linux-gnueabihf-strip... no
checking for strip... strip
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking whether ln -s works... yes
checking for arm-ca9-linux-gnueabihf-gcc... arm-ca9-linux-gnueabihf-gcc
checking whether the C compiler works... no
configure: error: in `/home/const/workspace/Download/valgrind-3.12.0':
configure: error: C compiler cannot create executables
See `config.log' for more details
5.2、使用期间的问题
- 测试valgrind或者使用时,出现以下问题,其原因是没有找到valgrind的lib库文件,同时使用
LD_LIBRARY_PATH
环境变量也是没有用的。
valgrind: failed to start tool 'memcheck' for platform 'arm-linux': No such file or directory
解决办法:在板载终端上设置valgrind的环境变量:
export VALGRIND_LIB=/tmp/nfs/valgrind/lib/libexec/valgrind
#这个根据交叉编译后安装的实际路径填写
- 使用时出现
ld-linux-armhf.so.3
的错误,原因是板载的ld-2.29.so
是被stripped后的动态库文件,而valgrind需要的却是debug版本的动态库,需要注意的是ld-linux-armhf.so.3
是ld-2.29.so
的软链接。
# /mnt/valgrind/bin/valgrind --error-limit=no --leak-check=full --tool=memcheck /usr/local/bin/sample
==701== Memcheck, a memory error detector
==701== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==701== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==701== Command: /usr/local/bin/sample
==701== 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: strcmp
valgrind: in an object with soname matching: ld-linux-armhf.so.3
valgrind: was not found whilst processing
valgrind: symbols from the object with soname: ld-linux-armhf.so.3
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: Note that if you are debugging a 32 bit process on a
valgrind: 64 bit system, you will need a corresponding 32 bit debuginfo
valgrind: package (e.g. libc6-dbg:i386).
valgrind:
valgrind: Cannot continue -- exiting now. Sorry.
网上很多说用not stripped的ld-2.29.so去替换目标板上的ld-2.29.so,解决办法相对来说非常的繁琐而且废时间,一般嵌入式linux系统,文件系统这块基本都是只读文件系统,而且只是用于调试,没必要将文件系统重新烧来烧去,并且还需要重新交叉编译ld库。所以需要解决就是在调用ld-2.29.so的时候,需要使用not stripped的动态库,而不是stripped的库。经过一番搜索资料后通过以下的命令可以正确调用not stripped的动态库。
/tmp/nfs/sv82x/apps/sat_eeasytech/valgrind --tool=memcheck --leak-check=full --main-stacksize=16777216 /tmp/nfs/sv82x/toolchain/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/lib/ld-linux-armhf.so.3 /tmp/nfs/sv82x/apps/sat_eeasytech/EEASYTECH_SAT.BIN
在使用过程中,按照实际的路径配置即可。
这篇关于valgrind-内存泄漏定位工具的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!