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工具進行內存泄露檢測和性能分析
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章