blob: 1588df40866505b7503f3f0b84331c62ee06c767 [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
209 struct LocalVariableContext {
210 explicit LocalVariableContext(jvmtiEnv* jenv) : env_(jenv), variables_(), err_(OK) {}
211
212 static void Callback(void* raw_ctx, const art::DexFile::LocalInfo& entry) {
213 reinterpret_cast<LocalVariableContext*>(raw_ctx)->Insert(entry);
214 }
215
216 void Insert(const art::DexFile::LocalInfo& entry) {
217 if (err_ != OK) {
218 return;
219 }
220 JvmtiUniquePtr<char[]> name_str = CopyString(env_, entry.name_, &err_);
221 if (err_ != OK) {
222 return;
223 }
224 JvmtiUniquePtr<char[]> sig_str = CopyString(env_, entry.descriptor_, &err_);
225 if (err_ != OK) {
226 return;
227 }
228 JvmtiUniquePtr<char[]> generic_sig_str = CopyString(env_, entry.signature_, &err_);
229 if (err_ != OK) {
230 return;
231 }
232 variables_.push_back({
233 .start_location = static_cast<jlocation>(entry.start_address_),
234 .length = static_cast<jint>(entry.end_address_ - entry.start_address_),
235 .name = name_str.release(),
236 .signature = sig_str.release(),
237 .generic_signature = generic_sig_str.release(),
238 .slot = entry.reg_,
239 });
240 }
241
242 jvmtiError Release(jint* out_entry_count_ptr, jvmtiLocalVariableEntry** out_table_ptr) {
243 jlong table_size = sizeof(jvmtiLocalVariableEntry) * variables_.size();
244 if (err_ != OK ||
245 (err_ = env_->Allocate(table_size,
246 reinterpret_cast<unsigned char**>(out_table_ptr))) != OK) {
247 Cleanup();
248 return err_;
249 } else {
250 *out_entry_count_ptr = variables_.size();
251 memcpy(*out_table_ptr, variables_.data(), table_size);
252 return OK;
253 }
254 }
255
256 void Cleanup() {
257 for (jvmtiLocalVariableEntry& e : variables_) {
258 env_->Deallocate(reinterpret_cast<unsigned char*>(e.name));
259 env_->Deallocate(reinterpret_cast<unsigned char*>(e.signature));
260 env_->Deallocate(reinterpret_cast<unsigned char*>(e.generic_signature));
261 }
262 }
263
264 jvmtiEnv* env_;
265 std::vector<jvmtiLocalVariableEntry> variables_;
266 jvmtiError err_;
267 };
268
269 LocalVariableContext context(env);
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800270 if (!accessor.DecodeDebugLocalInfo(art_method->IsStatic(),
271 art_method->GetDexMethodIndex(),
272 LocalVariableContext::Callback,
273 &context)) {
Alex Lightce68cc62017-07-26 10:30:38 -0700274 // Something went wrong with decoding the debug information. It might as well not be there.
275 return ERR(ABSENT_INFORMATION);
276 } else {
277 return context.Release(entry_count_ptr, table_ptr);
278 }
279}
280
Andreas Gampef71832e2017-01-09 11:38:04 -0800281jvmtiError MethodUtil::GetMaxLocals(jvmtiEnv* env ATTRIBUTE_UNUSED,
282 jmethodID method,
283 jint* max_ptr) {
284 if (method == nullptr) {
285 return ERR(INVALID_METHODID);
286 }
287 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
288
289 if (art_method->IsNative()) {
290 return ERR(NATIVE_METHOD);
291 }
292
293 if (max_ptr == nullptr) {
294 return ERR(NULL_POINTER);
295 }
296
297 art::ScopedObjectAccess soa(art::Thread::Current());
298 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
299 // This isn't specified as an error case, so return 0.
300 *max_ptr = 0;
301 return ERR(NONE);
302 }
303
304 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
David Sehr0225f8e2018-01-31 08:52:24 +0000305 *max_ptr = art_method->DexInstructionData().RegistersSize();
Andreas Gampef71832e2017-01-09 11:38:04 -0800306
307 return ERR(NONE);
308}
309
Andreas Gampe3c252f02016-10-27 18:25:17 -0700310jvmtiError MethodUtil::GetMethodName(jvmtiEnv* env,
311 jmethodID method,
312 char** name_ptr,
313 char** signature_ptr,
314 char** generic_ptr) {
315 art::ScopedObjectAccess soa(art::Thread::Current());
Andreas Gampe13b27842016-11-07 16:48:23 -0800316 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe3c252f02016-10-27 18:25:17 -0700317 art_method = art_method->GetInterfaceMethodIfProxy(art::kRuntimePointerSize);
318
Andreas Gampe54711412017-02-21 12:41:43 -0800319 JvmtiUniquePtr<char[]> name_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700320 if (name_ptr != nullptr) {
321 const char* method_name = art_method->GetName();
322 if (method_name == nullptr) {
323 method_name = "<error>";
324 }
Andreas Gampe54711412017-02-21 12:41:43 -0800325 jvmtiError ret;
326 name_copy = CopyString(env, method_name, &ret);
327 if (name_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700328 return ret;
329 }
Andreas Gampe54711412017-02-21 12:41:43 -0800330 *name_ptr = name_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700331 }
332
Andreas Gampe54711412017-02-21 12:41:43 -0800333 JvmtiUniquePtr<char[]> signature_copy;
Andreas Gampe3c252f02016-10-27 18:25:17 -0700334 if (signature_ptr != nullptr) {
335 const art::Signature sig = art_method->GetSignature();
336 std::string str = sig.ToString();
Andreas Gampe54711412017-02-21 12:41:43 -0800337 jvmtiError ret;
338 signature_copy = CopyString(env, str.c_str(), &ret);
339 if (signature_copy == nullptr) {
Andreas Gampe3c252f02016-10-27 18:25:17 -0700340 return ret;
341 }
Andreas Gampe54711412017-02-21 12:41:43 -0800342 *signature_ptr = signature_copy.get();
Andreas Gampe3c252f02016-10-27 18:25:17 -0700343 }
344
Andreas Gampe862bdd82016-11-18 13:31:13 -0800345 if (generic_ptr != nullptr) {
346 *generic_ptr = nullptr;
Andreas Gampe27dfa052017-02-16 15:04:36 -0800347 if (!art_method->GetDeclaringClass()->IsProxyClass()) {
Vladimir Markoacb906d2018-05-30 10:23:49 +0100348 art::ObjPtr<art::mirror::ObjectArray<art::mirror::String>> str_array =
Andreas Gampe27dfa052017-02-16 15:04:36 -0800349 art::annotations::GetSignatureAnnotationForMethod(art_method);
350 if (str_array != nullptr) {
351 std::ostringstream oss;
352 for (int32_t i = 0; i != str_array->GetLength(); ++i) {
353 oss << str_array->Get(i)->ToModifiedUtf8();
354 }
355 std::string output_string = oss.str();
Andreas Gampe54711412017-02-21 12:41:43 -0800356 jvmtiError ret;
357 JvmtiUniquePtr<char[]> generic_copy = CopyString(env, output_string.c_str(), &ret);
358 if (generic_copy == nullptr) {
Andreas Gampe27dfa052017-02-16 15:04:36 -0800359 return ret;
360 }
Andreas Gampe54711412017-02-21 12:41:43 -0800361 *generic_ptr = generic_copy.release();
Andreas Gampe27dfa052017-02-16 15:04:36 -0800362 } else if (soa.Self()->IsExceptionPending()) {
363 // TODO: Should we report an error here?
364 soa.Self()->ClearException();
365 }
366 }
Andreas Gampe862bdd82016-11-18 13:31:13 -0800367 }
Andreas Gampe3c252f02016-10-27 18:25:17 -0700368
369 // Everything is fine, release the buffers.
370 name_copy.release();
371 signature_copy.release();
372
373 return ERR(NONE);
374}
375
Andreas Gampe368a2082016-10-28 17:33:13 -0700376jvmtiError MethodUtil::GetMethodDeclaringClass(jvmtiEnv* env ATTRIBUTE_UNUSED,
377 jmethodID method,
378 jclass* declaring_class_ptr) {
379 if (declaring_class_ptr == nullptr) {
380 return ERR(NULL_POINTER);
381 }
382
Andreas Gampe13b27842016-11-07 16:48:23 -0800383 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe368a2082016-10-28 17:33:13 -0700384 // Note: No GetInterfaceMethodIfProxy, we want to actual class.
385
Andreas Gampe13b27842016-11-07 16:48:23 -0800386 art::ScopedObjectAccess soa(art::Thread::Current());
Vladimir Markod93e3742018-07-18 10:58:13 +0100387 art::ObjPtr<art::mirror::Class> klass = art_method->GetDeclaringClass();
Andreas Gampe368a2082016-10-28 17:33:13 -0700388 *declaring_class_ptr = soa.AddLocalReference<jclass>(klass);
389
390 return ERR(NONE);
391}
392
Andreas Gampef71832e2017-01-09 11:38:04 -0800393jvmtiError MethodUtil::GetMethodLocation(jvmtiEnv* env ATTRIBUTE_UNUSED,
394 jmethodID method,
395 jlocation* start_location_ptr,
396 jlocation* end_location_ptr) {
397 if (method == nullptr) {
398 return ERR(INVALID_METHODID);
399 }
400 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
401
402 if (art_method->IsNative()) {
403 return ERR(NATIVE_METHOD);
404 }
405
406 if (start_location_ptr == nullptr || end_location_ptr == nullptr) {
407 return ERR(NULL_POINTER);
408 }
409
410 art::ScopedObjectAccess soa(art::Thread::Current());
411 if (art_method->IsProxyMethod() || art_method->IsAbstract()) {
Andreas Gampee1f79b62017-04-12 21:11:28 -0700412 // This isn't specified as an error case, so return -1/-1 as the RI does.
413 *start_location_ptr = -1;
414 *end_location_ptr = -1;
Andreas Gampef71832e2017-01-09 11:38:04 -0800415 return ERR(NONE);
416 }
417
418 DCHECK_NE(art_method->GetCodeItemOffset(), 0u);
419 *start_location_ptr = 0;
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800420 *end_location_ptr = art_method->DexInstructions().InsnsSizeInCodeUnits() - 1;
Andreas Gampef71832e2017-01-09 11:38:04 -0800421
422 return ERR(NONE);
423}
424
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700425jvmtiError MethodUtil::GetMethodModifiers(jvmtiEnv* env ATTRIBUTE_UNUSED,
426 jmethodID method,
427 jint* modifiers_ptr) {
428 if (modifiers_ptr == nullptr) {
429 return ERR(NULL_POINTER);
430 }
431
Andreas Gampe13b27842016-11-07 16:48:23 -0800432 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700433 uint32_t modifiers = art_method->GetAccessFlags();
434
435 // Note: Keep this code in sync with Executable.fixMethodFlags.
436 if ((modifiers & art::kAccAbstract) != 0) {
437 modifiers &= ~art::kAccNative;
438 }
439 modifiers &= ~art::kAccSynchronized;
440 if ((modifiers & art::kAccDeclaredSynchronized) != 0) {
441 modifiers |= art::kAccSynchronized;
442 }
443 modifiers &= art::kAccJavaFlagsMask;
444
445 *modifiers_ptr = modifiers;
446 return ERR(NONE);
447}
448
Andreas Gampeda3e5612016-12-13 19:00:53 -0800449using LineNumberContext = std::vector<jvmtiLineNumberEntry>;
450
451static bool CollectLineNumbers(void* void_context, const art::DexFile::PositionInfo& entry) {
452 LineNumberContext* context = reinterpret_cast<LineNumberContext*>(void_context);
453 jvmtiLineNumberEntry jvmti_entry = { static_cast<jlocation>(entry.address_),
454 static_cast<jint>(entry.line_) };
455 context->push_back(jvmti_entry);
456 return false; // Collect all, no early exit.
457}
458
459jvmtiError MethodUtil::GetLineNumberTable(jvmtiEnv* env,
460 jmethodID method,
461 jint* entry_count_ptr,
462 jvmtiLineNumberEntry** table_ptr) {
463 if (method == nullptr) {
464 return ERR(NULL_POINTER);
465 }
466 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
467 DCHECK(!art_method->IsRuntimeMethod());
468
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800469 art::CodeItemDebugInfoAccessor accessor;
Andreas Gampeda3e5612016-12-13 19:00:53 -0800470 const art::DexFile* dex_file;
471 {
472 art::ScopedObjectAccess soa(art::Thread::Current());
473
474 if (art_method->IsProxyMethod()) {
475 return ERR(ABSENT_INFORMATION);
476 }
477 if (art_method->IsNative()) {
478 return ERR(NATIVE_METHOD);
479 }
480 if (entry_count_ptr == nullptr || table_ptr == nullptr) {
481 return ERR(NULL_POINTER);
482 }
483
David Sehr0225f8e2018-01-31 08:52:24 +0000484 accessor = art::CodeItemDebugInfoAccessor(art_method->DexInstructionDebugInfo());
Andreas Gampeda3e5612016-12-13 19:00:53 -0800485 dex_file = art_method->GetDexFile();
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800486 DCHECK(accessor.HasCodeItem()) << art_method->PrettyMethod() << " " << dex_file->GetLocation();
Andreas Gampeda3e5612016-12-13 19:00:53 -0800487 }
488
489 LineNumberContext context;
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +0000490 bool success = dex_file->DecodeDebugPositionInfo(
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800491 accessor.DebugInfoOffset(), CollectLineNumbers, &context);
Andreas Gampeda3e5612016-12-13 19:00:53 -0800492 if (!success) {
493 return ERR(ABSENT_INFORMATION);
494 }
495
496 unsigned char* data;
497 jlong mem_size = context.size() * sizeof(jvmtiLineNumberEntry);
498 jvmtiError alloc_error = env->Allocate(mem_size, &data);
499 if (alloc_error != ERR(NONE)) {
500 return alloc_error;
501 }
502 *table_ptr = reinterpret_cast<jvmtiLineNumberEntry*>(data);
503 memcpy(*table_ptr, context.data(), mem_size);
504 *entry_count_ptr = static_cast<jint>(context.size());
505
506 return ERR(NONE);
507}
508
Andreas Gampefdeef522017-01-09 14:40:25 -0800509template <typename T>
510static jvmtiError IsMethodT(jvmtiEnv* env ATTRIBUTE_UNUSED,
511 jmethodID method,
512 T test,
513 jboolean* is_t_ptr) {
514 if (method == nullptr) {
515 return ERR(INVALID_METHODID);
516 }
517 if (is_t_ptr == nullptr) {
518 return ERR(NULL_POINTER);
519 }
520
521 art::ArtMethod* art_method = art::jni::DecodeArtMethod(method);
522 *is_t_ptr = test(art_method) ? JNI_TRUE : JNI_FALSE;
523
524 return ERR(NONE);
525}
526
527jvmtiError MethodUtil::IsMethodNative(jvmtiEnv* env, jmethodID m, jboolean* is_native_ptr) {
528 auto test = [](art::ArtMethod* method) {
529 return method->IsNative();
530 };
531 return IsMethodT(env, m, test, is_native_ptr);
532}
533
534jvmtiError MethodUtil::IsMethodObsolete(jvmtiEnv* env, jmethodID m, jboolean* is_obsolete_ptr) {
535 auto test = [](art::ArtMethod* method) {
536 return method->IsObsolete();
537 };
538 return IsMethodT(env, m, test, is_obsolete_ptr);
539}
540
541jvmtiError MethodUtil::IsMethodSynthetic(jvmtiEnv* env, jmethodID m, jboolean* is_synthetic_ptr) {
542 auto test = [](art::ArtMethod* method) {
543 return method->IsSynthetic();
544 };
545 return IsMethodT(env, m, test, is_synthetic_ptr);
546}
547
Alex Lightbebd7bd2017-07-25 14:05:52 -0700548class CommonLocalVariableClosure : public art::Closure {
549 public:
Alex Light318afe62018-03-22 16:50:10 -0700550 CommonLocalVariableClosure(jint depth, jint slot)
551 : result_(ERR(INTERNAL)), depth_(depth), slot_(slot) {}
Alex Lightbebd7bd2017-07-25 14:05:52 -0700552
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100553 void Run(art::Thread* self) override REQUIRES(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700554 art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current());
Alex Light318afe62018-03-22 16:50:10 -0700555 art::ScopedAssertNoThreadSuspension sants("CommonLocalVariableClosure::Run");
Alex Lightbebd7bd2017-07-25 14:05:52 -0700556 std::unique_ptr<art::Context> context(art::Context::Create());
557 FindFrameAtDepthVisitor visitor(self, context.get(), depth_);
558 visitor.WalkStack();
559 if (!visitor.FoundFrame()) {
560 // Must have been a bad depth.
561 result_ = ERR(NO_MORE_FRAMES);
562 return;
563 }
564 art::ArtMethod* method = visitor.GetMethod();
Alex Light3dea2122017-10-11 15:56:48 +0000565 // Native and 'art' proxy methods don't have registers.
566 if (method->IsNative() || method->IsProxyMethod()) {
567 // TODO It might be useful to fake up support for get at least on proxy frames.
Alex Lightbebd7bd2017-07-25 14:05:52 -0700568 result_ = ERR(OPAQUE_FRAME);
569 return;
David Sehr0225f8e2018-01-31 08:52:24 +0000570 } else if (method->DexInstructionData().RegistersSize() <= slot_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700571 result_ = ERR(INVALID_SLOT);
572 return;
573 }
Alex Light0a5ec3d2017-07-25 16:50:26 -0700574 bool needs_instrument = !visitor.IsShadowFrame();
Alex Lightbebd7bd2017-07-25 14:05:52 -0700575 uint32_t pc = visitor.GetDexPc(/*abort_on_failure*/ false);
Andreas Gampee2abbc62017-09-15 11:59:26 -0700576 if (pc == art::dex::kDexNoIndex) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700577 // Cannot figure out current PC.
578 result_ = ERR(OPAQUE_FRAME);
579 return;
580 }
581 std::string descriptor;
582 art::Primitive::Type slot_type = art::Primitive::kPrimVoid;
583 jvmtiError err = GetSlotType(method, pc, &descriptor, &slot_type);
584 if (err != OK) {
585 result_ = err;
586 return;
587 }
588
589 err = GetTypeError(method, slot_type, descriptor);
590 if (err != OK) {
591 result_ = err;
592 return;
593 }
594 result_ = Execute(method, visitor);
Alex Light0a5ec3d2017-07-25 16:50:26 -0700595 if (needs_instrument) {
596 art::Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(self);
597 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700598 }
599
Alex Light318afe62018-03-22 16:50:10 -0700600 virtual jvmtiError GetResult() {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700601 return result_;
602 }
603
604 protected:
605 virtual jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
Alex Light318afe62018-03-22 16:50:10 -0700606 REQUIRES_SHARED(art::Locks::mutator_lock_) = 0;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700607 virtual jvmtiError GetTypeError(art::ArtMethod* method,
608 art::Primitive::Type type,
609 const std::string& descriptor)
Alex Light318afe62018-03-22 16:50:10 -0700610 REQUIRES_SHARED(art::Locks::mutator_lock_) = 0;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700611
612 jvmtiError GetSlotType(art::ArtMethod* method,
613 uint32_t dex_pc,
614 /*out*/std::string* descriptor,
615 /*out*/art::Primitive::Type* type)
616 REQUIRES(art::Locks::mutator_lock_) {
617 const art::DexFile* dex_file = method->GetDexFile();
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800618 if (dex_file == nullptr) {
619 return ERR(OPAQUE_FRAME);
620 }
David Sehr0225f8e2018-01-31 08:52:24 +0000621 art::CodeItemDebugInfoAccessor accessor(method->DexInstructionDebugInfo());
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800622 if (!accessor.HasCodeItem()) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700623 return ERR(OPAQUE_FRAME);
624 }
625
626 struct GetLocalVariableInfoContext {
627 explicit GetLocalVariableInfoContext(jint slot,
628 uint32_t pc,
629 std::string* out_descriptor,
630 art::Primitive::Type* out_type)
631 : found_(false), jslot_(slot), pc_(pc), descriptor_(out_descriptor), type_(out_type) {
632 *descriptor_ = "";
633 *type_ = art::Primitive::kPrimVoid;
634 }
635
636 static void Callback(void* raw_ctx, const art::DexFile::LocalInfo& entry) {
637 reinterpret_cast<GetLocalVariableInfoContext*>(raw_ctx)->Handle(entry);
638 }
639
640 void Handle(const art::DexFile::LocalInfo& entry) {
641 if (found_) {
642 return;
643 } else if (entry.start_address_ <= pc_ &&
644 entry.end_address_ > pc_ &&
645 entry.reg_ == jslot_) {
646 found_ = true;
647 *type_ = art::Primitive::GetType(entry.descriptor_[0]);
648 *descriptor_ = entry.descriptor_;
649 }
650 return;
651 }
652
653 bool found_;
654 jint jslot_;
655 uint32_t pc_;
656 std::string* descriptor_;
657 art::Primitive::Type* type_;
658 };
659
660 GetLocalVariableInfoContext context(slot_, dex_pc, descriptor, type);
Mathieu Chartier31f4c9f2017-12-08 15:46:11 -0800661 if (!dex_file->DecodeDebugLocalInfo(accessor.RegistersSize(),
662 accessor.InsSize(),
663 accessor.InsnsSizeInCodeUnits(),
664 accessor.DebugInfoOffset(),
Alex Lightbebd7bd2017-07-25 14:05:52 -0700665 method->IsStatic(),
666 method->GetDexMethodIndex(),
667 GetLocalVariableInfoContext::Callback,
668 &context) || !context.found_) {
669 // Something went wrong with decoding the debug information. It might as well not be there.
670 return ERR(INVALID_SLOT);
671 } else {
672 return OK;
673 }
674 }
675
676 jvmtiError result_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700677 jint depth_;
678 jint slot_;
679};
680
681class GetLocalVariableClosure : public CommonLocalVariableClosure {
682 public:
Alex Light318afe62018-03-22 16:50:10 -0700683 GetLocalVariableClosure(jint depth,
Alex Lightbebd7bd2017-07-25 14:05:52 -0700684 jint slot,
685 art::Primitive::Type type,
686 jvalue* val)
Alex Light318afe62018-03-22 16:50:10 -0700687 : CommonLocalVariableClosure(depth, slot),
688 type_(type),
689 val_(val),
690 obj_val_(nullptr) {}
691
692 virtual jvmtiError GetResult() REQUIRES_SHARED(art::Locks::mutator_lock_) {
693 if (result_ == OK && type_ == art::Primitive::kPrimNot) {
694 val_->l = obj_val_.IsNull()
695 ? nullptr
696 : art::Thread::Current()->GetJniEnv()->AddLocalReference<jobject>(obj_val_.Read());
697 }
698 return CommonLocalVariableClosure::GetResult();
699 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700700
701 protected:
702 jvmtiError GetTypeError(art::ArtMethod* method ATTRIBUTE_UNUSED,
703 art::Primitive::Type slot_type,
704 const std::string& descriptor ATTRIBUTE_UNUSED)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100705 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700706 switch (slot_type) {
707 case art::Primitive::kPrimByte:
708 case art::Primitive::kPrimChar:
709 case art::Primitive::kPrimInt:
710 case art::Primitive::kPrimShort:
711 case art::Primitive::kPrimBoolean:
712 return type_ == art::Primitive::kPrimInt ? OK : ERR(TYPE_MISMATCH);
713 case art::Primitive::kPrimLong:
714 case art::Primitive::kPrimFloat:
715 case art::Primitive::kPrimDouble:
716 case art::Primitive::kPrimNot:
717 return type_ == slot_type ? OK : ERR(TYPE_MISMATCH);
718 case art::Primitive::kPrimVoid:
719 LOG(FATAL) << "Unexpected primitive type " << slot_type;
720 UNREACHABLE();
721 }
722 }
723
724 jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100725 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700726 switch (type_) {
727 case art::Primitive::kPrimNot: {
728 uint32_t ptr_val;
729 if (!visitor.GetVReg(method,
730 static_cast<uint16_t>(slot_),
731 art::kReferenceVReg,
732 &ptr_val)) {
733 return ERR(OPAQUE_FRAME);
734 }
Alex Light318afe62018-03-22 16:50:10 -0700735 obj_val_ = art::GcRoot<art::mirror::Object>(
736 reinterpret_cast<art::mirror::Object*>(ptr_val));
Alex Lightbebd7bd2017-07-25 14:05:52 -0700737 break;
738 }
739 case art::Primitive::kPrimInt:
740 case art::Primitive::kPrimFloat: {
741 if (!visitor.GetVReg(method,
742 static_cast<uint16_t>(slot_),
743 type_ == art::Primitive::kPrimFloat ? art::kFloatVReg : art::kIntVReg,
744 reinterpret_cast<uint32_t*>(&val_->i))) {
745 return ERR(OPAQUE_FRAME);
746 }
747 break;
748 }
749 case art::Primitive::kPrimDouble:
750 case art::Primitive::kPrimLong: {
751 auto lo_type = type_ == art::Primitive::kPrimLong ? art::kLongLoVReg : art::kDoubleLoVReg;
752 auto high_type = type_ == art::Primitive::kPrimLong ? art::kLongHiVReg : art::kDoubleHiVReg;
753 if (!visitor.GetVRegPair(method,
754 static_cast<uint16_t>(slot_),
755 lo_type,
756 high_type,
757 reinterpret_cast<uint64_t*>(&val_->j))) {
758 return ERR(OPAQUE_FRAME);
759 }
760 break;
761 }
762 default: {
763 LOG(FATAL) << "unexpected register type " << type_;
764 UNREACHABLE();
765 }
766 }
767 return OK;
768 }
769
770 private:
771 art::Primitive::Type type_;
772 jvalue* val_;
Alex Light318afe62018-03-22 16:50:10 -0700773 art::GcRoot<art::mirror::Object> obj_val_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700774};
775
776jvmtiError MethodUtil::GetLocalVariableGeneric(jvmtiEnv* env ATTRIBUTE_UNUSED,
777 jthread thread,
778 jint depth,
779 jint slot,
780 art::Primitive::Type type,
781 jvalue* val) {
782 if (depth < 0) {
783 return ERR(ILLEGAL_ARGUMENT);
784 }
785 art::Thread* self = art::Thread::Current();
786 art::ScopedObjectAccess soa(self);
Alex Lightb1e31a82017-10-04 16:57:36 -0700787 art::Locks::thread_list_lock_->ExclusiveLock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700788 art::Thread* target = nullptr;
789 jvmtiError err = ERR(INTERNAL);
790 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
Alex Lightb1e31a82017-10-04 16:57:36 -0700791 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700792 return err;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700793 }
Alex Light318afe62018-03-22 16:50:10 -0700794 art::ScopedAssertNoThreadSuspension sants("Performing GetLocalVariable");
795 GetLocalVariableClosure c(depth, slot, type, val);
796 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. We
797 // need to avoid suspending as we wait for the checkpoint to occur since we are (potentially)
798 // transfering a GcRoot across threads.
799 if (!target->RequestSynchronousCheckpoint(&c, art::ThreadState::kRunnable)) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700800 return ERR(THREAD_NOT_ALIVE);
801 } else {
802 return c.GetResult();
803 }
804}
805
806class SetLocalVariableClosure : public CommonLocalVariableClosure {
807 public:
808 SetLocalVariableClosure(art::Thread* caller,
809 jint depth,
810 jint slot,
811 art::Primitive::Type type,
812 jvalue val)
Alex Light318afe62018-03-22 16:50:10 -0700813 : CommonLocalVariableClosure(depth, slot), caller_(caller), type_(type), val_(val) {}
Alex Lightbebd7bd2017-07-25 14:05:52 -0700814
815 protected:
816 jvmtiError GetTypeError(art::ArtMethod* method,
817 art::Primitive::Type slot_type,
818 const std::string& descriptor)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100819 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700820 switch (slot_type) {
821 case art::Primitive::kPrimNot: {
822 if (type_ != art::Primitive::kPrimNot) {
823 return ERR(TYPE_MISMATCH);
824 } else if (val_.l == nullptr) {
825 return OK;
826 } else {
827 art::ClassLinker* cl = art::Runtime::Current()->GetClassLinker();
828 art::ObjPtr<art::mirror::Class> set_class =
829 caller_->DecodeJObject(val_.l)->GetClass();
830 art::ObjPtr<art::mirror::ClassLoader> loader =
831 method->GetDeclaringClass()->GetClassLoader();
832 art::ObjPtr<art::mirror::Class> slot_class =
833 cl->LookupClass(caller_, descriptor.c_str(), loader);
834 DCHECK(!slot_class.IsNull());
835 return slot_class->IsAssignableFrom(set_class) ? OK : ERR(TYPE_MISMATCH);
836 }
837 }
838 case art::Primitive::kPrimByte:
839 case art::Primitive::kPrimChar:
840 case art::Primitive::kPrimInt:
841 case art::Primitive::kPrimShort:
842 case art::Primitive::kPrimBoolean:
843 return type_ == art::Primitive::kPrimInt ? OK : ERR(TYPE_MISMATCH);
844 case art::Primitive::kPrimLong:
845 case art::Primitive::kPrimFloat:
846 case art::Primitive::kPrimDouble:
847 return type_ == slot_type ? OK : ERR(TYPE_MISMATCH);
848 case art::Primitive::kPrimVoid:
849 LOG(FATAL) << "Unexpected primitive type " << slot_type;
850 UNREACHABLE();
851 }
852 }
853
854 jvmtiError Execute(art::ArtMethod* method, art::StackVisitor& visitor)
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100855 override REQUIRES_SHARED(art::Locks::mutator_lock_) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700856 switch (type_) {
857 case art::Primitive::kPrimNot: {
858 uint32_t ptr_val;
859 art::ObjPtr<art::mirror::Object> obj(caller_->DecodeJObject(val_.l));
860 ptr_val = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(obj.Ptr()));
861 if (!visitor.SetVReg(method,
862 static_cast<uint16_t>(slot_),
863 ptr_val,
864 art::kReferenceVReg)) {
865 return ERR(OPAQUE_FRAME);
866 }
867 break;
868 }
869 case art::Primitive::kPrimInt:
870 case art::Primitive::kPrimFloat: {
871 if (!visitor.SetVReg(method,
872 static_cast<uint16_t>(slot_),
873 static_cast<uint32_t>(val_.i),
874 type_ == art::Primitive::kPrimFloat ? art::kFloatVReg
875 : art::kIntVReg)) {
876 return ERR(OPAQUE_FRAME);
877 }
878 break;
879 }
880 case art::Primitive::kPrimDouble:
881 case art::Primitive::kPrimLong: {
882 auto lo_type = type_ == art::Primitive::kPrimLong ? art::kLongLoVReg : art::kDoubleLoVReg;
883 auto high_type = type_ == art::Primitive::kPrimLong ? art::kLongHiVReg : art::kDoubleHiVReg;
884 if (!visitor.SetVRegPair(method,
885 static_cast<uint16_t>(slot_),
886 static_cast<uint64_t>(val_.j),
887 lo_type,
888 high_type)) {
889 return ERR(OPAQUE_FRAME);
890 }
891 break;
892 }
893 default: {
894 LOG(FATAL) << "unexpected register type " << type_;
895 UNREACHABLE();
896 }
897 }
898 return OK;
899 }
900
901 private:
Alex Light318afe62018-03-22 16:50:10 -0700902 art::Thread* caller_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700903 art::Primitive::Type type_;
904 jvalue val_;
905};
906
907jvmtiError MethodUtil::SetLocalVariableGeneric(jvmtiEnv* env ATTRIBUTE_UNUSED,
908 jthread thread,
909 jint depth,
910 jint slot,
911 art::Primitive::Type type,
912 jvalue val) {
913 if (depth < 0) {
914 return ERR(ILLEGAL_ARGUMENT);
915 }
Alex Lightf2858632018-04-02 11:28:50 -0700916 // Make sure that we know not to do any OSR anymore.
917 // TODO We should really keep track of this at the Frame granularity.
918 DeoptManager::Get()->SetLocalsUpdated();
Alex Lightbebd7bd2017-07-25 14:05:52 -0700919 art::Thread* self = art::Thread::Current();
920 art::ScopedObjectAccess soa(self);
Alex Lightb1e31a82017-10-04 16:57:36 -0700921 art::Locks::thread_list_lock_->ExclusiveLock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700922 art::Thread* target = nullptr;
923 jvmtiError err = ERR(INTERNAL);
924 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
Alex Lightb1e31a82017-10-04 16:57:36 -0700925 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700926 return err;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700927 }
928 SetLocalVariableClosure c(self, depth, slot, type, val);
Alex Lightb1e31a82017-10-04 16:57:36 -0700929 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution.
Alex Light318afe62018-03-22 16:50:10 -0700930 if (!target->RequestSynchronousCheckpoint(&c)) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700931 return ERR(THREAD_NOT_ALIVE);
932 } else {
933 return c.GetResult();
934 }
935}
936
937class GetLocalInstanceClosure : public art::Closure {
938 public:
Alex Light318afe62018-03-22 16:50:10 -0700939 explicit GetLocalInstanceClosure(jint depth)
Alex Lightbebd7bd2017-07-25 14:05:52 -0700940 : result_(ERR(INTERNAL)),
Alex Lightbebd7bd2017-07-25 14:05:52 -0700941 depth_(depth),
Alex Light318afe62018-03-22 16:50:10 -0700942 val_(nullptr) {}
Alex Lightbebd7bd2017-07-25 14:05:52 -0700943
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100944 void Run(art::Thread* self) override REQUIRES(art::Locks::mutator_lock_) {
Alex Light318afe62018-03-22 16:50:10 -0700945 art::ScopedAssertNoThreadSuspension sants("GetLocalInstanceClosure::Run");
Alex Lightbebd7bd2017-07-25 14:05:52 -0700946 art::Locks::mutator_lock_->AssertSharedHeld(art::Thread::Current());
947 std::unique_ptr<art::Context> context(art::Context::Create());
948 FindFrameAtDepthVisitor visitor(self, context.get(), depth_);
949 visitor.WalkStack();
950 if (!visitor.FoundFrame()) {
951 // Must have been a bad depth.
952 result_ = ERR(NO_MORE_FRAMES);
953 return;
954 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700955 result_ = OK;
Alex Light318afe62018-03-22 16:50:10 -0700956 val_ = art::GcRoot<art::mirror::Object>(visitor.GetThisObject());
Alex Lightbebd7bd2017-07-25 14:05:52 -0700957 }
958
Alex Light318afe62018-03-22 16:50:10 -0700959 jvmtiError GetResult(jobject* data_out) REQUIRES_SHARED(art::Locks::mutator_lock_) {
960 if (result_ == OK) {
961 *data_out = val_.IsNull()
962 ? nullptr
963 : art::Thread::Current()->GetJniEnv()->AddLocalReference<jobject>(val_.Read());
964 }
Alex Lightbebd7bd2017-07-25 14:05:52 -0700965 return result_;
966 }
967
968 private:
969 jvmtiError result_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700970 jint depth_;
Alex Light318afe62018-03-22 16:50:10 -0700971 art::GcRoot<art::mirror::Object> val_;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700972};
973
974jvmtiError MethodUtil::GetLocalInstance(jvmtiEnv* env ATTRIBUTE_UNUSED,
975 jthread thread,
976 jint depth,
977 jobject* data) {
978 if (depth < 0) {
979 return ERR(ILLEGAL_ARGUMENT);
980 }
981 art::Thread* self = art::Thread::Current();
982 art::ScopedObjectAccess soa(self);
Alex Lightb1e31a82017-10-04 16:57:36 -0700983 art::Locks::thread_list_lock_->ExclusiveLock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700984 art::Thread* target = nullptr;
985 jvmtiError err = ERR(INTERNAL);
986 if (!ThreadUtil::GetAliveNativeThread(thread, soa, &target, &err)) {
Alex Lightb1e31a82017-10-04 16:57:36 -0700987 art::Locks::thread_list_lock_->ExclusiveUnlock(self);
Alex Light7ddc23d2017-09-22 15:33:41 -0700988 return err;
Alex Lightbebd7bd2017-07-25 14:05:52 -0700989 }
Alex Light318afe62018-03-22 16:50:10 -0700990 art::ScopedAssertNoThreadSuspension sants("Performing GetLocalInstance");
991 GetLocalInstanceClosure c(depth);
992 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. We
993 // need to avoid suspending as we wait for the checkpoint to occur since we are (potentially)
994 // transfering a GcRoot across threads.
995 if (!target->RequestSynchronousCheckpoint(&c, art::ThreadState::kRunnable)) {
Alex Lightbebd7bd2017-07-25 14:05:52 -0700996 return ERR(THREAD_NOT_ALIVE);
997 } else {
Alex Light318afe62018-03-22 16:50:10 -0700998 return c.GetResult(data);
Alex Lightbebd7bd2017-07-25 14:05:52 -0700999 }
1000}
1001
1002#define FOR_JVMTI_JVALUE_TYPES(fn) \
1003 fn(jint, art::Primitive::kPrimInt, i) \
1004 fn(jlong, art::Primitive::kPrimLong, j) \
1005 fn(jfloat, art::Primitive::kPrimFloat, f) \
1006 fn(jdouble, art::Primitive::kPrimDouble, d) \
1007 fn(jobject, art::Primitive::kPrimNot, l)
1008
1009namespace impl {
1010
1011template<typename T> void WriteJvalue(T, jvalue*);
1012template<typename T> void ReadJvalue(jvalue, T*);
1013template<typename T> art::Primitive::Type GetJNIType();
1014
1015#define JNI_TYPE_CHAR(type, prim, id) \
1016template<> art::Primitive::Type GetJNIType<type>() { \
1017 return prim; \
1018}
1019
1020FOR_JVMTI_JVALUE_TYPES(JNI_TYPE_CHAR);
1021
1022#undef JNI_TYPE_CHAR
1023
Andreas Gampe49fc60e2017-08-24 13:19:59 -07001024#define RW_JVALUE(srctype, prim, id) \
1025 template<> void ReadJvalue<srctype>(jvalue in, std::add_pointer<srctype>::type out) { \
Alex Lightbebd7bd2017-07-25 14:05:52 -07001026 *out = in.id; \
1027 } \
Andreas Gampe49fc60e2017-08-24 13:19:59 -07001028 template<> void WriteJvalue<srctype>(srctype in, jvalue* out) { \
Alex Lightbebd7bd2017-07-25 14:05:52 -07001029 out->id = in; \
1030 }
1031
1032FOR_JVMTI_JVALUE_TYPES(RW_JVALUE);
1033
1034#undef RW_JVALUE
1035
1036} // namespace impl
1037
1038template<typename T>
1039jvmtiError MethodUtil::SetLocalVariable(jvmtiEnv* env,
1040 jthread thread,
1041 jint depth,
1042 jint slot,
1043 T data) {
1044 jvalue v = {.j = 0};
1045 art::Primitive::Type type = impl::GetJNIType<T>();
1046 impl::WriteJvalue(data, &v);
1047 return SetLocalVariableGeneric(env, thread, depth, slot, type, v);
1048}
1049
1050template<typename T>
1051jvmtiError MethodUtil::GetLocalVariable(jvmtiEnv* env,
1052 jthread thread,
1053 jint depth,
1054 jint slot,
1055 T* data) {
1056 if (data == nullptr) {
1057 return ERR(NULL_POINTER);
1058 }
1059 jvalue v = {.j = 0};
1060 art::Primitive::Type type = impl::GetJNIType<T>();
1061 jvmtiError err = GetLocalVariableGeneric(env, thread, depth, slot, type, &v);
1062 if (err != OK) {
1063 return err;
1064 } else {
1065 impl::ReadJvalue(v, data);
1066 return OK;
1067 }
1068}
1069
Andreas Gampe49fc60e2017-08-24 13:19:59 -07001070#define GET_SET_LV(srctype, prim, id) \
1071 template jvmtiError MethodUtil::GetLocalVariable<srctype>(jvmtiEnv*, \
1072 jthread, \
1073 jint, \
1074 jint, \
1075 std::add_pointer<srctype>::type); \
1076 template jvmtiError MethodUtil::SetLocalVariable<srctype>(jvmtiEnv*, \
1077 jthread, \
1078 jint, \
1079 jint, \
1080 srctype);
Alex Lightbebd7bd2017-07-25 14:05:52 -07001081
1082FOR_JVMTI_JVALUE_TYPES(GET_SET_LV);
1083
1084#undef GET_SET_LV
1085
1086#undef FOR_JVMTI_JVALUE_TYPES
1087
Andreas Gampe3c252f02016-10-27 18:25:17 -07001088} // namespace openjdkjvmti