Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 1 | /* 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" |
| 37 | #include "jni_internal.h" |
| 38 | #include "modifiers.h" |
| 39 | #include "scoped_thread_state_change-inl.h" |
| 40 | #include "thread-inl.h" |
| 41 | |
| 42 | namespace openjdkjvmti { |
| 43 | |
| 44 | // Note: For all these functions, we could do a check that the field actually belongs to the given |
| 45 | // class. But the spec seems to assume a certain encoding of the field ID, and so doesn't |
| 46 | // specify any errors. |
| 47 | |
| 48 | jvmtiError FieldUtil::GetFieldName(jvmtiEnv* env, |
| 49 | jclass klass, |
| 50 | jfieldID field, |
| 51 | char** name_ptr, |
| 52 | char** signature_ptr, |
| 53 | char** generic_ptr) { |
| 54 | if (klass == nullptr) { |
| 55 | return ERR(INVALID_CLASS); |
| 56 | } |
| 57 | if (field == nullptr) { |
| 58 | return ERR(INVALID_FIELDID); |
| 59 | } |
| 60 | |
| 61 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 62 | art::ArtField* art_field = art::jni::DecodeArtField(field); |
| 63 | |
| 64 | JvmtiUniquePtr name_copy; |
| 65 | if (name_ptr != nullptr) { |
| 66 | const char* field_name = art_field->GetName(); |
| 67 | if (field_name == nullptr) { |
| 68 | field_name = "<error>"; |
| 69 | } |
| 70 | unsigned char* tmp; |
| 71 | jvmtiError ret = CopyString(env, field_name, &tmp); |
| 72 | if (ret != ERR(NONE)) { |
| 73 | return ret; |
| 74 | } |
| 75 | name_copy = MakeJvmtiUniquePtr(env, tmp); |
| 76 | *name_ptr = reinterpret_cast<char*>(tmp); |
| 77 | } |
| 78 | |
| 79 | JvmtiUniquePtr signature_copy; |
| 80 | if (signature_ptr != nullptr) { |
| 81 | const char* sig = art_field->GetTypeDescriptor(); |
| 82 | unsigned char* tmp; |
| 83 | jvmtiError ret = CopyString(env, sig, &tmp); |
| 84 | if (ret != ERR(NONE)) { |
| 85 | return ret; |
| 86 | } |
| 87 | signature_copy = MakeJvmtiUniquePtr(env, tmp); |
| 88 | *signature_ptr = reinterpret_cast<char*>(tmp); |
| 89 | } |
| 90 | |
| 91 | // TODO: Support generic signature. |
| 92 | if (generic_ptr != nullptr) { |
| 93 | *generic_ptr = nullptr; |
| 94 | } |
| 95 | |
| 96 | // Everything is fine, release the buffers. |
| 97 | name_copy.release(); |
| 98 | signature_copy.release(); |
| 99 | |
| 100 | return ERR(NONE); |
| 101 | } |
| 102 | |
| 103 | jvmtiError FieldUtil::GetFieldDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 104 | jclass klass, |
| 105 | jfieldID field, |
| 106 | jclass* declaring_class_ptr) { |
| 107 | if (klass == nullptr) { |
| 108 | return ERR(INVALID_CLASS); |
| 109 | } |
| 110 | if (field == nullptr) { |
| 111 | return ERR(INVALID_FIELDID); |
| 112 | } |
| 113 | if (declaring_class_ptr == nullptr) { |
| 114 | return ERR(NULL_POINTER); |
| 115 | } |
| 116 | |
| 117 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 118 | art::ArtField* art_field = art::jni::DecodeArtField(field); |
| 119 | art::ObjPtr<art::mirror::Class> field_klass = art_field->GetDeclaringClass(); |
| 120 | |
| 121 | *declaring_class_ptr = soa.AddLocalReference<jclass>(field_klass); |
| 122 | |
| 123 | return ERR(NONE); |
| 124 | } |
| 125 | |
| 126 | jvmtiError FieldUtil::GetFieldModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 127 | jclass klass, |
| 128 | jfieldID field, |
| 129 | jint* modifiers_ptr) { |
| 130 | if (klass == nullptr) { |
| 131 | return ERR(INVALID_CLASS); |
| 132 | } |
| 133 | if (field == nullptr) { |
| 134 | return ERR(INVALID_FIELDID); |
| 135 | } |
| 136 | if (modifiers_ptr == nullptr) { |
| 137 | return ERR(NULL_POINTER); |
| 138 | } |
| 139 | |
| 140 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 141 | art::ArtField* art_field = art::jni::DecodeArtField(field); |
| 142 | // Note: Keep this code in sync with Field.getModifiers. |
| 143 | uint32_t modifiers = art_field->GetAccessFlags() & 0xFFFF; |
| 144 | |
| 145 | *modifiers_ptr = modifiers; |
| 146 | return ERR(NONE); |
| 147 | } |
| 148 | |
| 149 | jvmtiError FieldUtil::IsFieldSynthetic(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 150 | jclass klass, |
| 151 | jfieldID field, |
| 152 | jboolean* is_synthetic_ptr) { |
| 153 | if (klass == nullptr) { |
| 154 | return ERR(INVALID_CLASS); |
| 155 | } |
| 156 | if (field == nullptr) { |
| 157 | return ERR(INVALID_FIELDID); |
| 158 | } |
| 159 | if (is_synthetic_ptr == nullptr) { |
| 160 | return ERR(NULL_POINTER); |
| 161 | } |
| 162 | |
| 163 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 164 | art::ArtField* art_field = art::jni::DecodeArtField(field); |
| 165 | uint32_t modifiers = art_field->GetAccessFlags(); |
| 166 | |
| 167 | *is_synthetic_ptr = ((modifiers & art::kAccSynthetic) != 0) ? JNI_TRUE : JNI_FALSE; |
| 168 | return ERR(NONE); |
| 169 | } |
| 170 | |
| 171 | } // namespace openjdkjvmti |