Centos下Valgrind使用与安装

前端之家收集整理的这篇文章主要介绍了Centos下Valgrind使用与安装前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

第一步:

获取Valgrind 包 可以先使用浏览器访问http://valgrind.org/downloads/查看当前版本

wget http://valgrind.org/downloads/valgrind-3.10.1.tar.bz2

第二步:

将下载的文件解压

tar -jxvf valgrind-3.10.1.tar.bz2

第三步:

安装和配置
./autogen.sh
./configure

make; make install

Linux下利用Valgrind工具进行内存泄露检测和性能分析

@H_301_44@Valgrind通常用来成分析程序性能及程序中的内存泄露错误

@H_301_44@一Valgrind工具集简绍

Valgrind包含下列工具:

1、memcheck:检查程序中的内存问题,如泄漏、越界、非法指针等。

2、callgrind:检测程序代码的运行时间和调用过程,以及分析程序性能

3、cachegrind:分析cpu的cache命中率、丢失率,用于进行代码优化。

4、helgrind:用于检查多线程程序的竞态条件。

5、massif:堆栈分析器,指示程序中使用了多少堆内存等信息。

6、lackey:

7、nulgrind:

这几个工具的使用是通过命令:valgrand --tool=name 程序名来分别调用的,当不指定tool参数时默认是--tool=memcheck

二Valgrind工具详解

1.Memcheck

最常用的工具,用来检测程序中出现的内存问题,所有对内存的读写都会被检测到,一切对malloc、free、new、delete的调用都会被捕获。所以,它能检测以下问题:

1、对未初始化内存的使用;

2、读/写释放后的内存块;

3、读/写超出malloc分配的内存块;

4、读/写不适当的栈中内存块;

5、内存泄漏,指向一块内存的指针永远丢失;

6、不正确的malloc/free或new/delete匹配;

7、memcpy()相关函数中的dst和src指针重叠。

这些问题往往是C/C++程序员最头疼的问题,Memcheck能在这里帮上大忙。
例如:

  1. @H_301_44@#include<stdlib.h>
  2. @H_301_44@#include<malloc.h>
  3. @H_301_44@#include<string.h>
  4. @H_301_44@
  5. @H_301_44@voidtest()
  6. @H_301_44@{
  7. @H_301_44@int*ptr=malloc(sizeof(int)*10);
  8. @H_301_44@ptr[10]=7;//内存越界
  9. @H_301_44@memcpy(ptr+1,ptr,5);//踩内存
  10. @H_301_44@
  11. @H_301_44@free(ptr);
  12. @H_301_44@free(ptr);//重复释放
  13. @H_301_44@int*p1;
  14. @H_301_44@*p1=1;//非法指针
  15. @H_301_44@}
  16. @H_301_44@intmain(void)
  17. @H_301_44@test();
  18. @H_301_44@return0;
  19. @H_301_44@}
@H_301_44@将程序编译生成可执行文件后执行valgrind --leak-check=full ./程序名

输出结果如下:

==4832==Memcheck,amemoryerrordetector
  • @H_301_44@==4832==Copyright(C)2002-2010,andGNUGPL'd,byJulianSewardetal.
  • @H_301_44@==4832==UsingValgrind-3.6.1andLibVEX;rerunwith-hforcopyrightinfo
  • @H_301_44@==4832==Command:./tmp
  • @H_301_44@==4832==
  • @H_301_44@==4832==Invalidwriteofsize4//内存越界
  • @H_301_44@==4832==at0x804843F:test(in/home/yanghao/Desktop/testC/testmem/tmp)
  • @H_301_44@==4832==by0x804848D:main(in/home/yanghao/Desktop/testC/testmem/tmp)
  • @H_301_44@==4832==Address0x41a6050is0bytesafterablockofsize40alloc'd
  • @H_301_44@==4832==at0x4026864:malloc(vg_replace_malloc.c:236)
  • @H_301_44@==4832==by0x8048435:test(in/home/yanghao/Desktop/testC/testmem/tmp)
  • @H_301_44@==4832==Sourceanddestinationoverlapinmemcpy(0x41a602c,0x41a6028,5)//踩内存
  • @H_301_44@==4832==at0x4027BD6:memcpy(mc_replace_strmem.c:635)
  • @H_301_44@==4832==by0x8048461:test(in/home/yanghao/Desktop/testC/testmem/tmp)
  • @H_301_44@==4832==by0x804848D:main(in/home/yanghao/Desktop/testC/testmem/tmp)
  • @H_301_44@==4832==
  • @H_301_44@==4832==Invalidfree()/delete/delete[]//重复释放
  • @H_301_44@==4832==at0x4025BF0:free(vg_replace_malloc.c:366)
  • @H_301_44@==4832==by0x8048477:test(in/home/yanghao/Desktop/testC/testmem/tmp)
  • @H_301_44@==4832==Address0x41a6028is0bytesinsideablockofsize40free'd
  • @H_301_44@==4832==by0x804846C:test(in/home/yanghao/Desktop/testC/testmem/tmp)
  • @H_301_44@==4832==USEOfuninitialisedvalueofsize4//非法指针
  • @H_301_44@==4832==at0x804847B:test(in/home/yanghao/Desktop/testC/testmem/tmp)
  • @H_301_44@==4832==Processterminatingwithdefaultactionofsignal11(SIGSEGV)//由于非法指针赋值导致的程序崩溃
  • @H_301_44@==4832==Badpermissionsformappedregionataddress0x419FFF4
  • @H_301_44@==4832==HEAPSUMMARY:
  • @H_301_44@==4832==inuseatexit:0bytesin0blocks
  • @H_301_44@==4832==totalheapusage:1allocs,2frees,40bytesallocated
  • @H_301_44@==4832==Allheapblockswerefreed--noleaksarepossible
  • @H_301_44@==4832==Forcountsofdetectedandsuppressederrors,rerunwith:-v
  • @H_301_44@==4832==Use--track-origins=yestoseewhereuninitialisedvaluescomefrom
  • @H_301_44@==4832==ERRORSUMMARY:4errorsfrom4contexts(suppressed:11from6)
  • @H_301_44@Segmentationfault
  • 从valgrind的检测输出结果看,这几个错误都找了出来。

    猜你在找的CentOS相关文章