Linux下安装Valgrind工具

 
valgrind是一款很优秀的内存泄漏检测工具。
 
下面介绍如何在Linux下安装Valgrind。
 
首先下载安装包到/root目录下(或者其他目录);
 
[root@localhost ~]# ll
total 7200
-rw-------. 1 root root    2692 Apr 10 13:52 anaconda-ks.cfg
-rw-r--r--. 1 root root   39935 Apr 10 13:52 install.log
-rw-r--r--. 1 root root    9154 Apr 10 13:51 install.log.syslog
-rw-r--r--. 1 root root  303586 Apr 11 22:50 lua-5.3.4.tar.gz
-rw-r--r--. 1 root root 6998648 Apr 11 19:34 ntp-4.2.8p10.tar.gz
drwxr-xr-x. 2 root root    4096 Apr 12 18:57 sort
[root@localhost ~]# which valgrind
/usr/bin/which: no valgrind in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# ll /root/
total 19692
-rw-------. 1 root root     2692 Apr 10 13:52 anaconda-ks.cfg
-rw-r--r--. 1 root root    39935 Apr 10 13:52 install.log
-rw-r--r--. 1 root root     9154 Apr 10 13:51 install.log.syslog
-rw-r--r--. 1 root root   303586 Apr 11 22:50 lua-5.3.4.tar.gz
-rw-r--r--. 1 root root  6998648 Apr 11 19:34 ntp-4.2.8p10.tar.gz
drwxr-xr-x. 2 root root     4096 Apr 12 18:57 sort
-rw-r--r--. 1 root root 12788556 Apr 14 17:56 valgrind-3.12.0.tar.bz2
[root@localhost src]# tar jxf /root/valgrind-3.12.0.tar.bz2 
[root@localhost src]# ll
total 12
drwxr-xr-x.  4 1026 1000 4096 Apr 11 23:02 lua-5.3.4
drwxrwsr-x. 23  427 6011 4096 Mar 21 07:47 ntp-4.2.8p10
drwxrwxr-x. 24 1000 1000 4096 Oct 21 04:03 valgrind-3.12.0
[root@localhost src]# cd valgrind-3.12.0/
[root@localhost valgrind-3.12.0]# ll
total 1996
-rw-rw-r--.  1 1000 1000  44566 Oct 21 03:42 aclocal.m4
-rw-rw-r--.  1 1000 1000   3267 Oct 21 03:37 AUTHORS
-rwxrwxr-x.  1 1000 1000    191 Oct 21 03:37 autogen.sh
drwxrwxr-x.  3 1000 1000   4096 Oct 21 04:01 auxprogs


将源码解压缩到/usr/local/src下。

 
阅读下valgrind中的README文件,可以看到如下信息:
 

Building and installing it
~~~~~~~~~~~~~~~~~~~~~~~~~~
To install from the Subversion repository :

  0. Check out the code from SVN, following the instructions at
     http://www.valgrind.org/downloads/repository.html.

  1. cd into the source directory.

  2. Run ./autogen.sh to setup the environment (you need the standard
     autoconf tools to do so). 

  3. Continue with the following instructions...

To install from a tar.bz2 distribution:

  4. Run ./configure, with some options if you wish.  The only interesting
     one is the usual --prefix=/where/you/want/it/installed.

  5. Run "make". 

  6. Run "make install", possibly as root if the destination permissions
     require that.

  7. See if it works.  Try "valgrind ls -l".  Either this works, or it
     bombs out with some complaint.  In that case, please let us know
     (see www.valgrind.org).

Important!  Do not move the valgrind installation into a place
different from that specified by --prefix at build time.  This will
cause things to break in subtle ways, mostly when Valgrind handles
fork/exec calls.

 
和通常的安装步骤一样:configure;make;make install
 
过程如下:
 

[root@localhost valgrind-3.12.0]# ./configure --prefix=/usr/local/valgrind
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
……
config.status: executing depfiles commands

         Maximum build arch: x86
         Primary build arch: x86
       Secondary build arch: 
                   Build OS: linux
       Primary build target: X86_LINUX
     Secondary build target: 
           Platform variant: vanilla
      Primary -DVGPV string: -DVGPV_x86_linux_vanilla=1
         Default supp files: exp-sgcheck.supp xfree-3.supp xfree-4.supp glibc-2.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-2.X.supp 

