blob: 5bda7ebac87f77aa60d0076e539725b3a7643127 [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
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070017#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 Gampe336c3c32016-11-08 17:02:19 -080028#include "ti-agent/common_helper.h"
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070029#include "ti-agent/common_load.h"
30
31namespace art {
32namespace Test907GetLoadedClasses {
33
34static 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
40extern "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 Light41960712017-01-06 14:44:23 -080049 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070050 return nullptr;
51 }
52
Andreas Gampe336c3c32016-11-08 17:02:19 -080053 auto callback = [&](jint i) {
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070054 jstring class_name = GetClassName(env, classes[i]);
Andreas Gampeef54d8d2016-10-25 09:55:53 -070055 env->DeleteLocalRef(classes[i]);
Andreas Gampe336c3c32016-11-08 17:02:19 -080056 return class_name;
57 };
58 jobjectArray ret = CreateObjectArray(env, count, "java/lang/String", callback);
59
60 // Need to Deallocate.
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070061 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(classes));
62
63 return ret;
64}
65
Andreas Gampeaa8b60c2016-10-12 12:51:25 -070066} // namespace Test907GetLoadedClasses
67} // namespace art