├── valgrind_test
├── app
│ ├── .gitignore
│ ├── src
│ │ ├── main
│ │ │ ├── res
│ │ │ │ ├── values
│ │ │ │ │ ├── strings.xml
│ │ │ │ │ ├── colors.xml
│ │ │ │ │ └── styles.xml
│ │ │ │ ├── mipmap-hdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-mdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-xhdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-xxhdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-xxxhdpi
│ │ │ │ │ ├── ic_launcher.png
│ │ │ │ │ └── ic_launcher_round.png
│ │ │ │ ├── mipmap-anydpi-v26
│ │ │ │ │ ├── ic_launcher.xml
│ │ │ │ │ └── ic_launcher_round.xml
│ │ │ │ ├── layout
│ │ │ │ │ └── activity_main.xml
│ │ │ │ ├── drawable-v24
│ │ │ │ │ └── ic_launcher_foreground.xml
│ │ │ │ └── drawable
│ │ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── cpp
│ │ │ │ └── native-lib.cpp
│ │ │ ├── AndroidManifest.xml
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── sunshushu
│ │ │ │ └── test
│ │ │ │ └── MainActivity.java
│ │ ├── test
│ │ │ └── java
│ │ │ │ └── com
│ │ │ │ └── sunshushu
│ │ │ │ └── test
│ │ │ │ └── ExampleUnitTest.java
│ │ └── androidTest
│ │ │ └── java
│ │ │ └── com
│ │ │ └── sunshushu
│ │ │ └── test
│ │ │ └── ExampleInstrumentedTest.java
│ ├── proguard-rules.pro
│ ├── build.gradle
│ └── CMakeLists.txt
├── settings.gradle
├── gradle
│ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
├── .gitignore
├── .idea
│ ├── modules.xml
│ ├── runConfigurations.xml
│ ├── gradle.xml
│ └── misc.xml
├── build.gradle
├── gradle.properties
├── gradlew.bat
└── gradlew
├── readme_attachment.png
├── README.md
└── sss_valgrind_android.sh
/valgrind_test/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/valgrind_test/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/readme_attachment.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunShuShu/ValgrindHelperForAndroid/HEAD/readme_attachment.png
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | valgrind_test
3 |
4 |
--------------------------------------------------------------------------------
/valgrind_test/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunShuShu/ValgrindHelperForAndroid/HEAD/valgrind_test/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/valgrind_test/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunShuShu/ValgrindHelperForAndroid/HEAD/valgrind_test/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunShuShu/ValgrindHelperForAndroid/HEAD/valgrind_test/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunShuShu/ValgrindHelperForAndroid/HEAD/valgrind_test/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunShuShu/ValgrindHelperForAndroid/HEAD/valgrind_test/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunShuShu/ValgrindHelperForAndroid/HEAD/valgrind_test/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunShuShu/ValgrindHelperForAndroid/HEAD/valgrind_test/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunShuShu/ValgrindHelperForAndroid/HEAD/valgrind_test/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunShuShu/ValgrindHelperForAndroid/HEAD/valgrind_test/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunShuShu/ValgrindHelperForAndroid/HEAD/valgrind_test/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sunShuShu/ValgrindHelperForAndroid/HEAD/valgrind_test/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/valgrind_test/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Nov 23 16:46:31 CST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
7 |
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/valgrind_test/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/valgrind_test/app/src/test/java/com/sunshushu/test/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.sunshushu.test;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/valgrind_test/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/valgrind_test/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 |
5 | repositories {
6 | google()
7 | jcenter()
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:3.0.0'
11 |
12 |
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 | }
16 | }
17 |
18 | allprojects {
19 | repositories {
20 | google()
21 | jcenter()
22 | }
23 | }
24 |
25 | task clean(type: Delete) {
26 | delete rootProject.buildDir
27 | }
28 |
--------------------------------------------------------------------------------
/valgrind_test/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/valgrind_test/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/valgrind_test/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/cpp/native-lib.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #import "android/log.h"
5 | #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, "CTest", __VA_ARGS__)
6 | static void test_memory_overrun()
7 | {
8 | char *p = (char *)malloc(1);
9 | *(short*)p = 2;
10 | free(p);
11 | }
12 |
13 | static void test_strcpy_overrun()
14 | {
15 | char *p = (char *)malloc(sizeof(char) * 5);
16 | strcpy(p, "hello");
17 | }
18 |
19 | static void test_memory_free_wild_pointer()
20 | {
21 | char *p;
22 | free(p);
23 | }
24 |
25 | extern "C"
26 | JNIEXPORT void
27 | JNICALL
28 | Java_com_sunshushu_test_MainActivity_fromIssue(
29 | JNIEnv *env,
30 | jobject /* this */) {
31 |
32 | test_memory_overrun();
33 | test_strcpy_overrun();
34 | test_memory_free_wild_pointer();
35 | LOGI("ctest: memory issues!");
36 | }
--------------------------------------------------------------------------------
/valgrind_test/app/src/androidTest/java/com/sunshushu/test/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.sunshushu.test;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.sunshushu.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
12 |
18 |
19 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/java/com/sunshushu/test/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.sunshushu.test;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.widget.Button;
7 |
8 | public class MainActivity extends AppCompatActivity implements View.OnClickListener {
9 |
10 | // Used to load the 'native-lib' library on application startup.
11 | static {
12 | System.loadLibrary("native-lib");
13 | }
14 |
15 | @Override
16 | protected void onCreate(Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | setContentView(R.layout.activity_main);
19 |
20 | Button btn1 = (Button)findViewById(R.id.btn_issue);
21 | Button btn2 = (Button)findViewById(R.id.btn_exit);
22 | btn1.setOnClickListener(this);
23 | btn2.setOnClickListener(this);
24 | }
25 |
26 | public void onClick(View viewv)
27 | {
28 | int id = viewv.getId();
29 | if (id == R.id.btn_issue)
30 | {
31 | fromIssue();
32 | }
33 | else if (id == R.id.btn_exit)
34 | {
35 | System.exit(0);
36 | }
37 | }
38 |
39 | private native void fromIssue();
40 | }
41 |
--------------------------------------------------------------------------------
/valgrind_test/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | defaultConfig {
6 | applicationId "com.sunshushu.test"
7 | minSdkVersion 19
8 | targetSdkVersion 27
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | externalNativeBuild {
13 | cmake {
14 | cppFlags ""
15 | }
16 | }
17 | }
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 | externalNativeBuild {
25 | cmake {
26 | path "CMakeLists.txt"
27 | }
28 | }
29 | }
30 |
31 | dependencies {
32 | implementation fileTree(dir: 'libs', include: ['*.jar'])
33 | implementation 'com.android.support:appcompat-v7:26.1.0'
34 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
35 | testImplementation 'junit:junit:4.12'
36 | androidTestImplementation 'com.android.support.test:runner:1.0.1'
37 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
38 | }
39 |
--------------------------------------------------------------------------------
/valgrind_test/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/valgrind_test/app/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | # For more information about using CMake with Android Studio, read the
2 | # documentation: https://d.android.com/studio/projects/add-native-code.html
3 |
4 | # Sets the minimum version of CMake required to build the native library.
5 |
6 | cmake_minimum_required(VERSION 3.4.1)
7 |
8 | # Creates and names a library, sets it as either STATIC
9 | # or SHARED, and provides the relative paths to its source code.
10 | # You can define multiple libraries, and CMake builds them for you.
11 | # Gradle automatically packages shared libraries with your APK.
12 |
13 | add_library( # Sets the name of the library.
14 | native-lib
15 |
16 | # Sets the library as a shared library.
17 | SHARED
18 |
19 | # Provides a relative path to your source file(s).
20 | src/main/cpp/native-lib.cpp )
21 |
22 | # Searches for a specified prebuilt library and stores the path as a
23 | # variable. Because CMake includes system libraries in the search path by
24 | # default, you only need to specify the name of the public NDK library
25 | # you want to add. CMake verifies that the library exists before
26 | # completing its build.
27 |
28 | find_library( # Sets the name of the path variable.
29 | log-lib
30 |
31 | # Specifies the name of the NDK library that
32 | # you want CMake to locate.
33 | log )
34 |
35 | # Specifies libraries CMake should link to your target library. You
36 | # can link multiple libraries, such as libraries you define in this
37 | # build script, prebuilt third-party libraries, or system libraries.
38 |
39 | target_link_libraries( # Specifies the target library.
40 | native-lib
41 | # Links the target library to the log library
42 | # included in the NDK.
43 | ${log-lib} )
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ValgrindHelperForAndroid
2 | A easy way to use valgrind on Android device.
3 |
4 | # 简介
5 | 现在有很多安卓应用因为性能或安全方面原因,会通过JNI调用原生代码。排查原生的C、C++代码时可以使用Valgrind。Valgrind工具包括Memcheck(用于检测C和C++中与内存相关的错误)、Cachegrind(缓存分析器)、Massif(堆分析器)和其他几种工具。Valgrind在Linux开发中应用广泛,但在安卓开发中用起来比较麻烦,官方文档和网上的资料也比较少。这就是这个工具诞生的原因。
6 | 关于Valgrind这个工具,这里就不多做介绍了,大家可以参考[官网](http://valgrind.org)或google.
7 |
8 | * 开始之前的说明:
9 | 1.你的安卓设备需要root.(有兴趣完善这个脚本的朋友可以尝试一下把这条限制去掉)
10 | 2.目前,在下面的电脑和安卓设备中测试成功了(欢迎提交Pull request补充),其他PC和设备可能需要修改脚本。如果需要修改脚本,可以参考*原理*中的内容。
11 | - `macOS 10.13` `华为荣耀3C(H30-T00) 4.4.2`
12 | - `Ubuntu 16.04.3` `华为荣耀3C(H30-T00) 4.4.2`
13 |
14 | **\*原理和FAQ在[Wiki](https://github.com/sunShuShu/ValgrindHelperForAndroid/wiki)里。**
15 | **如果你优化了这个脚本的兼容性或修改了bug,(或者有能力把README和Wiki里的内容翻译成英文😂)欢迎提交[Pull request](https://github.com/sunShuShu/ValgrindHelperForAndroid/pulls)❤️**
16 |
17 | # 使用
18 | * 将Valgrind安装到安卓设备上:
19 | - 1.下载[ValgrindHelperForAndroid.zip](https://github.com/sunShuShu/ValgrindHelperForAndroid/releases)和对应你手机cpu架构的Valgrind压缩包:
20 | [ARMv7](https://github.com/sunShuShu/ValgrindForAndroid/raw/master/valgrind-arm.zip),
21 | [ARM64](https://github.com/sunShuShu/ValgrindForAndroid/raw/master/valgrind-arm64.zip).
22 | - 2.解压ValgrindHelperForAndroid.zip,把Valgrind压缩包放到ValgrindHelperForAndroid文件夹中。就像这样:
23 | 
24 | - 3.在终端中执行下面的命令:
25 | ```
26 | cd
27 | ./sss_valgrind_android.sh -i
28 | ```
29 | - 4.出现下面的提示说明安装成功:
30 | ```DONE, install valgrind success!```
31 |
32 | * 用Valgrind启动安卓app:
33 | - 1.安装app。建议用ValgrindHelperForAndroid中的valgrind\_test工程测试,make project, debug app。
34 | - 2.在终端中执行下面的命令:
35 | ```
36 | cd
37 | ./sss_valgrind_android.sh
38 | ```
39 | - 3.输入Valgrind命令参数。(可以直接输入回车,用Valgrind默认的参数)
40 | - 4.输入app的包名。(valgrind\_test工程为com.sunshushu.test)
41 | - 5.输入app的MainActivity的名称。(valgrind\_test工程为MainActivity)
42 | - 6.输入工程中带符号表的库的路径。(valgrind\_test工程为valgrind_test/app/build/intermediates/cmake/debug/obj/armeabi-v7a)
43 | - 7.等待app启动。这个过程可能会很长,设备中途可能会无响应或黑屏,你可以先去喝杯咖啡☕️(**先不要关闭终端**)
44 | - 8.测试app。valgrind_test中,点击app的“MEMORY ISSUES”按钮,会产生3个内存问题。然后点击“EXIT”按钮,**正常退出**app.
45 | *(由于Valgrind在应用结束之前要进行一些工作,如果直接**强制关闭**app可能导致内存问题排查不全面,所以这里设计了EXIT按钮。)*
46 | * **强制关闭**app,并从安卓设备取回Valgrind的日志。
47 | 在终端中**继续**执行下面的命令:
48 | `DONE`
49 | 等待终端中出现下面的提示时,到ValgrindHelperForAndroid文件夹中查看Valgrind日志即可。
50 | `DONE. Check the log(s) in ...`
51 | (*如果你在上面的第8步中点击“EXIT”按钮关闭了app,这里的强制关闭不会影响Valgrind彻底排查内存问题。*)
52 | * 查看日志。在日志中搜索app的包名,看一下是不是有内存问题吧。
53 | *(日志文件名中的数字是线程ID(PID)。)*
54 |
--------------------------------------------------------------------------------
/valgrind_test/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/valgrind_test/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/valgrind_test/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/sss_valgrind_android.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | install_valgrind() {
4 | DEVICE_CPU_INFO=`adb shell cat /proc/cpuinfo`
5 | if [ $? -ne 0 ]; then
6 | echo "FAIL! Ensure adb is installed and device is pluged!"
7 | exit 1
8 | fi
9 | DEVICE_CPU_INDEX=`echo $DEVICE_CPU_INFO | awk -F"AArch64" '{print $1}' | wc -c`
10 | if [ $DEVICE_CPU_INDEX -ge ${#DEVICE_CPU_INFO} ]; then
11 | DEVICE_CPU_INDEX=`echo $DEVICE_CPU_INFO | awk -F"ARMv7" '{print $1}' | wc -c`
12 | if [ $DEVICE_CPU_INDEX -ge ${#DEVICE_CPU_INFO} ]; then
13 | echo "FAIL! Can not support the device cpu arch now."
14 | echo "You can try to install valgrind manually, see http://www.valgrind.org"
15 | exit 1
16 | else
17 | DEVICE_CPU_TYPE="ARMv7"
18 | fi
19 | else
20 | DEVICE_CPU_TYPE="ARM64"
21 | fi
22 | echo "DEVICE_CPU_TYPE: $DEVICE_CPU_TYPE"
23 |
24 | rm -rf ./Inst
25 | echo "Unzip valgrind package..."
26 | case $DEVICE_CPU_TYPE in
27 | "ARMv7")
28 | unzip -nq valgrind-arm.zip
29 | ;;
30 | "ARM64")
31 | unzip -nq valgrind-arm64.zip
32 | ;;
33 | esac
34 | if [[ $? -ne 0 ]]; then
35 | rm -rf ./Inst
36 | echo "FAIL! Can not unzip valgrind package."
37 | exit 1
38 | fi
39 |
40 | adb push ./Inst/data/local/Inst /data/local
41 | if [[ $? -ne 0 ]]; then
42 | echo "FAIL! Can not push valgrind to device."
43 | echo "You can try to move the `pwd`/Inst/data/local/Inst to /data/local manually."
44 | exit 1
45 | else
46 | rm -rf ./Inst
47 | fi
48 |
49 | echo "DONE, install valgrind success!"
50 | }
51 |
52 | input_parameter() {
53 | if [[ ! -e .sss_last_input ]]; then
54 | PACKAGE_NAME="com.sunshushu.test"
55 | APP_MAIN_ACTIVITY="MainActivity"
56 | SYMBOLIZED_LIB_PATH="`pwd`/valgrind_test/app/build/intermediates/cmake/debug/obj/armeabi-v7a"
57 | echo -e "${PACKAGE_NAME}\n${APP_MAIN_ACTIVITY}\n${SYMBOLIZED_LIB_PATH}" > .sss_last_input
58 | fi
59 |
60 | echo "Type valgrind parameter, nullable."
61 | echo -e " (You don't need to set \"--log-file\". By default, the log will be written to file(s) and got back automatically.)"
62 | read VALGRIND_PARAMS
63 | echo "$VALGRIND_PARAMS" | grep -q "log-file"
64 | if [[ $? -ne 0 ]]; then
65 | VALGRIND_PARAMS="$VALGRIND_PARAMS --log-file=/sdcard/valgrind.%p.log"
66 | fi
67 |
68 | PACKAGE_NAME=`sed -n "1p" .sss_last_input`
69 | echo "Type the package name of your android app. [$PACKAGE_NAME]"
70 | while read PACKAGE_NAME_INPUT; do
71 | if [[ -n $PACKAGE_NAME_INPUT ]]; then
72 | PACKAGE_NAME="$PACKAGE_NAME_INPUT"
73 | else
74 | echo "$PACKAGE_NAME"
75 | fi
76 | if [[ ${#PACKAGE_NAME} -gt 26 ]]; then
77 | echo "FAIL! The length of package name can not be greater than 26."
78 | exit 1
79 | fi
80 | ALL_PACKAGES=`adb shell pm list packages`
81 | if [[ $? -ne 0 ]]; then
82 | echo "Ensure adb is installed and device is pluged!"
83 | continue
84 | fi
85 | echo "$ALL_PACKAGES" | grep -q "package:${PACKAGE_NAME}"
86 | if [[ $? -ne 0 ]]; then
87 | echo "There is no specified app on the device, try again."
88 | continue
89 | else
90 | break
91 | fi
92 | done
93 |
94 | APP_MAIN_ACTIVITY=`sed -n "2p" .sss_last_input`
95 | echo "Type the main avtivity name of the app. [$APP_MAIN_ACTIVITY]"
96 | read APP_MAIN_ACTIVITY_INPUT
97 | if [[ -n $APP_MAIN_ACTIVITY_INPUT ]]; then
98 | APP_MAIN_ACTIVITY="$APP_MAIN_ACTIVITY_INPUT"
99 | else
100 | echo "$APP_MAIN_ACTIVITY"
101 | fi
102 |
103 | SYMBOLIZED_LIB_PATH=`sed -n "3p" .sss_last_input`
104 | echo "Type the directory of lib with symbol table. [$SYMBOLIZED_LIB_PATH]"
105 | while read SYMBOLIZED_LIB_PATH_INPUT; do
106 | if [[ -n $SYMBOLIZED_LIB_PATH_INPUT ]]; then
107 | SYMBOLIZED_LIB_PATH="$SYMBOLIZED_LIB_PATH_INPUT"
108 | else
109 | echo "$SYMBOLIZED_LIB_PATH"
110 | fi
111 | ALL_SYMBOLIZED_LIB=`ls "$SYMBOLIZED_LIB_PATH" | grep -e ".so" -e ".a"`
112 | if [[ -z $ALL_SYMBOLIZED_LIB ]]; then
113 | echo "There is not lib file, try again."
114 | continue
115 | else
116 | break
117 | fi
118 | done
119 |
120 | echo -e "${PACKAGE_NAME}\n${APP_MAIN_ACTIVITY}\n${SYMBOLIZED_LIB_PATH}" > .sss_last_input
121 | }
122 |
123 | push_sh_to_device() {
124 | START_CALGRIND_SH="#!/system/bin/sh \n
125 | PACKAGE=\"${PACKAGE_NAME}\" \n
126 | VGPARAMS='${VALGRIND_PARAMS}' \n
127 | export TMPDIR=/data/data/\$PACKAGE \n
128 | exec /data/local/Inst/bin/valgrind \$VGPARAMS \$* "
129 | echo -e $START_CALGRIND_SH > start_valgrind.sh
130 |
131 | adb push start_valgrind.sh /data/local/
132 | if [ $? -ne 0 ]; then
133 | rm -f ./start_valgrind.sh
134 | echo "FAIL! Ensure adb is installed and device is pluged!"
135 | exit 1
136 | fi
137 | rm -f ./start_valgrind.sh
138 |
139 | adb shell chmod 777 /data/local/start_valgrind.sh
140 | if [ $? -ne 0 ]; then
141 | echo "FAIL! Can not execute: adb shell chmod 777 /data/local/start_valgrind.sh"
142 | exit 1
143 | fi
144 | }
145 |
146 | replace_lib() {
147 | echo "Replace app lib..."
148 | echo "$ALL_SYMBOLIZED_LIB" | while read LIB_PATH; do
149 | adb push "$SYMBOLIZED_LIB_PATH/$LIB_PATH" "/data/data/$PACKAGE_NAME/lib"
150 | if [[ $? -ne 0 ]]; then
151 | echo "FAIL! Can not push symbolized lib to /data/data/$PACKAGE_NAME/lib"
152 | exit 1
153 | fi
154 | done
155 | }
156 |
157 | launch_valgrind() {
158 | LOG_WRAPPER="logwrapper /data/local/start_valgrind.sh"
159 | adb root
160 | echo -e "setprop wrap.$PACKAGE_NAME \"$LOG_WRAPPER\"\n exit" > .temp
161 | adb shell < .temp
162 | rm -f ./.temp
163 | GETPROP=`adb shell getprop wrap.$PACKAGE_NAME | tr -d "\r"`
164 | if [[ $LOG_WRAPPER != $GETPROP ]]; then
165 | echo -e "FAIL! Can not set device system property. Ensure the device can execute \"adb shell setprop\"."
166 | exit 1
167 | fi
168 |
169 | echo -e "\"$PACKAGE_NAME\" will relaunch..."
170 | adb shell am force-stop $PACKAGE_NAME
171 | adb shell am start -a android.intent.action.MAIN -n "$PACKAGE_NAME/.$APP_MAIN_ACTIVITY"
172 | clear
173 | echo "Launching app..."
174 | echo "This may take as long as a song. Have a cup of coffee ☕️ "
175 | }
176 |
177 | wait_and_terminate_app() {
178 | while true; do
179 | echo -e " (Input \"done\" to terminate app and get back logs.)"
180 | read NEXT_ACTION
181 | if [[ $NEXT_ACTION != "done" ]]; then
182 | continue
183 | fi
184 | clear
185 | echo "Terminating app..."
186 | adb shell am force-stop $PACKAGE_NAME
187 | if [[ $? -ne 0 ]]; then
188 | echo "Please plugin your device."
189 | else
190 | echo -e "setprop wrap.$PACKAGE_NAME \"\"\n exit" > .temp
191 | adb shell < .temp
192 | rm -f ./.temp
193 | break
194 | fi
195 | done
196 | }
197 |
198 | pull_logs() {
199 | echo "Getting back logs..."
200 | ALL_LOGS=`adb shell ls /sdcard/ | grep "valgrind.*.log" | tr -d "\r"`
201 | if [[ ${#ALL_LOGS} -lt 13 ]]; then
202 | echo "DONE, There is no log file."
203 | exit 0
204 | fi
205 |
206 | echo "$ALL_LOGS" | while read LOG_PATH; do
207 | LOG_PATH="/sdcard/$LOG_PATH"
208 | adb pull "$LOG_PATH"
209 | if [[ $? -ne 0 ]]; then
210 | echo -e "FAIL! Can not pull log files: \"$LOG_PATH\""
211 | exit 1
212 | fi
213 | done
214 |
215 | adb shell rm -f /sdcard/valgrind.*.log
216 | echo "DONE. Check the log(s) in `pwd`"
217 | }
218 |
219 | if [[ $# -eq 0 ]]; then
220 | input_parameter
221 | else
222 | if [[ $1 = "-i" ]]; then
223 | install_valgrind
224 | exit 0
225 | fi
226 | if [[ $# -eq 4 ]]; then
227 | VALGRIND_PARAMS=$1
228 | PACKAGE_NAME=$2
229 | APP_MAIN_ACTIVITY=$3
230 | SYMBOLIZED_LIB_PATH=$4
231 | else
232 | echo "FAIL! Parameter error."
233 | echo "Perhaps you should try to execute this script without any arguments."
234 | echo "Or you could use it like this:"
235 | echo "sss_valgrind_android.sh \"valgrind_parameters\" \"com.example.test\" \"MainActivity\" \"/lib_with_symbol_table_path\""
236 | exit 1
237 | fi
238 | fi
239 |
240 | push_sh_to_device
241 | replace_lib
242 | launch_valgrind
243 | wait_and_terminate_app
244 | pull_logs
245 |
246 | exit 0
247 |
--------------------------------------------------------------------------------