[root@localhost valgrind-3.12.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[1]: Entering directory `/usr/local/src/valgrind-3.12.0'
Making all in include
make[2]: Entering directory `/usr/local/src/valgrind-3.12.0/include'
make[2]: Nothing to be done for `all'.
make[2]: Leaving directory `/usr/local/src/valgrind-3.12.0/include'
Making all in VEX
……
make[1]: Leaving directory `/usr/local/src/valgrind-3.12.0'
[root@localhost valgrind-3.12.0]# make install
make  install-recursive
make[1]: Entering directory `/usr/local/src/valgrind-3.12.0'
Making install in include
make[2]: Entering directory `/usr/local/src/valgrind-3.12.0/include'
make[3]: Entering directory `/usr/local/src/valgrind-3.12.0/include'
make[3]: Nothing to be done for `install-exec-am'.
……
make[1]: Leaving directory `/usr/local/src/valgrind-3.12.0'
[root@localhost valgrind-3.12.0]# cd ..
[root@localhost src]# cd ..
[root@localhost local]# ll
total 40
drwxr-xr-x. 2 root root 4096 Sep 23  2011 bin
drwxr-xr-x. 2 root root 4096 Sep 23  2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23  2011 games
drwxr-xr-x. 2 root root 4096 Sep 23  2011 include
drwxr-xr-x. 2 root root 4096 Sep 23  2011 lib
drwxr-xr-x. 2 root root 4096 Sep 23  2011 libexec
drwxr-xr-x. 2 root root 4096 Sep 23  2011 sbin
drwxr-xr-x. 5 root root 4096 Apr 10 13:42 share
drwxr-xr-x. 5 root root 4096 Apr 14 17:57 src
drwxr-xr-x. 6 root root 4096 Apr 14 18:04 valgrind
[root@localhost local]# which valgrind
/usr/bin/which: no valgrind in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)


make install后发现还是没有(没有找到)valgrind可执行文件。
 
其实这个时候已经有了,只是没有找到罢了。
 
我们可以将valgrind做一个软连接到/usr/local/bin下:
 
[root@localhost local]# cd valgrind/
[root@localhost valgrind]# ll
total 16
drwxr-xr-x. 2 root root 4096 Apr 14 18:04 bin
drwxr-xr-x. 3 root root 4096 Apr 14 18:04 include
drwxr-xr-x. 4 root root 4096 Apr 14 18:04 lib
drwxr-xr-x. 4 root root 4096 Apr 14 18:04 share
[root@localhost valgrind]# cd bin/
[root@localhost bin]# ll
total 392
-rwxr-xr-x. 1 root root 41112 Apr 14 18:04 callgrind_annotate
-rwxr-xr-x. 1 root root 12016 Apr 14 18:04 callgrind_control
-rwxr-xr-x. 1 root root 32170 Apr 14 18:04 cg_annotate
-rwxr-xr-x. 1 root root 10418 Apr 14 18:04 cg_diff
-rwxr-xr-x. 1 root root 49307 Apr 14 18:04 cg_merge
-rwxr-xr-x. 1 root root 24398 Apr 14 18:04 ms_print
-rwxr-xr-x. 1 root root 39093 Apr 14 18:04 valgrind
-rwxr-xr-x. 1 root root 65697 Apr 14 18:04 valgrind-di-server
-rwxr-xr-x. 1 root root 21037 Apr 14 18:04 valgrind-listener
-rwxr-xr-x. 1 root root 85247 Apr 14 18:04 vgdb
[root@localhost bin]# which valgrind
/usr/bin/which: no valgrind in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost bin]# cd /usr/local/bin/
[root@localhost bin]# ln -s /usr/local/valgrind/bin/valgrind valgrind
[root@localhost bin]# which valgrind
/usr/local/bin/valgrind

测试下:
 
[root@localhost bin]# valgrind ls -l
==16148== Memcheck, a memory error detector
==16148== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==16148== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==16148== Command: ls -l
==16148== 
total 0
lrwxrwxrwx. 1 root root 32 Apr 14 18:06 valgrind -> /usr/local/valgrind/bin/valgrind
==16148== 
==16148== HEAP SUMMARY:
==16148==     in use at exit: 13,069 bytes in 11 blocks
==16148==   total heap usage: 206 allocs, 195 frees, 62,575 bytes allocated
==16148== 
==16148== LEAK SUMMARY:
==16148==    definitely lost: 0 bytes in 0 blocks
==16148==    indirectly lost: 0 bytes in 0 blocks
==16148==      possibly lost: 0 bytes in 0 blocks
==16148==    still reachable: 13,069 bytes in 11 blocks
==16148==         suppressed: 0 bytes in 0 blocks
==16148== Rerun with --leak-check=full to see details of leaked memory
==16148== 
==16148== For counts of detected and suppressed errors, rerun with: -v
==16148== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 21 from 10)

OK,安装完成。
 
回想刚刚的安装步骤:
 
1.解压源代码到/usr/local/src下;
2.configure --prefix=/usr/local/valgrind;
3.make && make install;
4.ln做一个软连接到/usr/local/bin中;
 
大功告成。
Logo

更多推荐