Valgrind 3.11.0編譯安裝
Valgrind是一款用於內存調試、內存泄漏檢測以及性能分析的軟件開發工具。
Valgrind遵守GNU通用公共許可證條款,是一款自由軟件。
到3.3.0版本為止,Valgrind支持x86、x86-64以及PowerPC上的Linux。除此之外,還有一些其它非正式支持的類Unix平台(如FreeBSD、NetBSD以及Mac OS X)。
1、下載Valgrind 3.11.0
直接下載源碼包
wget http://valgrind.org/downloads/valgrind-3.11.0.tar.bz2
tar -xjvf valgrind-3.11.0.tar.bz2
cd valgrind-3.11.0/
使用svn克隆一個
svn co svn://svn.valgrind.org/valgrind/trunk valgrind
2、生成Makefile並使用它進行編譯
生成Makefile
的步驟在README
這個文件中有寫。
-
- 運行
./autogen.sh
來設置環境(你需要標准的autoconf
工具)
這個腳本其實是調用的aclocal
autoheader
automake
autoconf
,所以必須先安裝好它,如果沒有安裝,在運行這個腳本的時候會提示你的。
- 運行
-
- 運行
./configure
來生成Makefile
文件
這里你可以使用./configure --help
來查看可以使用哪些參數設置。
一般設置好安裝路徑即可./configure --prefix=/usr/local/valgrind
- 運行
-
- 運行
make
進行編譯,運行make install
進行安裝。
- 運行
下面是我編譯時候的步驟
#運行 autogen.sh o@o-pc:~/software/valgrind-3.11.0$ ./autogen.sh running: aclocal running: autoheader running: automake -a running: autoconf #運行configure o@o-pc:~/software/valgrind-3.11.0$ ./configure --prefix=/usr/local/valgrind checking for a BSD-compatible install... /usr/bin/install -c .... ..... config.status: executing depfiles commands Maximum build arch: amd64 Primary build arch: amd64 Secondary build arch: x86 Build OS: linux ... ... #運行make o@o-pc:~/software/valgrind-3.11.0$ make echo "# This is a generated file, composed of the following suppression rules:" > default.supp echo "# " exp-sgcheck.supp xfree-3.supp xfree-4.supp glibc-2.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-2.X.supp >> default.supp cat exp-sgcheck.supp xfree-3.supp xfree-4.supp glibc-2.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-2.X.supp >> default.supp make all-recursive ... ... #運行make install o@o-pc:~/software/valgrind-3.11.0$ sudo make install [sudo] o 的密碼: make install-recursive ... ...
3、測試一下
編譯完成之后可以測試一下。
因為上面編譯安裝的時候指定了安裝目錄,所以還需要把valgrind
的bin
目錄路徑添加到環境變量PATH
中。否則只能使用全路徑來運行valgrind
這里我把它寫入到~/.bashrc
文件中。打開~/.bashrc
文件,然后在最后添加一行PATH=${PATH}:/usr/local/valgrind/bin
,之后使用source ~/.bashrc
來更新一下。
真的測試一下哦(這是README里面給出的測試命令)
o@o-pc:~/software$ valgrind ls -l ==4504== Memcheck, a memory error detector ==4504== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. ==4504== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info ==4504== Command: ls -l ==4504== 總用量 11636 drwxrwxr-x 26 o o 4096 12月 29 11:11 valgrind-3.11.0 -rw-rw-r-- 1 o o 11910809 9月 23 18:53 valgrind-3.11.0.tar.bz2 ==4504== ==4504== HEAP SUMMARY: ==4504== in use at exit: 19,436 bytes in 8 blocks ==4504== total heap usage: 205 allocs, 197 frees, 86,286 bytes allocated ==4504== ==4504== LEAK SUMMARY: ==4504== definitely lost: 0 bytes in 0 blocks ==4504== indirectly lost: 0 bytes in 0 blocks ==4504== possibly lost: 0 bytes in 0 blocks ==4504== still reachable: 19,436 bytes in 8 blocks ==4504== suppressed: 0 bytes in 0 blocks ==4504== Rerun with --leak-check=full to see details of leaked memory ==4504== ==4504== For counts of detected and suppressed errors, rerun with: -v ==4504== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1)
自己寫一個內存訪問越界和泄露的小例子來測試一下
#include <stdlib.h> int main() { ((char*)malloc(10))[10] = 100; return 0; }
使用下面的命令來檢查
valgrind --track-fds=yes --leak-check=full --undef-value-errors=yes ./test
檢查結果
==4842== Memcheck, a memory error detector ==4842== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al. ==4842== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info ==4842== Command: ./test ==4842== ==4842== Invalid write of size 1 # 這里檢測到了內存越界訪問 ==4842== at 0x400548: main (in /home/o/software/test) ==4842== Address 0x520204a is 0 bytes after a block of size 10 alloc'd ==4842== at 0x4C2BC50: malloc (vg_replace_malloc.c:299) ==4842== by 0x400543: main (in /home/o/software/test) ==4842== ==4842== ==4842== FILE DESCRIPTORS: 3 open at exit. #打開了三個文件描述符(標准輸入輸出錯誤) ==4842== Open file descriptor 2: /dev/pts/12 ==4842== <inherited from parent> ==4842== ==4842== Open file descriptor 1: /dev/pts/12 ==4842== <inherited from parent> ==4842== ==4842== Open file descriptor 0: /dev/pts/12 ==4842== <inherited from parent> ==4842== ==4842== ==4842== HEAP SUMMARY: #堆使用摘要 ==4842== in use at exit: 10 bytes in 1 blocks ==4842== total heap usage: 1 allocs, 0 frees, 10 bytes allocated #申請/釋放詳情 ==4842== ==4842== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==4842== at 0x4C2BC50: malloc (vg_replace_malloc.c:299) ==4842== by 0x400543: main (in /home/o/software/test) ==4842== ==4842== LEAK SUMMARY: 泄露摘要 ==4842== definitely lost: 10 bytes in 1 blocks ==4842== indirectly lost: 0 bytes in 0 blocks ==4842== possibly lost: 0 bytes in 0 blocks ==4842== still reachable: 0 bytes in 0 blocks ==4842== suppressed: 0 bytes in 0 blocks ==4842== ==4842== For counts of detected and suppressed errors, rerun with: -v ==4842== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 1 from 1)
可以看出上面檢測到了內存越界訪問和內存泄露。
一般寫玩程序后都可以使用valgrind
來檢測一下,避免內存泄露的問題(還有文件打開情況)