1 /*
2 * Copyright (c) 2003, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "classfile/classLoaderDataGraph.hpp"
26 #include "classfile/javaClasses.inline.hpp"
27 #include "classfile/moduleEntry.hpp"
28 #include "classfile/symbolTable.hpp"
29 #include "classfile/vmSymbols.hpp"
30 #include "jvmtifiles/jvmtiEnv.hpp"
31 #include "memory/iterator.hpp"
32 #include "memory/resourceArea.hpp"
33 #include "oops/klass.inline.hpp"
34 #include "oops/objArrayKlass.hpp"
35 #include "oops/objArrayOop.hpp"
36 #include "oops/oop.inline.hpp"
37 #include "oops/oopHandle.inline.hpp"
38 #include "prims/jvmtiEnvBase.hpp"
39 #include "prims/jvmtiEventController.inline.hpp"
40 #include "prims/jvmtiExtensions.hpp"
41 #include "prims/jvmtiImpl.hpp"
42 #include "prims/jvmtiManageCapabilities.hpp"
43 #include "prims/jvmtiTagMap.hpp"
44 #include "prims/jvmtiThreadState.inline.hpp"
45 #include "runtime/continuationEntry.inline.hpp"
46 #include "runtime/deoptimization.hpp"
47 #include "runtime/frame.inline.hpp"
48 #include "runtime/handles.inline.hpp"
49 #include "runtime/interfaceSupport.inline.hpp"
50 #include "runtime/javaCalls.hpp"
51 #include "runtime/javaThread.inline.hpp"
52 #include "runtime/jfieldIDWorkaround.hpp"
53 #include "runtime/jniHandles.inline.hpp"
54 #include "runtime/mountUnmountDisabler.hpp"
55 #include "runtime/objectMonitor.inline.hpp"
56 #include "runtime/osThread.hpp"
57 #include "runtime/signature.hpp"
58 #include "runtime/stackWatermarkSet.inline.hpp"
59 #include "runtime/synchronizer.hpp"
60 #include "runtime/threads.hpp"
61 #include "runtime/threadSMR.inline.hpp"
62 #include "runtime/vframe.inline.hpp"
63 #include "runtime/vframe_hp.hpp"
64 #include "runtime/vmOperations.hpp"
65 #include "runtime/vmThread.hpp"
66 #include "services/threadService.hpp"
67
68
69 ///////////////////////////////////////////////////////////////
70 //
71 // JvmtiEnvBase
72 //
73
74 JvmtiEnvBase* JvmtiEnvBase::_head_environment = nullptr;
75
76 bool JvmtiEnvBase::_globally_initialized = false;
77 volatile bool JvmtiEnvBase::_needs_clean_up = false;
78
79 jvmtiPhase JvmtiEnvBase::_phase = JVMTI_PHASE_PRIMORDIAL;
80
81 volatile int JvmtiEnvBase::_dying_thread_env_iteration_count = 0;
82
83 extern jvmtiInterface_1_ jvmti_Interface;
84 extern jvmtiInterface_1_ jvmtiTrace_Interface;
85
86
87 // perform initializations that must occur before any JVMTI environments
88 // are released but which should only be initialized once (no matter
89 // how many environments are created).
90 void
91 JvmtiEnvBase::globally_initialize() {
92 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
93 assert(_globally_initialized == false, "bad call");
94
95 JvmtiManageCapabilities::initialize();
96
97 // register extension functions and events
98 JvmtiExtensions::register_extensions();
99
100 #ifdef JVMTI_TRACE
101 JvmtiTrace::initialize();
102 #endif
103
104 _globally_initialized = true;
105 }
106
107
108 void
109 JvmtiEnvBase::initialize() {
110 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
111
112 // Add this environment to the end of the environment list (order is important)
113 {
114 // This block of code must not contain any safepoints, as list deallocation
115 // (which occurs at a safepoint) cannot occur simultaneously with this list
116 // addition. Note: NoSafepointVerifier cannot, currently, be used before
117 // threads exist.
118 JvmtiEnvIterator it;
119 JvmtiEnvBase *previous_env = nullptr;
120 for (JvmtiEnvBase* env = it.first(); env != nullptr; env = it.next(env)) {
121 previous_env = env;
122 }
123 if (previous_env == nullptr) {
124 _head_environment = this;
125 } else {
126 previous_env->set_next_environment(this);
127 }
128 }
129
130 if (_globally_initialized == false) {
131 globally_initialize();
132 }
133 }
134
135 jvmtiPhase
136 JvmtiEnvBase::phase() {
137 // For the JVMTI environments possessed the can_generate_early_vmstart:
138 // replace JVMTI_PHASE_PRIMORDIAL with JVMTI_PHASE_START
139 if (_phase == JVMTI_PHASE_PRIMORDIAL &&
140 JvmtiExport::early_vmstart_recorded() &&
141 early_vmstart_env()) {
142 return JVMTI_PHASE_START;
143 }
144 return _phase; // Normal case
145 }
146
147 bool
148 JvmtiEnvBase::is_valid() {
149 jlong value = 0;
150
151 // This object might not be a JvmtiEnvBase so we can't assume
152 // the _magic field is properly aligned. Get the value in a safe
153 // way and then check against JVMTI_MAGIC.
154
155 switch (sizeof(_magic)) {
156 case 2:
157 value = Bytes::get_native_u2((address)&_magic);
158 break;
159
160 case 4:
161 value = Bytes::get_native_u4((address)&_magic);
162 break;
163
164 case 8:
165 value = Bytes::get_native_u8((address)&_magic);
166 break;
167
168 default:
169 guarantee(false, "_magic field is an unexpected size");
170 }
171
172 return value == JVMTI_MAGIC;
173 }
174
175
176 bool
177 JvmtiEnvBase::use_version_1_0_semantics() {
178 int major, minor, micro;
179
180 JvmtiExport::decode_version_values(_version, &major, &minor, µ);
181 return major == 1 && minor == 0; // micro version doesn't matter here
182 }
183
184
185 bool
186 JvmtiEnvBase::use_version_1_1_semantics() {
187 int major, minor, micro;
188
189 JvmtiExport::decode_version_values(_version, &major, &minor, µ);
190 return major == 1 && minor == 1; // micro version doesn't matter here
191 }
192
193 bool
194 JvmtiEnvBase::use_version_1_2_semantics() {
195 int major, minor, micro;
196
197 JvmtiExport::decode_version_values(_version, &major, &minor, µ);
198 return major == 1 && minor == 2; // micro version doesn't matter here
199 }
200
201
202 JvmtiEnvBase::JvmtiEnvBase(jint version) : _env_event_enable() {
203 _version = version;
204 _env_local_storage = nullptr;
205 _tag_map = nullptr;
206 _native_method_prefix_count = 0;
207 _native_method_prefixes = nullptr;
208 _next = nullptr;
209 _class_file_load_hook_ever_enabled = false;
210
211 // Moot since ClassFileLoadHook not yet enabled.
212 // But "true" will give a more predictable ClassFileLoadHook behavior
213 // for environment creation during ClassFileLoadHook.
214 _is_retransformable = true;
215
216 // all callbacks initially null
217 memset(&_event_callbacks, 0, sizeof(jvmtiEventCallbacks));
218 memset(&_ext_event_callbacks, 0, sizeof(jvmtiExtEventCallbacks));
219
220 // all capabilities initially off
221 memset(&_current_capabilities, 0, sizeof(_current_capabilities));
222
223 // all prohibited capabilities initially off
224 memset(&_prohibited_capabilities, 0, sizeof(_prohibited_capabilities));
225
226 _magic = JVMTI_MAGIC;
227
228 JvmtiEventController::env_initialize((JvmtiEnv*)this);
229
230 #ifdef JVMTI_TRACE
231 _jvmti_external.functions = TraceJVMTI != nullptr ? &jvmtiTrace_Interface : &jvmti_Interface;
232 #else
233 _jvmti_external.functions = &jvmti_Interface;
234 #endif
235 }
236
237
238 void
239 JvmtiEnvBase::dispose() {
240
241 #ifdef JVMTI_TRACE
242 JvmtiTrace::shutdown();
243 #endif
244
245 // Dispose of event info and let the event controller call us back
246 // in a locked state (env_dispose, below)
247 JvmtiEventController::env_dispose(this);
248 }
249
250 void
251 JvmtiEnvBase::env_dispose() {
252 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
253
254 // We have been entered with all events disabled on this environment.
255 // A race to re-enable events (by setting callbacks) is prevented by
256 // checking for a valid environment when setting callbacks (while
257 // holding the JvmtiThreadState_lock).
258
259 // Mark as invalid.
260 _magic = DISPOSED_MAGIC;
261
262 // Relinquish all capabilities.
263 jvmtiCapabilities *caps = get_capabilities();
264 JvmtiManageCapabilities::relinquish_capabilities(caps, caps, caps);
265
266 // Same situation as with events (see above)
267 set_native_method_prefixes(0, nullptr);
268
269 JvmtiTagMap* tag_map_to_clear = tag_map_acquire();
270 // A tag map can be big, clear it now to save memory until
271 // the destructor runs.
272 if (tag_map_to_clear != nullptr) {
273 tag_map_to_clear->clear();
274 }
275
276 _needs_clean_up = true;
277 }
278
279
280 JvmtiEnvBase::~JvmtiEnvBase() {
281 assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
282
283 // There is a small window of time during which the tag map of a
284 // disposed environment could have been reallocated.
285 // Make sure it is gone.
286 JvmtiTagMap* tag_map_to_deallocate = _tag_map;
287 set_tag_map(nullptr);
288 // A tag map can be big, deallocate it now
289 if (tag_map_to_deallocate != nullptr) {
290 delete tag_map_to_deallocate;
291 }
292
293 _magic = BAD_MAGIC;
294 }
295
296
297 void
298 JvmtiEnvBase::periodic_clean_up() {
299 assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
300
301 // JvmtiEnvBase reference is saved in JvmtiEnvThreadState. So
302 // clean up JvmtiThreadState before deleting JvmtiEnv pointer.
303 JvmtiThreadState::periodic_clean_up();
304
305 // Unlink all invalid environments from the list of environments
306 // and deallocate them
307 JvmtiEnvIterator it;
308 JvmtiEnvBase* previous_env = nullptr;
309 JvmtiEnvBase* env = it.first();
310 while (env != nullptr) {
311 if (env->is_valid()) {
312 previous_env = env;
313 env = it.next(env);
314 } else {
315 // This one isn't valid, remove it from the list and deallocate it
316 JvmtiEnvBase* defunct_env = env;
317 env = it.next(env);
318 if (previous_env == nullptr) {
319 _head_environment = env;
320 } else {
321 previous_env->set_next_environment(env);
322 }
323 delete defunct_env;
324 }
325 }
326
327 }
328
329
330 void
331 JvmtiEnvBase::check_for_periodic_clean_up() {
332 assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
333
334 class ThreadInsideIterationClosure: public ThreadClosure {
335 private:
336 bool _inside;
337 public:
338 ThreadInsideIterationClosure() : _inside(false) {};
339
340 void do_thread(Thread* thread) {
341 _inside |= thread->is_inside_jvmti_env_iteration();
342 }
343
344 bool is_inside_jvmti_env_iteration() {
345 return _inside;
346 }
347 };
348
349 if (_needs_clean_up) {
350 // Check if we are currently iterating environment,
351 // deallocation should not occur if we are
352 ThreadInsideIterationClosure tiic;
353 Threads::threads_do(&tiic);
354 if (!tiic.is_inside_jvmti_env_iteration() &&
355 !is_inside_dying_thread_env_iteration()) {
356 _needs_clean_up = false;
357 JvmtiEnvBase::periodic_clean_up();
358 }
359 }
360 }
361
362
363 void
364 JvmtiEnvBase::record_first_time_class_file_load_hook_enabled() {
365 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(),
366 "sanity check");
367
368 if (!_class_file_load_hook_ever_enabled) {
369 _class_file_load_hook_ever_enabled = true;
370
371 if (get_capabilities()->can_retransform_classes) {
372 _is_retransformable = true;
373 } else {
374 _is_retransformable = false;
375
376 // cannot add retransform capability after ClassFileLoadHook has been enabled
377 get_prohibited_capabilities()->can_retransform_classes = 1;
378 }
379 }
380 }
381
382
383 void
384 JvmtiEnvBase::record_class_file_load_hook_enabled() {
385 if (!_class_file_load_hook_ever_enabled) {
386 if (Threads::number_of_threads() == 0) {
387 record_first_time_class_file_load_hook_enabled();
388 } else {
389 MutexLocker mu(JvmtiThreadState_lock);
390 record_first_time_class_file_load_hook_enabled();
391 }
392 }
393 }
394
395
396 jvmtiError
397 JvmtiEnvBase::set_native_method_prefixes(jint prefix_count, char** prefixes) {
398 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(),
399 "sanity check");
400
401 int old_prefix_count = get_native_method_prefix_count();
402 char **old_prefixes = get_native_method_prefixes();
403
404 // allocate and install the new prefixex
405 if (prefix_count == 0 || !is_valid()) {
406 _native_method_prefix_count = 0;
407 _native_method_prefixes = nullptr;
408 } else {
409 // there are prefixes, allocate an array to hold them, and fill it
410 char** new_prefixes = (char**)os::malloc((prefix_count) * sizeof(char*), mtInternal);
411 if (new_prefixes == nullptr) {
412 return JVMTI_ERROR_OUT_OF_MEMORY;
413 }
414 for (int i = 0; i < prefix_count; i++) {
415 char* prefix = prefixes[i];
416 if (prefix == nullptr) {
417 for (int j = 0; j < (i-1); j++) {
418 os::free(new_prefixes[j]);
419 }
420 os::free(new_prefixes);
421 return JVMTI_ERROR_NULL_POINTER;
422 }
423 prefix = os::strdup(prefixes[i]);
424 if (prefix == nullptr) {
425 for (int j = 0; j < (i-1); j++) {
426 os::free(new_prefixes[j]);
427 }
428 os::free(new_prefixes);
429 return JVMTI_ERROR_OUT_OF_MEMORY;
430 }
431 new_prefixes[i] = prefix;
432 }
433 _native_method_prefix_count = prefix_count;
434 _native_method_prefixes = new_prefixes;
435 }
436
437 // now that we know the new prefixes have been successfully installed we can
438 // safely remove the old ones
439 if (old_prefix_count != 0) {
440 for (int i = 0; i < old_prefix_count; i++) {
441 os::free(old_prefixes[i]);
442 }
443 os::free(old_prefixes);
444 }
445
446 return JVMTI_ERROR_NONE;
447 }
448
449
450 // Collect all the prefixes which have been set in any JVM TI environments
451 // by the SetNativeMethodPrefix(es) functions. Be sure to maintain the
452 // order of environments and the order of prefixes within each environment.
453 // Return in a resource allocated array.
454 char**
455 JvmtiEnvBase::get_all_native_method_prefixes(int* count_ptr) {
456 assert(Threads::number_of_threads() == 0 ||
457 SafepointSynchronize::is_at_safepoint() ||
458 JvmtiThreadState_lock->is_locked(),
459 "sanity check");
460
461 int total_count = 0;
462 GrowableArray<char*>* prefix_array =new GrowableArray<char*>(5);
463
464 JvmtiEnvIterator it;
465 for (JvmtiEnvBase* env = it.first(); env != nullptr; env = it.next(env)) {
466 int prefix_count = env->get_native_method_prefix_count();
467 char** prefixes = env->get_native_method_prefixes();
468 for (int j = 0; j < prefix_count; j++) {
469 // retrieve a prefix and so that it is safe against asynchronous changes
470 // copy it into the resource area
471 char* prefix = prefixes[j];
472 char* prefix_copy = ResourceArea::strdup(prefix);
473 prefix_array->at_put_grow(total_count++, prefix_copy);
474 }
475 }
476
477 char** all_prefixes = NEW_RESOURCE_ARRAY(char*, total_count);
478 char** p = all_prefixes;
479 for (int i = 0; i < total_count; ++i) {
480 *p++ = prefix_array->at(i);
481 }
482 *count_ptr = total_count;
483 return all_prefixes;
484 }
485
486 void
487 JvmtiEnvBase::set_event_callbacks(const jvmtiEventCallbacks* callbacks,
488 jint size_of_callbacks) {
489 assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
490
491 size_t byte_cnt = sizeof(jvmtiEventCallbacks);
492
493 // clear in either case to be sure we got any gap between sizes
494 memset(&_event_callbacks, 0, byte_cnt);
495
496 // Now that JvmtiThreadState_lock is held, prevent a possible race condition where events
497 // are re-enabled by a call to set event callbacks where the DisposeEnvironment
498 // occurs after the boiler-plate environment check and before the lock is acquired.
499 if (callbacks != nullptr && is_valid()) {
500 if (size_of_callbacks < (jint)byte_cnt) {
501 byte_cnt = size_of_callbacks;
502 }
503 memcpy(&_event_callbacks, callbacks, byte_cnt);
504 }
505 }
506
507
508 // In the fullness of time, all users of the method should instead
509 // directly use allocate, besides being cleaner and faster, this will
510 // mean much better out of memory handling
511 unsigned char *
512 JvmtiEnvBase::jvmtiMalloc(jlong size) {
513 unsigned char* mem = nullptr;
514 jvmtiError result = allocate(size, &mem);
515 assert(result == JVMTI_ERROR_NONE, "Allocate failed");
516 return mem;
517 }
518
519
520 // Handle management
521
522 jobject JvmtiEnvBase::jni_reference(Handle hndl) {
523 return JNIHandles::make_local(hndl());
524 }
525
526 jobject JvmtiEnvBase::jni_reference(JavaThread *thread, Handle hndl) {
527 return JNIHandles::make_local(thread, hndl());
528 }
529
530 void JvmtiEnvBase::destroy_jni_reference(jobject jobj) {
531 JNIHandles::destroy_local(jobj);
532 }
533
534 void JvmtiEnvBase::destroy_jni_reference(JavaThread *thread, jobject jobj) {
535 JNIHandles::destroy_local(jobj); // thread is unused.
536 }
537
538 //
539 // Threads
540 //
541
542 jthread *
543 JvmtiEnvBase::new_jthreadArray(int length, Handle *handles) {
544 if (length == 0) {
545 return nullptr;
546 }
547
548 jthread* objArray = (jthread *) jvmtiMalloc(sizeof(jthread) * length);
549 NULL_CHECK(objArray, nullptr);
550
551 for (int i = 0; i < length; i++) {
552 objArray[i] = (jthread)jni_reference(handles[i]);
553 }
554 return objArray;
555 }
556
557 jthreadGroup *
558 JvmtiEnvBase::new_jthreadGroupArray(int length, refArrayHandle groups) {
559 if (length == 0) {
560 return nullptr;
561 }
562
563 jthreadGroup* objArray = (jthreadGroup *) jvmtiMalloc(sizeof(jthreadGroup) * length);
564 NULL_CHECK(objArray, nullptr);
565
566 for (int i = 0; i < length; i++) {
567 objArray[i] = (jthreadGroup)JNIHandles::make_local(groups->obj_at(i));
568 }
569 return objArray;
570 }
571
572 // Return the vframe on the specified thread and depth, null if no such frame.
573 // The thread and the oops in the returned vframe might not have been processed.
574 javaVFrame*
575 JvmtiEnvBase::jvf_for_thread_and_depth(JavaThread* java_thread, jint depth) {
576 if (!java_thread->has_last_Java_frame()) {
577 return nullptr;
578 }
579 RegisterMap reg_map(java_thread,
580 RegisterMap::UpdateMap::include,
581 RegisterMap::ProcessFrames::skip,
582 RegisterMap::WalkContinuation::include);
583 javaVFrame *jvf = java_thread->last_java_vframe(®_map);
584
585 jvf = JvmtiEnvBase::check_and_skip_hidden_frames(java_thread, jvf);
586 for (int d = 0; jvf != nullptr && d < depth; d++) {
587 jvf = jvf->java_sender();
588 }
589 return jvf;
590 }
591
592 //
593 // utilities: JNI objects
594 //
595
596
597 jclass
598 JvmtiEnvBase::get_jni_class_non_null(Klass* k) {
599 assert(k != nullptr, "k != null");
600 assert(k->is_loader_alive(), "Must be alive");
601 Thread *thread = Thread::current();
602 return (jclass)jni_reference(Handle(thread, k->java_mirror()));
603 }
604
605 //
606 // Field Information
607 //
608
609 bool
610 JvmtiEnvBase::get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd) {
611 if (!jfieldIDWorkaround::is_valid_jfieldID(k, field)) {
612 return false;
613 }
614 bool found = false;
615 if (jfieldIDWorkaround::is_static_jfieldID(field)) {
616 JNIid* id = jfieldIDWorkaround::from_static_jfieldID(field);
617 found = id->find_local_field(fd);
618 } else {
619 // Non-static field. The fieldID is really the offset of the field within the object.
620 int offset = jfieldIDWorkaround::from_instance_jfieldID(k, field);
621 found = InstanceKlass::cast(k)->find_field_from_offset(offset, false, fd);
622 }
623 return found;
624 }
625
626 bool
627 JvmtiEnvBase::is_vthread_alive(oop vt) {
628 oop cont = java_lang_VirtualThread::continuation(vt);
629 return !jdk_internal_vm_Continuation::done(cont) &&
630 java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::NEW;
631 }
632
633 // Return JavaThread if virtual thread is mounted, null otherwise.
634 JavaThread* JvmtiEnvBase::get_JavaThread_or_null(oop vthread) {
635 oop carrier_thread = java_lang_VirtualThread::carrier_thread(vthread);
636 if (carrier_thread == nullptr) {
637 return nullptr;
638 }
639
640 JavaThread* java_thread = java_lang_Thread::thread(carrier_thread);
641
642 // This could be a different thread to the current one. So we need to ensure that
643 // processing has started before we are allowed to read the continuation oop of
644 // another thread, as it is a direct root of that other thread.
645 StackWatermarkSet::start_processing(java_thread, StackWatermarkKind::gc);
646
647 oop cont = java_lang_VirtualThread::continuation(vthread);
648 assert(cont != nullptr, "must be");
649 assert(Continuation::continuation_scope(cont) == java_lang_VirtualThread::vthread_scope(), "must be");
650 return Continuation::is_continuation_mounted(java_thread, cont) ? java_thread : nullptr;
651 }
652
653 // An unmounted vthread may have an empty stack.
654 // If unmounted from Java, it always has the yield0() and yield() frames we
655 // need to hide. The methods yield0() and yield() are annotated with the @JvmtiHideEvents.
656 javaVFrame*
657 JvmtiEnvBase::skip_yield_frames_for_unmounted_vthread(oop vthread, javaVFrame* jvf) {
658 if (jvf == nullptr) {
659 return jvf; // empty stack is possible
660 }
661 if (java_lang_VirtualThread::is_preempted(vthread)) {
662 // Top method should not be from Continuation class.
663 assert(jvf->method()->method_holder() != vmClasses::Continuation_klass(), "");
664 return jvf;
665 }
666
667 assert(jvf->method()->jvmti_hide_events(), "sanity check");
668 assert(jvf->method()->method_holder() == vmClasses::Continuation_klass(), "expected Continuation class");
669 jvf = jvf->java_sender(); // skip yield0 frame
670
671 assert(jvf != nullptr && jvf->method()->jvmti_hide_events(), "sanity check");
672 assert(jvf->method()->method_holder() == vmClasses::Continuation_klass(), "expected Continuation class");
673 jvf = jvf->java_sender(); // skip yield frame
674 return jvf;
675 }
676
677 // A thread may have an empty stack.
678 // Otherwise, some top frames may heed to be hidden.
679 // Two cases are processed below:
680 // - top frame is annotated with @JvmtiMountTransition: just skip top frames with annotated methods
681 // - JavaThread is in VTMS transition: skip top frames until a frame annotated with @ChangesCurrentThread is found
682 javaVFrame*
683 JvmtiEnvBase::check_and_skip_hidden_frames(bool is_in_VTMS_transition, javaVFrame* jvf) {
684 if (jvf == nullptr) {
685 return jvf; // empty stack is possible
686 }
687 if (jvf->method()->jvmti_mount_transition()) {
688 // Skip frames annotated with @JvmtiMountTransition.
689 for ( ; jvf != nullptr && jvf->method()->jvmti_mount_transition(); jvf = jvf->java_sender()) {
690 }
691 } else if (is_in_VTMS_transition) {
692 // Skip frames above the frame annotated with @ChangesCurrentThread.
693 for ( ; jvf != nullptr && !jvf->method()->changes_current_thread(); jvf = jvf->java_sender()) {
694 }
695 }
696 return jvf;
697 }
698
699 javaVFrame*
700 JvmtiEnvBase::check_and_skip_hidden_frames(JavaThread* jt, javaVFrame* jvf) {
701 jvf = check_and_skip_hidden_frames(jt->is_in_vthread_transition(), jvf);
702 return jvf;
703 }
704
705 javaVFrame*
706 JvmtiEnvBase::get_vthread_jvf(oop vthread) {
707 assert(java_lang_VirtualThread::state(vthread) != java_lang_VirtualThread::NEW, "sanity check");
708 assert(java_lang_VirtualThread::state(vthread) != java_lang_VirtualThread::TERMINATED, "sanity check");
709
710 Thread* cur_thread = Thread::current();
711 oop cont = java_lang_VirtualThread::continuation(vthread);
712 javaVFrame* jvf = nullptr;
713
714 JavaThread* java_thread = get_JavaThread_or_null(vthread);
715 if (java_thread != nullptr) {
716 if (!java_thread->has_last_Java_frame()) {
717 // TBD: This is a temporary work around to avoid a guarantee caused by
718 // the native enterSpecial frame on the top. No frames will be found
719 // by the JVMTI functions such as GetStackTrace.
720 return nullptr;
721 }
722 vframeStream vfs(java_thread);
723 assert(!java_thread->is_in_vthread_transition(), "invariant");
724 jvf = vfs.at_end() ? nullptr : vfs.asJavaVFrame();
725 jvf = check_and_skip_hidden_frames(false, jvf);
726 } else {
727 vframeStream vfs(cont);
728 jvf = vfs.at_end() ? nullptr : vfs.asJavaVFrame();
729 jvf = skip_yield_frames_for_unmounted_vthread(vthread, jvf);
730 }
731 return jvf;
732 }
733
734 // Return correct javaVFrame for a carrier (non-virtual) thread.
735 // It strips vthread frames at the top if there are any.
736 javaVFrame*
737 JvmtiEnvBase::get_cthread_last_java_vframe(JavaThread* jt, RegisterMap* reg_map_p) {
738 // Strip vthread frames in case of carrier thread with mounted continuation.
739 bool cthread_with_cont = JvmtiEnvBase::is_cthread_with_continuation(jt);
740 javaVFrame *jvf = cthread_with_cont ? jt->carrier_last_java_vframe(reg_map_p)
741 : jt->last_java_vframe(reg_map_p);
742
743 // Skip hidden frames for carrier threads only.
744 jvf = check_and_skip_hidden_frames(jt, jvf);
745 return jvf;
746 }
747
748 jint
749 JvmtiEnvBase::get_thread_state_base(oop thread_oop, JavaThread* jt) {
750 jint state = 0;
751
752 if (thread_oop != nullptr) {
753 // Get most state bits.
754 state = (jint)java_lang_Thread::get_thread_status(thread_oop);
755 }
756 if (jt != nullptr) {
757 // We have a JavaThread* so add more state bits.
758 JavaThreadState jts = jt->thread_state();
759
760 if (jt->is_carrier_thread_suspended() ||
761 ((jt->jvmti_vthread() == nullptr || jt->jvmti_vthread() == thread_oop) && jt->is_suspended())) {
762 // Suspended non-virtual thread.
763 state |= JVMTI_THREAD_STATE_SUSPENDED;
764 }
765 if (jts == _thread_in_native) {
766 state |= JVMTI_THREAD_STATE_IN_NATIVE;
767 }
768 if (jt->is_interrupted(false)) {
769 state |= JVMTI_THREAD_STATE_INTERRUPTED;
770 }
771 }
772 return state;
773 }
774
775 jint
776 JvmtiEnvBase::get_thread_state(oop thread_oop, JavaThread* jt) {
777 jint state = 0;
778
779 if (is_thread_carrying_vthread(jt, thread_oop)) {
780 state = (jint)java_lang_Thread::get_thread_status(thread_oop);
781
782 // This is for extra safety. Other bits are not expected nor needed.
783 state &= (JVMTI_THREAD_STATE_ALIVE | JVMTI_THREAD_STATE_INTERRUPTED);
784
785 if (jt->is_carrier_thread_suspended()) {
786 state |= JVMTI_THREAD_STATE_SUSPENDED;
787 }
788 // It's okay for the JVMTI state to be reported as WAITING when waiting
789 // for something other than an Object.wait. So, we treat a thread carrying
790 // a virtual thread as waiting indefinitely which is not runnable.
791 // It is why the RUNNABLE bit is not needed and the WAITING bits are added.
792 state |= JVMTI_THREAD_STATE_WAITING | JVMTI_THREAD_STATE_WAITING_INDEFINITELY;
793 } else {
794 state = get_thread_state_base(thread_oop, jt);
795 }
796 return state;
797 }
798
799 jint
800 JvmtiEnvBase::get_vthread_state(oop thread_oop, JavaThread* java_thread) {
801 jint state = 0;
802 bool ext_suspended = JvmtiVTSuspender::is_vthread_suspended(thread_oop);
803 jint interrupted = java_lang_Thread::interrupted(thread_oop);
804
805 if (java_thread != nullptr) {
806 // If virtual thread is blocked on a monitor enter the BLOCKED_ON_MONITOR_ENTER bit
807 // is set for carrier thread instead of virtual.
808 // Other state bits except filtered ones are expected to be the same.
809 oop ct_oop = java_lang_VirtualThread::carrier_thread(thread_oop);
810 jint filtered_bits = JVMTI_THREAD_STATE_SUSPENDED | JVMTI_THREAD_STATE_INTERRUPTED;
811
812 // This call can trigger a safepoint, so thread_oop must not be used after it.
813 state = get_thread_state_base(ct_oop, java_thread) & ~filtered_bits;
814 } else {
815 int vt_state = java_lang_VirtualThread::state(thread_oop);
816 state = (jint)java_lang_VirtualThread::map_state_to_thread_status(vt_state);
817 }
818 // Ensure the thread has not exited after retrieving suspended/interrupted values.
819 if ((state & JVMTI_THREAD_STATE_ALIVE) != 0) {
820 if (ext_suspended) {
821 state |= JVMTI_THREAD_STATE_SUSPENDED;
822 }
823 if (interrupted) {
824 state |= JVMTI_THREAD_STATE_INTERRUPTED;
825 }
826 }
827 return state;
828 }
829
830 jint
831 JvmtiEnvBase::get_thread_or_vthread_state(oop thread_oop, JavaThread* java_thread) {
832 jint state = 0;
833 if (java_lang_VirtualThread::is_instance(thread_oop)) {
834 state = JvmtiEnvBase::get_vthread_state(thread_oop, java_thread);
835 } else {
836 state = JvmtiEnvBase::get_thread_state(thread_oop, java_thread);
837 }
838 return state;
839 }
840
841 jvmtiError
842 JvmtiEnvBase::get_live_threads(JavaThread* current_thread, Handle group_hdl, jint *count_ptr, Handle **thread_objs_p) {
843 jint count = 0;
844 Handle *thread_objs = nullptr;
845 ThreadsListEnumerator tle(current_thread, /* include_jvmti_agent_threads */ true);
846 int nthreads = tle.num_threads();
847 if (nthreads > 0) {
848 thread_objs = NEW_RESOURCE_ARRAY_RETURN_NULL(Handle, nthreads);
849 NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
850 for (int i = 0; i < nthreads; i++) {
851 Handle thread = tle.get_threadObj(i);
852 if (thread()->is_a(vmClasses::Thread_klass()) && java_lang_Thread::threadGroup(thread()) == group_hdl()) {
853 thread_objs[count++] = thread;
854 }
855 }
856 }
857 *thread_objs_p = thread_objs;
858 *count_ptr = count;
859 return JVMTI_ERROR_NONE;
860 }
861
862 jvmtiError
863 JvmtiEnvBase::get_subgroups(JavaThread* current_thread, Handle group_hdl, jint *count_ptr, refArrayHandle *group_objs_p) {
864
865 // This call collects the strong and weak groups
866 JavaThread* THREAD = current_thread;
867 JvmtiJavaUpcallMark jjum(current_thread); // hide JVMTI events for Java upcall
868 JavaValue result(T_OBJECT);
869 JavaCalls::call_virtual(&result,
870 group_hdl,
871 vmClasses::ThreadGroup_klass(),
872 SymbolTable::new_permanent_symbol("subgroupsAsArray"),
873 vmSymbols::void_threadgroup_array_signature(),
874 THREAD);
875 if (HAS_PENDING_EXCEPTION) {
876 Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
877 CLEAR_PENDING_EXCEPTION;
878 if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
879 return JVMTI_ERROR_OUT_OF_MEMORY;
880 } else {
881 return JVMTI_ERROR_INTERNAL;
882 }
883 }
884
885 assert(result.get_type() == T_OBJECT, "just checking");
886 refArrayOop groups = (refArrayOop)result.get_oop();
887
888 *count_ptr = groups == nullptr ? 0 : groups->length();
889 *group_objs_p = refArrayHandle(current_thread, groups);
890
891 return JVMTI_ERROR_NONE;
892 }
893
894 //
895 // Object Monitor Information
896 //
897
898 //
899 // Count the number of objects for a lightweight monitor. The hobj
900 // parameter is object that owns the monitor so this routine will
901 // count the number of times the same object was locked by frames
902 // in java_thread.
903 //
904 jint
905 JvmtiEnvBase::count_locked_objects(JavaThread *java_thread, Handle hobj) {
906 jint ret = 0;
907 if (!java_thread->has_last_Java_frame()) {
908 return ret; // no Java frames so no monitors
909 }
910
911 Thread* current_thread = Thread::current();
912 ResourceMark rm(current_thread);
913 HandleMark hm(current_thread);
914 RegisterMap reg_map(java_thread,
915 RegisterMap::UpdateMap::include,
916 RegisterMap::ProcessFrames::include,
917 RegisterMap::WalkContinuation::skip);
918
919 for (javaVFrame *jvf = java_thread->last_java_vframe(®_map); jvf != nullptr;
920 jvf = jvf->java_sender()) {
921 GrowableArray<MonitorInfo*>* mons = jvf->monitors();
922 if (!mons->is_empty()) {
923 for (int i = 0; i < mons->length(); i++) {
924 MonitorInfo *mi = mons->at(i);
925 if (mi->owner_is_scalar_replaced()) continue;
926
927 // see if owner of the monitor is our object
928 if (mi->owner() != nullptr && mi->owner() == hobj()) {
929 ret++;
930 }
931 }
932 }
933 }
934 return ret;
935 }
936
937 jvmtiError
938 JvmtiEnvBase::get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread,
939 jobject *monitor_ptr, bool is_virtual) {
940 Thread *current_thread = Thread::current();
941 assert(java_thread->is_handshake_safe_for(current_thread),
942 "call by myself or at handshake");
943 if (!is_virtual && JvmtiEnvBase::is_cthread_with_continuation(java_thread)) {
944 // Carrier thread with a mounted continuation case.
945 // No contended monitor can be owned by carrier thread in this case.
946 *monitor_ptr = nullptr;
947 return JVMTI_ERROR_NONE;
948 }
949 oop obj = nullptr;
950 // The ObjectMonitor* can't be async deflated since we are either
951 // at a safepoint or the calling thread is operating on itself so
952 // it cannot leave the underlying wait()/enter() call.
953 ObjectMonitor *mon = java_thread->current_waiting_monitor();
954 if (mon == nullptr) {
955 // thread is not doing an Object.wait() call
956 mon = java_thread->current_pending_monitor();
957 if (mon != nullptr) {
958 // The thread is trying to enter() an ObjectMonitor.
959 obj = mon->object();
960 assert(obj != nullptr, "ObjectMonitor should have a valid object!");
961 }
962 } else {
963 // thread is doing an Object.wait() call
964 oop thread_oop = get_vthread_or_thread_oop(java_thread);
965 jint state = get_thread_or_vthread_state(thread_oop, java_thread);
966
967 if (state & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) {
968 // thread is re-entering the monitor in an Object.wait() call
969 obj = mon->object();
970 assert(obj != nullptr, "Object.wait() should have an object");
971 }
972 }
973
974 if (obj == nullptr) {
975 *monitor_ptr = nullptr;
976 } else {
977 HandleMark hm(current_thread);
978 Handle hobj(current_thread, obj);
979 *monitor_ptr = jni_reference(calling_thread, hobj);
980 }
981 return JVMTI_ERROR_NONE;
982 }
983
984 jvmtiError
985 JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
986 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
987 // Note:
988 // calling_thread is the thread that requested the list of monitors for java_thread.
989 // java_thread is the thread owning the monitors.
990 // current_thread is the thread executing this code, can be a non-JavaThread (e.g. VM Thread).
991 // And they all may be different threads.
992 jvmtiError err = JVMTI_ERROR_NONE;
993 Thread *current_thread = Thread::current();
994 assert(java_thread->is_handshake_safe_for(current_thread),
995 "call by myself or at handshake");
996
997 if (JvmtiEnvBase::is_cthread_with_continuation(java_thread)) {
998 // Carrier thread with a mounted continuation case.
999 // No contended monitor can be owned by carrier thread in this case.
1000 return JVMTI_ERROR_NONE;
1001 }
1002 if (java_thread->has_last_Java_frame()) {
1003 ResourceMark rm(current_thread);
1004 HandleMark hm(current_thread);
1005 RegisterMap reg_map(java_thread,
1006 RegisterMap::UpdateMap::include,
1007 RegisterMap::ProcessFrames::include,
1008 RegisterMap::WalkContinuation::skip);
1009
1010 int depth = 0;
1011 for (javaVFrame *jvf = get_cthread_last_java_vframe(java_thread, ®_map);
1012 jvf != nullptr; jvf = jvf->java_sender()) {
1013 if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) { // check for stack too deep
1014 // add locked objects for this frame into list
1015 err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
1016 if (err != JVMTI_ERROR_NONE) {
1017 return err;
1018 }
1019 }
1020 }
1021 }
1022
1023 // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
1024 JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
1025 ObjectSynchronizer::owned_monitors_iterate(&jmc, java_thread);
1026 err = jmc.error();
1027
1028 return err;
1029 }
1030
1031 jvmtiError
1032 JvmtiEnvBase::get_owned_monitors(JavaThread* calling_thread, JavaThread* carrier, javaVFrame* jvf,
1033 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list, oop vthread) {
1034 jvmtiError err = JVMTI_ERROR_NONE;
1035 Thread *current_thread = Thread::current();
1036 assert(carrier == nullptr || carrier->is_handshake_safe_for(current_thread),
1037 "call by myself or at handshake");
1038
1039 int depth = 0;
1040 for ( ; jvf != nullptr; jvf = jvf->java_sender()) {
1041 if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) { // check for stack too deep
1042 // Add locked objects for this frame into list.
1043 err = get_locked_objects_in_frame(calling_thread, carrier, jvf, owned_monitors_list, depth - 1, vthread);
1044 if (err != JVMTI_ERROR_NONE) {
1045 return err;
1046 }
1047 }
1048 }
1049
1050 // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
1051 JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
1052 ObjectSynchronizer::owned_monitors_iterate(&jmc, carrier != nullptr ? carrier->threadObj() : vthread);
1053 err = jmc.error();
1054
1055 return err;
1056 }
1057
1058 // Save JNI local handles for any objects that this frame owns.
1059 jvmtiError
1060 JvmtiEnvBase::get_locked_objects_in_frame(JavaThread* calling_thread, JavaThread* target,
1061 javaVFrame *jvf, GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list,
1062 jint stack_depth, oop vthread) {
1063 jvmtiError err = JVMTI_ERROR_NONE;
1064 Thread* current_thread = Thread::current();
1065 ResourceMark rm(current_thread);
1066 HandleMark hm(current_thread);
1067
1068 GrowableArray<MonitorInfo*>* mons = jvf->monitors();
1069 if (mons->is_empty()) {
1070 return err; // this javaVFrame holds no monitors
1071 }
1072
1073 oop wait_obj = nullptr;
1074 {
1075 // The ObjectMonitor* can't be async deflated since we are either
1076 // at a safepoint or the calling thread is operating on itself so
1077 // it cannot leave the underlying wait() call.
1078 // Save object of current wait() call (if any) for later comparison.
1079 if (target != nullptr) {
1080 ObjectMonitor *mon = target->current_waiting_monitor();
1081 if (mon != nullptr) wait_obj = mon->object();
1082 } else {
1083 ObjectMonitor *mon = java_lang_VirtualThread::current_waiting_monitor(vthread);
1084 if (mon != nullptr) wait_obj = mon->object();
1085 }
1086 }
1087 oop pending_obj = nullptr;
1088 {
1089 // The ObjectMonitor* can't be async deflated since we are either
1090 // at a safepoint or the calling thread is operating on itself so
1091 // it cannot leave the underlying enter() call.
1092 // Save object of current enter() call (if any) for later comparison.
1093 if (target != nullptr) {
1094 ObjectMonitor *mon = target->current_pending_monitor();
1095 if (mon != nullptr) pending_obj = mon->object();
1096 } else {
1097 ObjectMonitor *mon = java_lang_VirtualThread::current_pending_monitor(vthread);
1098 if (mon != nullptr) pending_obj = mon->object();
1099 }
1100 }
1101
1102 for (int i = 0; i < mons->length(); i++) {
1103 MonitorInfo *mi = mons->at(i);
1104
1105 if (mi->owner_is_scalar_replaced()) continue;
1106
1107 oop obj = mi->owner();
1108 if (obj == nullptr) {
1109 // this monitor doesn't have an owning object so skip it
1110 continue;
1111 }
1112
1113 if (wait_obj == obj) {
1114 // the thread is waiting on this monitor so it isn't really owned
1115 continue;
1116 }
1117
1118 if (pending_obj == obj) {
1119 // the thread is pending on this monitor so it isn't really owned
1120 continue;
1121 }
1122
1123 if (owned_monitors_list->length() > 0) {
1124 // Our list has at least one object on it so we have to check
1125 // for recursive object locking
1126 bool found = false;
1127 for (int j = 0; j < owned_monitors_list->length(); j++) {
1128 jobject jobj = ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(j))->monitor;
1129 oop check = JNIHandles::resolve(jobj);
1130 if (check == obj) {
1131 found = true; // we found the object
1132 break;
1133 }
1134 }
1135
1136 if (found) {
1137 // already have this object so don't include it
1138 continue;
1139 }
1140 }
1141
1142 // add the owning object to our list
1143 jvmtiMonitorStackDepthInfo *jmsdi;
1144 err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
1145 if (err != JVMTI_ERROR_NONE) {
1146 return err;
1147 }
1148 Handle hobj(Thread::current(), obj);
1149 jmsdi->monitor = jni_reference(calling_thread, hobj);
1150 jmsdi->stack_depth = stack_depth;
1151 owned_monitors_list->append(jmsdi);
1152 }
1153
1154 return err;
1155 }
1156
1157 jvmtiError
1158 JvmtiEnvBase::get_stack_trace(javaVFrame *jvf,
1159 jint start_depth, jint max_count,
1160 jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1161 Thread *current_thread = Thread::current();
1162 ResourceMark rm(current_thread);
1163 HandleMark hm(current_thread);
1164 int count = 0;
1165
1166 if (start_depth != 0) {
1167 if (start_depth > 0) {
1168 for (int j = 0; j < start_depth && jvf != nullptr; j++) {
1169 jvf = jvf->java_sender();
1170 }
1171 if (jvf == nullptr) {
1172 // start_depth is deeper than the stack depth.
1173 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1174 }
1175 } else { // start_depth < 0
1176 // We are referencing the starting depth based on the oldest
1177 // part of the stack.
1178 // Optimize to limit the number of times that java_sender() is called.
1179 javaVFrame *jvf_cursor = jvf;
1180 javaVFrame *jvf_prev = nullptr;
1181 javaVFrame *jvf_prev_prev = nullptr;
1182 int j = 0;
1183 while (jvf_cursor != nullptr) {
1184 jvf_prev_prev = jvf_prev;
1185 jvf_prev = jvf_cursor;
1186 for (j = 0; j > start_depth && jvf_cursor != nullptr; j--) {
1187 jvf_cursor = jvf_cursor->java_sender();
1188 }
1189 }
1190 if (j == start_depth) {
1191 // Previous pointer is exactly where we want to start.
1192 jvf = jvf_prev;
1193 } else {
1194 // We need to back up further to get to the right place.
1195 if (jvf_prev_prev == nullptr) {
1196 // The -start_depth is greater than the stack depth.
1197 return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1198 }
1199 // j is now the number of frames on the stack starting with
1200 // jvf_prev, we start from jvf_prev_prev and move older on
1201 // the stack that many, and the result is -start_depth frames
1202 // remaining.
1203 jvf = jvf_prev_prev;
1204 for (; j < 0; j++) {
1205 jvf = jvf->java_sender();
1206 }
1207 }
1208 }
1209 }
1210 for (; count < max_count && jvf != nullptr; count++) {
1211 frame_buffer[count].method = jvf->method()->jmethod_id();
1212 frame_buffer[count].location = (jvf->method()->is_native() ? -1 : jvf->bci());
1213 jvf = jvf->java_sender();
1214 }
1215 *count_ptr = count;
1216 return JVMTI_ERROR_NONE;
1217 }
1218
1219 jvmtiError
1220 JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
1221 jint start_depth, jint max_count,
1222 jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1223 Thread *current_thread = Thread::current();
1224 assert(SafepointSynchronize::is_at_safepoint() ||
1225 java_thread->is_handshake_safe_for(current_thread),
1226 "call by myself / at safepoint / at handshake");
1227 int count = 0;
1228 jvmtiError err = JVMTI_ERROR_NONE;
1229
1230 if (java_thread->has_last_Java_frame()) {
1231 RegisterMap reg_map(java_thread,
1232 RegisterMap::UpdateMap::include,
1233 RegisterMap::ProcessFrames::skip,
1234 RegisterMap::WalkContinuation::skip);
1235 ResourceMark rm(current_thread);
1236 javaVFrame *jvf = get_cthread_last_java_vframe(java_thread, ®_map);
1237
1238 err = get_stack_trace(jvf, start_depth, max_count, frame_buffer, count_ptr);
1239 } else {
1240 *count_ptr = 0;
1241 if (start_depth != 0) {
1242 // no frames and there is a starting depth
1243 err = JVMTI_ERROR_ILLEGAL_ARGUMENT;
1244 }
1245 }
1246 return err;
1247 }
1248
1249 jint
1250 JvmtiEnvBase::get_frame_count(javaVFrame *jvf) {
1251 int count = 0;
1252
1253 while (jvf != nullptr) {
1254 jvf = jvf->java_sender();
1255 count++;
1256 }
1257 return count;
1258 }
1259
1260 jvmtiError
1261 JvmtiEnvBase::get_frame_count(JavaThread* jt, jint *count_ptr) {
1262 Thread *current_thread = Thread::current();
1263 assert(current_thread == jt ||
1264 SafepointSynchronize::is_at_safepoint() ||
1265 jt->is_handshake_safe_for(current_thread),
1266 "call by myself / at safepoint / at handshake");
1267
1268 if (!jt->has_last_Java_frame()) { // no Java frames
1269 *count_ptr = 0;
1270 } else {
1271 ResourceMark rm(current_thread);
1272 RegisterMap reg_map(jt,
1273 RegisterMap::UpdateMap::include,
1274 RegisterMap::ProcessFrames::include,
1275 RegisterMap::WalkContinuation::skip);
1276 javaVFrame *jvf = get_cthread_last_java_vframe(jt, ®_map);
1277
1278 *count_ptr = get_frame_count(jvf);
1279 }
1280 return JVMTI_ERROR_NONE;
1281 }
1282
1283 jvmtiError
1284 JvmtiEnvBase::get_frame_count(oop vthread_oop, jint *count_ptr) {
1285 Thread *current_thread = Thread::current();
1286 ResourceMark rm(current_thread);
1287 javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(vthread_oop);
1288
1289 *count_ptr = get_frame_count(jvf);
1290 return JVMTI_ERROR_NONE;
1291 }
1292
1293 jvmtiError
1294 JvmtiEnvBase::get_frame_location(javaVFrame* jvf, jint depth,
1295 jmethodID* method_ptr, jlocation* location_ptr) {
1296 int cur_depth = 0;
1297
1298 while (jvf != nullptr && cur_depth < depth) {
1299 jvf = jvf->java_sender();
1300 cur_depth++;
1301 }
1302 assert(depth >= cur_depth, "ran out of frames too soon");
1303 if (jvf == nullptr) {
1304 return JVMTI_ERROR_NO_MORE_FRAMES;
1305 }
1306 Method* method = jvf->method();
1307 if (method->is_native()) {
1308 *location_ptr = -1;
1309 } else {
1310 *location_ptr = jvf->bci();
1311 }
1312 *method_ptr = method->jmethod_id();
1313 return JVMTI_ERROR_NONE;
1314 }
1315
1316 jvmtiError
1317 JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth,
1318 jmethodID* method_ptr, jlocation* location_ptr) {
1319 Thread* current = Thread::current();
1320 assert(java_thread->is_handshake_safe_for(current),
1321 "call by myself or at handshake");
1322 if (!java_thread->has_last_Java_frame()) {
1323 return JVMTI_ERROR_NO_MORE_FRAMES;
1324 }
1325 ResourceMark rm(current);
1326 HandleMark hm(current);
1327 RegisterMap reg_map(java_thread,
1328 RegisterMap::UpdateMap::include,
1329 RegisterMap::ProcessFrames::skip,
1330 RegisterMap::WalkContinuation::include);
1331 javaVFrame* jvf = JvmtiEnvBase::get_cthread_last_java_vframe(java_thread, ®_map);
1332
1333 return get_frame_location(jvf, depth, method_ptr, location_ptr);
1334 }
1335
1336 jvmtiError
1337 JvmtiEnvBase::get_frame_location(oop vthread_oop, jint depth,
1338 jmethodID* method_ptr, jlocation* location_ptr) {
1339 Thread* current = Thread::current();
1340 ResourceMark rm(current);
1341 HandleMark hm(current);
1342 javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(vthread_oop);
1343
1344 return get_frame_location(jvf, depth, method_ptr, location_ptr);
1345 }
1346
1347 jvmtiError
1348 JvmtiEnvBase::set_frame_pop(JvmtiThreadState* state, javaVFrame* jvf, jint depth) {
1349 for (int d = 0; jvf != nullptr && d < depth; d++) {
1350 jvf = jvf->java_sender();
1351 }
1352 if (jvf == nullptr) {
1353 return JVMTI_ERROR_NO_MORE_FRAMES;
1354 }
1355 if (jvf->method()->is_native() ||
1356 (depth == 0 && state->top_frame_is_exiting()) ||
1357 (state->is_virtual() && jvf->method()->jvmti_hide_events())) {
1358 return JVMTI_ERROR_OPAQUE_FRAME;
1359 }
1360 assert(jvf->frame_pointer() != nullptr, "frame pointer mustn't be null");
1361 int frame_number = (int)get_frame_count(jvf);
1362 JvmtiEnvThreadState* ets = state->env_thread_state(this);
1363 if (ets->is_frame_pop(frame_number)) {
1364 return JVMTI_ERROR_DUPLICATE;
1365 }
1366 ets->set_frame_pop(frame_number);
1367 return JVMTI_ERROR_NONE;
1368 }
1369
1370 jvmtiError
1371 JvmtiEnvBase::clear_all_frame_pops(JvmtiThreadState* state) {
1372 JvmtiEnvThreadState* ets = state->env_thread_state(this);
1373 ets->clear_all_frame_pops();
1374 return JVMTI_ERROR_NONE;
1375 }
1376
1377 bool
1378 JvmtiEnvBase::is_cthread_with_mounted_vthread(JavaThread* jt) {
1379 oop thread_oop = jt->threadObj();
1380 assert(thread_oop != nullptr, "sanity check");
1381 oop mounted_vt = jt->jvmti_vthread();
1382
1383 return mounted_vt != nullptr && mounted_vt != thread_oop;
1384 }
1385
1386 bool
1387 JvmtiEnvBase::is_cthread_with_continuation(JavaThread* jt) {
1388 const ContinuationEntry* cont_entry = nullptr;
1389 if (jt->has_last_Java_frame()) {
1390 cont_entry = jt->vthread_continuation();
1391 }
1392 return cont_entry != nullptr && is_cthread_with_mounted_vthread(jt);
1393 }
1394
1395 // Check if VirtualThread or BoundVirtualThread is suspended.
1396 bool
1397 JvmtiEnvBase::is_vthread_suspended(oop vt_oop, JavaThread* jt) {
1398 bool suspended = false;
1399 if (java_lang_VirtualThread::is_instance(vt_oop)) {
1400 suspended = JvmtiVTSuspender::is_vthread_suspended(vt_oop);
1401 } else if (vt_oop->is_a(vmClasses::BoundVirtualThread_klass())) {
1402 suspended = jt->is_suspended();
1403 }
1404 return suspended;
1405 }
1406
1407 // If (thread == null) then return current thread object.
1408 // Otherwise return JNIHandles::resolve_external_guard(thread).
1409 oop
1410 JvmtiEnvBase::current_thread_obj_or_resolve_external_guard(jthread thread) {
1411 oop thread_obj = JNIHandles::resolve_external_guard(thread);
1412 if (thread == nullptr) {
1413 thread_obj = get_vthread_or_thread_oop(JavaThread::current());
1414 }
1415 return thread_obj;
1416 }
1417
1418 jvmtiError
1419 JvmtiEnvBase::get_threadOop_and_JavaThread(ThreadsList* t_list, jthread thread, JavaThread* cur_thread,
1420 JavaThread** jt_pp, oop* thread_oop_p) {
1421 JavaThread* java_thread = nullptr;
1422 oop thread_oop = nullptr;
1423
1424 if (thread == nullptr) {
1425 if (cur_thread == nullptr) { // cur_thread can be null when called from a VM_op
1426 return JVMTI_ERROR_INVALID_THREAD;
1427 }
1428 java_thread = cur_thread;
1429 thread_oop = get_vthread_or_thread_oop(java_thread);
1430 if (thread_oop == nullptr || !thread_oop->is_a(vmClasses::Thread_klass())) {
1431 return JVMTI_ERROR_INVALID_THREAD;
1432 }
1433 } else {
1434 jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(t_list, thread, &java_thread, &thread_oop);
1435 if (err != JVMTI_ERROR_NONE) {
1436 // We got an error code so we don't have a JavaThread*, but only return
1437 // an error from here if we didn't get a valid thread_oop. In a vthread case
1438 // the cv_external_thread_to_JavaThread is expected to correctly set the
1439 // thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here.
1440 if (thread_oop == nullptr || err != JVMTI_ERROR_INVALID_THREAD) {
1441 *thread_oop_p = thread_oop;
1442 return err;
1443 }
1444 }
1445 if (java_thread == nullptr && java_lang_VirtualThread::is_instance(thread_oop)) {
1446 java_thread = get_JavaThread_or_null(thread_oop);
1447 }
1448 }
1449 *jt_pp = java_thread;
1450 *thread_oop_p = thread_oop;
1451 if (java_lang_VirtualThread::is_instance(thread_oop) &&
1452 !JvmtiEnvBase::is_vthread_alive(thread_oop)) {
1453 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1454 }
1455 return JVMTI_ERROR_NONE;
1456 }
1457
1458 // Check for JVMTI_ERROR_NOT_SUSPENDED and JVMTI_ERROR_OPAQUE_FRAME errors.
1459 // Used in PopFrame and ForceEarlyReturn implementations.
1460 jvmtiError
1461 JvmtiEnvBase::check_non_suspended_or_opaque_frame(JavaThread* jt, oop thr_obj, bool self) {
1462 bool is_virtual = thr_obj != nullptr && thr_obj->is_a(vmClasses::BaseVirtualThread_klass());
1463
1464 if (is_virtual) {
1465 if (!is_JavaThread_current(jt, thr_obj)) {
1466 if (!is_vthread_suspended(thr_obj, jt)) {
1467 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1468 }
1469 if (jt == nullptr) { // unmounted virtual thread
1470 return JVMTI_ERROR_OPAQUE_FRAME;
1471 }
1472 }
1473 } else { // platform thread
1474 if (!self && !jt->is_suspended() &&
1475 !jt->is_carrier_thread_suspended()) {
1476 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1477 }
1478 }
1479 return JVMTI_ERROR_NONE;
1480 }
1481
1482 jvmtiError
1483 JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
1484 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1485 Thread* current_thread = VMThread::vm_thread();
1486 assert(current_thread == Thread::current(), "must be");
1487
1488 HandleMark hm(current_thread);
1489 Handle hobj;
1490
1491 // Check arguments
1492 {
1493 oop mirror = JNIHandles::resolve_external_guard(object);
1494 NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
1495 NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
1496
1497 hobj = Handle(current_thread, mirror);
1498 }
1499
1500 ThreadsListHandle tlh(current_thread);
1501 JavaThread *owning_thread = nullptr;
1502 jvmtiMonitorUsage ret = {
1503 nullptr, 0, 0, nullptr, 0, nullptr
1504 };
1505
1506 uint32_t debug_bits = 0;
1507 // first derive the object's owner and entry_count (if any)
1508 owning_thread = ObjectSynchronizer::get_lock_owner(tlh.list(), hobj);
1509 if (owning_thread != nullptr) {
1510 oop thread_oop = get_vthread_or_thread_oop(owning_thread);
1511 bool is_virtual = thread_oop->is_a(vmClasses::BaseVirtualThread_klass());
1512 if (is_virtual) {
1513 thread_oop = nullptr;
1514 }
1515 Handle th(current_thread, thread_oop);
1516 ret.owner = (jthread)jni_reference(calling_thread, th);
1517
1518 // The recursions field of a monitor does not reflect recursions
1519 // as lightweight locks before inflating the monitor are not included.
1520 // We have to count the number of recursive monitor entries the hard way.
1521 // We pass a handle to survive any GCs along the way.
1522 ret.entry_count = is_virtual ? 0 : count_locked_objects(owning_thread, hobj);
1523 }
1524 // implied else: entry_count == 0
1525
1526 jint nWant = 0, nWait = 0;
1527 markWord mark = hobj->mark();
1528 ResourceMark rm(current_thread);
1529 GrowableArray<JavaThread*>* wantList = nullptr;
1530
1531 ObjectMonitor* mon = mark.has_monitor()
1532 ? ObjectSynchronizer::read_monitor(hobj(), mark)
1533 : nullptr;
1534
1535 if (mon != nullptr) {
1536 // this object has a heavyweight monitor
1537 nWant = mon->contentions(); // # of threads contending for monitor entry, but not re-entry
1538 nWait = mon->waiters(); // # of threads waiting for notification,
1539 // or to re-enter monitor, in Object.wait()
1540
1541 // Get the actual set of threads trying to enter, or re-enter, the monitor.
1542 wantList = Threads::get_pending_threads(tlh.list(), nWant + nWait, (address)mon);
1543 nWant = wantList->length();
1544 } else {
1545 // this object has a lightweight monitor
1546 }
1547
1548 jint skipped = 0;
1549 if (mon != nullptr) {
1550 // Robustness: the actual waiting list can be smaller.
1551 // The nWait count we got from the mon->waiters() may include the re-entering
1552 // the monitor threads after being notified. Here we are correcting the actual
1553 // number of the waiting threads by excluding those re-entering the monitor.
1554 nWait = 0;
1555 for (ObjectWaiter* waiter = mon->first_waiter();
1556 waiter != nullptr && (nWait == 0 || waiter != mon->first_waiter());
1557 waiter = mon->next_waiter(waiter)) {
1558 JavaThread *w = mon->thread_of_waiter(waiter);
1559 if (w == nullptr) {
1560 skipped++;
1561 } else {
1562 oop thread_oop = get_vthread_or_thread_oop(w);
1563 if (thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
1564 skipped++;
1565 }
1566 }
1567 nWait++;
1568 }
1569 }
1570 ret.waiter_count = nWant;
1571 ret.notify_waiter_count = nWait - skipped;
1572
1573 // Allocate memory for heavyweight and lightweight monitor.
1574 jvmtiError err;
1575 err = allocate(ret.waiter_count * sizeof(jthread *), (unsigned char**)&ret.waiters);
1576 if (err != JVMTI_ERROR_NONE) {
1577 return err;
1578 }
1579 err = allocate(ret.notify_waiter_count * sizeof(jthread *),
1580 (unsigned char**)&ret.notify_waiters);
1581 if (err != JVMTI_ERROR_NONE) {
1582 deallocate((unsigned char*)ret.waiters);
1583 return err;
1584 }
1585
1586 // now derive the rest of the fields
1587 if (mon != nullptr) {
1588 // this object has a heavyweight monitor
1589
1590 // null out memory for robustness
1591 if (ret.waiters != nullptr) {
1592 memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *));
1593 }
1594 if (ret.notify_waiters != nullptr) {
1595 memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *));
1596 }
1597
1598 if (ret.waiter_count > 0) { // we have contending threads waiting to enter/re-enter the monitor
1599 // identify threads waiting to enter and re-enter the monitor
1600 // get_pending_threads returns only java thread so we do not need to
1601 // check for non java threads.
1602 for (int i = 0; i < nWant; i++) {
1603 JavaThread *pending_thread = wantList->at(i);
1604 Handle th(current_thread, get_vthread_or_thread_oop(pending_thread));
1605 ret.waiters[i] = (jthread)jni_reference(calling_thread, th);
1606 }
1607 }
1608 if (ret.notify_waiter_count > 0) { // we have threads waiting to be notified in Object.wait()
1609 ObjectWaiter *waiter = mon->first_waiter();
1610 jint skipped = 0;
1611 for (int i = 0; i < nWait; i++) {
1612 JavaThread *w = mon->thread_of_waiter(waiter);
1613 bool is_virtual;
1614 if (w == nullptr) {
1615 is_virtual = true;
1616 } else {
1617 oop thread_oop = get_vthread_or_thread_oop(w);
1618 is_virtual = thread_oop->is_a(vmClasses::BaseVirtualThread_klass());
1619 }
1620 if (is_virtual) {
1621 skipped++;
1622 } else {
1623 // If the thread was found on the ObjectWaiter list, then
1624 // it has not been notified.
1625 Handle th(current_thread, w->threadObj());
1626 ret.notify_waiters[i - skipped] = (jthread)jni_reference(calling_thread, th);
1627 }
1628 waiter = mon->next_waiter(waiter);
1629 }
1630 }
1631 } else {
1632 // this object has a lightweight monitor and we have nothing more
1633 // to do here because the defaults are just fine.
1634 }
1635
1636 // we don't update return parameter unless everything worked
1637 *info_ptr = ret;
1638
1639 return JVMTI_ERROR_NONE;
1640 }
1641
1642 jvmtiError
1643 JvmtiEnvBase::check_thread_list(jint count, const jthread* list) {
1644 if (list == nullptr && count != 0) {
1645 return JVMTI_ERROR_NULL_POINTER;
1646 }
1647 for (int i = 0; i < count; i++) {
1648 jthread thread = list[i];
1649 oop thread_oop = JNIHandles::resolve_external_guard(thread);
1650 if (thread_oop == nullptr || !thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
1651 return JVMTI_ERROR_INVALID_THREAD;
1652 }
1653 }
1654 return JVMTI_ERROR_NONE;
1655 }
1656
1657 bool
1658 JvmtiEnvBase::is_in_thread_list(jint count, const jthread* list, oop jt_oop) {
1659 for (int idx = 0; idx < count; idx++) {
1660 jthread thread = list[idx];
1661 oop thread_oop = JNIHandles::resolve_external_guard(thread);
1662 if (thread_oop == jt_oop) {
1663 return true;
1664 }
1665 }
1666 return false;
1667 }
1668
1669 class VM_SetNotifyJvmtiEventsMode : public VM_Operation {
1670 private:
1671 bool _enable;
1672
1673 static void correct_jvmti_thread_state(JavaThread* jt) {
1674 oop ct_oop = jt->threadObj();
1675 oop vt_oop = jt->vthread();
1676 JvmtiThreadState* jt_state = jt->jvmti_thread_state();
1677 JvmtiThreadState* ct_state = java_lang_Thread::jvmti_thread_state(jt->threadObj());
1678 JvmtiThreadState* vt_state = vt_oop != nullptr ? java_lang_Thread::jvmti_thread_state(vt_oop) : nullptr;
1679 bool virt = vt_oop != nullptr && java_lang_VirtualThread::is_instance(vt_oop);
1680
1681 // Correct jt->jvmti_thread_state() and jt->jvmti_vthread().
1682 // It was not maintained while notifyJvmti was disabled.
1683 if (virt) {
1684 jt->set_jvmti_thread_state(nullptr); // reset jt->jvmti_thread_state()
1685 jt->set_jvmti_vthread(vt_oop); // restore jt->jvmti_vthread()
1686 } else {
1687 jt->set_jvmti_thread_state(ct_state); // restore jt->jvmti_thread_state()
1688 jt->set_jvmti_vthread(ct_oop); // restore jt->jvmti_vthread()
1689 }
1690 }
1691
1692 // This function is called only if _enable == true.
1693 // Iterates over all JavaThread's, restores jt->jvmti_thread_state() and
1694 // jt->jvmti_vthread() for VTMS transition protocol.
1695 void correct_jvmti_thread_states() {
1696 for (JavaThread* jt : ThreadsListHandle()) {
1697 if (jt->is_in_vthread_transition()) {
1698 continue; // no need in JvmtiThreadState correction below if in transition
1699 }
1700 correct_jvmti_thread_state(jt);
1701 }
1702 }
1703
1704 public:
1705 VMOp_Type type() const { return VMOp_SetNotifyJvmtiEventsMode; }
1706 bool allow_nested_vm_operations() const { return false; }
1707 VM_SetNotifyJvmtiEventsMode(bool enable) : _enable(enable) {
1708 }
1709
1710 void doit() {
1711 if (_enable) {
1712 correct_jvmti_thread_states();
1713 }
1714 MountUnmountDisabler::set_notify_jvmti_events(_enable);
1715 }
1716 };
1717
1718 // This function is to support agents loaded into running VM.
1719 // Must be called in thread-in-native mode.
1720 bool
1721 JvmtiEnvBase::enable_virtual_threads_notify_jvmti() {
1722 if (!Continuations::enabled()) {
1723 return false;
1724 }
1725 if (MountUnmountDisabler::notify_jvmti_events()) {
1726 return false; // already enabled
1727 }
1728 VM_SetNotifyJvmtiEventsMode op(true);
1729 VMThread::execute(&op);
1730 return true;
1731 }
1732
1733 // This function is used in WhiteBox, only needed to test the function above.
1734 // It is unsafe to use this function when virtual threads are executed.
1735 // Must be called in thread-in-native mode.
1736 bool
1737 JvmtiEnvBase::disable_virtual_threads_notify_jvmti() {
1738 if (!Continuations::enabled()) {
1739 return false;
1740 }
1741 if (!MountUnmountDisabler::notify_jvmti_events()) {
1742 return false; // already disabled
1743 }
1744 MountUnmountDisabler disabler(true); // ensure there are no other disablers
1745 VM_SetNotifyJvmtiEventsMode op(false);
1746 VMThread::execute(&op);
1747 return true;
1748 }
1749
1750 // java_thread - protected by ThreadsListHandle
1751 jvmtiError
1752 JvmtiEnvBase::suspend_thread(oop thread_oop, JavaThread* java_thread, bool single_suspend) {
1753 JavaThread* current = JavaThread::current();
1754 HandleMark hm(current);
1755 Handle thread_h(current, thread_oop);
1756 bool is_virtual = java_lang_VirtualThread::is_instance(thread_h());
1757
1758 // Unmounted vthread case.
1759
1760 if (is_virtual && java_thread == nullptr) {
1761 assert(single_suspend, "sanity check");
1762 if (JvmtiVTSuspender::is_vthread_suspended(thread_h())) {
1763 return JVMTI_ERROR_THREAD_SUSPENDED;
1764 }
1765 JvmtiVTSuspender::register_vthread_suspend(thread_h());
1766 return JVMTI_ERROR_NONE;
1767 }
1768
1769 // Platform thread or mounted vthread cases.
1770
1771 assert(java_thread != nullptr, "sanity check");
1772
1773 // Don't allow hidden thread suspend request.
1774 if (java_thread->is_hidden_from_external_view()) {
1775 return JVMTI_ERROR_NONE;
1776 }
1777
1778 // An attempt to handshake-suspend a thread carrying a virtual thread will result in
1779 // suspension of mounted virtual thread. So, we just mark it as suspended
1780 // and it will be actually suspended at virtual thread unmount transition.
1781 bool is_thread_carrying = is_thread_carrying_vthread(java_thread, thread_h());
1782 if (is_thread_carrying) {
1783 return java_thread->set_carrier_thread_suspended() ? JVMTI_ERROR_NONE : JVMTI_ERROR_THREAD_SUSPENDED;
1784 } else {
1785 // Platform thread (not carrying vthread) or mounted vthread cases.
1786 assert(thread_h() != nullptr, "sanity check");
1787 assert(single_suspend || thread_h()->is_a(vmClasses::BaseVirtualThread_klass()),
1788 "SuspendAllVirtualThreads should never suspend non-virtual threads");
1789
1790 // Ideally we would just need to check java_thread->is_suspended(), but we have to
1791 // consider the case of trying to suspend a thread that was previously suspended while
1792 // carrying a vthread but has already unmounted it.
1793 if (java_thread->is_suspended() || (!is_virtual && java_thread->is_carrier_thread_suspended())) {
1794 return JVMTI_ERROR_THREAD_SUSPENDED;
1795 }
1796 if (!java_thread->java_suspend(is_virtual && single_suspend)) {
1797 // Thread is already suspended or in process of exiting.
1798 if (java_thread->is_exiting()) {
1799 // The thread was in the process of exiting.
1800 return JVMTI_ERROR_THREAD_NOT_ALIVE;
1801 }
1802 return JVMTI_ERROR_THREAD_SUSPENDED;
1803 }
1804 return JVMTI_ERROR_NONE;
1805 }
1806 }
1807
1808 // java_thread - protected by ThreadsListHandle
1809 jvmtiError
1810 JvmtiEnvBase::resume_thread(oop thread_oop, JavaThread* java_thread, bool single_resume) {
1811 JavaThread* current = JavaThread::current();
1812 HandleMark hm(current);
1813 Handle thread_h(current, thread_oop);
1814 bool is_virtual = java_lang_VirtualThread::is_instance(thread_h());
1815
1816 // Unmounted vthread case.
1817
1818 if (is_virtual && java_thread == nullptr) {
1819 assert(single_resume, "sanity check");
1820 if (!JvmtiVTSuspender::is_vthread_suspended(thread_h())) {
1821 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1822 }
1823 JvmtiVTSuspender::register_vthread_resume(thread_h());
1824 return JVMTI_ERROR_NONE;
1825 }
1826
1827 // Platform thread or mounted vthread cases.
1828
1829 assert(java_thread != nullptr, "sanity check");
1830
1831 // Don't allow hidden thread resume request.
1832 if (java_thread->is_hidden_from_external_view()) {
1833 return JVMTI_ERROR_NONE;
1834 }
1835
1836 bool is_thread_carrying = is_thread_carrying_vthread(java_thread, thread_h());
1837 if (is_thread_carrying) {
1838 return java_thread->clear_carrier_thread_suspended() ? JVMTI_ERROR_NONE : JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1839 } else {
1840 // Platform thread (not carrying vthread) or mounted vthread cases.
1841
1842 assert(thread_h() != nullptr, "sanity check");
1843 assert(single_resume || thread_h()->is_a(vmClasses::BaseVirtualThread_klass()),
1844 "ResumeAllVirtualThreads should never resume non-virtual threads");
1845
1846 // Ideally we would not have to check this but we have to consider the case
1847 // of trying to resume a thread that was previously suspended while carrying
1848 // a vthread but has already unmounted it.
1849 if (!is_virtual && java_thread->is_carrier_thread_suspended()) {
1850 bool res = java_thread->clear_carrier_thread_suspended();
1851 assert(res, "resume operations running concurrently?");
1852 return JVMTI_ERROR_NONE;
1853 }
1854
1855 if (!java_thread->java_resume(is_virtual && single_resume)) {
1856 return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1857 }
1858 return JVMTI_ERROR_NONE;
1859 }
1860 }
1861
1862 ResourceTracker::ResourceTracker(JvmtiEnv* env) {
1863 _env = env;
1864 _allocations = new (mtServiceability) GrowableArray<unsigned char*>(20, mtServiceability);
1865 _failed = false;
1866 }
1867 ResourceTracker::~ResourceTracker() {
1868 if (_failed) {
1869 for (int i=0; i<_allocations->length(); i++) {
1870 _env->deallocate(_allocations->at(i));
1871 }
1872 }
1873 delete _allocations;
1874 }
1875
1876 jvmtiError ResourceTracker::allocate(jlong size, unsigned char** mem_ptr) {
1877 unsigned char *ptr;
1878 jvmtiError err = _env->allocate(size, &ptr);
1879 if (err == JVMTI_ERROR_NONE) {
1880 _allocations->append(ptr);
1881 *mem_ptr = ptr;
1882 } else {
1883 *mem_ptr = nullptr;
1884 _failed = true;
1885 }
1886 return err;
1887 }
1888
1889 unsigned char* ResourceTracker::allocate(jlong size) {
1890 unsigned char* ptr;
1891 allocate(size, &ptr);
1892 return ptr;
1893 }
1894
1895 char* ResourceTracker::strdup(const char* str) {
1896 char *dup_str = (char*)allocate(strlen(str)+1);
1897 if (dup_str != nullptr) {
1898 strcpy(dup_str, str);
1899 }
1900 return dup_str;
1901 }
1902
1903 struct StackInfoNode {
1904 struct StackInfoNode *next;
1905 jvmtiStackInfo info;
1906 };
1907
1908 // Create a jvmtiStackInfo inside a linked list node and create a
1909 // buffer for the frame information, both allocated as resource objects.
1910 // Fill in both the jvmtiStackInfo and the jvmtiFrameInfo.
1911 // Note that either or both of thr and thread_oop
1912 // may be null if the thread is new or has exited.
1913 void
1914 MultipleStackTracesCollector::fill_frames(jthread jt, JavaThread *thr, oop thread_oop) {
1915 #ifdef ASSERT
1916 Thread *current_thread = Thread::current();
1917 assert(SafepointSynchronize::is_at_safepoint() ||
1918 thr == nullptr ||
1919 thr->is_handshake_safe_for(current_thread),
1920 "unmounted virtual thread / call by myself / at safepoint / at handshake");
1921 #endif
1922
1923 jint state = 0;
1924 struct StackInfoNode *node = NEW_RESOURCE_OBJ(struct StackInfoNode);
1925 jvmtiStackInfo *infop = &(node->info);
1926
1927 node->next = head();
1928 set_head(node);
1929 infop->frame_count = 0;
1930 infop->frame_buffer = nullptr;
1931 infop->thread = jt;
1932
1933 if (java_lang_VirtualThread::is_instance(thread_oop)) {
1934 state = JvmtiEnvBase::get_vthread_state(thread_oop, thr);
1935
1936 if ((state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1937 javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(thread_oop);
1938 infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
1939 _result = env()->get_stack_trace(jvf, 0, max_frame_count(),
1940 infop->frame_buffer, &(infop->frame_count));
1941 }
1942 } else {
1943 state = JvmtiEnvBase::get_thread_state(thread_oop, thr);
1944 if (thr != nullptr && (state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1945 infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
1946 _result = env()->get_stack_trace(thr, 0, max_frame_count(),
1947 infop->frame_buffer, &(infop->frame_count));
1948 }
1949 }
1950 _frame_count_total += infop->frame_count;
1951 infop->state = state;
1952 }
1953
1954 // Based on the stack information in the linked list, allocate memory
1955 // block to return and fill it from the info in the linked list.
1956 void
1957 MultipleStackTracesCollector::allocate_and_fill_stacks(jint thread_count) {
1958 // do I need to worry about alignment issues?
1959 jlong alloc_size = thread_count * sizeof(jvmtiStackInfo)
1960 + _frame_count_total * sizeof(jvmtiFrameInfo);
1961 env()->allocate(alloc_size, (unsigned char **)&_stack_info);
1962
1963 // pointers to move through the newly allocated space as it is filled in
1964 jvmtiStackInfo *si = _stack_info + thread_count; // bottom of stack info
1965 jvmtiFrameInfo *fi = (jvmtiFrameInfo *)si; // is the top of frame info
1966
1967 // copy information in resource area into allocated buffer
1968 // insert stack info backwards since linked list is backwards
1969 // insert frame info forwards
1970 // walk the StackInfoNodes
1971 for (struct StackInfoNode *sin = head(); sin != nullptr; sin = sin->next) {
1972 jint frame_count = sin->info.frame_count;
1973 size_t frames_size = frame_count * sizeof(jvmtiFrameInfo);
1974 --si;
1975 memcpy(si, &(sin->info), sizeof(jvmtiStackInfo));
1976 if (frames_size == 0) {
1977 si->frame_buffer = nullptr;
1978 } else {
1979 memcpy(fi, sin->info.frame_buffer, frames_size);
1980 si->frame_buffer = fi; // point to the new allocated copy of the frames
1981 fi += frame_count;
1982 }
1983 }
1984 assert(si == _stack_info, "the last copied stack info must be the first record");
1985 assert((unsigned char *)fi == ((unsigned char *)_stack_info) + alloc_size,
1986 "the last copied frame info must be the last record");
1987 }
1988
1989 // AdapterClosure is to make use of JvmtiUnitedHandshakeClosure objects from
1990 // Handshake::execute() which is unaware of the do_vthread() member functions.
1991 class AdapterClosure : public HandshakeClosure {
1992 JvmtiUnitedHandshakeClosure* _hs_cl;
1993 Handle _target_h;
1994
1995 public:
1996 AdapterClosure(JvmtiUnitedHandshakeClosure* hs_cl, Handle target_h)
1997 : HandshakeClosure(hs_cl->name()), _hs_cl(hs_cl), _target_h(target_h) {}
1998
1999 virtual void do_thread(Thread* target) {
2000 if (java_lang_VirtualThread::is_instance(_target_h())) {
2001 _hs_cl->do_vthread(_target_h); // virtual thread
2002 } else {
2003 _hs_cl->do_thread(target); // platform thread
2004 }
2005 }
2006 };
2007
2008 // Supports platform and virtual threads.
2009 // MountUnmountDisabler is always set by this function.
2010 void
2011 JvmtiHandshake::execute(JvmtiUnitedHandshakeClosure* hs_cl, jthread target) {
2012 JavaThread* current = JavaThread::current();
2013 HandleMark hm(current);
2014 MountUnmountDisabler disabler(target);
2015 ThreadsListHandle tlh(current);
2016 JavaThread* java_thread = nullptr;
2017 oop thread_obj = nullptr;
2018
2019 jvmtiError err = JvmtiEnvBase::get_threadOop_and_JavaThread(tlh.list(), target, current, &java_thread, &thread_obj);
2020 if (err != JVMTI_ERROR_NONE) {
2021 hs_cl->set_result(err);
2022 return;
2023 }
2024 Handle target_h(current, thread_obj);
2025 execute(hs_cl, &tlh, java_thread, target_h);
2026 }
2027
2028 // Supports platform and virtual threads.
2029 // A virtual thread is always identified by the target_h oop handle.
2030 // The target_jt is always nullptr for an unmounted virtual thread.
2031 // MountUnmountDisabler has to be set before call to this function.
2032 void
2033 JvmtiHandshake::execute(JvmtiUnitedHandshakeClosure* hs_cl, ThreadsListHandle* tlh,
2034 JavaThread* target_jt, Handle target_h) {
2035 JavaThread* current = JavaThread::current();
2036 bool is_virtual = java_lang_VirtualThread::is_instance(target_h());
2037 bool self = target_jt == current;
2038
2039 assert(!Continuations::enabled() || self || !is_virtual || current->is_vthread_transition_disabler(), "sanity check");
2040
2041 hs_cl->set_target_jt(target_jt); // can be needed in the virtual thread case
2042 hs_cl->set_is_virtual(is_virtual); // can be needed in the virtual thread case
2043 hs_cl->set_self(self); // needed when suspend is required for non-current target thread
2044
2045 if (is_virtual) { // virtual thread
2046 if (!JvmtiEnvBase::is_vthread_alive(target_h())) {
2047 return;
2048 }
2049 if (target_jt == nullptr) { // unmounted virtual thread
2050 hs_cl->do_vthread(target_h); // execute handshake closure callback on current thread directly
2051 }
2052 }
2053 if (target_jt != nullptr) { // mounted virtual or platform thread
2054 AdapterClosure acl(hs_cl, target_h);
2055 if (self) { // target platform thread is current
2056 acl.do_thread(target_jt); // execute handshake closure callback on current thread directly
2057 } else {
2058 Handshake::execute(&acl, tlh, target_jt); // delegate to Handshake implementation
2059 }
2060 }
2061 }
2062
2063 void
2064 VM_GetThreadListStackTraces::doit() {
2065 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2066
2067 ResourceMark rm;
2068 ThreadsListHandle tlh;
2069 for (int i = 0; i < _thread_count; ++i) {
2070 jthread jt = _thread_list[i];
2071 JavaThread* java_thread = nullptr;
2072 oop thread_oop = nullptr;
2073 jvmtiError err = JvmtiEnvBase::get_threadOop_and_JavaThread(tlh.list(), jt, nullptr, &java_thread, &thread_oop);
2074
2075 if (err != JVMTI_ERROR_NONE) {
2076 // We got an error code so we don't have a JavaThread *, but
2077 // only return an error from here if we didn't get a valid
2078 // thread_oop.
2079 // In the virtual thread case the get_threadOop_and_JavaThread is expected to correctly set
2080 // the thread_oop and return JVMTI_ERROR_THREAD_NOT_ALIVE which we ignore here.
2081 // The corresponding thread state will be recorded in the jvmtiStackInfo.state.
2082 if (thread_oop == nullptr) {
2083 _collector.set_result(err);
2084 return;
2085 }
2086 // We have a valid thread_oop.
2087 }
2088 _collector.fill_frames(jt, java_thread, thread_oop);
2089 }
2090 _collector.allocate_and_fill_stacks(_thread_count);
2091 }
2092
2093 void
2094 GetSingleStackTraceClosure::doit() {
2095 JavaThread *jt = _target_jt;
2096 oop thread_oop = JNIHandles::resolve_external_guard(_jthread);
2097
2098 if ((jt == nullptr || !jt->is_exiting()) && thread_oop != nullptr) {
2099 ResourceMark rm;
2100 _collector.fill_frames(_jthread, jt, thread_oop);
2101 _collector.allocate_and_fill_stacks(1);
2102 set_result(_collector.result());
2103 }
2104 }
2105
2106 void
2107 GetSingleStackTraceClosure::do_thread(Thread *target) {
2108 assert(_target_jt == JavaThread::cast(target), "sanity check");
2109 doit();
2110 }
2111
2112 void
2113 GetSingleStackTraceClosure::do_vthread(Handle target_h) {
2114 // Use jvmti_vthread() instead of vthread() as target could have temporarily changed
2115 // identity to carrier thread (see VirtualThread.switchToCarrierThread).
2116 assert(_target_jt == nullptr || _target_jt->jvmti_vthread() == target_h(), "sanity check");
2117 doit();
2118 }
2119
2120 void
2121 VM_GetAllStackTraces::doit() {
2122 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2123
2124 ResourceMark rm;
2125 _final_thread_count = 0;
2126 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2127 oop thread_oop = jt->threadObj();
2128 if (thread_oop != nullptr &&
2129 !jt->is_exiting() &&
2130 java_lang_Thread::is_alive(thread_oop) &&
2131 !jt->is_hidden_from_external_view() &&
2132 !thread_oop->is_a(vmClasses::BoundVirtualThread_klass())) {
2133 ++_final_thread_count;
2134 // Handle block of the calling thread is used to create local refs.
2135 _collector.fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
2136 jt, thread_oop);
2137 }
2138 }
2139 _collector.allocate_and_fill_stacks(_final_thread_count);
2140 }
2141
2142 // Verifies that the top frame is a java frame in an expected state.
2143 // Deoptimizes frame if needed.
2144 // Checks that the frame method signature matches the return type (tos).
2145 // HandleMark must be defined in the caller only.
2146 // It is to keep a ret_ob_h handle alive after return to the caller.
2147 jvmtiError
2148 JvmtiEnvBase::check_top_frame(Thread* current_thread, JavaThread* java_thread,
2149 jvalue value, TosState tos, Handle* ret_ob_h) {
2150 ResourceMark rm(current_thread);
2151
2152 javaVFrame* jvf = jvf_for_thread_and_depth(java_thread, 0);
2153 NULL_CHECK(jvf, JVMTI_ERROR_NO_MORE_FRAMES);
2154
2155 if (jvf->method()->is_native()) {
2156 return JVMTI_ERROR_OPAQUE_FRAME;
2157 }
2158
2159 // If the frame is a compiled one, need to deoptimize it.
2160 if (jvf->is_compiled_frame()) {
2161 if (!jvf->fr().can_be_deoptimized()) {
2162 return JVMTI_ERROR_OPAQUE_FRAME;
2163 }
2164 Deoptimization::deoptimize_frame(java_thread, jvf->fr().id());
2165 }
2166
2167 // Get information about method return type
2168 Symbol* signature = jvf->method()->signature();
2169
2170 ResultTypeFinder rtf(signature);
2171 TosState fr_tos = as_TosState(rtf.type());
2172 if (fr_tos != tos) {
2173 if (tos != itos || (fr_tos != btos && fr_tos != ztos && fr_tos != ctos && fr_tos != stos)) {
2174 return JVMTI_ERROR_TYPE_MISMATCH;
2175 }
2176 }
2177
2178 // Check that the jobject class matches the return type signature.
2179 jobject jobj = value.l;
2180 if (tos == atos && jobj != nullptr) { // null reference is allowed
2181 Handle ob_h(current_thread, JNIHandles::resolve_external_guard(jobj));
2182 NULL_CHECK(ob_h, JVMTI_ERROR_INVALID_OBJECT);
2183 Klass* ob_k = ob_h()->klass();
2184 NULL_CHECK(ob_k, JVMTI_ERROR_INVALID_OBJECT);
2185
2186 // Method return type signature.
2187 char* ty_sign = 1 + strchr(signature->as_C_string(), JVM_SIGNATURE_ENDFUNC);
2188
2189 if (!VM_GetOrSetLocal::is_assignable(ty_sign, ob_k, current_thread)) {
2190 return JVMTI_ERROR_TYPE_MISMATCH;
2191 }
2192 *ret_ob_h = ob_h;
2193 }
2194 return JVMTI_ERROR_NONE;
2195 } /* end check_top_frame */
2196
2197
2198 // ForceEarlyReturn<type> follows the PopFrame approach in many aspects.
2199 // Main difference is on the last stage in the interpreter.
2200 // The PopFrame stops method execution to continue execution
2201 // from the same method call instruction.
2202 // The ForceEarlyReturn forces return from method so the execution
2203 // continues at the bytecode following the method call.
2204
2205 // thread - NOT protected by ThreadsListHandle and NOT pre-checked
2206
2207 jvmtiError
2208 JvmtiEnvBase::force_early_return(jthread thread, jvalue value, TosState tos) {
2209 JavaThread* current_thread = JavaThread::current();
2210 HandleMark hm(current_thread);
2211
2212 MountUnmountDisabler disabler(thread);
2213 ThreadsListHandle tlh(current_thread);
2214
2215 JavaThread* java_thread = nullptr;
2216 oop thread_obj = nullptr;
2217 jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2218
2219 if (err != JVMTI_ERROR_NONE) {
2220 return err;
2221 }
2222 Handle thread_handle(current_thread, thread_obj);
2223 bool self = java_thread == current_thread;
2224
2225 err = check_non_suspended_or_opaque_frame(java_thread, thread_obj, self);
2226 if (err != JVMTI_ERROR_NONE) {
2227 return err;
2228 }
2229
2230 // retrieve or create the state
2231 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
2232 if (state == nullptr) {
2233 return JVMTI_ERROR_THREAD_NOT_ALIVE;
2234 }
2235
2236 // Eagerly reallocate scalar replaced objects.
2237 EscapeBarrier eb(true, current_thread, java_thread);
2238 if (!eb.deoptimize_objects(0)) {
2239 // Reallocation of scalar replaced objects failed -> return with error
2240 return JVMTI_ERROR_OUT_OF_MEMORY;
2241 }
2242
2243 MutexLocker mu(JvmtiThreadState_lock);
2244 SetForceEarlyReturn op(state, value, tos);
2245 JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
2246 return op.result();
2247 }
2248
2249 void
2250 SetForceEarlyReturn::doit(Thread *target) {
2251 JavaThread* java_thread = JavaThread::cast(target);
2252 Thread* current_thread = Thread::current();
2253 HandleMark hm(current_thread);
2254
2255 if (java_thread->is_exiting()) {
2256 return; /* JVMTI_ERROR_THREAD_NOT_ALIVE (default) */
2257 }
2258
2259 // Check to see if a ForceEarlyReturn was already in progress
2260 if (_state->is_earlyret_pending()) {
2261 // Probably possible for JVMTI clients to trigger this, but the
2262 // JPDA backend shouldn't allow this to happen
2263 _result = JVMTI_ERROR_INTERNAL;
2264 return;
2265 }
2266 {
2267 // The same as for PopFrame. Workaround bug:
2268 // 4812902: popFrame hangs if the method is waiting at a synchronize
2269 // Catch this condition and return an error to avoid hanging.
2270 // Now JVMTI spec allows an implementation to bail out with an opaque
2271 // frame error.
2272 OSThread* osThread = java_thread->osthread();
2273 if (osThread->get_state() == MONITOR_WAIT) {
2274 _result = JVMTI_ERROR_OPAQUE_FRAME;
2275 return;
2276 }
2277 }
2278
2279 Handle ret_ob_h;
2280 _result = JvmtiEnvBase::check_top_frame(current_thread, java_thread, _value, _tos, &ret_ob_h);
2281 if (_result != JVMTI_ERROR_NONE) {
2282 return;
2283 }
2284 assert(_tos != atos || _value.l == nullptr || ret_ob_h() != nullptr,
2285 "return object oop must not be null if jobject is not null");
2286
2287 // Update the thread state to reflect that the top frame must be
2288 // forced to return.
2289 // The current frame will be returned later when the suspended
2290 // thread is resumed and right before returning from VM to Java.
2291 // (see call_VM_base() in assembler_<cpu>.cpp).
2292
2293 _state->set_earlyret_pending();
2294 _state->set_earlyret_oop(ret_ob_h());
2295 _state->set_earlyret_value(_value, _tos);
2296
2297 // Set pending step flag for this early return.
2298 // It is cleared when next step event is posted.
2299 _state->set_pending_step_for_earlyret();
2300 }
2301
2302 void
2303 JvmtiMonitorClosure::do_monitor(ObjectMonitor* mon) {
2304 if ( _error != JVMTI_ERROR_NONE) {
2305 // Error occurred in previous iteration so no need to add
2306 // to the list.
2307 return;
2308 }
2309 // Filter out on stack monitors collected during stack walk.
2310 oop obj = mon->object();
2311
2312 if (obj == nullptr) {
2313 // This can happen if JNI code drops all references to the
2314 // owning object.
2315 return;
2316 }
2317
2318 bool found = false;
2319 for (int j = 0; j < _owned_monitors_list->length(); j++) {
2320 jobject jobj = ((jvmtiMonitorStackDepthInfo*)_owned_monitors_list->at(j))->monitor;
2321 oop check = JNIHandles::resolve(jobj);
2322 if (check == obj) {
2323 // On stack monitor already collected during the stack walk.
2324 found = true;
2325 break;
2326 }
2327 }
2328 if (found == false) {
2329 // This is off stack monitor (e.g. acquired via jni MonitorEnter).
2330 jvmtiError err;
2331 jvmtiMonitorStackDepthInfo *jmsdi;
2332 err = _env->allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
2333 if (err != JVMTI_ERROR_NONE) {
2334 _error = err;
2335 return;
2336 }
2337 Handle hobj(Thread::current(), obj);
2338 jmsdi->monitor = _env->jni_reference(_calling_thread, hobj);
2339 // stack depth is unknown for this monitor.
2340 jmsdi->stack_depth = -1;
2341 _owned_monitors_list->append(jmsdi);
2342 }
2343 }
2344
2345 GrowableArray<OopHandle>* JvmtiModuleClosure::_tbl = nullptr;
2346
2347 void JvmtiModuleClosure::do_module(ModuleEntry* entry) {
2348 assert_locked_or_safepoint(Module_lock);
2349 OopHandle module = entry->module_handle();
2350 guarantee(module.resolve() != nullptr, "module object is null");
2351 _tbl->push(module);
2352 }
2353
2354 jvmtiError
2355 JvmtiModuleClosure::get_all_modules(JvmtiEnv* env, jint* module_count_ptr, jobject** modules_ptr) {
2356 ResourceMark rm;
2357 MutexLocker mcld(ClassLoaderDataGraph_lock);
2358 MutexLocker ml(Module_lock);
2359
2360 _tbl = new GrowableArray<OopHandle>(77);
2361 if (_tbl == nullptr) {
2362 return JVMTI_ERROR_OUT_OF_MEMORY;
2363 }
2364
2365 // Iterate over all the modules loaded to the system.
2366 ClassLoaderDataGraph::modules_do_keepalive(&do_module);
2367
2368 jint len = _tbl->length();
2369 guarantee(len > 0, "at least one module must be present");
2370
2371 jobject* array = (jobject*)env->jvmtiMalloc((jlong)(len * sizeof(jobject)));
2372 if (array == nullptr) {
2373 return JVMTI_ERROR_OUT_OF_MEMORY;
2374 }
2375 for (jint idx = 0; idx < len; idx++) {
2376 array[idx] = JNIHandles::make_local(_tbl->at(idx).resolve());
2377 }
2378 _tbl = nullptr;
2379 *modules_ptr = array;
2380 *module_count_ptr = len;
2381 return JVMTI_ERROR_NONE;
2382 }
2383
2384 void
2385 UpdateForPopTopFrameClosure::doit(Thread *target) {
2386 Thread* current_thread = Thread::current();
2387 HandleMark hm(current_thread);
2388 JavaThread* java_thread = JavaThread::cast(target);
2389
2390 if (java_thread->is_exiting()) {
2391 return; /* JVMTI_ERROR_THREAD_NOT_ALIVE (default) */
2392 }
2393 assert(java_thread == _state->get_thread(), "Must be");
2394
2395 // Check to see if a PopFrame was already in progress
2396 if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
2397 // Probably possible for JVMTI clients to trigger this, but the
2398 // JPDA backend shouldn't allow this to happen
2399 _result = JVMTI_ERROR_INTERNAL;
2400 return;
2401 }
2402
2403 // Was workaround bug
2404 // 4812902: popFrame hangs if the method is waiting at a synchronize
2405 // Catch this condition and return an error to avoid hanging.
2406 // Now JVMTI spec allows an implementation to bail out with an opaque frame error.
2407 OSThread* osThread = java_thread->osthread();
2408 if (osThread->get_state() == MONITOR_WAIT) {
2409 _result = JVMTI_ERROR_OPAQUE_FRAME;
2410 return;
2411 }
2412
2413 ResourceMark rm(current_thread);
2414 // Check if there is more than one Java frame in this thread, that the top two frames
2415 // are Java (not native) frames, and that there is no intervening VM frame
2416 int frame_count = 0;
2417 bool is_interpreted[2];
2418 intptr_t *frame_sp[2];
2419 // The 2-nd arg of constructor is needed to stop iterating at java entry frame.
2420 for (vframeStream vfs(java_thread, true, false /* process_frames */); !vfs.at_end(); vfs.next()) {
2421 methodHandle mh(current_thread, vfs.method());
2422 if (mh->is_native()) {
2423 _result = JVMTI_ERROR_OPAQUE_FRAME;
2424 return;
2425 }
2426 is_interpreted[frame_count] = vfs.is_interpreted_frame();
2427 frame_sp[frame_count] = vfs.frame_id();
2428 if (++frame_count > 1) break;
2429 }
2430 if (frame_count < 2) {
2431 // We haven't found two adjacent non-native Java frames on the top.
2432 // There can be two situations here:
2433 // 1. There are no more java frames
2434 // 2. Two top java frames are separated by non-java native frames
2435 if (JvmtiEnvBase::jvf_for_thread_and_depth(java_thread, 1) == nullptr) {
2436 _result = JVMTI_ERROR_NO_MORE_FRAMES;
2437 return;
2438 } else {
2439 // Intervening non-java native or VM frames separate java frames.
2440 // Current implementation does not support this. See bug #5031735.
2441 // In theory it is possible to pop frames in such cases.
2442 _result = JVMTI_ERROR_OPAQUE_FRAME;
2443 return;
2444 }
2445 }
2446
2447 // If any of the top 2 frames is a compiled one, need to deoptimize it
2448 for (int i = 0; i < 2; i++) {
2449 if (!is_interpreted[i]) {
2450 Deoptimization::deoptimize_frame(java_thread, frame_sp[i]);
2451 }
2452 }
2453
2454 // Update the thread state to reflect that the top frame is popped
2455 // so that cur_stack_depth is maintained properly and all frameIDs
2456 // are invalidated.
2457 // The current frame will be popped later when the suspended thread
2458 // is resumed and right before returning from VM to Java.
2459 // (see call_VM_base() in assembler_<cpu>.cpp).
2460
2461 // It's fine to update the thread state here because no JVMTI events
2462 // shall be posted for this PopFrame.
2463
2464 _state->update_for_pop_top_frame();
2465 java_thread->set_popframe_condition(JavaThread::popframe_pending_bit);
2466 // Set pending step flag for this popframe and it is cleared when next
2467 // step event is posted.
2468 _state->set_pending_step_for_popframe();
2469 _result = JVMTI_ERROR_NONE;
2470 }
2471
2472 void
2473 SetOrClearFramePopClosure::do_thread(Thread *target) {
2474 Thread* current = Thread::current();
2475 ResourceMark rm(current); // vframes are resource allocated
2476 JavaThread* java_thread = JavaThread::cast(target);
2477
2478 if (java_thread->is_exiting()) {
2479 return; // JVMTI_ERROR_THREAD_NOT_ALIVE (default)
2480 }
2481
2482 if (!_self && !java_thread->is_suspended()) {
2483 _result = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
2484 return;
2485 }
2486 if (!_set) { // ClearAllFramePops
2487 _result = _env->clear_all_frame_pops(_state);
2488 return;
2489 }
2490 if (!java_thread->has_last_Java_frame()) {
2491 _result = JVMTI_ERROR_NO_MORE_FRAMES;
2492 return;
2493 }
2494 assert(_state->get_thread() == java_thread, "Must be");
2495
2496 RegisterMap reg_map(java_thread,
2497 RegisterMap::UpdateMap::include,
2498 RegisterMap::ProcessFrames::skip,
2499 RegisterMap::WalkContinuation::include);
2500 javaVFrame* jvf = JvmtiEnvBase::get_cthread_last_java_vframe(java_thread, ®_map);
2501 _result = _env->set_frame_pop(_state, jvf, _depth);
2502 }
2503
2504 void
2505 SetOrClearFramePopClosure::do_vthread(Handle target_h) {
2506 Thread* current = Thread::current();
2507 ResourceMark rm(current); // vframes are resource allocated
2508
2509 if (!_self && !JvmtiVTSuspender::is_vthread_suspended(target_h())) {
2510 _result = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
2511 return;
2512 }
2513 if (!_set) { // ClearAllFramePops
2514 _result = _env->clear_all_frame_pops(_state);
2515 return;
2516 }
2517 javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h());
2518 _result = _env->set_frame_pop(_state, jvf, _depth);
2519 }
2520
2521 void
2522 GetOwnedMonitorInfoClosure::do_thread(Thread *target) {
2523 JavaThread *jt = JavaThread::cast(target);
2524 if (!jt->is_exiting() && (jt->threadObj() != nullptr)) {
2525 _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread,
2526 jt,
2527 _owned_monitors_list);
2528 }
2529 }
2530
2531 void
2532 GetOwnedMonitorInfoClosure::do_vthread(Handle target_h) {
2533 Thread* current = Thread::current();
2534 ResourceMark rm(current); // vframes are resource allocated
2535 HandleMark hm(current);
2536
2537 javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h());
2538
2539 if (_target_jt == nullptr || (!_target_jt->is_exiting() && _target_jt->threadObj() != nullptr)) {
2540 _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread,
2541 _target_jt,
2542 jvf,
2543 _owned_monitors_list,
2544 target_h());
2545 }
2546 }
2547
2548 void
2549 GetCurrentContendedMonitorClosure::do_thread(Thread *target) {
2550 JavaThread *jt = JavaThread::cast(target);
2551 if (!jt->is_exiting() && (jt->threadObj() != nullptr)) {
2552 _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,
2553 jt,
2554 _owned_monitor_ptr,
2555 _is_virtual);
2556 }
2557 }
2558
2559 void
2560 GetCurrentContendedMonitorClosure::do_vthread(Handle target_h) {
2561 if (_target_jt == nullptr) {
2562 ObjectMonitor *mon = java_lang_VirtualThread::current_pending_monitor(target_h());
2563 if (mon != nullptr) {
2564 *_owned_monitor_ptr = JNIHandles::make_local(_calling_thread, mon->object());
2565 }
2566 _result = JVMTI_ERROR_NONE; // target virtual thread is unmounted
2567 return;
2568 }
2569 // mounted virtual thread case
2570 do_thread(_target_jt);
2571 }
2572
2573 void
2574 GetStackTraceClosure::do_thread(Thread *target) {
2575 Thread* current = Thread::current();
2576 ResourceMark rm(current);
2577
2578 JavaThread *jt = JavaThread::cast(target);
2579 if (!jt->is_exiting() && jt->threadObj() != nullptr) {
2580 _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jt,
2581 _start_depth, _max_count,
2582 _frame_buffer, _count_ptr);
2583 }
2584 }
2585
2586 void
2587 GetStackTraceClosure::do_vthread(Handle target_h) {
2588 Thread* current = Thread::current();
2589 ResourceMark rm(current);
2590
2591 javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h());
2592 _result = ((JvmtiEnvBase *)_env)->get_stack_trace(jvf,
2593 _start_depth, _max_count,
2594 _frame_buffer, _count_ptr);
2595 }
2596
2597 #ifdef ASSERT
2598 void
2599 PrintStackTraceClosure::do_thread_impl(Thread *target) {
2600 JavaThread *java_thread = JavaThread::cast(target);
2601 Thread *current_thread = Thread::current();
2602
2603 ResourceMark rm (current_thread);
2604 const char* tname = JvmtiTrace::safe_get_thread_name(java_thread);
2605 oop t_oop = java_thread->jvmti_vthread();
2606 t_oop = t_oop == nullptr ? java_thread->threadObj() : t_oop;
2607 bool is_vt_suspended = java_lang_VirtualThread::is_instance(t_oop) && JvmtiVTSuspender::is_vthread_suspended(t_oop);
2608
2609 log_error(jvmti)("%s(%s) exiting: %d is_susp: %d is_thread_susp: %d is_vthread_susp: %d "
2610 "is_VTMS_transition_disabler: %d, is_in_VTMS_transition = %d\n",
2611 tname, java_thread->name(), java_thread->is_exiting(),
2612 java_thread->is_suspended(), java_thread->is_carrier_thread_suspended(), is_vt_suspended,
2613 java_thread->is_vthread_transition_disabler(), java_thread->is_in_vthread_transition());
2614
2615 if (java_thread->has_last_Java_frame()) {
2616 RegisterMap reg_map(java_thread,
2617 RegisterMap::UpdateMap::include,
2618 RegisterMap::ProcessFrames::include,
2619 RegisterMap::WalkContinuation::skip);
2620 ResourceMark rm(current_thread);
2621 HandleMark hm(current_thread);
2622 javaVFrame *jvf = java_thread->last_java_vframe(®_map);
2623 while (jvf != nullptr) {
2624 log_error(jvmti)(" %s:%d",
2625 jvf->method()->external_name(),
2626 jvf->method()->line_number_from_bci(jvf->bci()));
2627 jvf = jvf->java_sender();
2628 }
2629 }
2630 log_error(jvmti)("\n");
2631 }
2632
2633 void
2634 PrintStackTraceClosure::do_thread(Thread *target) {
2635 JavaThread *java_thread = JavaThread::cast(target);
2636 Thread *current_thread = Thread::current();
2637
2638 assert(SafepointSynchronize::is_at_safepoint() ||
2639 java_thread->is_handshake_safe_for(current_thread),
2640 "call by myself / at safepoint / at handshake");
2641
2642 PrintStackTraceClosure::do_thread_impl(target);
2643 }
2644 #endif
2645
2646 void
2647 GetFrameCountClosure::do_thread(Thread *target) {
2648 JavaThread* jt = JavaThread::cast(target);
2649 assert(target == jt, "just checking");
2650
2651 if (!jt->is_exiting() && jt->threadObj() != nullptr) {
2652 _result = ((JvmtiEnvBase*)_env)->get_frame_count(jt, _count_ptr);
2653 }
2654 }
2655
2656 void
2657 GetFrameCountClosure::do_vthread(Handle target_h) {
2658 _result = ((JvmtiEnvBase*)_env)->get_frame_count(target_h(), _count_ptr);
2659 }
2660
2661 void
2662 GetFrameLocationClosure::do_thread(Thread *target) {
2663 JavaThread *jt = JavaThread::cast(target);
2664 assert(target == jt, "just checking");
2665
2666 if (!jt->is_exiting() && jt->threadObj() != nullptr) {
2667 _result = ((JvmtiEnvBase*)_env)->get_frame_location(jt, _depth,
2668 _method_ptr, _location_ptr);
2669 }
2670 }
2671
2672 void
2673 GetFrameLocationClosure::do_vthread(Handle target_h) {
2674 _result = ((JvmtiEnvBase*)_env)->get_frame_location(target_h(), _depth,
2675 _method_ptr, _location_ptr);
2676 }