├── .gitignore
├── .idea
├── checkstyle-idea.xml
├── codeStyles
│ └── Project.xml
├── compiler.xml
├── copyright
│ ├── Baidu.xml
│ └── profiles_settings.xml
├── encodings.xml
├── google-java-format.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── LICENSE
├── README.md
├── Screenshot.png
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── dodola
│ │ └── jvmti
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── dodola
│ │ │ └── jvmti
│ │ │ ├── Main2Activity.java
│ │ │ └── MainActivity.java
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ ├── activity_main.xml
│ │ └── activity_main2.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.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
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── dodola
│ └── jvmti
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── jvmtilib
├── .gitignore
├── CMakeLists.txt
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── dodola
│ │ └── jvmtilib
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── cpp
│ │ ├── dexer
│ │ │ ├── Android.bp
│ │ │ ├── bytecode_encoder.cc
│ │ │ ├── code_ir.cc
│ │ │ ├── common.cc
│ │ │ ├── control_flow_graph.cc
│ │ │ ├── debuginfo_encoder.cc
│ │ │ ├── dex_bytecode.cc
│ │ │ ├── dex_format.cc
│ │ │ ├── dex_ir.cc
│ │ │ ├── dex_ir_builder.cc
│ │ │ ├── dex_utf8.cc
│ │ │ ├── instrumentation.cc
│ │ │ ├── reader.cc
│ │ │ ├── slicer
│ │ │ │ ├── arrayview.h
│ │ │ │ ├── buffer.h
│ │ │ │ ├── bytecode_encoder.h
│ │ │ │ ├── chronometer.h
│ │ │ │ ├── code_ir.h
│ │ │ │ ├── common.h
│ │ │ │ ├── control_flow_graph.h
│ │ │ │ ├── debuginfo_encoder.h
│ │ │ │ ├── dex_bytecode.h
│ │ │ │ ├── dex_format.h
│ │ │ │ ├── dex_ir.h
│ │ │ │ ├── dex_ir_builder.h
│ │ │ │ ├── dex_leb128.h
│ │ │ │ ├── dex_utf8.h
│ │ │ │ ├── hash_table.h
│ │ │ │ ├── index_map.h
│ │ │ │ ├── instrumentation.h
│ │ │ │ ├── intrusive_list.h
│ │ │ │ ├── memview.h
│ │ │ │ ├── reader.h
│ │ │ │ ├── scopeguard.h
│ │ │ │ ├── tryblocks_encoder.h
│ │ │ │ └── writer.h
│ │ │ ├── tryblocks_encoder.cc
│ │ │ └── writer.cc
│ │ ├── jvmti.h
│ │ └── native-lib.cpp
│ ├── java
│ │ └── com
│ │ │ └── dodola
│ │ │ └── jvmtilib
│ │ │ └── JVMTIHelper.java
│ └── res
│ │ └── values
│ │ └── strings.xml
│ └── test
│ └── java
│ └── com
│ └── dodola
│ └── jvmtilib
│ └── ExampleUnitTest.java
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # IntelliJ
36 | *.iml
37 | .idea/workspace.xml
38 | .idea/tasks.xml
39 | .idea/gradle.xml
40 | .idea/assetWizardSettings.xml
41 | .idea/dictionaries
42 | .idea/libraries
43 | .idea/caches
44 |
45 | # Keystore files
46 | # Uncomment the following line if you do not want to check your keystore files in.
47 | #*.jks
48 |
49 | # External native build folder generated in Android Studio 2.2 and later
50 | .externalNativeBuild
51 |
52 | # Google Services (e.g. APIs or Firebase)
53 | google-services.json
54 |
55 | # Freeline
56 | freeline.py
57 | freeline/
58 | freeline_project_description.json
59 |
60 | # fastlane
61 | fastlane/report.xml
62 | fastlane/Preview.html
63 | fastlane/screenshots
64 | fastlane/test_output
65 | fastlane/readme.md
66 |
--------------------------------------------------------------------------------
/.idea/checkstyle-idea.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
15 |
16 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.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 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/copyright/Baidu.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/google-java-format.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Android Advance With Geektime
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # JVMTI_Sample
2 |
3 | 该例子主要展示了JVM TI支持的几种功能:
4 |
5 | 1. Memory Alloc Tracker
6 | 2. GC Event Tracker
7 | 3. JNI Method Rebind
8 | 4. Class Retransform
9 |
10 | 注意:例子最好在9.0上测试,支持模拟器,8.0下修改 Class 功能无效
11 |
12 | 运行界面
13 | ======
14 |
15 | 
16 |
17 | 产生的日志可以在 Logcat 中查看
18 |
19 | ```
20 | I/jvmti: ==========alloc callback======= [I {size:32}
21 | I/jvmti: ==========alloc callback======= java.lang.ref.WeakReference {size:24}
22 | I/jvmti: ==========alloc callback======= java.lang.ref.WeakReference {size:24}
23 | I/jvmti: ==========alloc callback======= [Ljava.lang.Object; {size:16}
24 | I/jvmti: ==========触发 GCStart=======
25 | I/jvmti: ==========触发 GCFinish=======
26 | ```
--------------------------------------------------------------------------------
/Screenshot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidAdvanceWithGeektime/JVMTI_Sample/3d0213160f49132706c134f7c059b3fe3e7d826d/Screenshot.png
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | defaultConfig {
6 | applicationId "com.dodola.jvmti"
7 | minSdkVersion 14
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 |
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 |
21 | }
22 |
23 | dependencies {
24 | implementation fileTree(include: ['*.jar'], dir: 'libs')
25 | implementation 'com.android.support.constraint:constraint-layout:1.1.3'
26 | testImplementation 'junit:junit:4.12'
27 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
28 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
29 | implementation project(':jvmtilib')
30 | }
31 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/dodola/jvmti/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.dodola.jvmti;
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() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.dodola.jvmti", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/java/com/dodola/jvmti/Main2Activity.java:
--------------------------------------------------------------------------------
1 | package com.dodola.jvmti;
2 |
3 | import android.os.Bundle;
4 | import android.app.Activity;
5 |
6 | public class Main2Activity extends Activity {
7 |
8 | @Override
9 | protected void onCreate(Bundle savedInstanceState) {
10 | super.onCreate(savedInstanceState);
11 | setContentView(R.layout.activity_main2);
12 | }
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/app/src/main/java/com/dodola/jvmti/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.dodola.jvmti;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.view.View;
7 | import android.widget.Button;
8 |
9 | import com.dodola.jvmtilib.JVMTIHelper;
10 |
11 | public class MainActivity extends Activity {
12 |
13 | @Override
14 | protected void onCreate(Bundle savedInstanceState) {
15 | super.onCreate(savedInstanceState);
16 | setContentView(R.layout.activity_main);
17 |
18 | Button tv = findViewById(R.id.sample_text);
19 | tv.setOnClickListener(new View.OnClickListener() {
20 | @Override
21 | public void onClick(View v) {
22 | JVMTIHelper.init(MainActivity.this);
23 | }
24 | });
25 | findViewById(R.id.button_gc).setOnClickListener(new View.OnClickListener() {
26 | @Override
27 | public void onClick(View v) {
28 | System.gc();
29 | System.runFinalization();
30 | }
31 | });
32 | findViewById(R.id.button_modify_class).setOnClickListener(new View.OnClickListener() {
33 | @Override
34 | public void onClick(View v) {
35 | JVMTIHelper.retransformClasses(new Class[]{Activity.class});
36 | }
37 | });
38 | findViewById(R.id.button_start_activity).setOnClickListener(new View.OnClickListener() {
39 | @Override
40 | public void onClick(View v) {
41 | startActivity(new Intent(MainActivity.this, Main2Activity.class));
42 | }
43 | });
44 | }
45 |
46 | }
47 |
48 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
20 |
25 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main2.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidAdvanceWithGeektime/JVMTI_Sample/3d0213160f49132706c134f7c059b3fe3e7d826d/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidAdvanceWithGeektime/JVMTI_Sample/3d0213160f49132706c134f7c059b3fe3e7d826d/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidAdvanceWithGeektime/JVMTI_Sample/3d0213160f49132706c134f7c059b3fe3e7d826d/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidAdvanceWithGeektime/JVMTI_Sample/3d0213160f49132706c134f7c059b3fe3e7d826d/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidAdvanceWithGeektime/JVMTI_Sample/3d0213160f49132706c134f7c059b3fe3e7d826d/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidAdvanceWithGeektime/JVMTI_Sample/3d0213160f49132706c134f7c059b3fe3e7d826d/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidAdvanceWithGeektime/JVMTI_Sample/3d0213160f49132706c134f7c059b3fe3e7d826d/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidAdvanceWithGeektime/JVMTI_Sample/3d0213160f49132706c134f7c059b3fe3e7d826d/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidAdvanceWithGeektime/JVMTI_Sample/3d0213160f49132706c134f7c059b3fe3e7d826d/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidAdvanceWithGeektime/JVMTI_Sample/3d0213160f49132706c134f7c059b3fe3e7d826d/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | JVMTI
3 | Main2Activity
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/test/java/com/dodola/jvmti/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.dodola.jvmti;
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() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/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.2.1'
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 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
15 |
16 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AndroidAdvanceWithGeektime/JVMTI_Sample/3d0213160f49132706c134f7c059b3fe3e7d826d/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/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 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
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 Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/jvmtilib/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # IntelliJ
36 | *.iml
37 | .idea/workspace.xml
38 | .idea/tasks.xml
39 | .idea/gradle.xml
40 | .idea/assetWizardSettings.xml
41 | .idea/dictionaries
42 | .idea/libraries
43 | .idea/caches
44 |
45 | # Keystore files
46 | # Uncomment the following line if you do not want to check your keystore files in.
47 | #*.jks
48 |
49 | # External native build folder generated in Android Studio 2.2 and later
50 | .externalNativeBuild
51 |
52 | # Google Services (e.g. APIs or Firebase)
53 | google-services.json
54 |
55 | # Freeline
56 | freeline.py
57 | freeline/
58 | freeline_project_description.json
59 |
60 | # fastlane
61 | fastlane/report.xml
62 | fastlane/Preview.html
63 | fastlane/screenshots
64 | fastlane/test_output
65 | fastlane/readme.md
--------------------------------------------------------------------------------
/jvmtilib/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 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -frtti -Wall -Wno-unused-parameter -Wno-unused-variable -Wno-shift-count-overflow -Wno-error=non-virtual-dtor -Wno-sign-compare -Wno-switch -Wno-missing-braces")
13 |
14 | add_library(
15 | dexter
16 | STATIC
17 | src/main/cpp/dexer/bytecode_encoder.cc
18 | src/main/cpp/dexer/code_ir.cc
19 | src/main/cpp/dexer/common.cc
20 | src/main/cpp/dexer/control_flow_graph.cc
21 | src/main/cpp/dexer/debuginfo_encoder.cc
22 | src/main/cpp/dexer/dex_bytecode.cc
23 | src/main/cpp/dexer/dex_format.cc
24 | src/main/cpp/dexer/dex_ir.cc
25 | src/main/cpp/dexer/dex_ir_builder.cc
26 | src/main/cpp/dexer/dex_utf8.cc
27 | src/main/cpp/dexer/instrumentation.cc
28 | src/main/cpp/dexer/reader.cc
29 | src/main/cpp/dexer/tryblocks_encoder.cc
30 | src/main/cpp/dexer/writer.cc
31 |
32 | )
33 | include_directories(src/main/cpp/dexer)
34 | add_library( # Sets the name of the library.
35 | jvmti_agent
36 |
37 | # Sets the library as a shared library.
38 | SHARED
39 |
40 | # Provides a relative path to your source file(s).
41 | src/main/cpp/native-lib.cpp)
42 |
43 | # Searches for a specified prebuilt library and stores the path as a
44 | # variable. Because CMake includes system libraries in the search path by
45 | # default, you only need to specify the name of the public NDK library
46 | # you want to add. CMake verifies that the library exists before
47 | # completing its build.
48 |
49 | find_library( # Sets the name of the path variable.
50 | log-lib
51 |
52 | # Specifies the name of the NDK library that
53 | # you want CMake to locate.
54 | log)
55 |
56 | # Specifies libraries CMake should link to your target library. You
57 | # can link multiple libraries, such as libraries you define in this
58 | # build script, prebuilt third-party libraries, or system libraries.
59 | target_link_libraries( # Specifies the target library.
60 | jvmti_agent
61 | dexter
62 | z
63 | # Links the target library to the log library
64 | # included in the NDK.
65 | ${log-lib})
--------------------------------------------------------------------------------
/jvmtilib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 28
5 |
6 |
7 |
8 | defaultConfig {
9 | minSdkVersion 14
10 | targetSdkVersion 28
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15 | externalNativeBuild {
16 | cmake {
17 | cppFlags ""
18 | abiFilters "armeabi-v7a", "arm64-v8a", "x86"
19 |
20 | }
21 | }
22 | }
23 |
24 | buildTypes {
25 | release {
26 | minifyEnabled false
27 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
28 | }
29 | }
30 | externalNativeBuild {
31 | cmake {
32 | path "CMakeLists.txt"
33 | }
34 | }
35 | }
36 |
37 | dependencies {
38 | implementation fileTree(dir: 'libs', include: ['*.jar'])
39 |
40 | implementation 'com.android.support:appcompat-v7:28.0.0'
41 | testImplementation 'junit:junit:4.12'
42 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
43 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
44 | }
45 |
--------------------------------------------------------------------------------
/jvmtilib/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 |
--------------------------------------------------------------------------------
/jvmtilib/src/androidTest/java/com/dodola/jvmtilib/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.dodola.jvmtilib;
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() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.dodola.jvmtilib.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/jvmtilib/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/jvmtilib/src/main/cpp/dexer/Android.bp:
--------------------------------------------------------------------------------
1 | cc_defaults {
2 | name: "slicer_defaults",
3 | srcs: [
4 | "bytecode_encoder.cc",
5 | "code_ir.cc",
6 | "common.cc",
7 | "control_flow_graph.cc",
8 | "debuginfo_encoder.cc",
9 | "dex_bytecode.cc",
10 | "dex_format.cc",
11 | "dex_ir.cc",
12 | "dex_ir_builder.cc",
13 | "dex_utf8.cc",
14 | "instrumentation.cc",
15 | "reader.cc",
16 | "tryblocks_encoder.cc",
17 | "writer.cc",
18 | ],
19 | export_include_dirs: ["export"],
20 | device_supported: true,
21 | shared_libs: ["libz"],
22 | rtti: true,
23 | cflags: [
24 | "-Wall",
25 | "-Werror",
26 | // TODO Remove these flags: b/65298177
27 | "-Wno-sign-compare",
28 | "-Wno-unused-parameter",
29 | "-Wno-shift-count-overflow",
30 | "-Wno-missing-braces",
31 | ],
32 | }
33 |
34 | cc_library_static {
35 | name: "slicer_ndk",
36 | defaults: [
37 | "slicer_defaults",
38 | ],
39 | host_supported: false,
40 | sdk_version: "current",
41 | stl: "libc++_static",
42 | }
43 |
44 | cc_library {
45 | name: "slicer",
46 | defaults: [
47 | "slicer_defaults",
48 | ],
49 | host_supported: true,
50 | }
51 |
52 |
--------------------------------------------------------------------------------
/jvmtilib/src/main/cpp/dexer/common.cc:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include "slicer/common.h"
18 |
19 | #include
20 | #include
21 | #include
22 | #include
23 | #include
24 |
25 | namespace slicer {
26 |
27 | // Helper for the default SLICER_CHECK() policy
28 | void _checkFailed(const char* expr, int line, const char* file) {
29 | printf("\nSLICER_CHECK failed [%s] at %s:%d\n\n", expr, file, line);
30 | abort();
31 | }
32 |
33 | // keep track of the failures we already saw to avoid spamming with duplicates
34 | thread_local std::set> weak_failures;
35 |
36 | // Helper for the default SLICER_WEAK_CHECK() policy
37 | //
38 | // TODO: implement a modal switch (abort/continue)
39 | //
40 | void _weakCheckFailed(const char* expr, int line, const char* file) {
41 | auto failure_id = std::make_pair(line, file);
42 | if (weak_failures.find(failure_id) == weak_failures.end()) {
43 | printf("\nSLICER_WEAK_CHECK failed [%s] at %s:%d\n\n", expr, file, line);
44 | weak_failures.insert(failure_id);
45 | }
46 | }
47 |
48 | // Prints a formatted message and aborts
49 | void _fatal(const char* format, ...) {
50 | va_list args;
51 | va_start(args, format);
52 | vprintf(format, args);
53 | va_end(args);
54 | abort();
55 | }
56 |
57 | } // namespace slicer
58 |
59 |
--------------------------------------------------------------------------------
/jvmtilib/src/main/cpp/dexer/control_flow_graph.cc:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include "slicer/control_flow_graph.h"
18 | #include "slicer/chronometer.h"
19 |
20 | namespace lir {
21 |
22 | std::vector BasicBlocksVisitor::Finish() {
23 | // the .dex format specification has the following constraint:
24 | //
25 | // B17 The last reachable instruction of a method must either be a
26 | // backwards goto or branch, a return, or a throw instruction. It must not
27 | // be possible to leave the insns array at the bottom. 4.8.2.20
28 | //
29 | // NOTE: this is a very aggressive check though since in the LIR we also
30 | // have labels, annotations, directives, etc. For example it's possible to have
31 | // debug annotations (.line, .endlocal, ...) after the last bytecode.
32 | //
33 | SLICER_WEAK_CHECK(state_ == State::Outside);
34 | SLICER_CHECK(state_ != State::BlockBody);
35 | current_block_.region = {};
36 | state_ = State::Outside;
37 | return std::move(basic_blocks_);
38 | }
39 |
40 | bool BasicBlocksVisitor::Visit(Bytecode* bytecode) {
41 | switch (state_) {
42 | case State::Outside:
43 | StartBlock(bytecode);
44 | state_ = State::BlockBody;
45 | break;
46 | case State::BlockHeader:
47 | state_ = State::BlockBody;
48 | break;
49 | case State::BlockBody:
50 | // inside basic block body, nothing to do.
51 | break;
52 | }
53 |
54 | // terminate the current block?
55 | bool terminate_block = false;
56 | const auto flags = dex::GetFlagsFromOpcode(bytecode->opcode);
57 | if (model_exceptions_) {
58 | constexpr auto exit_instr_flags =
59 | dex::kInstrCanBranch |
60 | dex::kInstrCanSwitch |
61 | dex::kInstrCanThrow |
62 | dex::kInstrCanReturn;
63 | terminate_block = (flags & exit_instr_flags) != 0;
64 | } else {
65 | constexpr auto exit_instr_flags =
66 | dex::kInstrCanBranch |
67 | dex::kInstrCanSwitch |
68 | dex::kInstrCanReturn;
69 | terminate_block = bytecode->opcode == dex::OP_THROW || (flags & exit_instr_flags) != 0;
70 | }
71 | if (terminate_block) {
72 | EndBlock(bytecode);
73 | }
74 |
75 | return true;
76 | }
77 |
78 | bool BasicBlocksVisitor::Visit(Label* label) {
79 | switch (state_) {
80 | case State::Outside:
81 | StartBlock(label);
82 | break;
83 | case State::BlockBody:
84 | EndBlock(label->prev);
85 | StartBlock(label);
86 | break;
87 | case State::BlockHeader:
88 | break;
89 | }
90 | return true;
91 | }
92 |
93 | bool BasicBlocksVisitor::HandleAnnotation(Instruction* instr) {
94 | if (state_ == State::Outside) {
95 | StartBlock(instr);
96 | }
97 | return true;
98 | }
99 |
100 | bool BasicBlocksVisitor::SkipInstruction(Instruction* instr) {
101 | if (state_ != State::Outside) {
102 | EndBlock(instr->prev);
103 | }
104 | return true;
105 | }
106 |
107 | void BasicBlocksVisitor::StartBlock(Instruction* instr) {
108 | assert(instr != nullptr);
109 | assert(state_ == State::Outside);
110 | // mark the location of the "first" instruction,
111 | // "last" will be set when we end the basic block.
112 | current_block_.region.first = instr;
113 | current_block_.region.last = nullptr;
114 | state_ = State::BlockHeader;
115 | }
116 |
117 | void BasicBlocksVisitor::EndBlock(Instruction* instr) {
118 | assert(instr != nullptr);
119 | if (state_ == State::BlockBody) {
120 | ++current_block_.id;
121 | assert(current_block_.region.first != nullptr);
122 | current_block_.region.last = instr;
123 | basic_blocks_.push_back(current_block_);
124 | } else {
125 | assert(state_ == State::BlockHeader);
126 | }
127 | current_block_.region = {};
128 | state_ = State::Outside;
129 | }
130 |
131 | void ControlFlowGraph::CreateBasicBlocks(bool model_exceptions) {
132 | BasicBlocksVisitor visitor(model_exceptions);
133 | for (auto instr : code_ir->instructions) {
134 | instr->Accept(&visitor);
135 | }
136 | basic_blocks = visitor.Finish();
137 | }
138 |
139 | } // namespace lir
140 |
--------------------------------------------------------------------------------
/jvmtilib/src/main/cpp/dexer/debuginfo_encoder.cc:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include "slicer/common.h"
18 | #include "slicer/debuginfo_encoder.h"
19 | #include "slicer/chronometer.h"
20 |
21 | #include
22 |
23 | namespace lir {
24 |
25 | bool DebugInfoEncoder::Visit(DbgInfoHeader* dbg_header) {
26 | assert(param_names_ == nullptr);
27 | param_names_ = &dbg_header->param_names;
28 | return true;
29 | }
30 |
31 | bool DebugInfoEncoder::Visit(DbgInfoAnnotation* dbg_annotation) {
32 | // keep the address in sync
33 | if (last_address_ != dbg_annotation->offset) {
34 | SLICER_CHECK(dbg_annotation->offset > last_address_);
35 | dbginfo_.Push(dex::DBG_ADVANCE_PC);
36 | dbginfo_.PushULeb128(dbg_annotation->offset - last_address_);
37 | last_address_ = dbg_annotation->offset;
38 | }
39 |
40 | // encode the annotation itself
41 | switch (dbg_annotation->dbg_opcode) {
42 | case dex::DBG_ADVANCE_LINE: {
43 | // DBG_ANDVANCE_LINE is used a bit differently in the code IR
44 | // vs the .dex image: the code IR uses it exclusively for source
45 | // location (the .line directive) while .dex format uses it to
46 | // advance the "line" register without emitting a "position entry"
47 | int line = dbg_annotation->CastOperand(0)->line;
48 | if (line_start_ == 0) {
49 | // it's not perfectly clear from the .dex specification
50 | // if initial line == 0 is valid, but a number of existing
51 | // .dex files do this so we have to support it
52 | SLICER_CHECK(line >= 0);
53 | line_start_ = line;
54 | } else {
55 | SLICER_WEAK_CHECK(line > 0);
56 | int delta = line - last_line_;
57 | int adj_opcode = delta - dex::DBG_LINE_BASE;
58 | // out of range for special opcode?
59 | if (adj_opcode < 0 || adj_opcode >= dex::DBG_LINE_RANGE) {
60 | dbginfo_.Push(dex::DBG_ADVANCE_LINE);
61 | dbginfo_.PushSLeb128(delta);
62 | adj_opcode = -dex::DBG_LINE_BASE;
63 | }
64 | assert(adj_opcode >= 0 && dex::DBG_FIRST_SPECIAL + adj_opcode < 256);
65 | dex::u1 special_opcode = dex::DBG_FIRST_SPECIAL + adj_opcode;
66 | dbginfo_.Push(special_opcode);
67 | }
68 | last_line_ = line;
69 | } break;
70 |
71 | case dex::DBG_START_LOCAL: {
72 | auto reg = dbg_annotation->CastOperand(0)->reg;
73 | auto name_index = dbg_annotation->CastOperand(1)->index;
74 | auto type_index = dbg_annotation->CastOperand(2)->index;
75 | dbginfo_.Push(dex::DBG_START_LOCAL);
76 | dbginfo_.PushULeb128(reg);
77 | dbginfo_.PushULeb128(name_index + 1);
78 | dbginfo_.PushULeb128(type_index + 1);
79 | } break;
80 |
81 | case dex::DBG_START_LOCAL_EXTENDED: {
82 | auto reg = dbg_annotation->CastOperand(0)->reg;
83 | auto name_index = dbg_annotation->CastOperand(1)->index;
84 | auto type_index = dbg_annotation->CastOperand(2)->index;
85 | auto sig_index = dbg_annotation->CastOperand(3)->index;
86 | dbginfo_.Push(dex::DBG_START_LOCAL_EXTENDED);
87 | dbginfo_.PushULeb128(reg);
88 | dbginfo_.PushULeb128(name_index + 1);
89 | dbginfo_.PushULeb128(type_index + 1);
90 | dbginfo_.PushULeb128(sig_index + 1);
91 | } break;
92 |
93 | case dex::DBG_END_LOCAL:
94 | case dex::DBG_RESTART_LOCAL: {
95 | auto reg = dbg_annotation->CastOperand(0)->reg;
96 | dbginfo_.Push(dbg_annotation->dbg_opcode);
97 | dbginfo_.PushULeb128(reg);
98 | } break;
99 |
100 | case dex::DBG_SET_PROLOGUE_END:
101 | case dex::DBG_SET_EPILOGUE_BEGIN:
102 | dbginfo_.Push(dbg_annotation->dbg_opcode);
103 | break;
104 |
105 | case dex::DBG_SET_FILE: {
106 | auto file_name = dbg_annotation->CastOperand(0);
107 | if (file_name->ir_string != source_file_) {
108 | source_file_ = file_name->ir_string;
109 | dbginfo_.Push(dex::DBG_SET_FILE);
110 | dbginfo_.PushULeb128(file_name->index + 1);
111 | }
112 | } break;
113 |
114 | default:
115 | SLICER_FATAL("Unexpected debug info opcode: 0x%02x", dbg_annotation->dbg_opcode);
116 | }
117 |
118 | return true;
119 | }
120 |
121 | void DebugInfoEncoder::Encode(ir::EncodedMethod* ir_method, std::shared_ptr dex_ir) {
122 | auto ir_debug_info = ir_method->code->debug_info;
123 |
124 | SLICER_CHECK(dbginfo_.empty());
125 | SLICER_CHECK(param_names_ == nullptr);
126 | SLICER_CHECK(line_start_ == 0);
127 | SLICER_CHECK(last_line_ == 0);
128 | SLICER_CHECK(last_address_ == 0);
129 | SLICER_CHECK(source_file_ == nullptr);
130 |
131 | // generate new debug info
132 | source_file_ = ir_method->decl->parent->class_def->source_file;
133 | for (auto instr : instructions_) {
134 | instr->Accept(this);
135 | }
136 | dbginfo_.Push(dex::DBG_END_SEQUENCE);
137 | dbginfo_.Seal(1);
138 |
139 | SLICER_CHECK(!dbginfo_.empty());
140 |
141 | // update ir::DebugInfo
142 | ir_debug_info->line_start = line_start_;
143 | ir_debug_info->data = slicer::MemView(dbginfo_.data(), dbginfo_.size());
144 |
145 | if (param_names_ != nullptr) {
146 | ir_debug_info->param_names = *param_names_;
147 | } else {
148 | ir_debug_info->param_names = {};
149 | }
150 |
151 | // attach the debug info buffer to the dex IR
152 | dex_ir->AttachBuffer(std::move(dbginfo_));
153 | }
154 |
155 | } // namespace lir
156 |
--------------------------------------------------------------------------------
/jvmtilib/src/main/cpp/dexer/dex_format.cc:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include "slicer/dex_format.h"
18 | #include "slicer/common.h"
19 |
20 | #include
21 | #include
22 |
23 | namespace dex {
24 |
25 | // Compute the DEX file checksum for a memory-mapped DEX file
26 | u4 ComputeChecksum(const Header* header) {
27 | const u1* start = reinterpret_cast(header);
28 |
29 | uLong adler = adler32(0L, Z_NULL, 0);
30 | const int non_sum = sizeof(header->magic) + sizeof(header->checksum);
31 |
32 | return static_cast(
33 | adler32(adler, start + non_sum, header->file_size - non_sum));
34 | }
35 |
36 | // Returns the human-readable name for a primitive type
37 | static const char* PrimitiveTypeName(char type_char) {
38 | switch (type_char) {
39 | case 'B': return "byte";
40 | case 'C': return "char";
41 | case 'D': return "double";
42 | case 'F': return "float";
43 | case 'I': return "int";
44 | case 'J': return "long";
45 | case 'S': return "short";
46 | case 'V': return "void";
47 | case 'Z': return "boolean";
48 | }
49 | SLICER_CHECK(!"unexpected type");
50 | return nullptr;
51 | }
52 |
53 | // Converts a type descriptor to human-readable "dotted" form. For
54 | // example, "Ljava/lang/String;" becomes "java.lang.String", and
55 | // "[I" becomes "int[]".
56 | std::string DescriptorToDecl(const char* descriptor) {
57 | std::stringstream ss;
58 |
59 | int array_dimensions = 0;
60 | while (*descriptor == '[') {
61 | ++array_dimensions;
62 | ++descriptor;
63 | }
64 |
65 | if (*descriptor == 'L') {
66 | for (++descriptor; *descriptor != ';'; ++descriptor) {
67 | SLICER_CHECK(*descriptor != '\0');
68 | ss << (*descriptor == '/' ? '.' : *descriptor);
69 | }
70 | } else {
71 | ss << PrimitiveTypeName(*descriptor);
72 | }
73 |
74 | SLICER_CHECK(descriptor[1] == '\0');
75 |
76 | // add the array brackets
77 | for (int i = 0; i < array_dimensions; ++i) {
78 | ss << "[]";
79 | }
80 |
81 | return ss.str();
82 | }
83 |
84 | // Converts a type descriptor to a single "shorty" char
85 | // (ex. "LFoo;" and "[[I" become 'L', "I" stays 'I')
86 | char DescriptorToShorty(const char* descriptor) {
87 | // skip array dimensions
88 | int array_dimensions = 0;
89 | while (*descriptor == '[') {
90 | ++array_dimensions;
91 | ++descriptor;
92 | }
93 |
94 | char short_descriptor = *descriptor;
95 | if (short_descriptor == 'L') {
96 | // skip the full class name
97 | for(; *descriptor && *descriptor != ';'; ++descriptor);
98 | SLICER_CHECK(*descriptor == ';');
99 | }
100 |
101 | SLICER_CHECK(descriptor[1] == '\0');
102 | SLICER_CHECK(short_descriptor == 'L' || PrimitiveTypeName(short_descriptor) != nullptr);
103 |
104 | return array_dimensions > 0 ? 'L' : short_descriptor;
105 | }
106 |
107 | } // namespace dex
108 |
--------------------------------------------------------------------------------
/jvmtilib/src/main/cpp/dexer/dex_ir.cc:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | #include "slicer/dex_ir.h"
18 | #include "slicer/chronometer.h"
19 | #include "slicer/dex_utf8.h"
20 | #include "slicer/dex_format.h"
21 |
22 | #include
23 | #include
24 | #include