Linux 内存检测工具valgrind使用手册(转)

来源:互联网 发布:天津大学网络教育缴费 编辑:程序博客网 时间:2024/06/09 18:50
 

转自:http://xueyueming.blog.163.com/blog/static/60411785200981045547110/

Valgrind 是一款 Linux下(支持 x86、x86_64和ppc32)程序的内存调试工具,它可以对编译后的二进制程序进行内存使用监测(C语言中的malloc和free,以及C++中的new和delete),找出内存泄漏问题。

  Valgrind 中包含的 Memcheck 工具可以检查以下的程序错误:

  使用未初始化的内存 (Use of uninitialised memory)

  使用已经释放了的内存 (Reading/writing memory after it has been free’d)

  使用超过malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)

  对堆栈的非法访问 (Reading/writing inappropriate areas on the stack)

  申请的空间是否有释放 (Memory leaks – where pointers to malloc’d blocks are lost forever)

  malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])

  src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)

  重复free

  1、编译安装 Valgrind:

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

tar xvf valgrind-3.4.1.tar.bz2

cd valgrind-3.4.1/

./configure --prefix=/usr/local/webserver/valgrind

make

make install

  2、使用示例:对“ls”程序进程检查,返回结果中的“definitely lost: 0 bytes in 0 blocks.”表示没有内存泄漏。

[root@xoyo42 /]# /usr/local/webserver/valgrind/bin/valgrind --tool=memcheck --leak-check=full ls /

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

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

==1157== Using LibVEX rev 1884, a library for dynamic binary translation.

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

==1157== Using valgrind-3.4.1, a dynamic binary instrumentation framework.

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

==1157== For more details, rerun with: -v

==1157==

bin   data0  dev  home  lib64       media  mnt  opt   root  selinux  sys       tcsql.db.idx.pkey.dec  ttserver.pid  var

boot  data1  etc  lib   lost+found  misc   net  proc  sbin  srv      tcsql.db  tmp                    usr

==1157==

==1157== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 5 from 1)

==1157== malloc/free: in use at exit: 28,471 bytes in 36 blocks.

==1157== malloc/free: 166 allocs, 130 frees, 51,377 bytes allocated.

==1157== For counts of detected errors, rerun with: -v

==1157== searching for pointers to 36 not-freed blocks.

==1157== checked 174,640 bytes.

==1157==

==1157== LEAK SUMMARY:

==1157==    definitely lost: 0 bytes in 0 blocks.

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

==1157==    still reachable: 28,471 bytes in 36 blocks.

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

==1157== Reachable blocks (those to which a pointer was found) are not shown.

==1157== To see them, rerun with: --leak-check=full --show-reachable=yes

  3、使用示例:对一个使用libevent库编写的“httptest”程序进程检查,返回结果中的“definitely lost: 255 bytes in 5 blocks.”表示发生内存泄漏。

[root@xoyo42 tcsql-0.1]# /usr/local/webserver/valgrind/bin/valgrind --tool=memcheck --leak-check=full ./httptest

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

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

==1274== Using LibVEX rev 1884, a library for dynamic binary translation.

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

==1274== Using valgrind-3.4.1, a dynamic binary instrumentation framework.

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

==1274== For more details, rerun with: -v

==1274==

==1274== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1005 from 2)

==1274== malloc/free: in use at exit: 402,291 bytes in 74 blocks.

==1274== malloc/free: 15,939 allocs, 15,865 frees, 6,281,523 bytes allocated.

==1274== For counts of detected errors, rerun with: -v

==1274== searching for pointers to 74 not-freed blocks.

==1274== checked 682,468,160 bytes.

==1274==

==1274== 255 bytes in 5 blocks are definitely lost in loss record 17 of 32

==1274==    at 0x4A05FBB: malloc (vg_replace_malloc.c:207)

==1274==    by 0x3C1D809BC6: evhttp_decode_uri (http.c:2105)

==1274==    by 0x401C75: tcsql_handler (in /data0/tcsql/cankao/tcsql-0.1/tcsql)

==1274==    by 0x3C1D80C88F: evhttp_get_body (http.c:1582)

==1274==    by 0x3C1D8065F7: event_base_loop (event.c:392)

