Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 "classes.h" |
| 18 | |
| 19 | #include <stdio.h> |
| 20 | |
| 21 | #include "base/macros.h" |
| 22 | #include "jni.h" |
| 23 | #include "openjdkjvmti/jvmti.h" |
| 24 | #include "ScopedLocalRef.h" |
| 25 | |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 26 | #include "ti-agent/common_helper.h" |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 27 | #include "ti-agent/common_load.h" |
| 28 | |
| 29 | namespace art { |
| 30 | namespace Test912Classes { |
| 31 | |
| 32 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getClassSignature( |
| 33 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 34 | char* sig; |
| 35 | char* gen; |
| 36 | jvmtiError result = jvmti_env->GetClassSignature(klass, &sig, &gen); |
| 37 | if (result != JVMTI_ERROR_NONE) { |
| 38 | char* err; |
| 39 | jvmti_env->GetErrorName(result, &err); |
| 40 | printf("Failure running GetClassSignature: %s\n", err); |
| 41 | return nullptr; |
| 42 | } |
| 43 | |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 44 | auto callback = [&](jint i) { |
| 45 | if (i == 0) { |
| 46 | return sig == nullptr ? nullptr : env->NewStringUTF(sig); |
| 47 | } else { |
| 48 | return gen == nullptr ? nullptr : env->NewStringUTF(gen); |
| 49 | } |
| 50 | }; |
| 51 | jobjectArray ret = CreateObjectArray(env, 2, "java/lang/String", callback); |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 52 | |
| 53 | // Need to deallocate the strings. |
| 54 | if (sig != nullptr) { |
| 55 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig)); |
| 56 | } |
| 57 | if (gen != nullptr) { |
| 58 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen)); |
| 59 | } |
| 60 | |
| 61 | return ret; |
| 62 | } |
| 63 | |
Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 64 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterface( |
| 65 | JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 66 | jboolean is_interface = JNI_FALSE; |
| 67 | jvmtiError result = jvmti_env->IsInterface(klass, &is_interface); |
| 68 | if (result != JVMTI_ERROR_NONE) { |
| 69 | char* err; |
| 70 | jvmti_env->GetErrorName(result, &err); |
| 71 | printf("Failure running IsInterface: %s\n", err); |
| 72 | return JNI_FALSE; |
| 73 | } |
| 74 | return is_interface; |
| 75 | } |
| 76 | |
| 77 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isArrayClass( |
| 78 | JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 79 | jboolean is_array_class = JNI_FALSE; |
| 80 | jvmtiError result = jvmti_env->IsArrayClass(klass, &is_array_class); |
| 81 | if (result != JVMTI_ERROR_NONE) { |
| 82 | char* err; |
| 83 | jvmti_env->GetErrorName(result, &err); |
| 84 | printf("Failure running IsArrayClass: %s\n", err); |
| 85 | return JNI_FALSE; |
| 86 | } |
| 87 | return is_array_class; |
| 88 | } |
| 89 | |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 90 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getClassFields( |
| 91 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 92 | jint count = 0; |
| 93 | jfieldID* fields = nullptr; |
| 94 | jvmtiError result = jvmti_env->GetClassFields(klass, &count, &fields); |
| 95 | if (result != JVMTI_ERROR_NONE) { |
| 96 | char* err; |
| 97 | jvmti_env->GetErrorName(result, &err); |
| 98 | printf("Failure running GetClassFields: %s\n", err); |
| 99 | return nullptr; |
| 100 | } |
| 101 | |
| 102 | auto callback = [&](jint i) { |
| 103 | jint modifiers; |
| 104 | // Ignore any errors for simplicity. |
| 105 | jvmti_env->GetFieldModifiers(klass, fields[i], &modifiers); |
| 106 | constexpr jint kStatic = 0x8; |
| 107 | return env->ToReflectedField(klass, |
| 108 | fields[i], |
| 109 | (modifiers & kStatic) != 0 ? JNI_TRUE : JNI_FALSE); |
| 110 | }; |
| 111 | return CreateObjectArray(env, count, "java/lang/Object", callback); |
| 112 | } |
| 113 | |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 114 | extern "C" JNIEXPORT jint JNICALL Java_Main_getClassStatus( |
| 115 | JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 116 | jint status; |
| 117 | jvmtiError result = jvmti_env->GetClassStatus(klass, &status); |
| 118 | if (result != JVMTI_ERROR_NONE) { |
| 119 | char* err; |
| 120 | jvmti_env->GetErrorName(result, &err); |
| 121 | printf("Failure running GetClassStatus: %s\n", err); |
| 122 | return JNI_FALSE; |
| 123 | } |
| 124 | return status; |
| 125 | } |
| 126 | |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 127 | // Don't do anything |
| 128 | jint OnLoad(JavaVM* vm, |
| 129 | char* options ATTRIBUTE_UNUSED, |
| 130 | void* reserved ATTRIBUTE_UNUSED) { |
| 131 | if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) { |
| 132 | printf("Unable to get jvmti env!\n"); |
| 133 | return 1; |
| 134 | } |
Alex Light | e657424 | 2016-08-17 09:56:24 -0700 | [diff] [blame] | 135 | SetAllCapabilities(jvmti_env); |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | } // namespace Test912Classes |
| 140 | } // namespace art |