C/C++等底层语言在提供强大功能及性能的同时,其灵活的内存访问也带来了各种纠结的问题。如果crash的地方正是内存使用错误的地方,说明你人品好。如果crash的地方内存明显不是consistent的,或者内存管理信息都已被破坏,编译器不能发现这些问题,.运行时才能捕获到这些错误并且还是随机出现的,那就比较麻烦了。当然,祼看code打log是一个办法,但其效率不是太高,尤其是在运行成本高或重现概率低的情况下。另外,静态检查也是一类方法,有很多工具(lint, cppcheck, klockwork, splint, etc.)。但缺点是误报很多,不适合针对性问题。另外好点的一般还要钱。最后,就是动态检查工具。下面介绍几个Linux平台下主要的运行时内存检查工具。绝大多数都是开源免费且支持x86和ARM平台的。

首先,比较常见的内存问题有下面几种:
• memory overrun:写内存越界(越界访问堆、栈和全局变量)
• double free:同一块内存释放两次
• use after free:内存释放后使用
• wild free:释放内存的参数为非法值
• access uninitialized memory:访问未初始化内存
• read invalid memory:读取非法内存,本质上也属于内存越界
• memory leak:内存泄露
• use after return:caller访问一个指针,该指针指向callee的栈内内存
• stack overflow:栈溢出

针对上面的问题,主要有以下几种方法:
1. 为了检测内存非法使用,需要hook内存分配和操作函数。hook的方法可以是用C-preprocessor,也可以是在链接库中直接定义(因为Glibc中的malloc/free等函数都是weak symbol),或是用LD_PRELOAD。另外,通过hook strcpy(),memmove()等函数可以检测它们是否引起buffer overflow。
2. 为了检查内存的非法访问,需要对程序的内存进行bookkeeping,然后截获每次访存操作并检测是否合法。bookkeeping的方法大同小异,主要思想是用shadow memory来验证某块内存的合法性。至于instrumentation的方法各种各样。有run-time的,比如通过把程序运行在虚拟机中或是通过binary translator来运行;或是compile-time的,在编译时就在访存指令时就加入检查操作。另外也可以通过在分配内存前后加设为不可访问的guard page,这样可以利用硬件(MMU)来触发SIGSEGV,从而提高速度。
3. 为了检测栈的问题,一般在stack上设置canary,即在函数调用时在栈上写magic number或是随机值,然后在函数返回时检查是否被改写。另外可以通过mprotect()在stack的顶端设置guard page,这样栈溢出会导致SIGSEGV而不至于破坏数据。

以下是几种常用工具在linux x86_64平台的实验结果,注意其它平台可能结果有差异。

Tool\Problem memory overrun double free use after free wild free access uninited read invalid memory memory leak use after return stack overflow
Memory checking tools in Glibc   Yes   Yes     Yes   Yes(if use memcpy, strcpy, etc)
Valgrind Yes Yes Yes Yes Yes Yes Yes Yes Yes
Memwatch   Yes   Yes     Yes    
Dmalloc Yes Yes Yes Yes     Yes    

下面简单介绍一下这些工具以及基本用法。更详细用法请参见各自manual。

valgrind通常用来成分析程序性能及程序中的内存泄露等错误

一 Valgrind工具集介绍

Valgrind包含下列工具:

1、memcheck:检查程序中的内存问题,如泄漏、越界、非法指针等。

2、callgrind:检测程序代码的运行时间和调用过程,以及分析程序性能。

3、cachegrind:分析CPU的cache命中率、丢失率,用于进行代码优化。

4、helgrind:用于检查多线程程序的竞态条件。

5、massif:堆栈分析器,指示程序中使用了多少堆内存等信息。

这几个工具的使用是通过命令:valgrand --tool=name 程序名来分别调用的,当不指定tool参数时默认是 --tool=memcheck

二 Valgrind工具详解

1.Memcheck

最常用的工具,用来检测程序中出现的内存问题,所有对内存的读写都会被检测到,一切对malloc、free、new、delete的调用都会被捕获。所以,它能检测以下问题:

1、对未初始化内存的使用;

2、读/写释放后的内存块;

3、读/写超出malloc分配的内存块;

4、读/写不适当的栈中内存块;

5、内存泄漏,指向一块内存的指针永远丢失;

