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 | 64013e5 | 2017-01-06 13:07:19 -0800 | [diff] [blame] | 90 | extern "C" JNIEXPORT jint JNICALL Java_Main_getClassModifiers( |
| 91 | JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 92 | jint mod; |
| 93 | jvmtiError result = jvmti_env->GetClassModifiers(klass, &mod); |
| 94 | if (result != JVMTI_ERROR_NONE) { |
| 95 | char* err; |
| 96 | jvmti_env->GetErrorName(result, &err); |
| 97 | printf("Failure running GetClassModifiers: %s\n", err); |
| 98 | return JNI_FALSE; |
| 99 | } |
| 100 | return mod; |
| 101 | } |
| 102 | |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 103 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getClassFields( |
| 104 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 105 | jint count = 0; |
| 106 | jfieldID* fields = nullptr; |
| 107 | jvmtiError result = jvmti_env->GetClassFields(klass, &count, &fields); |
| 108 | if (result != JVMTI_ERROR_NONE) { |
| 109 | char* err; |
| 110 | jvmti_env->GetErrorName(result, &err); |
| 111 | printf("Failure running GetClassFields: %s\n", err); |
| 112 | return nullptr; |
| 113 | } |
| 114 | |
| 115 | auto callback = [&](jint i) { |
| 116 | jint modifiers; |
| 117 | // Ignore any errors for simplicity. |
| 118 | jvmti_env->GetFieldModifiers(klass, fields[i], &modifiers); |
| 119 | constexpr jint kStatic = 0x8; |
| 120 | return env->ToReflectedField(klass, |
| 121 | fields[i], |
| 122 | (modifiers & kStatic) != 0 ? JNI_TRUE : JNI_FALSE); |
| 123 | }; |
Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 124 | jobjectArray ret = CreateObjectArray(env, count, "java/lang/Object", callback); |
| 125 | if (fields != nullptr) { |
| 126 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(fields)); |
| 127 | } |
| 128 | return ret; |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 131 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getClassMethods( |
| 132 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 133 | jint count = 0; |
| 134 | jmethodID* methods = nullptr; |
| 135 | jvmtiError result = jvmti_env->GetClassMethods(klass, &count, &methods); |
| 136 | if (result != JVMTI_ERROR_NONE) { |
| 137 | char* err; |
| 138 | jvmti_env->GetErrorName(result, &err); |
| 139 | printf("Failure running GetClassMethods: %s\n", err); |
| 140 | return nullptr; |
| 141 | } |
| 142 | |
| 143 | auto callback = [&](jint i) { |
| 144 | jint modifiers; |
| 145 | // Ignore any errors for simplicity. |
| 146 | jvmti_env->GetMethodModifiers(methods[i], &modifiers); |
| 147 | constexpr jint kStatic = 0x8; |
| 148 | return env->ToReflectedMethod(klass, |
| 149 | methods[i], |
| 150 | (modifiers & kStatic) != 0 ? JNI_TRUE : JNI_FALSE); |
| 151 | }; |
Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 152 | jobjectArray ret = CreateObjectArray(env, count, "java/lang/Object", callback); |
| 153 | if (methods != nullptr) { |
| 154 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(methods)); |
| 155 | } |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getImplementedInterfaces( |
| 160 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 161 | jint count = 0; |
| 162 | jclass* classes = nullptr; |
| 163 | jvmtiError result = jvmti_env->GetImplementedInterfaces(klass, &count, &classes); |
| 164 | if (result != JVMTI_ERROR_NONE) { |
| 165 | char* err; |
| 166 | jvmti_env->GetErrorName(result, &err); |
| 167 | printf("Failure running GetImplementedInterfaces: %s\n", err); |
| 168 | return nullptr; |
| 169 | } |
| 170 | |
| 171 | auto callback = [&](jint i) { |
| 172 | return classes[i]; |
| 173 | }; |
| 174 | jobjectArray ret = CreateObjectArray(env, count, "java/lang/Class", callback); |
| 175 | if (classes != nullptr) { |
| 176 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(classes)); |
| 177 | } |
| 178 | return ret; |
Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 181 | extern "C" JNIEXPORT jint JNICALL Java_Main_getClassStatus( |
| 182 | JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 183 | jint status; |
| 184 | jvmtiError result = jvmti_env->GetClassStatus(klass, &status); |
| 185 | if (result != JVMTI_ERROR_NONE) { |
| 186 | char* err; |
| 187 | jvmti_env->GetErrorName(result, &err); |
| 188 | printf("Failure running GetClassStatus: %s\n", err); |
| 189 | return JNI_FALSE; |
| 190 | } |
| 191 | return status; |
| 192 | } |
| 193 | |
Andreas Gampe | 8f5b603 | 2017-01-06 15:50:55 -0800 | [diff] [blame^] | 194 | extern "C" JNIEXPORT jobject JNICALL Java_Main_getClassLoader( |
| 195 | JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 196 | jobject classloader; |
| 197 | jvmtiError result = jvmti_env->GetClassLoader(klass, &classloader); |
| 198 | if (result != JVMTI_ERROR_NONE) { |
| 199 | char* err; |
| 200 | jvmti_env->GetErrorName(result, &err); |
| 201 | printf("Failure running GetClassLoader: %s\n", err); |
| 202 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
| 203 | return nullptr; |
| 204 | } |
| 205 | return classloader; |
| 206 | } |
| 207 | |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 208 | // Don't do anything |
| 209 | jint OnLoad(JavaVM* vm, |
| 210 | char* options ATTRIBUTE_UNUSED, |
| 211 | void* reserved ATTRIBUTE_UNUSED) { |
| 212 | if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) { |
| 213 | printf("Unable to get jvmti env!\n"); |
| 214 | return 1; |
| 215 | } |
Alex Light | e657424 | 2016-08-17 09:56:24 -0700 | [diff] [blame] | 216 | SetAllCapabilities(jvmti_env); |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | } // namespace Test912Classes |
| 221 | } // namespace art |