Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1715484
  • 博文数量: 782
  • 博客积分: 2455
  • 博客等级: 大尉
  • 技术积分: 4140
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-06 21:37
个人简介

Linux ,c/c++, web,前端,php,js

文章分类

全部博文(782)

文章存档

2015年(8)

2014年(28)

2013年(110)

2012年(307)

2011年(329)

分类:

2012-05-07 09:08:07

原文地址:valgrind 作者:djstava

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

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

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

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

安装valgrind

    sudo apt-get install valgrind

Example:

//test.c

#include <stdio.h>

int main()
{
    int* x = malloc(10 * sizeof(int));
    x[10] = 0;
    return 0;
}


gcc test.c

djstava@DELL:~/Templates$ valgrind --tool=memcheck --leak-check=full ./a.out
==2672== Memcheck, a memory error detector
==2672== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2672== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==2672== Command: ./a.out
==2672==
==2672== Invalid write of size 4
==2672==    at 0x80483E4: main (in /home/djstava/Templates/a.out)
==2672==  Address 0x419c050 is 0 bytes after a block of size 40 alloc'd
==2672==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2672==    by 0x80483D8: main (in /home/djstava/Templates/a.out)
==2672==
==2672==
==2672== HEAP SUMMARY:
==2672==     in use at exit: 40 bytes in 1 blocks
==2672==   total heap usage: 1 allocs, 0 frees, 40 bytes allocated
==2672==
==2672== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
==2672==    at 0x4025BD3: malloc (vg_replace_malloc.c:236)
==2672==    by 0x80483D8: main (in /home/djstava/Templates/a.out)
==2672==
==2672== LEAK SUMMARY:
==2672==    definitely lost: 40 bytes in 1 blocks
==2672==    indirectly lost: 0 bytes in 0 blocks
==2672==      possibly lost: 0 bytes in 0 blocks
==2672==    still reachable: 0 bytes in 0 blocks
==2672==         suppressed: 0 bytes in 0 blocks
==2672==
==2672== For counts of detected and suppressed errors, rerun with: -v
==2672== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 13 from 8)

阅读(497) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~