1 /*
  2  * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "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);
147       delete zap;
148     }
149     JvmtiEnvBase::leaving_dying_thread_env_iteration();
150   }
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();
691   }
692 
693   // clearing the flag indicates we are done with the PopFrame() dance
694   clr_pending_step_for_popframe();
695 
696   // If exception was thrown in this frame, need to reset jvmti thread state.
697   // Single stepping may not get enabled correctly by the agent since
698   // exception state is passed in MethodExit event which may be sent at some
699   // time in the future. JDWP agent ignores MethodExit events if caused by
700   // an exception.
701   //
702   if (is_exception_detected()) {
703     clear_exception_state();
704   }
705   // If step is pending for popframe then it may not be
706   // a repeat step. The new_bci and method_id is same as current_bci
707   // and current method_id after pop and step for recursive calls.
708   // Force the step by clearing the last location.
709   JvmtiEnvThreadStateIterator it(this);
710   for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
711     ets->clear_current_location();
712   }
713 }
714 
715 
716 // Class:     JvmtiThreadState
717 // Function:  update_for_pop_top_frame
718 // Description:
719 //   This function removes any frame pop notification request for
720 //   the top frame and invalidates both the current stack depth and
721 //   all cached frameIDs.
722 //
723 // Called by: PopFrame
724 //
725 void JvmtiThreadState::update_for_pop_top_frame() {
726   if (is_interp_only_mode()) {
727     // remove any frame pop notification request for the top frame
728     // in any environment
729     int popframe_number = cur_stack_depth();
730     {
731       JvmtiEnvThreadStateIterator it(this);
732       for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
733         if (ets->is_frame_pop(popframe_number)) {
734           ets->clear_frame_pop(popframe_number);
735         }
736       }
737     }
738     // force stack depth to be recalculated
739     invalidate_cur_stack_depth();
740   } else {
741     assert(!is_enabled(JVMTI_EVENT_FRAME_POP), "Must have no framepops set");
742   }
743 }
744 
745 
746 void JvmtiThreadState::process_pending_step_for_earlyret() {
747   // We are single stepping as the last part of the ForceEarlyReturn
748   // dance so we have some house keeping to do.
749 
750   if (is_earlyret_pending()) {
751     // If the earlyret_state field is not earlyret_inactive, then
752     // we missed all of the earlyret_field cleanup points:
753     //
754     // - remove_activation() was not called
755     //   (did not get suspended in a call_vm() family call and did
756     //   not complete a call_vm() family call on the way here)
757     //
758     // One legitimate way for us to miss all the cleanup points is
759     // if we got here right after handling a compiled return. If that
760     // is the case, then we consider our return from compiled code to
761     // complete the ForceEarlyReturn request and we clear the condition.
762     clr_earlyret_pending();
763     set_earlyret_oop(NULL);
764     clr_earlyret_value();
765   }
766 
767   // clearing the flag indicates we are done with
768   // the ForceEarlyReturn() dance
769   clr_pending_step_for_earlyret();
770 
771   // If exception was thrown in this frame, need to reset jvmti thread state.
772   // Single stepping may not get enabled correctly by the agent since
773   // exception state is passed in MethodExit event which may be sent at some
774   // time in the future. JDWP agent ignores MethodExit events if caused by
775   // an exception.
776   //
777   if (is_exception_detected()) {
778     clear_exception_state();
779   }
780   // If step is pending for earlyret then it may not be a repeat step.
781   // The new_bci and method_id is same as current_bci and current
782   // method_id after earlyret and step for recursive calls.
783   // Force the step by clearing the last location.
784   JvmtiEnvThreadStateIterator it(this);
785   for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
786     ets->clear_current_location();
787   }
788 }
789 
790 void JvmtiThreadState::oops_do(OopClosure* f, CodeBlobClosure* cf) {
791   f->do_oop((oop*) &_earlyret_oop);
792 
793   // Keep nmethods from unloading on the event queue
794   if (_jvmti_event_queue != NULL) {
795     _jvmti_event_queue->oops_do(f, cf);
796   }
797 }
798 
799 void JvmtiThreadState::nmethods_do(CodeBlobClosure* cf) {
800   // Keep nmethods from unloading on the event queue
801   if (_jvmti_event_queue != NULL) {
802     _jvmti_event_queue->nmethods_do(cf);
803   }
804 }
805 
806 // Thread local event queue.
807 void JvmtiThreadState::enqueue_event(JvmtiDeferredEvent* event) {
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