使用Valgrind

时间:2016-01-20 21:41:54

标签: android android-ndk valgrind

我是Valgrind的新手。我从这个线程学习了如何在Android上使用Valgrind: Can't run a Java Android program with Valgrind

我的桌面上的脚本几乎与原始脚本相同,只不过我手动使用aarch-64工具链构建了Valgrind并在Samsung S6上运行了Valgrind。我已经使用编译的c二进制文件测试了Android上的Valgrind,它有效。当我测试HelloJni应用程序时,屏幕冻结,我看不到应用程序的输出。我使用了" adb shell top"看到正在运行的进程,并且有一个名为:

的进程
{main} valgrind --error-limit=no --trace-children=yes --compress-strings=no --compress-pos=no --log-file=/sdcard/valgrind.log.%p --tool=callgrind 

当我终止这个过程时,/ sdcard /上有一些日志文件。 logcat运行正常。所以,Valgrind确实在后台工作,但我看不到应用程序在前台运行。

我真的被困在这里了。有人有什么想法吗?

另外,有谁知道如何在apk文件上运行Valgrind?

非常感谢。

我的代码:

build_valgrind.sh :(我在其他地方构建了我的Valgrind并将其推送到/ data / local / Inst)

#!/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 $* 

0 个答案:

没有答案