6、不正确的malloc/free或new/delete匹配;

7、memcpy()相关函数中的dst和src指针重叠。

这些问题往往是C/C++程序员最头疼的问题,Memcheck能在这里帮上大忙。
例如:

[plain] view plaincopy
  1. #include <stdlib.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4. void test()
  5. {
  6. int *ptr = malloc(sizeof(int)*10);
  7. ptr[10] = 7; // 内存越界
  8. memcpy(ptr +1, ptr, 5); // 踩内存
  9. free(ptr);
  10. free(ptr);// 重复释放
  11. int *p1;
  12. *p1 = 1; // 非法指针
  13. }
  14. int main(void)
  15. {
  16. test();
  17. return 0;
  18. }

将程序编译生成可执行文件后执行:valgrind --leak-check=full ./程序名

输出结果如下:

==4832== Memcheck, a memory error detector
==4832== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==4832== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info
==4832== Command: ./tmp
==4832== 
==4832== Invalid write of size 4      // 内存越界
==4832==    at 0x804843F: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==  Address 0x41a6050 is 0 bytes after a block of size 40 alloc'd
==4832==    at 0x4026864: malloc (vg_replace_malloc.c:236)
==4832==    by 0x8048435: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== 
==4832== Source and destination overlap in memcpy(0x41a602c, 0x41a6028, 5) // 踩内存
==4832==    at 0x4027BD6: memcpy (mc_replace_strmem.c:635)
==4832==    by 0x8048461: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== 
==4832== Invalid free() / delete / delete[] // 重复释放
==4832==    at 0x4025BF0: free (vg_replace_malloc.c:366)
==4832==    by 0x8048477: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==  Address 0x41a6028 is 0 bytes inside a block of size 40 free'd
==4832==    at 0x4025BF0: free (vg_replace_malloc.c:366)
==4832==    by 0x804846C: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== 
==4832== Use of uninitialised value of size 4 // 非法指针
==4832==    at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== 
==4832== 
==4832== Process terminating with default action of signal 11 (SIGSEGV) //由于非法指针赋值导致的程序崩溃
==4832==  Bad permissions for mapped region at address 0x419FFF4
==4832==    at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)
==4832== 
==4832== HEAP SUMMARY:
==4832==     in use at exit: 0 bytes in 0 blocks
==4832==   total heap usage: 1 allocs, 2 frees, 40 bytes allocated
==4832== 
==4832== All heap blocks were freed -- no leaks are possible
==4832== 
==4832== For counts of detected and suppressed errors, rerun with: -v
==4832== Use --track-origins=yes to see where uninitialised values come from
==4832== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 11 from 6)
Segmentation fault

从valgrind的检测输出结果看,这几个错误都找了出来。

2.Callgrind

和gprof类似的分析工具,但它对程序的运行观察更是入微,能给我们提供更多的信息。和gprof不同,它不需要在编译源代码时附加特殊选项,但加上调试选项是推荐的。Callgrind收集程序运行时的一些数据,建立函数调用关系图,还可以有选择地进行cache模拟。在运行结束时,它会把分析数据写入一个文件。callgrind_annotate可以把这个文件的内容转化成可读的形式。

生成可视化的图形需要下载gprof2dot:http://jrfonseca.googlecode.com/svn/trunk/gprof2dot/gprof2dot.py

这是个Python脚本,把它下载之后修改其权限chmod +7 gprof2dot.py ,并把这个脚本添加到$PATH路径中的任一文件夹下,我是将它放到了/usr/bin目录下,这样就可以直接在终端下执行gprof2dot.py了。

Callgrind可以生成程序性能分析的图形,首先来说说程序性能分析的工具吧,通常可以使用gnu自带的gprof,它的使用方法是:在编译程序时添加-pg参数,例如:

[plain] view plaincopy
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. void test()
  4. {
  5. sleep(1);
  6. }
  7. void f()
  8. {
  9. int i;
  10. for( i = 0; i < 5; i ++)
  11. test();
  12. }
  13. int main()
  14. {
  15. f();
  16. printf("process is over!\n");
  17. return 0;
  18. }

首先执行 gcc -pg -o tmp tmp.c,然后运行该程序./tmp,程序运行完成后会在当前目录下生成gmon.out文件(这个文件gprof在分析程序时需要),
再执行gprof ./tmp | gprof2dot.py |dot -Tpng -o report.png,打开report.png结果:

