blob: 36d33b63cc4263351f32a8137573659cb693d97b [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
Andreas Gampe336c3c32016-11-08 17:02:19 -080030#include "ti-agent/common_helper.h"
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070031#include "ti-agent/common_load.h"
32
33namespace art {
34namespace Test907GetLoadedClasses {
35
36static 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
42extern "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 Light41960712017-01-06 14:44:23 -080051 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070052 return nullptr;
53 }
54
Andreas Gampe336c3c32016-11-08 17:02:19 -080055 auto callback = [&](jint i) {
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070056 jstring class_name = GetClassName(env, classes[i]);
Andreas Gampeef54d8d2016-10-25 09:55:53 -070057 env->DeleteLocalRef(classes[i]);
Andreas Gampe336c3c32016-11-08 17:02:19 -080058 return class_name;
59 };
60 jobjectArray ret = CreateObjectArray(env, count, "java/lang/String", callback);
61
62 // Need to Deallocate.
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070063 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(classes));
64
65 return ret;
66}
67
Andreas Gampe9b8c5882016-10-21 15:27:46 -070068// Don't do anything
69jint 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 Lighte6574242016-08-17 09:56:24 -070076 SetAllCapabilities(jvmti_env);
Andreas Gampe9b8c5882016-10-21 15:27:46 -070077 return 0;
78}
79
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070080} // namespace Test907GetLoadedClasses
81} // namespace art