Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [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_method.h" |
| 33 | |
| 34 | #include "art_jvmti.h" |
| 35 | #include "art_method-inl.h" |
| 36 | #include "base/enums.h" |
Andreas Gampe | 27dfa05 | 2017-02-16 15:04:36 -0800 | [diff] [blame] | 37 | #include "dex_file_annotations.h" |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 38 | #include "jni_internal.h" |
Andreas Gampe | 27dfa05 | 2017-02-16 15:04:36 -0800 | [diff] [blame] | 39 | #include "mirror/object_array-inl.h" |
Andreas Gampe | 36bcd4f | 2016-10-28 18:07:18 -0700 | [diff] [blame] | 40 | #include "modifiers.h" |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 41 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | ab2f0d0 | 2017-01-05 17:23:45 -0800 | [diff] [blame] | 42 | #include "thread-inl.h" |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 43 | |
| 44 | namespace openjdkjvmti { |
| 45 | |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 46 | jvmtiError MethodUtil::GetArgumentsSize(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 47 | jmethodID method, |
| 48 | jint* size_ptr) { |
| 49 | if (method == nullptr) { |
| 50 | return ERR(INVALID_METHODID); |
| 51 | } |
| 52 | art::ArtMethod* art_method = art::jni::DecodeArtMethod(method); |
| 53 | |
| 54 | if (art_method->IsNative()) { |
| 55 | return ERR(NATIVE_METHOD); |
| 56 | } |
| 57 | |
| 58 | if (size_ptr == nullptr) { |
| 59 | return ERR(NULL_POINTER); |
| 60 | } |
| 61 | |
| 62 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 63 | if (art_method->IsProxyMethod() || art_method->IsAbstract()) { |
| 64 | // This isn't specified as an error case, so return 0. |
| 65 | *size_ptr = 0; |
| 66 | return ERR(NONE); |
| 67 | } |
| 68 | |
| 69 | DCHECK_NE(art_method->GetCodeItemOffset(), 0u); |
| 70 | *size_ptr = art_method->GetCodeItem()->ins_size_; |
| 71 | |
| 72 | return ERR(NONE); |
| 73 | } |
| 74 | |
| 75 | jvmtiError MethodUtil::GetMaxLocals(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 76 | jmethodID method, |
| 77 | jint* max_ptr) { |
| 78 | if (method == nullptr) { |
| 79 | return ERR(INVALID_METHODID); |
| 80 | } |
| 81 | art::ArtMethod* art_method = art::jni::DecodeArtMethod(method); |
| 82 | |
| 83 | if (art_method->IsNative()) { |
| 84 | return ERR(NATIVE_METHOD); |
| 85 | } |
| 86 | |
| 87 | if (max_ptr == nullptr) { |
| 88 | return ERR(NULL_POINTER); |
| 89 | } |
| 90 | |
| 91 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 92 | if (art_method->IsProxyMethod() || art_method->IsAbstract()) { |
| 93 | // This isn't specified as an error case, so return 0. |
| 94 | *max_ptr = 0; |
| 95 | return ERR(NONE); |
| 96 | } |
| 97 | |
| 98 | DCHECK_NE(art_method->GetCodeItemOffset(), 0u); |
| 99 | *max_ptr = art_method->GetCodeItem()->registers_size_; |
| 100 | |
| 101 | return ERR(NONE); |
| 102 | } |
| 103 | |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 104 | jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env, |
| 105 | jmethodID method, |
| 106 | char** name_ptr, |
| 107 | char** signature_ptr, |
| 108 | char** generic_ptr) { |
| 109 | art::ScopedObjectAccess soa(art::Thread::Current()); |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 110 | art::ArtMethod* art_method = art::jni::DecodeArtMethod(method); |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 111 | art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize); |
| 112 | |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 113 | JvmtiUniquePtr<char[]> name_copy; |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 114 | if (name_ptr != nullptr) { |
| 115 | const char* method_name = art_method->GetName(); |
| 116 | if (method_name == nullptr) { |
| 117 | method_name = "<error>"; |
| 118 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 119 | jvmtiError ret; |
| 120 | name_copy = CopyString(env, method_name, &ret); |
| 121 | if (name_copy == nullptr) { |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 122 | return ret; |
| 123 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 124 | *name_ptr = name_copy.get(); |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 127 | JvmtiUniquePtr<char[]> signature_copy; |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 128 | if (signature_ptr != nullptr) { |
| 129 | const art::Signature sig = art_method->GetSignature(); |
| 130 | std::string str = sig.ToString(); |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 131 | jvmtiError ret; |
| 132 | signature_copy = CopyString(env, str.c_str(), &ret); |
| 133 | if (signature_copy == nullptr) { |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 134 | return ret; |
| 135 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 136 | *signature_ptr = signature_copy.get(); |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Andreas Gampe | 862bdd8 | 2016-11-18 13:31:13 -0800 | [diff] [blame] | 139 | if (generic_ptr != nullptr) { |
| 140 | *generic_ptr = nullptr; |
Andreas Gampe | 27dfa05 | 2017-02-16 15:04:36 -0800 | [diff] [blame] | 141 | if (!art_method->GetDeclaringClass()->IsProxyClass()) { |
| 142 | art::mirror::ObjectArray<art::mirror::String>* str_array = |
| 143 | art::annotations::GetSignatureAnnotationForMethod(art_method); |
| 144 | if (str_array != nullptr) { |
| 145 | std::ostringstream oss; |
| 146 | for (int32_t i = 0; i != str_array->GetLength(); ++i) { |
| 147 | oss << str_array->Get(i)->ToModifiedUtf8(); |
| 148 | } |
| 149 | std::string output_string = oss.str(); |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 150 | jvmtiError ret; |
| 151 | JvmtiUniquePtr<char[]> generic_copy = CopyString(env, output_string.c_str(), &ret); |
| 152 | if (generic_copy == nullptr) { |
Andreas Gampe | 27dfa05 | 2017-02-16 15:04:36 -0800 | [diff] [blame] | 153 | return ret; |
| 154 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame^] | 155 | *generic_ptr = generic_copy.release(); |
Andreas Gampe | 27dfa05 | 2017-02-16 15:04:36 -0800 | [diff] [blame] | 156 | } else if (soa.Self()->IsExceptionPending()) { |
| 157 | // TODO: Should we report an error here? |
| 158 | soa.Self()->ClearException(); |
| 159 | } |
| 160 | } |
Andreas Gampe | 862bdd8 | 2016-11-18 13:31:13 -0800 | [diff] [blame] | 161 | } |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 162 | |
| 163 | // Everything is fine, release the buffers. |
| 164 | name_copy.release(); |
| 165 | signature_copy.release(); |
| 166 | |
| 167 | return ERR(NONE); |
| 168 | } |
| 169 | |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 170 | jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 171 | jmethodID method, |
| 172 | jclass* declaring_class_ptr) { |
| 173 | if (declaring_class_ptr == nullptr) { |
| 174 | return ERR(NULL_POINTER); |
| 175 | } |
| 176 | |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 177 | art::ArtMethod* art_method = art::jni::DecodeArtMethod(method); |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 178 | // Note: No GetInterfaceMethodIfProxy, we want to actual class. |
| 179 | |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 180 | art::ScopedObjectAccess soa(art::Thread::Current()); |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 181 | art::mirror::Class* klass = art_method->GetDeclaringClass(); |
| 182 | *declaring_class_ptr = soa.AddLocalReference<jclass>(klass); |
| 183 | |
| 184 | return ERR(NONE); |
| 185 | } |
| 186 | |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 187 | jvmtiError MethodUtil::GetMethodLocation(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 188 | jmethodID method, |
| 189 | jlocation* start_location_ptr, |
| 190 | jlocation* end_location_ptr) { |
| 191 | if (method == nullptr) { |
| 192 | return ERR(INVALID_METHODID); |
| 193 | } |
| 194 | art::ArtMethod* art_method = art::jni::DecodeArtMethod(method); |
| 195 | |
| 196 | if (art_method->IsNative()) { |
| 197 | return ERR(NATIVE_METHOD); |
| 198 | } |
| 199 | |
| 200 | if (start_location_ptr == nullptr || end_location_ptr == nullptr) { |
| 201 | return ERR(NULL_POINTER); |
| 202 | } |
| 203 | |
| 204 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 205 | if (art_method->IsProxyMethod() || art_method->IsAbstract()) { |
| 206 | // This isn't specified as an error case, so return 0/0. |
| 207 | *start_location_ptr = 0; |
| 208 | *end_location_ptr = 0; |
| 209 | return ERR(NONE); |
| 210 | } |
| 211 | |
| 212 | DCHECK_NE(art_method->GetCodeItemOffset(), 0u); |
| 213 | *start_location_ptr = 0; |
| 214 | *end_location_ptr = art_method->GetCodeItem()->insns_size_in_code_units_ - 1; |
| 215 | |
| 216 | return ERR(NONE); |
| 217 | } |
| 218 | |
Andreas Gampe | 36bcd4f | 2016-10-28 18:07:18 -0700 | [diff] [blame] | 219 | jvmtiError MethodUtil::GetMethodModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 220 | jmethodID method, |
| 221 | jint* modifiers_ptr) { |
| 222 | if (modifiers_ptr == nullptr) { |
| 223 | return ERR(NULL_POINTER); |
| 224 | } |
| 225 | |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 226 | art::ArtMethod* art_method = art::jni::DecodeArtMethod(method); |
Andreas Gampe | 36bcd4f | 2016-10-28 18:07:18 -0700 | [diff] [blame] | 227 | uint32_t modifiers = art_method->GetAccessFlags(); |
| 228 | |
| 229 | // Note: Keep this code in sync with Executable.fixMethodFlags. |
| 230 | if ((modifiers & art::kAccAbstract) != 0) { |
| 231 | modifiers &= ~art::kAccNative; |
| 232 | } |
| 233 | modifiers &= ~art::kAccSynchronized; |
| 234 | if ((modifiers & art::kAccDeclaredSynchronized) != 0) { |
| 235 | modifiers |= art::kAccSynchronized; |
| 236 | } |
| 237 | modifiers &= art::kAccJavaFlagsMask; |
| 238 | |
| 239 | *modifiers_ptr = modifiers; |
| 240 | return ERR(NONE); |
| 241 | } |
| 242 | |
Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame] | 243 | using LineNumberContext = std::vector<jvmtiLineNumberEntry>; |
| 244 | |
| 245 | static bool CollectLineNumbers(void* void_context, const art::DexFile::PositionInfo& entry) { |
| 246 | LineNumberContext* context = reinterpret_cast<LineNumberContext*>(void_context); |
| 247 | jvmtiLineNumberEntry jvmti_entry = { static_cast<jlocation>(entry.address_), |
| 248 | static_cast<jint>(entry.line_) }; |
| 249 | context->push_back(jvmti_entry); |
| 250 | return false; // Collect all, no early exit. |
| 251 | } |
| 252 | |
| 253 | jvmtiError MethodUtil::GetLineNumberTable(jvmtiEnv* env, |
| 254 | jmethodID method, |
| 255 | jint* entry_count_ptr, |
| 256 | jvmtiLineNumberEntry** table_ptr) { |
| 257 | if (method == nullptr) { |
| 258 | return ERR(NULL_POINTER); |
| 259 | } |
| 260 | art::ArtMethod* art_method = art::jni::DecodeArtMethod(method); |
| 261 | DCHECK(!art_method->IsRuntimeMethod()); |
| 262 | |
| 263 | const art::DexFile::CodeItem* code_item; |
| 264 | const art::DexFile* dex_file; |
| 265 | { |
| 266 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 267 | |
| 268 | if (art_method->IsProxyMethod()) { |
| 269 | return ERR(ABSENT_INFORMATION); |
| 270 | } |
| 271 | if (art_method->IsNative()) { |
| 272 | return ERR(NATIVE_METHOD); |
| 273 | } |
| 274 | if (entry_count_ptr == nullptr || table_ptr == nullptr) { |
| 275 | return ERR(NULL_POINTER); |
| 276 | } |
| 277 | |
| 278 | code_item = art_method->GetCodeItem(); |
| 279 | dex_file = art_method->GetDexFile(); |
| 280 | DCHECK(code_item != nullptr) << art_method->PrettyMethod() << " " << dex_file->GetLocation(); |
| 281 | } |
| 282 | |
| 283 | LineNumberContext context; |
| 284 | bool success = dex_file->DecodeDebugPositionInfo(code_item, CollectLineNumbers, &context); |
| 285 | if (!success) { |
| 286 | return ERR(ABSENT_INFORMATION); |
| 287 | } |
| 288 | |
| 289 | unsigned char* data; |
| 290 | jlong mem_size = context.size() * sizeof(jvmtiLineNumberEntry); |
| 291 | jvmtiError alloc_error = env->Allocate(mem_size, &data); |
| 292 | if (alloc_error != ERR(NONE)) { |
| 293 | return alloc_error; |
| 294 | } |
| 295 | *table_ptr = reinterpret_cast<jvmtiLineNumberEntry*>(data); |
| 296 | memcpy(*table_ptr, context.data(), mem_size); |
| 297 | *entry_count_ptr = static_cast<jint>(context.size()); |
| 298 | |
| 299 | return ERR(NONE); |
| 300 | } |
| 301 | |
Andreas Gampe | fdeef52 | 2017-01-09 14:40:25 -0800 | [diff] [blame] | 302 | template <typename T> |
| 303 | static jvmtiError IsMethodT(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 304 | jmethodID method, |
| 305 | T test, |
| 306 | jboolean* is_t_ptr) { |
| 307 | if (method == nullptr) { |
| 308 | return ERR(INVALID_METHODID); |
| 309 | } |
| 310 | if (is_t_ptr == nullptr) { |
| 311 | return ERR(NULL_POINTER); |
| 312 | } |
| 313 | |
| 314 | art::ArtMethod* art_method = art::jni::DecodeArtMethod(method); |
| 315 | *is_t_ptr = test(art_method) ? JNI_TRUE : JNI_FALSE; |
| 316 | |
| 317 | return ERR(NONE); |
| 318 | } |
| 319 | |
| 320 | jvmtiError MethodUtil::IsMethodNative(jvmtiEnv* env, jmethodID m, jboolean* is_native_ptr) { |
| 321 | auto test = [](art::ArtMethod* method) { |
| 322 | return method->IsNative(); |
| 323 | }; |
| 324 | return IsMethodT(env, m, test, is_native_ptr); |
| 325 | } |
| 326 | |
| 327 | jvmtiError MethodUtil::IsMethodObsolete(jvmtiEnv* env, jmethodID m, jboolean* is_obsolete_ptr) { |
| 328 | auto test = [](art::ArtMethod* method) { |
| 329 | return method->IsObsolete(); |
| 330 | }; |
| 331 | return IsMethodT(env, m, test, is_obsolete_ptr); |
| 332 | } |
| 333 | |
| 334 | jvmtiError MethodUtil::IsMethodSynthetic(jvmtiEnv* env, jmethodID m, jboolean* is_synthetic_ptr) { |
| 335 | auto test = [](art::ArtMethod* method) { |
| 336 | return method->IsSynthetic(); |
| 337 | }; |
| 338 | return IsMethodT(env, m, test, is_synthetic_ptr); |
| 339 | } |
| 340 | |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 341 | } // namespace openjdkjvmti |