显示test被调用了5次,程序中耗时所占百分比最多的是test函数。

再来看 Callgrind的生成调用图过程吧,执行:valgrind --tool=callgrind ./tmp,执行完成后在目录下生成"callgrind.out.XXX"的文件这是分析文件,可以直接利用:callgrind_annotate callgrind.out.XXX 打印结果,也可以使用:gprof2dot.py -f callgrind callgrind.out.XXX |dot -Tpng -o report.png 来生成图形化结果:

它生成的结果非常详细,甚至连函数入口,及库函数调用都标识出来了。

3.Cachegrind

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

作一下广告:valgrind自身利用该工具在过去几个月内使性能提高了25%-30%。据早先报道,kde的开发team也对valgrind在提高kde性能方面的帮助表示感谢。

它的使用方法也是:valgrind --tool=cachegrind 程序名,

4.Helgrind

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

首先举一个竞态的例子吧:

[plain] view plaincopy
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #define NLOOP 50
  4. int counter = 0; /* incremented by threads */
  5. void *threadfn(void *);
  6. int main(int argc, char **argv)
  7. {
  8. pthread_t tid1, tid2,tid3;
  9. pthread_create(&tid1, NULL, &threadfn, NULL);
  10. pthread_create(&tid2, NULL, &threadfn, NULL);
  11. pthread_create(&tid3, NULL, &threadfn, NULL);
  12. /* wait for both threads to terminate */
  13. pthread_join(tid1, NULL);
  14. pthread_join(tid2, NULL);
  15. pthread_join(tid3, NULL);
  16. return 0;
  17. }
  18. void *threadfn(void *vptr)
  19. {
  20. int i, val;
  21. for (i = 0; i < NLOOP; i++) {
  22. val = counter;
  23. printf("%x: %d \n", (unsigned int)pthread_self(),  val+1);
  24. counter = val+1;
  25. }
  26. return NULL;
  27. }

这段程序的竞态在30~32行,我们想要的效果是3个线程分别对全局变量累加50次,最后全局变量的值为150,由于这里没有加锁,很明显竞态使得程序不能达到我们的目标。我们来看Helgrind是如何帮我们检测到竞态的。先编译程序:gcc -o test thread.c -lpthread ,然后执行:valgrind --tool=helgrind ./test 输出结果如下:

49c0b70: 1 
49c0b70: 2 
==4666== Thread #3 was created
==4666==    at 0x412E9D8: clone (clone.S:111)
==4666==    by 0x40494B5: pthread_create@@GLIBC_2.1 (createthread.c:256)
==4666==    by 0x4026E2D: pthread_create_WRK (hg_intercepts.c:257)
==4666==    by 0x4026F8B: pthread_create@* (hg_intercepts.c:288)
==4666==    by 0x8048524: main (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666== 
==4666== Thread #2 was created
==4666==    at 0x412E9D8: clone (clone.S:111)
==4666==    by 0x40494B5: pthread_create@@GLIBC_2.1 (createthread.c:256)
==4666==    by 0x4026E2D: pthread_create_WRK (hg_intercepts.c:257)
==4666==    by 0x4026F8B: pthread_create@* (hg_intercepts.c:288)
==4666==    by 0x8048500: main (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666== 
==4666== Possible data race during read of size 4 at 0x804a028 by thread #3
==4666==    at 0x804859C: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666==    by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)
==4666==    by 0x4048E98: start_thread (pthread_create.c:304)
==4666==    by 0x412E9ED: clone (clone.S:130)
==4666==  This conflicts with a previous write of size 4 by thread #2
==4666==    at 0x80485CA: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666==    by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)
==4666==    by 0x4048E98: start_thread (pthread_create.c:304)
==4666==    by 0x412E9ED: clone (clone.S:130)
==4666== 
==4666== Possible data race during write of size 4 at 0x804a028 by thread #2
==4666==    at 0x80485CA: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666==    by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)
==4666==    by 0x4048E98: start_thread (pthread_create.c:304)
==4666==    by 0x412E9ED: clone (clone.S:130)
==4666==  This conflicts with a previous read of size 4 by thread #3
==4666==    at 0x804859C: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)
==4666==    by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)
==4666==    by 0x4048E98: start_thread (pthread_create.c:304)
==4666==    by 0x412E9ED: clone (clone.S:130)
==4666== 
49c0b70: 3 
......
55c1b70: 51 
==4666== 
==4666== For counts of detected and suppressed errors, rerun with: -v
==4666== Use --history-level=approx or =none to gain increased speed, at
==4666== the cost of reduced accuracy of conflicting-access information
==4666== ERROR SUMMARY: 8 errors from 2 contexts (suppressed: 99 from 31)

