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