Building and running valgrind on Android

Note to self post for when the next time I need it, or: how to build and run your application under valgrind on Android (tested on 4.4).

Preqres: Patience, Android NDK, rooted device, linux host (VM, or whatever).

Step 1:
Make sure you are running `dalvik` as your runtime. `art` might be new and flashy, but it does not play well with valgrind in my experience.

Step 2:
Get valgrind sources from valgrind.org or do

svn co svn://svn.valgrind.org/valgrind/trunk valgrind

Unpack it if you downloaded the source tarball, obviously.

Step 3:

./autogen.sh

in the svn checkout (skip it if you downloaded the tarball)

Step 4:

export ANDROID_NDK_HOME=~/android-ndk-r9d

or where ever you have your Android NDK setup.

Step 5:
Configuring and building, replace `arm-linux-androideabi-4.6` with your toolchain and `linux-x86` with your platform.

export HWKIND=generic
export TOOLCHAIN=$ANDROID_NDK_HOME/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi

export AR=$TOOLCHAIN-ar
export LD=$TOOLCHAIN-ld
export CC=$TOOLCHAIN-gcc

CPPFLAGS="--sysroot=$ANDROID_NDK_HOME/platforms/android-14/arch-arm -DANDROID_HARDWARE_$HWKIND" \
CFLAGS="--sysroot=$ANDROID_NDK_HOME/platforms/android-14/arch-arm" \
./configure --prefix=/data/local/Inst \
--host=armv7-unknown-linux --target=armv7-unknown-linux \
--with-tmpdir=/sdcard

make
make -j4 install DESTDIR=`pwd`/Inst

Step 6:
Installing on your device:
If you have an engineering build or install adbd insecure, just do:

adb root
adb remount

and then jump to the next step!

Otherwise, just remount /system read-write:

adb shell
mount | grep system
#/dev/block/platform/msm_sdcc.1/by-name/system /system ext4 ro,seclabel,relatime,data=ordered 0 0
su
mount -o rw,remount /dev/block/platform/msm_sdcc.1/by-name/system /system

Step 7:
Putting it on your device

adb shell "mkdir /data/local/Inst"
## could be optimised... probably
adb push Inst /
adb shell "chmod 777 /data/local/Inst"
adb shell "/data/local/Inst/bin/valgrind --version"

If you built your own Android image to get symbols for everything, put them in place as well:

adb shell mkdir -p /sdcard/symbols/system
adb push ~/android_src/out/target/product/mako/symbols/system /sdcard/symbols/system/

Step 8:
Get these two shell scripts and optionally the suppression file for valgrind. Make sure your apps package name is less than 26 chars long, because setprop fails to set properties longer than 31 chars (26 + ‘wrap.’).

chmod +x run_valgrind.sh
./run_valgrind.sh

Done! Now just watch adb logcat and marvel at the wonders of valgrind. :)

Credits: building and running

One thought on “Building and running valgrind on Android

Comments are closed.