==1274==    by 0x403E2F: main (in /data0/tcsql/cankao/tcsql-0.1/tcsql)

==1274==

==1274== LEAK SUMMARY:

==1274==    definitely lost: 255 bytes in 5 blocks.

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

==1274==    still reachable: 402,036 bytes in 69 blocks.

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

==1274== Reachable blocks (those to which a pointer was found) are not shown.

==1274== To see them, rerun with: --leak-check=full --show-reachable=yes

  检查httptest程序,发现有一处“char *decode_uri = evhttp_decode_uri(evhttp_request_uri(req));”中的“decode_uri”没有被free,再程序处理完成后加上“free(decode_uri);”后,再使用Valgrind检查,结果已经是“definitely lost: 0 bytes in 0 blocks.”。

valgrind使用手册(1)

用C/C++开发其中最令人头疼的一个问题就是内存管理,有时候为了查找一个内存泄漏或者一个内存访问越界,需要要花上好几天时间,如果有一款工具能够帮助我们做这件事情就好了,valgrind正好就是这样的一款工具。

Valgrind是一款基于模拟linux下的程序调试器和剖析器的软件套件,可以运行于x86, amd64和ppc32架构上。valgrind包含一个核心,它提供一个虚拟的CPU运行程序,还有一系列的工具,它们完成调试,剖析和一些类似的任务。valgrind是高度模块化的,所以开发人员或者用户可以给它添加新的工具而不会损坏己有的结构。

valgrind的官方网址是:http://valgrind.org

你可以在它的网站上下载到最新的valgrind,它是开放源码和免费的。

一、介绍

valgrind包含几个标准的工具,它们是:

1、memcheck

   memcheck探测程序中内存管理存在的问题。它检查所有对内存的读/写操作,并截取所有的malloc/new/free/delete调用。因此memcheck工具能够探测到以下问题:

    1)使用未初始化的内存

    2)读/写已经被释放的内存

    3)读/写内存越界

    4)读/写不恰当的内存栈空间

    5)内存泄漏

    6)使用malloc/new/new[]和free/delete/delete[]不匹配。

2、cachegrind

   cachegrind是一个cache剖析器。它模拟执行CPU中的L1, D1和L2 cache,因此它能很精确的指出代码中的cache未命中。如果你需要,它可以打印出cache未命中的次数,内存引用和发生cache未命中的每一行代码,每一个函数,每一个模块和整个程序的摘要。如果你要求更细致的信息,它可以打印出每一行机器码的未命中次数。在x86和amd64上,cachegrind通过CPUID自动探测机器的cache配置,所以在多数情况下它不再需要更多的配置信息了。

3、helgrind

   helgrind查找多线程程序中的竞争数据。helgrind查找内存地址,那些被多于一条线程访问的内存地址,但是没有使用一致的锁就会被查出。这表示这些地址在多线程间访问的时候没有进行同步,很可能会引起很难查找的时序问题。

二、valgrind对你的程序都做了些什么

valgrind被设计成非侵入式的,它直接工作于可执行文件上,因此在检查前不需要重新编译、连接和修改你的程序。要检查一个程序很简单,只需要执行下面的命令就可以了

     valgrind --tool=tool_name  program_name

比如我们要对ls -l命令做内存检查,只需要执行下面的命令就可以了

     valgrind --tool=memcheck ls -l

不管是使用哪个工具,valgrind在开始之前总会先取得对你的程序的控制权,从可执行关联库里读取调试信息。然后在valgrind核心提供的虚拟CPU上运行程序,valgrind会根据选择的工具来处理代码,该工具会向代码中加入检测代码,并把这些代码作为最终代码返回给valgrind核心,最后valgrind核心运行这些代码。

如果要检查内存泄漏,只需要增加--leak-check=yes就可以了,命令如下

     valgrind --tool=memcheck --leak-check=yes ls -l

不同工具间加入的代码变化非常的大。在每个作用域的末尾,memcheck加入代码检查每一片内存的访问和进行值计算,代码大小至少增加12倍,运行速度要比平时慢25到50倍。

valgrind模拟程序中的每一条指令执行,因此,检查工具和剖析工具不仅仅是对你的应用程序,还有对共享库,GNU C库,X的客户端库都起作用。

三、现在开始

