Skip to content

Commit 657df03

Browse files
author
duke
committedSep 12, 2023
Automatic merge of jdk:master into master
2 parents 36039d3 + fda142f commit 657df03

File tree

9 files changed

+404
-48
lines changed

9 files changed

+404
-48
lines changed
 

‎src/hotspot/share/prims/jvmtiEventController.cpp

+23-8
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ class JvmtiEventControllerPrivate : public AllStatic {
309309
static void clear_to_frame_pop(JvmtiEnvThreadState *env_thread, JvmtiFramePop fpop);
310310
static void change_field_watch(jvmtiEvent event_type, bool added);
311311

312+
static bool is_any_thread_filtered_event_enabled_globally();
313+
static void recompute_thread_filtered(JvmtiThreadState *state);
312314
static void thread_started(JavaThread *thread);
313315
static void thread_ended(JavaThread *thread);
314316

@@ -730,6 +732,20 @@ JvmtiEventControllerPrivate::recompute_enabled() {
730732
EC_TRACE(("[-] # recompute enabled - after " JULONG_FORMAT_X, any_env_thread_enabled));
731733
}
732734

735+
bool
736+
JvmtiEventControllerPrivate::is_any_thread_filtered_event_enabled_globally() {
737+
julong global_thread_events = JvmtiEventController::_universal_global_event_enabled.get_bits() & THREAD_FILTERED_EVENT_BITS;
738+
return global_thread_events != 0L;
739+
}
740+
741+
void
742+
JvmtiEventControllerPrivate::recompute_thread_filtered(JvmtiThreadState *state) {
743+
assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
744+
745+
if (is_any_thread_filtered_event_enabled_globally()) {
746+
JvmtiEventControllerPrivate::recompute_thread_enabled(state);
747+
}
748+
}
733749

734750
void
735751
JvmtiEventControllerPrivate::thread_started(JavaThread *thread) {
@@ -739,17 +755,11 @@ JvmtiEventControllerPrivate::thread_started(JavaThread *thread) {
739755
EC_TRACE(("[%s] # thread started", JvmtiTrace::safe_get_thread_name(thread)));
740756

741757
// if we have any thread filtered events globally enabled, create/update the thread state
742-
if ((JvmtiEventController::_universal_global_event_enabled.get_bits() & THREAD_FILTERED_EVENT_BITS) != 0) {
743-
MutexLocker mu(JvmtiThreadState_lock);
744-
// create the thread state if missing
745-
JvmtiThreadState *state = JvmtiThreadState::state_for_while_locked(thread);
746-
if (state != nullptr) { // skip threads with no JVMTI thread state
747-
recompute_thread_enabled(state);
748-
}
758+
if (is_any_thread_filtered_event_enabled_globally()) { // intentionally racy
759+
JvmtiThreadState::state_for(thread);
749760
}
750761
}
751762

752-
753763
void
754764
JvmtiEventControllerPrivate::thread_ended(JavaThread *thread) {
755765
// Removes the JvmtiThreadState associated with the specified thread.
@@ -1115,6 +1125,11 @@ JvmtiEventController::change_field_watch(jvmtiEvent event_type, bool added) {
11151125
JvmtiEventControllerPrivate::change_field_watch(event_type, added);
11161126
}
11171127

1128+
void
1129+
JvmtiEventController::recompute_thread_filtered(JvmtiThreadState *state) {
1130+
JvmtiEventControllerPrivate::recompute_thread_filtered(state);
1131+
}
1132+
11181133
void
11191134
JvmtiEventController::thread_started(JavaThread *thread) {
11201135
// operates only on the current thread

‎src/hotspot/share/prims/jvmtiEventController.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ class JvmtiEventController : AllStatic {
234234

235235
static void change_field_watch(jvmtiEvent event_type, bool added);
236236

237+
static void recompute_thread_filtered(JvmtiThreadState *state);
237238
static void thread_started(JavaThread *thread);
238239
static void thread_ended(JavaThread *thread);
239240

‎src/hotspot/share/prims/jvmtiExport.cpp

+50-40
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,15 @@ JvmtiExport::get_jvmti_interface(JavaVM *jvm, void **penv, jint version) {
417417
}
418418
}
419419

420+
JvmtiThreadState*
421+
JvmtiExport::get_jvmti_thread_state(JavaThread *thread) {
422+
assert(thread == JavaThread::current(), "must be current thread");
423+
if (thread->is_vthread_mounted() && thread->jvmti_thread_state() == nullptr) {
424+
JvmtiEventController::thread_started(thread);
425+
}
426+
return thread->jvmti_thread_state();
427+
}
428+
420429
void
421430
JvmtiExport::add_default_read_edges(Handle h_module, TRAPS) {
422431
if (!Universe::is_module_initialized()) {
@@ -920,7 +929,7 @@ class JvmtiClassFileLoadHookPoster : public StackObj {
920929
_has_been_modified = false;
921930

922931
assert(!_thread->is_in_any_VTMS_transition(), "CFLH events are not allowed in any VTMS transition");
923-
_state = _thread->jvmti_thread_state();
932+
_state = JvmtiExport::get_jvmti_thread_state(_thread);
924933
if (_state != nullptr) {
925934
_class_being_redefined = _state->get_class_being_redefined();
926935
_load_kind = _state->get_class_load_kind();
@@ -1212,7 +1221,7 @@ void JvmtiExport::post_raw_breakpoint(JavaThread *thread, Method* method, addres
12121221
HandleMark hm(thread);
12131222
methodHandle mh(thread, method);
12141223

1215-
JvmtiThreadState *state = thread->jvmti_thread_state();
1224+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
12161225
if (state == nullptr) {
12171226
return;
12181227
}
@@ -1310,7 +1319,7 @@ void JvmtiExport::at_single_stepping_point(JavaThread *thread, Method* method, a
13101319
methodHandle mh(thread, method);
13111320

13121321
// update information about current location and post a step event
1313-
JvmtiThreadState *state = thread->jvmti_thread_state();
1322+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
13141323
if (state == nullptr) {
13151324
return;
13161325
}
@@ -1329,15 +1338,15 @@ void JvmtiExport::at_single_stepping_point(JavaThread *thread, Method* method, a
13291338

13301339

13311340
void JvmtiExport::expose_single_stepping(JavaThread *thread) {
1332-
JvmtiThreadState *state = thread->jvmti_thread_state();
1341+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
13331342
if (state != nullptr) {
13341343
state->clear_hide_single_stepping();
13351344
}
13361345
}
13371346

13381347

13391348
bool JvmtiExport::hide_single_stepping(JavaThread *thread) {
1340-
JvmtiThreadState *state = thread->jvmti_thread_state();
1349+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
13411350
if (state != nullptr && state->is_enabled(JVMTI_EVENT_SINGLE_STEP)) {
13421351
state->set_hide_single_stepping();
13431352
return true;
@@ -1352,7 +1361,7 @@ void JvmtiExport::post_class_load(JavaThread *thread, Klass* klass) {
13521361
}
13531362
HandleMark hm(thread);
13541363

1355-
JvmtiThreadState* state = thread->jvmti_thread_state();
1364+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
13561365
if (state == nullptr) {
13571366
return;
13581367
}
@@ -1390,7 +1399,7 @@ void JvmtiExport::post_class_prepare(JavaThread *thread, Klass* klass) {
13901399
}
13911400
HandleMark hm(thread);
13921401

1393-
JvmtiThreadState* state = thread->jvmti_thread_state();
1402+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
13941403
if (state == nullptr) {
13951404
return;
13961405
}
@@ -1519,7 +1528,7 @@ void JvmtiExport::post_thread_end(JavaThread *thread) {
15191528
EVT_TRIG_TRACE(JVMTI_EVENT_THREAD_END, ("[%s] Trg Thread End event triggered",
15201529
JvmtiTrace::safe_get_thread_name(thread)));
15211530

1522-
JvmtiThreadState *state = thread->jvmti_thread_state();
1531+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
15231532
if (state == nullptr) {
15241533
return;
15251534
}
@@ -1567,7 +1576,7 @@ void JvmtiExport::post_vthread_start(jobject vthread) {
15671576
EVT_TRIG_TRACE(JVMTI_EVENT_VIRTUAL_THREAD_START, ("[%p] Trg Virtual Thread Start event triggered", vthread));
15681577

15691578
JavaThread *cur_thread = JavaThread::current();
1570-
JvmtiThreadState *state = cur_thread->jvmti_thread_state();
1579+
JvmtiThreadState *state = get_jvmti_thread_state(cur_thread);
15711580
if (state == nullptr) {
15721581
return;
15731582
}
@@ -1601,7 +1610,7 @@ void JvmtiExport::post_vthread_end(jobject vthread) {
16011610
EVT_TRIG_TRACE(JVMTI_EVENT_VIRTUAL_THREAD_END, ("[%p] Trg Virtual Thread End event triggered", vthread));
16021611

16031612
JavaThread *cur_thread = JavaThread::current();
1604-
JvmtiThreadState *state = cur_thread->jvmti_thread_state();
1613+
JvmtiThreadState *state = get_jvmti_thread_state(cur_thread);
16051614
if (state == nullptr) {
16061615
return;
16071616
}
@@ -1636,7 +1645,7 @@ void JvmtiExport::post_vthread_mount(jobject vthread) {
16361645
HandleMark hm(thread);
16371646
EVT_TRIG_TRACE(EXT_EVENT_VIRTUAL_THREAD_MOUNT, ("[%p] Trg Virtual Thread Mount event triggered", vthread));
16381647

1639-
JvmtiThreadState *state = thread->jvmti_thread_state();
1648+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
16401649
if (state == nullptr) {
16411650
return;
16421651
}
@@ -1671,7 +1680,7 @@ void JvmtiExport::post_vthread_unmount(jobject vthread) {
16711680
HandleMark hm(thread);
16721681
EVT_TRIG_TRACE(EXT_EVENT_VIRTUAL_THREAD_UNMOUNT, ("[%p] Trg Virtual Thread Unmount event triggered", vthread));
16731682

1674-
JvmtiThreadState *state = thread->jvmti_thread_state();
1683+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
16751684
if (state == nullptr) {
16761685
return;
16771686
}
@@ -1704,7 +1713,7 @@ void JvmtiExport::continuation_yield_cleanup(JavaThread* thread, jint continuati
17041713
}
17051714

17061715
assert(thread == JavaThread::current(), "must be");
1707-
JvmtiThreadState *state = thread->jvmti_thread_state();
1716+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
17081717
if (state == nullptr) {
17091718
return;
17101719
}
@@ -1798,7 +1807,7 @@ void JvmtiExport::post_method_entry(JavaThread *thread, Method* method, frame cu
17981807
HandleMark hm(thread);
17991808
methodHandle mh(thread, method);
18001809

1801-
JvmtiThreadState* state = thread->jvmti_thread_state();
1810+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
18021811
if (state == nullptr || !state->is_interp_only_mode()) {
18031812
// for any thread that actually wants method entry, interp_only_mode is set
18041813
return;
@@ -1838,7 +1847,7 @@ void JvmtiExport::post_method_exit(JavaThread* thread, Method* method, frame cur
18381847
HandleMark hm(thread);
18391848
methodHandle mh(thread, method);
18401849

1841-
JvmtiThreadState *state = thread->jvmti_thread_state();
1850+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
18421851

18431852
if (state == nullptr || !state->is_interp_only_mode()) {
18441853
// for any thread that actually wants method exit, interp_only_mode is set
@@ -1959,7 +1968,7 @@ void JvmtiExport::post_single_step(JavaThread *thread, Method* method, address l
19591968
HandleMark hm(thread);
19601969
methodHandle mh(thread, method);
19611970

1962-
JvmtiThreadState *state = thread->jvmti_thread_state();
1971+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
19631972
if (state == nullptr) {
19641973
return;
19651974
}
@@ -2001,7 +2010,7 @@ void JvmtiExport::post_exception_throw(JavaThread *thread, Method* method, addre
20012010
// ensure the stack is sufficiently processed.
20022011
KeepStackGCProcessedMark ksgcpm(thread);
20032012

2004-
JvmtiThreadState *state = thread->jvmti_thread_state();
2013+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
20052014
if (state == nullptr) {
20062015
return;
20072016
}
@@ -2089,7 +2098,7 @@ void JvmtiExport::notice_unwind_due_to_exception(JavaThread *thread, Method* met
20892098
methodHandle mh(thread, method);
20902099
Handle exception_handle(thread, exception);
20912100

2092-
JvmtiThreadState *state = thread->jvmti_thread_state();
2101+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
20932102
if (state == nullptr) {
20942103
return;
20952104
}
@@ -2205,7 +2214,7 @@ void JvmtiExport::post_field_access(JavaThread *thread, Method* method,
22052214
HandleMark hm(thread);
22062215
methodHandle mh(thread, method);
22072216

2208-
JvmtiThreadState *state = thread->jvmti_thread_state();
2217+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
22092218
if (state == nullptr) {
22102219
return;
22112220
}
@@ -2361,7 +2370,7 @@ void JvmtiExport::post_field_modification(JavaThread *thread, Method* method,
23612370
HandleMark hm(thread);
23622371
methodHandle mh(thread, method);
23632372

2364-
JvmtiThreadState *state = thread->jvmti_thread_state();
2373+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
23652374
if (state == nullptr) {
23662375
return;
23672376
}
@@ -2603,7 +2612,7 @@ void JvmtiExport::post_dynamic_code_generated_while_holding_locks(const char* na
26032612
// jvmti thread state.
26042613
// The collector and/or state might be null if JvmtiDynamicCodeEventCollector
26052614
// has been initialized while JVMTI_EVENT_DYNAMIC_CODE_GENERATED was disabled.
2606-
JvmtiThreadState* state = thread->jvmti_thread_state();
2615+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
26072616
if (state != nullptr) {
26082617
JvmtiDynamicCodeEventCollector *collector = state->get_dynamic_code_event_collector();
26092618
if (collector != nullptr) {
@@ -2722,17 +2731,17 @@ void JvmtiExport::post_data_dump() {
27222731

27232732
void JvmtiExport::post_monitor_contended_enter(JavaThread *thread, ObjectMonitor *obj_mntr) {
27242733
oop object = obj_mntr->object();
2725-
JvmtiThreadState *state = thread->jvmti_thread_state();
2734+
HandleMark hm(thread);
2735+
Handle h(thread, object);
2736+
2737+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
27262738
if (state == nullptr) {
27272739
return;
27282740
}
27292741
if (thread->is_in_any_VTMS_transition()) {
27302742
return; // no events should be posted if thread is in any VTMS transition
27312743
}
27322744

2733-
HandleMark hm(thread);
2734-
Handle h(thread, object);
2735-
27362745
EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTER,
27372746
("[%s] monitor contended enter event triggered",
27382747
JvmtiTrace::safe_get_thread_name(thread)));
@@ -2755,17 +2764,17 @@ void JvmtiExport::post_monitor_contended_enter(JavaThread *thread, ObjectMonitor
27552764

27562765
void JvmtiExport::post_monitor_contended_entered(JavaThread *thread, ObjectMonitor *obj_mntr) {
27572766
oop object = obj_mntr->object();
2758-
JvmtiThreadState *state = thread->jvmti_thread_state();
2767+
HandleMark hm(thread);
2768+
Handle h(thread, object);
2769+
2770+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
27592771
if (state == nullptr) {
27602772
return;
27612773
}
27622774
if (thread->is_in_any_VTMS_transition()) {
27632775
return; // no events should be posted if thread is in any VTMS transition
27642776
}
27652777

2766-
HandleMark hm(thread);
2767-
Handle h(thread, object);
2768-
27692778
EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_CONTENDED_ENTERED,
27702779
("[%s] monitor contended entered event triggered",
27712780
JvmtiTrace::safe_get_thread_name(thread)));
@@ -2789,17 +2798,17 @@ void JvmtiExport::post_monitor_contended_entered(JavaThread *thread, ObjectMonit
27892798

27902799
void JvmtiExport::post_monitor_wait(JavaThread *thread, oop object,
27912800
jlong timeout) {
2792-
JvmtiThreadState *state = thread->jvmti_thread_state();
2801+
HandleMark hm(thread);
2802+
Handle h(thread, object);
2803+
2804+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
27932805
if (state == nullptr) {
27942806
return;
27952807
}
27962808
if (thread->is_in_any_VTMS_transition()) {
27972809
return; // no events should be posted if thread is in any VTMS transition
27982810
}
27992811

2800-
HandleMark hm(thread);
2801-
Handle h(thread, object);
2802-
28032812
EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAIT,
28042813
("[%s] monitor wait event triggered",
28052814
JvmtiTrace::safe_get_thread_name(thread)));
@@ -2823,17 +2832,17 @@ void JvmtiExport::post_monitor_wait(JavaThread *thread, oop object,
28232832

28242833
void JvmtiExport::post_monitor_waited(JavaThread *thread, ObjectMonitor *obj_mntr, jboolean timed_out) {
28252834
oop object = obj_mntr->object();
2826-
JvmtiThreadState *state = thread->jvmti_thread_state();
2835+
HandleMark hm(thread);
2836+
Handle h(thread, object);
2837+
2838+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
28272839
if (state == nullptr) {
28282840
return;
28292841
}
28302842
if (thread->is_in_any_VTMS_transition()) {
28312843
return; // no events should be posted if thread is in any VTMS transition
28322844
}
28332845

2834-
HandleMark hm(thread);
2835-
Handle h(thread, object);
2836-
28372846
EVT_TRIG_TRACE(JVMTI_EVENT_MONITOR_WAITED,
28382847
("[%s] monitor waited event triggered",
28392848
JvmtiTrace::safe_get_thread_name(thread)));
@@ -2886,7 +2895,10 @@ void JvmtiExport::post_vm_object_alloc(JavaThread *thread, oop object) {
28862895
}
28872896

28882897
void JvmtiExport::post_sampled_object_alloc(JavaThread *thread, oop object) {
2889-
JvmtiThreadState *state = thread->jvmti_thread_state();
2898+
HandleMark hm(thread);
2899+
Handle h(thread, object);
2900+
2901+
JvmtiThreadState *state = get_jvmti_thread_state(thread);
28902902
if (state == nullptr) {
28912903
return;
28922904
}
@@ -2896,8 +2908,6 @@ void JvmtiExport::post_sampled_object_alloc(JavaThread *thread, oop object) {
28962908
if (thread->is_in_any_VTMS_transition()) {
28972909
return; // no events should be posted if thread is in any VTMS transition
28982910
}
2899-
HandleMark hm(thread);
2900-
Handle h(thread, object);
29012911

29022912
EVT_TRIG_TRACE(JVMTI_EVENT_SAMPLED_OBJECT_ALLOC,
29032913
("[%s] Trg sampled object alloc triggered",

‎src/hotspot/share/prims/jvmtiExport.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ class JvmtiExport : public AllStatic {
298298
static void decode_version_values(jint version, int * major, int * minor,
299299
int * micro) NOT_JVMTI_RETURN;
300300

301+
// If the jvmti_thread_state is absent and any thread filtered event
302+
// is enabled globally then it is created.
303+
// Otherwise, the thread->jvmti_thread_state() is returned.
304+
static JvmtiThreadState* get_jvmti_thread_state(JavaThread *thread);
305+
301306
// single stepping management methods
302307
static void at_single_stepping_point(JavaThread *thread, Method* method, address location) NOT_JVMTI_RETURN;
303308
static void expose_single_stepping(JavaThread *thread) NOT_JVMTI_RETURN;

0 commit comments

Comments
 (0)
Failed to load comments.