使用valgrind检测Android native程序的内存

原创 2012年03月27日 17:44:36

下载http://valgrind.org/downloads/valgrind-3.7.0.tar.bz2,使用ndk toolchain,按照代码中的README.android编译Android版本的valgrind,push到/data分区,这里笔者push到了/data/local/valgrind/,同时把VALGRIND_LIB 环境变量设置为/data/local/valgrind/lib/valgrind

编写一个有很多内存错误的程序:

main()
{
        {   
                int x;
                printf ("x = %d\n", x); 
        }   
        {   
                char* arr  = malloc(10);
                int*  arr2 = malloc(sizeof(int));
                write( 1 /* stdout */, arr, 10 );
        }   
        {   
                char a[100];
                memcpy(a, a + 20, 40);
        }   
        {   
                char *q; 
                q = malloc(1024*1024);

                q[1] = 1024;
        }   
        {   
                char *p; 
                p = malloc(1024*1024);

                p[0] = p[0];
                p[1] = 1024;

                free(p);
                free(p);
        }   
}

使用valgrind运行之:

/data/local/valgrind/bin/valgrind --leak-check=full --track-origins=yes  /data/check


得到如下结果:

==965== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==965== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==965== Command: /data/check
==965== 
==965== Conditional jump or move depends on uninitialised value(s)
==965==    at 0xAFD1AF6A: vfprintf (in /system/lib/libc.so)
==965==  Uninitialised value was created by a stack allocation
==965==    at 0x83D8: main (check.c:2)
==965== 
==965== Conditional jump or move depends on uninitialised value(s)
==965==    at 0xAFD1B3FA: vfprintf (in /system/lib/libc.so)
==965==  Uninitialised value was created by a stack allocation
==965==    at 0x83D8: main (check.c:2)
==965== 
==965== Conditional jump or move depends on uninitialised value(s)
==965==    at 0xAFD1B3FE: vfprintf (in /system/lib/libc.so)
==965==  Uninitialised value was created by a stack allocation
==965==    at 0x83D8: main (check.c:2)
==965== 
==965== Conditional jump or move depends on uninitialised value(s)
==965==    at 0xAFD1B478: vfprintf (in /system/lib/libc.so)
==965==  Uninitialised value was created by a stack allocation
==965==    at 0x83D8: main (check.c:2)
==965== 
==965== Conditional jump or move depends on uninitialised value(s)
==965==    at 0xAFD1B47E: vfprintf (in /system/lib/libc.so)
==965==  Uninitialised value was created by a stack allocation
==965==    at 0x83D8: main (check.c:2)
==965== 
==965== Conditional jump or move depends on uninitialised value(s)
==965==    at 0xAFD0FE00: __udivdi3 (in /system/lib/libc.so)
==965==  Uninitialised value was created by a stack allocation
==965==    at 0x83D8: main (check.c:2)
==965== 
==965== Conditional jump or move depends on uninitialised value(s)
==965==    at 0xAFD0D230: __udivsi3 (in /system/lib/libc.so)
==965==  Uninitialised value was created by a stack allocation
==965==    at 0x83D8: main (check.c:2)
==965== 
==965== Conditional jump or move depends on uninitialised value(s)
==965==    at 0xAFD0D294: __udivsi3 (in /system/lib/libc.so)
==965==  Uninitialised value was created by a stack allocation
==965==    at 0x83D8: main (check.c:2)
==965== 
==965== Conditional jump or move depends on uninitialised value(s)
==965==    at 0xAFD0FE5C: __udivdi3 (in /system/lib/libc.so)
==965==  Uninitialised value was created by a stack allocation
==965==    at 0x83D8: main (check.c:2)
==965== 
==965== Conditional jump or move depends on uninitialised value(s)
==965==    at 0xAFD0FEAC: __udivdi3 (in /system/lib/libc.so)
==965==  Uninitialised value was created by a stack allocation
==965==    at 0x83D8: main (check.c:2)
==965== 
==965== Syscall param write(buf) points to uninitialised byte(s)
==965==    at 0xAFD0B47C: write (in /system/lib/libc.so)
==965==  Address 0x480a058 is 0 bytes inside a block of size 10 alloc'd
==965==    at 0x80103318: malloc (vg_replace_malloc.c:263)
==965==    by 0x83F7: main (check.c:8)
==965==  Uninitialised value was created by a heap allocation
==965==    at 0x80103318: malloc (vg_replace_malloc.c:263)
==965==    by 0x83F7: main (check.c:8)
==965== 
==965== Source and destination overlap in memcpy(0xbde6b908, 0xbde6b91c, 40)
==965==    at 0x80106A64: memcpy (mc_replace_strmem.c:838)
==965==    by 0x843B: main (check.c:14)
==965== 
==965== Invalid free() / delete / delete[] / realloc()
==965==    at 0x80102E1C: free (vg_replace_malloc.c:427)
==965==    by 0x849B: main (check.c:30)
==965==  Address 0x490a100 is 0 bytes inside a block of size 1,048,576 free'd
==965==    at 0x80102E1C: free (vg_replace_malloc.c:427)
==965==    by 0x8493: main (check.c:29)
==965== 
==965== 
==965== HEAP SUMMARY:
==965==     in use at exit: 1,052,686 bytes in 4 blocks
==965==   total heap usage: 5 allocs, 2 frees, 2,101,262 bytes allocated
==965== 
==965== 4 bytes in 1 blocks are definitely lost in loss record 1 of 4
==965==    at 0x80103318: malloc (vg_replace_malloc.c:263)
==965==    by 0x8407: main (check.c:9)
==965== 
==965== 10 bytes in 1 blocks are definitely lost in loss record 2 of 4
==965==    at 0x80103318: malloc (vg_replace_malloc.c:263)
==965==    by 0x83F7: main (check.c:8)
==965== 
==965== 1,048,576 bytes in 1 blocks are definitely lost in loss record 4 of 4
==965==    at 0x80103318: malloc (vg_replace_malloc.c:263)
==965==    by 0x8443: main (check.c:18)
==965== 
==965== LEAK SUMMARY:
==965==    definitely lost: 1,048,590 bytes in 3 blocks
==965==    indirectly lost: 0 bytes in 0 blocks
==965==      possibly lost: 0 bytes in 0 blocks
==965==    still reachable: 4,096 bytes in 1 blocks
==965==         suppressed: 0 bytes in 0 blocks
==965== Reachable blocks (those to which a pointer was found) are not shown.
==965== To see them, rerun with: --leak-check=full --show-reachable=yes
==965== 
==965== For counts of detected and suppressed errors, rerun with: -v
==965== ERROR SUMMARY: 275 errors from 16 contexts (suppressed: 0 from 0)

