1.下载地址:http://valgrind.org
2.下载的是tar.bz2文件
3.用以下命令解压:例如
$bzip2 -d valgrind-3.5.0.tar.dz2
$tar xvf valgrind-3.5.0.tar
这时候有一个valgrind-3.5.0的文件夹
4.然后安装
$cd valgrind-3.5.0
#./configure
#make
#make install
即可...........
valgrind是针对Intel x86的工具,它模拟x86型号的CPU处理器来直接地观察所有的内存访问,并且分析数据流
简单用法:看一下例子:
$ cat -n test.c
1 #include<stdio.h>
2 #include<malloc.h>
3 #include<string.h>
4
5 int main(int argc,char *argv[])
6 {
7 char * str;
8 str=(char *)malloc(5);
9 strcpy(str,"1234");
10 printf("%s",str);
11 free(str);
12 return 0;
13 }
现在我们把它编译一下,
$ gcc -g testm.c -o testm
我们尝试运行一下这个程序
$ valgrind --leak-check=full testm
==5338== Memcheck, a memory error detector
==5338== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==5338== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==5338== Command: testm
==5338==
1234==5338==
==5338== HEAP SUMMARY:
==5338== in use at exit: 0 bytes in 0 blocks
==5338== total heap usage: 1 allocs, 1 frees, 5 bytes allocated
==5338==
==5338== All heap blocks were freed -- no leaks are possible
==5338==
==5338== For counts of detected and suppressed errors, rerun with: -v
==5338== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 11 from 6)
注意划线部分,然后,我们把这个程序改成错误的:
这句 9 strcpy(str,"1234");
改成 9 strcpy(str,"12345"); //这时候已经是缓冲区溢出了
然后把 free(str);这句去掉; //这时候是内存泄露了
然后,我们在编译一次。
$ gcc -g testm.c -o testm
然后,这时候运行./testm也是可以进行的。
但,用$ valgrind --leak-check=full testm 会有一下结果:
$ valgrind --leak-check=full testm
==5557== Memcheck, a memory error detector
==5557== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==5557== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==5557== Command: testm
==5557==
==5557== Invalid write of size 1
==5557== at 0x40271A7: memcpy (mc_replace_strmem.c:482)
==5557== by 0x804845E: main (testm.c:9)
==5557== Address 0x419f02d is 0 bytes after a block of size 5 alloc'd
==5557== at 0x4025C1C: malloc (vg_replace_malloc.c:195)
==5557== by 0x8048440: main (testm.c:8)
==5557==
==5557== Invalid read of size 1
==5557== at 0x4026BA3: strlen (mc_replace_strmem.c:275)
==5557== by 0x407E6D7: vfprintf (in /lib/tls/i686/cmov/libc-2.9.so)
==5557== by 0x4084B5F: printf (in /lib/tls/i686/cmov/libc-2.9.so)
==5557== by 0x8048471: main (testm.c:10)
==5557== Address 0x419f02d is 0 bytes after a block of size 5 alloc'd
==5557== at 0x4025C1C: malloc (vg_replace_malloc.c:195)
==5557== by 0x8048440: main (testm.c:8)
==5557==
12345==5557==
==5557== HEAP SUMMARY:
==5557== in use at exit: 5 bytes in 1 blocks
==5557== total heap usage: 1 allocs, 0 frees, 5 bytes allocated
==5557==
==5557== 5 bytes in 1 blocks are definitely lost in loss record 1 of 1
==5557== at 0x4025C1C: malloc (vg_replace_malloc.c:195)
==5557== by 0x8048440: main (testm.c:8)
==5557==
==5557== LEAK SUMMARY:
==5557== definitely lost: 5 bytes in 1 blocks
==5557== indirectly lost: 0 bytes in 0 blocks
==5557== possibly lost: 0 bytes in 0 blocks
==5557== still reachable: 0 bytes in 0 blocks
==5557== suppressed: 0 bytes in 0 blocks
==5557==
==5557== For counts of detected and suppressed errors, rerun with: -v
==5557== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 11 from 6)
也是注意划线部分,前面的是指示出错地方了。
1 allocs, 0 frees, 5 bytes allocated 指明了没有释放内存。。
我们可以按着它的提示去修改我们的程序。