blob: 28c59315218fd0a9bad3fb49d602faaa18639711 [file] [log] [blame]
Andreas Gampee492ae32016-10-28 19:34:57 -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 "classes.h"
18
19#include <stdio.h>
20
21#include "base/macros.h"
22#include "jni.h"
23#include "openjdkjvmti/jvmti.h"
24#include "ScopedLocalRef.h"
25
Andreas Gampe336c3c32016-11-08 17:02:19 -080026#include "ti-agent/common_helper.h"
Andreas Gampee492ae32016-10-28 19:34:57 -070027#include "ti-agent/common_load.h"
28
29namespace art {
30namespace Test912Classes {
31
32extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getClassSignature(
33 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) {
34 char* sig;
35 char* gen;
36 jvmtiError result = jvmti_env->GetClassSignature(klass, &sig, &gen);
37 if (result != JVMTI_ERROR_NONE) {
38 char* err;
39 jvmti_env->GetErrorName(result, &err);
40 printf("Failure running GetClassSignature: %s\n", err);
41 return nullptr;
42 }
43
Andreas Gampe336c3c32016-11-08 17:02:19 -080044 auto callback = [&](jint i) {
45 if (i == 0) {
46 return sig == nullptr ? nullptr : env->NewStringUTF(sig);
47 } else {
48 return gen == nullptr ? nullptr : env->NewStringUTF(gen);
49 }
50 };
51 jobjectArray ret = CreateObjectArray(env, 2, "java/lang/String", callback);
Andreas Gampee492ae32016-10-28 19:34:57 -070052
53 // Need to deallocate the strings.
54 if (sig != nullptr) {
55 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
56 }
57 if (gen != nullptr) {
58 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
59 }
60
61 return ret;
62}
63
Andreas Gampe4fd66ec2017-01-05 14:42:13 -080064extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterface(
65 JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) {
66 jboolean is_interface = JNI_FALSE;
67 jvmtiError result = jvmti_env->IsInterface(klass, &is_interface);
68 if (result != JVMTI_ERROR_NONE) {
69 char* err;
70 jvmti_env->GetErrorName(result, &err);
71 printf("Failure running IsInterface: %s\n", err);
72 return JNI_FALSE;
73 }
74 return is_interface;
75}
76
77extern "C" JNIEXPORT jboolean JNICALL Java_Main_isArrayClass(
78 JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) {
79 jboolean is_array_class = JNI_FALSE;
80 jvmtiError result = jvmti_env->IsArrayClass(klass, &is_array_class);
81 if (result != JVMTI_ERROR_NONE) {
82 char* err;
83 jvmti_env->GetErrorName(result, &err);
84 printf("Failure running IsArrayClass: %s\n", err);
85 return JNI_FALSE;
86 }
87 return is_array_class;
88}
89
Andreas Gampeac587272017-01-05 15:21:34 -080090extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getClassFields(
91 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) {
92 jint count = 0;
93 jfieldID* fields = nullptr;
94 jvmtiError result = jvmti_env->GetClassFields(klass, &count, &fields);
95 if (result != JVMTI_ERROR_NONE) {
96 char* err;
97 jvmti_env->GetErrorName(result, &err);
98 printf("Failure running GetClassFields: %s\n", err);
99 return nullptr;
100 }
101
102 auto callback = [&](jint i) {
103 jint modifiers;
104 // Ignore any errors for simplicity.
105 jvmti_env->GetFieldModifiers(klass, fields[i], &modifiers);
106 constexpr jint kStatic = 0x8;
107 return env->ToReflectedField(klass,
108 fields[i],
109 (modifiers & kStatic) != 0 ? JNI_TRUE : JNI_FALSE);
110 };
111 return CreateObjectArray(env, count, "java/lang/Object", callback);
112}
113
Andreas Gampeff9d2092017-01-06 09:12:49 -0800114extern "C" JNIEXPORT jint JNICALL Java_Main_getClassStatus(
115 JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) {
116 jint status;
117 jvmtiError result = jvmti_env->GetClassStatus(klass, &status);
118 if (result != JVMTI_ERROR_NONE) {
119 char* err;
120 jvmti_env->GetErrorName(result, &err);
121 printf("Failure running GetClassStatus: %s\n", err);
122 return JNI_FALSE;
123 }
124 return status;
125}
126
Andreas Gampee492ae32016-10-28 19:34:57 -0700127// Don't do anything
128jint OnLoad(JavaVM* vm,
129 char* options ATTRIBUTE_UNUSED,
130 void* reserved ATTRIBUTE_UNUSED) {
131 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
132 printf("Unable to get jvmti env!\n");
133 return 1;
134 }
Alex Lighte6574242016-08-17 09:56:24 -0700135 SetAllCapabilities(jvmti_env);
Andreas Gampee492ae32016-10-28 19:34:57 -0700136 return 0;
137}
138
139} // namespace Test912Classes
140} // namespace art