blob: 415e25857bedbcd7c2bb9c5993c7038932f73396 [file] [log] [blame]
Andreas Gampee492ae32016-10-28 19:34:57 -07001/* 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_class.h"
33
34#include "art_jvmti.h"
Andreas Gampeac587272017-01-05 15:21:34 -080035#include "jni_internal.h"
Andreas Gampee492ae32016-10-28 19:34:57 -070036#include "scoped_thread_state_change-inl.h"
37#include "thread-inl.h"
38
39namespace openjdkjvmti {
40
Andreas Gampeac587272017-01-05 15:21:34 -080041jvmtiError ClassUtil::GetClassFields(jvmtiEnv* env,
42 jclass jklass,
43 jint* field_count_ptr,
44 jfieldID** fields_ptr) {
45 art::ScopedObjectAccess soa(art::Thread::Current());
46 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
47 if (klass == nullptr) {
48 return ERR(INVALID_CLASS);
49 }
50
51 if (field_count_ptr == nullptr || fields_ptr == nullptr) {
52 return ERR(NULL_POINTER);
53 }
54
Andreas Gampeac587272017-01-05 15:21:34 -080055 art::IterationRange<art::StrideIterator<art::ArtField>> ifields = klass->GetIFields();
56 art::IterationRange<art::StrideIterator<art::ArtField>> sfields = klass->GetSFields();
57 size_t array_size = klass->NumInstanceFields() + klass->NumStaticFields();
58
59 unsigned char* out_ptr;
60 jvmtiError allocError = env->Allocate(array_size * sizeof(jfieldID), &out_ptr);
61 if (allocError != ERR(NONE)) {
62 return allocError;
63 }
64 jfieldID* field_array = reinterpret_cast<jfieldID*>(out_ptr);
65
66 size_t array_idx = 0;
67 for (art::ArtField& field : sfields) {
68 field_array[array_idx] = art::jni::EncodeArtField(&field);
69 ++array_idx;
70 }
71 for (art::ArtField& field : ifields) {
72 field_array[array_idx] = art::jni::EncodeArtField(&field);
73 ++array_idx;
74 }
75
76 *field_count_ptr = static_cast<jint>(array_size);
77 *fields_ptr = field_array;
78
79 return ERR(NONE);
80}
81
Andreas Gampe18fee4d2017-01-06 11:36:35 -080082jvmtiError ClassUtil::GetClassMethods(jvmtiEnv* env,
83 jclass jklass,
84 jint* method_count_ptr,
85 jmethodID** methods_ptr) {
86 art::ScopedObjectAccess soa(art::Thread::Current());
87 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
88 if (klass == nullptr) {
89 return ERR(INVALID_CLASS);
90 }
91
92 if (method_count_ptr == nullptr || methods_ptr == nullptr) {
93 return ERR(NULL_POINTER);
94 }
95
96 size_t array_size = klass->NumDeclaredVirtualMethods() + klass->NumDirectMethods();
97 unsigned char* out_ptr;
98 jvmtiError allocError = env->Allocate(array_size * sizeof(jmethodID), &out_ptr);
99 if (allocError != ERR(NONE)) {
100 return allocError;
101 }
102 jmethodID* method_array = reinterpret_cast<jmethodID*>(out_ptr);
103
104 if (art::kIsDebugBuild) {
105 size_t count = 0;
106 for (auto& m ATTRIBUTE_UNUSED : klass->GetDeclaredMethods(art::kRuntimePointerSize)) {
107 count++;
108 }
109 CHECK_EQ(count, klass->NumDirectMethods() + klass->NumDeclaredVirtualMethods());
110 }
111
112 size_t array_idx = 0;
113 for (auto& m : klass->GetDeclaredMethods(art::kRuntimePointerSize)) {
114 method_array[array_idx] = art::jni::EncodeArtMethod(&m);
115 ++array_idx;
116 }
117
118 *method_count_ptr = static_cast<jint>(array_size);
119 *methods_ptr = method_array;
120
121 return ERR(NONE);
122}
123
124
Andreas Gampee492ae32016-10-28 19:34:57 -0700125jvmtiError ClassUtil::GetClassSignature(jvmtiEnv* env,
126 jclass jklass,
127 char** signature_ptr,
128 char** generic_ptr) {
129 art::ScopedObjectAccess soa(art::Thread::Current());
130 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
131 if (klass == nullptr) {
132 return ERR(INVALID_CLASS);
133 }
134
135 JvmtiUniquePtr sig_copy;
136 if (signature_ptr != nullptr) {
137 std::string storage;
138 const char* descriptor = klass->GetDescriptor(&storage);
139
140 unsigned char* tmp;
141 jvmtiError ret = CopyString(env, descriptor, &tmp);
142 if (ret != ERR(NONE)) {
143 return ret;
144 }
145 sig_copy = MakeJvmtiUniquePtr(env, tmp);
146 *signature_ptr = reinterpret_cast<char*>(tmp);
147 }
148
149 // TODO: Support generic signature.
150 *generic_ptr = nullptr;
151
152 // Everything is fine, release the buffers.
153 sig_copy.release();
154
155 return ERR(NONE);
156}
157
Andreas Gampeff9d2092017-01-06 09:12:49 -0800158jvmtiError ClassUtil::GetClassStatus(jvmtiEnv* env ATTRIBUTE_UNUSED,
159 jclass jklass,
160 jint* status_ptr) {
161 art::ScopedObjectAccess soa(art::Thread::Current());
162 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
163 if (klass == nullptr) {
164 return ERR(INVALID_CLASS);
165 }
166
167 if (status_ptr == nullptr) {
168 return ERR(NULL_POINTER);
169 }
170
171 if (klass->IsArrayClass()) {
172 *status_ptr = JVMTI_CLASS_STATUS_ARRAY;
173 } else if (klass->IsPrimitive()) {
174 *status_ptr = JVMTI_CLASS_STATUS_PRIMITIVE;
175 } else {
176 *status_ptr = JVMTI_CLASS_STATUS_VERIFIED; // All loaded classes are structurally verified.
177 // This is finicky. If there's an error, we'll say it wasn't prepared.
178 if (klass->IsResolved()) {
179 *status_ptr |= JVMTI_CLASS_STATUS_PREPARED;
180 }
181 if (klass->IsInitialized()) {
182 *status_ptr |= JVMTI_CLASS_STATUS_INITIALIZED;
183 }
184 // Technically the class may be erroneous for other reasons, but we do not have enough info.
185 if (klass->IsErroneous()) {
186 *status_ptr |= JVMTI_CLASS_STATUS_ERROR;
187 }
188 }
189
190 return ERR(NONE);
191}
192
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800193template <typename T>
194static jvmtiError ClassIsT(jclass jklass, T test, jboolean* is_t_ptr) {
195 art::ScopedObjectAccess soa(art::Thread::Current());
196 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
197 if (klass == nullptr) {
198 return ERR(INVALID_CLASS);
199 }
200
201 if (is_t_ptr == nullptr) {
202 return ERR(NULL_POINTER);
203 }
204
205 *is_t_ptr = test(klass) ? JNI_TRUE : JNI_FALSE;
206 return ERR(NONE);
207}
208
209jvmtiError ClassUtil::IsInterface(jvmtiEnv* env ATTRIBUTE_UNUSED,
210 jclass jklass,
211 jboolean* is_interface_ptr) {
212 auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
213 return klass->IsInterface();
214 };
215 return ClassIsT(jklass, test, is_interface_ptr);
216}
217
218jvmtiError ClassUtil::IsArrayClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
219 jclass jklass,
220 jboolean* is_array_class_ptr) {
221 auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
222 return klass->IsArrayClass();
223 };
224 return ClassIsT(jklass, test, is_array_class_ptr);
225}
226
Andreas Gampee492ae32016-10-28 19:34:57 -0700227} // namespace openjdkjvmti