Chinaunix首页 | 论坛 | 博客
  • 博客访问: 359739
  • 博文数量: 66
  • 博客积分: 3201
  • 博客等级: 中校
  • 技术积分: 695
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-04 11:17
文章分类

全部博文(66)

文章存档

2016年(1)

2014年(1)

2012年(1)

2011年(2)

2010年(18)

2009年(42)

2008年(1)

分类: LINUX

2009-02-10 18:18:44

1)LeakTracer
一,LeakTracer
    1,LeakTracer是一个用于检查c++程序内存泄漏的小工具
    2,基于gdb和gcc的__builtin_return_address()函数实现
        __builtin_return_address(LEVEL)
        LEVEL=0:返回调用者的返回地址
        LEVEL=1:返回调用者的调用者的返回地址
       
二,工具的使用
    1,LeakCheak和leak-analyze为两个主要的程序
    2,LeakCheak对可执行文件进行检查,生成report文件leak.out
    3,leak-analyze分析生成的leak.out,最终输出可读性更强的信息
    (信息包括在哪里new/new[],在哪里delete)
   
三,加入功能
    加入了对malloc/free的检查(LeakTracer-mf.cc)
 
2)Valgrind
    Valgrind是x86架构Linux上的多重用途代码剖析和内存调试工具。你可以在它的环境中运行你的程序来监视内存的使用情况,比如C语言中的malloc和free或者C++中的new和delete。
   
一,用Valgrind查找内存泄漏
Valgrind支持很多工具:memcheck,addrcheck,cachegrind,massif,helgrind和callgrind等。在运行Valgrind时,你必须指明想用的工具。
通过命令:
%valgrind --tool=tool_name program_name可以对program_name执行程序运行不同的分析工具。
它的输出结果:
==18515== malloc/free: in use at exit: 0 bytes in 0 blocks.
==18515== malloc/free: 1 allocs, 1 frees, 10 bytes allocated.
==18515== For a detailed leak analysis, rerun with: --leak-check=yes
如果程序中有内存泄漏的现象,内存分配的数量和内存释放的数量会不一致(你不能使用一个free调用来释放多个分配的内存)。
如果程序内存分配和释放的数量不一致,你可以加上leak-check参数重新运行程序,这样就可以看见分配了内存但却没有释放的代码。
可以加上--leak-check=yes对没有释放的内存检查:
%valgrind --tool=memcheck --leak-check=yes program
==2330== 100 bytes in 1 blocks are definitely lost in loss record 1 of 1
==2330== at 0x1B900DD0: malloc (vg_replace_malloc.c:131)
==2330== by 0x804840F: main (example1.c:5)
不过在编译时加上-g选项,输出的信息更多一些!
二,内存泄漏的其他信息
    用memcheck工具,Valgrind也可以找出无效堆内存使用。
    还有一类Valgrind可以检测的操作是在条件判断语句中使用未初始化变量。
    Valgrind还能发现其它不正确使用内存的错误:如果你对同一块内存释放了两次,Valgrind就会探测到,而你则得到非法free的调用栈信息。
    Valgrind不对静态数组(分配在栈上)进行边界检查。
 
3)ElectricFence
ElectricFence是用于 C 语言编程和调试的工具。 ElectricFence 使用您的系统中的虚拟内存硬件来检测软件何时溢出 malloc() 缓冲边界,它还可用来检测由 free() 释放的多余内存。 ElectricFence 将会在导致出界违例的第一个指令处停止,然后您便可以用您首选的调试程序来显示出错之处。
Electric Fence是一个库可以用来C编程和调试. 在编译时连接它, 它会警告您可能的错误, 如没有内存等.
Electric Fence (efence) stops your program on the exact instruction that overruns (or underruns) a malloc() memory buffer. GDB will then display the source-code line that causes the bug. It works by using the virtual-memory hardware to create a red-zone at the border of each buffer - touch that, and your program stops. Catch all of those formerly impossible-to-catch overrun bugs that have been bothering you for years.
Electric Fence is a different kind of malloc() debugger. It uses the virtual memory hardware of your system to detect when software overruns the boundaries of a malloc() buffer. It will also detect any accesses of memory that has been released by free(). Because it uses the VM hardware for detection, Electric Fence stops your program on the first instruction that causes a bounds violation. It's then trivial to use a debugger to display the offending statement.
 
根据以上相关说明,应试是在使用时把安装文件编译后生成的库连接到程序中,当malloc/free发生错误时将产生一个虚拟硬件中断,使程序停止,再结合gdb跟踪调试定位malloc/free的错误!
阅读(708) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~