首先,在编译程序的时候打开调试模式(gcc编译器的-g选项)。如果没有调试信息,即使最好的valgrind工具也将中能够猜测特定的代码是属于哪一个函数。打开调试选项进行编译后再用valgrind检查,valgrind将会给你的个详细的报告,比如哪一行代码出现了内存泄漏。

当检查的是C++程序的时候,还应该考虑另一个选项 -fno-inline。它使得函数调用链很清晰,这样可以减少你在浏览大型C++程序时的混乱。比如在使用这个选项的时候,用memcheck检查openoffice就很容易。当然,你可能不会做这项工作,但是使用这一选项使得valgrind生成更精确的错误报告和减少混乱。

一些编译优化选项(比如-O2或者更高的优化选项),可能会使得memcheck提交错误的未初始化报告,因此,为了使得valgrind的报告更精确,在编译的时候最好不要使用优化选项。

如果程序是通过脚本启动的,可以修改脚本里启动程序的代码,或者使用--trace-children=yes选项来运行脚本。

下面是用memcheck检查ls -l命令的输出报告,在终端下执行下面的命令

    valgrind --tool=memcheck ls -l

程序会打印出ls -l命令的结果,最后是valgrind的检查报告如下:

==4187==

==4187== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 19 from 2)

==4187== malloc/free: in use at exit: 15,154 bytes in 105 blocks.

==4187== malloc/free: 310 allocs, 205 frees, 60,093 bytes allocated.

==4187== For counts of detected errors, rerun with: -v

==4187== searching for pointers to 105 not-freed blocks.

==4187== checked 145,292 bytes.

==4187==

==4187== LEAK SUMMARY:

==4187==    definitely lost: 0 bytes in 0 blocks.

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

==4187==    still reachable: 15,154 bytes in 105 blocks.

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

==4187== Reachable blocks (those to which a pointer was found) are not shown.

==4187== To see them, rerun with: --show-reachable=yes

这里的“4187”指的是执行ls -l的进程ID,这有利于区别不同进程的报告。memcheck会给出报告,分配置和释放了多少内存,有多少内存泄漏了,还有多少内存的访问是可达的,检查了多少字节的内存。

下面举两个用valgrind做内存检查的例子

例子一 (test.c):

#include <string.h>

int main(int argc, char *argv[])

{

    char *ptr;

    ptr = (char*) malloc(10);

    strcpy(ptr, "01234567890");

    return 0;

}

编译程序

    gcc -g -o test test.c

用valgrind执行命令

    valgrind --tool=memcheck --leak-check=yes ./test

报告如下

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

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

==4270== Using LibVEX rev 1606, a library for dynamic binary translation.

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

==4270== Using valgrind-3.2.0, a dynamic binary instrumentation framework.

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

==4270== For more details, rerun with: -v

==4270==

==4270== Invalid write of size 1

==4270==    at 0x4006190: strcpy (mc_replace_strmem.c:271)

==4270==    by 0x80483DB: main (test.c:8)

==4270==  Address 0x4023032 is 0 bytes after a block of size 10 alloc'd

==4270==    at 0x40044F6: malloc (vg_replace_malloc.c:149)

==4270==    by 0x80483C5: main (test.c:7)

==4270==

==4270== Invalid write of size 1

==4270==    at 0x400619C: strcpy (mc_replace_strmem.c:271)

==4270==    by 0x80483DB: main (test.c:8)

==4270==  Address 0x4023033 is 1 bytes after a block of size 10 alloc'd

==4270==    at 0x40044F6: malloc (vg_replace_malloc.c:149)

==4270==    by 0x80483C5: main (test.c:7)

==4270==

==4270== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 12 from 1)

==4270== malloc/free: in use at exit: 10 bytes in 1 blocks.

==4270== malloc/free: 1 allocs, 0 frees, 10 bytes allocated.

==4270== For counts of detected errors, rerun with: -v

==4270== searching for pointers to 1 not-freed blocks.

==4270== checked 51,496 bytes.

==4270==

==4270==

==4270== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1

==4270==    at 0x40044F6: malloc (vg_replace_malloc.c:149)

==4270==    by 0x80483C5: main (test.c:7)

==4270==

==4270== LEAK SUMMARY:

