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
在使用過程中,按照實際的路徑配置即可。