helgrind成功的找到了竞态的所在位置,标红所示。

5. Massif

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

Massif对内存的分配和释放做profile。程序开发者通过它可以深入了解程序的内存使用行为,从而对内存使用进行优化。这个功能对C++尤其有用,因为C++有很多隐藏的内存分配和释放。

此外,lackey和nulgrind也会提供。Lackey是小型工具,很少用到;Nulgrind只是为开发者展示如何创建一个工具。我们就不做介绍了。

三 使用Valgrind

Valgrind使用起来非常简单,你甚至不需要重新编译你的程序就可以用它。当然如果要达到最好的效果,获得最准确的信息,还是需要按要求重新编译一下的。比如在使用memcheck的时候,最好关闭优化选项。

valgrind命令的格式如下:

valgrind [valgrind-options] your-prog [your-prog options]

valgrind --tool=massif --stacks=yes ./test

(这个工具有个bug, 只有程序中出现new或者malloc之类的堆操作,才会统计栈的使用,否则只统计堆的使用)

一些常用的选项如下:

选项

作用

-h --help

显示帮助信息。

--version

显示valgrind内核的版本,每个工具都有各自的版本。

-q --quiet

安静地运行,只打印错误信息。

-v --verbose

打印更详细的信息。

--tool=<toolname> [default: memcheck]

最常用的选项。运行valgrind中名为toolname的工具。如果省略工具名,默认运行memcheck。

--db-attach=<yes|no> [default: no]

绑定到调试器上,便于调试错误。

注意:

1、对于项目工程测试适合使用Valgrind和mtrace,而单元测试选择Dmalloc和memwatch。

2、inux中从打印出的地址找到行号,执行
addr2line -e test 0x40053E

如果可执行文件错误,您将获得??:?作为响应。

如果可执行文件中没有包括调试符号,您将获得??:0 作为响应。

3、交叉编译valgrind步骤

1)配置./configure --host=arm-linux CC=arm-histbv310-linux-gcc --prefix=/opt/valgrind

2)安装make&&make install

3)将valgrind拷贝到开发板的opt目录下 : cp /opt/valgrind /opt/ -r

4)将opt设置为开发板全局变量或在valgrind/bin目录下使用内存泄露检测memcheck 和性能分析callgrind 工具,操作方法和linux的使用相同

4、对于arm交叉编译版本不要使用arm-linux-gcc 的strip工具来处理根文件系统的库文件,保留二进制文件中的包含的符号表和调试信息。

否则执行memcheck的工具时,出现以下错误:

4、把callgrind.out.3131传到 window 7 ,

如果要时行源代码显示,XX.C也传到windows 7

用kcachegrind 打开callgrind.out.3131 ,即显示下面图

  LINUX: http://kcachegrind.sourceforge.net/html/Home.html

windows: https://sourceforge.net/projects/precompiledbin/files/?source=navbar

参考博客

Valgrind使用说明
http://www.cnblogs.com/wangkangluo1/archive/2011/07/20/2111248.html
linux下利用valgrind工具进行内存泄露检测和性能分析
http://blog.csdn.net/yanghao23/article/details/7514587
Linux中的常用内存问题检测工具
http://blog.csdn.net/jinzhuojun/article/details/46659155

