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 | |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | |
| 19 | #include "base/macros.h" |
| 20 | #include "jni.h" |
| 21 | #include "openjdkjvmti/jvmti.h" |
| 22 | #include "ScopedLocalRef.h" |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 23 | #include "thread-inl.h" |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 24 | |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 25 | #include "ti-agent/common_helper.h" |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 26 | #include "ti-agent/common_load.h" |
| 27 | |
| 28 | namespace art { |
| 29 | namespace Test912Classes { |
| 30 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 31 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isModifiableClass( |
| 32 | JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 33 | jboolean res = JNI_FALSE; |
| 34 | jvmtiError result = jvmti_env->IsModifiableClass(klass, &res); |
| 35 | if (result != JVMTI_ERROR_NONE) { |
| 36 | char* err; |
| 37 | jvmti_env->GetErrorName(result, &err); |
| 38 | printf("Failure running IsModifiableClass: %s\n", err); |
| 39 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
| 40 | return JNI_FALSE; |
| 41 | } |
| 42 | return res; |
| 43 | } |
| 44 | |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 45 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getClassSignature( |
| 46 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 47 | char* sig; |
| 48 | char* gen; |
| 49 | jvmtiError result = jvmti_env->GetClassSignature(klass, &sig, &gen); |
| 50 | if (result != JVMTI_ERROR_NONE) { |
| 51 | char* err; |
| 52 | jvmti_env->GetErrorName(result, &err); |
| 53 | printf("Failure running GetClassSignature: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 54 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 55 | return nullptr; |
| 56 | } |
| 57 | |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 58 | auto callback = [&](jint i) { |
| 59 | if (i == 0) { |
| 60 | return sig == nullptr ? nullptr : env->NewStringUTF(sig); |
| 61 | } else { |
| 62 | return gen == nullptr ? nullptr : env->NewStringUTF(gen); |
| 63 | } |
| 64 | }; |
| 65 | jobjectArray ret = CreateObjectArray(env, 2, "java/lang/String", callback); |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 66 | |
| 67 | // Need to deallocate the strings. |
| 68 | if (sig != nullptr) { |
| 69 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig)); |
| 70 | } |
| 71 | if (gen != nullptr) { |
| 72 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen)); |
| 73 | } |
| 74 | |
| 75 | return ret; |
| 76 | } |
| 77 | |
Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 78 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterface( |
| 79 | JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 80 | jboolean is_interface = JNI_FALSE; |
| 81 | jvmtiError result = jvmti_env->IsInterface(klass, &is_interface); |
| 82 | if (result != JVMTI_ERROR_NONE) { |
| 83 | char* err; |
| 84 | jvmti_env->GetErrorName(result, &err); |
| 85 | printf("Failure running IsInterface: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 86 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 87 | return JNI_FALSE; |
| 88 | } |
| 89 | return is_interface; |
| 90 | } |
| 91 | |
| 92 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isArrayClass( |
| 93 | JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 94 | jboolean is_array_class = JNI_FALSE; |
| 95 | jvmtiError result = jvmti_env->IsArrayClass(klass, &is_array_class); |
| 96 | if (result != JVMTI_ERROR_NONE) { |
| 97 | char* err; |
| 98 | jvmti_env->GetErrorName(result, &err); |
| 99 | printf("Failure running IsArrayClass: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 100 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | 4fd66ec | 2017-01-05 14:42:13 -0800 | [diff] [blame] | 101 | return JNI_FALSE; |
| 102 | } |
| 103 | return is_array_class; |
| 104 | } |
| 105 | |
Andreas Gampe | 64013e5 | 2017-01-06 13:07:19 -0800 | [diff] [blame] | 106 | extern "C" JNIEXPORT jint JNICALL Java_Main_getClassModifiers( |
| 107 | JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 108 | jint mod; |
| 109 | jvmtiError result = jvmti_env->GetClassModifiers(klass, &mod); |
| 110 | if (result != JVMTI_ERROR_NONE) { |
| 111 | char* err; |
| 112 | jvmti_env->GetErrorName(result, &err); |
| 113 | printf("Failure running GetClassModifiers: %s\n", err); |
| 114 | return JNI_FALSE; |
| 115 | } |
| 116 | return mod; |
| 117 | } |
| 118 | |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 119 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getClassFields( |
| 120 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 121 | jint count = 0; |
| 122 | jfieldID* fields = nullptr; |
| 123 | jvmtiError result = jvmti_env->GetClassFields(klass, &count, &fields); |
| 124 | if (result != JVMTI_ERROR_NONE) { |
| 125 | char* err; |
| 126 | jvmti_env->GetErrorName(result, &err); |
| 127 | printf("Failure running GetClassFields: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 128 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 129 | return nullptr; |
| 130 | } |
| 131 | |
| 132 | auto callback = [&](jint i) { |
| 133 | jint modifiers; |
| 134 | // Ignore any errors for simplicity. |
| 135 | jvmti_env->GetFieldModifiers(klass, fields[i], &modifiers); |
| 136 | constexpr jint kStatic = 0x8; |
| 137 | return env->ToReflectedField(klass, |
| 138 | fields[i], |
| 139 | (modifiers & kStatic) != 0 ? JNI_TRUE : JNI_FALSE); |
| 140 | }; |
Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 141 | jobjectArray ret = CreateObjectArray(env, count, "java/lang/Object", callback); |
| 142 | if (fields != nullptr) { |
| 143 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(fields)); |
| 144 | } |
| 145 | return ret; |
Andreas Gampe | ac58727 | 2017-01-05 15:21:34 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 148 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getClassMethods( |
| 149 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 150 | jint count = 0; |
| 151 | jmethodID* methods = nullptr; |
| 152 | jvmtiError result = jvmti_env->GetClassMethods(klass, &count, &methods); |
| 153 | if (result != JVMTI_ERROR_NONE) { |
| 154 | char* err; |
| 155 | jvmti_env->GetErrorName(result, &err); |
| 156 | printf("Failure running GetClassMethods: %s\n", err); |
| 157 | return nullptr; |
| 158 | } |
| 159 | |
| 160 | auto callback = [&](jint i) { |
| 161 | jint modifiers; |
| 162 | // Ignore any errors for simplicity. |
| 163 | jvmti_env->GetMethodModifiers(methods[i], &modifiers); |
| 164 | constexpr jint kStatic = 0x8; |
| 165 | return env->ToReflectedMethod(klass, |
| 166 | methods[i], |
| 167 | (modifiers & kStatic) != 0 ? JNI_TRUE : JNI_FALSE); |
| 168 | }; |
Andreas Gampe | 8b07e47 | 2017-01-06 14:20:39 -0800 | [diff] [blame] | 169 | jobjectArray ret = CreateObjectArray(env, count, "java/lang/Object", callback); |
| 170 | if (methods != nullptr) { |
| 171 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(methods)); |
| 172 | } |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getImplementedInterfaces( |
| 177 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 178 | jint count = 0; |
| 179 | jclass* classes = nullptr; |
| 180 | jvmtiError result = jvmti_env->GetImplementedInterfaces(klass, &count, &classes); |
| 181 | if (result != JVMTI_ERROR_NONE) { |
| 182 | char* err; |
| 183 | jvmti_env->GetErrorName(result, &err); |
| 184 | printf("Failure running GetImplementedInterfaces: %s\n", err); |
| 185 | return nullptr; |
| 186 | } |
| 187 | |
| 188 | auto callback = [&](jint i) { |
| 189 | return classes[i]; |
| 190 | }; |
| 191 | jobjectArray ret = CreateObjectArray(env, count, "java/lang/Class", callback); |
| 192 | if (classes != nullptr) { |
| 193 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(classes)); |
| 194 | } |
| 195 | return ret; |
Andreas Gampe | 18fee4d | 2017-01-06 11:36:35 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 198 | extern "C" JNIEXPORT jint JNICALL Java_Main_getClassStatus( |
| 199 | JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 200 | jint status; |
| 201 | jvmtiError result = jvmti_env->GetClassStatus(klass, &status); |
| 202 | if (result != JVMTI_ERROR_NONE) { |
| 203 | char* err; |
| 204 | jvmti_env->GetErrorName(result, &err); |
| 205 | printf("Failure running GetClassStatus: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 206 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | ff9d209 | 2017-01-06 09:12:49 -0800 | [diff] [blame] | 207 | return JNI_FALSE; |
| 208 | } |
| 209 | return status; |
| 210 | } |
| 211 | |
Andreas Gampe | 8f5b603 | 2017-01-06 15:50:55 -0800 | [diff] [blame] | 212 | extern "C" JNIEXPORT jobject JNICALL Java_Main_getClassLoader( |
| 213 | JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 214 | jobject classloader; |
| 215 | jvmtiError result = jvmti_env->GetClassLoader(klass, &classloader); |
| 216 | if (result != JVMTI_ERROR_NONE) { |
| 217 | char* err; |
| 218 | jvmti_env->GetErrorName(result, &err); |
| 219 | printf("Failure running GetClassLoader: %s\n", err); |
| 220 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
| 221 | return nullptr; |
| 222 | } |
| 223 | return classloader; |
| 224 | } |
| 225 | |
Andreas Gampe | 70f1639 | 2017-01-16 14:20:10 -0800 | [diff] [blame] | 226 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getClassLoaderClasses( |
| 227 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jobject jclassloader) { |
| 228 | jint count = 0; |
| 229 | jclass* classes = nullptr; |
| 230 | jvmtiError result = jvmti_env->GetClassLoaderClasses(jclassloader, &count, &classes); |
| 231 | if (JvmtiErrorToException(env, result)) { |
| 232 | return nullptr; |
| 233 | } |
| 234 | |
| 235 | auto callback = [&](jint i) { |
| 236 | return classes[i]; |
| 237 | }; |
| 238 | jobjectArray ret = CreateObjectArray(env, count, "java/lang/Class", callback); |
| 239 | if (classes != nullptr) { |
| 240 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(classes)); |
| 241 | } |
| 242 | return ret; |
| 243 | } |
| 244 | |
Andreas Gampe | 812a244 | 2017-01-19 22:04:46 -0800 | [diff] [blame] | 245 | extern "C" JNIEXPORT jintArray JNICALL Java_Main_getClassVersion( |
| 246 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) { |
| 247 | jint major, minor; |
| 248 | jvmtiError result = jvmti_env->GetClassVersionNumbers(klass, &minor, &major); |
| 249 | if (JvmtiErrorToException(env, result)) { |
| 250 | return nullptr; |
| 251 | } |
| 252 | |
| 253 | jintArray int_array = env->NewIntArray(2); |
| 254 | if (int_array == nullptr) { |
| 255 | return nullptr; |
| 256 | } |
| 257 | jint buf[2] = { major, minor }; |
| 258 | env->SetIntArrayRegion(int_array, 0, 2, buf); |
| 259 | |
| 260 | return int_array; |
| 261 | } |
| 262 | |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 263 | static std::string GetClassName(jvmtiEnv* jenv, JNIEnv* jni_env, jclass klass) { |
| 264 | char* name; |
| 265 | jvmtiError result = jenv->GetClassSignature(klass, &name, nullptr); |
| 266 | if (result != JVMTI_ERROR_NONE) { |
| 267 | if (jni_env != nullptr) { |
| 268 | JvmtiErrorToException(jni_env, result); |
| 269 | } else { |
| 270 | printf("Failed to get class signature.\n"); |
| 271 | } |
| 272 | return ""; |
| 273 | } |
| 274 | |
| 275 | std::string tmp(name); |
| 276 | jenv->Deallocate(reinterpret_cast<unsigned char*>(name)); |
| 277 | |
| 278 | return tmp; |
| 279 | } |
| 280 | |
| 281 | static std::string GetThreadName(jvmtiEnv* jenv, JNIEnv* jni_env, jthread thread) { |
| 282 | jvmtiThreadInfo info; |
| 283 | jvmtiError result = jenv->GetThreadInfo(thread, &info); |
| 284 | if (result != JVMTI_ERROR_NONE) { |
| 285 | if (jni_env != nullptr) { |
| 286 | JvmtiErrorToException(jni_env, result); |
| 287 | } else { |
| 288 | printf("Failed to get thread name.\n"); |
| 289 | } |
| 290 | return ""; |
| 291 | } |
| 292 | |
| 293 | std::string tmp(info.name); |
| 294 | jenv->Deallocate(reinterpret_cast<unsigned char*>(info.name)); |
| 295 | jni_env->DeleteLocalRef(info.context_class_loader); |
| 296 | jni_env->DeleteLocalRef(info.thread_group); |
| 297 | |
| 298 | return tmp; |
| 299 | } |
| 300 | |
| 301 | static std::string GetThreadName(Thread* thread) { |
| 302 | std::string tmp; |
| 303 | thread->GetThreadName(tmp); |
| 304 | return tmp; |
| 305 | } |
| 306 | |
| 307 | static void JNICALL ClassPrepareCallback(jvmtiEnv* jenv, |
| 308 | JNIEnv* jni_env, |
| 309 | jthread thread, |
| 310 | jclass klass) { |
| 311 | std::string name = GetClassName(jenv, jni_env, klass); |
| 312 | if (name == "") { |
| 313 | return; |
| 314 | } |
| 315 | std::string thread_name = GetThreadName(jenv, jni_env, thread); |
| 316 | if (thread_name == "") { |
| 317 | return; |
| 318 | } |
| 319 | std::string cur_thread_name = GetThreadName(Thread::Current()); |
| 320 | printf("Prepare: %s on %s (cur=%s)\n", |
| 321 | name.c_str(), |
| 322 | thread_name.c_str(), |
| 323 | cur_thread_name.c_str()); |
| 324 | } |
| 325 | |
| 326 | static void JNICALL ClassLoadCallback(jvmtiEnv* jenv, |
| 327 | JNIEnv* jni_env, |
| 328 | jthread thread, |
| 329 | jclass klass) { |
| 330 | std::string name = GetClassName(jenv, jni_env, klass); |
| 331 | if (name == "") { |
| 332 | return; |
| 333 | } |
| 334 | std::string thread_name = GetThreadName(jenv, jni_env, thread); |
| 335 | if (thread_name == "") { |
| 336 | return; |
| 337 | } |
| 338 | printf("Load: %s on %s\n", name.c_str(), thread_name.c_str()); |
| 339 | } |
| 340 | |
| 341 | extern "C" JNIEXPORT void JNICALL Java_Main_enableClassLoadEvents( |
| 342 | JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jboolean b) { |
| 343 | if (b == JNI_FALSE) { |
| 344 | jvmtiError ret = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, |
| 345 | JVMTI_EVENT_CLASS_LOAD, |
| 346 | nullptr); |
| 347 | if (JvmtiErrorToException(env, ret)) { |
| 348 | return; |
| 349 | } |
| 350 | ret = jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, |
| 351 | JVMTI_EVENT_CLASS_PREPARE, |
| 352 | nullptr); |
| 353 | JvmtiErrorToException(env, ret); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | jvmtiEventCallbacks callbacks; |
| 358 | memset(&callbacks, 0, sizeof(jvmtiEventCallbacks)); |
| 359 | callbacks.ClassLoad = ClassLoadCallback; |
| 360 | callbacks.ClassPrepare = ClassPrepareCallback; |
| 361 | jvmtiError ret = jvmti_env->SetEventCallbacks(&callbacks, sizeof(callbacks)); |
| 362 | if (JvmtiErrorToException(env, ret)) { |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | ret = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, |
| 367 | JVMTI_EVENT_CLASS_LOAD, |
| 368 | nullptr); |
| 369 | if (JvmtiErrorToException(env, ret)) { |
| 370 | return; |
| 371 | } |
| 372 | ret = jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, |
| 373 | JVMTI_EVENT_CLASS_PREPARE, |
| 374 | nullptr); |
| 375 | JvmtiErrorToException(env, ret); |
| 376 | } |
| 377 | |
Andreas Gampe | e492ae3 | 2016-10-28 19:34:57 -0700 | [diff] [blame] | 378 | } // namespace Test912Classes |
| 379 | } // namespace art |