让优秀变成一种习惯

Valgrind 3.11.0编译安装

上一篇 / 下一篇  2017-11-22 07:44:57 / 个人分类:学习

Valgrind是一款用于内存调试、内存泄漏检测以及性能分析的软件开发工具。
Valgrind遵守GNU通用公共许可证条款,是一款自由软件。
到3.3.0版本为止,Valgrind支持x86、x86-64以及PowerPC上的Linux。除此之外,还有一些其它非正式支持的类Unix平台(如FreeBSD、NetBSD以及Mac OS X)。

1、下载Valgrind 3.11.0

直接下载源码包

wget http://valgrind.org/downloads/valgrind-3.11.0.tar.bz2
tar -xjvf valgrind-3.11.0.tar.bz2 
cd valgrind-3.11.0/

使用svn克隆一个

svn co svn://svn.valgrind.org/valgrind/trunk valgrind

2、生成Makefile并使用它进行编译

生成Makefile的步骤在README这个文件中有写。

    1. 运行./autogen.sh来设置环境(你需要标准的autoconf工具)
      这个脚本其实是调用的aclocal autoheader automake autoconf,所以必须先安装好它,如果没有安装,在运行这个脚本的时候会提示你的。
    1. 运行./configure来生成Makefile文件
      这里你可以使用./configure --help来查看可以使用哪些参数设置。
      一般设置好安装路径即可./configure --prefix=/usr/local/valgrind
    1. 运行make进行编译,运行make install进行安装。
3、测试一下
编译完成之后可以测试一下。

因为上面编译安装的时候指定了安装目录,所以还需要把valgrindbin目录路径添加到环境变量PATH中。否则只能使用全路径来运行valgrind
这里我把它写入到~/.bashrc文件中。打开~/.bashrc文件,然后在最后添加一行PATH=${PATH}:/usr/local/valgrind/bin,之后使用source ~/.bashrc来更新一下。

真的测试一下哦(这是README里面给出的测试命令)


o@o-pc:~/software$ valgrindls-l==4504==Memcheck, a memory error detector==4504== Copyright (C)2002-2015, and GNU GPL'd, by Julian Seward et al.==4504== Using Valgrind-3.11.0and LibVEX; rerun with -hforcopyrightinfo==4504== Command:ls-l==4504==总用量11636drwxrwxr-x26o o409612月2911:11valgrind-3.11.0-rw-rw-r--1o o119108099月2318:53valgrind-3.11.0.tar.bz2==4504== 
==4504==HEAP SUMMARY:==4504==inuse at exit:19,436bytesin8blocks==4504==   total heap usage:205allocs,197frees,86,286bytes allocated==4504== 
==4504==LEAK SUMMARY:==4504==    definitely lost:0bytesin0blocks==4504==    indirectly lost:0bytesin0blocks==4504==      possibly lost:0bytesin0blocks==4504==    still reachable:19,436bytesin8blocks==4504==         suppressed:0bytesin0blocks==4504== Rerun with --leak-check=full to see details of leaked memory==4504== 
==4504== For counts of detected and suppressed errors, rerun with: -v==4504== ERROR SUMMARY:0errors from0contexts (suppressed:1from1)
自己写一个内存访问越界和泄露的小例子来测试一下

#include <stdlib.h>intmain()
{
    ((char*)malloc(10))[10] =100;return0;
}

使用下面的命令来检查

valgrind --track-fds=yes --leak-check=full --undef-value-errors=yes ./test

检查结果

==4842==Memcheck, a memory error detector==4842== Copyright (C)2002-2015, and GNU GPL'd, by Julian Seward et al.==4842== Using Valgrind-3.11.0and LibVEX; rerun with -hforcopyrightinfo==4842== Command: ./test==4842== 
==4842== Invalidwriteof size1# 这里检测到了内存越界访问==4842==    at0x400548: main (in/home/o/software/test)==4842==  Address0x520204ais0bytes after a block of size10alloc'd==4842==    at0x4C2BC50: malloc (vg_replace_malloc.c:299)==4842==    by0x400543: main (in/home/o/software/test)==4842== 
==4842== 
==4842== FILE DESCRIPTORS:3open at exit.    #打开了三个文件描述符(标准输入输出错误)==4842== Openfiledescriptor2: /dev/pts/12==4842==    <inherited from parent>
==4842== 
==4842== Openfiledescriptor1: /dev/pts/12==4842==    <inherited from parent>
==4842== 
==4842== Openfiledescriptor0: /dev/pts/12==4842==    <inherited from parent>
==4842== 
==4842== 
==4842==HEAP SUMMARY:   #堆使用摘要==4842==inuse at exit:10bytesin1blocks==4842==   total heap usage:1allocs,0frees,10bytes allocated #申请/释放详情==4842== 
==4842==10bytesin1blocks are definitely lostinloss record1of1==4842==    at0x4C2BC50: malloc (vg_replace_malloc.c:299)==4842==    by0x400543: main (in/home/o/software/test)==4842== 
==4842==LEAK SUMMARY:   泄露摘要==4842==    definitely lost:10bytesin1blocks==4842==    indirectly lost:0bytesin0blocks==4842==      possibly lost:0bytesin0blocks==4842==    still reachable:0bytesin0blocks==4842==         suppressed:0bytesin0blocks==4842== 
==4842== For counts of detected and suppressed errors, rerun with: -v==4842== ERROR SUMMARY:2errors from2contexts (suppressed:1from1)

TAG:

 

评分:0

我来说两句

我的栏目

日历

« 2024-03-22  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 41481
  • 日志数: 40
  • 建立时间: 2015-12-17
  • 更新时间: 2017-12-28

RSS订阅

Open Toolbar