==4270==    definitely lost: 10 bytes in 1 blocks.

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

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

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

==4270== Reachable blocks (those to which a pointer was found) are not shown.

==4270== To see them, rerun with: --show-reachable=yes

从这份报告可以看出,进程号是4270,test.c的第8行写内存越界了,引起写内存越界的是strcpy函数,

第7行泄漏了10个字节的内存,引起内存泄漏的是malloc函数。

例子二(test2.c)

#include <stdio.h>

int foo(int x)

{

    if (x < 0) {

        printf("%d ", x);

    }

    return 0;

}

int main(int argc, char *argv[])

{

    int x;

   

    foo(x);

    return 0;

}

编译程序

    gcc -g -o test2 test2.c

用valgrind做内存检查

    valgrind --tool=memcheck ./test2

输出报告如下

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

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

==4285== Using LibVEX rev 1606, a library for dynamic binary translation.

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

==4285== Using valgrind-3.2.0, a dynamic binary instrumentation framework.

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

==4285== For more details, rerun with: -v

==4285==

==4285== Conditional jump or move depends on uninitialised value(s)

==4285==    at 0x8048372: foo (test2.c:5)

==4285==    by 0x80483B4: main (test2.c:16)

==4285==p p

==4285== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 12 from 1)

==4285== malloc/free: in use at exit: 0 bytes in 0 blocks.

==4285== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.

==4285== For counts of detected errors, rerun with: -v

==4285== All heap blocks were freed -- no leaks are possible.

从这份报告可以看出进程PID是4285,test2.c文件的第16行调用了foo函数,在test2.c文件的第5行foo函数使用了一个未初始化的变量。

valgrind还有很多使用选项,具体可以查看valgrind的man手册页和valgrind官方网站的在线文档。

Valgrind 使用简单说明(ZZ)

2008-01-29 19:03

调不尽的内存泄露,用不完的Valgrind

