blob: 4d2b34b94efa7b2dfe483224e9bea22e52ec3af0 [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);
Alex Light41960712017-01-06 14:44:23 -080045 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampeab2f0d02017-01-05 17:23:45 -080046 return nullptr;
47 }
48
49 auto callback = [&](jint i) {
50 if (i == 0) {
51 return name == nullptr ? nullptr : env->NewStringUTF(name);
52 } else if (i == 1) {
53 return sig == nullptr ? nullptr : env->NewStringUTF(sig);
54 } else {
55 return gen == nullptr ? nullptr : env->NewStringUTF(gen);
56 }
57 };
58 jobjectArray ret = CreateObjectArray(env, 3, "java/lang/String", callback);
59
60 // Need to deallocate the strings.
61 if (name != nullptr) {
62 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name));
63 }
64 if (sig != nullptr) {
65 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig));
66 }
67 if (gen != nullptr) {
68 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen));
69 }
70
71 // Also run GetMethodName with all parameter pointers null to check for segfaults.
72 jvmtiError result2 = jvmti_env->GetFieldName(klass, id, nullptr, nullptr, nullptr);
73 if (result2 != JVMTI_ERROR_NONE) {
74 char* err;
75 jvmti_env->GetErrorName(result2, &err);
76 printf("Failure running GetFieldName(null, null, null): %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080077 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampeab2f0d02017-01-05 17:23:45 -080078 return nullptr;
79 }
80
81 return ret;
82}
83
84extern "C" JNIEXPORT jclass JNICALL Java_Main_getFieldDeclaringClass(
85 JNIEnv* env, jclass klass, jobject field) {
86 jfieldID id = env->FromReflectedField(field);
87
88 jclass declaring_class;
89 jvmtiError result = jvmti_env->GetFieldDeclaringClass(klass, id, &declaring_class);
90 if (result != JVMTI_ERROR_NONE) {
91 char* err;
92 jvmti_env->GetErrorName(result, &err);
93 printf("Failure running GetFieldDeclaringClass: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -080094 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampeab2f0d02017-01-05 17:23:45 -080095 return nullptr;
96 }
97
98 return declaring_class;
99}
100
101extern "C" JNIEXPORT jint JNICALL Java_Main_getFieldModifiers(
102 JNIEnv* env, jclass klass, jobject field) {
103 jfieldID id = env->FromReflectedField(field);
104
105 jint modifiers;
106 jvmtiError result = jvmti_env->GetFieldModifiers(klass, id, &modifiers);
107 if (result != JVMTI_ERROR_NONE) {
108 char* err;
109 jvmti_env->GetErrorName(result, &err);
110 printf("Failure running GetFieldModifiers: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -0800111 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800112 return 0;
113 }
114
115 return modifiers;
116}
117
118extern "C" JNIEXPORT jboolean JNICALL Java_Main_isFieldSynthetic(
119 JNIEnv* env, jclass klass, jobject field) {
120 jfieldID id = env->FromReflectedField(field);
121
122 jboolean synth;
123 jvmtiError result = jvmti_env->IsFieldSynthetic(klass, id, &synth);
124 if (result != JVMTI_ERROR_NONE) {
125 char* err;
126 jvmti_env->GetErrorName(result, &err);
127 printf("Failure running IsFieldSynthetic: %s\n", err);
Alex Light41960712017-01-06 14:44:23 -0800128 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(err));
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800129 return 0;
130 }
131
132 return synth;
133}
134
135// Don't do anything
136jint OnLoad(JavaVM* vm,
137 char* options ATTRIBUTE_UNUSED,
138 void* reserved ATTRIBUTE_UNUSED) {
139 if (vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_0)) {
140 printf("Unable to get jvmti env!\n");
141 return 1;
142 }
143 SetAllCapabilities(jvmti_env);
144 return 0;
145}
146
147} // namespace Test918Fields
148} // namespace art