blob: 7b30a9da7486eeec33c908b3c7da93f68823a199 [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
55 art::StackHandleScope<1> hs(soa.Self());
56 art::IterationRange<art::StrideIterator<art::ArtField>> ifields = klass->GetIFields();
57 art::IterationRange<art::StrideIterator<art::ArtField>> sfields = klass->GetSFields();
58 size_t array_size = klass->NumInstanceFields() + klass->NumStaticFields();
59
60 unsigned char* out_ptr;
61 jvmtiError allocError = env->Allocate(array_size * sizeof(jfieldID), &out_ptr);
62 if (allocError != ERR(NONE)) {
63 return allocError;
64 }
65 jfieldID* field_array = reinterpret_cast<jfieldID*>(out_ptr);
66
67 size_t array_idx = 0;
68 for (art::ArtField& field : sfields) {
69 field_array[array_idx] = art::jni::EncodeArtField(&field);
70 ++array_idx;
71 }
72 for (art::ArtField& field : ifields) {
73 field_array[array_idx] = art::jni::EncodeArtField(&field);
74 ++array_idx;
75 }
76
77 *field_count_ptr = static_cast<jint>(array_size);
78 *fields_ptr = field_array;
79
80 return ERR(NONE);
81}
82
Andreas Gampee492ae32016-10-28 19:34:57 -070083jvmtiError ClassUtil::GetClassSignature(jvmtiEnv* env,
84 jclass jklass,
85 char** signature_ptr,
86 char** generic_ptr) {
87 art::ScopedObjectAccess soa(art::Thread::Current());
88 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
89 if (klass == nullptr) {
90 return ERR(INVALID_CLASS);
91 }
92
93 JvmtiUniquePtr sig_copy;
94 if (signature_ptr != nullptr) {
95 std::string storage;
96 const char* descriptor = klass->GetDescriptor(&storage);
97
98 unsigned char* tmp;
99 jvmtiError ret = CopyString(env, descriptor, &tmp);
100 if (ret != ERR(NONE)) {
101 return ret;
102 }
103 sig_copy = MakeJvmtiUniquePtr(env, tmp);
104 *signature_ptr = reinterpret_cast<char*>(tmp);
105 }
106
107 // TODO: Support generic signature.
108 *generic_ptr = nullptr;
109
110 // Everything is fine, release the buffers.
111 sig_copy.release();
112
113 return ERR(NONE);
114}
115
Andreas Gampeff9d2092017-01-06 09:12:49 -0800116jvmtiError ClassUtil::GetClassStatus(jvmtiEnv* env ATTRIBUTE_UNUSED,
117 jclass jklass,
118 jint* status_ptr) {
119 art::ScopedObjectAccess soa(art::Thread::Current());
120 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
121 if (klass == nullptr) {
122 return ERR(INVALID_CLASS);
123 }
124
125 if (status_ptr == nullptr) {
126 return ERR(NULL_POINTER);
127 }
128
129 if (klass->IsArrayClass()) {
130 *status_ptr = JVMTI_CLASS_STATUS_ARRAY;
131 } else if (klass->IsPrimitive()) {
132 *status_ptr = JVMTI_CLASS_STATUS_PRIMITIVE;
133 } else {
134 *status_ptr = JVMTI_CLASS_STATUS_VERIFIED; // All loaded classes are structurally verified.
135 // This is finicky. If there's an error, we'll say it wasn't prepared.
136 if (klass->IsResolved()) {
137 *status_ptr |= JVMTI_CLASS_STATUS_PREPARED;
138 }
139 if (klass->IsInitialized()) {
140 *status_ptr |= JVMTI_CLASS_STATUS_INITIALIZED;
141 }
142 // Technically the class may be erroneous for other reasons, but we do not have enough info.
143 if (klass->IsErroneous()) {
144 *status_ptr |= JVMTI_CLASS_STATUS_ERROR;
145 }
146 }
147
148 return ERR(NONE);
149}
150
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800151template <typename T>
152static jvmtiError ClassIsT(jclass jklass, T test, jboolean* is_t_ptr) {
153 art::ScopedObjectAccess soa(art::Thread::Current());
154 art::ObjPtr<art::mirror::Class> klass = soa.Decode<art::mirror::Class>(jklass);
155 if (klass == nullptr) {
156 return ERR(INVALID_CLASS);
157 }
158
159 if (is_t_ptr == nullptr) {
160 return ERR(NULL_POINTER);
161 }
162
163 *is_t_ptr = test(klass) ? JNI_TRUE : JNI_FALSE;
164 return ERR(NONE);
165}
166
167jvmtiError ClassUtil::IsInterface(jvmtiEnv* env ATTRIBUTE_UNUSED,
168 jclass jklass,
169 jboolean* is_interface_ptr) {
170 auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
171 return klass->IsInterface();
172 };
173 return ClassIsT(jklass, test, is_interface_ptr);
174}
175
176jvmtiError ClassUtil::IsArrayClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
177 jclass jklass,
178 jboolean* is_array_class_ptr) {
179 auto test = [](art::ObjPtr<art::mirror::Class> klass) REQUIRES_SHARED(art::Locks::mutator_lock_) {
180 return klass->IsArrayClass();
181 };
182 return ClassIsT(jklass, test, is_array_class_ptr);
183}
184
Andreas Gampee492ae32016-10-28 19:34:57 -0700185} // namespace openjdkjvmti