这些错误可以分为如下几类:

  1. Illegal read / Illegal write errors
  2. Use of uninitialised values
  3. Use of uninitialised or unaddressable values in system calls
  4. Illegal frees
  5. When a heap block is freed with an inappropriate deallocation function
  6. Overlapping source and destination blocks
  7. Memory leak detection



Linux:Valgrind使用

Linux:Valgrind使用
  • test1280
  • test1280
  • 2017年05月07日 10:13
  • 184

valgrind简介以及在ARM上交叉编译运行

Valgrind是一款用于内存调试、内存泄漏检测以及性能分析的软件开发工具。 Valgrind的最初作者是Julian Seward,他于2006年由于在开发Valgrind上的工作获得了第二届Go...
  • dengcanjun6
  • dengcanjun6
  • 2017年02月09日 21:52
  • 1422

使用Valgrind找出Android中Native程序内存泄露问题

Android程序通常使用Java程序编写,由于Dalvik虚拟机集成了垃圾回收机制,所以内存使用比较不容易出错,通常就是一个本该被释放的对象却被另一个对象长时间持有着。对于这类问题,可以使用MAT工...
  • Roland_Sun
  • Roland_Sun
  • 2015年05月27日 17:18
  • 6020

使用valgrind检测Android native程序的内存

使用valgrind检测Android native程序的内存 分类: Android系统架构2012-03-27 17:44 6087人阅读 评论(7) 收藏 举报 androida...
  • xinzhou201
  • xinzhou201
  • 2015年04月24日 10:17
  • 328

valgrind for android 编译安装

首先下载最新源码
  • ss0429
  • ss0429
  • 2014年08月12日 10:42
  • 1518

在Android环境中使用Valgrind工具进行内存错误检测

首先,到官网下载valgrind源码: http://valgrind.org/downloads/current.html 对源码进行编译。 我用的是mac系统,对源码解压后,在源码目录下创建配...
  • loushuai
  • loushuai
  • 2016年06月23日 17:37
  • 1043

实例介绍利用valgrind定位内存异常释放问题(double free 和wrong free)

之前介绍过利用valgrind来定位内存泄漏(慢性病, 会导致程序在某个不确定的时刻异常), 本文我们来简要介绍利用valgrind来定位内存的重复释放(急性病, 会报纸程序崩溃)。 看程序: #i...
  • stpeace
  • stpeace
  • 2017年03月12日 18:48
  • 1277

使用valgrind来发现内存泄漏和非法内存操作

原文地址:http://www.cprogramming.com/debugging/valgrind.html 翻译难免会因个人水平原因而有不准确的地方,请大家多批评指正,上面是原文链接,大家也可...
  • wzzfeitian
  • wzzfeitian
  • 2013年02月04日 13:30
  • 18259

使用Valgrind找出Android中Native程序内存泄露问题

Android程序通常使用Java程序编写,由于Dalvik虚拟机集成了垃圾回收机制,所以内存使用比较不容易出错,通常就是一个本该被释放的对象却被另一个对象长时间持有着。对于这类问题,可以使用MAT工...
  • Roland_Sun
  • Roland_Sun
  • 2015年05月27日 17:18
  • 6020

使用valgrind检测Android native程序的内存

使用valgrind检测Android native程序的内存 分类: Android系统架构2012-03-27 17:44 6087人阅读 评论(7) 收藏 举报 androida...
  • xinzhou201
  • xinzhou201
  • 2015年04月24日 10:17
  • 328
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:使用valgrind检测Android native程序的内存
举报原因:
原因补充:

(最多只允许输入30个字)