1.内存问题的原因及分类

在C/C++程序中,有关内存使用的问题是最难发现和解决的。这些问题可能导致程序莫名其妙地停止、崩溃,或者不断消耗内存直至资源耗尽。由于C/C++语言本身的特质和历史原因,程序员使用内存需要注意的事项较多,而且语言本身也不提供类似Java的垃圾清理机制。编程人员使用一定的工具来查找和调试内存相关问题是十分必要的。

总的说来,与内存有关的问题可以分成两类:内存访问错误和内存使用错误。内存访问错误包括错误地读取内存和错误地写内存。错误地读取内存可能让你的模块返回意想不到的结果,从而导致后续的模块运行异常。错误地写内存可能导致系统崩溃。内存使用方面的错误主要是指申请的内存没有正确释放,从而使程序运行逐渐减慢,直至停止。这方面的错误由于表现比较慢很难被人工察觉。程序也许运行了很久才会耗净资源,发生问题。

1.1 内存解剖

一个典型的C++内存布局如下图所示:

自底向上,内存中依次存放着只读的程序代码和数据,全局变量和静态变量,堆中的动态申请变量和堆栈中的自动变量。自动变量就是在函数内声明的局部变量。当函数被调用时,它们被压入栈;当函数返回时,它们就要被弹出堆栈。堆栈的使用基本上由系统控制,用户一般不会直接对其进行控制,所以堆栈的使用还是相对安全的。动态内存是一柄双刃剑:它可以提供程序员更灵活的内存使用方法,而且有些算法没有动态内存会很难实现;但是动态内存往往是内存问题存在的沃土。

1.2 内存访问错误

相对用户使用的语言,动态内存的申请一般由malloc/new来完成,释放由free/delete完成。基本的原则可以总结为:一对一,不混用。也就是说一个malloc必须对应一且唯一的free;new对应一且唯一的delete; malloc不能和delete, new不能和free对应。另外在C++中要注意delete和delete[]的区别。delete用来释放单元变量,delete[]用来释放数组等集聚变量。有关这方面的详细信息可以参考[C++Adv]。

我们可以将内存访问错误大致分成以下几类:数组越界读或写、访问未初始化内存、访问已经释放的内存和重复释放内存或释放非法内存。

下面的代码集中显示了上述问题的典型例子:

1   #include <iostream>
2   using namespace std;
3   int main(){
4      char* str1="four";
5      char* str2=new char[4]; //not enough space
6      char* str3=str2;
7      cout<<str2<<endl;    //UMR
8      strcpy(str2,str1);   //ABW
9      cout<<str2<<endl;  //ABR
10     delete str2;
11     str2[0]+=2;    //FMR and FMW
12     delete str3; //FFM
13   }

由以上的程序,我们可以看到:在第5行分配内存时,忽略了字符串终止符"/0"所占空间导致了第8行的数组越界写(Array Bounds Write)和第9行的数组越界读(Array Bounds Read); 在第7行,打印尚未赋值的str2将产生访问未初始化内存错误(Uninitialized Memory Read);在第11行使用已经释放的变量将导致释放内存读和写错误(Freed Memory Read and Freed Memory Write);最后由于str3和str2所指的是同一片内存,第12行又一次释放了已经被释放的空间 (Free Freed Memory)。

这个包含许多错误的程序可以编译连接,而且可以在很多平台上运行。但是这些错误就像定时炸弹,会在特殊配置下触发,造成不可预见的错误。这就是内存错误难以发现的一个主要原因。

1.3 内存使用错误

内存使用错误主要是指内存泄漏,也就是指申请的动态内存没有被正确地释放,或者是没有指针可以访问这些内存。这些小的被人遗忘的内存块占据了一定的地址空间。当系统压力增大时,这些越来越多的小块将最终导致系统内存耗尽。内存使用错误比内存访问错误更加难以发现。这主要有两点原因:第一,内存使用错误是"慢性病",它的症状可能不会在少数、短时间的运行中体现;第二,内存使用错误是因为"不做为"(忘记释放内存)而不是"做错"造成的。这样由于忽略造成的错误在检查局部代码时很难发现,尤其是当系统相当复杂的时候。

