blob: bb48a2b5422c0151c1fed0c97d1ac2e90c3fbe0a [file] [log] [blame]
Andreas Gampe3c252f02016-10-27 18:25:17 -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_method.h"
33
34#include "art_jvmti.h"
35#include "art_method-inl.h"
36#include "base/enums.h"
Andreas Gampe13b27842016-11-07 16:48:23 -080037#include "jni_internal.h"
Andreas Gampe36bcd4f2016-10-28 18:07:18 -070038#include "modifiers.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070039#include "scoped_thread_state_change-inl.h"
Andreas Gampeab2f0d02017-01-05 17:23:45 -080040#include "thread-inl.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070041
42namespace openjdkjvmti {
43
Andreas Gampef71832e2017-01-09 11:38:04 -080044jvmtiError MethodUtil::GetArgumentsSize(jvmtiEnv* env ATTRIBUTE_UNUSED,
45 jmethodID method,
46 jint* size_ptr) {
47 if (method == nullptr) {
48 return ERR(INVALID_METHODID);
49 }
50 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
51
52 if (art_method->IsNative()) {
53 return ERR(NATIVE_METHOD);
54 }
55
56 if (size_ptr == nullptr) {
57 return ERR(NULL_POINTER);
58 }
59
60 art::ScopedObjectAccess soa(art::Thread::Current());
61 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
62 // This isn't specified as an error case, so return 0.
63 *size_ptr = 0;
64 return ERR(NONE);
65 }
66
67 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
68 *size_ptr = art_method->GetCodeItem()->ins_size_;
69
70 return ERR(NONE);
71}
72
73jvmtiError MethodUtil::GetMaxLocals(jvmtiEnv* env ATTRIBUTE_UNUSED,
74 jmethodID method,
75 jint* max_ptr) {
76 if (method == nullptr) {
77 return ERR(INVALID_METHODID);
78 }
79 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
80
81 if (art_method->IsNative()) {
82 return ERR(NATIVE_METHOD);
83 }
84
85 if (max_ptr == nullptr) {
86 return ERR(NULL_POINTER);
87 }
88
89 art::ScopedObjectAccess soa(art::Thread::Current());
90 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
91 // This isn't specified as an error case, so return 0.
92 *max_ptr = 0;
93 return ERR(NONE);
94 }
95
96 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
97 *max_ptr = art_method->GetCodeItem()->registers_size_;
98
99 return ERR(NONE);
100}
101
Andreas Gampe3c252f02016-10-27 18:25:17 -0700102jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env,
103 jmethodID method,
104 char** name_ptr,
105 char** signature_ptr,
106 char** generic_ptr) {
107 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe13b27842016-11-07 16:48:23 -0800108 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe3c252f02016-10-27 18:25:17 -0700109 art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
110
111 JvmtiUniquePtr name_copy;
112 if (name_ptr != nullptr) {
113 const char* method_name = art_method->GetName();
114 if (method_name == nullptr) {
115 method_name = "<error>";
116 }
117 unsigned char* tmp;
118 jvmtiError ret = CopyString(env, method_name, &tmp);
119 if (ret != ERR(NONE)) {
120 return ret;
121 }
122 name_copy = MakeJvmtiUniquePtr(env, tmp);
123 *name_ptr = reinterpret_cast<char*>(tmp);
124 }
125
126 JvmtiUniquePtr signature_copy;
127 if (signature_ptr != nullptr) {
128 const art::Signature sig = art_method->GetSignature();
129 std::string str = sig.ToString();
130 unsigned char* tmp;
131 jvmtiError ret = CopyString(env, str.c_str(), &tmp);
132 if (ret != ERR(NONE)) {
133 return ret;
134 }
135 signature_copy = MakeJvmtiUniquePtr(env, tmp);
136 *signature_ptr = reinterpret_cast<char*>(tmp);
137 }
138
139 // TODO: Support generic signature.
Andreas Gampe862bdd82016-11-18 13:31:13 -0800140 if (generic_ptr != nullptr) {
141 *generic_ptr = nullptr;
142 }
Andreas Gampe3c252f02016-10-27 18:25:17 -0700143
144 // Everything is fine, release the buffers.
145 name_copy.release();
146 signature_copy.release();
147
148 return ERR(NONE);
149}
150
Andreas Gampe368a2082016-10-28 17:33:13 -0700151jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
152 jmethodID method,
153 jclass* declaring_class_ptr) {
154 if (declaring_class_ptr == nullptr) {
155 return ERR(NULL_POINTER);
156 }
157
Andreas Gampe13b27842016-11-07 16:48:23 -0800158 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe368a2082016-10-28 17:33:13 -0700159 // Note: No GetInterfaceMethodIfProxy, we want to actual class.
160
Andreas Gampe13b27842016-11-07 16:48:23 -0800161 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe368a2082016-10-28 17:33:13 -0700162 art::mirror::Class* klass = art_method->GetDeclaringClass();
163 *declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
164
165 return ERR(NONE);
166}
167
Andreas Gampef71832e2017-01-09 11:38:04 -0800168jvmtiError MethodUtil::GetMethodLocation(jvmtiEnv* env ATTRIBUTE_UNUSED,
169 jmethodID method,
170 jlocation* start_location_ptr,
171 jlocation* end_location_ptr) {
172 if (method == nullptr) {
173 return ERR(INVALID_METHODID);
174 }
175 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
176
177 if (art_method->IsNative()) {
178 return ERR(NATIVE_METHOD);
179 }
180
181 if (start_location_ptr == nullptr || end_location_ptr == nullptr) {
182 return ERR(NULL_POINTER);
183 }
184
185 art::ScopedObjectAccess soa(art::Thread::Current());
186 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
187 // This isn't specified as an error case, so return 0/0.
188 *start_location_ptr = 0;
189 *end_location_ptr = 0;
190 return ERR(NONE);
191 }
192
193 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
194 *start_location_ptr = 0;
195 *end_location_ptr = art_method->GetCodeItem()->insns_size_in_code_units_ - 1;
196
197 return ERR(NONE);
198}
199
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700200jvmtiError MethodUtil::GetMethodModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
201 jmethodID method,
202 jint* modifiers_ptr) {
203 if (modifiers_ptr == nullptr) {
204 return ERR(NULL_POINTER);
205 }
206
Andreas Gampe13b27842016-11-07 16:48:23 -0800207 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700208 uint32_t modifiers = art_method->GetAccessFlags();
209
210 // Note: Keep this code in sync with Executable.fixMethodFlags.
211 if ((modifiers & art::kAccAbstract) != 0) {
212 modifiers &= ~art::kAccNative;
213 }
214 modifiers &= ~art::kAccSynchronized;
215 if ((modifiers & art::kAccDeclaredSynchronized) != 0) {
216 modifiers |= art::kAccSynchronized;
217 }
218 modifiers &= art::kAccJavaFlagsMask;
219
220 *modifiers_ptr = modifiers;
221 return ERR(NONE);
222}
223
Andreas Gampeda3e5612016-12-13 19:00:53 -0800224using LineNumberContext = std::vector<jvmtiLineNumberEntry>;
225
226static bool CollectLineNumbers(void* void_context, const art::DexFile::PositionInfo& entry) {
227 LineNumberContext* context = reinterpret_cast<LineNumberContext*>(void_context);
228 jvmtiLineNumberEntry jvmti_entry = { static_cast<jlocation>(entry.address_),
229 static_cast<jint>(entry.line_) };
230 context->push_back(jvmti_entry);
231 return false; // Collect all, no early exit.
232}
233
234jvmtiError MethodUtil::GetLineNumberTable(jvmtiEnv* env,
235 jmethodID method,
236 jint* entry_count_ptr,
237 jvmtiLineNumberEntry** table_ptr) {
238 if (method == nullptr) {
239 return ERR(NULL_POINTER);
240 }
241 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
242 DCHECK(!art_method->IsRuntimeMethod());
243
244 const art::DexFile::CodeItem* code_item;
245 const art::DexFile* dex_file;
246 {
247 art::ScopedObjectAccess soa(art::Thread::Current());
248
249 if (art_method->IsProxyMethod()) {
250 return ERR(ABSENT_INFORMATION);
251 }
252 if (art_method->IsNative()) {
253 return ERR(NATIVE_METHOD);
254 }
255 if (entry_count_ptr == nullptr || table_ptr == nullptr) {
256 return ERR(NULL_POINTER);
257 }
258
259 code_item = art_method->GetCodeItem();
260 dex_file = art_method->GetDexFile();
261 DCHECK(code_item != nullptr) << art_method->PrettyMethod() << " " << dex_file->GetLocation();
262 }
263
264 LineNumberContext context;
265 bool success = dex_file->DecodeDebugPositionInfo(code_item, CollectLineNumbers, &context);
266 if (!success) {
267 return ERR(ABSENT_INFORMATION);
268 }
269
270 unsigned char* data;
271 jlong mem_size = context.size() * sizeof(jvmtiLineNumberEntry);
272 jvmtiError alloc_error = env->Allocate(mem_size, &data);
273 if (alloc_error != ERR(NONE)) {
274 return alloc_error;
275 }
276 *table_ptr = reinterpret_cast<jvmtiLineNumberEntry*>(data);
277 memcpy(*table_ptr, context.data(), mem_size);
278 *entry_count_ptr = static_cast<jint>(context.size());
279
280 return ERR(NONE);
281}
282
Andreas Gampe3c252f02016-10-27 18:25:17 -0700283} // namespace openjdkjvmti