< prev index next >

src/hotspot/share/prims/jvmtiThreadState.cpp

Print this page

  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "jvmtifiles/jvmtiEnv.hpp"
 27 #include "memory/resourceArea.hpp"

 28 #include "prims/jvmtiEventController.inline.hpp"
 29 #include "prims/jvmtiImpl.hpp"
 30 #include "prims/jvmtiThreadState.inline.hpp"



 31 #include "runtime/safepointVerifiers.hpp"

 32 #include "runtime/vframe.hpp"

 33 
 34 // marker for when the stack depth has been reset and is now unknown.
 35 // any negative number would work but small ones might obscure an
 36 // underrun error.
 37 static const int UNKNOWN_STACK_DEPTH = -99;
 38 
 39 ///////////////////////////////////////////////////////////////
 40 //
 41 // class JvmtiThreadState
 42 //
 43 // Instances of JvmtiThreadState hang off of each thread.
 44 // Thread local storage for JVMTI.
 45 //
 46 
 47 JvmtiThreadState *JvmtiThreadState::_head = NULL;
 48 
 49 JvmtiThreadState::JvmtiThreadState(JavaThread* thread)
 50   : _thread_event_enable() {
 51   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
 52   _thread               = thread;

 53   _exception_state      = ES_CLEARED;
 54   _debuggable           = true;
 55   _hide_single_stepping = false;
 56   _hide_level           = 0;
 57   _pending_step_for_popframe = false;
 58   _class_being_redefined = NULL;
 59   _class_load_kind = jvmti_class_load_kind_load;
 60   _classes_being_redefined = NULL;
 61   _head_env_thread_state = NULL;
 62   _dynamic_code_event_collector = NULL;
 63   _vm_object_alloc_event_collector = NULL;
 64   _sampled_object_alloc_event_collector = NULL;
 65   _the_class_for_redefinition_verification = NULL;
 66   _scratch_class_for_redefinition_verification = NULL;
 67   _cur_stack_depth = UNKNOWN_STACK_DEPTH;

 68 
 69   // JVMTI ForceEarlyReturn support
 70   _pending_step_for_earlyret = false;
 71   _earlyret_state = earlyret_inactive;
 72   _earlyret_tos = ilgl;
 73   _earlyret_value.j = 0L;
 74   _earlyret_oop = NULL;
 75 
 76   _jvmti_event_queue = NULL;




 77 
 78   // add all the JvmtiEnvThreadState to the new JvmtiThreadState
 79   {
 80     JvmtiEnvIterator it;
 81     for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
 82       if (env->is_valid()) {
 83         add_env(env);
 84       }
 85     }
 86   }
 87 
 88   // link us into the list
 89   {
 90     // The thread state list manipulation code must not have safepoints.
 91     // See periodic_clean_up().
 92     debug_only(NoSafepointVerifier nosafepoint;)
 93 
 94     _prev = NULL;
 95     _next = _head;
 96     if (_head != NULL) {
 97       _head->_prev = this;
 98     }
 99     _head = this;
100   }
101 
102   // set this as the state for the thread
103   thread->set_jvmti_thread_state(this);