arm linux下交叉编译valgrind工具进行内存泄露检测和性能分析相关推荐

  1. linux下利用valgrind工具进行内存泄露检测和性能分析

    http://blog.csdn.net/yanghao23/article/details/7514587 valgrind通常用来成分析程序性能及程序中的内存泄露错误 一 Valgrind工具集简 ...

  2. Linux valgrind java_linux下利用valgrind工具进行内存泄露检测和性能分析

    valgrind通常用来成分析程序性能及程序中的内存泄露错误 一 Valgrind工具集简绍 Valgrind包含下列工具: 1.memcheck:检查程序中的内存问题,如泄漏.越界.非法指针等. 2 ...

  3. Valgrind ---内存调试,内存泄漏检测以及性能分析的软件开发工具

    Valgrind是一款用于内存调试.内存泄漏检测以及性能分析的软件开发工具.Valgrind这个名字取自北欧神话中英灵殿的入口. 一般使用方式  valgrind --leak-check=full ...

  4. linux下交叉编译strace工具

    1. strace工具最新源码包下载 github下载地址: https://github.com/strace/strace 2. 执行bootstrap,生成configure文件 root@:/ ...

  5. Unix下C程序内存泄露检测工具:valgrind的安装使用

    Valgrind是一款用于内存调试.内存泄漏检测以及性能分析的软件开发工具. Valgrind的最初作者是Julian Seward,他于2006年由于在开发Valgrind上的工作获得了第二届Goo ...

  6. 内存泄露检测方案和代码实现

    文章目录 1 内存泄露核心及原因 2 内存泄露的危害 3 常用的检查内存泄露的工具 4 内存泄露检测的4种方案和代码实现 内存泄露检测围绕两个问题解决 定位到代码中行的方法 方法1 方法2 方法3 ( ...

  7. Ubuntu下内存泄露检测工具Valgrind的使用

    在VS中可以用VLD检测是否有内存泄露,可以参考http://blog.csdn.net/fengbingchun/article/details/44195959,下面介绍下Ubuntu中内存泄露检 ...

  8. 防止内存泄露 Linux下用Valgrind做检查

    Valgrind技术参考 http://valgrind.org/docs/manual/tech-docs.html 防止内存泄露 Linux下用Valgrind做检查 发布时间:2007.10.2 ...

  9. ARM Linux下UPnP使用

    ARM Linux下UPnP使用   一.UPnP简介   UPnP(Universal Plug and Play)技术是一种屏蔽各种数字设备的硬件和操作系统的通信协议.它是一种数字网络中间件技术, ...

最新文章

  1. 传图识字有次数限制吗_5岁娃识字3000?别羡慕!过早逼娃认字,后果很严重
  2. Ubuntu下SSH设置
  3. XenApp6 建立请求的连接时出错解决方法
  4. 树莓派作为客户端与WemosD1作为服务器的无线通信(局域网通信)
  5. javascript函数调用的各种方法!!
  6. 如何过滤某一频率的声音
  7. mysql数据=_mysql 数据操作
  8. 魔术师利用一副牌中的13张红桃c语言,魔术师的猜牌术(1) 魔术师利用一副牌中的13张黑桃 - 下载 - 搜珍网...
  9. 腾讯回应“暴力裁员”;小米否认常程与联想签有竞业禁止条款;NumPy 1.16.6 发布 | 极客头条...
  10. c语言程序怎样输出一个图形,用c语言如何输出并显示一个序列图形
  11. 如何使用手机通过校园无线网在知网免费下载论文
  12. 决策树(2)——CART算法
  13. ValueError: Error initializing torch.distributed using tcp:// rendezvous: port number missing
  14. 第十六届全国大学生智能车竞赛创意组获奖名单 | 带有学生与指导教师信息
  15. 针对OpenSSL吐嘈的吐嘈-如此唱反调
  16. 7-5 判断某整数是正整数、负整数还是零 (6分)
  17. c++内存分区、创建变量开辟内存
  18. win10在此计算机上找不到系统映像,解决Win10系统Windows找不到文件确定是否正确...
  19. java hid_Java HIDAPI - 异步读取
  20. 7-4 莫尔斯码(Morse Code) (25 分)

热门文章

  1. 定区关联快递员 定区关联收派时间
  2. riot.js教程【二】组件撰写准则、预处理器、标签样式和装配方法
  3. 大数据技术在发展 挑战与机遇并存
  4. Linux命令(二)
  5. 笔记本安装CentOS环境
  6. SQL2K数据库开发七之表操作添加删除和修改列
  7. [转载]遗留系统中的RUBY中文解决方案
  8. 一段MYSQL的插入代码
  9. spark++读写mysql_spark读写mysql
  10. python redis 订阅发布_【Python之旅】第七篇(三):使用Redis订阅服务