(一个介绍程序调试的精彩讲解:http://www.ibm.com/developerworks/cn/linux/l-pow-debug/)

Valgrind 介绍

Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析。你可以在它的环境中运行你的程序来监视内存的使用情况,比如C 语言中的malloc和free或者 C++中的new和 delete。使用Valgrind的工具包,你可以自动的检测许多内存管理和线程的bug,避免花费太多的时间在bug寻找上,使得你的程序更加稳固。

Valgrind的主要功能

Valgrind工具包包含多个工具,如Memcheck,Cachegrind,Helgrind, Callgrind,Massif。下面分别介绍个工具的作用:

Memcheck 工具主要检查下面的程序错误:

  • 使用未初始化的内存 (Use of uninitialised memory)

  • 使用已经释放了的内存 (Reading/writing memory after it has been free’d)

  • 使用超过 malloc分配的内存空间(Reading/writing off the end of malloc’d blocks)

  • 对堆栈的非法访问 (Reading/writing inappropriate areas on the stack)

  • 申请的空间是否有释放 (Memory leaks – where pointers to malloc’d blocks are lost forever)

  • malloc/free/new/delete申请和释放内存的匹配(Mismatched use of malloc/new/new [] vs free/delete/delete [])

  • src和dst的重叠(Overlapping src and dst pointers in memcpy() and related functions)

Callgrind

Callgrind收集程序运行时的一些数据,函数调用关系等信息,还可以有选择地进行cache 模拟。在运行结束时,它会把分析数据写入一个文件。callgrind_annotate可以把这个文件的内容转化成可读的形式。

Cachegrind

它模拟 CPU中的一级缓存I1,D1和L2二级缓存,能够精确地指出程序中 cache的丢失和命中。如果需要,它还能够为我们提供cache丢失次数,内存引用次数,以及每行代码,每个函数,每个模块,整个程序产生的指令数。这对优化程序有很大的帮助。

Helgrind

它主要用来检查多线程程序中出现的竞争问题。Helgrind 寻找内存中被多个线程访问,而又没有一贯加锁的区域,这些区域往往是线程之间失去同步的地方,而且会导致难以发掘的错误。Helgrind实现了名为” Eraser” 的竞争检测算法,并做了进一步改进,减少了报告错误的次数。

Massif

堆栈分析器,它能测量程序在堆栈中使用了多少内存,告诉我们堆块,堆管理块和栈的大小。Massif能帮助我们减少内存的使用,在带有虚拟内存的现代系统中,它还能够加速我们程序的运行,减少程序停留在交换区中的几率。

Valgrind 安装

1、 到www.valgrind.org下载最新版valgrind-3.2.3.tar.bz2

2、 解压安装包:tar –jxvf valgrind-3.2.3.tar.bz2

3、 解压后生成目录valgrind-3.2.3

4、 cd valgrind-3.2.3

5、 ./configure

6、 Make;make install

Valgrind 使用

用法: valgrind [options] prog-and-args [options]: 常用选项,适用于所有Valgrind工具

  1. -tool=<name> 最常用的选项。运行 valgrind中名为toolname的工具。默认memcheck。

  2. h –help 显示帮助信息。

  3. -version 显示valgrind内核的版本,每个工具都有各自的版本。

  4. q –quiet 安静地运行,只打印错误信息。

  5. v –verbose 更详细的信息, 增加错误数统计。

  6. -trace-children=no|yes 跟踪子线程? [no]

  7. -track-fds=no|yes 跟踪打开的文件描述?[no]

  8. -time-stamp=no|yes 增加时间戳到LOG信息? [no]

  9. -log-fd=<number> 输出LOG到描述符文件 [2=stderr]

  10. -log-file=<file> 将输出的信息写入到filename.PID的文件里,PID是运行程序的进行ID

  11. -log-file-exactly=<file> 输出LOG信息到 file

  12. -log-file-qualifier=<VAR> 取得环境变量的值来做为输出信息的文件名。 [none]

  13. -log-socket=ipaddr:port 输出LOG到socket ,ipaddr:port

LOG信息输出

  1. -xml=yes 将信息以xml格式输出,只有memcheck可用

  2. -num-callers=<number> show <number> callers in stack traces [12]

  3. -error-limit=no|yes 如果太多错误,则停止显示新错误? [yes]

  4. -error-exitcode=<number> 如果发现错误则返回错误代码 [0=disable]

  5. -db-attach=no|yes 当出现错误,valgrind会自动启动调试器gdb。[no]

  6. -db-command=<command> 启动调试器的命令行选项[gdb -nw %f %p]

适用于Memcheck工具的相关选项:

  1. -leak-check=no|summary|full 要求对leak给出详细信息? [summary]

  2. -leak-resolution=low|med|high how much bt merging in leak check [low]

  3. -show-reachable=no|yes show reachable blocks in leak check? [no]

Valgrind 使用举例

下面是一段有问题的C程序代码test.c

#include <stdlib.h>

void f(void)

{

int* x = malloc(10 * sizeof(int));

x[10] = 0; //问题1: 数组下标越界

} //问题2: 内存没有释放

int main(void)

{

f();

return 0;

}

1、 编译程序test.c

gcc -Wall test.c -g -o test

2、 使用Valgrind检查程序BUG

valgrind --tool=memcheck --leak-check=full ./test

3、 分析输出的调试信息

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

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

==3908== Using LibVEX rev 1732, a library for dynamic binary translation.

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

==3908== Using valgrind-3.2.3, a dynamic binary instrumentation framework.

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

==3908== For more details, rerun with: -v

==3908==

--3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50

--3908-- DWARF2 CFI reader: unhandled CFI instruction 0:50

/*数组越界错误*/

==3908== Invalid write of size 4

==3908== at 0x8048384: f (test.c:6)

==3908== by 0x80483AC: main (test.c:11)

==3908== Address 0x400C050 is 0 bytes after a block of size 40 alloc'd

==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)

==3908== by 0x8048377: f (test.c:5)

==3908== by 0x80483AC: main (test.c:11)

==3908==

==3908== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 14 from 1)

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

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

==3908== For counts of detected errors, rerun with: -v

==3908== searching for pointers to 1 not-freed blocks.

==3908== checked 59,124 bytes.

==3908==

==3908==

/*有内存空间没有释放*/

==3908== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1

==3908== at 0x40046F2: malloc (vg_replace_malloc.c:149)

==3908== by 0x8048377: f (test.c:5)

==3908== by 0x80483AC: main (test.c:11)

==3908==

==3908== LEAK SUMMARY:

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

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

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

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

原创粉丝点击