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

I. 首先,到官网下载valgrind源码:
http://valgrind.org/downloads/current.html

II. 对源码进行编译。
我用的是mac系统,对源码解压后,在源码目录下创建配置脚本

export NDKROOT=/android-ndk-r9d
export HWKIND=generic
export AR=$NDKROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ar
export LD=$NDKROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ld
export CC=$NDKROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-gcc
export RANLIB=$NDKROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-ranlib
export STRIP=$NDKROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-strip
export CPPFLAGS="--sysroot=$NDKROOT/platforms/android-14/arch-arm -DANDROID_HARDWARE_$HWKIND"
export CFLAGS="--sysroot=$NDKROOT/platforms/android-14/arch-arm"
export LIBS="-L$NDKROOT/platforms/android-14/arch-arm/usr/lib"
./configure --host=armv7-unknown-linux --target=arm-linux-androideabi

III. 运行配置脚本,输出结果是这样的

         Maximum build arch: arm
         Primary build arch: arm
       Secondary build arch: 
                   Build OS: linux
       Primary build target: ARM_LINUX
     Secondary build target: 
           Platform variant: android
      Primary -DVGPV string: -DVGPV_arm_linux_android=1
         Default supp files: exp-sgcheck.supp xfree-3.supp xfree-4.supp bionic.supp 

IV. make编译

V. 将编译出来的文件拷贝到手机上,目前用到的有:
valgrind
memcheck-arm-linux
vgpreload_core-arm-linux.so
vgpreload_memcheck-arm-linux.so
default.supp

VI. 进入手机的shell环境,设置环境变量VALGRIND_LIB为上面文件的拷贝目录,以便valgrind能够找到相关的库和可执行文件。

这样,Valgrind的安装工作就完成了。

我们可以通过一个小的测试程序来检验一下效果。

#include <stdlib.h>

int main() {

  char *p = malloc(100);

  return 0;
}

这里故意申请了一块内存且没有释放。
运行Valgrind:
./valgrind –leak-check=full –track-origins=yes –log-file=check.log executable-file

输出结果为:

==13902== Memcheck, a memory error detector
==13902== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==13902== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==13902== Command: ../memoryleak
==13902== Parent PID: 1580
==13902== 
==13902== 
==13902== HEAP SUMMARY:
==13902==     in use at exit: 272 bytes in 5 blocks
==13902==   total heap usage: 6 allocs, 1 frees, 1,296 bytes allocated
==13902== 
==13902== 8 bytes in 1 blocks are possibly lost in loss record 2 of 5
==13902==    at 0x485A058: malloc (vg_replace_malloc.c:299)
==13902==    by 0x48F5D7B: emutls_alloc (in /system/lib/libc.so)
==13902== 
==13902== 100 bytes in 1 blocks are definitely lost in loss record 4 of 5
==13902==    at 0x485A058: malloc (vg_replace_malloc.c:299)
==13902==    by 0x1084CB: ??? (in /data/local/tmp/memoryleak)
==13902==    by 0x48AC3AD: __libc_init (in /system/lib/libc.so)
==13902== 
==13902== LEAK SUMMARY:
==13902==    definitely lost: 100 bytes in 1 blocks
==13902==    indirectly lost: 0 bytes in 0 blocks
==13902==      possibly lost: 8 bytes in 1 blocks
==13902==    still reachable: 164 bytes in 3 blocks
==13902==         suppressed: 0 bytes in 0 blocks
==13902== Reachable blocks (those to which a pointer was found) are not shown.
==13902== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==13902== 
==13902== For counts of detected and suppressed errors, rerun with: -v
==13902== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

可以看到,泄露的100字节内存已经被检测到了。

参考文献:
http://blog.csdn.net/foruok/article/details/20701991

扩展:
http://stackoverflow.com/questions/9123124/how-to-start-an-android-app-with-valgrind
http://blog.csdn.net/roland_sun/article/details/45870677

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值