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 | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 37 | #include "jni_internal.h" |
Andreas Gampe | 36bcd4f | 2016-10-28 18:07:18 -0700 | [diff] [blame] | 38 | #include "modifiers.h" |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 39 | #include "scoped_thread_state_change-inl.h" |
| 40 | |
| 41 | namespace openjdkjvmti { |
| 42 | |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 43 | jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env, |
| 44 | jmethodID method, |
| 45 | char** name_ptr, |
| 46 | char** signature_ptr, |
| 47 | char** generic_ptr) { |
| 48 | art::ScopedObjectAccess soa(art::Thread::Current()); |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 49 | art::ArtMethod* art_method = art::jni::DecodeArtMethod(method); |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 50 | art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize); |
| 51 | |
| 52 | JvmtiUniquePtr name_copy; |
| 53 | if (name_ptr != nullptr) { |
| 54 | const char* method_name = art_method->GetName(); |
| 55 | if (method_name == nullptr) { |
| 56 | method_name = "<error>"; |
| 57 | } |
| 58 | unsigned char* tmp; |
| 59 | jvmtiError ret = CopyString(env, method_name, &tmp); |
| 60 | if (ret != ERR(NONE)) { |
| 61 | return ret; |
| 62 | } |
| 63 | name_copy = MakeJvmtiUniquePtr(env, tmp); |
| 64 | *name_ptr = reinterpret_cast<char*>(tmp); |
| 65 | } |
| 66 | |
| 67 | JvmtiUniquePtr signature_copy; |
| 68 | if (signature_ptr != nullptr) { |
| 69 | const art::Signature sig = art_method->GetSignature(); |
| 70 | std::string str = sig.ToString(); |
| 71 | unsigned char* tmp; |
| 72 | jvmtiError ret = CopyString(env, str.c_str(), &tmp); |
| 73 | if (ret != ERR(NONE)) { |
| 74 | return ret; |
| 75 | } |
| 76 | signature_copy = MakeJvmtiUniquePtr(env, tmp); |
| 77 | *signature_ptr = reinterpret_cast<char*>(tmp); |
| 78 | } |
| 79 | |
| 80 | // TODO: Support generic signature. |
Andreas Gampe | 862bdd8 | 2016-11-18 13:31:13 -0800 | [diff] [blame] | 81 | if (generic_ptr != nullptr) { |
| 82 | *generic_ptr = nullptr; |
| 83 | } |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 84 | |
| 85 | // Everything is fine, release the buffers. |
| 86 | name_copy.release(); |
| 87 | signature_copy.release(); |
| 88 | |
| 89 | return ERR(NONE); |
| 90 | } |
| 91 | |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 92 | jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 93 | jmethodID method, |
| 94 | jclass* declaring_class_ptr) { |
| 95 | if (declaring_class_ptr == nullptr) { |
| 96 | return ERR(NULL_POINTER); |
| 97 | } |
| 98 | |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 99 | art::ArtMethod* art_method = art::jni::DecodeArtMethod(method); |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 100 | // Note: No GetInterfaceMethodIfProxy, we want to actual class. |
| 101 | |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 102 | art::ScopedObjectAccess soa(art::Thread::Current()); |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 103 | art::mirror::Class* klass = art_method->GetDeclaringClass(); |
| 104 | *declaring_class_ptr = soa.AddLocalReference<jclass>(klass); |
| 105 | |
| 106 | return ERR(NONE); |
| 107 | } |
| 108 | |
Andreas Gampe | 36bcd4f | 2016-10-28 18:07:18 -0700 | [diff] [blame] | 109 | jvmtiError MethodUtil::GetMethodModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 110 | jmethodID method, |
| 111 | jint* modifiers_ptr) { |
| 112 | if (modifiers_ptr == nullptr) { |
| 113 | return ERR(NULL_POINTER); |
| 114 | } |
| 115 | |
Andreas Gampe | 13b2784 | 2016-11-07 16:48:23 -0800 | [diff] [blame] | 116 | art::ArtMethod* art_method = art::jni::DecodeArtMethod(method); |
Andreas Gampe | 36bcd4f | 2016-10-28 18:07:18 -0700 | [diff] [blame] | 117 | uint32_t modifiers = art_method->GetAccessFlags(); |
| 118 | |
| 119 | // Note: Keep this code in sync with Executable.fixMethodFlags. |
| 120 | if ((modifiers & art::kAccAbstract) != 0) { |
| 121 | modifiers &= ~art::kAccNative; |
| 122 | } |
| 123 | modifiers &= ~art::kAccSynchronized; |
| 124 | if ((modifiers & art::kAccDeclaredSynchronized) != 0) { |
| 125 | modifiers |= art::kAccSynchronized; |
| 126 | } |
| 127 | modifiers &= art::kAccJavaFlagsMask; |
| 128 | |
| 129 | *modifiers_ptr = modifiers; |
| 130 | return ERR(NONE); |
| 131 | } |
| 132 | |
Andreas Gampe | da3e561 | 2016-12-13 19:00:53 -0800 | [diff] [blame^] | 133 | using LineNumberContext = std::vector<jvmtiLineNumberEntry>; |
| 134 | |
| 135 | static bool CollectLineNumbers(void* void_context, const art::DexFile::PositionInfo& entry) { |
| 136 | LineNumberContext* context = reinterpret_cast<LineNumberContext*>(void_context); |
| 137 | jvmtiLineNumberEntry jvmti_entry = { static_cast<jlocation>(entry.address_), |
| 138 | static_cast<jint>(entry.line_) }; |
| 139 | context->push_back(jvmti_entry); |
| 140 | return false; // Collect all, no early exit. |
| 141 | } |
| 142 | |
| 143 | jvmtiError MethodUtil::GetLineNumberTable(jvmtiEnv* env, |
| 144 | jmethodID method, |
| 145 | jint* entry_count_ptr, |
| 146 | jvmtiLineNumberEntry** table_ptr) { |
| 147 | if (method == nullptr) { |
| 148 | return ERR(NULL_POINTER); |
| 149 | } |
| 150 | art::ArtMethod* art_method = art::jni::DecodeArtMethod(method); |
| 151 | DCHECK(!art_method->IsRuntimeMethod()); |
| 152 | |
| 153 | const art::DexFile::CodeItem* code_item; |
| 154 | const art::DexFile* dex_file; |
| 155 | { |
| 156 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 157 | |
| 158 | if (art_method->IsProxyMethod()) { |
| 159 | return ERR(ABSENT_INFORMATION); |
| 160 | } |
| 161 | if (art_method->IsNative()) { |
| 162 | return ERR(NATIVE_METHOD); |
| 163 | } |
| 164 | if (entry_count_ptr == nullptr || table_ptr == nullptr) { |
| 165 | return ERR(NULL_POINTER); |
| 166 | } |
| 167 | |
| 168 | code_item = art_method->GetCodeItem(); |
| 169 | dex_file = art_method->GetDexFile(); |
| 170 | DCHECK(code_item != nullptr) << art_method->PrettyMethod() << " " << dex_file->GetLocation(); |
| 171 | } |
| 172 | |
| 173 | LineNumberContext context; |
| 174 | bool success = dex_file->DecodeDebugPositionInfo(code_item, CollectLineNumbers, &context); |
| 175 | if (!success) { |
| 176 | return ERR(ABSENT_INFORMATION); |
| 177 | } |
| 178 | |
| 179 | unsigned char* data; |
| 180 | jlong mem_size = context.size() * sizeof(jvmtiLineNumberEntry); |
| 181 | jvmtiError alloc_error = env->Allocate(mem_size, &data); |
| 182 | if (alloc_error != ERR(NONE)) { |
| 183 | return alloc_error; |
| 184 | } |
| 185 | *table_ptr = reinterpret_cast<jvmtiLineNumberEntry*>(data); |
| 186 | memcpy(*table_ptr, context.data(), mem_size); |
| 187 | *entry_count_ptr = static_cast<jint>(context.size()); |
| 188 | |
| 189 | return ERR(NONE); |
| 190 | } |
| 191 | |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 192 | } // namespace openjdkjvmti |