104 }
105 
106 
107 JvmtiThreadState::~JvmtiThreadState()   {
108   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
109 
110   if (_classes_being_redefined != NULL) {
111     delete _classes_being_redefined; // free the GrowableArray on C heap
112   }
113 
114   // clear this as the state for the thread
115   get_thread()->set_jvmti_thread_state(NULL);
116 
117   // zap our env thread states
118   {
119     JvmtiEnvBase::entering_dying_thread_env_iteration();
120     JvmtiEnvThreadStateIterator it(this);
121     for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ) {
122       JvmtiEnvThreadState* zap = ets;
123       ets = it.next(ets);

128 
129   // remove us from the list
130   {
131     // The thread state list manipulation code must not have safepoints.
132     // See periodic_clean_up().
133     debug_only(NoSafepointVerifier nosafepoint;)
134 
135     if (_prev == NULL) {
136       assert(_head == this, "sanity check");
137       _head = _next;
138     } else {
139       assert(_head != this, "sanity check");
140       _prev->_next = _next;
141     }
142     if (_next != NULL) {
143       _next->_prev = _prev;
144     }
145     _next = NULL;
146     _prev = NULL;
147   }




148 }
149 
150 
151 void
152 JvmtiThreadState::periodic_clean_up() {
153   assert(SafepointSynchronize::is_at_safepoint(), "at safepoint");
154 
155   // This iteration is initialized with "_head" instead of "JvmtiThreadState::first()"
156   // because the latter requires the JvmtiThreadState_lock.
157   // This iteration is safe at a safepoint as well, see the NoSafepointVerifier
158   // asserts at all list manipulation sites.
159   for (JvmtiThreadState *state = _head; state != NULL; state = state->next()) {
160     // For each environment thread state corresponding to an invalid environment
161     // unlink it from the list and deallocate it.
162     JvmtiEnvThreadStateIterator it(state);
163     JvmtiEnvThreadState* previous_ets = NULL;
164     JvmtiEnvThreadState* ets = it.first();
165     while (ets != NULL) {
166       if (ets->get_env()->is_valid()) {
167         previous_ets = ets;
168         ets = it.next(ets);
169       } else {
170         // This one isn't valid, remove it from the list and deallocate it
171         JvmtiEnvThreadState* defunct_ets = ets;
172         ets = ets->next();
173         if (previous_ets == NULL) {
174           assert(state->head_env_thread_state() == defunct_ets, "sanity check");
175           state->set_head_env_thread_state(ets);
176         } else {
177           previous_ets->set_next(ets);
178         }
179         delete defunct_ets;
180       }
181     }
182   }
183 }
184 




























































































































































































































































































































185 void JvmtiThreadState::add_env(JvmtiEnvBase *env) {
186   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
187 
188   JvmtiEnvThreadState *new_ets = new JvmtiEnvThreadState(_thread, env);
189   // add this environment thread state to the end of the list (order is important)
190   {
191     // list deallocation (which occurs at a safepoint) cannot occur simultaneously
192     debug_only(NoSafepointVerifier nosafepoint;)
193 
194     JvmtiEnvThreadStateIterator it(this);
195     JvmtiEnvThreadState* previous_ets = NULL;
196     for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
197       previous_ets = ets;
198     }
199     if (previous_ets == NULL) {
200       set_head_env_thread_state(new_ets);
201     } else {
202       previous_ets->set_next(new_ets);
203     }
204   }
205 }
206 
207 
208 
209 
210 void JvmtiThreadState::enter_interp_only_mode() {
211   assert(_thread->get_interp_only_mode() == 0, "entering interp only when mode not zero");
212   _thread->increment_interp_only_mode();



















213 }
214 
215 
216 void JvmtiThreadState::leave_interp_only_mode() {
217   assert(_thread->get_interp_only_mode() == 1, "leaving interp only when mode not one");
218   _thread->decrement_interp_only_mode();





219 }
220 
221 
222 // Helper routine used in several places
223 int JvmtiThreadState::count_frames() {







224 #ifdef ASSERT
225   Thread *current_thread = Thread::current();
226 #endif
227   assert(SafepointSynchronize::is_at_safepoint() ||
228          get_thread()->is_handshake_safe_for(current_thread),
229          "call by myself / at safepoint / at handshake");
230 
231   if (!get_thread()->has_last_Java_frame()) return 0;  // no Java frames
232 
233   ResourceMark rm;
234   RegisterMap reg_map(get_thread());
235   javaVFrame *jvf = get_thread()->last_java_vframe(&reg_map);
236   int n = 0;
237   while (jvf != NULL) {
238     Method* method = jvf->method();
239     jvf = jvf->java_sender();
240     n++;
241   }
242   return n;
243 }
244 
245 
246 void JvmtiThreadState::invalidate_cur_stack_depth() {
247   assert(SafepointSynchronize::is_at_safepoint() ||
248          get_thread()->is_handshake_safe_for(Thread::current()),
249          "bad synchronization with owner thread");
250 
251   _cur_stack_depth = UNKNOWN_STACK_DEPTH;
252 }
253 
254 void JvmtiThreadState::incr_cur_stack_depth() {
255   guarantee(JavaThread::current() == get_thread(), "must be current thread");
256 
257   if (!is_interp_only_mode()) {
258     _cur_stack_depth = UNKNOWN_STACK_DEPTH;
259   }
260   if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) {
261     ++_cur_stack_depth;








262   }
263 }
264 
265 void JvmtiThreadState::decr_cur_stack_depth() {
266   guarantee(JavaThread::current() == get_thread(), "must be current thread");
267 
268   if (!is_interp_only_mode()) {
269     _cur_stack_depth = UNKNOWN_STACK_DEPTH;
270   }
271   if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) {








272     --_cur_stack_depth;
273     assert(_cur_stack_depth >= 0, "incr/decr_cur_stack_depth mismatch");
274   }
275 }
276 
277 int JvmtiThreadState::cur_stack_depth() {
278   Thread *current = Thread::current();
279   guarantee(get_thread()->is_handshake_safe_for(current),
280             "must be current thread or direct handshake");
281 
282   if (!is_interp_only_mode() || _cur_stack_depth == UNKNOWN_STACK_DEPTH) {
283     _cur_stack_depth = count_frames();
284   } else {
285 #ifdef ASSERT
286     if (EnableJVMTIStackDepthAsserts) {
287       // heavy weight assert
288       jint num_frames = count_frames();
289       assert(_cur_stack_depth == num_frames, "cur_stack_depth out of sync _cur_stack_depth: %d num_frames: %d",
290              _cur_stack_depth, num_frames);
291     }
292 #endif
293   }
294   return _cur_stack_depth;
295 }
296 
297 void JvmtiThreadState::process_pending_step_for_popframe() {
298   // We are single stepping as the last part of the PopFrame() dance
299   // so we have some house keeping to do.
300 
301   JavaThread *thr = get_thread();
302   if (thr->popframe_condition() != JavaThread::popframe_inactive) {
303     // If the popframe_condition field is not popframe_inactive, then
304     // we missed all of the popframe_field cleanup points:
305     //
306     // - unpack_frames() was not called (nothing to deopt)
307     // - remove_activation_preserving_args_entry() was not called
308     //   (did not get suspended in a call_vm() family call and did
309     //   not complete a call_vm() family call on the way here)
310     thr->clear_popframe_condition();

428   if (_jvmti_event_queue == NULL) {
429     _jvmti_event_queue = new JvmtiDeferredEventQueue();
430   }
431   // copy the event
432   _jvmti_event_queue->enqueue(*event);
433 }
434 
435 void JvmtiThreadState::post_events(JvmtiEnv* env) {
436   if (_jvmti_event_queue != NULL) {
437     _jvmti_event_queue->post(env);  // deletes each queue node
438     delete _jvmti_event_queue;
439     _jvmti_event_queue = NULL;
440   }
441 }
442 
443 void JvmtiThreadState::run_nmethod_entry_barriers() {
444   if (_jvmti_event_queue != NULL) {
445     _jvmti_event_queue->run_nmethod_entry_barriers();
446   }
447 }























  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "jvmtifiles/jvmtiEnv.hpp"
 27 #include "memory/resourceArea.hpp"
 28 #include "oops/oopHandle.inline.hpp"
 29 #include "prims/jvmtiEventController.inline.hpp"
 30 #include "prims/jvmtiImpl.hpp"
 31 #include "prims/jvmtiThreadState.inline.hpp"
 32 #include "runtime/handles.inline.hpp"
 33 #include "runtime/interfaceSupport.inline.hpp"
 34 #include "runtime/jniHandles.hpp"
 35 #include "runtime/safepointVerifiers.hpp"
 36 #include "runtime/stackFrameStream.inline.hpp"
 37 #include "runtime/vframe.hpp"
 38 #include "prims/jvmtiEnvBase.hpp"
 39 
 40 // marker for when the stack depth has been reset and is now unknown.
 41 // any negative number would work but small ones might obscure an
 42 // underrun error.
 43 static const int UNKNOWN_STACK_DEPTH = -99;
 44 
 45 ///////////////////////////////////////////////////////////////
 46 //
 47 // class JvmtiThreadState
 48 //
 49 // Instances of JvmtiThreadState hang off of each thread.
 50 // Thread local storage for JVMTI.
 51 //
 52 
 53 JvmtiThreadState *JvmtiThreadState::_head = NULL;
 54 
 55 JvmtiThreadState::JvmtiThreadState(JavaThread* thread, oop thread_oop)
 56   : _thread_event_enable() {
 57   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
 58   _thread               = thread;
 59   _thread_saved         = NULL;
 60   _exception_state      = ES_CLEARED;
 61   _debuggable           = true;
 62   _hide_single_stepping = false;
 63   _hide_level           = 0;
 64   _pending_step_for_popframe = false;
 65   _class_being_redefined = NULL;
 66   _class_load_kind = jvmti_class_load_kind_load;
 67   _classes_being_redefined = NULL;
 68   _head_env_thread_state = NULL;
 69   _dynamic_code_event_collector = NULL;
 70   _vm_object_alloc_event_collector = NULL;
 71   _sampled_object_alloc_event_collector = NULL;
 72   _the_class_for_redefinition_verification = NULL;
 73   _scratch_class_for_redefinition_verification = NULL;
 74   _cur_stack_depth = UNKNOWN_STACK_DEPTH;
 75   _saved_interp_only_mode = 0;
 76 
 77   // JVMTI ForceEarlyReturn support
 78   _pending_step_for_earlyret = false;
 79   _earlyret_state = earlyret_inactive;
 80   _earlyret_tos = ilgl;
 81   _earlyret_value.j = 0L;
 82   _earlyret_oop = NULL;
 83 
 84   _jvmti_event_queue = NULL;
 85   _is_in_VTMT = false;
 86   _is_virtual = false;
 87 
 88   _thread_oop_h = OopHandle(JvmtiExport::jvmti_oop_storage(), thread_oop);
 89 
 90   // add all the JvmtiEnvThreadState to the new JvmtiThreadState
 91   {
 92     JvmtiEnvIterator it;
 93     for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
 94       if (env->is_valid()) {
 95         add_env(env);
 96       }
 97     }
 98   }
 99 
100   // link us into the list
101   {
102     // The thread state list manipulation code must not have safepoints.
103     // See periodic_clean_up().
104     debug_only(NoSafepointVerifier nosafepoint;)
105 
106     _prev = NULL;
107     _next = _head;
108     if (_head != NULL) {
109       _head->_prev = this;
110     }
111     _head = this;
112   }
113 
114   if (thread_oop != NULL) {
115     java_lang_Thread::set_jvmti_thread_state(thread_oop, (JvmtiThreadState*)this);
116     _is_virtual = java_lang_VirtualThread::is_instance(thread_oop);
117   }
118 
119   // thread can be NULL if virtual thread is unmounted
120   if (thread != NULL) {
121     // set this as the state for the thread only if thread_oop is current thread->mounted_vthread()
122     if (thread_oop == NULL || thread->mounted_vthread() == NULL || thread->mounted_vthread() == thread_oop) {
123       thread->set_jvmti_thread_state(this);
124     }
125     thread->set_interp_only_mode(0);
126   }
127 }
128 
129 
130 JvmtiThreadState::~JvmtiThreadState()   {
131   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
132 
133   if (_classes_being_redefined != NULL) {
134     delete _classes_being_redefined; // free the GrowableArray on C heap
135   }
136 
137   // clear this as the state for the thread
138   get_thread()->set_jvmti_thread_state(NULL);
139 
140   // zap our env thread states
141   {
142     JvmtiEnvBase::entering_dying_thread_env_iteration();
143     JvmtiEnvThreadStateIterator it(this);
144     for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ) {
145       JvmtiEnvThreadState* zap = ets;
146       ets = it.next(ets);

151 
152   // remove us from the list
153   {
154     // The thread state list manipulation code must not have safepoints.
155     // See periodic_clean_up().
156     debug_only(NoSafepointVerifier nosafepoint;)
157 
158     if (_prev == NULL) {
159       assert(_head == this, "sanity check");
160       _head = _next;
161     } else {
162       assert(_head != this, "sanity check");
163       _prev->_next = _next;
164     }
165     if (_next != NULL) {
166       _next->_prev = _prev;
167     }
168     _next = NULL;
169     _prev = NULL;
170   }
171   if (get_thread_oop() != NULL) {
172     java_lang_Thread::set_jvmti_thread_state(get_thread_oop(), NULL);
173   }
174   _thread_oop_h.release(JvmtiExport::jvmti_oop_storage());
175 }
176 
177 
178 void
179 JvmtiThreadState::periodic_clean_up() {
180   assert(SafepointSynchronize::is_at_safepoint(), "at safepoint");
181 
182   // This iteration is initialized with "_head" instead of "JvmtiThreadState::first()"
183   // because the latter requires the JvmtiThreadState_lock.
184   // This iteration is safe at a safepoint as well, see the NoSafepointVerifier
185   // asserts at all list manipulation sites.
186   for (JvmtiThreadState *state = _head; state != NULL; state = state->next()) {
187     // For each environment thread state corresponding to an invalid environment
188     // unlink it from the list and deallocate it.
189     JvmtiEnvThreadStateIterator it(state);
190     JvmtiEnvThreadState* previous_ets = NULL;
191     JvmtiEnvThreadState* ets = it.first();
192     while (ets != NULL) {
193       if (ets->get_env()->is_valid()) {
194         previous_ets = ets;
195         ets = it.next(ets);
196       } else {
197         // This one isn't valid, remove it from the list and deallocate it
198         JvmtiEnvThreadState* defunct_ets = ets;
199         ets = ets->next();
200         if (previous_ets == NULL) {
201           assert(state->head_env_thread_state() == defunct_ets, "sanity check");
202           state->set_head_env_thread_state(ets);
203         } else {
204           previous_ets->set_next(ets);
205         }
206         delete defunct_ets;
207       }
208     }
209   }
210 }
211 
212 /* Virtual Threads Mount Transition (VTMT) mechanism */
213 
214 #ifdef ASSERT
215 // used for debugging
216 volatile unsigned short JvmtiVTMTDisabler::_suspend_count = 0;
217 #endif
218 
219 // VTMT can not be disabled while this counter is positive
220 volatile unsigned short JvmtiVTMTDisabler::_VTMT_count = 0;
221 
222 // VTMT is disabled while this counter is positive
223 volatile unsigned short JvmtiVTMTDisabler::_VTMT_disable_count = 0;
224 
225 JvmtiVTMTDisabler::JvmtiVTMTDisabler(bool is_suspender) {
226   _self_suspend = false;
227   _is_suspender = is_suspender;
228 #ifdef ASSERT
229   {
230     ThreadBlockInVM tbivm(JavaThread::current());
231     MonitorLocker ml(JvmtiVTMT_lock, Mutex::_no_safepoint_check_flag);
232     if (_is_suspender) {
233       _suspend_count++;
234     }
235   }
236 #endif
237   disable_VTMT();
238 }
239 
240 JvmtiVTMTDisabler::~JvmtiVTMTDisabler() {
241 #ifdef ASSERT
242   {
243     ThreadBlockInVM tbivm(JavaThread::current());
244     MonitorLocker ml(JvmtiVTMT_lock, Mutex::_no_safepoint_check_flag);
245     if (_is_suspender) {
246       _suspend_count--;
247     }
248   }
249 #endif
250   enable_VTMT();
251   if (_self_suspend) {
252     JvmtiSuspendControl::suspend(JavaThread::current());
253   }
254 }
255 
256 void
257 JvmtiVTMTDisabler::set_self_suspend() {
258   _self_suspend = true;
259 }
260 
261 #ifdef ASSERT
262 void
263 JvmtiVTMTDisabler::print_info() {
264   tty->print_cr("_VTMT_disable_count: %d _VTMT_count: %d _suspend_count: %d\n",
265                 _VTMT_disable_count, _VTMT_count, _suspend_count);
266   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *java_thread = jtiwh.next(); ) {
267     ResourceMark rm;
268     // Handshake with target
269     PrintStackTraceClosure pstc;
270     Handshake::execute(&pstc, java_thread);
271   }
272 }
273 #endif
274 
275 void
276 JvmtiVTMTDisabler::disable_VTMT() {
277   JavaThread* thread = JavaThread::current();
278   int attempts = 10;
279   {
280     ThreadBlockInVM tbivm(thread);
281     MonitorLocker ml(JvmtiVTMT_lock, Mutex::_no_safepoint_check_flag);
282 
283     assert(!thread->is_in_VTMT(), "VTMT sanity check");
284     _VTMT_disable_count++;
285 #if 0
286     assert(_suspend_count < 2, "TMP assert if we ever have multiple suspenders");
287     assert(_suspend_count == 0 || _VTMT_disable_count < 2, "TMP assert if we ever have disablers with suspender");
288 #endif
289 
290     // Block while some mount/unmount transitions are in progress.
291     // Debug version fails and print diagnostic information
292     while (_VTMT_count > 0) {
293       if (ml.wait(1000)) {
294         attempts--;
295       }
296       DEBUG_ONLY(if (attempts == 0) break;)
297     }
298     assert(!thread->is_VTMT_disabler(), "VTMT sanity check");
299     if (attempts != 0) {
300       thread->set_is_VTMT_disabler(true);
301     }
302   }
303 #ifdef ASSERT
304   if (attempts == 0) {
305     print_info();
306     assert(false, "stuck in JvmtiVTMTDisabler::disable_VTMT for 10 seconds.");
307   }
308 #endif
309 }
310 
311 void
312 JvmtiVTMTDisabler::enable_VTMT() {
313   MonitorLocker ml(JvmtiVTMT_lock, Mutex::_no_safepoint_check_flag);
314   assert(_VTMT_count == 0 && _VTMT_disable_count > 0, "VTMT sanity check");
315 
316 #if 0
317     assert(_suspend_count < 2, "TMP assert if we ever have multiple suspenders");
318     assert(_suspend_count == 0 || _VTMT_disable_count < 2, "TMP assert if we ever have disablers with suspender");
319 #endif
320  
321   if (--_VTMT_disable_count == 0) {
322     ml.notify_all();
323   }
324   JavaThread* thread = JavaThread::current();
325   thread->set_is_VTMT_disabler(false);
326 }
327 
328 void
329 JvmtiVTMTDisabler::start_VTMT(jthread vthread, int callsite_tag) {
330   JavaThread* thread = JavaThread::current();
331   HandleMark hm(thread);
332   Handle vth = Handle(thread, JNIHandles::resolve_external_guard(vthread));
333 
334   // Do not allow suspends inside VTMT transitions.
335   // Block while transitions are disabled or there are suspend requests.
336   int attempts = 10 * 100;
337   while (true) {
338     ThreadBlockInVM tbivm(thread);
339     MonitorLocker ml(JvmtiVTMT_lock, Mutex::_no_safepoint_check_flag);
340 
341 #if 0
342     assert(_suspend_count < 2, "TMP assert if we ever have multiple suspenders");
343     assert(_suspend_count == 0 || _VTMT_disable_count < 2, "TMP assert if we ever have disablers with suspender");
344 #endif
345     // block while transitions are disabled or there are suspend requests
346     if (_VTMT_disable_count > 0 ||
347         thread->is_suspended() ||
348         JvmtiVTSuspender::is_vthread_suspended(vth())
349     ) {
350       if (ml.wait(10)) {
351         attempts--;
352       }
353       DEBUG_ONLY(if (attempts == 0) break;)
354       continue; // ~ThreadBlockInVM has handshake-based suspend point
355     }
356     assert(!thread->is_in_VTMT(), "VTMT sanity check");
357     thread->set_is_in_VTMT(true);
358     JvmtiThreadState* vstate = java_lang_Thread::jvmti_thread_state(vth());
359     if (vstate != NULL) {
360       vstate->set_is_in_VTMT(true);
361     }
362     _VTMT_count++;
363     break;
364   }
365 #ifdef ASSERT
366   if (attempts == 0) {
367     tty->print_cr("DBG: start_VTMT: thread->is_suspended: %d is_vthread_suspended: %d\n",
368                   thread->is_suspended(), JvmtiVTSuspender::is_vthread_suspended(vth()));
369     print_info();
370     assert(false, "stuck in JvmtiVTMTDisabler::start_VTMT for 10 seconds.");
371   }
372 #endif
373 }
374 
375 void
376 JvmtiVTMTDisabler::finish_VTMT(jthread vthread, int callsite_tag) {
377   JavaThread* thread = JavaThread::current();
378   MonitorLocker ml(JvmtiVTMT_lock, Mutex::_no_safepoint_check_flag);
379 
380 #if 0
381     assert(_suspend_count < 2, "TMP assert if we ever have multiple suspenders");
382     assert(_suspend_count == 0 || _VTMT_disable_count < 2, "TMP assert if we ever have disablers with suspender");
383 #endif
384   _VTMT_count--;
385 
386   // unblock waiting VTMT disablers
387   if (_VTMT_disable_count > 0) {
388     ml.notify_all();
389   }
390   assert(thread->is_in_VTMT(), "sanity check");
391   thread->set_is_in_VTMT(false);
392   oop vt = JNIHandles::resolve_external_guard(vthread);
393   JvmtiThreadState* vstate = java_lang_Thread::jvmti_thread_state(vt);
394   if (vstate != NULL) {
395     vstate->set_is_in_VTMT(false);
396   }
397 }
398 
399 /* VThreadList implementation */
400 
401 int
402 VThreadList::find(oop vt) const {
403   for (int idx = 0; idx < length(); idx++) {
404     if (vt == at(idx).resolve()) return idx;
405   }
406   return -1;
407 }
408 
409 bool
410 VThreadList::contains(oop vt) const {
411   int idx = find(vt);
412   return idx != -1;
413 }
414 
415 static OopHandle NULLHandle = OopHandle(NULL);
416 
417 void
418 VThreadList::append(oop vt) {
419   assert(!contains(vt), "VThreadList::append sanity check");
420 
421   OopHandle vthandle(JvmtiExport::jvmti_oop_storage(), vt);
422   GrowableArrayCHeap<OopHandle, mtServiceability>::append(vthandle);
423 }
424 
425 void
426 VThreadList::remove(oop vt) {
427   int idx = find(vt);
428   assert(idx != -1, "VThreadList::remove sanity check");
429   at(idx).release(JvmtiExport::jvmti_oop_storage());
430   at_put(idx, NULLHandle); // clear released OopHandle entry
431 
432   // To work around assert in OopHandle assignment operator do not use remove_at().
433   // OopHandle doesn't allow overwrites if the oop pointer is non-null.
434   // Order doesn't matter, put the last element in idx
435   int last = length() - 1;
436   if (last > idx) {
437     at_put(idx, at(last));
438     at_put(last, NULLHandle); // clear moved OopHandle entry.
439   }
440   pop();
441 }
442 
443 void
444 VThreadList::invalidate() {
445   for (int idx = length() - 1; idx >= 0; idx--) {
446     at(idx).release(JvmtiExport::jvmti_oop_storage());
447     at_put(idx, NULLHandle); // clear released OopHandle entries
448   }
449   clear();
450 }
451 
452 /* Virtual Threads Suspend/Resume management */
453 
454 JvmtiVTSuspender::VThreadSuspendMode
455 JvmtiVTSuspender::_vthread_suspend_mode = vthread_suspend_none;
456 
457 VThreadList*
458 JvmtiVTSuspender::_vthread_suspend_list = new VThreadList();
459 
460 VThreadList*
461 JvmtiVTSuspender::_vthread_resume_list = new VThreadList();
462 
463 void
464 JvmtiVTSuspender::register_all_vthreads_suspend() {
465   MonitorLocker ml(JvmtiVTMT_lock, Mutex::_no_safepoint_check_flag);
466 
467   _vthread_suspend_mode = vthread_suspend_all;
468   _vthread_suspend_list->invalidate();
469   _vthread_resume_list->invalidate();
470 }
471 
472 void
473 JvmtiVTSuspender::register_all_vthreads_resume() {
474   MonitorLocker ml(JvmtiVTMT_lock, Mutex::_no_safepoint_check_flag);
475 
476   _vthread_suspend_mode = vthread_suspend_none;
477   _vthread_suspend_list->invalidate();
478   _vthread_resume_list->invalidate();
479 }
480 
481 bool
482 JvmtiVTSuspender::register_vthread_suspend(oop vt) {
483   MonitorLocker ml(JvmtiVTMT_lock, Mutex::_no_safepoint_check_flag);
484 
485   if (_vthread_suspend_mode == vthread_suspend_all) {
486     assert(_vthread_resume_list->contains(vt),
487            "register_vthread_suspend sanity check");
488     _vthread_resume_list->remove(vt);
489   } else {
490     assert(!_vthread_suspend_list->contains(vt),
491            "register_vthread_suspend sanity check");
492     _vthread_suspend_mode = vthread_suspend_ind;
493     _vthread_suspend_list->append(vt);
494   }
495   return true;
496 }
497 
498 bool
499 JvmtiVTSuspender::register_vthread_resume(oop vt) {
500   MonitorLocker ml(JvmtiVTMT_lock, Mutex::_no_safepoint_check_flag);
501 
502   if (_vthread_suspend_mode == vthread_suspend_all) {
503     assert(!_vthread_resume_list->contains(vt),
504            "register_vthread_resume sanity check");
505     _vthread_resume_list->append(vt);
506   } else if (_vthread_suspend_mode == vthread_suspend_ind) {
507     assert(_vthread_suspend_list->contains(vt),
508            "register_vthread_resume check");
509     _vthread_suspend_list->remove(vt);
510     if (_vthread_suspend_list->length() == 0) {
511       _vthread_suspend_mode = vthread_suspend_none;
512     }
513   } else {
514     assert(false, "register_vthread_resume: no suspend mode enabled");
515   }
516   return true;
517 }
518 
519 bool
520 JvmtiVTSuspender::is_vthread_suspended(oop vt) {
521   bool suspend_is_needed =
522    (_vthread_suspend_mode == vthread_suspend_all && !_vthread_resume_list->contains(vt)) ||
523    (_vthread_suspend_mode == vthread_suspend_ind && _vthread_suspend_list->contains(vt));
524 
525   return suspend_is_needed;
526 }
527 
528 void JvmtiThreadState::add_env(JvmtiEnvBase *env) {
529   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
530 
531   JvmtiEnvThreadState *new_ets = new JvmtiEnvThreadState(this, env);
532   // add this environment thread state to the end of the list (order is important)
533   {
534     // list deallocation (which occurs at a safepoint) cannot occur simultaneously
535     debug_only(NoSafepointVerifier nosafepoint;)
536 
537     JvmtiEnvThreadStateIterator it(this);
538     JvmtiEnvThreadState* previous_ets = NULL;
539     for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
540       previous_ets = ets;
541     }
542     if (previous_ets == NULL) {
543       set_head_env_thread_state(new_ets);
544     } else {
545       previous_ets->set_next(new_ets);
546     }
547   }
548 }
549 



