1 /*
  2  * Copyright (c) 2006, 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 #ifndef SHARE_PRIMS_JVMTITHREADSTATE_INLINE_HPP
 26 #define SHARE_PRIMS_JVMTITHREADSTATE_INLINE_HPP
 27 
 28 #include "classfile/javaClasses.hpp"
 29 #include "prims/jvmtiEnvThreadState.hpp"
 30 
 31 #include "prims/jvmtiThreadState.hpp"
 32 #include "runtime/handles.inline.hpp"
 33 #include "runtime/safepointVerifiers.hpp"
 34 #include "runtime/thread.inline.hpp"
 35 
 36 // JvmtiEnvThreadStateIterator implementation
 37 
 38 inline JvmtiEnvThreadStateIterator::JvmtiEnvThreadStateIterator(JvmtiThreadState* thread_state) {
 39   state = thread_state;
 40   Thread::current()->entering_jvmti_env_iteration();
 41 }
 42 
 43 inline JvmtiEnvThreadStateIterator::~JvmtiEnvThreadStateIterator() {
 44   Thread::current()->leaving_jvmti_env_iteration();
 45 }
 46 
 47 inline JvmtiEnvThreadState* JvmtiEnvThreadStateIterator::first() {
 48   return state->head_env_thread_state();
 49 }
 50 
 51 inline JvmtiEnvThreadState* JvmtiEnvThreadStateIterator::next(JvmtiEnvThreadState* ets) {
 52   return ets->next();
 53 }
 54 
 55 // JvmtiThreadState implementation
 56 
 57 JvmtiEnvThreadState* JvmtiThreadState::env_thread_state(JvmtiEnvBase *env) {
 58   JvmtiEnvThreadStateIterator it(this);
 59   for (JvmtiEnvThreadState* ets = it.first(); ets != NULL; ets = it.next(ets)) {
 60     if ((JvmtiEnvBase*)(ets->get_env()) == env) {
 61       return ets;
 62     }
 63   }
 64   return NULL;
 65 }
 66 
 67 JvmtiEnvThreadState* JvmtiThreadState::head_env_thread_state() {
 68   return _head_env_thread_state;
 69 }
 70 
 71 void JvmtiThreadState::set_head_env_thread_state(JvmtiEnvThreadState* ets) {
 72   _head_env_thread_state = ets;
 73 }
 74 
 75 inline JvmtiThreadState* JvmtiThreadState::state_for_while_locked(JavaThread *thread, oop thread_oop) {
 76   assert(JvmtiThreadState_lock->is_locked(), "sanity check");
 77   assert(thread != NULL || thread_oop != NULL, "sanity check");
 78 
 79   NoSafepointVerifier nsv;  // oop is safe to use.
 80 
 81   if (thread_oop == NULL) { // then thread should not be NULL (see assert above)
 82     thread_oop = thread->mounted_vthread() != NULL ? thread->mounted_vthread() : thread->threadObj();
 83   }
 84 
 85   // in a case of unmounted virtual thread the thread can be NULL
 86   JvmtiThreadState *state = thread == NULL ? NULL : thread->jvmti_thread_state();
 87   const char* action = "FOUND";
 88 
 89   if (state == NULL && thread != NULL && thread->is_exiting()) {
 90     // don't add a JvmtiThreadState to a thread that is exiting
 91     return NULL;
 92   }
 93   if (state == NULL || state->get_thread_oop() != thread_oop) {
 94     // check if java_lang_Thread already has a link to the JvmtiThreadState
 95     if (thread_oop != NULL) { // thread_oop can be NULL at early VMStart
 96       state = java_lang_Thread::jvmti_thread_state(thread_oop);
 97     }
 98     if (state == NULL) { // need to create state
 99       state = new JvmtiThreadState(thread, thread_oop);
100       if (thread_oop != NULL) { // thread_oop can be NULL at early VMStart
101         java_lang_Thread::set_jvmti_thread_state(thread_oop, state);
102       }
103       action = "CREATED";
104     }
105   }
106   return state;
107 }
108 
109 inline JvmtiThreadState* JvmtiThreadState::state_for(JavaThread *thread, Handle thread_handle) {
110   // in a case of unmounted virtual thread the thread can be NULL
111   JvmtiThreadState* state = thread_handle == NULL ? thread->jvmti_thread_state() :
112                                                 java_lang_Thread::jvmti_thread_state(thread_handle());
113   if (state == NULL) {
114     MutexLocker mu(JvmtiThreadState_lock);
115     // check again with the lock held
116     state = state_for_while_locked(thread, thread_handle());
117   } else {
118     // Check possible safepoint even if state is non-null.
119     // (Note: the thread argument isn't the current thread)
120     DEBUG_ONLY(JavaThread::current()->check_possible_safepoint());
121   }
122   return state;
123 }
124 
125 inline JavaThread* JvmtiThreadState::get_thread_or_saved() {
126   // Use _thread_saved if cthread is detached from JavaThread (_thread == NULL).
127   return (_thread == NULL && !is_virtual()) ? _thread_saved : _thread;
128 }
129 
130 inline void JvmtiThreadState::set_should_post_on_exceptions(bool val) {
131   get_thread_or_saved()->set_should_post_on_exceptions_flag(val ? JNI_TRUE : JNI_FALSE);
132 }
133 
134 inline void JvmtiThreadState::unbind_from(JavaThread* thread) {
135   if (this == NULL) {
136     return;
137   }
138   // save interp_only_mode
139   _saved_interp_only_mode = thread->get_interp_only_mode();
140   set_thread(NULL); // it is to make sure stale _thread value is never used
141 }
142 
143 inline void JvmtiThreadState::bind_to(JavaThread* thread) {
144   // restore thread interp_only_mode
145   thread->set_interp_only_mode(this == NULL ? 0 : _saved_interp_only_mode);
146 
147   // make continuation to notice the interp_only_mode change
148   Continuation::set_cont_fastpath_thread_state(thread);
149 
150   // bind JavaThread to JvmtiThreadState
151   thread->set_jvmti_thread_state(this);
152 
153   // TBD: This is hacky and may need a better solution.
154   JvmtiEventController::thread_started(thread);
155 
156   if (this != NULL) {
157     // bind to JavaThread
158     set_thread(thread);
159   }
160 }
161 #endif // SHARE_PRIMS_JVMTITHREADSTATE_INLINE_HPP