回页首

2.Purify的原理及使用

IBM Rational PurifyPlus是一组程序运行时的分析软件。她包括了程序性能瓶颈分析软件Quantify, 程序覆盖面分析软件PureCoverage,和本文的主角:程序运行错误分析软件Purify。Purify可以发现程序运行时的内存访问,内存泄漏和其他难以发现的问题。

同时她也是市场上唯一支持多种平台的类似工具,并且可以和很多主流开发工具集成。Purify可以检查应用的每一个模块,甚至可以查出复杂的多线程或进程应用中的错误。另外她不仅可以检查C/C++,还可以对Java或.NET中的内存泄漏问题给出报告。

2.1 Purify的原理

程序运行时的分析可以采用多种方法。Purify使用了具有专利的目标代码插入技术(OCI:Object Code Insertion)。她在程序的目标代码中插入了特殊的指令用来检查内存的状态和使用情况。这样做的好处是不需要修改源代码,只需要重新编译就可以对程序进行分析。

对于所有程序中使用的动态内存,Purify将它们按照状态进行归类。这可以由下图来说明(来自[DEV205]):

参见本文中以上给出的代码,在程序第5行执行后,str2处于黄色状态。当在第7行进行读的时候,系统就会报告一个访问未初始化内存错误(Uninitialized Memory Read)。因为只有在绿色状态下,内存才可以被合法访问。

为了检查数据越界错误(ABR,ABW),Purify还在每个分配的内存前后插入了红色区域。这样一来,超过边界的访问指令必定落在非法区域,从而触发ABR或者ABW错误报告。这里需要指出一点。访问未初始化内存错误UMR在某些情况下其实是合法的操作,例如内存拷贝。所以在分析报告时可以把UMR放到最后,或者干脆从结果中滤除。

2.2 Purify的使用

这里简单介绍一下Purify在Windows和UNIX环境下的使用。

在Windows中,只要运行Purify,填入需要分析的程序及参数就可。Purify会自动插入检测代码并显示报告。报告的格式如下(来自[DEV205]):

蓝色的图标代表一些运行的信息,比如开始和结束等。黄色是Purify给出的警告。通常UMR会作为警告列出。红色则代表严重的错误。每一种相同的错误,尤其是在循环中的,会被集中在一起显示,并且标明发生的次数。由每个错误的详细信息,用户可以知道相应的内存地址和源代码的位置,并直接修改。另外用户还可以设置不同的滤过器,用来隐藏暂时不关心的消息。

在UNIX系统中,使用Purify需要重新编译程序。通常的做法是修改Makefile中的编译器变量。下面是用来编译本文中程序的Makefile:

        CC=purify gcc
all: pplusdemo
pplusdemo: pplusdemo.o$(CC) -o pplusdemo pplusdemo.o -lstdc++
pplusdemo.o: pplusdemo.cpp$(CC) -g -c -w pplusdemo.cpp
clean:-rm pplusdemo pplusdemo.o

首先运行Purify安装目录下的purifyplus_setup.sh来设置环境变量,然后运行make重新编译程序。需要指出的是,程序必须编译成调试版本。在gcc中,也就是必须使用"-g"选项。在重新编译的程序运行结束后,Purify会打印出一个分析报告。它的格式和含义与Windows平台大同小异。

下面是本文中的程序在Linux上Purify运行的结果:

    ****  Purify instrumented ./pplusdemo (pid 30669)  ****
