Ubuntu上交叉编译valgrind for Android 4.0.4的过程与注意事项

编译环境:Ubuntu x86_64(Linux root 2.6.32-45-generic #101-Ubuntu SMP Mon Dec 3 15:39:38 UTC 2012 x86_64 GNU/Linux)

运行环境:Android 4.0.4 (Linux kernel 3.0.21 OMAP4460)

一、下载NDK9和valgrind 3.8.1。

二、按照valgrind 3.8.1中的README.android进行编译,步骤如下:

1. 指定NDK根目录

export NDKROOT=/path/to/android-ndk-r9

2. 指定通用的安卓设备

export HWKIND=generic

3. 指定交叉工具链路径

export AR=$NDKROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ar

export LD=$NDKROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld

export CC=$NDKROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc

4. 到valgrind目录下配置valgrind

cd /path/to/valgrind

CPPFLAGS="--sysroot=$NDKROOT/platforms/android-3/arch-arm -DANDROID_HARDWARE_$HWKIND"

CFLAGS="--sysroot=$NDKROOT/platforms/android-3/arch-arm"

./configure --prefix=/data/local/valgrind --host=armv7-unknown-linux --target=armv7-unknown-linux --with-tmpdir=/sdcard

注:配置这步经常报错,前面的路径等设置需要细心啊。

5. 编译并安装到指定路径

make -j2

make -j2 install DESTDIR=`pwd`/Inst

注:-j2表示利用双核提升编译速度,对速度不在乎的可以去掉。

三、安卓上运行

1. 拷贝Inst到安卓系统/data/local/valgrind中。

注:假如路径不是编译配置时的路径,这时需要重新设置一下,否则会报错。“valgrind: failed to start tool 'memcheck' for platform 'arm-linux': No such file or directory”

export VALGRIND_LIB=/data/local/valgrind/lib/valgrind/

2. 安卓4.0.4下(OMAP4460)执行Memcheck效果

127|shell@android:/data/local # /data/local/valgrind/bin/valgrind ls

==3509== Memcheck, a memory error detector

==3509== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.

==3509== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info

==3509== Command: ls

==3509==

--3509-- WARNING: Serious error when reading debug info

--3509-- When reading debug info from /data/local/valgrind/lib/valgrind/vgpreload_core-arm-linux.so:

--3509-- Can't make sense of .data section mapping

test

tmp

==3509==

==3509== HEAP SUMMARY:

==3509== in use at exit: 4,096 bytes in 1 blocks

==3509== total heap usage: 5 allocs, 4 frees, 8,337 bytes allocated

==3509==

==3509== LEAK SUMMARY:

==3509== definitely lost: 0 bytes in 0 blocks

==3509== indirectly lost: 0 bytes in 0 blocks

==3509== possibly lost: 0 bytes in 0 blocks

==3509== still reachable: 4,096 bytes in 1 blocks

==3509== suppressed: 0 bytes in 0 blocks

==3509== Rerun with --leak-check=full to see details of leaked memory

==3509==

==3509== For counts of detected and suppressed errors, rerun with: -v

==3509== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

shell@android:/data/local #

3. 安卓4.0.4下(OMAP4460)执行callgrind步骤

/data/local/valgrind/bin/valgrind --tool=callgrind ./test

执行完成后在目录下生成"callgrind.out.XXX"(XXX是进程ID号)是分析文件,可以直接利用:/data/local/valgrind/bin/callgrind_annotate callgrind.out.XXX 打印结果;

也可以在Ubuntu上使用:gprof2dot.py -f callgrind callgrind.out.XXX |dot -Tpng -o report.png 来生成图形化结果(需下载gprof2dot.py脚本,详见参考资料3)。

注:

1、使用callgrind时,./test程序运行时间短(<2s),运算量小的可以正常执行callgrind;而当test程序运行时间较长(>6S),运算量较大时,会导致安卓系统不稳定,无法正确执行下去,估计valgrind对安卓的兼容性较差吧。

参考资料:

1. http://blog.csdn.net/21cnbao/article/details/7399863 宋宝华 使用valgrind检测Android native程序的内存

2. http://blog.csdn.net/gxh9314/article/details/8447559 mips 交叉编译valgrind

3. http://blog.csdn.net/yanghao23/article/details/7514587 linux下利用valgrind工具进行内存泄露检测和性能分析