Andreas Gampe | aa8b60c | 2016-10-12 12:51:25 -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 | aa8b60c | 2016-10-12 12:51:25 -0700 | [diff] [blame] | 17 | #include <iostream> |
| 18 | #include <pthread.h> |
| 19 | #include <stdio.h> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "base/macros.h" |
| 23 | #include "jni.h" |
| 24 | #include "openjdkjvmti/jvmti.h" |
| 25 | #include "ScopedLocalRef.h" |
| 26 | #include "ScopedUtfChars.h" |
| 27 | |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 28 | #include "ti-agent/common_helper.h" |
Andreas Gampe | aa8b60c | 2016-10-12 12:51:25 -0700 | [diff] [blame] | 29 | #include "ti-agent/common_load.h" |
| 30 | |
| 31 | namespace art { |
| 32 | namespace Test907GetLoadedClasses { |
| 33 | |
| 34 | static jstring GetClassName(JNIEnv* jni_env, jclass cls) { |
| 35 | ScopedLocalRef<jclass> class_class(jni_env, jni_env->GetObjectClass(cls)); |
| 36 | jmethodID mid = jni_env->GetMethodID(class_class.get(), "getName", "()Ljava/lang/String;"); |
| 37 | return reinterpret_cast<jstring>(jni_env->CallObjectMethod(cls, mid)); |
| 38 | } |
| 39 | |
| 40 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getLoadedClasses( |
| 41 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) { |
| 42 | jint count = -1; |
| 43 | jclass* classes = nullptr; |
| 44 | jvmtiError result = jvmti_env->GetLoadedClasses(&count, &classes); |
| 45 | if (result != JVMTI_ERROR_NONE) { |
| 46 | char* err; |
| 47 | jvmti_env->GetErrorName(result, &err); |
| 48 | printf("Failure running GetLoadedClasses: %s\n", err); |
Alex Light | 4196071 | 2017-01-06 14:44:23 -0800 | [diff] [blame] | 49 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err)); |
Andreas Gampe | aa8b60c | 2016-10-12 12:51:25 -0700 | [diff] [blame] | 50 | return nullptr; |
| 51 | } |
| 52 | |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 53 | auto callback = [&](jint i) { |
Andreas Gampe | aa8b60c | 2016-10-12 12:51:25 -0700 | [diff] [blame] | 54 | jstring class_name = GetClassName(env, classes[i]); |
Andreas Gampe | ef54d8d | 2016-10-25 09:55:53 -0700 | [diff] [blame] | 55 | env->DeleteLocalRef(classes[i]); |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 56 | return class_name; |
| 57 | }; |
| 58 | jobjectArray ret = CreateObjectArray(env, count, "java/lang/String", callback); |
| 59 | |
| 60 | // Need to Deallocate. |
Andreas Gampe | aa8b60c | 2016-10-12 12:51:25 -0700 | [diff] [blame] | 61 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(classes)); |
| 62 | |
| 63 | return ret; |
| 64 | } |
| 65 | |
Andreas Gampe | aa8b60c | 2016-10-12 12:51:25 -0700 | [diff] [blame] | 66 | } // namespace Test907GetLoadedClasses |
| 67 | } // namespace art |