转自:http://www.cnblogs.com/IDoIUnderstand/p/3280717.html
编译环境: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.21OMAP4460)
一、下载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
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
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 #
==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://www.jb51.cc/article/p-utntujkd-sv.html 宋宝华使用valgrind检测Android native程序的内存
2.
http://www.jb51.cc/article/p-wyqjkaew-mn.html mips 交叉编译valgrind
3.
http://www.jb51.cc/article/p-nmukalze-gg.htmllinux下利用valgrind工具进行内存泄露检测和性能分析
三、安卓上运行 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/ adb push valgrind /data/local/valgrind/ /data/local/valgrind/bin/valgrind ls /data/local/valgrind/bin/valgrind --tool=callgrind ./test /data/local/valgrind/bin/valgrind --tool=memcheck --leak-check=full ./test /data/local/valgrind/bin/callgrind_annotate callgrind.out.XXX
build.sh
export NDKROOT=/root/android/android-ndk-r11c export HWKIND=generic #export VALGRIND_LIB=/data/local/valgrind/lib/valgrind/ # Set up toolchain paths. # # For ARM export AR=$NDKROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ar export LD=$NDKROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld export CC=$NDKROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc # for ARM CPPFLAGS="--sysroot=$NDKROOT/platforms/android-14/arch-arm -DANDROID_HARDWARE_$HWKIND" \ CFLAGS="--sysroot=$NDKROOT/platforms/android-14/arch-arm" \ ./configure --prefix=/data/local/Inst \ --host=armv7-unknown-linux --target=armv7-linux-androideabi \ --with-tmpdir=/sdcard # note: on android emulator,android-14 platform was also tested and works. # It is not clear what this platform nr really is.