UMR: Uninitialized memory read:* This is occurring while in:strlen         [rtlib.o]std::basic_ostream< char,std::char_traits< char>> & std::operator<<<std::char_traits< char>>(std::basic_ostream< char,std::char_traits<char>> &, char const *) [libstdc++.so.5]main           [pplusdemo.cpp:7]__libc_start_main [libc.so.6]_start         [crt1.o]* Reading 1 byte from 0x80b45e0 in the heap.* Address 0x80b45e0 is at the beginning of a malloc'd block of 4 bytes.* This block was allocated from:malloc         [rtlib.o]operator new( unsigned) [libstdc++.so.5]operator new []( unsigned) [libstdc++.so.5]main           [pplusdemo.cpp:5]__libc_start_main [libc.so.6]_start         [crt1.o]
****  Purify instrumented ./pplusdemo (pid 30669)  ****
ABW: Array bounds write:* This is occurring while in:strcpy         [rtlib.o]main           [pplusdemo.cpp:8]__libc_start_main [libc.so.6]_start         [crt1.o]* Writing 5 bytes to 0x80b45e0 in the heap (1 byte at 0x80b45e4 illegal).* Address 0x80b45e0 is at the beginning of a malloc'd block of 4 bytes.* This block was allocated from:malloc         [rtlib.o]operator new( unsigned) [libstdc++.so.5]operator new []( unsigned) [libstdc++.so.5]main           [pplusdemo.cpp:5]__libc_start_main [libc.so.6]_start         [crt1.o]
****  Purify instrumented ./pplusdemo (pid 30669)  ****
ABR: Array bounds read:* This is occurring while in:strlen         [rtlib.o]std::basic_ostream< char,std::char_traits< char>> & std::operator<<<std::char_traits< char>>(std::basic_ostream< char,std::char_traits<char>> &, char const *) [libstdc++.so.5]main           [pplusdemo.cpp:9]__libc_start_main [libc.so.6]_start         [crt1.o]* Reading 5 bytes from 0x80b45e0 in the heap (1 byte at 0x80b45e4 illegal).* Address 0x80b45e0 is at the beginning of a malloc'd block of 4 bytes.* This block was allocated from:malloc         [rtlib.o]operator new( unsigned) [libstdc++.so.5]operator new []( unsigned) [libstdc++.so.5]main           [pplusdemo.cpp:5]__libc_start_main [libc.so.6]_start         [crt1.o]
****  Purify instrumented ./pplusdemo (pid 30669)  ****
FMM: Freeing mismatched memory:* This is occurring while in:operator delete( void *) [rtlib.o]main           [pplusdemo.cpp:10]__libc_start_main [libc.so.6]_start         [crt1.o]* Attempting to free block at 0x80b45e0 in the heap.* Address 0x80b45e0 is at the beginning of a malloc'd block of 4 bytes.* This block was allocated from:malloc         [rtlib.o]operator new( unsigned) [libstdc++.so.5]operator new []( unsigned) [libstdc++.so.5]main           [pplusdemo.cpp:5]__libc_start_main [libc.so.6]_start         [crt1.o]* This block of memory was obtained using an allocation routine which isnot compatible with the routine by which it is being freed.
****  Purify instrumented ./pplusdemo (pid 30669)  ****
FMR: Free memory read:* This is occurring while in:main           [pplusdemo.cpp:11]__libc_start_main [libc.so.6]_start         [crt1.o]* Reading 1 byte from 0x80b45e0 in the heap.* Address 0x80b45e0 is at the beginning of a freed block of 4 bytes.* This block was allocated from:malloc         [rtlib.o]operator new( unsigned) [libstdc++.so.5]operator new []( unsigned) [libstdc++.so.5]main           [pplusdemo.cpp:5]__libc_start_main [libc.so.6]_start         [crt1.o]* There have been 0 frees since this block was freed from:free           [rtlib.o]_ZdLpV         [libstdc++.so.5]main           [pplusdemo.cpp:10]__libc_start_main [libc.so.6]_start         [crt1.o]
****  Purify instrumented ./pplusdemo (pid 30669)  ****
FMW: Free memory write:* This is occurring while in:main           [pplusdemo.cpp:11]__libc_start_main [libc.so.6]_start         [crt1.o]* Writing 1 byte to 0x80b45e0 in the heap.* Address 0x80b45e0 is at the beginning of a freed block of 4 bytes.* This block was allocated from:malloc         [rtlib.o]operator new( unsigned) [libstdc++.so.5]operator new []( unsigned) [libstdc++.so.5]main           [pplusdemo.cpp:5]__libc_start_main [libc.so.6]_start         [crt1.o]* There have been 0 frees since this block was freed from:free           [rtlib.o]_ZdLpV         [libstdc++.so.5]main           [pplusdemo.cpp:10]__libc_start_main [libc.so.6]_start         [crt1.o]
****  Purify instrumented ./pplusdemo (pid 30669)  ****
FUM: Freeing unallocated memory:* This is occurring while in:free           [rtlib.o]_ZdLpV         [libstdc++.so.5]main           [pplusdemo.cpp:12]__libc_start_main [libc.so.6]_start         [crt1.o]* Attempting to free block at 0x80b45e0 already freed.* This block was allocated from:malloc         [rtlib.o]operator new( unsigned) [libstdc++.so.5]operator new []( unsigned) [libstdc++.so.5]main           [pplusdemo.cpp:5]__libc_start_main [libc.so.6]_start         [crt1.o]* There have been 1 frees since this block was freed from:free           [rtlib.o]_ZdLpV         [libstdc++.so.5]main           [pplusdemo.cpp:10]__libc_start_main [libc.so.6]_start         [crt1.o]
****  Purify instrumented ./pplusdemo (pid 30669)  ****
Current file descriptors in use: 5
FIU: file descriptor 0: <stdin>
FIU: file descriptor 1: <stdout>
FIU: file descriptor 2: <stderr>
FIU: file descriptor 26: <reserved for Purify internal use>
FIU: file descriptor 27: <reserved for Purify internal use>
****  Purify instrumented ./pplusdemo (pid 30669)  ****
Purify: Searching for all memory leaks...
Memory leaked: 0 bytes (0%); potentially leaked: 0 bytes (0%)
Purify Heap Analysis (combining suppressed and unsuppressed blocks)Blocks        BytesLeaked          0            0Potentially Leaked          0            0In-Use          0            0----------------------------------------Total Allocated          0            0
****  Purify instrumented ./pplusdemo (pid 30669)  ***** Program exited with status code 0.* 7 access errors, 7 total occurrences.* 0 bytes leaked.* 0 bytes potentially leaked.* Basic memory usage (including Purify overhead):290012 code152928 data/bss6816 heap (peak use)7800 stack

