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" |
Andreas Gampe | 188abab | 2017-02-16 10:34:05 -0800 | [diff] [blame] | 37 | #include "dex_file_annotations.h" |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 38 | #include "jni_internal.h" |
Andreas Gampe | 188abab | 2017-02-16 10:34:05 -0800 | [diff] [blame] | 39 | #include "mirror/object_array-inl.h" |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 40 | #include "modifiers.h" |
| 41 | #include "scoped_thread_state_change-inl.h" |
| 42 | #include "thread-inl.h" |
| 43 | |
| 44 | namespace 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 | |
| 50 | jvmtiError 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 Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 66 | JvmtiUniquePtr<char[]> name_copy; |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 67 | if (name_ptr != nullptr) { |
| 68 | const char* field_name = art_field->GetName(); |
| 69 | if (field_name == nullptr) { |
| 70 | field_name = "<error>"; |
| 71 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 72 | jvmtiError ret; |
| 73 | name_copy = CopyString(env, field_name, &ret); |
| 74 | if (name_copy == nullptr) { |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 75 | return ret; |
| 76 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 77 | *name_ptr = name_copy.get(); |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 80 | JvmtiUniquePtr<char[]> signature_copy; |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 81 | if (signature_ptr != nullptr) { |
| 82 | const char* sig = art_field->GetTypeDescriptor(); |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 83 | jvmtiError ret; |
| 84 | signature_copy = CopyString(env, sig, &ret); |
| 85 | if (signature_copy == nullptr) { |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 86 | return ret; |
| 87 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 88 | *signature_ptr = signature_copy.get(); |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // TODO: Support generic signature. |
| 92 | if (generic_ptr != nullptr) { |
| 93 | *generic_ptr = nullptr; |
Andreas Gampe | 188abab | 2017-02-16 10:34:05 -0800 | [diff] [blame] | 94 | 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 Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 103 | jvmtiError ret; |
| 104 | JvmtiUniquePtr<char[]> copy = CopyString(env, output_string.c_str(), &ret); |
| 105 | if (copy == nullptr) { |
Andreas Gampe | 188abab | 2017-02-16 10:34:05 -0800 | [diff] [blame] | 106 | return ret; |
| 107 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 108 | *generic_ptr = copy.release(); |
Andreas Gampe | 188abab | 2017-02-16 10:34:05 -0800 | [diff] [blame] | 109 | } else if (soa.Self()->IsExceptionPending()) { |
| 110 | // TODO: Should we report an error here? |
| 111 | soa.Self()->ClearException(); |
| 112 | } |
| 113 | } |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // Everything is fine, release the buffers. |
| 117 | name_copy.release(); |
| 118 | signature_copy.release(); |
| 119 | |
| 120 | return ERR(NONE); |
| 121 | } |
| 122 | |
| 123 | jvmtiError 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 | |
| 146 | jvmtiError 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 | |
| 169 | jvmtiError 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 |