Valgrind 3.11.0编译安装

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进行安装。

下面是我编译时候的步骤

#运行 autogen.sh
o@o-pc:~/software/valgrind-3.11.$ ./autogen.sh
running: aclocal
running: autoheader
running: automake -a
running: autoconf #运行configure
o@o-pc:~/software/valgrind-3.11.$ ./configure --prefix=/usr/local/valgrind
checking for a BSD-compatible install... /usr/bin/install -c
.... .....
config.status: executing depfiles commands Maximum build arch: amd64
Primary build arch: amd64
Secondary build arch: x86
Build OS: linux
... ... #运行make o@o-pc:~/software/valgrind-3.11.$ make
echo "# This is a generated file, composed of the following suppression rules:" > default.supp
echo "# " exp-sgcheck.supp xfree-.supp xfree-.supp glibc-.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-.X.supp >> default.supp
cat exp-sgcheck.supp xfree-.supp xfree-.supp glibc-.X-drd.supp glibc-2.34567-NPTL-helgrind.supp glibc-.X.supp >> default.supp
make all-recursive
... ...
#运行make install
o@o-pc:~/software/valgrind-3.11.$ sudo make install
[sudo] o 的密码:
make install-recursive
... ...

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

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

o@o-pc:~/software$ valgrind ls -l
==== Memcheck, a memory error detector
==== Copyright (C) -, and GNU GPL'd, by Julian Seward et al.
==== Using Valgrind-3.11. and LibVEX; rerun with -h for copyright info
==== Command: ls -l
====
总用量
drwxrwxr-x o o 12月 : valgrind-3.11.
-rw-rw-r-- o o 9月 : valgrind-3.11..tar.bz2
====
==== HEAP SUMMARY:
==== in use at exit: , bytes in blocks
==== total heap usage: allocs, frees, , bytes allocated
====
==== LEAK SUMMARY:
==== definitely lost: bytes in blocks
==== indirectly lost: bytes in blocks
==== possibly lost: bytes in blocks
==== still reachable: , bytes in blocks
==== suppressed: bytes in blocks
==== Rerun with --leak-check=full to see details of leaked memory
====
==== For counts of detected and suppressed errors, rerun with: -v
==== ERROR SUMMARY: errors from contexts (suppressed: from )

自己写一个内存访问越界和泄露的小例子来测试一下

#include <stdlib.h>
int main()
{
((char*)malloc())[] = ;
return ;
}

使用下面的命令来检查

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

检查结果

==== Memcheck, a memory error detector
==== Copyright (C) -, and GNU GPL'd, by Julian Seward et al.
==== Using Valgrind-3.11. and LibVEX; rerun with -h for copyright info
==== Command: ./test
====
==== Invalid write of size # 这里检测到了内存越界访问
==== at 0x400548: main (in /home/o/software/test)
==== Address 0x520204a is bytes after a block of size alloc'd
==== at 0x4C2BC50: malloc (vg_replace_malloc.c:)
==== by 0x400543: main (in /home/o/software/test)
====
====
==== FILE DESCRIPTORS: open at exit. #打开了三个文件描述符(标准输入输出错误)
==== Open file descriptor : /dev/pts/
==== <inherited from parent>
====
==== Open file descriptor : /dev/pts/
==== <inherited from parent>
====
==== Open file descriptor : /dev/pts/
==== <inherited from parent>
====
====
==== HEAP SUMMARY: #堆使用摘要
==== in use at exit: bytes in blocks
==== total heap usage: allocs, frees, bytes allocated #申请/释放详情
====
==== bytes in blocks are definitely lost in loss record of
==== at 0x4C2BC50: malloc (vg_replace_malloc.c:)
==== by 0x400543: main (in /home/o/software/test)
====
==== LEAK SUMMARY: 泄露摘要
==== definitely lost: bytes in blocks
==== indirectly lost: bytes in blocks
==== possibly lost: bytes in blocks
==== still reachable: bytes in blocks
==== suppressed: bytes in blocks
====
==== For counts of detected and suppressed errors, rerun with: -v
==== ERROR SUMMARY: errors from contexts (suppressed: from )

