blob: c7fca06747b59948dc19219752663e5601c49b00 [file] [log] [blame]
Andreas Gampeab2f0d02017-01-05 17:23:45 -08001/*
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 "fields.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
26#include "ti-agent/common_helper.h"
27#include "ti-agent/common_load.h"
28
29namespace art {
30namespace Test918Fields {
31
32extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getFieldName(
33 JNIEnv* env, jclass klass, jobject field) {
34 jfieldID id = env->FromReflectedField(field);
35
36 char* name;
37 char* sig;
38 char* gen;
39 // Note: technically putting the caller class here is wrong, but we don't need it, anyways.
40 jvmtiError result = jvmti_env->GetFieldName(klass, id, &name, &sig, &gen);
41 if (result != JVMTI_ERROR_NONE) {
42 char* err;
43 jvmti_env->GetErrorName(result, &err);
44 printf("Failure running GetFieldName: %s\n", err);
45 return nullptr;
46 }
47
48 auto callback = [&](jint i) {
49 if (i == 0) {
50 return name == nullptr ? nullptr : env->NewStringUTF(name);
51 } else if (i == 1) {
52 return sig == nullptr ? nullptr : env->NewStringUTF(sig);
53 } else {
54 return gen == nullptr ? nullptr : env->NewStringUTF(gen);
55 }
56 };
57 jobjectArray ret = CreateObjectArray(env, 3, "java/lang/String", callback);
58
59 // Need to deallocate the strings.
60 if (name != nullptr) {
61 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name));
62 }
63 if (sig != nullptr) {
64 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
65 }
66 if (gen != nullptr) {
67 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
68 }
69
70 // Also run GetMethodName with all parameter pointers null to check for segfaults.
71 jvmtiError result2 = jvmti_env->GetFieldName(klass, id, nullptr, nullptr, nullptr);
72 if (result2 != JVMTI_ERROR_NONE) {
73 char* err;
74 jvmti_env->GetErrorName(result2, &err);
75 printf("Failure running GetFieldName(null, null, null): %s\n", err);
76 return nullptr;
77 }
78
79 return ret;
80}
81
82extern "C" JNIEXPORT jclass JNICALL Java_Main_getFieldDeclaringClass(
83 JNIEnv* env, jclass klass, jobject field) {
84 jfieldID id = env->FromReflectedField(field);
85
86 jclass declaring_class;
87 jvmtiError result = jvmti_env->GetFieldDeclaringClass(klass, id, &declaring_class);
88 if (result != JVMTI_ERROR_NONE) {
89 char* err;
90 jvmti_env->GetErrorName(result, &err);
91 printf("Failure running GetFieldDeclaringClass: %s\n", err);
92 return nullptr;
93 }
94
95 return declaring_class;
96}
97
98extern "C" JNIEXPORT jint JNICALL Java_Main_getFieldModifiers(
99 JNIEnv* env, jclass klass, jobject field) {
100 jfieldID id = env->FromReflectedField(field);
101
102 jint modifiers;
103 jvmtiError result = jvmti_env->GetFieldModifiers(klass, id, &modifiers);
104 if (result != JVMTI_ERROR_NONE) {
105 char* err;
106 jvmti_env->GetErrorName(result, &err);
107 printf("Failure running GetFieldModifiers: %s\n", err);
108 return 0;
109 }
110
111 return modifiers;
112}
113
114extern "C" JNIEXPORT jboolean JNICALL Java_Main_isFieldSynthetic(
115 JNIEnv* env, jclass klass, jobject field) {
116 jfieldID id = env->FromReflectedField(field);
117
118 jboolean synth;
119 jvmtiError result = jvmti_env->IsFieldSynthetic(klass, id, &synth);
120 if (result != JVMTI_ERROR_NONE) {
121 char* err;
122 jvmti_env->GetErrorName(result, &err);
123 printf("Failure running IsFieldSynthetic: %s\n", err);
124 return 0;
125 }
126
127 return synth;
128}
129
130// Don't do anything
131jint OnLoad(JavaVM* vm,
132 char* options ATTRIBUTE_UNUSED,
133 void* reserved ATTRIBUTE_UNUSED) {
134 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
135 printf("Unable to get jvmti env!\n");
136 return 1;
137 }
138 SetAllCapabilities(jvmti_env);
139 return 0;
140}
141
142} // namespace Test918Fields
143} // namespace art