我们对照程序可以发现Purify查出了程序中所有的错误。对于每个错误,她不但给出了源代码的位置还指出这些内存最初分配的源代码位置。这对于查找问题提供了很大帮助。对于程序12行的解释,Purify将其认为是不匹配的内存释放(FMM: Freeing mismatched memory),因为她认为这样的释放方式不符合严格的规定。

Purify在其报告和文档中使用了很多的缩写,在此一并列出,以便读者在使用时参考(来自[Purify]):

2.3 Purify的一些特性

这里简单介绍一下Purify提供的几个特性。有关这些特性的详细信息,请查阅文档[Purify]。

  • 观察点(Watchpoint):通过在程序或者调试器中调用Purify 提供的观察点函数,Purify可以报告有关被观察对象的读写或其他操作。
  • 与Rational其他产品的集成:在Puify的用户界面中可以方便地进入ClearCase和ClearQuest。Purify还可以和PureCoverage同时使用,对程序进行分析。
  • Purify的定制:无论是Purify报告中的消息,还是界面中的元素,都可以进行一定程度的定制。另外通过修改配置文件和调用Purify API,用户还可以自动记录运行日志,发送电子邮件等。
  • Purify提供的API:为了更好地把Purify融合到自动化测试的体系中,Purify提供了一系列的公开函数。用户完全可以通过脚本的方式自动运行,记录,和分析Purify。

回页首

3.总结

当使用C/C++进行开发时,采用良好的一致的编程规范是防止内存问题第一道也是最重要的措施。在此前提下,IBM Rational Purify作为一种运行时分析软件可以很好地帮助您发现忽略的内存问题,或成为软件自动测试中的一个重要组成部分。

回页首

4.参考资料

[C++Adv]
[DEV205] Essentials of Rational PurifyPlus
[Purify] IBM Rational PurifyPlus for Linux and UNIX Documentation

补充阅读:

http://blog.csdn.net/haoel/archive/2003/12/16/2904.aspx

转载于:https://www.cnblogs.com/wangfengju/archive/2013/04/10/6173210.html