可以看出上面检测到了内存越界访问和内存泄露。
一般写玩程序后都可以使用valgrind来检测一下,避免内存泄露的问题(还有文件打开情况)

Valgrind 3.11.0编译安装的更多相关文章

  1. CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14

    准备篇: CentOS 7.0系统安装配置图解教程 http://www.osyunwei.com/archives/7829.html 一.配置防火墙,开启80端口.3306端口 CentOS 7. ...

  2. centos 7.0 编译安装php 7.0.3

    php下载页面 http://cn2.php.net/downloads.php 7.0.3多地区下载页面 http://cn2.php.net/get/php-7.0.3.tar.gz/from/a ...

  3. CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14方法分享

    一.配置防火墙,开启80端口.3306端口 CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop fi ...

  4. Redis 3.0 编译安装

    Redis 3.0 编译安装 http://www.xuchanggang.cn/archives/991.html

  5. centos 7.0 编译安装mysql 5.6.22 再次总结 成功编译安装~ 越来越熟练了~

    查找php.ini文件所在位置 [root@localhost /]# find -name php.ini ./usr/etc/php/etc/php.ini mysql官网的安装说明http:// ...

  6. msyql8.0编译安装

    1.安装依赖 yum  -y install wget  cmake gcc gcc-c++ncurses  ncurses-devel  libaio-devel openssl openssl-d ...

  7. centos 7.0 编译安装php 5.6.7

    编译安装php参考资料 MySQL PHP API http://dev.mysql.com/doc/apis-php/en/index.html nginx + php +mysql 最简单安装 官 ...

  8. hadoop2.1.0编译安装教程

    由于现在hadoop2.0还处于beta版本,在apache官方网站上发布的beta版本中只有编译好的32bit可用,如果你直接下载安装在64bit的linux系统的机器上,运行会报一个INFO ut ...

  9. hadoop2.1.0和hadoop2.2.0编译安装教程

    由于现在hadoop2.0还处于beta版本,在apache官方网站上发布的beta版本中只有编译好的32bit可用,如果你直接下载安装在64bit的linux系统的机器上,运行会报一个INFO ut ...

随机推荐

  1. angularjs笔记(一)

    简介 AngularJS API angularjs是javascript框架,通过指令(指令就是自定义的html标签属性)扩展了HTML,并且可以通过表达式(表达式使用)绑定数据到HTML. 1.a ...

  2. 免费api大全

    天气接口 气象局接口 完整数据:http://m.weather.com.cn/data/101010100.html 解析 用例 当天数据:http://www.weather.com.cn/dat ...

  3. Specified key was too long; max key length is 767 bytes mysql

    Specified key was too long; max key length is 767 bytes 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该 ...

  4. IP欺骗原理与过程分析

    IP欺骗攻击法 原创:r00t <r00t@unsecret.org> QQ: 22664566 http://www.unsecret.org --------------------- ...

  5. jQuery简单实现iframe的高度根据页面内容自适应的方法

    同域下: //注意:下面的代码是放在和iframe同一个页面中调用 $("#myiframe").load(function () { var myiframeH = $(this ...

  6. Yii2-admin RBAC权限管理的实现

    原文地址:http://www.open-open.com/lib/view/open1434638805348.html   http://wlzyan.blog.163.com/blog/stat ...

  7. C# volatile关键字

    ; public int GetAge() { return Age; } 如上例子,调用GetAge()得到的是“主”内存区域的Age数值.用volatile修饰后的变量不允许有不同于“主”内存区域 ...

  8. Xshell中文版使用说明/下载

    个人本来一直用SecureCRT的这几天朋友推荐用xshell就尝试下载了一个结果发现这个比SecureCRT和puttytel好使多了切更能更完善强大特此推荐附带注册码一枚可装999台机器 1309 ...

  9. 使用SandCastle创建.Net帮助文档

    使用SandCastle创建.Net帮助文档 引用自:http://www.cnblogs.com/DotNetNuke/archive/2009/04/23/1441899.html Sandcas ...

  10. 在c#中运行js脚本(将js文件生成为.dll文件)

    原文链接:http://www.cnblogs.com/xhan/archive/2010/10/22/1857992.html 前言: 本来在搞一个Google翻译的接口--向Google翻译发送请 ...