550 void JvmtiThreadState::enter_interp_only_mode() {
551   if (_thread == NULL) {
552     assert(!is_interp_only_mode(), "entering interp only when mode not zero");
553     ++_saved_interp_only_mode;
554     // TBD: It seems, invalidate_cur_stack_depth() has to be called at VTMT?
555   } else {
556 #ifdef DBG // TMP
557   if (is_interp_only_mode()) {
558     ResourceMark rm(JavaThread::current());
559     oop name_oop = java_lang_Thread::name(get_thread_oop());
560     const char* name_str = java_lang_String::as_utf8_string(name_oop);
561     name_str = name_str == NULL ? "<NULL>" : name_str;
562 
563     const char* virt = is_virtual() ? "virtual" : "carrier";
564     printf("DBG: enter_interp_only_mode: %s state: %p, interp_only: %d, saved_interp_only: %d, %s\n",
565            virt, (void*)this, _thread->get_interp_only_mode(), _saved_interp_only_mode, name_str); fflush(0);
566   }
567 #endif
568     assert(!is_interp_only_mode(), "entering interp only when mode not zero");
569     _thread->increment_interp_only_mode();
570     invalidate_cur_stack_depth();
571   }
572 }
573 
574 
575 void JvmtiThreadState::leave_interp_only_mode() {
576   if (_thread == NULL) {
577     assert(is_interp_only_mode(), "leaving interp only when mode not one");
578     --_saved_interp_only_mode;
579   } else {
580     assert(is_interp_only_mode(), "leaving interp only when mode not one");
581     _thread->decrement_interp_only_mode();
582   }
583 }
584 
585 
586 // Helper routine used in several places
587 int JvmtiThreadState::count_frames() {
588   JavaThread* thread = get_thread_or_saved();
589   javaVFrame *jvf;
590   ResourceMark rm;
591   if (thread == NULL) {
592     oop thread_obj = get_thread_oop();
593     jvf = JvmtiEnvBase::get_vthread_jvf(thread_obj);
594   } else {
595 #ifdef ASSERT
596     Thread *current_thread = Thread::current();
597 #endif
598     assert(SafepointSynchronize::is_at_safepoint() ||
599         thread->is_handshake_safe_for(current_thread),
600            "call by myself / at safepoint / at handshake");
601     if (!thread->has_last_Java_frame()) return 0;  // no Java frames
602     // TBD: This might need to be corrected for detached carrier threads.
603     RegisterMap reg_map(thread, false, false, true);
604     jvf = thread->last_java_vframe(&reg_map);
605     jvf = JvmtiEnvBase::check_and_skip_hidden_frames(thread, jvf);






606   }
607   return (int)JvmtiEnvBase::get_frame_count(jvf);
608 }
609 
610 
611 void JvmtiThreadState::invalidate_cur_stack_depth() {
612   assert(SafepointSynchronize::is_at_safepoint() ||
613          get_thread()->is_handshake_safe_for(Thread::current()),
614          "bad synchronization with owner thread");
615 
616   _cur_stack_depth = UNKNOWN_STACK_DEPTH;
617 }
618 
619 void JvmtiThreadState::incr_cur_stack_depth() {
620   guarantee(JavaThread::current() == get_thread(), "must be current thread");
621 
622   if (!is_interp_only_mode()) {
623     _cur_stack_depth = UNKNOWN_STACK_DEPTH;
624   }
625   if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) {
626     ++_cur_stack_depth;
627 #ifdef ASSERT
628     if (EnableJVMTIStackDepthAsserts) {
629       // heavy weight assert
630       // fixme: remove this before merging loom with main jdk repo
631       jint num_frames = count_frames();
632       assert(_cur_stack_depth == num_frames, "cur_stack_depth out of sync _cur_stack_depth: %d num_frames: %d", _cur_stack_depth, num_frames);
633     }
634 #endif
635   }
636 }
637 
638 void JvmtiThreadState::decr_cur_stack_depth() {
639   guarantee(JavaThread::current() == get_thread(), "must be current thread");
640 
641   if (!is_interp_only_mode()) {
642     _cur_stack_depth = UNKNOWN_STACK_DEPTH;
643   }
644   if (_cur_stack_depth != UNKNOWN_STACK_DEPTH) {
645 #ifdef ASSERT
646     if (EnableJVMTIStackDepthAsserts) {
647       // heavy weight assert
648       // fixme: remove this before merging loom with main jdk repo
649       jint num_frames = count_frames();
650       assert(_cur_stack_depth == num_frames, "cur_stack_depth out of sync _cur_stack_depth: %d num_frames: %d", _cur_stack_depth, num_frames);
651     }
652 #endif
653     --_cur_stack_depth;
654     assert(_cur_stack_depth >= 0, "incr/decr_cur_stack_depth mismatch");
655   }
656 }
657 
658 int JvmtiThreadState::cur_stack_depth() {
659   Thread *current = Thread::current();
660   guarantee(get_thread()->is_handshake_safe_for(current),
661             "must be current thread or direct handshake");
662 
663   if (!is_interp_only_mode() || _cur_stack_depth == UNKNOWN_STACK_DEPTH) {
664     _cur_stack_depth = count_frames();
665   } else {
666 #ifdef ASSERT
667     if (EnableJVMTIStackDepthAsserts) {
668       // heavy weight assert
669       jint num_frames = count_frames();
670       assert(_cur_stack_depth == num_frames, "cur_stack_depth out of sync _cur_stack_depth: %d num_frames: %d", _cur_stack_depth, num_frames);

671     }
672 #endif
673   }
674   return _cur_stack_depth;
675 }
676 
677 void JvmtiThreadState::process_pending_step_for_popframe() {
678   // We are single stepping as the last part of the PopFrame() dance
679   // so we have some house keeping to do.
680 
681   JavaThread *thr = get_thread();
682   if (thr->popframe_condition() != JavaThread::popframe_inactive) {
683     // If the popframe_condition field is not popframe_inactive, then
684     // we missed all of the popframe_field cleanup points:
685     //
686     // - unpack_frames() was not called (nothing to deopt)
687     // - remove_activation_preserving_args_entry() was not called
688     //   (did not get suspended in a call_vm() family call and did
689     //   not complete a call_vm() family call on the way here)
690     thr->clear_popframe_condition();

808   if (_jvmti_event_queue == NULL) {
809     _jvmti_event_queue = new JvmtiDeferredEventQueue();
810   }
811   // copy the event
812   _jvmti_event_queue->enqueue(*event);
813 }
814 
815 void JvmtiThreadState::post_events(JvmtiEnv* env) {
816   if (_jvmti_event_queue != NULL) {
817     _jvmti_event_queue->post(env);  // deletes each queue node
818     delete _jvmti_event_queue;
819     _jvmti_event_queue = NULL;
820   }
821 }
822 
823 void JvmtiThreadState::run_nmethod_entry_barriers() {
824   if (_jvmti_event_queue != NULL) {
825     _jvmti_event_queue->run_nmethod_entry_barriers();
826   }
827 }
828 
829 oop JvmtiThreadState::get_thread_oop() {
830   return _thread_oop_h.resolve();
831 }
832 
833 void JvmtiThreadState::set_thread(JavaThread* thread) {
834   _thread_saved = NULL; // common case;
835   if (!_is_virtual && thread == NULL) {
836     // Save JavaThread* if carrier thread is being detached.
837     _thread_saved = _thread;
838   }
839   _thread = thread;
840 }
841 
842 int JvmtiVTMTDisabler::VTMT_disable_count() {
843   return _VTMT_disable_count;
844 }
845 
846 int JvmtiVTMTDisabler::VTMT_count() {
847   return _VTMT_count;
848 }
849 
< prev index next >