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