blob: 57d0d69c16604ddab42be8161dbb8e09cd45f98c [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);
Alex Light41960712017-01-06 14:44:23 -080041 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampee492ae32016-10-28 19:34:57 -070042 return nullptr;
43 }
44
Andreas Gampe336c3c32016-11-08 17:02:19 -080045 auto callback = [&](jint i) {
46 if (i == 0) {
47 return sig == nullptr ? nullptr : env->NewStringUTF(sig);
48 } else {
49 return gen == nullptr ? nullptr : env->NewStringUTF(gen);
50 }
51 };
52 jobjectArray ret = CreateObjectArray(env, 2, "java/lang/String", callback);
Andreas Gampee492ae32016-10-28 19:34:57 -070053
54 // Need to deallocate the strings.
55 if (sig != nullptr) {
56 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
57 }
58 if (gen != nullptr) {
59 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
60 }
61
62 return ret;
63}
64
Andreas Gampe4fd66ec2017-01-05 14:42:13 -080065extern "C" JNIEXPORT jboolean JNICALL Java_Main_isInterface(
66 JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) {
67 jboolean is_interface = JNI_FALSE;
68 jvmtiError result = jvmti_env->IsInterface(klass, &is_interface);
69 if (result != JVMTI_ERROR_NONE) {
70 char* err;
71 jvmti_env->GetErrorName(result, &err);
72 printf("Failure running IsInterface: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080073 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe4fd66ec2017-01-05 14:42:13 -080074 return JNI_FALSE;
75 }
76 return is_interface;
77}
78
79extern "C" JNIEXPORT jboolean JNICALL Java_Main_isArrayClass(
80 JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) {
81 jboolean is_array_class = JNI_FALSE;
82 jvmtiError result = jvmti_env->IsArrayClass(klass, &is_array_class);
83 if (result != JVMTI_ERROR_NONE) {
84 char* err;
85 jvmti_env->GetErrorName(result, &err);
86 printf("Failure running IsArrayClass: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080087 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampe4fd66ec2017-01-05 14:42:13 -080088 return JNI_FALSE;
89 }
90 return is_array_class;
91}
92
Andreas Gampeac587272017-01-05 15:21:34 -080093extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getClassFields(
94 JNIEnv* env, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) {
95 jint count = 0;
96 jfieldID* fields = nullptr;
97 jvmtiError result = jvmti_env->GetClassFields(klass, &count, &fields);
98 if (result != JVMTI_ERROR_NONE) {
99 char* err;
100 jvmti_env->GetErrorName(result, &err);
101 printf("Failure running GetClassFields: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -0800102 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampeac587272017-01-05 15:21:34 -0800103 return nullptr;
104 }
105
106 auto callback = [&](jint i) {
107 jint modifiers;
108 // Ignore any errors for simplicity.
109 jvmti_env->GetFieldModifiers(klass, fields[i], &modifiers);
110 constexpr jint kStatic = 0x8;
111 return env->ToReflectedField(klass,
112 fields[i],
113 (modifiers & kStatic) != 0 ? JNI_TRUE : JNI_FALSE);
114 };
115 return CreateObjectArray(env, count, "java/lang/Object", callback);
116}
117
Andreas Gampeff9d2092017-01-06 09:12:49 -0800118extern "C" JNIEXPORT jint JNICALL Java_Main_getClassStatus(
119 JNIEnv* env ATTRIBUTE_UNUSED, jclass Main_klass ATTRIBUTE_UNUSED, jclass klass) {
120 jint status;
121 jvmtiError result = jvmti_env->GetClassStatus(klass, &status);
122 if (result != JVMTI_ERROR_NONE) {
123 char* err;
124 jvmti_env->GetErrorName(result, &err);
125 printf("Failure running GetClassStatus: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -0800126 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampeff9d2092017-01-06 09:12:49 -0800127 return JNI_FALSE;
128 }
129 return status;
130}
131
Andreas Gampee492ae32016-10-28 19:34:57 -0700132// Don't do anything
133jint OnLoad(JavaVM* vm,
134 char* options ATTRIBUTE_UNUSED,
135 void* reserved ATTRIBUTE_UNUSED) {
136 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
137 printf("Unable to get jvmti env!\n");
138 return 1;
139 }
Alex Lighte6574242016-08-17 09:56:24 -0700140 SetAllCapabilities(jvmti_env);
Andreas Gampee492ae32016-10-28 19:34:57 -0700141 return 0;
142}
143
144} // namespace Test912Classes
145} // namespace art