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