blob: 8c3f2fffbd88291925513f3b984aae0134028939 [file] [log] [blame]
Andreas Gampeab2f0d02017-01-05 17:23:45 -08001/* Copyright (C) 2016 The Android Open Source Project
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This file implements interfaces from the file jvmti.h. This implementation
5 * is licensed under the same terms as the file jvmti.h. The
6 * copyright and license information for the file jvmti.h follows.
7 *
8 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10 *
11 * This code is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 only, as
13 * published by the Free Software Foundation. Oracle designates this
14 * particular file as subject to the "Classpath" exception as provided
15 * by Oracle in the LICENSE file that accompanied this code.
16 *
17 * This code is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * version 2 for more details (a copy is included in the LICENSE file that
21 * accompanied this code).
22 *
23 * You should have received a copy of the GNU General Public License version
24 * 2 along with this work; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 *
27 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28 * or visit www.oracle.com if you need additional information or have any
29 * questions.
30 */
31
32#include "ti_field.h"
33
34#include "art_jvmti.h"
35#include "art_field-inl.h"
36#include "base/enums.h"
Andreas Gampe188abab2017-02-16 10:34:05 -080037#include "dex_file_annotations.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080038#include "jni_internal.h"
Andreas Gampe188abab2017-02-16 10:34:05 -080039#include "mirror/object_array-inl.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080040#include "modifiers.h"
41#include "scoped_thread_state_change-inl.h"
42#include "thread-inl.h"
43
44namespace openjdkjvmti {
45
46// Note: For all these functions, we could do a check that the field actually belongs to the given
47// class. But the spec seems to assume a certain encoding of the field ID, and so doesn't
48// specify any errors.
49
50jvmtiError FieldUtil::GetFieldName(jvmtiEnv* env,
51 jclass klass,
52 jfieldID field,
53 char** name_ptr,
54 char** signature_ptr,
55 char** generic_ptr) {
56 if (klass == nullptr) {
57 return ERR(INVALID_CLASS);
58 }
59 if (field == nullptr) {
60 return ERR(INVALID_FIELDID);
61 }
62
63 art::ScopedObjectAccess soa(art::Thread::Current());
64 art::ArtField* art_field = art::jni::DecodeArtField(field);
65
Andreas Gampe54711412017-02-21 12:41:43 -080066 JvmtiUniquePtr<char[]> name_copy;
Andreas Gampeab2f0d02017-01-05 17:23:45 -080067 if (name_ptr != nullptr) {
68 const char* field_name = art_field->GetName();
69 if (field_name == nullptr) {
70 field_name = "<error>";
71 }
Andreas Gampe54711412017-02-21 12:41:43 -080072 jvmtiError ret;
73 name_copy = CopyString(env, field_name, &ret);
74 if (name_copy == nullptr) {
Andreas Gampeab2f0d02017-01-05 17:23:45 -080075 return ret;
76 }
Andreas Gampe54711412017-02-21 12:41:43 -080077 *name_ptr = name_copy.get();
Andreas Gampeab2f0d02017-01-05 17:23:45 -080078 }
79
Andreas Gampe54711412017-02-21 12:41:43 -080080 JvmtiUniquePtr<char[]> signature_copy;
Andreas Gampeab2f0d02017-01-05 17:23:45 -080081 if (signature_ptr != nullptr) {
82 const char* sig = art_field->GetTypeDescriptor();
Andreas Gampe54711412017-02-21 12:41:43 -080083 jvmtiError ret;
84 signature_copy = CopyString(env, sig, &ret);
85 if (signature_copy == nullptr) {
Andreas Gampeab2f0d02017-01-05 17:23:45 -080086 return ret;
87 }
Andreas Gampe54711412017-02-21 12:41:43 -080088 *signature_ptr = signature_copy.get();
Andreas Gampeab2f0d02017-01-05 17:23:45 -080089 }
90
91 // TODO: Support generic signature.
92 if (generic_ptr != nullptr) {
93 *generic_ptr = nullptr;
Andreas Gampe188abab2017-02-16 10:34:05 -080094 if (!art_field->GetDeclaringClass()->IsProxyClass()) {
95 art::mirror::ObjectArray<art::mirror::String>* str_array =
96 art::annotations::GetSignatureAnnotationForField(art_field);
97 if (str_array != nullptr) {
98 std::ostringstream oss;
99 for (int32_t i = 0; i != str_array->GetLength(); ++i) {
100 oss << str_array->Get(i)->ToModifiedUtf8();
101 }
102 std::string output_string = oss.str();
Andreas Gampe54711412017-02-21 12:41:43 -0800103 jvmtiError ret;
104 JvmtiUniquePtr<char[]> copy = CopyString(env, output_string.c_str(), &ret);
105 if (copy == nullptr) {
Andreas Gampe188abab2017-02-16 10:34:05 -0800106 return ret;
107 }
Andreas Gampe54711412017-02-21 12:41:43 -0800108 *generic_ptr = copy.release();
Andreas Gampe188abab2017-02-16 10:34:05 -0800109 } else if (soa.Self()->IsExceptionPending()) {
110 // TODO: Should we report an error here?
111 soa.Self()->ClearException();
112 }
113 }
Andreas Gampeab2f0d02017-01-05 17:23:45 -0800114 }
115
116 // Everything is fine, release the buffers.
117 name_copy.release();
118 signature_copy.release();
119
120 return ERR(NONE);
121}
122
123jvmtiError FieldUtil::GetFieldDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
124 jclass klass,
125 jfieldID field,
126 jclass* declaring_class_ptr) {
127 if (klass == nullptr) {
128 return ERR(INVALID_CLASS);
129 }
130 if (field == nullptr) {
131 return ERR(INVALID_FIELDID);
132 }
133 if (declaring_class_ptr == nullptr) {
134 return ERR(NULL_POINTER);
135 }
136
137 art::ScopedObjectAccess soa(art::Thread::Current());
138 art::ArtField* art_field = art::jni::DecodeArtField(field);
139 art::ObjPtr<art::mirror::Class> field_klass = art_field->GetDeclaringClass();
140
141 *declaring_class_ptr = soa.AddLocalReference<jclass>(field_klass);
142
143 return ERR(NONE);
144}
145
146jvmtiError FieldUtil::GetFieldModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
147 jclass klass,
148 jfieldID field,
149 jint* modifiers_ptr) {
150 if (klass == nullptr) {
151 return ERR(INVALID_CLASS);
152 }
153 if (field == nullptr) {
154 return ERR(INVALID_FIELDID);
155 }
156 if (modifiers_ptr == nullptr) {
157 return ERR(NULL_POINTER);
158 }
159
160 art::ScopedObjectAccess soa(art::Thread::Current());
161 art::ArtField* art_field = art::jni::DecodeArtField(field);
162 // Note: Keep this code in sync with Field.getModifiers.
163 uint32_t modifiers = art_field->GetAccessFlags() & 0xFFFF;
164
165 *modifiers_ptr = modifiers;
166 return ERR(NONE);
167}
168
169jvmtiError FieldUtil::IsFieldSynthetic(jvmtiEnv* env ATTRIBUTE_UNUSED,
170 jclass klass,
171 jfieldID field,
172 jboolean* is_synthetic_ptr) {
173 if (klass == nullptr) {
174 return ERR(INVALID_CLASS);
175 }
176 if (field == nullptr) {
177 return ERR(INVALID_FIELDID);
178 }
179 if (is_synthetic_ptr == nullptr) {
180 return ERR(NULL_POINTER);
181 }
182
183 art::ScopedObjectAccess soa(art::Thread::Current());
184 art::ArtField* art_field = art::jni::DecodeArtField(field);
185 uint32_t modifiers = art_field->GetAccessFlags();
186
187 *is_synthetic_ptr = ((modifiers & art::kAccSynthetic) != 0) ? JNI_TRUE : JNI_FALSE;
188 return ERR(NONE);
189}
190
191} // namespace openjdkjvmti