valgrind的使用

一.Valgrind是什么?

Valgrind是一个提供程序调试及性能分析的工具集。其包含的工具主要有Memcheck,Cachegrind,Callgrind,Massif等。其中,最为常用的是Memcheck,其主要用来检查程序heap上的内存使用情况。本文档主要介绍Memcheck的用法和一些使用技巧。

其官方网站是:      http://valgrind.org/

 

二.Valgrind能干什么不能干什么?

Valgrind主要用来检查程序中可能出现的以下问题:

1.       Use ofuninitialised memory

2.       Reading/writingmemory after it has been free’d

3.       Reading/writingoff the end of malloc’d block

4.       Memoryleaks -- where pointers to malloc’d blocks are lost foreve

5.       Mismatcheduse of malloc/new/new [] vs free/delete/delete []

6.       Overlappingsrc and dst pointers in memcpy() and related functions

 

功能约束如下:

1.       只能检查heap上的错误,不能检查出static和stack内存的使用,如数组越界等。

2.       不能指出为什么泄漏,也不能指出在哪内存泄漏

3.       指出的错误并非100%正确,但建议在编译时至少以warning的心态对待它们。

 

三.Valgrind的安装与部署

 

若有root权限,其安装方式如下:

1.       从官网上下载valgrind 安装包:http://valgrind.org/downloads/valgrind-3.3.0.tar.bz2

2.       用bzip2及tar命令解压压缩包。

3.       进入解压目录,运行./configure

4.       运行“make”命令

5.       运行“make install”命令

6.       运行“valgrind ls- l”测试valgrind是否已经正确安装到计算机上。若正确安装,则会出现类似第四部分的报错信息。

 

若没有root权限,则在第3步时,可以用--prefix指定安装的目录

./configure –prefix= /home/work/yangfenqiang/

以下步骤相同。

 

四.Valgrind使用示例及报错信息说明

 

编写程序test.cpp如下:

 

      1 #include <iostream>

      2 using namespace std;

      3

      4 int main()

      5 {

      6    int *a = new int[10];

      7     a[11] = 0;

      8    cout << a[11]<< endl;

      9    return 0;

     10 }

     11

 

编译该程序:gcc –g –o test test.cpp

注意加入-g参数,便于valgrind读入符号表之类的信息以提供更丰富的错误定位信息。不推荐加入-O等优化参数,因为优化后的代码易于让valgrind解释错误。

 

运行“valgrind --tool=memcheck --leak-check=full ./test”,显示如下信息:

 

==2051== Memcheck,a memory error detector.

==2051== Copyright(C) 2002-2007, and GNU GPL'd, by Julian Seward et al.

==2051== UsingLibVEX rev 1804, a library for dynamic binary translation.

==2051== Copyright(C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.

==2051== Usingvalgrind-3.3.0, a dynamic binary instrumentationframework.

==2051== Copyright(C) 2000-2007, and GNU GPL'd, by Julian Seward et al.

==2051== For moredetails, rerun with: -v

==2051==

==2051== Invalidwrite of size 4

==2051==    at 0x4009C6:main (test.cpp:7)

==2051==  Address 0x4a2005cis 4 bytes after a block of size 40 alloc'd

==2051==    at 0x490581B: operator new[](unsigned long)(vg_replace_malloc.c:274)

==2051==    by 0x4009B9: main (test.cpp:6)

==2051==

==2051== Invalidread of size 4

==2051==    at 0x4009D4: main (test.cpp:8)

==2051==  Address 0x4a2005cis 4 bytes after a block of size 40 alloc'd

==2051==    at 0x490581B: operator new[](unsigned long)(vg_replace_malloc.c:274)

==2051==    by 0x4009B9: main (test.cpp:6)

0

==2051==

==2051== ERRORSUMMARY: 2 errors from 2 contexts (suppressed: 9 from 4)

==2051==malloc/free: in use at exit: 40 bytes in 1 blocks.

==2051==malloc/free: 1 allocs, 0 frees, 40 bytes allocated.

==2051== Forcounts of detected errors, rerun with: -v

==2051== searchingfor pointers to 1 not-freed blocks.

==2051== checked198,560 bytes.

==2051==

==2051==

==2051== 40 bytesin 1 blocks are definitely lost in loss record 1 of 1

==2051==    at 0x490581B: operator new[](unsigned long)(vg_replace_malloc.c:274)

==2051==    by 0x4009B9: main (test.cpp:6)

==2051==

==2051== LEAKSUMMARY:

==2051==    definitely lost: 40 bytes in 1 blocks.

==2051==      possibly lost: 0 bytes in 0 blocks.

==2051==    still reachable: 0 bytes in 0 blocks.

==2051==         suppressed: 0 bytes in 0 blocks.

 

 

其中:

1.       ==2014==表示进程号信息,基本没用。

2.       接下来是Memcheck的版权声明信息。

3.       详细的报错信息,如at 0x4009C6:main (test.cpp:7) Address 0x4a2005c is 4 bytes after a block of size 40alloc'd

说明test.cpp的第7行发生内存访问越界,越界的位移为4。

4.       ERRORSUMMARY下面为错误汇总信息。

5.       接着是内存泄漏信息。说明有40byte的内存泄漏。

6.       LEAKSUMMARY为内存泄漏信息。

在LEAK SUMMARY中:

l definitelylost:表明没有任何指针指向该区域,已经造成了内存泄漏。

l possiblylost:存在指针指向内存中的某个位置,valgrind认为你有可能是在做一些其他的高级应用(将指针放在申请的内存块中间)

l stillreachable:仍有指针引用该内存块,只是没有释放而已,可以通过设置—show-reachable=yes来报错。

 

五.Valgrind常用命令参数

1.       --tool=<name> [default=memcheck]

--tool参数指明所要使用valgrind的哪一个工具,默认的为memcheck。因为大多数情况下我们只会用到memcheck工具,因此该参数可以不写。

 

2.       --leak-check=<no|summary|yes|full>[default:summary]

在退出时检查是否有泄漏。Summary只是告诉我们有多少次泄漏,yes或full会告诉我们每次泄漏的详细信息。

 

3.       --show-reachable=<yes|no>[default:no]

通过设定该参数为yes,则显示still reachable类型的内存泄漏信息。

 

其他更多的运行参数信息可以查看《valgrind使用指南》及《valgrind manual》。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值