2
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "classfile/classLoaderDataGraph.hpp"
27
#include "classfile/javaClasses.inline.hpp"
28
#include "classfile/moduleEntry.hpp"
29
#include "classfile/symbolTable.hpp"
30
#include "classfile/vmSymbols.hpp"
31
#include "jvmtifiles/jvmtiEnv.hpp"
32
#include "memory/iterator.hpp"
33
#include "memory/resourceArea.hpp"
34
#include "oops/klass.inline.hpp"
35
#include "oops/objArrayKlass.hpp"
36
#include "oops/objArrayOop.hpp"
37
#include "oops/oop.inline.hpp"
38
#include "oops/oopHandle.inline.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/objectMonitor.inline.hpp"
56
#include "runtime/osThread.hpp"
57
#include "runtime/signature.hpp"
58
#include "runtime/stackWatermarkSet.inline.hpp"
59
#include "runtime/threads.hpp"
60
#include "runtime/threadSMR.inline.hpp"
61
#include "runtime/vframe.inline.hpp"
62
#include "runtime/vframe_hp.hpp"
63
#include "runtime/vmThread.hpp"
64
#include "runtime/vmOperations.hpp"
65
#include "services/threadService.hpp"
68
///////////////////////////////////////////////////////////////
73
JvmtiEnvBase* JvmtiEnvBase::_head_environment = nullptr;
75
bool JvmtiEnvBase::_globally_initialized = false;
76
volatile bool JvmtiEnvBase::_needs_clean_up = false;
78
jvmtiPhase JvmtiEnvBase::_phase = JVMTI_PHASE_PRIMORDIAL;
80
volatile int JvmtiEnvBase::_dying_thread_env_iteration_count = 0;
82
extern jvmtiInterface_1_ jvmti_Interface;
83
extern jvmtiInterface_1_ jvmtiTrace_Interface;
86
// perform initializations that must occur before any JVMTI environments
87
// are released but which should only be initialized once (no matter
88
// how many environments are created).
90
JvmtiEnvBase::globally_initialize() {
91
assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
92
assert(_globally_initialized == false, "bad call");
94
JvmtiManageCapabilities::initialize();
96
// register extension functions and events
97
JvmtiExtensions::register_extensions();
100
JvmtiTrace::initialize();
103
_globally_initialized = true;
108
JvmtiEnvBase::initialize() {
109
assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
111
// Add this environment to the end of the environment list (order is important)
113
// This block of code must not contain any safepoints, as list deallocation
114
// (which occurs at a safepoint) cannot occur simultaneously with this list
115
// addition. Note: NoSafepointVerifier cannot, currently, be used before
118
JvmtiEnvBase *previous_env = nullptr;
119
for (JvmtiEnvBase* env = it.first(); env != nullptr; env = it.next(env)) {
122
if (previous_env == nullptr) {
123
_head_environment = this;
125
previous_env->set_next_environment(this);
129
if (_globally_initialized == false) {
130
globally_initialize();
135
JvmtiEnvBase::phase() {
136
// For the JVMTI environments possessed the can_generate_early_vmstart:
137
// replace JVMTI_PHASE_PRIMORDIAL with JVMTI_PHASE_START
138
if (_phase == JVMTI_PHASE_PRIMORDIAL &&
139
JvmtiExport::early_vmstart_recorded() &&
140
early_vmstart_env()) {
141
return JVMTI_PHASE_START;
143
return _phase; // Normal case
147
JvmtiEnvBase::is_valid() {
150
// This object might not be a JvmtiEnvBase so we can't assume
151
// the _magic field is properly aligned. Get the value in a safe
152
// way and then check against JVMTI_MAGIC.
154
switch (sizeof(_magic)) {
156
value = Bytes::get_native_u2((address)&_magic);
160
value = Bytes::get_native_u4((address)&_magic);
164
value = Bytes::get_native_u8((address)&_magic);
168
guarantee(false, "_magic field is an unexpected size");
171
return value == JVMTI_MAGIC;
176
JvmtiEnvBase::use_version_1_0_semantics() {
177
int major, minor, micro;
179
JvmtiExport::decode_version_values(_version, &major, &minor, µ);
180
return major == 1 && minor == 0; // micro version doesn't matter here
185
JvmtiEnvBase::use_version_1_1_semantics() {
186
int major, minor, micro;
188
JvmtiExport::decode_version_values(_version, &major, &minor, µ);
189
return major == 1 && minor == 1; // micro version doesn't matter here
193
JvmtiEnvBase::use_version_1_2_semantics() {
194
int major, minor, micro;
196
JvmtiExport::decode_version_values(_version, &major, &minor, µ);
197
return major == 1 && minor == 2; // micro version doesn't matter here
201
JvmtiEnvBase::JvmtiEnvBase(jint version) : _env_event_enable() {
203
_env_local_storage = nullptr;
205
_native_method_prefix_count = 0;
206
_native_method_prefixes = nullptr;
208
_class_file_load_hook_ever_enabled = false;
210
// Moot since ClassFileLoadHook not yet enabled.
211
// But "true" will give a more predictable ClassFileLoadHook behavior
212
// for environment creation during ClassFileLoadHook.
213
_is_retransformable = true;
215
// all callbacks initially null
216
memset(&_event_callbacks, 0, sizeof(jvmtiEventCallbacks));
217
memset(&_ext_event_callbacks, 0, sizeof(jvmtiExtEventCallbacks));
219
// all capabilities initially off
220
memset(&_current_capabilities, 0, sizeof(_current_capabilities));
222
// all prohibited capabilities initially off
223
memset(&_prohibited_capabilities, 0, sizeof(_prohibited_capabilities));
225
_magic = JVMTI_MAGIC;
227
JvmtiEventController::env_initialize((JvmtiEnv*)this);
230
_jvmti_external.functions = TraceJVMTI != nullptr ? &jvmtiTrace_Interface : &jvmti_Interface;
232
_jvmti_external.functions = &jvmti_Interface;
238
JvmtiEnvBase::dispose() {
241
JvmtiTrace::shutdown();
244
// Dispose of event info and let the event controller call us back
245
// in a locked state (env_dispose, below)
246
JvmtiEventController::env_dispose(this);
250
JvmtiEnvBase::env_dispose() {
251
assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
253
// We have been entered with all events disabled on this environment.
254
// A race to re-enable events (by setting callbacks) is prevented by
255
// checking for a valid environment when setting callbacks (while
256
// holding the JvmtiThreadState_lock).
259
_magic = DISPOSED_MAGIC;
261
// Relinquish all capabilities.
262
jvmtiCapabilities *caps = get_capabilities();
263
JvmtiManageCapabilities::relinquish_capabilities(caps, caps, caps);
265
// Same situation as with events (see above)
266
set_native_method_prefixes(0, nullptr);
268
JvmtiTagMap* tag_map_to_clear = tag_map_acquire();
269
// A tag map can be big, clear it now to save memory until
270
// the destructor runs.
271
if (tag_map_to_clear != nullptr) {
272
tag_map_to_clear->clear();
275
_needs_clean_up = true;
279
JvmtiEnvBase::~JvmtiEnvBase() {
280
assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
282
// There is a small window of time during which the tag map of a
283
// disposed environment could have been reallocated.
284
// Make sure it is gone.
285
JvmtiTagMap* tag_map_to_deallocate = _tag_map;
286
set_tag_map(nullptr);
287
// A tag map can be big, deallocate it now
288
if (tag_map_to_deallocate != nullptr) {
289
delete tag_map_to_deallocate;
297
JvmtiEnvBase::periodic_clean_up() {
298
assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
300
// JvmtiEnvBase reference is saved in JvmtiEnvThreadState. So
301
// clean up JvmtiThreadState before deleting JvmtiEnv pointer.
302
JvmtiThreadState::periodic_clean_up();
304
// Unlink all invalid environments from the list of environments
305
// and deallocate them
307
JvmtiEnvBase* previous_env = nullptr;
308
JvmtiEnvBase* env = it.first();
309
while (env != nullptr) {
310
if (env->is_valid()) {
314
// This one isn't valid, remove it from the list and deallocate it
315
JvmtiEnvBase* defunct_env = env;
317
if (previous_env == nullptr) {
318
_head_environment = env;
320
previous_env->set_next_environment(env);
330
JvmtiEnvBase::check_for_periodic_clean_up() {
331
assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
333
class ThreadInsideIterationClosure: public ThreadClosure {
337
ThreadInsideIterationClosure() : _inside(false) {};
339
void do_thread(Thread* thread) {
340
_inside |= thread->is_inside_jvmti_env_iteration();
343
bool is_inside_jvmti_env_iteration() {
348
if (_needs_clean_up) {
349
// Check if we are currently iterating environment,
350
// deallocation should not occur if we are
351
ThreadInsideIterationClosure tiic;
352
Threads::threads_do(&tiic);
353
if (!tiic.is_inside_jvmti_env_iteration() &&
354
!is_inside_dying_thread_env_iteration()) {
355
_needs_clean_up = false;
356
JvmtiEnvBase::periodic_clean_up();
363
JvmtiEnvBase::record_first_time_class_file_load_hook_enabled() {
364
assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(),
367
if (!_class_file_load_hook_ever_enabled) {
368
_class_file_load_hook_ever_enabled = true;
370
if (get_capabilities()->can_retransform_classes) {
371
_is_retransformable = true;
373
_is_retransformable = false;
375
// cannot add retransform capability after ClassFileLoadHook has been enabled
376
get_prohibited_capabilities()->can_retransform_classes = 1;
383
JvmtiEnvBase::record_class_file_load_hook_enabled() {
384
if (!_class_file_load_hook_ever_enabled) {
385
if (Threads::number_of_threads() == 0) {
386
record_first_time_class_file_load_hook_enabled();
388
MutexLocker mu(JvmtiThreadState_lock);
389
record_first_time_class_file_load_hook_enabled();
396
JvmtiEnvBase::set_native_method_prefixes(jint prefix_count, char** prefixes) {
397
assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(),
400
int old_prefix_count = get_native_method_prefix_count();
401
char **old_prefixes = get_native_method_prefixes();
403
// allocate and install the new prefixex
404
if (prefix_count == 0 || !is_valid()) {
405
_native_method_prefix_count = 0;
406
_native_method_prefixes = nullptr;
408
// there are prefixes, allocate an array to hold them, and fill it
409
char** new_prefixes = (char**)os::malloc((prefix_count) * sizeof(char*), mtInternal);
410
if (new_prefixes == nullptr) {
411
return JVMTI_ERROR_OUT_OF_MEMORY;
413
for (int i = 0; i < prefix_count; i++) {
414
char* prefix = prefixes[i];
415
if (prefix == nullptr) {
416
for (int j = 0; j < (i-1); j++) {
417
os::free(new_prefixes[j]);
419
os::free(new_prefixes);
420
return JVMTI_ERROR_NULL_POINTER;
422
prefix = os::strdup(prefixes[i]);
423
if (prefix == nullptr) {
424
for (int j = 0; j < (i-1); j++) {
425
os::free(new_prefixes[j]);
427
os::free(new_prefixes);
428
return JVMTI_ERROR_OUT_OF_MEMORY;
430
new_prefixes[i] = prefix;
432
_native_method_prefix_count = prefix_count;
433
_native_method_prefixes = new_prefixes;
436
// now that we know the new prefixes have been successfully installed we can
437
// safely remove the old ones
438
if (old_prefix_count != 0) {
439
for (int i = 0; i < old_prefix_count; i++) {
440
os::free(old_prefixes[i]);
442
os::free(old_prefixes);
445
return JVMTI_ERROR_NONE;
449
// Collect all the prefixes which have been set in any JVM TI environments
450
// by the SetNativeMethodPrefix(es) functions. Be sure to maintain the
451
// order of environments and the order of prefixes within each environment.
452
// Return in a resource allocated array.
454
JvmtiEnvBase::get_all_native_method_prefixes(int* count_ptr) {
455
assert(Threads::number_of_threads() == 0 ||
456
SafepointSynchronize::is_at_safepoint() ||
457
JvmtiThreadState_lock->is_locked(),
461
GrowableArray<char*>* prefix_array =new GrowableArray<char*>(5);
464
for (JvmtiEnvBase* env = it.first(); env != nullptr; env = it.next(env)) {
465
int prefix_count = env->get_native_method_prefix_count();
466
char** prefixes = env->get_native_method_prefixes();
467
for (int j = 0; j < prefix_count; j++) {
468
// retrieve a prefix and so that it is safe against asynchronous changes
469
// copy it into the resource area
470
char* prefix = prefixes[j];
471
char* prefix_copy = NEW_RESOURCE_ARRAY(char, strlen(prefix)+1);
472
strcpy(prefix_copy, prefix);
473
prefix_array->at_put_grow(total_count++, prefix_copy);
477
char** all_prefixes = NEW_RESOURCE_ARRAY(char*, total_count);
478
char** p = all_prefixes;
479
for (int i = 0; i < total_count; ++i) {
480
*p++ = prefix_array->at(i);
482
*count_ptr = total_count;
487
JvmtiEnvBase::set_event_callbacks(const jvmtiEventCallbacks* callbacks,
488
jint size_of_callbacks) {
489
assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
491
size_t byte_cnt = sizeof(jvmtiEventCallbacks);
493
// clear in either case to be sure we got any gap between sizes
494
memset(&_event_callbacks, 0, byte_cnt);
496
// Now that JvmtiThreadState_lock is held, prevent a possible race condition where events
497
// are re-enabled by a call to set event callbacks where the DisposeEnvironment
498
// occurs after the boiler-plate environment check and before the lock is acquired.
499
if (callbacks != nullptr && is_valid()) {
500
if (size_of_callbacks < (jint)byte_cnt) {
501
byte_cnt = size_of_callbacks;
503
memcpy(&_event_callbacks, callbacks, byte_cnt);
508
// In the fullness of time, all users of the method should instead
509
// directly use allocate, besides being cleaner and faster, this will
510
// mean much better out of memory handling
512
JvmtiEnvBase::jvmtiMalloc(jlong size) {
513
unsigned char* mem = nullptr;
514
jvmtiError result = allocate(size, &mem);
515
assert(result == JVMTI_ERROR_NONE, "Allocate failed");
522
jobject JvmtiEnvBase::jni_reference(Handle hndl) {
523
return JNIHandles::make_local(hndl());
526
jobject JvmtiEnvBase::jni_reference(JavaThread *thread, Handle hndl) {
527
return JNIHandles::make_local(thread, hndl());
530
void JvmtiEnvBase::destroy_jni_reference(jobject jobj) {
531
JNIHandles::destroy_local(jobj);
534
void JvmtiEnvBase::destroy_jni_reference(JavaThread *thread, jobject jobj) {
535
JNIHandles::destroy_local(jobj); // thread is unused.
543
JvmtiEnvBase::new_jthreadArray(int length, Handle *handles) {
548
jthread* objArray = (jthread *) jvmtiMalloc(sizeof(jthread) * length);
549
NULL_CHECK(objArray, nullptr);
551
for (int i = 0; i < length; i++) {
552
objArray[i] = (jthread)jni_reference(handles[i]);
558
JvmtiEnvBase::new_jthreadGroupArray(int length, objArrayHandle groups) {
563
jthreadGroup* objArray = (jthreadGroup *) jvmtiMalloc(sizeof(jthreadGroup) * length);
564
NULL_CHECK(objArray, nullptr);
566
for (int i = 0; i < length; i++) {
567
objArray[i] = (jthreadGroup)JNIHandles::make_local(groups->obj_at(i));
572
// Return the vframe on the specified thread and depth, null if no such frame.
573
// The thread and the oops in the returned vframe might not have been processed.
575
JvmtiEnvBase::jvf_for_thread_and_depth(JavaThread* java_thread, jint depth) {
576
if (!java_thread->has_last_Java_frame()) {
579
RegisterMap reg_map(java_thread,
580
RegisterMap::UpdateMap::include,
581
RegisterMap::ProcessFrames::skip,
582
RegisterMap::WalkContinuation::include);
583
javaVFrame *jvf = java_thread->last_java_vframe(®_map);
585
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();
594
// utilities: JNI objects
599
JvmtiEnvBase::get_jni_class_non_null(Klass* k) {
600
assert(k != nullptr, "k != null");
601
Thread *thread = Thread::current();
602
return (jclass)jni_reference(Handle(thread, k->java_mirror()));
610
JvmtiEnvBase::get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd) {
611
if (!jfieldIDWorkaround::is_valid_jfieldID(k, field)) {
615
if (jfieldIDWorkaround::is_static_jfieldID(field)) {
616
JNIid* id = jfieldIDWorkaround::from_static_jfieldID(field);
617
found = id->find_local_field(fd);
619
// Non-static field. The fieldID is really the offset of the field within the object.
620
int offset = jfieldIDWorkaround::from_instance_jfieldID(k, field);
621
found = InstanceKlass::cast(k)->find_field_from_offset(offset, false, fd);
627
JvmtiEnvBase::is_vthread_alive(oop vt) {
628
oop cont = java_lang_VirtualThread::continuation(vt);
629
return !jdk_internal_vm_Continuation::done(cont) &&
630
java_lang_VirtualThread::state(vt) != java_lang_VirtualThread::NEW;
633
// Return JavaThread if virtual thread is mounted, null otherwise.
634
JavaThread* JvmtiEnvBase::get_JavaThread_or_null(oop vthread) {
635
oop carrier_thread = java_lang_VirtualThread::carrier_thread(vthread);
636
if (carrier_thread == nullptr) {
640
JavaThread* java_thread = java_lang_Thread::thread(carrier_thread);
642
// This could be a different thread to the current one. So we need to ensure that
643
// processing has started before we are allowed to read the continuation oop of
644
// another thread, as it is a direct root of that other thread.
645
StackWatermarkSet::start_processing(java_thread, StackWatermarkKind::gc);
647
oop cont = java_lang_VirtualThread::continuation(vthread);
648
assert(cont != nullptr, "must be");
649
assert(Continuation::continuation_scope(cont) == java_lang_VirtualThread::vthread_scope(), "must be");
650
return Continuation::is_continuation_mounted(java_thread, cont) ? java_thread : nullptr;
654
JvmtiEnvBase::check_and_skip_hidden_frames(bool is_in_VTMS_transition, javaVFrame* jvf) {
655
// The second condition is needed to hide notification methods.
656
if (!is_in_VTMS_transition && (jvf == nullptr || !jvf->method()->jvmti_mount_transition())) {
657
return jvf; // No frames to skip.
659
// Find jvf with a method annotated with @JvmtiMountTransition.
660
for ( ; jvf != nullptr; jvf = jvf->java_sender()) {
661
if (jvf->method()->jvmti_mount_transition()) { // Cannot actually appear in an unmounted continuation; they're never frozen.
662
jvf = jvf->java_sender(); // Skip annotated method.
665
if (jvf->method()->changes_current_thread()) {
668
// Skip frame above annotated method.
674
JvmtiEnvBase::check_and_skip_hidden_frames(JavaThread* jt, javaVFrame* jvf) {
675
jvf = check_and_skip_hidden_frames(jt->is_in_VTMS_transition(), jvf);
680
JvmtiEnvBase::check_and_skip_hidden_frames(oop vthread, javaVFrame* jvf) {
681
JvmtiThreadState* state = java_lang_Thread::jvmti_thread_state(vthread);
682
if (state == nullptr) {
686
jvf = check_and_skip_hidden_frames(java_lang_Thread::is_in_VTMS_transition(vthread), jvf);
691
JvmtiEnvBase::get_vthread_jvf(oop vthread) {
692
assert(java_lang_VirtualThread::state(vthread) != java_lang_VirtualThread::NEW, "sanity check");
693
assert(java_lang_VirtualThread::state(vthread) != java_lang_VirtualThread::TERMINATED, "sanity check");
695
Thread* cur_thread = Thread::current();
696
oop cont = java_lang_VirtualThread::continuation(vthread);
697
javaVFrame* jvf = nullptr;
699
JavaThread* java_thread = get_JavaThread_or_null(vthread);
700
if (java_thread != nullptr) {
701
if (!java_thread->has_last_Java_frame()) {
702
// TBD: This is a temporary work around to avoid a guarantee caused by
703
// the native enterSpecial frame on the top. No frames will be found
704
// by the JVMTI functions such as GetStackTrace.
707
vframeStream vfs(java_thread);
708
jvf = vfs.at_end() ? nullptr : vfs.asJavaVFrame();
709
jvf = check_and_skip_hidden_frames(java_thread, jvf);
711
vframeStream vfs(cont);
712
jvf = vfs.at_end() ? nullptr : vfs.asJavaVFrame();
713
jvf = check_and_skip_hidden_frames(vthread, jvf);
718
// Return correct javaVFrame for a carrier (non-virtual) thread.
719
// It strips vthread frames at the top if there are any.
721
JvmtiEnvBase::get_cthread_last_java_vframe(JavaThread* jt, RegisterMap* reg_map_p) {
722
// Strip vthread frames in case of carrier thread with mounted continuation.
723
bool cthread_with_cont = JvmtiEnvBase::is_cthread_with_continuation(jt);
724
javaVFrame *jvf = cthread_with_cont ? jt->carrier_last_java_vframe(reg_map_p)
725
: jt->last_java_vframe(reg_map_p);
726
// Skip hidden frames only for carrier threads
727
// which are in non-temporary VTMS transition.
728
if (jt->is_in_VTMS_transition()) {
729
jvf = check_and_skip_hidden_frames(jt, jvf);
735
JvmtiEnvBase::get_thread_state_base(oop thread_oop, JavaThread* jt) {
738
if (thread_oop != nullptr) {
739
// Get most state bits.
740
state = (jint)java_lang_Thread::get_thread_status(thread_oop);
743
// We have a JavaThread* so add more state bits.
744
JavaThreadState jts = jt->thread_state();
746
if (jt->is_carrier_thread_suspended() ||
747
((jt->jvmti_vthread() == nullptr || jt->jvmti_vthread() == thread_oop) && jt->is_suspended())) {
748
// Suspended non-virtual thread.
749
state |= JVMTI_THREAD_STATE_SUSPENDED;
751
if (jts == _thread_in_native) {
752
state |= JVMTI_THREAD_STATE_IN_NATIVE;
754
if (jt->is_interrupted(false)) {
755
state |= JVMTI_THREAD_STATE_INTERRUPTED;
762
JvmtiEnvBase::get_thread_state(oop thread_oop, JavaThread* jt) {
765
if (is_thread_carrying_vthread(jt, thread_oop)) {
766
state = (jint)java_lang_Thread::get_thread_status(thread_oop);
768
// This is for extra safety. Other bits are not expected nor needed.
769
state &= (JVMTI_THREAD_STATE_ALIVE | JVMTI_THREAD_STATE_INTERRUPTED);
771
if (jt->is_carrier_thread_suspended()) {
772
state |= JVMTI_THREAD_STATE_SUSPENDED;
774
// It's okay for the JVMTI state to be reported as WAITING when waiting
775
// for something other than an Object.wait. So, we treat a thread carrying
776
// a virtual thread as waiting indefinitely which is not runnable.
777
// It is why the RUNNABLE bit is not needed and the WAITING bits are added.
778
state |= JVMTI_THREAD_STATE_WAITING | JVMTI_THREAD_STATE_WAITING_INDEFINITELY;
780
state = get_thread_state_base(thread_oop, jt);
786
JvmtiEnvBase::get_vthread_state(oop thread_oop, JavaThread* java_thread) {
788
bool ext_suspended = JvmtiVTSuspender::is_vthread_suspended(thread_oop);
789
jint interrupted = java_lang_Thread::interrupted(thread_oop);
791
if (java_thread != nullptr) {
792
// If virtual thread is blocked on a monitor enter the BLOCKED_ON_MONITOR_ENTER bit
793
// is set for carrier thread instead of virtual.
794
// Other state bits except filtered ones are expected to be the same.
795
oop ct_oop = java_lang_VirtualThread::carrier_thread(thread_oop);
796
jint filtered_bits = JVMTI_THREAD_STATE_SUSPENDED | JVMTI_THREAD_STATE_INTERRUPTED;
798
// This call can trigger a safepoint, so thread_oop must not be used after it.
799
state = get_thread_state_base(ct_oop, java_thread) & ~filtered_bits;
801
int vt_state = java_lang_VirtualThread::state(thread_oop);
802
state = (jint)java_lang_VirtualThread::map_state_to_thread_status(vt_state);
804
// Ensure the thread has not exited after retrieving suspended/interrupted values.
805
if ((state & JVMTI_THREAD_STATE_ALIVE) != 0) {
807
state |= JVMTI_THREAD_STATE_SUSPENDED;
810
state |= JVMTI_THREAD_STATE_INTERRUPTED;
817
JvmtiEnvBase::get_thread_or_vthread_state(oop thread_oop, JavaThread* java_thread) {
819
if (java_lang_VirtualThread::is_instance(thread_oop)) {
820
state = JvmtiEnvBase::get_vthread_state(thread_oop, java_thread);
822
state = JvmtiEnvBase::get_thread_state(thread_oop, java_thread);
828
JvmtiEnvBase::get_live_threads(JavaThread* current_thread, Handle group_hdl, jint *count_ptr, Handle **thread_objs_p) {
830
Handle *thread_objs = nullptr;
831
ThreadsListEnumerator tle(current_thread, /* include_jvmti_agent_threads */ true);
832
int nthreads = tle.num_threads();
834
thread_objs = NEW_RESOURCE_ARRAY_RETURN_NULL(Handle, nthreads);
835
NULL_CHECK(thread_objs, JVMTI_ERROR_OUT_OF_MEMORY);
836
for (int i = 0; i < nthreads; i++) {
837
Handle thread = tle.get_threadObj(i);
838
if (thread()->is_a(vmClasses::Thread_klass()) && java_lang_Thread::threadGroup(thread()) == group_hdl()) {
839
thread_objs[count++] = thread;
843
*thread_objs_p = thread_objs;
845
return JVMTI_ERROR_NONE;
849
JvmtiEnvBase::get_subgroups(JavaThread* current_thread, Handle group_hdl, jint *count_ptr, objArrayHandle *group_objs_p) {
851
// This call collects the strong and weak groups
852
JavaThread* THREAD = current_thread;
853
JavaValue result(T_OBJECT);
854
JavaCalls::call_virtual(&result,
856
vmClasses::ThreadGroup_klass(),
857
SymbolTable::new_permanent_symbol("subgroupsAsArray"),
858
vmSymbols::void_threadgroup_array_signature(),
860
if (HAS_PENDING_EXCEPTION) {
861
Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
862
CLEAR_PENDING_EXCEPTION;
863
if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
864
return JVMTI_ERROR_OUT_OF_MEMORY;
866
return JVMTI_ERROR_INTERNAL;
870
assert(result.get_type() == T_OBJECT, "just checking");
871
objArrayOop groups = (objArrayOop)result.get_oop();
873
*count_ptr = groups == nullptr ? 0 : groups->length();
874
*group_objs_p = objArrayHandle(current_thread, groups);
876
return JVMTI_ERROR_NONE;
880
// Object Monitor Information
884
// Count the number of objects for a lightweight monitor. The hobj
885
// parameter is object that owns the monitor so this routine will
886
// count the number of times the same object was locked by frames
890
JvmtiEnvBase::count_locked_objects(JavaThread *java_thread, Handle hobj) {
892
if (!java_thread->has_last_Java_frame()) {
893
return ret; // no Java frames so no monitors
896
Thread* current_thread = Thread::current();
897
ResourceMark rm(current_thread);
898
HandleMark hm(current_thread);
899
RegisterMap reg_map(java_thread,
900
RegisterMap::UpdateMap::include,
901
RegisterMap::ProcessFrames::include,
902
RegisterMap::WalkContinuation::skip);
904
for (javaVFrame *jvf = java_thread->last_java_vframe(®_map); jvf != nullptr;
905
jvf = jvf->java_sender()) {
906
GrowableArray<MonitorInfo*>* mons = jvf->monitors();
907
if (!mons->is_empty()) {
908
for (int i = 0; i < mons->length(); i++) {
909
MonitorInfo *mi = mons->at(i);
910
if (mi->owner_is_scalar_replaced()) continue;
912
// see if owner of the monitor is our object
913
if (mi->owner() != nullptr && mi->owner() == hobj()) {
923
JvmtiEnvBase::get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread,
924
jobject *monitor_ptr, bool is_virtual) {
925
Thread *current_thread = Thread::current();
926
assert(java_thread->is_handshake_safe_for(current_thread),
927
"call by myself or at handshake");
928
if (!is_virtual && JvmtiEnvBase::is_cthread_with_continuation(java_thread)) {
929
// Carrier thread with a mounted continuation case.
930
// No contended monitor can be owned by carrier thread in this case.
931
*monitor_ptr = nullptr;
932
return JVMTI_ERROR_NONE;
935
// The ObjectMonitor* can't be async deflated since we are either
936
// at a safepoint or the calling thread is operating on itself so
937
// it cannot leave the underlying wait()/enter() call.
938
ObjectMonitor *mon = java_thread->current_waiting_monitor();
939
if (mon == nullptr) {
940
// thread is not doing an Object.wait() call
941
mon = java_thread->current_pending_monitor();
942
if (mon != nullptr) {
943
// The thread is trying to enter() an ObjectMonitor.
945
assert(obj != nullptr, "ObjectMonitor should have a valid object!");
948
// thread is doing an Object.wait() call
949
oop thread_oop = get_vthread_or_thread_oop(java_thread);
950
jint state = get_thread_or_vthread_state(thread_oop, java_thread);
952
if (state & JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER) {
953
// thread is re-entering the monitor in an Object.wait() call
955
assert(obj != nullptr, "Object.wait() should have an object");
959
if (obj == nullptr) {
960
*monitor_ptr = nullptr;
962
HandleMark hm(current_thread);
963
Handle hobj(current_thread, obj);
964
*monitor_ptr = jni_reference(calling_thread, hobj);
966
return JVMTI_ERROR_NONE;
970
JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
971
GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
973
// calling_thread is the thread that requested the list of monitors for java_thread.
974
// java_thread is the thread owning the monitors.
975
// current_thread is the thread executing this code, can be a non-JavaThread (e.g. VM Thread).
976
// And they all may be different threads.
977
jvmtiError err = JVMTI_ERROR_NONE;
978
Thread *current_thread = Thread::current();
979
assert(java_thread->is_handshake_safe_for(current_thread),
980
"call by myself or at handshake");
982
if (JvmtiEnvBase::is_cthread_with_continuation(java_thread)) {
983
// Carrier thread with a mounted continuation case.
984
// No contended monitor can be owned by carrier thread in this case.
985
return JVMTI_ERROR_NONE;
987
if (java_thread->has_last_Java_frame()) {
988
ResourceMark rm(current_thread);
989
HandleMark hm(current_thread);
990
RegisterMap reg_map(java_thread,
991
RegisterMap::UpdateMap::include,
992
RegisterMap::ProcessFrames::include,
993
RegisterMap::WalkContinuation::skip);
996
for (javaVFrame *jvf = get_cthread_last_java_vframe(java_thread, ®_map);
997
jvf != nullptr; jvf = jvf->java_sender()) {
998
if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) { // check for stack too deep
999
// add locked objects for this frame into list
1000
err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
1001
if (err != JVMTI_ERROR_NONE) {
1008
// Get off stack monitors. (e.g. acquired via jni MonitorEnter).
1009
JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
1010
ObjectSynchronizer::owned_monitors_iterate(&jmc, java_thread);
1017
JvmtiEnvBase::get_owned_monitors(JavaThread* calling_thread, JavaThread* java_thread, javaVFrame* jvf,
1018
GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
1019
jvmtiError err = JVMTI_ERROR_NONE;
1020
Thread *current_thread = Thread::current();
1021
assert(java_thread->is_handshake_safe_for(current_thread),
1022
"call by myself or at handshake");
1025
for ( ; jvf != nullptr; jvf = jvf->java_sender()) {
1026
if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) { // check for stack too deep
1027
// Add locked objects for this frame into list.
1028
err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth - 1);
1029
if (err != JVMTI_ERROR_NONE) {
1035
// Get off stack monitors. (e.g. acquired via jni MonitorEnter).
1036
JvmtiMonitorClosure jmc(calling_thread, owned_monitors_list, this);
1037
ObjectSynchronizer::owned_monitors_iterate(&jmc, java_thread);
1043
// Save JNI local handles for any objects that this frame owns.
1045
JvmtiEnvBase::get_locked_objects_in_frame(JavaThread* calling_thread, JavaThread* java_thread,
1046
javaVFrame *jvf, GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list, jint stack_depth) {
1047
jvmtiError err = JVMTI_ERROR_NONE;
1048
Thread* current_thread = Thread::current();
1049
ResourceMark rm(current_thread);
1050
HandleMark hm(current_thread);
1052
GrowableArray<MonitorInfo*>* mons = jvf->monitors();
1053
if (mons->is_empty()) {
1054
return err; // this javaVFrame holds no monitors
1057
oop wait_obj = nullptr;
1059
// The ObjectMonitor* can't be async deflated since we are either
1060
// at a safepoint or the calling thread is operating on itself so
1061
// it cannot leave the underlying wait() call.
1062
// Save object of current wait() call (if any) for later comparison.
1063
ObjectMonitor *mon = java_thread->current_waiting_monitor();
1064
if (mon != nullptr) {
1065
wait_obj = mon->object();
1068
oop pending_obj = nullptr;
1070
// The ObjectMonitor* can't be async deflated since we are either
1071
// at a safepoint or the calling thread is operating on itself so
1072
// it cannot leave the underlying enter() call.
1073
// Save object of current enter() call (if any) for later comparison.
1074
ObjectMonitor *mon = java_thread->current_pending_monitor();
1075
if (mon != nullptr) {
1076
pending_obj = mon->object();
1080
for (int i = 0; i < mons->length(); i++) {
1081
MonitorInfo *mi = mons->at(i);
1083
if (mi->owner_is_scalar_replaced()) continue;
1085
oop obj = mi->owner();
1086
if (obj == nullptr) {
1087
// this monitor doesn't have an owning object so skip it
1091
if (wait_obj == obj) {
1092
// the thread is waiting on this monitor so it isn't really owned
1096
if (pending_obj == obj) {
1097
// the thread is pending on this monitor so it isn't really owned
1101
if (owned_monitors_list->length() > 0) {
1102
// Our list has at least one object on it so we have to check
1103
// for recursive object locking
1105
for (int j = 0; j < owned_monitors_list->length(); j++) {
1106
jobject jobj = ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(j))->monitor;
1107
oop check = JNIHandles::resolve(jobj);
1109
found = true; // we found the object
1115
// already have this object so don't include it
1120
// add the owning object to our list
1121
jvmtiMonitorStackDepthInfo *jmsdi;
1122
err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
1123
if (err != JVMTI_ERROR_NONE) {
1126
Handle hobj(Thread::current(), obj);
1127
jmsdi->monitor = jni_reference(calling_thread, hobj);
1128
jmsdi->stack_depth = stack_depth;
1129
owned_monitors_list->append(jmsdi);
1136
JvmtiEnvBase::get_stack_trace(javaVFrame *jvf,
1137
jint start_depth, jint max_count,
1138
jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1139
Thread *current_thread = Thread::current();
1140
ResourceMark rm(current_thread);
1141
HandleMark hm(current_thread);
1144
if (start_depth != 0) {
1145
if (start_depth > 0) {
1146
for (int j = 0; j < start_depth && jvf != nullptr; j++) {
1147
jvf = jvf->java_sender();
1149
if (jvf == nullptr) {
1150
// start_depth is deeper than the stack depth.
1151
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1153
} else { // start_depth < 0
1154
// We are referencing the starting depth based on the oldest
1155
// part of the stack.
1156
// Optimize to limit the number of times that java_sender() is called.
1157
javaVFrame *jvf_cursor = jvf;
1158
javaVFrame *jvf_prev = nullptr;
1159
javaVFrame *jvf_prev_prev = nullptr;
1161
while (jvf_cursor != nullptr) {
1162
jvf_prev_prev = jvf_prev;
1163
jvf_prev = jvf_cursor;
1164
for (j = 0; j > start_depth && jvf_cursor != nullptr; j--) {
1165
jvf_cursor = jvf_cursor->java_sender();
1168
if (j == start_depth) {
1169
// Previous pointer is exactly where we want to start.
1172
// We need to back up further to get to the right place.
1173
if (jvf_prev_prev == nullptr) {
1174
// The -start_depth is greater than the stack depth.
1175
return JVMTI_ERROR_ILLEGAL_ARGUMENT;
1177
// j is now the number of frames on the stack starting with
1178
// jvf_prev, we start from jvf_prev_prev and move older on
1179
// the stack that many, and the result is -start_depth frames
1181
jvf = jvf_prev_prev;
1182
for (; j < 0; j++) {
1183
jvf = jvf->java_sender();
1188
for (; count < max_count && jvf != nullptr; count++) {
1189
frame_buffer[count].method = jvf->method()->jmethod_id();
1190
frame_buffer[count].location = (jvf->method()->is_native() ? -1 : jvf->bci());
1191
jvf = jvf->java_sender();
1194
return JVMTI_ERROR_NONE;
1198
JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
1199
jint start_depth, jint max_count,
1200
jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
1201
Thread *current_thread = Thread::current();
1202
assert(SafepointSynchronize::is_at_safepoint() ||
1203
java_thread->is_handshake_safe_for(current_thread),
1204
"call by myself / at safepoint / at handshake");
1206
jvmtiError err = JVMTI_ERROR_NONE;
1208
if (java_thread->has_last_Java_frame()) {
1209
RegisterMap reg_map(java_thread,
1210
RegisterMap::UpdateMap::include,
1211
RegisterMap::ProcessFrames::skip,
1212
RegisterMap::WalkContinuation::skip);
1213
ResourceMark rm(current_thread);
1214
javaVFrame *jvf = get_cthread_last_java_vframe(java_thread, ®_map);
1216
err = get_stack_trace(jvf, start_depth, max_count, frame_buffer, count_ptr);
1219
if (start_depth != 0) {
1220
// no frames and there is a starting depth
1221
err = JVMTI_ERROR_ILLEGAL_ARGUMENT;
1228
JvmtiEnvBase::get_frame_count(javaVFrame *jvf) {
1231
while (jvf != nullptr) {
1232
jvf = jvf->java_sender();
1239
JvmtiEnvBase::get_frame_count(JavaThread* jt, jint *count_ptr) {
1240
Thread *current_thread = Thread::current();
1241
assert(current_thread == jt ||
1242
SafepointSynchronize::is_at_safepoint() ||
1243
jt->is_handshake_safe_for(current_thread),
1244
"call by myself / at safepoint / at handshake");
1246
if (!jt->has_last_Java_frame()) { // no Java frames
1249
ResourceMark rm(current_thread);
1250
RegisterMap reg_map(jt,
1251
RegisterMap::UpdateMap::include,
1252
RegisterMap::ProcessFrames::include,
1253
RegisterMap::WalkContinuation::skip);
1254
javaVFrame *jvf = get_cthread_last_java_vframe(jt, ®_map);
1256
*count_ptr = get_frame_count(jvf);
1258
return JVMTI_ERROR_NONE;
1262
JvmtiEnvBase::get_frame_count(oop vthread_oop, jint *count_ptr) {
1263
Thread *current_thread = Thread::current();
1264
ResourceMark rm(current_thread);
1265
javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(vthread_oop);
1267
*count_ptr = get_frame_count(jvf);
1268
return JVMTI_ERROR_NONE;
1272
JvmtiEnvBase::get_frame_location(javaVFrame* jvf, jint depth,
1273
jmethodID* method_ptr, jlocation* location_ptr) {
1276
while (jvf != nullptr && cur_depth < depth) {
1277
jvf = jvf->java_sender();
1280
assert(depth >= cur_depth, "ran out of frames too soon");
1281
if (jvf == nullptr) {
1282
return JVMTI_ERROR_NO_MORE_FRAMES;
1284
Method* method = jvf->method();
1285
if (method->is_native()) {
1288
*location_ptr = jvf->bci();
1290
*method_ptr = method->jmethod_id();
1291
return JVMTI_ERROR_NONE;
1295
JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth,
1296
jmethodID* method_ptr, jlocation* location_ptr) {
1297
Thread* current = Thread::current();
1298
assert(java_thread->is_handshake_safe_for(current),
1299
"call by myself or at handshake");
1300
if (!java_thread->has_last_Java_frame()) {
1301
return JVMTI_ERROR_NO_MORE_FRAMES;
1303
ResourceMark rm(current);
1304
HandleMark hm(current);
1305
RegisterMap reg_map(java_thread,
1306
RegisterMap::UpdateMap::include,
1307
RegisterMap::ProcessFrames::skip,
1308
RegisterMap::WalkContinuation::include);
1309
javaVFrame* jvf = JvmtiEnvBase::get_cthread_last_java_vframe(java_thread, ®_map);
1311
return get_frame_location(jvf, depth, method_ptr, location_ptr);
1315
JvmtiEnvBase::get_frame_location(oop vthread_oop, jint depth,
1316
jmethodID* method_ptr, jlocation* location_ptr) {
1317
Thread* current = Thread::current();
1318
ResourceMark rm(current);
1319
HandleMark hm(current);
1320
javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(vthread_oop);
1322
return get_frame_location(jvf, depth, method_ptr, location_ptr);
1326
JvmtiEnvBase::set_frame_pop(JvmtiThreadState* state, javaVFrame* jvf, jint depth) {
1327
for (int d = 0; jvf != nullptr && d < depth; d++) {
1328
jvf = jvf->java_sender();
1330
if (jvf == nullptr) {
1331
return JVMTI_ERROR_NO_MORE_FRAMES;
1333
if (jvf->method()->is_native()) {
1334
return JVMTI_ERROR_OPAQUE_FRAME;
1336
assert(jvf->frame_pointer() != nullptr, "frame pointer mustn't be null");
1337
int frame_number = (int)get_frame_count(jvf);
1338
state->env_thread_state((JvmtiEnvBase*)this)->set_frame_pop(frame_number);
1339
return JVMTI_ERROR_NONE;
1343
JvmtiEnvBase::is_cthread_with_mounted_vthread(JavaThread* jt) {
1344
oop thread_oop = jt->threadObj();
1345
assert(thread_oop != nullptr, "sanity check");
1346
oop mounted_vt = jt->jvmti_vthread();
1348
return mounted_vt != nullptr && mounted_vt != thread_oop;
1352
JvmtiEnvBase::is_cthread_with_continuation(JavaThread* jt) {
1353
const ContinuationEntry* cont_entry = nullptr;
1354
if (jt->has_last_Java_frame()) {
1355
cont_entry = jt->vthread_continuation();
1357
return cont_entry != nullptr && is_cthread_with_mounted_vthread(jt);
1360
// Check if VirtualThread or BoundVirtualThread is suspended.
1362
JvmtiEnvBase::is_vthread_suspended(oop vt_oop, JavaThread* jt) {
1363
bool suspended = false;
1364
if (java_lang_VirtualThread::is_instance(vt_oop)) {
1365
suspended = JvmtiVTSuspender::is_vthread_suspended(vt_oop);
1367
if (vt_oop->is_a(vmClasses::BoundVirtualThread_klass())) {
1368
suspended = jt->is_suspended();
1373
// If (thread == null) then return current thread object.
1374
// Otherwise return JNIHandles::resolve_external_guard(thread).
1376
JvmtiEnvBase::current_thread_obj_or_resolve_external_guard(jthread thread) {
1377
oop thread_obj = JNIHandles::resolve_external_guard(thread);
1378
if (thread == nullptr) {
1379
thread_obj = get_vthread_or_thread_oop(JavaThread::current());
1385
JvmtiEnvBase::get_threadOop_and_JavaThread(ThreadsList* t_list, jthread thread, JavaThread* cur_thread,
1386
JavaThread** jt_pp, oop* thread_oop_p) {
1387
JavaThread* java_thread = nullptr;
1388
oop thread_oop = nullptr;
1390
if (thread == nullptr) {
1391
if (cur_thread == nullptr) { // cur_thread can be null when called from a VM_op
1392
return JVMTI_ERROR_INVALID_THREAD;
1394
java_thread = cur_thread;
1395
thread_oop = get_vthread_or_thread_oop(java_thread);
1396
if (thread_oop == nullptr || !thread_oop->is_a(vmClasses::Thread_klass())) {
1397
return JVMTI_ERROR_INVALID_THREAD;
1400
jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(t_list, thread, &java_thread, &thread_oop);
1401
if (err != JVMTI_ERROR_NONE) {
1402
// We got an error code so we don't have a JavaThread*, but only return
1403
// an error from here if we didn't get a valid thread_oop. In a vthread case
1404
// the cv_external_thread_to_JavaThread is expected to correctly set the
1405
// thread_oop and return JVMTI_ERROR_INVALID_THREAD which we ignore here.
1406
if (thread_oop == nullptr || err != JVMTI_ERROR_INVALID_THREAD) {
1407
*thread_oop_p = thread_oop;
1411
if (java_thread == nullptr && java_lang_VirtualThread::is_instance(thread_oop)) {
1412
java_thread = get_JavaThread_or_null(thread_oop);
1415
*jt_pp = java_thread;
1416
*thread_oop_p = thread_oop;
1417
if (java_lang_VirtualThread::is_instance(thread_oop) &&
1418
!JvmtiEnvBase::is_vthread_alive(thread_oop)) {
1419
return JVMTI_ERROR_THREAD_NOT_ALIVE;
1421
return JVMTI_ERROR_NONE;
1424
// Check for JVMTI_ERROR_NOT_SUSPENDED and JVMTI_ERROR_OPAQUE_FRAME errors.
1425
// Used in PopFrame and ForceEarlyReturn implementations.
1427
JvmtiEnvBase::check_non_suspended_or_opaque_frame(JavaThread* jt, oop thr_obj, bool self) {
1428
bool is_virtual = thr_obj != nullptr && thr_obj->is_a(vmClasses::BaseVirtualThread_klass());
1431
if (!is_JavaThread_current(jt, thr_obj)) {
1432
if (!is_vthread_suspended(thr_obj, jt)) {
1433
return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1435
if (jt == nullptr) { // unmounted virtual thread
1436
return JVMTI_ERROR_OPAQUE_FRAME;
1439
} else { // platform thread
1440
if (!self && !jt->is_suspended() &&
1441
!jt->is_carrier_thread_suspended()) {
1442
return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1445
return JVMTI_ERROR_NONE;
1449
JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
1450
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1451
Thread* current_thread = VMThread::vm_thread();
1452
assert(current_thread == Thread::current(), "must be");
1454
HandleMark hm(current_thread);
1459
oop mirror = JNIHandles::resolve_external_guard(object);
1460
NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
1461
NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
1463
hobj = Handle(current_thread, mirror);
1466
ThreadsListHandle tlh(current_thread);
1467
JavaThread *owning_thread = nullptr;
1468
ObjectMonitor *mon = nullptr;
1469
jvmtiMonitorUsage ret = {
1470
nullptr, 0, 0, nullptr, 0, nullptr
1473
uint32_t debug_bits = 0;
1474
// first derive the object's owner and entry_count (if any)
1475
owning_thread = ObjectSynchronizer::get_lock_owner(tlh.list(), hobj);
1476
if (owning_thread != nullptr) {
1477
oop thread_oop = get_vthread_or_thread_oop(owning_thread);
1478
bool is_virtual = thread_oop->is_a(vmClasses::BaseVirtualThread_klass());
1480
thread_oop = nullptr;
1482
Handle th(current_thread, thread_oop);
1483
ret.owner = (jthread)jni_reference(calling_thread, th);
1485
// The recursions field of a monitor does not reflect recursions
1486
// as lightweight locks before inflating the monitor are not included.
1487
// We have to count the number of recursive monitor entries the hard way.
1488
// We pass a handle to survive any GCs along the way.
1489
ret.entry_count = is_virtual ? 0 : count_locked_objects(owning_thread, hobj);
1491
// implied else: entry_count == 0
1493
jint nWant = 0, nWait = 0;
1494
markWord mark = hobj->mark();
1495
ResourceMark rm(current_thread);
1496
GrowableArray<JavaThread*>* wantList = nullptr;
1498
if (mark.has_monitor()) {
1499
mon = mark.monitor();
1500
assert(mon != nullptr, "must have monitor");
1501
// this object has a heavyweight monitor
1502
nWant = mon->contentions(); // # of threads contending for monitor entry, but not re-entry
1503
nWait = mon->waiters(); // # of threads waiting for notification,
1504
// or to re-enter monitor, in Object.wait()
1506
// Get the actual set of threads trying to enter, or re-enter, the monitor.
1507
wantList = Threads::get_pending_threads(tlh.list(), nWant + nWait, (address)mon);
1508
nWant = wantList->length();
1510
// this object has a lightweight monitor
1514
if (mon != nullptr) {
1515
// Robustness: the actual waiting list can be smaller.
1516
// The nWait count we got from the mon->waiters() may include the re-entering
1517
// the monitor threads after being notified. Here we are correcting the actual
1518
// number of the waiting threads by excluding those re-entering the monitor.
1520
for (ObjectWaiter* waiter = mon->first_waiter();
1521
waiter != nullptr && (nWait == 0 || waiter != mon->first_waiter());
1522
waiter = mon->next_waiter(waiter)) {
1523
JavaThread *w = mon->thread_of_waiter(waiter);
1524
oop thread_oop = get_vthread_or_thread_oop(w);
1525
if (thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
1531
ret.waiter_count = nWant;
1532
ret.notify_waiter_count = nWait - skipped;
1534
// Allocate memory for heavyweight and lightweight monitor.
1536
err = allocate(ret.waiter_count * sizeof(jthread *), (unsigned char**)&ret.waiters);
1537
if (err != JVMTI_ERROR_NONE) {
1540
err = allocate(ret.notify_waiter_count * sizeof(jthread *),
1541
(unsigned char**)&ret.notify_waiters);
1542
if (err != JVMTI_ERROR_NONE) {
1543
deallocate((unsigned char*)ret.waiters);
1547
// now derive the rest of the fields
1548
if (mon != nullptr) {
1549
// this object has a heavyweight monitor
1551
// null out memory for robustness
1552
if (ret.waiters != nullptr) {
1553
memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *));
1555
if (ret.notify_waiters != nullptr) {
1556
memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *));
1559
if (ret.waiter_count > 0) { // we have contending threads waiting to enter/re-enter the monitor
1560
// identify threads waiting to enter and re-enter the monitor
1561
// get_pending_threads returns only java thread so we do not need to
1562
// check for non java threads.
1563
for (int i = 0; i < nWant; i++) {
1564
JavaThread *pending_thread = wantList->at(i);
1565
Handle th(current_thread, get_vthread_or_thread_oop(pending_thread));
1566
ret.waiters[i] = (jthread)jni_reference(calling_thread, th);
1569
if (ret.notify_waiter_count > 0) { // we have threads waiting to be notified in Object.wait()
1570
ObjectWaiter *waiter = mon->first_waiter();
1572
for (int i = 0; i < nWait; i++) {
1573
JavaThread *w = mon->thread_of_waiter(waiter);
1574
oop thread_oop = get_vthread_or_thread_oop(w);
1575
bool is_virtual = thread_oop->is_a(vmClasses::BaseVirtualThread_klass());
1576
assert(w != nullptr, "sanity check");
1580
// If the thread was found on the ObjectWaiter list, then
1581
// it has not been notified.
1582
Handle th(current_thread, get_vthread_or_thread_oop(w));
1583
ret.notify_waiters[i - skipped] = (jthread)jni_reference(calling_thread, th);
1585
waiter = mon->next_waiter(waiter);
1589
// this object has a lightweight monitor and we have nothing more
1590
// to do here because the defaults are just fine.
1593
// we don't update return parameter unless everything worked
1596
return JVMTI_ERROR_NONE;
1600
JvmtiEnvBase::check_thread_list(jint count, const jthread* list) {
1601
if (list == nullptr && count != 0) {
1602
return JVMTI_ERROR_NULL_POINTER;
1604
for (int i = 0; i < count; i++) {
1605
jthread thread = list[i];
1606
oop thread_oop = JNIHandles::resolve_external_guard(thread);
1607
if (thread_oop == nullptr || !thread_oop->is_a(vmClasses::BaseVirtualThread_klass())) {
1608
return JVMTI_ERROR_INVALID_THREAD;
1611
return JVMTI_ERROR_NONE;
1615
JvmtiEnvBase::is_in_thread_list(jint count, const jthread* list, oop jt_oop) {
1616
for (int idx = 0; idx < count; idx++) {
1617
jthread thread = list[idx];
1618
oop thread_oop = JNIHandles::resolve_external_guard(thread);
1619
if (thread_oop == jt_oop) {
1626
class VM_SetNotifyJvmtiEventsMode : public VM_Operation {
1630
static void correct_jvmti_thread_state(JavaThread* jt) {
1631
oop ct_oop = jt->threadObj();
1632
oop vt_oop = jt->vthread();
1633
JvmtiThreadState* jt_state = jt->jvmti_thread_state();
1634
JvmtiThreadState* ct_state = java_lang_Thread::jvmti_thread_state(jt->threadObj());
1635
JvmtiThreadState* vt_state = vt_oop != nullptr ? java_lang_Thread::jvmti_thread_state(vt_oop) : nullptr;
1636
bool virt = vt_oop != nullptr && java_lang_VirtualThread::is_instance(vt_oop);
1638
// Correct jt->jvmti_thread_state() and jt->jvmti_vthread().
1639
// It was not maintained while notifyJvmti was disabled.
1641
jt->set_jvmti_thread_state(nullptr); // reset jt->jvmti_thread_state()
1642
jt->set_jvmti_vthread(vt_oop); // restore jt->jvmti_vthread()
1644
jt->set_jvmti_thread_state(ct_state); // restore jt->jvmti_thread_state()
1645
jt->set_jvmti_vthread(ct_oop); // restore jt->jvmti_vthread()
1649
// This function is called only if _enable == true.
1650
// Iterates over all JavaThread's, restores jt->jvmti_thread_state() and
1651
// jt->jvmti_vthread() for VTMS transition protocol.
1652
void correct_jvmti_thread_states() {
1653
for (JavaThread* jt : ThreadsListHandle()) {
1654
if (jt->is_in_VTMS_transition()) {
1655
jt->set_VTMS_transition_mark(true);
1656
continue; // no need in JvmtiThreadState correction below if in transition
1658
correct_jvmti_thread_state(jt);
1663
VMOp_Type type() const { return VMOp_SetNotifyJvmtiEventsMode; }
1664
bool allow_nested_vm_operations() const { return false; }
1665
VM_SetNotifyJvmtiEventsMode(bool enable) : _enable(enable) {
1670
correct_jvmti_thread_states();
1672
JvmtiVTMSTransitionDisabler::set_VTMS_notify_jvmti_events(_enable);
1676
// This function is to support agents loaded into running VM.
1677
// Must be called in thread-in-native mode.
1679
JvmtiEnvBase::enable_virtual_threads_notify_jvmti() {
1680
if (!Continuations::enabled()) {
1683
if (JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
1684
return false; // already enabled
1686
VM_SetNotifyJvmtiEventsMode op(true);
1687
VMThread::execute(&op);
1691
// This function is used in WhiteBox, only needed to test the function above.
1692
// It is unsafe to use this function when virtual threads are executed.
1693
// Must be called in thread-in-native mode.
1695
JvmtiEnvBase::disable_virtual_threads_notify_jvmti() {
1696
if (!Continuations::enabled()) {
1699
if (!JvmtiVTMSTransitionDisabler::VTMS_notify_jvmti_events()) {
1700
return false; // already disabled
1702
JvmtiVTMSTransitionDisabler disabler(true); // ensure there are no other disablers
1703
VM_SetNotifyJvmtiEventsMode op(false);
1704
VMThread::execute(&op);
1708
// java_thread - protected by ThreadsListHandle
1710
JvmtiEnvBase::suspend_thread(oop thread_oop, JavaThread* java_thread, bool single_suspend,
1711
int* need_safepoint_p) {
1712
JavaThread* current = JavaThread::current();
1713
HandleMark hm(current);
1714
Handle thread_h(current, thread_oop);
1715
bool is_virtual = java_lang_VirtualThread::is_instance(thread_h());
1718
if (single_suspend) {
1719
if (JvmtiVTSuspender::is_vthread_suspended(thread_h())) {
1720
return JVMTI_ERROR_THREAD_SUSPENDED;
1722
JvmtiVTSuspender::register_vthread_suspend(thread_h());
1723
// Check if virtual thread is mounted and there is a java_thread.
1724
// A non-null java_thread is always passed in the !single_suspend case.
1725
oop carrier_thread = java_lang_VirtualThread::carrier_thread(thread_h());
1726
java_thread = carrier_thread == nullptr ? nullptr : java_lang_Thread::thread(carrier_thread);
1728
// The java_thread can be still blocked in VTMS transition after a previous JVMTI resume call.
1729
// There is no need to suspend the java_thread in this case. After vthread unblocking,
1730
// it will check for ext_suspend request and suspend itself if necessary.
1731
if (java_thread == nullptr || java_thread->is_suspended()) {
1732
// We are done if the virtual thread is unmounted or
1733
// the java_thread is externally suspended.
1734
return JVMTI_ERROR_NONE;
1736
// The virtual thread is mounted: suspend the java_thread.
1738
// Don't allow hidden thread suspend request.
1739
if (java_thread->is_hidden_from_external_view()) {
1740
return JVMTI_ERROR_NONE;
1742
bool is_thread_carrying = is_thread_carrying_vthread(java_thread, thread_h());
1744
// A case of non-virtual thread.
1746
// Thread.suspend() is used in some tests. It sets jt->is_suspended() only.
1747
if (java_thread->is_carrier_thread_suspended() ||
1748
(!is_thread_carrying && java_thread->is_suspended())) {
1749
return JVMTI_ERROR_THREAD_SUSPENDED;
1751
java_thread->set_carrier_thread_suspended();
1753
assert(!java_thread->is_in_VTMS_transition(), "sanity check");
1755
assert(!single_suspend || (!is_virtual && java_thread->is_carrier_thread_suspended()) ||
1756
(is_virtual && JvmtiVTSuspender::is_vthread_suspended(thread_h())),
1759
// An attempt to handshake-suspend a thread carrying a virtual thread will result in
1760
// suspension of mounted virtual thread. So, we just mark it as suspended
1761
// and it will be actually suspended at virtual thread unmount transition.
1762
if (!is_thread_carrying) {
1763
assert(thread_h() != nullptr, "sanity check");
1764
assert(single_suspend || thread_h()->is_a(vmClasses::BaseVirtualThread_klass()),
1765
"SuspendAllVirtualThreads should never suspend non-virtual threads");
1766
// Case of mounted virtual or attached carrier thread.
1767
if (!JvmtiSuspendControl::suspend(java_thread)) {
1768
// Thread is already suspended or in process of exiting.
1769
if (java_thread->is_exiting()) {
1770
// The thread was in the process of exiting.
1771
return JVMTI_ERROR_THREAD_NOT_ALIVE;
1773
return JVMTI_ERROR_THREAD_SUSPENDED;
1776
return JVMTI_ERROR_NONE;
1779
// java_thread - protected by ThreadsListHandle
1781
JvmtiEnvBase::resume_thread(oop thread_oop, JavaThread* java_thread, bool single_resume) {
1782
JavaThread* current = JavaThread::current();
1783
HandleMark hm(current);
1784
Handle thread_h(current, thread_oop);
1785
bool is_virtual = java_lang_VirtualThread::is_instance(thread_h());
1788
if (single_resume) {
1789
if (!JvmtiVTSuspender::is_vthread_suspended(thread_h())) {
1790
return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1792
JvmtiVTSuspender::register_vthread_resume(thread_h());
1793
// Check if virtual thread is mounted and there is a java_thread.
1794
// A non-null java_thread is always passed in the !single_resume case.
1795
oop carrier_thread = java_lang_VirtualThread::carrier_thread(thread_h());
1796
java_thread = carrier_thread == nullptr ? nullptr : java_lang_Thread::thread(carrier_thread);
1798
// The java_thread can be still blocked in VTMS transition after a previous JVMTI suspend call.
1799
// There is no need to resume the java_thread in this case. After vthread unblocking,
1800
// it will check for is_vthread_suspended request and remain resumed if necessary.
1801
if (java_thread == nullptr || !java_thread->is_suspended()) {
1802
// We are done if the virtual thread is unmounted or
1803
// the java_thread is not externally suspended.
1804
return JVMTI_ERROR_NONE;
1806
// The virtual thread is mounted and java_thread is supended: resume the java_thread.
1808
// Don't allow hidden thread resume request.
1809
if (java_thread->is_hidden_from_external_view()) {
1810
return JVMTI_ERROR_NONE;
1812
bool is_thread_carrying = is_thread_carrying_vthread(java_thread, thread_h());
1814
// A case of a non-virtual thread.
1816
if (!java_thread->is_carrier_thread_suspended() &&
1817
(is_thread_carrying || !java_thread->is_suspended())) {
1818
return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1820
java_thread->clear_carrier_thread_suspended();
1822
assert(!java_thread->is_in_VTMS_transition(), "sanity check");
1824
if (!is_thread_carrying) {
1825
assert(thread_h() != nullptr, "sanity check");
1826
assert(single_resume || thread_h()->is_a(vmClasses::BaseVirtualThread_klass()),
1827
"ResumeAllVirtualThreads should never resume non-virtual threads");
1828
if (java_thread->is_suspended()) {
1829
if (!JvmtiSuspendControl::resume(java_thread)) {
1830
return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
1834
return JVMTI_ERROR_NONE;
1837
ResourceTracker::ResourceTracker(JvmtiEnv* env) {
1839
_allocations = new (mtServiceability) GrowableArray<unsigned char*>(20, mtServiceability);
1842
ResourceTracker::~ResourceTracker() {
1844
for (int i=0; i<_allocations->length(); i++) {
1845
_env->deallocate(_allocations->at(i));
1848
delete _allocations;
1851
jvmtiError ResourceTracker::allocate(jlong size, unsigned char** mem_ptr) {
1853
jvmtiError err = _env->allocate(size, &ptr);
1854
if (err == JVMTI_ERROR_NONE) {
1855
_allocations->append(ptr);
1864
unsigned char* ResourceTracker::allocate(jlong size) {
1866
allocate(size, &ptr);
1870
char* ResourceTracker::strdup(const char* str) {
1871
char *dup_str = (char*)allocate(strlen(str)+1);
1872
if (dup_str != nullptr) {
1873
strcpy(dup_str, str);
1878
struct StackInfoNode {
1879
struct StackInfoNode *next;
1880
jvmtiStackInfo info;
1883
// Create a jvmtiStackInfo inside a linked list node and create a
1884
// buffer for the frame information, both allocated as resource objects.
1885
// Fill in both the jvmtiStackInfo and the jvmtiFrameInfo.
1886
// Note that either or both of thr and thread_oop
1887
// may be null if the thread is new or has exited.
1889
MultipleStackTracesCollector::fill_frames(jthread jt, JavaThread *thr, oop thread_oop) {
1891
Thread *current_thread = Thread::current();
1892
assert(SafepointSynchronize::is_at_safepoint() ||
1894
thr->is_handshake_safe_for(current_thread),
1895
"unmounted virtual thread / call by myself / at safepoint / at handshake");
1899
struct StackInfoNode *node = NEW_RESOURCE_OBJ(struct StackInfoNode);
1900
jvmtiStackInfo *infop = &(node->info);
1902
node->next = head();
1904
infop->frame_count = 0;
1905
infop->frame_buffer = nullptr;
1908
if (java_lang_VirtualThread::is_instance(thread_oop)) {
1909
state = JvmtiEnvBase::get_vthread_state(thread_oop, thr);
1911
if ((state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1912
javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(thread_oop);
1913
infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
1914
_result = env()->get_stack_trace(jvf, 0, max_frame_count(),
1915
infop->frame_buffer, &(infop->frame_count));
1918
state = JvmtiEnvBase::get_thread_state(thread_oop, thr);
1919
if (thr != nullptr && (state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1920
infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
1921
_result = env()->get_stack_trace(thr, 0, max_frame_count(),
1922
infop->frame_buffer, &(infop->frame_count));
1925
_frame_count_total += infop->frame_count;
1926
infop->state = state;
1929
// Based on the stack information in the linked list, allocate memory
1930
// block to return and fill it from the info in the linked list.
1932
MultipleStackTracesCollector::allocate_and_fill_stacks(jint thread_count) {
1933
// do I need to worry about alignment issues?
1934
jlong alloc_size = thread_count * sizeof(jvmtiStackInfo)
1935
+ _frame_count_total * sizeof(jvmtiFrameInfo);
1936
env()->allocate(alloc_size, (unsigned char **)&_stack_info);
1938
// pointers to move through the newly allocated space as it is filled in
1939
jvmtiStackInfo *si = _stack_info + thread_count; // bottom of stack info
1940
jvmtiFrameInfo *fi = (jvmtiFrameInfo *)si; // is the top of frame info
1942
// copy information in resource area into allocated buffer
1943
// insert stack info backwards since linked list is backwards
1944
// insert frame info forwards
1945
// walk the StackInfoNodes
1946
for (struct StackInfoNode *sin = head(); sin != nullptr; sin = sin->next) {
1947
jint frame_count = sin->info.frame_count;
1948
size_t frames_size = frame_count * sizeof(jvmtiFrameInfo);
1950
memcpy(si, &(sin->info), sizeof(jvmtiStackInfo));
1951
if (frames_size == 0) {
1952
si->frame_buffer = nullptr;
1954
memcpy(fi, sin->info.frame_buffer, frames_size);
1955
si->frame_buffer = fi; // point to the new allocated copy of the frames
1959
assert(si == _stack_info, "the last copied stack info must be the first record");
1960
assert((unsigned char *)fi == ((unsigned char *)_stack_info) + alloc_size,
1961
"the last copied frame info must be the last record");
1964
// AdapterClosure is to make use of JvmtiUnitedHandshakeClosure objects from
1965
// Handshake::execute() which is unaware of the do_vthread() member functions.
1966
class AdapterClosure : public HandshakeClosure {
1967
JvmtiUnitedHandshakeClosure* _hs_cl;
1971
AdapterClosure(JvmtiUnitedHandshakeClosure* hs_cl, Handle target_h)
1972
: HandshakeClosure(hs_cl->name()), _hs_cl(hs_cl), _target_h(target_h) {}
1974
virtual void do_thread(Thread* target) {
1975
if (java_lang_VirtualThread::is_instance(_target_h())) {
1976
_hs_cl->do_vthread(_target_h); // virtual thread
1978
_hs_cl->do_thread(target); // platform thread
1983
// Supports platform and virtual threads.
1984
// JvmtiVTMSTransitionDisabler is always set by this function.
1986
JvmtiHandshake::execute(JvmtiUnitedHandshakeClosure* hs_cl, jthread target) {
1987
JavaThread* current = JavaThread::current();
1988
HandleMark hm(current);
1990
JvmtiVTMSTransitionDisabler disabler(target);
1991
ThreadsListHandle tlh(current);
1992
JavaThread* java_thread = nullptr;
1993
oop thread_obj = nullptr;
1995
jvmtiError err = JvmtiEnvBase::get_threadOop_and_JavaThread(tlh.list(), target, current, &java_thread, &thread_obj);
1996
if (err != JVMTI_ERROR_NONE) {
1997
hs_cl->set_result(err);
2000
Handle target_h(current, thread_obj);
2001
execute(hs_cl, &tlh, java_thread, target_h);
2004
// Supports platform and virtual threads.
2005
// A virtual thread is always identified by the target_h oop handle.
2006
// The target_jt is always nullptr for an unmounted virtual thread.
2007
// JvmtiVTMSTransitionDisabler has to be set before call to this function.
2009
JvmtiHandshake::execute(JvmtiUnitedHandshakeClosure* hs_cl, ThreadsListHandle* tlh,
2010
JavaThread* target_jt, Handle target_h) {
2011
JavaThread* current = JavaThread::current();
2012
bool is_virtual = java_lang_VirtualThread::is_instance(target_h());
2013
bool self = target_jt == current;
2015
assert(!Continuations::enabled() || self || !is_virtual || current->is_VTMS_transition_disabler(), "sanity check");
2017
hs_cl->set_target_jt(target_jt); // can be needed in the virtual thread case
2018
hs_cl->set_is_virtual(is_virtual); // can be needed in the virtual thread case
2019
hs_cl->set_self(self); // needed when suspend is required for non-current target thread
2021
if (is_virtual) { // virtual thread
2022
if (!JvmtiEnvBase::is_vthread_alive(target_h())) {
2025
if (target_jt == nullptr) { // unmounted virtual thread
2026
hs_cl->do_vthread(target_h); // execute handshake closure callback on current thread directly
2029
if (target_jt != nullptr) { // mounted virtual or platform thread
2030
AdapterClosure acl(hs_cl, target_h);
2031
if (self) { // target platform thread is current
2032
acl.do_thread(target_jt); // execute handshake closure callback on current thread directly
2034
Handshake::execute(&acl, tlh, target_jt); // delegate to Handshake implementation
2040
VM_GetThreadListStackTraces::doit() {
2041
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2044
ThreadsListHandle tlh;
2045
for (int i = 0; i < _thread_count; ++i) {
2046
jthread jt = _thread_list[i];
2047
JavaThread* java_thread = nullptr;
2048
oop thread_oop = nullptr;
2049
jvmtiError err = JvmtiEnvBase::get_threadOop_and_JavaThread(tlh.list(), jt, nullptr, &java_thread, &thread_oop);
2051
if (err != JVMTI_ERROR_NONE) {
2052
// We got an error code so we don't have a JavaThread *, but
2053
// only return an error from here if we didn't get a valid
2055
// In the virtual thread case the get_threadOop_and_JavaThread is expected to correctly set
2056
// the thread_oop and return JVMTI_ERROR_THREAD_NOT_ALIVE which we ignore here.
2057
// The corresponding thread state will be recorded in the jvmtiStackInfo.state.
2058
if (thread_oop == nullptr) {
2059
_collector.set_result(err);
2062
// We have a valid thread_oop.
2064
_collector.fill_frames(jt, java_thread, thread_oop);
2066
_collector.allocate_and_fill_stacks(_thread_count);
2070
GetSingleStackTraceClosure::doit() {
2071
JavaThread *jt = _target_jt;
2072
oop thread_oop = JNIHandles::resolve_external_guard(_jthread);
2074
if ((jt == nullptr || !jt->is_exiting()) && thread_oop != nullptr) {
2076
_collector.fill_frames(_jthread, jt, thread_oop);
2077
_collector.allocate_and_fill_stacks(1);
2078
set_result(_collector.result());
2083
GetSingleStackTraceClosure::do_thread(Thread *target) {
2084
assert(_target_jt == JavaThread::cast(target), "sanity check");
2089
GetSingleStackTraceClosure::do_vthread(Handle target_h) {
2090
// Use jvmti_vthread() instead of vthread() as target could have temporarily changed
2091
// identity to carrier thread (see VirtualThread.switchToCarrierThread).
2092
assert(_target_jt == nullptr || _target_jt->jvmti_vthread() == target_h(), "sanity check");
2097
VM_GetAllStackTraces::doit() {
2098
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2101
_final_thread_count = 0;
2102
for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2103
oop thread_oop = jt->threadObj();
2104
if (thread_oop != nullptr &&
2105
!jt->is_exiting() &&
2106
java_lang_Thread::is_alive(thread_oop) &&
2107
!jt->is_hidden_from_external_view() &&
2108
!thread_oop->is_a(vmClasses::BoundVirtualThread_klass())) {
2109
++_final_thread_count;
2110
// Handle block of the calling thread is used to create local refs.
2111
_collector.fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
2115
_collector.allocate_and_fill_stacks(_final_thread_count);
2118
// Verifies that the top frame is a java frame in an expected state.
2119
// Deoptimizes frame if needed.
2120
// Checks that the frame method signature matches the return type (tos).
2121
// HandleMark must be defined in the caller only.
2122
// It is to keep a ret_ob_h handle alive after return to the caller.
2124
JvmtiEnvBase::check_top_frame(Thread* current_thread, JavaThread* java_thread,
2125
jvalue value, TosState tos, Handle* ret_ob_h) {
2126
ResourceMark rm(current_thread);
2128
javaVFrame* jvf = jvf_for_thread_and_depth(java_thread, 0);
2129
NULL_CHECK(jvf, JVMTI_ERROR_NO_MORE_FRAMES);
2131
if (jvf->method()->is_native()) {
2132
return JVMTI_ERROR_OPAQUE_FRAME;
2135
// If the frame is a compiled one, need to deoptimize it.
2136
if (jvf->is_compiled_frame()) {
2137
if (!jvf->fr().can_be_deoptimized()) {
2138
return JVMTI_ERROR_OPAQUE_FRAME;
2140
Deoptimization::deoptimize_frame(java_thread, jvf->fr().id());
2143
// Get information about method return type
2144
Symbol* signature = jvf->method()->signature();
2146
ResultTypeFinder rtf(signature);
2147
TosState fr_tos = as_TosState(rtf.type());
2148
if (fr_tos != tos) {
2149
if (tos != itos || (fr_tos != btos && fr_tos != ztos && fr_tos != ctos && fr_tos != stos)) {
2150
return JVMTI_ERROR_TYPE_MISMATCH;
2154
// Check that the jobject class matches the return type signature.
2155
jobject jobj = value.l;
2156
if (tos == atos && jobj != nullptr) { // null reference is allowed
2157
Handle ob_h(current_thread, JNIHandles::resolve_external_guard(jobj));
2158
NULL_CHECK(ob_h, JVMTI_ERROR_INVALID_OBJECT);
2159
Klass* ob_k = ob_h()->klass();
2160
NULL_CHECK(ob_k, JVMTI_ERROR_INVALID_OBJECT);
2162
// Method return type signature.
2163
char* ty_sign = 1 + strchr(signature->as_C_string(), JVM_SIGNATURE_ENDFUNC);
2165
if (!VM_GetOrSetLocal::is_assignable(ty_sign, ob_k, current_thread)) {
2166
return JVMTI_ERROR_TYPE_MISMATCH;
2170
return JVMTI_ERROR_NONE;
2171
} /* end check_top_frame */
2174
// ForceEarlyReturn<type> follows the PopFrame approach in many aspects.
2175
// Main difference is on the last stage in the interpreter.
2176
// The PopFrame stops method execution to continue execution
2177
// from the same method call instruction.
2178
// The ForceEarlyReturn forces return from method so the execution
2179
// continues at the bytecode following the method call.
2181
// thread - NOT protected by ThreadsListHandle and NOT pre-checked
2184
JvmtiEnvBase::force_early_return(jthread thread, jvalue value, TosState tos) {
2185
JavaThread* current_thread = JavaThread::current();
2186
HandleMark hm(current_thread);
2188
JvmtiVTMSTransitionDisabler disabler(thread);
2189
ThreadsListHandle tlh(current_thread);
2191
JavaThread* java_thread = nullptr;
2192
oop thread_obj = nullptr;
2193
jvmtiError err = get_threadOop_and_JavaThread(tlh.list(), thread, current_thread, &java_thread, &thread_obj);
2195
if (err != JVMTI_ERROR_NONE) {
2198
Handle thread_handle(current_thread, thread_obj);
2199
bool self = java_thread == current_thread;
2201
err = check_non_suspended_or_opaque_frame(java_thread, thread_obj, self);
2202
if (err != JVMTI_ERROR_NONE) {
2206
// retrieve or create the state
2207
JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
2208
if (state == nullptr) {
2209
return JVMTI_ERROR_THREAD_NOT_ALIVE;
2212
// Eagerly reallocate scalar replaced objects.
2213
EscapeBarrier eb(true, current_thread, java_thread);
2214
if (!eb.deoptimize_objects(0)) {
2215
// Reallocation of scalar replaced objects failed -> return with error
2216
return JVMTI_ERROR_OUT_OF_MEMORY;
2219
MutexLocker mu(JvmtiThreadState_lock);
2220
SetForceEarlyReturn op(state, value, tos);
2221
JvmtiHandshake::execute(&op, &tlh, java_thread, thread_handle);
2226
SetForceEarlyReturn::doit(Thread *target) {
2227
JavaThread* java_thread = JavaThread::cast(target);
2228
Thread* current_thread = Thread::current();
2229
HandleMark hm(current_thread);
2231
if (java_thread->is_exiting()) {
2232
return; /* JVMTI_ERROR_THREAD_NOT_ALIVE (default) */
2235
// Check to see if a ForceEarlyReturn was already in progress
2236
if (_state->is_earlyret_pending()) {
2237
// Probably possible for JVMTI clients to trigger this, but the
2238
// JPDA backend shouldn't allow this to happen
2239
_result = JVMTI_ERROR_INTERNAL;
2243
// The same as for PopFrame. Workaround bug:
2244
// 4812902: popFrame hangs if the method is waiting at a synchronize
2245
// Catch this condition and return an error to avoid hanging.
2246
// Now JVMTI spec allows an implementation to bail out with an opaque
2248
OSThread* osThread = java_thread->osthread();
2249
if (osThread->get_state() == MONITOR_WAIT) {
2250
_result = JVMTI_ERROR_OPAQUE_FRAME;
2256
_result = JvmtiEnvBase::check_top_frame(current_thread, java_thread, _value, _tos, &ret_ob_h);
2257
if (_result != JVMTI_ERROR_NONE) {
2260
assert(_tos != atos || _value.l == nullptr || ret_ob_h() != nullptr,
2261
"return object oop must not be null if jobject is not null");
2263
// Update the thread state to reflect that the top frame must be
2264
// forced to return.
2265
// The current frame will be returned later when the suspended
2266
// thread is resumed and right before returning from VM to Java.
2267
// (see call_VM_base() in assembler_<cpu>.cpp).
2269
_state->set_earlyret_pending();
2270
_state->set_earlyret_oop(ret_ob_h());
2271
_state->set_earlyret_value(_value, _tos);
2273
// Set pending step flag for this early return.
2274
// It is cleared when next step event is posted.
2275
_state->set_pending_step_for_earlyret();
2279
JvmtiMonitorClosure::do_monitor(ObjectMonitor* mon) {
2280
if ( _error != JVMTI_ERROR_NONE) {
2281
// Error occurred in previous iteration so no need to add
2285
// Filter out on stack monitors collected during stack walk.
2286
oop obj = mon->object();
2288
if (obj == nullptr) {
2289
// This can happen if JNI code drops all references to the
2295
for (int j = 0; j < _owned_monitors_list->length(); j++) {
2296
jobject jobj = ((jvmtiMonitorStackDepthInfo*)_owned_monitors_list->at(j))->monitor;
2297
oop check = JNIHandles::resolve(jobj);
2299
// On stack monitor already collected during the stack walk.
2304
if (found == false) {
2305
// This is off stack monitor (e.g. acquired via jni MonitorEnter).
2307
jvmtiMonitorStackDepthInfo *jmsdi;
2308
err = _env->allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
2309
if (err != JVMTI_ERROR_NONE) {
2313
Handle hobj(Thread::current(), obj);
2314
jmsdi->monitor = _env->jni_reference(_calling_thread, hobj);
2315
// stack depth is unknown for this monitor.
2316
jmsdi->stack_depth = -1;
2317
_owned_monitors_list->append(jmsdi);
2321
GrowableArray<OopHandle>* JvmtiModuleClosure::_tbl = nullptr;
2323
void JvmtiModuleClosure::do_module(ModuleEntry* entry) {
2324
assert_locked_or_safepoint(Module_lock);
2325
OopHandle module = entry->module_handle();
2326
guarantee(module.resolve() != nullptr, "module object is null");
2331
JvmtiModuleClosure::get_all_modules(JvmtiEnv* env, jint* module_count_ptr, jobject** modules_ptr) {
2333
MutexLocker mcld(ClassLoaderDataGraph_lock);
2334
MutexLocker ml(Module_lock);
2336
_tbl = new GrowableArray<OopHandle>(77);
2337
if (_tbl == nullptr) {
2338
return JVMTI_ERROR_OUT_OF_MEMORY;
2341
// Iterate over all the modules loaded to the system.
2342
ClassLoaderDataGraph::modules_do_keepalive(&do_module);
2344
jint len = _tbl->length();
2345
guarantee(len > 0, "at least one module must be present");
2347
jobject* array = (jobject*)env->jvmtiMalloc((jlong)(len * sizeof(jobject)));
2348
if (array == nullptr) {
2349
return JVMTI_ERROR_OUT_OF_MEMORY;
2351
for (jint idx = 0; idx < len; idx++) {
2352
array[idx] = JNIHandles::make_local(_tbl->at(idx).resolve());
2355
*modules_ptr = array;
2356
*module_count_ptr = len;
2357
return JVMTI_ERROR_NONE;
2361
UpdateForPopTopFrameClosure::doit(Thread *target) {
2362
Thread* current_thread = Thread::current();
2363
HandleMark hm(current_thread);
2364
JavaThread* java_thread = JavaThread::cast(target);
2366
if (java_thread->is_exiting()) {
2367
return; /* JVMTI_ERROR_THREAD_NOT_ALIVE (default) */
2369
assert(java_thread == _state->get_thread(), "Must be");
2371
// Check to see if a PopFrame was already in progress
2372
if (java_thread->popframe_condition() != JavaThread::popframe_inactive) {
2373
// Probably possible for JVMTI clients to trigger this, but the
2374
// JPDA backend shouldn't allow this to happen
2375
_result = JVMTI_ERROR_INTERNAL;
2379
// Was workaround bug
2380
// 4812902: popFrame hangs if the method is waiting at a synchronize
2381
// Catch this condition and return an error to avoid hanging.
2382
// Now JVMTI spec allows an implementation to bail out with an opaque frame error.
2383
OSThread* osThread = java_thread->osthread();
2384
if (osThread->get_state() == MONITOR_WAIT) {
2385
_result = JVMTI_ERROR_OPAQUE_FRAME;
2389
ResourceMark rm(current_thread);
2390
// Check if there is more than one Java frame in this thread, that the top two frames
2391
// are Java (not native) frames, and that there is no intervening VM frame
2392
int frame_count = 0;
2393
bool is_interpreted[2];
2394
intptr_t *frame_sp[2];
2395
// The 2-nd arg of constructor is needed to stop iterating at java entry frame.
2396
for (vframeStream vfs(java_thread, true, false /* process_frames */); !vfs.at_end(); vfs.next()) {
2397
methodHandle mh(current_thread, vfs.method());
2398
if (mh->is_native()) {
2399
_result = JVMTI_ERROR_OPAQUE_FRAME;
2402
is_interpreted[frame_count] = vfs.is_interpreted_frame();
2403
frame_sp[frame_count] = vfs.frame_id();
2404
if (++frame_count > 1) break;
2406
if (frame_count < 2) {
2407
// We haven't found two adjacent non-native Java frames on the top.
2408
// There can be two situations here:
2409
// 1. There are no more java frames
2410
// 2. Two top java frames are separated by non-java native frames
2411
if (JvmtiEnvBase::jvf_for_thread_and_depth(java_thread, 1) == nullptr) {
2412
_result = JVMTI_ERROR_NO_MORE_FRAMES;
2415
// Intervening non-java native or VM frames separate java frames.
2416
// Current implementation does not support this. See bug #5031735.
2417
// In theory it is possible to pop frames in such cases.
2418
_result = JVMTI_ERROR_OPAQUE_FRAME;
2423
// If any of the top 2 frames is a compiled one, need to deoptimize it
2424
for (int i = 0; i < 2; i++) {
2425
if (!is_interpreted[i]) {
2426
Deoptimization::deoptimize_frame(java_thread, frame_sp[i]);
2430
// Update the thread state to reflect that the top frame is popped
2431
// so that cur_stack_depth is maintained properly and all frameIDs
2433
// The current frame will be popped later when the suspended thread
2434
// is resumed and right before returning from VM to Java.
2435
// (see call_VM_base() in assembler_<cpu>.cpp).
2437
// It's fine to update the thread state here because no JVMTI events
2438
// shall be posted for this PopFrame.
2440
_state->update_for_pop_top_frame();
2441
java_thread->set_popframe_condition(JavaThread::popframe_pending_bit);
2442
// Set pending step flag for this popframe and it is cleared when next
2443
// step event is posted.
2444
_state->set_pending_step_for_popframe();
2445
_result = JVMTI_ERROR_NONE;
2449
SetFramePopClosure::do_thread(Thread *target) {
2450
Thread* current = Thread::current();
2451
ResourceMark rm(current); // vframes are resource allocated
2452
JavaThread* java_thread = JavaThread::cast(target);
2454
if (java_thread->is_exiting()) {
2455
return; // JVMTI_ERROR_THREAD_NOT_ALIVE (default)
2458
if (!_self && !java_thread->is_suspended()) {
2459
_result = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
2462
if (!java_thread->has_last_Java_frame()) {
2463
_result = JVMTI_ERROR_NO_MORE_FRAMES;
2466
assert(_state->get_thread_or_saved() == java_thread, "Must be");
2468
RegisterMap reg_map(java_thread,
2469
RegisterMap::UpdateMap::include,
2470
RegisterMap::ProcessFrames::skip,
2471
RegisterMap::WalkContinuation::include);
2472
javaVFrame* jvf = JvmtiEnvBase::get_cthread_last_java_vframe(java_thread, ®_map);
2473
_result = ((JvmtiEnvBase*)_env)->set_frame_pop(_state, jvf, _depth);
2477
SetFramePopClosure::do_vthread(Handle target_h) {
2478
Thread* current = Thread::current();
2479
ResourceMark rm(current); // vframes are resource allocated
2481
if (!_self && !JvmtiVTSuspender::is_vthread_suspended(target_h())) {
2482
_result = JVMTI_ERROR_THREAD_NOT_SUSPENDED;
2485
javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h());
2486
_result = ((JvmtiEnvBase*)_env)->set_frame_pop(_state, jvf, _depth);
2490
GetOwnedMonitorInfoClosure::do_thread(Thread *target) {
2491
JavaThread *jt = JavaThread::cast(target);
2492
if (!jt->is_exiting() && (jt->threadObj() != nullptr)) {
2493
_result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread,
2495
_owned_monitors_list);
2500
GetOwnedMonitorInfoClosure::do_vthread(Handle target_h) {
2501
assert(_target_jt != nullptr, "sanity check");
2502
Thread* current = Thread::current();
2503
ResourceMark rm(current); // vframes are resource allocated
2504
HandleMark hm(current);
2506
javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h());
2508
if (!_target_jt->is_exiting() && _target_jt->threadObj() != nullptr) {
2509
_result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread,
2512
_owned_monitors_list);
2517
GetCurrentContendedMonitorClosure::do_thread(Thread *target) {
2518
JavaThread *jt = JavaThread::cast(target);
2519
if (!jt->is_exiting() && (jt->threadObj() != nullptr)) {
2520
_result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,
2528
GetCurrentContendedMonitorClosure::do_vthread(Handle target_h) {
2529
if (_target_jt == nullptr) {
2530
_result = JVMTI_ERROR_NONE; // target virtual thread is unmounted
2533
// mounted virtual thread case
2534
do_thread(_target_jt);
2538
GetStackTraceClosure::do_thread(Thread *target) {
2539
Thread* current = Thread::current();
2540
ResourceMark rm(current);
2542
JavaThread *jt = JavaThread::cast(target);
2543
if (!jt->is_exiting() && jt->threadObj() != nullptr) {
2544
_result = ((JvmtiEnvBase *)_env)->get_stack_trace(jt,
2545
_start_depth, _max_count,
2546
_frame_buffer, _count_ptr);
2551
GetStackTraceClosure::do_vthread(Handle target_h) {
2552
Thread* current = Thread::current();
2553
ResourceMark rm(current);
2555
javaVFrame *jvf = JvmtiEnvBase::get_vthread_jvf(target_h());
2556
_result = ((JvmtiEnvBase *)_env)->get_stack_trace(jvf,
2557
_start_depth, _max_count,
2558
_frame_buffer, _count_ptr);
2563
PrintStackTraceClosure::do_thread_impl(Thread *target) {
2564
JavaThread *java_thread = JavaThread::cast(target);
2565
Thread *current_thread = Thread::current();
2567
ResourceMark rm (current_thread);
2568
const char* tname = JvmtiTrace::safe_get_thread_name(java_thread);
2569
oop t_oop = java_thread->jvmti_vthread();
2570
t_oop = t_oop == nullptr ? java_thread->threadObj() : t_oop;
2571
bool is_vt_suspended = java_lang_VirtualThread::is_instance(t_oop) && JvmtiVTSuspender::is_vthread_suspended(t_oop);
2573
log_error(jvmti)("%s(%s) exiting: %d is_susp: %d is_thread_susp: %d is_vthread_susp: %d "
2574
"is_VTMS_transition_disabler: %d, is_in_VTMS_transition = %d\n",
2575
tname, java_thread->name(), java_thread->is_exiting(),
2576
java_thread->is_suspended(), java_thread->is_carrier_thread_suspended(), is_vt_suspended,
2577
java_thread->is_VTMS_transition_disabler(), java_thread->is_in_VTMS_transition());
2579
if (java_thread->has_last_Java_frame()) {
2580
RegisterMap reg_map(java_thread,
2581
RegisterMap::UpdateMap::include,
2582
RegisterMap::ProcessFrames::include,
2583
RegisterMap::WalkContinuation::skip);
2584
ResourceMark rm(current_thread);
2585
HandleMark hm(current_thread);
2586
javaVFrame *jvf = java_thread->last_java_vframe(®_map);
2587
while (jvf != nullptr) {
2588
log_error(jvmti)(" %s:%d",
2589
jvf->method()->external_name(),
2590
jvf->method()->line_number_from_bci(jvf->bci()));
2591
jvf = jvf->java_sender();
2594
log_error(jvmti)("\n");
2598
PrintStackTraceClosure::do_thread(Thread *target) {
2599
JavaThread *java_thread = JavaThread::cast(target);
2600
Thread *current_thread = Thread::current();
2602
assert(SafepointSynchronize::is_at_safepoint() ||
2603
java_thread->is_handshake_safe_for(current_thread),
2604
"call by myself / at safepoint / at handshake");
2606
PrintStackTraceClosure::do_thread_impl(target);
2611
GetFrameCountClosure::do_thread(Thread *target) {
2612
JavaThread* jt = JavaThread::cast(target);
2613
assert(target == jt, "just checking");
2615
if (!jt->is_exiting() && jt->threadObj() != nullptr) {
2616
_result = ((JvmtiEnvBase*)_env)->get_frame_count(jt, _count_ptr);
2621
GetFrameCountClosure::do_vthread(Handle target_h) {
2622
_result = ((JvmtiEnvBase*)_env)->get_frame_count(target_h(), _count_ptr);
2626
GetFrameLocationClosure::do_thread(Thread *target) {
2627
JavaThread *jt = JavaThread::cast(target);
2628
assert(target == jt, "just checking");
2630
if (!jt->is_exiting() && jt->threadObj() != nullptr) {
2631
_result = ((JvmtiEnvBase*)_env)->get_frame_location(jt, _depth,
2632
_method_ptr, _location_ptr);
2637
GetFrameLocationClosure::do_vthread(Handle target_h) {
2638
_result = ((JvmtiEnvBase*)_env)->get_frame_location(target_h(), _depth,
2639
_method_ptr, _location_ptr);