内存分析利器purify简介相关推荐

  1. linux java内存分析_Java内存分析利器MAT使用详解

    这是一篇阅读MAT helper的笔记.Heap dump是Java进程在特定时间的一个内存快照.通常在触发heap dump之前会进行一次full gc,这样dump出来的内容就包含的是被gc后的对 ...

  2. 一个golang编写的redis内存分析工具rma4go

    redis 内存分析工具 rma4go 简介 redis是一个很有名的内存型数据库,这里不做详细介绍.而rma4go (redis memory analyzer for golang) 是一个red ...

  3. C/C++内存问题检查利器——Purify

    C/C++内存问题检查利器--Purify 一.           引言 我们都知道软件的测试(在以产品为主的软件公司中叫做QA-Quality Assessment)占了整个软件工程的30% -5 ...

  4. 内存问题检查利器——Purify

    内存问题检查利器--Purify https://www.cnblogs.com/Leo_wl/p/7699489.html 一.           引言 我们都知道软件的测试(在以产品为主的软件公 ...

  5. C/C++内存问题检查利器—Purify (一)

    C/C++内存问题检查利器--Purify 陈皓 一.           引言 我们都知道软件的测试(在以产品为主的软件公司中叫做QA-Quality Assessment)占了整个软件工程的30% ...

  6. C/C++内存问题检查利器—Purify

    文章转自:http://blog.csdn.net/haoel/article/details/2900 一.           引言 我们都知道软件的测试(在以产品为主的软件公司中叫做QA-Qua ...

  7. jmap, jhat, jvisualvm:java堆内存对象分析利器

    转载自 jmap, jhat, jvisualvm:java堆内存对象分析利器 jmap -help查看命令用法. jmap -heap <pid> 查看堆使用情况. jmap -dump ...

  8. C/C++内存问题检查利器—Purify (二)

    三.           示例 假设我们现在有这样一段程序:hello.c #include <stdio.h> #include <malloc.h>   static ch ...

  9. 内存分析工具MAT的使用

    原文链接:http://www.jianshu.com/p/d8e247b1e7b2 MAT简介 MAT(Memory Analyzer Tool),一个基于Eclipse的内存分析工具,是一个快速. ...

最新文章

  1. mysql 赋权_Mysql赋权
  2. vue图片不存在时加载默认图片
  3. boost::stl_interfaces模块实现节点迭代器的测试程序
  4. 【转】ASP.NET Web API 使用Swagger生成在线帮助测试文档,支持多个GET
  5. C# 声明方法关键字
  6. 在Ubuntu上安装Jupyter Notebook
  7. php打印倒立金字塔,编写程序打印*字符形成的等腰三角形倒立金字塔图形 ******* ***** *** *...
  8. Android ButterKnife示例
  9. “一人单挑 BAT”,黑客张福:我要的东西,比钱更贵
  10. Windows Style Builder一些路径分享-2022.8.21(不定期更新)
  11. 使用mysql Installer安装失败处理办法
  12. 凭什么都是Java开发三年,而他能进大厂薪资是“我”2倍?
  13. SHELL脚本编程小程序
  14. html雪碧图效果,综合雪碧图
  15. 评论-ClickTracks 2.0
  16. 1119 Pre- and Post-order Traversals (PAT甲级)
  17. meate30pro刷鸿蒙,华为Mate40E和mate30pro哪个好-参数对比测评
  18. 【SEO工具】抓取百度关键词相关关键词挖掘工具
  19. MATLAB符号变量做矩阵运算出现conj()
  20. Android性能优化系列:启动优化

热门文章

  1. 数据可视化:视觉感知与基本图表
  2. H2N-Gly-Pro-Glu-COOH,32302-76-4
  3. 官方自曝?新款iMac和iPad Pro等产品将于5月21日发售
  4. IPv6地址规划案例
  5. 【专业数据】六.2020~2022年北京交通大学【新一代电子信息技术】专业复试线/分数线差/计划招生数/复试数/录取数/复试比例/录取率
  6. DS1302时钟芯片时序
  7. ds1302模块 树莓派_Arduino控制DS1302时钟芯片(ds1302引脚图及功能和应用电路)
  8. MyErp-人事管理系统的问题总结
  9. Redis的过期键删除策略和数据逐出策略
  10. 大数据在应急管理中的应用