I am new to Valgrind. I learnt how to use Valgrind on Android from this thread: Can't run a Java Android program with Valgrind
The scripts on my Desktop are almost the same as the original ones, except that I built the Valgrind using aarch-64 toolchains manually and ran Valgrind on Samsung S6. I have tested the Valgrind on Android using compiled c binary, and it worked. When I tested the HelloJni application, the screen froze and I cannot see the output of the application. I used "adb shell top" to see the running process, and there was a process called:
{main} valgrind --error-limit=no --trace-children=yes --compress-strings=no --compress-pos=no --log-file=/sdcard/valgrind.log.%p --tool=callgrind
When I kill this process, there were some log files on /sdcard/. And the logcat worked OK. So, Valgrind did work on the background, but I could not see the application run on the foreground.
I am really stuck here. Anyone has any ideas?
Also, does anyone know how to run Valgrind on an apk file?
Thanks a lot.
My code:
build_valgrind.sh: (I built my Valgrind somewhere else and pushed it to /data/local/Inst already)
#!/usr/bin/env bash
#set -x
adb root
adb remount
# Ensure Valgrind on the phone is running
adb shell "/data/local/Inst/bin/valgrind --version"
# Add Valgrind executable to PATH (this might fail)
adb shell "export PATH=$PATH:/data/local/Inst/bin/"
export NDK_HOME=$HOME/Downloads/android-ndk-r10e
export SDK_HOME=$HOME/Downloads/android-sdk-linux
export PATH=$SDK_HOME/tools:$PATH
export PATH=$SDK_HOME/platform-tools:$PATH
RUN_HELLO_JNI_THROUGH_VALGRIND=true
cd valgrind-3.11.0
if [ $RUN_HELLO_JNI_THROUGH_VALGRIND = true ]; then
PACKAGE="com.example.hellojni"
# The location of the Hello JNI sample application
HELLO_JNI_PATH="$NDK_HOME/samples/hello-jni"
pushd "$HELLO_JNI_PATH"
# Update build target to the desired Android SDK version
ANDROID_PROJECT_TARGET="android-18"
android update project --target "$ANDROID_PROJECT_TARGET" --path . --name hello-jni --subprojects
# Enable Android NDK build with Ant
echo '<?xml version="1.0" encoding="utf-8"?>
<project name="HelloJni" basedir="." default="debug">
<target name="-pre-build">
<exec executable="${ndk.dir}/ndk-build" failonerror="true"/>
</target>
<target name="clean" depends="android_rules.clean">
<exec executable="${ndk.dir}/ndk-build" failonerror="true">
<arg value="clean"/>
</exec>
</target>
</project>
' > "custom_rules.xml"
# Set NDK HOME for Ant (only if not already set)
if ! grep -P -q "ndk.dir=.+" "local.properties" ; then
echo -e "\nndk.dir=$NDK_HOME" >> "local.properties"
fi
# Fix for Java 8 warning (warning: [options] source value 1.5 is obsolete and will be removed in a future release)
echo "java.compilerargs=-Xlint:-options" >> "ant.properties"
# Workaround INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES error
adb uninstall "$PACKAGE"
# Build Hello JNI project in debug mode and install it on the device
ant clean && ant debug && ant installd
popd
cd ..
# Start HelloJNI app
adb shell am start -a android.intent.action.MAIN -n $PACKAGE/.HelloJni
# Make the script executable
chmod a+x bootstrap_valgrind.sh
# Run application through Valgrind on the phone
/usr/bin/env bash bootstrap_valgrind.sh
adb shell ls -lR "/sdcard/*grind*"
adb shell ls -lR "/storage/sdcard0/*grind*"
adb shell ls -lR "/storage/sdcard1/*grind*"
fi
bootstrap_valgrind.sh:
#!/usr/bin/env bash
PACKAGE="com.example.hellojni"
adb push start_valgrind.sh /data/local/
adb shell su -c "chmod 777 /data/local/start_valgrind.sh"
adb root
adb shell setprop wrap.$PACKAGE "logwrapper /data/local/start_valgrind.sh"
echo "wrap.$PACKAGE: $(adb shell getprop wrap.$PACKAGE)"
adb shell am force-stop $PACKAGE
adb shell am start -a android.intent.action.MAIN -n $PACKAGE/.HelloJni
adb logcat -c
adb logcat
exit 0
start_valgrind.sh:
#!/system/bin/sh
PACKAGE="com.example.hellojni"
# Callgrind tool
VGPARAMS='--error-limit=no --trace-children=yes --compress-strings=no --compress-pos=no --log-file=/sdcard/valgrind.log.%p --tool=callgrind --callgrind-out-file=/sdcard/callgrind.out.%p'
# Memcheck tool
#VGPARAMS='-v --error-limit=no --trace-children=yes --log-file=/sdcard/valgrind.log.%p --tool=memcheck --leak-check=full --show-reachable=yes'
export TMPDIR=/data/data/$PACKAGE
exec /data/local/Inst/bin/valgrind $VGPARAMS $*