blob: 0e09d1be098a49de7d550232521d6773731152ef [file] [log] [blame]
Andreas Gampeaa8b60c2016-10-12 12:51:25 -07001/*
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
30#include "ti-agent/common_load.h"
31
32namespace art {
33namespace Test907GetLoadedClasses {
34
35static jstring GetClassName(JNIEnv* jni_env, jclass cls) {
36 ScopedLocalRef<jclass> class_class(jni_env, jni_env->GetObjectClass(cls));
37 jmethodID mid = jni_env->GetMethodID(class_class.get(), "getName", "()Ljava/lang/String;");
38 return reinterpret_cast<jstring>(jni_env->CallObjectMethod(cls, mid));
39}
40
41extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getLoadedClasses(
42 JNIEnv* env, jclass klass ATTRIBUTE_UNUSED) {
43 jint count = -1;
44 jclass* classes = nullptr;
45 jvmtiError result = jvmti_env->GetLoadedClasses(&count, &classes);
46 if (result != JVMTI_ERROR_NONE) {
47 char* err;
48 jvmti_env->GetErrorName(result, &err);
49 printf("Failure running GetLoadedClasses: %s\n", err);
50 return nullptr;
51 }
52
53 ScopedLocalRef<jclass> obj_class(env, env->FindClass("java/lang/String"));
54 if (obj_class.get() == nullptr) {
55 return nullptr;
56 }
57
58 jobjectArray ret = env->NewObjectArray(count, obj_class.get(), nullptr);
59 if (ret == nullptr) {
60 return ret;
61 }
62
63 for (size_t i = 0; i < static_cast<size_t>(count); ++i) {
64 jstring class_name = GetClassName(env, classes[i]);
65 env->SetObjectArrayElement(ret, static_cast<jint>(i), class_name);
66 env->DeleteLocalRef(class_name);
67 }
68
69 // Need to:
70 // 1) Free the local references.
71 // 2) Deallocate.
72 for (size_t i = 0; i < static_cast<size_t>(count); ++i) {
Andreas Gampeef54d8d2016-10-25 09:55:53 -070073 env->DeleteLocalRef(classes[i]);
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070074 }
75 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(classes));
76
77 return ret;
78}
79
Andreas Gampe9b8c5882016-10-21 15:27:46 -070080// Don't do anything
81jint OnLoad(JavaVM* vm,
82 char* options ATTRIBUTE_UNUSED,
83 void* reserved ATTRIBUTE_UNUSED) {
84 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
85 printf("Unable to get jvmti env!\n");
86 return 1;
87 }
88 return 0;
89}
90
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070091} // namespace Test907GetLoadedClasses
92} // namespace art