blob: 295894157cba64386fbe2b32f7b87a18a7a3de6a [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
Andreas Gampe49fc60e2017-08-24 13:19:59 -070034#include <type_traits>
35
Andreas Gampe3c252f02016-10-27 18:25:17 -070036#include "art_jvmti.h"
37#include "art_method-inl.h"
38#include "base/enums.h"
Alex Lightbebd7bd2017-07-25 14:05:52 -070039#include "base/mutex-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080040#include "dex/code_item_accessors-inl.h"
41#include "dex/dex_file_annotations.h"
42#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080043#include "dex/modifiers.h"
Alex Lightd78ddec2017-04-18 15:20:38 -070044#include "events-inl.h"
Alex Light318afe62018-03-22 16:50:10 -070045#include "gc_root-inl.h"
Alex Light0a5ec3d2017-07-25 16:50:26 -070046#include "jit/jit.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010047#include "jni/jni_internal.h"
Alex Lightbebd7bd2017-07-25 14:05:52 -070048#include "mirror/class-inl.h"
49#include "mirror/class_loader.h"
50#include "mirror/object-inl.h"
Andreas Gampe27dfa052017-02-16 15:04:36 -080051#include "mirror/object_array-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070052#include "nativehelper/scoped_local_ref.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000053#include "oat_file.h"
Alex Lightd78ddec2017-04-18 15:20:38 -070054#include "runtime_callbacks.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070055#include "scoped_thread_state_change-inl.h"
Alex Lightbebd7bd2017-07-25 14:05:52 -070056#include "stack.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070057#include "thread-current-inl.h"
Alex Lightd78ddec2017-04-18 15:20:38 -070058#include "thread_list.h"
Alex Lighte814f9d2017-07-31 16:14:39 -070059#include "ti_stack.h"
Alex Lightbebd7bd2017-07-25 14:05:52 -070060#include "ti_thread.h"
Alex Light0af8cde2017-04-20 13:35:05 -070061#include "ti_phase.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070062
63namespace openjdkjvmti {
64
Alex Lightd78ddec2017-04-18 15:20:38 -070065struct TiMethodCallback : public art::MethodCallback {
66 void RegisterNativeMethod(art::ArtMethod* method,
67 const void* cur_method,
68 /*out*/void** new_method)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010069 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightd78ddec2017-04-18 15:20:38 -070070 if (event_handler->IsEventEnabledAnywhere(ArtJvmtiEvent::kNativeMethodBind)) {
71 art::Thread* thread = art::Thread::Current();
Alex Light0af8cde2017-04-20 13:35:05 -070072 art::JNIEnvExt* jnienv = thread->GetJniEnv();
Alex Lightd78ddec2017-04-18 15:20:38 -070073 ScopedLocalRef<jthread> thread_jni(
Alex Light0af8cde2017-04-20 13:35:05 -070074 jnienv, PhaseUtil::IsLivePhase() ? jnienv->AddLocalReference<jthread>(thread->GetPeer())
75 : nullptr);
Alex Lightd78ddec2017-04-18 15:20:38 -070076 art::ScopedThreadSuspension sts(thread, art::ThreadState::kNative);
77 event_handler->DispatchEvent<ArtJvmtiEvent::kNativeMethodBind>(
78 thread,
Alex Light0af8cde2017-04-20 13:35:05 -070079 static_cast<JNIEnv*>(jnienv),
Alex Lightd78ddec2017-04-18 15:20:38 -070080 thread_jni.get(),
81 art::jni::EncodeArtMethod(method),
82 const_cast<void*>(cur_method),
83 new_method);
84 }
85 }
86
87 EventHandler* event_handler = nullptr;
88};
89
90TiMethodCallback gMethodCallback;
91
92void MethodUtil::Register(EventHandler* handler) {
93 gMethodCallback.event_handler = handler;
94 art::ScopedThreadStateChange stsc(art::Thread::Current(),
95 art::ThreadState::kWaitingForDebuggerToAttach);
96 art::ScopedSuspendAll ssa("Add method callback");
Alex Light21611932017-09-26 13:07:39 -070097 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
98 callbacks->AddMethodCallback(&gMethodCallback);
Alex Lightd78ddec2017-04-18 15:20:38 -070099}
100
101void MethodUtil::Unregister() {
102 art::ScopedThreadStateChange stsc(art::Thread::Current(),
103 art::ThreadState::kWaitingForDebuggerToAttach);
104 art::ScopedSuspendAll ssa("Remove method callback");
Alex Light21611932017-09-26 13:07:39 -0700105 art::RuntimeCallbacks* callbacks = art::Runtime::Current()->GetRuntimeCallbacks();
106 callbacks->RemoveMethodCallback(&gMethodCallback);
Alex Lightd78ddec2017-04-18 15:20:38 -0700107}
108
Alex Light4c174282017-07-05 10:18:18 -0700109jvmtiError MethodUtil::GetBytecodes(jvmtiEnv* env,
110 jmethodID method,
111 jint* size_ptr,
112 unsigned char** bytecode_ptr) {
113 if (method == nullptr) {
114 return ERR(INVALID_METHODID);
115 }
116 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
117
118 if (art_method->IsNative()) {
119 return ERR(NATIVE_METHOD);
120 }
121
122 if (size_ptr == nullptr || bytecode_ptr == nullptr) {
123 return ERR(NULL_POINTER);
124 }
125
126 art::ScopedObjectAccess soa(art::Thread::Current());
David Sehr0225f8e2018-01-31 08:52:24 +0000127 art::CodeItemInstructionAccessor accessor(art_method->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800128 if (!accessor.HasCodeItem()) {
Alex Light4c174282017-07-05 10:18:18 -0700129 *size_ptr = 0;
130 *bytecode_ptr = nullptr;
131 return OK;
132 }
133 // 2 bytes per instruction for dex code.
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800134 *size_ptr = accessor.InsnsSizeInCodeUnits() * 2;
Alex Light4c174282017-07-05 10:18:18 -0700135 jvmtiError err = env->Allocate(*size_ptr, bytecode_ptr);
136 if (err != OK) {
137 return err;
138 }
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800139 memcpy(*bytecode_ptr, accessor.Insns(), *size_ptr);
Alex Light4c174282017-07-05 10:18:18 -0700140 return OK;
141}
142
Andreas Gampef71832e2017-01-09 11:38:04 -0800143jvmtiError MethodUtil::GetArgumentsSize(jvmtiEnv* env ATTRIBUTE_UNUSED,
144 jmethodID method,
145 jint* size_ptr) {
146 if (method == nullptr) {
147 return ERR(INVALID_METHODID);
148 }
149 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
150
151 if (art_method->IsNative()) {
152 return ERR(NATIVE_METHOD);
153 }
154
155 if (size_ptr == nullptr) {
156 return ERR(NULL_POINTER);
157 }
158
159 art::ScopedObjectAccess soa(art::Thread::Current());
160 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -0700161 // Use the shorty.
162 art::ArtMethod* base_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
163 size_t arg_count = art::ArtMethod::NumArgRegisters(base_method->GetShorty());
164 if (!base_method->IsStatic()) {
165 arg_count++;
166 }
167 *size_ptr = static_cast<jint>(arg_count);
Andreas Gampef71832e2017-01-09 11:38:04 -0800168 return ERR(NONE);
169 }
170
171 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
David Sehr0225f8e2018-01-31 08:52:24 +0000172 *size_ptr = art_method->DexInstructionData().InsSize();
Andreas Gampef71832e2017-01-09 11:38:04 -0800173
174 return ERR(NONE);
175}
176
Alex Lightce68cc62017-07-26 10:30:38 -0700177jvmtiError MethodUtil::GetLocalVariableTable(jvmtiEnv* env,
178 jmethodID method,
179 jint* entry_count_ptr,
180 jvmtiLocalVariableEntry** table_ptr) {
181 if (method == nullptr) {
182 return ERR(INVALID_METHODID);
183 }
184 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
185
186 if (art_method->IsNative()) {
187 return ERR(NATIVE_METHOD);
188 }
189
190 if (entry_count_ptr == nullptr || table_ptr == nullptr) {
191 return ERR(NULL_POINTER);
192 }
193
194 art::ScopedObjectAccess soa(art::Thread::Current());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800195
196 const art::DexFile* const dex_file = art_method->GetDexFile();
197 if (dex_file == nullptr) {
198 return ERR(ABSENT_INFORMATION);
199 }
200
201 // TODO HasCodeItem == false means that the method is abstract (or native, but we check that
Alex Lightce68cc62017-07-26 10:30:38 -0700202 // earlier). We should check what is returned by the RI in this situation since it's not clear
203 // what the appropriate return value is from the spec.
David Sehr0225f8e2018-01-31 08:52:24 +0000204 art::CodeItemDebugInfoAccessor accessor(art_method->DexInstructionDebugInfo());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800205 if (!accessor.HasCodeItem()) {
Alex Lightce68cc62017-07-26 10:30:38 -0700206 return ERR(ABSENT_INFORMATION);
207 }
208
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700209 std::vector<jvmtiLocalVariableEntry> variables;
210 jvmtiError err = OK;
Alex Lightce68cc62017-07-26 10:30:38 -0700211
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700212 auto release = [&](jint* out_entry_count_ptr, jvmtiLocalVariableEntry** out_table_ptr) {
213 jlong table_size = sizeof(jvmtiLocalVariableEntry) * variables.size();
214 if (err != OK ||
215 (err = env->Allocate(table_size,
216 reinterpret_cast<unsigned char**>(out_table_ptr))) != OK) {
217 for (jvmtiLocalVariableEntry& e : variables) {
218 env->Deallocate(reinterpret_cast<unsigned char*>(e.name));
219 env->Deallocate(reinterpret_cast<unsigned char*>(e.signature));
220 env->Deallocate(reinterpret_cast<unsigned char*>(e.generic_signature));
221 }
222 return err;
Alex Lightce68cc62017-07-26 10:30:38 -0700223 }
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700224 *out_entry_count_ptr = variables.size();
225 memcpy(*out_table_ptr, variables.data(), table_size);
226 return OK;
Alex Lightce68cc62017-07-26 10:30:38 -0700227 };
228
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700229 auto visitor = [&](const art::DexFile::LocalInfo& entry) {
230 if (err != OK) {
231 return;
232 }
233 JvmtiUniquePtr<char[]> name_str = CopyString(env, entry.name_, &err);
234 if (err != OK) {
235 return;
236 }
237 JvmtiUniquePtr<char[]> sig_str = CopyString(env, entry.descriptor_, &err);
238 if (err != OK) {
239 return;
240 }
241 JvmtiUniquePtr<char[]> generic_sig_str = CopyString(env, entry.signature_, &err);
242 if (err != OK) {
243 return;
244 }
245 variables.push_back({
246 .start_location = static_cast<jlocation>(entry.start_address_),
247 .length = static_cast<jint>(entry.end_address_ - entry.start_address_),
248 .name = name_str.release(),
249 .signature = sig_str.release(),
250 .generic_signature = generic_sig_str.release(),
251 .slot = entry.reg_,
252 });
253 };
254
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800255 if (!accessor.DecodeDebugLocalInfo(art_method->IsStatic(),
256 art_method->GetDexMethodIndex(),
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700257 visitor)) {
Alex Lightce68cc62017-07-26 10:30:38 -0700258 // Something went wrong with decoding the debug information. It might as well not be there.
259 return ERR(ABSENT_INFORMATION);
Alex Lightce68cc62017-07-26 10:30:38 -0700260 }
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700261 return release(entry_count_ptr, table_ptr);
Alex Lightce68cc62017-07-26 10:30:38 -0700262}
263
Andreas Gampef71832e2017-01-09 11:38:04 -0800264jvmtiError MethodUtil::GetMaxLocals(jvmtiEnv* env ATTRIBUTE_UNUSED,
265 jmethodID method,
266 jint* max_ptr) {
267 if (method == nullptr) {
268 return ERR(INVALID_METHODID);
269 }
270 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
271
272 if (art_method->IsNative()) {
273 return ERR(NATIVE_METHOD);
274 }
275
276 if (max_ptr == nullptr) {
277 return ERR(NULL_POINTER);
278 }
279
280 art::ScopedObjectAccess soa(art::Thread::Current());
281 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
282 // This isn't specified as an error case, so return 0.
283 *max_ptr = 0;
284 return ERR(NONE);
285 }
286
287 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
David Sehr0225f8e2018-01-31 08:52:24 +0000288 *max_ptr = art_method->DexInstructionData().RegistersSize();
Andreas Gampef71832e2017-01-09 11:38:04 -0800289
290 return ERR(NONE);
291}
292
Andreas Gampe3c252f02016-10-27 18:25:17 -0700293jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env,
294 jmethodID method,
295 char** name_ptr,
296 char** signature_ptr,
297 char** generic_ptr) {
298 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe13b27842016-11-07 16:48:23 -0800299 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe3c252f02016-10-27 18:25:17 -0700300 art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
301
Andreas Gampe54711412017-02-21 12:41:43 -0800302 JvmtiUniquePtr<char[]> name_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700303 if (name_ptr != nullptr) {
304 const char* method_name = art_method->GetName();
305 if (method_name == nullptr) {
306 method_name = "<error>";
307 }
Andreas Gampe54711412017-02-21 12:41:43 -0800308 jvmtiError ret;
309 name_copy = CopyString(env, method_name, &ret);
310 if (name_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700311 return ret;
312 }
Andreas Gampe54711412017-02-21 12:41:43 -0800313 *name_ptr = name_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700314 }
315
Andreas Gampe54711412017-02-21 12:41:43 -0800316 JvmtiUniquePtr<char[]> signature_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700317 if (signature_ptr != nullptr) {
318 const art::Signature sig = art_method->GetSignature();
319 std::string str = sig.ToString();
Andreas Gampe54711412017-02-21 12:41:43 -0800320 jvmtiError ret;
321 signature_copy = CopyString(env, str.c_str(), &ret);
322 if (signature_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700323 return ret;
324 }
Andreas Gampe54711412017-02-21 12:41:43 -0800325 *signature_ptr = signature_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700326 }
327
Andreas Gampe862bdd82016-11-18 13:31:13 -0800328 if (generic_ptr != nullptr) {
329 *generic_ptr = nullptr;
Andreas Gampe27dfa052017-02-16 15:04:36 -0800330 if (!art_method->GetDeclaringClass()->IsProxyClass()) {
Vladimir Markoacb906d2018-05-30 10:23:49 +0100331 art::ObjPtr<art::mirror::ObjectArray<art::mirror::String>> str_array =
Andreas Gampe27dfa052017-02-16 15:04:36 -0800332 art::annotations::GetSignatureAnnotationForMethod(art_method);
333 if (str_array != nullptr) {
334 std::ostringstream oss;
335 for (int32_t i = 0; i != str_array->GetLength(); ++i) {
336 oss << str_array->Get(i)->ToModifiedUtf8();
337 }
338 std::string output_string = oss.str();
Andreas Gampe54711412017-02-21 12:41:43 -0800339 jvmtiError ret;
340 JvmtiUniquePtr<char[]> generic_copy = CopyString(env, output_string.c_str(), &ret);
341 if (generic_copy == nullptr) {
Andreas Gampe27dfa052017-02-16 15:04:36 -0800342 return ret;
343 }
Andreas Gampe54711412017-02-21 12:41:43 -0800344 *generic_ptr = generic_copy.release();
Andreas Gampe27dfa052017-02-16 15:04:36 -0800345 } else if (soa.Self()->IsExceptionPending()) {
346 // TODO: Should we report an error here?
347 soa.Self()->ClearException();
348 }
349 }
Andreas Gampe862bdd82016-11-18 13:31:13 -0800350 }
Andreas Gampe3c252f02016-10-27 18:25:17 -0700351
352 // Everything is fine, release the buffers.
353 name_copy.release();
354 signature_copy.release();
355
356 return ERR(NONE);
357}
358
Andreas Gampe368a2082016-10-28 17:33:13 -0700359jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
360 jmethodID method,
361 jclass* declaring_class_ptr) {
362 if (declaring_class_ptr == nullptr) {
363 return ERR(NULL_POINTER);
364 }
365
Andreas Gampe13b27842016-11-07 16:48:23 -0800366 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe368a2082016-10-28 17:33:13 -0700367 // Note: No GetInterfaceMethodIfProxy, we want to actual class.
368
Andreas Gampe13b27842016-11-07 16:48:23 -0800369 art::ScopedObjectAccess soa(art::Thread::Current());
Vladimir Markod93e3742018-07-18 10:58:13 +0100370 art::ObjPtr<art::mirror::Class> klass = art_method->GetDeclaringClass();
Andreas Gampe368a2082016-10-28 17:33:13 -0700371 *declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
372
373 return ERR(NONE);
374}
375
Andreas Gampef71832e2017-01-09 11:38:04 -0800376jvmtiError MethodUtil::GetMethodLocation(jvmtiEnv* env ATTRIBUTE_UNUSED,
377 jmethodID method,
378 jlocation* start_location_ptr,
379 jlocation* end_location_ptr) {
380 if (method == nullptr) {
381 return ERR(INVALID_METHODID);
382 }
383 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
384
385 if (art_method->IsNative()) {
386 return ERR(NATIVE_METHOD);
387 }
388
389 if (start_location_ptr == nullptr || end_location_ptr == nullptr) {
390 return ERR(NULL_POINTER);
391 }
392
393 art::ScopedObjectAccess soa(art::Thread::Current());
394 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -0700395 // This isn't specified as an error case, so return -1/-1 as the RI does.
396 *start_location_ptr = -1;
397 *end_location_ptr = -1;
Andreas Gampef71832e2017-01-09 11:38:04 -0800398 return ERR(NONE);
399 }
400
401 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
402 *start_location_ptr = 0;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800403 *end_location_ptr = art_method->DexInstructions().InsnsSizeInCodeUnits() - 1;
Andreas Gampef71832e2017-01-09 11:38:04 -0800404
405 return ERR(NONE);
406}
407
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700408jvmtiError MethodUtil::GetMethodModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
409 jmethodID method,
410 jint* modifiers_ptr) {
411 if (modifiers_ptr == nullptr) {
412 return ERR(NULL_POINTER);
413 }
414
Andreas Gampe13b27842016-11-07 16:48:23 -0800415 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700416 uint32_t modifiers = art_method->GetAccessFlags();
417
418 // Note: Keep this code in sync with Executable.fixMethodFlags.
419 if ((modifiers & art::kAccAbstract) != 0) {
420 modifiers &= ~art::kAccNative;
421 }
422 modifiers &= ~art::kAccSynchronized;
423 if ((modifiers & art::kAccDeclaredSynchronized) != 0) {
424 modifiers |= art::kAccSynchronized;
425 }
426 modifiers &= art::kAccJavaFlagsMask;
427
428 *modifiers_ptr = modifiers;
429 return ERR(NONE);
430}
431
Andreas Gampeda3e5612016-12-13 19:00:53 -0800432jvmtiError MethodUtil::GetLineNumberTable(jvmtiEnv* env,
433 jmethodID method,
434 jint* entry_count_ptr,
435 jvmtiLineNumberEntry** table_ptr) {
436 if (method == nullptr) {
437 return ERR(NULL_POINTER);
438 }
439 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
440 DCHECK(!art_method->IsRuntimeMethod());
441
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800442 art::CodeItemDebugInfoAccessor accessor;
Andreas Gampeda3e5612016-12-13 19:00:53 -0800443 const art::DexFile* dex_file;
444 {
445 art::ScopedObjectAccess soa(art::Thread::Current());
446
447 if (art_method->IsProxyMethod()) {
448 return ERR(ABSENT_INFORMATION);
449 }
450 if (art_method->IsNative()) {
451 return ERR(NATIVE_METHOD);
452 }
453 if (entry_count_ptr == nullptr || table_ptr == nullptr) {
454 return ERR(NULL_POINTER);
455 }
456
David Sehr0225f8e2018-01-31 08:52:24 +0000457 accessor = art::CodeItemDebugInfoAccessor(art_method->DexInstructionDebugInfo());
Andreas Gampeda3e5612016-12-13 19:00:53 -0800458 dex_file = art_method->GetDexFile();
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800459 DCHECK(accessor.HasCodeItem()) << art_method->PrettyMethod() << " " << dex_file->GetLocation();
Andreas Gampeda3e5612016-12-13 19:00:53 -0800460 }
461
Mathieu Chartier3e2e1232018-09-11 12:35:30 -0700462 std::vector<jvmtiLineNumberEntry> context;
463 bool success = accessor.DecodeDebugPositionInfo([&](const art::DexFile::PositionInfo& entry) {
464 context.push_back({static_cast<jlocation>(entry.address_), static_cast<jint>(entry.line_)});
465 return false;
466 });
Andreas Gampeda3e5612016-12-13 19:00:53 -0800467 if (!success) {
468 return ERR(ABSENT_INFORMATION);
469 }
470
471 unsigned char* data;
472 jlong mem_size = context.size() * sizeof(jvmtiLineNumberEntry);
473 jvmtiError alloc_error = env->Allocate(mem_size, &data);
474 if (alloc_error != ERR(NONE)) {
475 return alloc_error;
476 }
477 *table_ptr = reinterpret_cast<jvmtiLineNumberEntry*>(data);
478 memcpy(*table_ptr, context.data(), mem_size);
479 *entry_count_ptr = static_cast<jint>(context.size());
480
481 return ERR(NONE);
482}
483
Andreas Gampefdeef522017-01-09 14:40:25 -0800484template <typename T>
485static jvmtiError IsMethodT(jvmtiEnv* env ATTRIBUTE_UNUSED,
486 jmethodID method,
487 T test,
488 jboolean* is_t_ptr) {
489 if (method == nullptr) {
490 return ERR(INVALID_METHODID);
491 }
492 if (is_t_ptr == nullptr) {
493 return ERR(NULL_POINTER);
494 }
495
496 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
497 *is_t_ptr = test(art_method) ? JNI_TRUE : JNI_FALSE;
498
499 return ERR(NONE);
500}
501
502jvmtiError MethodUtil::IsMethodNative(jvmtiEnv* env, jmethodID m, jboolean* is_native_ptr) {
503 auto test = [](art::ArtMethod* method) {
504 return method->IsNative();
505 };
506 return IsMethodT(env, m, test, is_native_ptr);
507}
508
509jvmtiError MethodUtil::IsMethodObsolete(jvmtiEnv* env, jmethodID m, jboolean* is_obsolete_ptr) {
510 auto test = [](art::ArtMethod* method) {
511 return method->IsObsolete();
512 };
513 return IsMethodT(env, m, test, is_obsolete_ptr);
514}
515
516jvmtiError MethodUtil::IsMethodSynthetic(jvmtiEnv* env, jmethodID m, jboolean* is_synthetic_ptr) {
517 auto test = [](art::ArtMethod* method) {
518 return method->IsSynthetic();
519 };
520 return IsMethodT(env, m, test, is_synthetic_ptr);
521}
522
Alex Lightbebd7bd2017-07-25 14:05:52 -0700523class CommonLocalVariableClosure : public art::Closure {
524 public:
Alex Light318afe62018-03-22 16:50:10 -0700525 CommonLocalVariableClosure(jint depth, jint slot)
526 : result_(ERR(INTERNAL)), depth_(depth), slot_(slot) {}
Alex Lightbebd7bd2017-07-25 14:05:52 -0700527
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100528 void Run(art::Thread* self) override REQUIRES(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700529 art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current());
Alex Light318afe62018-03-22 16:50:10 -0700530 art::ScopedAssertNoThreadSuspension sants("CommonLocalVariableClosure::Run");
Alex Lightbebd7bd2017-07-25 14:05:52 -0700531 std::unique_ptr<art::Context> context(art::Context::Create());
532 FindFrameAtDepthVisitor visitor(self, context.get(), depth_);
533 visitor.WalkStack();
534 if (!visitor.FoundFrame()) {
535 // Must have been a bad depth.
536 result_ = ERR(NO_MORE_FRAMES);
537 return;
538 }
539 art::ArtMethod* method = visitor.GetMethod();
Alex Light3dea2122017-10-11 15:56:48 +0000540 // Native and 'art' proxy methods don't have registers.
541 if (method->IsNative() || method->IsProxyMethod()) {
542 // TODO It might be useful to fake up support for get at least on proxy frames.
Alex Lightbebd7bd2017-07-25 14:05:52 -0700543 result_ = ERR(OPAQUE_FRAME);
544 return;
David Sehr0225f8e2018-01-31 08:52:24 +0000545 } else if (method->DexInstructionData().RegistersSize() <= slot_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700546 result_ = ERR(INVALID_SLOT);
547 return;
548 }
Alex Light0a5ec3d2017-07-25 16:50:26 -0700549 bool needs_instrument = !visitor.IsShadowFrame();
Alex Lightbebd7bd2017-07-25 14:05:52 -0700550 uint32_t pc = visitor.GetDexPc(/*abort_on_failure*/ false);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700551 if (pc == art::dex::kDexNoIndex) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700552 // Cannot figure out current PC.
553 result_ = ERR(OPAQUE_FRAME);
554 return;
555 }
556 std::string descriptor;
557 art::Primitive::Type slot_type = art::Primitive::kPrimVoid;
558 jvmtiError err = GetSlotType(method, pc, &descriptor, &slot_type);
559 if (err != OK) {
560 result_ = err;
561 return;
562 }
563
564 err = GetTypeError(method, slot_type, descriptor);
565 if (err != OK) {
566 result_ = err;
567 return;
568 }
569 result_ = Execute(method, visitor);
Alex Light0a5ec3d2017-07-25 16:50:26 -0700570 if (needs_instrument) {
571 art::Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(self);
572 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700573 }
574
Alex Light318afe62018-03-22 16:50:10 -0700575 virtual jvmtiError GetResult() {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700576 return result_;
577 }
578
579 protected:
580 virtual jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
Alex Light318afe62018-03-22 16:50:10 -0700581 REQUIRES_SHARED(art::Locks::mutator_lock_) = 0;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700582 virtual jvmtiError GetTypeError(art::ArtMethod* method,
583 art::Primitive::Type type,
584 const std::string& descriptor)
Alex Light318afe62018-03-22 16:50:10 -0700585 REQUIRES_SHARED(art::Locks::mutator_lock_) = 0;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700586
587 jvmtiError GetSlotType(art::ArtMethod* method,
588 uint32_t dex_pc,
589 /*out*/std::string* descriptor,
590 /*out*/art::Primitive::Type* type)
591 REQUIRES(art::Locks::mutator_lock_) {
592 const art::DexFile* dex_file = method->GetDexFile();
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800593 if (dex_file == nullptr) {
594 return ERR(OPAQUE_FRAME);
595 }
David Sehr0225f8e2018-01-31 08:52:24 +0000596 art::CodeItemDebugInfoAccessor accessor(method->DexInstructionDebugInfo());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800597 if (!accessor.HasCodeItem()) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700598 return ERR(OPAQUE_FRAME);
599 }
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700600 bool found = false;
601 *type = art::Primitive::kPrimVoid;
602 descriptor->clear();
603 auto visitor = [&](const art::DexFile::LocalInfo& entry) {
604 if (!found &&
605 entry.start_address_ <= dex_pc &&
606 entry.end_address_ > dex_pc &&
607 entry.reg_ == slot_) {
608 found = true;
609 *type = art::Primitive::GetType(entry.descriptor_[0]);
610 *descriptor = entry.descriptor_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700611 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700612 };
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700613 if (!accessor.DecodeDebugLocalInfo(method->IsStatic(), method->GetDexMethodIndex(), visitor) ||
614 !found) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700615 // Something went wrong with decoding the debug information. It might as well not be there.
616 return ERR(INVALID_SLOT);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700617 }
Mathieu Chartiere5afbf32018-09-12 17:51:54 -0700618 return OK;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700619 }
620
621 jvmtiError result_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700622 jint depth_;
623 jint slot_;
624};
625
626class GetLocalVariableClosure : public CommonLocalVariableClosure {
627 public:
Alex Light318afe62018-03-22 16:50:10 -0700628 GetLocalVariableClosure(jint depth,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700629 jint slot,
630 art::Primitive::Type type,
631 jvalue* val)
Alex Light318afe62018-03-22 16:50:10 -0700632 : CommonLocalVariableClosure(depth, slot),
633 type_(type),
634 val_(val),
635 obj_val_(nullptr) {}
636
Andreas Gampefa6a1b02018-09-07 08:11:55 -0700637 jvmtiError GetResult() override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Light318afe62018-03-22 16:50:10 -0700638 if (result_ == OK && type_ == art::Primitive::kPrimNot) {
639 val_->l = obj_val_.IsNull()
640 ? nullptr
641 : art::Thread::Current()->GetJniEnv()->AddLocalReference<jobject>(obj_val_.Read());
642 }
643 return CommonLocalVariableClosure::GetResult();
644 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700645
646 protected:
647 jvmtiError GetTypeError(art::ArtMethod* method ATTRIBUTE_UNUSED,
648 art::Primitive::Type slot_type,
649 const std::string& descriptor ATTRIBUTE_UNUSED)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100650 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700651 switch (slot_type) {
652 case art::Primitive::kPrimByte:
653 case art::Primitive::kPrimChar:
654 case art::Primitive::kPrimInt:
655 case art::Primitive::kPrimShort:
656 case art::Primitive::kPrimBoolean:
657 return type_ == art::Primitive::kPrimInt ? OK : ERR(TYPE_MISMATCH);
658 case art::Primitive::kPrimLong:
659 case art::Primitive::kPrimFloat:
660 case art::Primitive::kPrimDouble:
661 case art::Primitive::kPrimNot:
662 return type_ == slot_type ? OK : ERR(TYPE_MISMATCH);
663 case art::Primitive::kPrimVoid:
664 LOG(FATAL) << "Unexpected primitive type " << slot_type;
665 UNREACHABLE();
666 }
667 }
668
669 jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100670 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700671 switch (type_) {
672 case art::Primitive::kPrimNot: {
673 uint32_t ptr_val;
674 if (!visitor.GetVReg(method,
675 static_cast<uint16_t>(slot_),
676 art::kReferenceVReg,
677 &ptr_val)) {
678 return ERR(OPAQUE_FRAME);
679 }
Alex Light318afe62018-03-22 16:50:10 -0700680 obj_val_ = art::GcRoot<art::mirror::Object>(
681 reinterpret_cast<art::mirror::Object*>(ptr_val));
Alex Lightbebd7bd2017-07-25 14:05:52 -0700682 break;
683 }
684 case art::Primitive::kPrimInt:
685 case art::Primitive::kPrimFloat: {
686 if (!visitor.GetVReg(method,
687 static_cast<uint16_t>(slot_),
688 type_ == art::Primitive::kPrimFloat ? art::kFloatVReg : art::kIntVReg,
689 reinterpret_cast<uint32_t*>(&val_->i))) {
690 return ERR(OPAQUE_FRAME);
691 }
692 break;
693 }
694 case art::Primitive::kPrimDouble:
695 case art::Primitive::kPrimLong: {
696 auto lo_type = type_ == art::Primitive::kPrimLong ? art::kLongLoVReg : art::kDoubleLoVReg;
697 auto high_type = type_ == art::Primitive::kPrimLong ? art::kLongHiVReg : art::kDoubleHiVReg;
698 if (!visitor.GetVRegPair(method,
699 static_cast<uint16_t>(slot_),
700 lo_type,
701 high_type,
702 reinterpret_cast<uint64_t*>(&val_->j))) {
703 return ERR(OPAQUE_FRAME);
704 }
705 break;
706 }
707 default: {
708 LOG(FATAL) << "unexpected register type " << type_;
709 UNREACHABLE();
710 }
711 }
712 return OK;
713 }
714
715 private:
716 art::Primitive::Type type_;
717 jvalue* val_;
Alex Light318afe62018-03-22 16:50:10 -0700718 art::GcRoot<art::mirror::Object> obj_val_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700719};
720
721jvmtiError MethodUtil::GetLocalVariableGeneric(jvmtiEnv* env ATTRIBUTE_UNUSED,
722 jthread thread,
723 jint depth,
724 jint slot,
725 art::Primitive::Type type,
726 jvalue* val) {
727 if (depth < 0) {
728 return ERR(ILLEGAL_ARGUMENT);
729 }
730 art::Thread* self = art::Thread::Current();
731 art::ScopedObjectAccess soa(self);
Alex Lightb1e31a82017-10-04 16:57:36 -0700732 art::Locks::thread_list_lock_->ExclusiveLock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700733 art::Thread* target = nullptr;
734 jvmtiError err = ERR(INTERNAL);
735 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
Alex Lightb1e31a82017-10-04 16:57:36 -0700736 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700737 return err;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700738 }
Alex Light318afe62018-03-22 16:50:10 -0700739 art::ScopedAssertNoThreadSuspension sants("Performing GetLocalVariable");
740 GetLocalVariableClosure c(depth, slot, type, val);
741 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. We
742 // need to avoid suspending as we wait for the checkpoint to occur since we are (potentially)
743 // transfering a GcRoot across threads.
744 if (!target->RequestSynchronousCheckpoint(&c, art::ThreadState::kRunnable)) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700745 return ERR(THREAD_NOT_ALIVE);
746 } else {
747 return c.GetResult();
748 }
749}
750
751class SetLocalVariableClosure : public CommonLocalVariableClosure {
752 public:
753 SetLocalVariableClosure(art::Thread* caller,
754 jint depth,
755 jint slot,
756 art::Primitive::Type type,
757 jvalue val)
Alex Light318afe62018-03-22 16:50:10 -0700758 : CommonLocalVariableClosure(depth, slot), caller_(caller), type_(type), val_(val) {}
Alex Lightbebd7bd2017-07-25 14:05:52 -0700759
760 protected:
761 jvmtiError GetTypeError(art::ArtMethod* method,
762 art::Primitive::Type slot_type,
763 const std::string& descriptor)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100764 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700765 switch (slot_type) {
766 case art::Primitive::kPrimNot: {
767 if (type_ != art::Primitive::kPrimNot) {
768 return ERR(TYPE_MISMATCH);
769 } else if (val_.l == nullptr) {
770 return OK;
771 } else {
772 art::ClassLinker* cl = art::Runtime::Current()->GetClassLinker();
773 art::ObjPtr<art::mirror::Class> set_class =
774 caller_->DecodeJObject(val_.l)->GetClass();
775 art::ObjPtr<art::mirror::ClassLoader> loader =
776 method->GetDeclaringClass()->GetClassLoader();
777 art::ObjPtr<art::mirror::Class> slot_class =
778 cl->LookupClass(caller_, descriptor.c_str(), loader);
779 DCHECK(!slot_class.IsNull());
780 return slot_class->IsAssignableFrom(set_class) ? OK : ERR(TYPE_MISMATCH);
781 }
782 }
783 case art::Primitive::kPrimByte:
784 case art::Primitive::kPrimChar:
785 case art::Primitive::kPrimInt:
786 case art::Primitive::kPrimShort:
787 case art::Primitive::kPrimBoolean:
788 return type_ == art::Primitive::kPrimInt ? OK : ERR(TYPE_MISMATCH);
789 case art::Primitive::kPrimLong:
790 case art::Primitive::kPrimFloat:
791 case art::Primitive::kPrimDouble:
792 return type_ == slot_type ? OK : ERR(TYPE_MISMATCH);
793 case art::Primitive::kPrimVoid:
794 LOG(FATAL) << "Unexpected primitive type " << slot_type;
795 UNREACHABLE();
796 }
797 }
798
799 jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100800 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700801 switch (type_) {
802 case art::Primitive::kPrimNot: {
803 uint32_t ptr_val;
804 art::ObjPtr<art::mirror::Object> obj(caller_->DecodeJObject(val_.l));
805 ptr_val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(obj.Ptr()));
806 if (!visitor.SetVReg(method,
807 static_cast<uint16_t>(slot_),
808 ptr_val,
809 art::kReferenceVReg)) {
810 return ERR(OPAQUE_FRAME);
811 }
812 break;
813 }
814 case art::Primitive::kPrimInt:
815 case art::Primitive::kPrimFloat: {
816 if (!visitor.SetVReg(method,
817 static_cast<uint16_t>(slot_),
818 static_cast<uint32_t>(val_.i),
819 type_ == art::Primitive::kPrimFloat ? art::kFloatVReg
820 : art::kIntVReg)) {
821 return ERR(OPAQUE_FRAME);
822 }
823 break;
824 }
825 case art::Primitive::kPrimDouble:
826 case art::Primitive::kPrimLong: {
827 auto lo_type = type_ == art::Primitive::kPrimLong ? art::kLongLoVReg : art::kDoubleLoVReg;
828 auto high_type = type_ == art::Primitive::kPrimLong ? art::kLongHiVReg : art::kDoubleHiVReg;
829 if (!visitor.SetVRegPair(method,
830 static_cast<uint16_t>(slot_),
831 static_cast<uint64_t>(val_.j),
832 lo_type,
833 high_type)) {
834 return ERR(OPAQUE_FRAME);
835 }
836 break;
837 }
838 default: {
839 LOG(FATAL) << "unexpected register type " << type_;
840 UNREACHABLE();
841 }
842 }
843 return OK;
844 }
845
846 private:
Alex Light318afe62018-03-22 16:50:10 -0700847 art::Thread* caller_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700848 art::Primitive::Type type_;
849 jvalue val_;
850};
851
852jvmtiError MethodUtil::SetLocalVariableGeneric(jvmtiEnv* env ATTRIBUTE_UNUSED,
853 jthread thread,
854 jint depth,
855 jint slot,
856 art::Primitive::Type type,
857 jvalue val) {
858 if (depth < 0) {
859 return ERR(ILLEGAL_ARGUMENT);
860 }
Alex Lightf2858632018-04-02 11:28:50 -0700861 // Make sure that we know not to do any OSR anymore.
862 // TODO We should really keep track of this at the Frame granularity.
863 DeoptManager::Get()->SetLocalsUpdated();
Alex Lightbebd7bd2017-07-25 14:05:52 -0700864 art::Thread* self = art::Thread::Current();
865 art::ScopedObjectAccess soa(self);
Alex Lightb1e31a82017-10-04 16:57:36 -0700866 art::Locks::thread_list_lock_->ExclusiveLock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700867 art::Thread* target = nullptr;
868 jvmtiError err = ERR(INTERNAL);
869 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
Alex Lightb1e31a82017-10-04 16:57:36 -0700870 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700871 return err;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700872 }
873 SetLocalVariableClosure c(self, depth, slot, type, val);
Alex Lightb1e31a82017-10-04 16:57:36 -0700874 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution.
Alex Light318afe62018-03-22 16:50:10 -0700875 if (!target->RequestSynchronousCheckpoint(&c)) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700876 return ERR(THREAD_NOT_ALIVE);
877 } else {
878 return c.GetResult();
879 }
880}
881
882class GetLocalInstanceClosure : public art::Closure {
883 public:
Alex Light318afe62018-03-22 16:50:10 -0700884 explicit GetLocalInstanceClosure(jint depth)
Alex Lightbebd7bd2017-07-25 14:05:52 -0700885 : result_(ERR(INTERNAL)),
Alex Lightbebd7bd2017-07-25 14:05:52 -0700886 depth_(depth),
Alex Light318afe62018-03-22 16:50:10 -0700887 val_(nullptr) {}
Alex Lightbebd7bd2017-07-25 14:05:52 -0700888
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100889 void Run(art::Thread* self) override REQUIRES(art::Locks::mutator_lock_) {
Alex Light318afe62018-03-22 16:50:10 -0700890 art::ScopedAssertNoThreadSuspension sants("GetLocalInstanceClosure::Run");
Alex Lightbebd7bd2017-07-25 14:05:52 -0700891 art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current());
892 std::unique_ptr<art::Context> context(art::Context::Create());
893 FindFrameAtDepthVisitor visitor(self, context.get(), depth_);
894 visitor.WalkStack();
895 if (!visitor.FoundFrame()) {
896 // Must have been a bad depth.
897 result_ = ERR(NO_MORE_FRAMES);
898 return;
899 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700900 result_ = OK;
Alex Light318afe62018-03-22 16:50:10 -0700901 val_ = art::GcRoot<art::mirror::Object>(visitor.GetThisObject());
Alex Lightbebd7bd2017-07-25 14:05:52 -0700902 }
903
Alex Light318afe62018-03-22 16:50:10 -0700904 jvmtiError GetResult(jobject* data_out) REQUIRES_SHARED(art::Locks::mutator_lock_) {
905 if (result_ == OK) {
906 *data_out = val_.IsNull()
907 ? nullptr
908 : art::Thread::Current()->GetJniEnv()->AddLocalReference<jobject>(val_.Read());
909 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700910 return result_;
911 }
912
913 private:
914 jvmtiError result_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700915 jint depth_;
Alex Light318afe62018-03-22 16:50:10 -0700916 art::GcRoot<art::mirror::Object> val_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700917};
918
919jvmtiError MethodUtil::GetLocalInstance(jvmtiEnv* env ATTRIBUTE_UNUSED,
920 jthread thread,
921 jint depth,
922 jobject* data) {
923 if (depth < 0) {
924 return ERR(ILLEGAL_ARGUMENT);
925 }
926 art::Thread* self = art::Thread::Current();
927 art::ScopedObjectAccess soa(self);
Alex Lightb1e31a82017-10-04 16:57:36 -0700928 art::Locks::thread_list_lock_->ExclusiveLock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700929 art::Thread* target = nullptr;
930 jvmtiError err = ERR(INTERNAL);
931 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
Alex Lightb1e31a82017-10-04 16:57:36 -0700932 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700933 return err;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700934 }
Alex Light318afe62018-03-22 16:50:10 -0700935 art::ScopedAssertNoThreadSuspension sants("Performing GetLocalInstance");
936 GetLocalInstanceClosure c(depth);
937 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. We
938 // need to avoid suspending as we wait for the checkpoint to occur since we are (potentially)
939 // transfering a GcRoot across threads.
940 if (!target->RequestSynchronousCheckpoint(&c, art::ThreadState::kRunnable)) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700941 return ERR(THREAD_NOT_ALIVE);
942 } else {
Alex Light318afe62018-03-22 16:50:10 -0700943 return c.GetResult(data);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700944 }
945}
946
947#define FOR_JVMTI_JVALUE_TYPES(fn) \
948 fn(jint, art::Primitive::kPrimInt, i) \
949 fn(jlong, art::Primitive::kPrimLong, j) \
950 fn(jfloat, art::Primitive::kPrimFloat, f) \
951 fn(jdouble, art::Primitive::kPrimDouble, d) \
952 fn(jobject, art::Primitive::kPrimNot, l)
953
954namespace impl {
955
956template<typename T> void WriteJvalue(T, jvalue*);
957template<typename T> void ReadJvalue(jvalue, T*);
958template<typename T> art::Primitive::Type GetJNIType();
959
960#define JNI_TYPE_CHAR(type, prim, id) \
961template<> art::Primitive::Type GetJNIType<type>() { \
962 return prim; \
963}
964
965FOR_JVMTI_JVALUE_TYPES(JNI_TYPE_CHAR);
966
967#undef JNI_TYPE_CHAR
968
Andreas Gampe49fc60e2017-08-24 13:19:59 -0700969#define RW_JVALUE(srctype, prim, id) \
970 template<> void ReadJvalue<srctype>(jvalue in, std::add_pointer<srctype>::type out) { \
Alex Lightbebd7bd2017-07-25 14:05:52 -0700971 *out = in.id; \
972 } \
Andreas Gampe49fc60e2017-08-24 13:19:59 -0700973 template<> void WriteJvalue<srctype>(srctype in, jvalue* out) { \
Alex Lightbebd7bd2017-07-25 14:05:52 -0700974 out->id = in; \
975 }
976
977FOR_JVMTI_JVALUE_TYPES(RW_JVALUE);
978
979#undef RW_JVALUE
980
981} // namespace impl
982
983template<typename T>
984jvmtiError MethodUtil::SetLocalVariable(jvmtiEnv* env,
985 jthread thread,
986 jint depth,
987 jint slot,
988 T data) {
989 jvalue v = {.j = 0};
990 art::Primitive::Type type = impl::GetJNIType<T>();
991 impl::WriteJvalue(data, &v);
992 return SetLocalVariableGeneric(env, thread, depth, slot, type, v);
993}
994
995template<typename T>
996jvmtiError MethodUtil::GetLocalVariable(jvmtiEnv* env,
997 jthread thread,
998 jint depth,
999 jint slot,
1000 T* data) {
1001 if (data == nullptr) {
1002 return ERR(NULL_POINTER);
1003 }
1004 jvalue v = {.j = 0};
1005 art::Primitive::Type type = impl::GetJNIType<T>();
1006 jvmtiError err = GetLocalVariableGeneric(env, thread, depth, slot, type, &v);
1007 if (err != OK) {
1008 return err;
1009 } else {
1010 impl::ReadJvalue(v, data);
1011 return OK;
1012 }
1013}
1014
Andreas Gampe49fc60e2017-08-24 13:19:59 -07001015#define GET_SET_LV(srctype, prim, id) \
1016 template jvmtiError MethodUtil::GetLocalVariable<srctype>(jvmtiEnv*, \
1017 jthread, \
1018 jint, \
1019 jint, \
1020 std::add_pointer<srctype>::type); \
1021 template jvmtiError MethodUtil::SetLocalVariable<srctype>(jvmtiEnv*, \
1022 jthread, \
1023 jint, \
1024 jint, \
1025 srctype);
Alex Lightbebd7bd2017-07-25 14:05:52 -07001026
1027FOR_JVMTI_JVALUE_TYPES(GET_SET_LV);
1028
1029#undef GET_SET_LV
1030
1031#undef FOR_JVMTI_JVALUE_TYPES
1032
Andreas Gampe3c252f02016-10-27 18:25:17 -07001033} // namespace openjdkjvmti