xref: /aosp_15_r20/art/openjdkjvmti/ti_thread.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /* Copyright (C) 2017 The Android Open Source Project
2*795d594fSAndroid Build Coastguard Worker  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * This file implements interfaces from the file jvmti.h. This implementation
5*795d594fSAndroid Build Coastguard Worker  * is licensed under the same terms as the file jvmti.h.  The
6*795d594fSAndroid Build Coastguard Worker  * copyright and license information for the file jvmti.h follows.
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
9*795d594fSAndroid Build Coastguard Worker  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
10*795d594fSAndroid Build Coastguard Worker  *
11*795d594fSAndroid Build Coastguard Worker  * This code is free software; you can redistribute it and/or modify it
12*795d594fSAndroid Build Coastguard Worker  * under the terms of the GNU General Public License version 2 only, as
13*795d594fSAndroid Build Coastguard Worker  * published by the Free Software Foundation.  Oracle designates this
14*795d594fSAndroid Build Coastguard Worker  * particular file as subject to the "Classpath" exception as provided
15*795d594fSAndroid Build Coastguard Worker  * by Oracle in the LICENSE file that accompanied this code.
16*795d594fSAndroid Build Coastguard Worker  *
17*795d594fSAndroid Build Coastguard Worker  * This code is distributed in the hope that it will be useful, but WITHOUT
18*795d594fSAndroid Build Coastguard Worker  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19*795d594fSAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20*795d594fSAndroid Build Coastguard Worker  * version 2 for more details (a copy is included in the LICENSE file that
21*795d594fSAndroid Build Coastguard Worker  * accompanied this code).
22*795d594fSAndroid Build Coastguard Worker  *
23*795d594fSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License version
24*795d594fSAndroid Build Coastguard Worker  * 2 along with this work; if not, write to the Free Software Foundation,
25*795d594fSAndroid Build Coastguard Worker  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26*795d594fSAndroid Build Coastguard Worker  *
27*795d594fSAndroid Build Coastguard Worker  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
28*795d594fSAndroid Build Coastguard Worker  * or visit www.oracle.com if you need additional information or have any
29*795d594fSAndroid Build Coastguard Worker  * questions.
30*795d594fSAndroid Build Coastguard Worker  */
31*795d594fSAndroid Build Coastguard Worker 
32*795d594fSAndroid Build Coastguard Worker #ifndef ART_OPENJDKJVMTI_TI_THREAD_H_
33*795d594fSAndroid Build Coastguard Worker #define ART_OPENJDKJVMTI_TI_THREAD_H_
34*795d594fSAndroid Build Coastguard Worker 
35*795d594fSAndroid Build Coastguard Worker #include <unordered_map>
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker #include "jni.h"
38*795d594fSAndroid Build Coastguard Worker #include "jvmti.h"
39*795d594fSAndroid Build Coastguard Worker 
40*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
41*795d594fSAndroid Build Coastguard Worker #include "base/mutex.h"
42*795d594fSAndroid Build Coastguard Worker #include "handle.h"
43*795d594fSAndroid Build Coastguard Worker #include "thread.h"
44*795d594fSAndroid Build Coastguard Worker 
45*795d594fSAndroid Build Coastguard Worker namespace art {
46*795d594fSAndroid Build Coastguard Worker class ArtField;
47*795d594fSAndroid Build Coastguard Worker class ScopedObjectAccessAlreadyRunnable;
48*795d594fSAndroid Build Coastguard Worker class Thread;
49*795d594fSAndroid Build Coastguard Worker class Closure;
50*795d594fSAndroid Build Coastguard Worker namespace mirror {
51*795d594fSAndroid Build Coastguard Worker class Throwable;
52*795d594fSAndroid Build Coastguard Worker }  // namespace mirror
53*795d594fSAndroid Build Coastguard Worker }  // namespace art
54*795d594fSAndroid Build Coastguard Worker 
55*795d594fSAndroid Build Coastguard Worker namespace openjdkjvmti {
56*795d594fSAndroid Build Coastguard Worker 
57*795d594fSAndroid Build Coastguard Worker class EventHandler;
58*795d594fSAndroid Build Coastguard Worker 
59*795d594fSAndroid Build Coastguard Worker // Gains the user_code_suspension_lock_ and ensures that the code will not suspend for user-code.
60*795d594fSAndroid Build Coastguard Worker class SCOPED_CAPABILITY ScopedNoUserCodeSuspension {
61*795d594fSAndroid Build Coastguard Worker  public:
62*795d594fSAndroid Build Coastguard Worker   explicit ScopedNoUserCodeSuspension(art::Thread* self)
63*795d594fSAndroid Build Coastguard Worker       ACQUIRE(art::Locks::user_code_suspension_lock_);
64*795d594fSAndroid Build Coastguard Worker   ~ScopedNoUserCodeSuspension() RELEASE(art::Locks::user_code_suspension_lock_);
65*795d594fSAndroid Build Coastguard Worker 
66*795d594fSAndroid Build Coastguard Worker  private:
67*795d594fSAndroid Build Coastguard Worker   art::Thread* self_;
68*795d594fSAndroid Build Coastguard Worker };
69*795d594fSAndroid Build Coastguard Worker 
70*795d594fSAndroid Build Coastguard Worker // The struct that we store in the art::Thread::custom_tls_ that maps the jvmtiEnvs to the data
71*795d594fSAndroid Build Coastguard Worker // stored with that thread. This is needed since different jvmtiEnvs are not supposed to share TLS
72*795d594fSAndroid Build Coastguard Worker // data but we only have a single slot in Thread objects to store data.
73*795d594fSAndroid Build Coastguard Worker struct JvmtiGlobalTLSData : public art::TLSData {
74*795d594fSAndroid Build Coastguard Worker   std::unordered_map<jvmtiEnv*, const void*> data GUARDED_BY(art::Locks::thread_list_lock_);
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker   // The depth of the last frame where popping using PopFrame it is not allowed. It is set to
77*795d594fSAndroid Build Coastguard Worker   // kNoDisallowedPopFrame if all frames can be popped. See b/117615146 for more information.
78*795d594fSAndroid Build Coastguard Worker   static constexpr size_t kNoDisallowedPopFrame = -1;
79*795d594fSAndroid Build Coastguard Worker   size_t disable_pop_frame_depth = kNoDisallowedPopFrame;
80*795d594fSAndroid Build Coastguard Worker };
81*795d594fSAndroid Build Coastguard Worker 
82*795d594fSAndroid Build Coastguard Worker class ThreadUtil {
83*795d594fSAndroid Build Coastguard Worker  public:
84*795d594fSAndroid Build Coastguard Worker   static void Register(EventHandler* event_handler);
85*795d594fSAndroid Build Coastguard Worker   static void Unregister();
86*795d594fSAndroid Build Coastguard Worker 
87*795d594fSAndroid Build Coastguard Worker   // To be called when it is safe to cache data. This means that we have at least entered the
88*795d594fSAndroid Build Coastguard Worker   // RuntimePhase::kInit but we might or might not have already called VMInit event.
89*795d594fSAndroid Build Coastguard Worker   static void CacheData();
90*795d594fSAndroid Build Coastguard Worker 
91*795d594fSAndroid Build Coastguard Worker   // Called just after we have sent the VMInit callback so that ThreadUtil can do final setup. This
92*795d594fSAndroid Build Coastguard Worker   // ensures that there are no timing issues between the two callbacks.
93*795d594fSAndroid Build Coastguard Worker   static void VMInitEventSent() REQUIRES_SHARED(art::Locks::mutator_lock_);
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker   // Handle a jvmtiEnv going away.
96*795d594fSAndroid Build Coastguard Worker   static void RemoveEnvironment(jvmtiEnv* env);
97*795d594fSAndroid Build Coastguard Worker 
98*795d594fSAndroid Build Coastguard Worker   static jvmtiError GetAllThreads(jvmtiEnv* env, jint* threads_count_ptr, jthread** threads_ptr);
99*795d594fSAndroid Build Coastguard Worker 
100*795d594fSAndroid Build Coastguard Worker   static jvmtiError GetCurrentThread(jvmtiEnv* env, jthread* thread_ptr);
101*795d594fSAndroid Build Coastguard Worker 
102*795d594fSAndroid Build Coastguard Worker   static jvmtiError GetThreadInfo(jvmtiEnv* env, jthread thread, jvmtiThreadInfo* info_ptr);
103*795d594fSAndroid Build Coastguard Worker 
104*795d594fSAndroid Build Coastguard Worker   static jvmtiError GetThreadState(jvmtiEnv* env, jthread thread, jint* thread_state_ptr);
105*795d594fSAndroid Build Coastguard Worker 
106*795d594fSAndroid Build Coastguard Worker   static jvmtiError SetThreadLocalStorage(jvmtiEnv* env, jthread thread, const void* data);
107*795d594fSAndroid Build Coastguard Worker   static jvmtiError GetThreadLocalStorage(jvmtiEnv* env, jthread thread, void** data_ptr);
108*795d594fSAndroid Build Coastguard Worker 
109*795d594fSAndroid Build Coastguard Worker   static jvmtiError RunAgentThread(jvmtiEnv* env,
110*795d594fSAndroid Build Coastguard Worker                                    jthread thread,
111*795d594fSAndroid Build Coastguard Worker                                    jvmtiStartFunction proc,
112*795d594fSAndroid Build Coastguard Worker                                    const void* arg,
113*795d594fSAndroid Build Coastguard Worker                                    jint priority);
114*795d594fSAndroid Build Coastguard Worker 
115*795d594fSAndroid Build Coastguard Worker   static jvmtiError SuspendThread(jvmtiEnv* env, jthread thread);
116*795d594fSAndroid Build Coastguard Worker   static jvmtiError ResumeThread(jvmtiEnv* env, jthread thread);
117*795d594fSAndroid Build Coastguard Worker 
118*795d594fSAndroid Build Coastguard Worker   static jvmtiError SuspendThreadList(jvmtiEnv* env,
119*795d594fSAndroid Build Coastguard Worker                                       jint request_count,
120*795d594fSAndroid Build Coastguard Worker                                       const jthread* threads,
121*795d594fSAndroid Build Coastguard Worker                                       jvmtiError* results);
122*795d594fSAndroid Build Coastguard Worker   static jvmtiError ResumeThreadList(jvmtiEnv* env,
123*795d594fSAndroid Build Coastguard Worker                                      jint request_count,
124*795d594fSAndroid Build Coastguard Worker                                      const jthread* threads,
125*795d594fSAndroid Build Coastguard Worker                                      jvmtiError* results);
126*795d594fSAndroid Build Coastguard Worker 
127*795d594fSAndroid Build Coastguard Worker   static jvmtiError StopThread(jvmtiEnv* env, jthread thr, jobject exception);
128*795d594fSAndroid Build Coastguard Worker   static jvmtiError InterruptThread(jvmtiEnv* env, jthread thr);
129*795d594fSAndroid Build Coastguard Worker 
130*795d594fSAndroid Build Coastguard Worker   // Returns true if we decoded the thread and it is alive, false otherwise with an appropriate
131*795d594fSAndroid Build Coastguard Worker   // error placed into 'err'. A thread is alive if it has had it's 'start' function called and has
132*795d594fSAndroid Build Coastguard Worker   // (or at least could have) executed managed code and has not yet returned past it's first managed
133*795d594fSAndroid Build Coastguard Worker   // frame. This means that the thread returned might have IsStillStarting() return true. Code that
134*795d594fSAndroid Build Coastguard Worker   // does not consider that alive should check manually.
135*795d594fSAndroid Build Coastguard Worker   static bool GetAliveNativeThread(jthread thread,
136*795d594fSAndroid Build Coastguard Worker                                    const art::ScopedObjectAccessAlreadyRunnable& soa,
137*795d594fSAndroid Build Coastguard Worker                                    /*out*/ art::Thread** thr,
138*795d594fSAndroid Build Coastguard Worker                                    /*out*/ jvmtiError* err)
139*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(art::Locks::mutator_lock_)
140*795d594fSAndroid Build Coastguard Worker       REQUIRES(art::Locks::thread_list_lock_);
141*795d594fSAndroid Build Coastguard Worker 
142*795d594fSAndroid Build Coastguard Worker   // Returns true if we decoded the thread, false otherwise with an appropriate error placed into
143*795d594fSAndroid Build Coastguard Worker   // 'err'
144*795d594fSAndroid Build Coastguard Worker   static bool GetNativeThread(jthread thread,
145*795d594fSAndroid Build Coastguard Worker                               const art::ScopedObjectAccessAlreadyRunnable& soa,
146*795d594fSAndroid Build Coastguard Worker                               /*out*/ art::Thread** thr,
147*795d594fSAndroid Build Coastguard Worker                               /*out*/ jvmtiError* err)
148*795d594fSAndroid Build Coastguard Worker       REQUIRES_SHARED(art::Locks::mutator_lock_)
149*795d594fSAndroid Build Coastguard Worker       REQUIRES(art::Locks::thread_list_lock_);
150*795d594fSAndroid Build Coastguard Worker 
151*795d594fSAndroid Build Coastguard Worker   // Go to sleep if this thread is suspended.
152*795d594fSAndroid Build Coastguard Worker   static void SuspendCheck(art::Thread* self)
153*795d594fSAndroid Build Coastguard Worker     REQUIRES(!art::Locks::mutator_lock_, !art::Locks::user_code_suspension_lock_);
154*795d594fSAndroid Build Coastguard Worker 
155*795d594fSAndroid Build Coastguard Worker   // Returns true if the thread would be suspended if it locks the mutator-lock or calls
156*795d594fSAndroid Build Coastguard Worker   // SuspendCheck. This function is called with the user_code_suspension_lock already held.
157*795d594fSAndroid Build Coastguard Worker   static bool WouldSuspendForUserCodeLocked(art::Thread* self)
158*795d594fSAndroid Build Coastguard Worker     REQUIRES(art::Locks::user_code_suspension_lock_,
159*795d594fSAndroid Build Coastguard Worker              !art::Locks::thread_suspend_count_lock_);
160*795d594fSAndroid Build Coastguard Worker 
161*795d594fSAndroid Build Coastguard Worker   // Returns true if this thread would go to sleep if it locks the mutator-lock or calls
162*795d594fSAndroid Build Coastguard Worker   // SuspendCheck.
163*795d594fSAndroid Build Coastguard Worker   static bool WouldSuspendForUserCode(art::Thread* self)
164*795d594fSAndroid Build Coastguard Worker     REQUIRES(!art::Locks::user_code_suspension_lock_,
165*795d594fSAndroid Build Coastguard Worker              !art::Locks::thread_suspend_count_lock_);
166*795d594fSAndroid Build Coastguard Worker 
167*795d594fSAndroid Build Coastguard Worker   static JvmtiGlobalTLSData* GetGlobalTLSData(art::Thread* thread)
168*795d594fSAndroid Build Coastguard Worker       REQUIRES(art::Locks::thread_list_lock_);
169*795d594fSAndroid Build Coastguard Worker   static JvmtiGlobalTLSData* GetOrCreateGlobalTLSData(art::Thread* thread)
170*795d594fSAndroid Build Coastguard Worker       REQUIRES(art::Locks::thread_list_lock_);
171*795d594fSAndroid Build Coastguard Worker 
172*795d594fSAndroid Build Coastguard Worker  private:
173*795d594fSAndroid Build Coastguard Worker   // We need to make sure only one thread tries to suspend threads at a time so we can get the
174*795d594fSAndroid Build Coastguard Worker   // 'suspend-only-once' behavior the spec requires. Internally, ART considers suspension to be a
175*795d594fSAndroid Build Coastguard Worker   // counted state, allowing a single thread to be suspended multiple times by different users. This
176*795d594fSAndroid Build Coastguard Worker   // makes mapping into the JVMTI idea of thread suspension difficult. We have decided to split the
177*795d594fSAndroid Build Coastguard Worker   // difference and ensure that JVMTI tries to treat suspension as the boolean flag as much as
178*795d594fSAndroid Build Coastguard Worker   // possible with the suspend/resume methods but only do best effort. On the other hand
179*795d594fSAndroid Build Coastguard Worker   // GetThreadState will be totally accurate as much as possible. This means that calling
180*795d594fSAndroid Build Coastguard Worker   // ResumeThread on a thread that has state JVMTI_THREAD_STATE_SUSPENDED will not necessarily
181*795d594fSAndroid Build Coastguard Worker   // cause the thread to wake up if the thread is suspended for the debugger or gc or something.
182*795d594fSAndroid Build Coastguard Worker   static jvmtiError SuspendSelf(art::Thread* self)
183*795d594fSAndroid Build Coastguard Worker       REQUIRES(!art::Locks::mutator_lock_, !art::Locks::user_code_suspension_lock_);
184*795d594fSAndroid Build Coastguard Worker   static jvmtiError SuspendOther(art::Thread* self, jthread target_jthread)
185*795d594fSAndroid Build Coastguard Worker       REQUIRES(!art::Locks::mutator_lock_, !art::Locks::user_code_suspension_lock_);
186*795d594fSAndroid Build Coastguard Worker 
187*795d594fSAndroid Build Coastguard Worker   static art::ArtField* context_class_loader_;
188*795d594fSAndroid Build Coastguard Worker };
189*795d594fSAndroid Build Coastguard Worker 
190*795d594fSAndroid Build Coastguard Worker }  // namespace openjdkjvmti
191*795d594fSAndroid Build Coastguard Worker 
192*795d594fSAndroid Build Coastguard Worker #endif  // ART_OPENJDKJVMTI_TI_THREAD_H_
193