blob: 6fa73f8a8c27c1353033bf5f93a8898e4c027cb9 [file] [log] [blame]
Andreas Gampeaf13ab92017-01-11 20:57:40 -08001/* 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#include "ti_thread.h"
33
Andreas Gampeeafaf572017-01-20 12:34:15 -080034#include "android-base/strings.h"
Andreas Gampea1d2f952017-04-20 22:53:58 -070035#include "art_field-inl.h"
Andreas Gampeaf13ab92017-01-11 20:57:40 -080036#include "art_jvmti.h"
37#include "base/logging.h"
38#include "base/mutex.h"
Andreas Gampeeafaf572017-01-20 12:34:15 -080039#include "events-inl.h"
Andreas Gampef26bf2d2017-01-13 16:47:14 -080040#include "gc/system_weak.h"
41#include "gc_root-inl.h"
Andreas Gampeaf13ab92017-01-11 20:57:40 -080042#include "jni_internal.h"
43#include "mirror/class.h"
44#include "mirror/object-inl.h"
45#include "mirror/string.h"
Steven Morelande431e272017-07-18 16:53:49 -070046#include "nativehelper/ScopedLocalRef.h"
Andreas Gampeaf13ab92017-01-11 20:57:40 -080047#include "obj_ptr.h"
Andreas Gampef26bf2d2017-01-13 16:47:14 -080048#include "runtime.h"
Andreas Gampeeafaf572017-01-20 12:34:15 -080049#include "runtime_callbacks.h"
Andreas Gampeaf13ab92017-01-11 20:57:40 -080050#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070051#include "thread-current-inl.h"
Andreas Gampe85807442017-01-13 14:40:58 -080052#include "thread_list.h"
Steven Morelande431e272017-07-18 16:53:49 -070053#include "ti_phase.h"
Andreas Gampeaf13ab92017-01-11 20:57:40 -080054#include "well_known_classes.h"
55
56namespace openjdkjvmti {
57
Andreas Gampedb6c2ab2017-03-28 17:28:32 -070058art::ArtField* ThreadUtil::context_class_loader_ = nullptr;
59
Andreas Gampeeafaf572017-01-20 12:34:15 -080060struct ThreadCallback : public art::ThreadLifecycleCallback, public art::RuntimePhaseCallback {
61 jthread GetThreadObject(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_) {
62 if (self->GetPeer() == nullptr) {
63 return nullptr;
64 }
65 return self->GetJniEnv()->AddLocalReference<jthread>(self->GetPeer());
66 }
Andreas Gampe983c1752017-01-23 19:46:56 -080067 template <ArtJvmtiEvent kEvent>
68 void Post(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampeeafaf572017-01-20 12:34:15 -080069 DCHECK_EQ(self, art::Thread::Current());
70 ScopedLocalRef<jthread> thread(self->GetJniEnv(), GetThreadObject(self));
Andreas Gampee6377462017-01-20 17:37:50 -080071 art::ScopedThreadSuspension sts(self, art::ThreadState::kNative);
Andreas Gampe983c1752017-01-23 19:46:56 -080072 event_handler->DispatchEvent<kEvent>(self,
73 reinterpret_cast<JNIEnv*>(self->GetJniEnv()),
74 thread.get());
Andreas Gampeeafaf572017-01-20 12:34:15 -080075 }
76
77 void ThreadStart(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
78 if (!started) {
79 // Runtime isn't started. We only expect at most the signal handler or JIT threads to be
80 // started here.
81 if (art::kIsDebugBuild) {
82 std::string name;
83 self->GetThreadName(name);
Alex Light5bd09542017-02-09 16:01:32 -080084 if (name != "JDWP" &&
85 name != "Signal Catcher" &&
86 !android::base::StartsWith(name, "Jit thread pool")) {
Andreas Gampeeafaf572017-01-20 12:34:15 -080087 LOG(FATAL) << "Unexpected thread before start: " << name;
88 }
89 }
90 return;
91 }
Andreas Gampe983c1752017-01-23 19:46:56 -080092 Post<ArtJvmtiEvent::kThreadStart>(self);
Andreas Gampeeafaf572017-01-20 12:34:15 -080093 }
94
95 void ThreadDeath(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampe983c1752017-01-23 19:46:56 -080096 Post<ArtJvmtiEvent::kThreadEnd>(self);
Andreas Gampeeafaf572017-01-20 12:34:15 -080097 }
98
99 void NextRuntimePhase(RuntimePhase phase) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
100 if (phase == RuntimePhase::kInit) {
101 // We moved to VMInit. Report the main thread as started (it was attached early, and must
102 // not be reported until Init.
103 started = true;
Andreas Gampe983c1752017-01-23 19:46:56 -0800104 Post<ArtJvmtiEvent::kThreadStart>(art::Thread::Current());
Andreas Gampeeafaf572017-01-20 12:34:15 -0800105 }
106 }
107
108 EventHandler* event_handler = nullptr;
109 bool started = false;
110};
111
112ThreadCallback gThreadCallback;
113
114void ThreadUtil::Register(EventHandler* handler) {
115 art::Runtime* runtime = art::Runtime::Current();
116
117 gThreadCallback.started = runtime->IsStarted();
118 gThreadCallback.event_handler = handler;
119
120 art::ScopedThreadStateChange stsc(art::Thread::Current(),
121 art::ThreadState::kWaitingForDebuggerToAttach);
122 art::ScopedSuspendAll ssa("Add thread callback");
123 runtime->GetRuntimeCallbacks()->AddThreadLifecycleCallback(&gThreadCallback);
124 runtime->GetRuntimeCallbacks()->AddRuntimePhaseCallback(&gThreadCallback);
125}
126
Andreas Gampedb6c2ab2017-03-28 17:28:32 -0700127void ThreadUtil::CacheData() {
128 art::ScopedObjectAccess soa(art::Thread::Current());
129 art::ObjPtr<art::mirror::Class> thread_class =
130 soa.Decode<art::mirror::Class>(art::WellKnownClasses::java_lang_Thread);
131 CHECK(thread_class != nullptr);
132 context_class_loader_ = thread_class->FindDeclaredInstanceField("contextClassLoader",
133 "Ljava/lang/ClassLoader;");
134 CHECK(context_class_loader_ != nullptr);
135}
136
Andreas Gampeeafaf572017-01-20 12:34:15 -0800137void ThreadUtil::Unregister() {
138 art::ScopedThreadStateChange stsc(art::Thread::Current(),
139 art::ThreadState::kWaitingForDebuggerToAttach);
140 art::ScopedSuspendAll ssa("Remove thread callback");
141 art::Runtime* runtime = art::Runtime::Current();
142 runtime->GetRuntimeCallbacks()->RemoveThreadLifecycleCallback(&gThreadCallback);
143 runtime->GetRuntimeCallbacks()->RemoveRuntimePhaseCallback(&gThreadCallback);
144}
145
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800146jvmtiError ThreadUtil::GetCurrentThread(jvmtiEnv* env ATTRIBUTE_UNUSED, jthread* thread_ptr) {
147 art::Thread* self = art::Thread::Current();
148
149 art::ScopedObjectAccess soa(self);
150
151 jthread thread_peer;
152 if (self->IsStillStarting()) {
153 thread_peer = nullptr;
154 } else {
155 thread_peer = soa.AddLocalReference<jthread>(self->GetPeer());
156 }
157
158 *thread_ptr = thread_peer;
159 return ERR(NONE);
160}
161
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800162// Get the native thread. The spec says a null object denotes the current thread.
Alex Lightbebd7bd2017-07-25 14:05:52 -0700163art::Thread* ThreadUtil::GetNativeThread(jthread thread,
164 const art::ScopedObjectAccessAlreadyRunnable& soa) {
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800165 if (thread == nullptr) {
166 return art::Thread::Current();
167 }
168
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800169 return art::Thread::FromManagedThread(soa, thread);
170}
171
172jvmtiError ThreadUtil::GetThreadInfo(jvmtiEnv* env, jthread thread, jvmtiThreadInfo* info_ptr) {
173 if (info_ptr == nullptr) {
174 return ERR(NULL_POINTER);
175 }
Andreas Gampedb6c2ab2017-03-28 17:28:32 -0700176 if (!PhaseUtil::IsLivePhase()) {
177 return JVMTI_ERROR_WRONG_PHASE;
178 }
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800179
Alex Light3ae82532017-07-26 13:59:07 -0700180 art::Thread* self = art::Thread::Current();
181 art::ScopedObjectAccess soa(self);
182 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800183
Alex Light3ae82532017-07-26 13:59:07 -0700184 art::Thread* target = GetNativeThread(thread, soa);
185 if (target == nullptr && thread == nullptr) {
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800186 return ERR(INVALID_THREAD);
187 }
188
Andreas Gampe54711412017-02-21 12:41:43 -0800189 JvmtiUniquePtr<char[]> name_uptr;
Alex Light3ae82532017-07-26 13:59:07 -0700190 if (target != nullptr) {
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800191 // Have a native thread object, this thread is alive.
192 std::string name;
Alex Light3ae82532017-07-26 13:59:07 -0700193 target->GetThreadName(name);
Andreas Gampe54711412017-02-21 12:41:43 -0800194 jvmtiError name_result;
195 name_uptr = CopyString(env, name.c_str(), &name_result);
196 if (name_uptr == nullptr) {
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800197 return name_result;
198 }
Andreas Gampe54711412017-02-21 12:41:43 -0800199 info_ptr->name = name_uptr.get();
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800200
Alex Light3ae82532017-07-26 13:59:07 -0700201 info_ptr->priority = target->GetNativePriority();
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800202
Alex Light3ae82532017-07-26 13:59:07 -0700203 info_ptr->is_daemon = target->IsDaemon();
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800204
Alex Light3ae82532017-07-26 13:59:07 -0700205 art::ObjPtr<art::mirror::Object> peer = target->GetPeerFromOtherThread();
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800206
207 // ThreadGroup.
208 if (peer != nullptr) {
209 art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_group);
210 CHECK(f != nullptr);
211 art::ObjPtr<art::mirror::Object> group = f->GetObject(peer);
212 info_ptr->thread_group = group == nullptr
213 ? nullptr
214 : soa.AddLocalReference<jthreadGroup>(group);
215 } else {
216 info_ptr->thread_group = nullptr;
217 }
218
219 // Context classloader.
Andreas Gampedb6c2ab2017-03-28 17:28:32 -0700220 DCHECK(context_class_loader_ != nullptr);
221 art::ObjPtr<art::mirror::Object> ccl = peer != nullptr
222 ? context_class_loader_->GetObject(peer)
223 : nullptr;
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800224 info_ptr->context_class_loader = ccl == nullptr
225 ? nullptr
226 : soa.AddLocalReference<jobject>(ccl);
227 } else {
228 // Only the peer. This thread has either not been started, or is dead. Read things from
229 // the Java side.
230 art::ObjPtr<art::mirror::Object> peer = soa.Decode<art::mirror::Object>(thread);
231
232 // Name.
233 {
234 art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_name);
235 CHECK(f != nullptr);
236 art::ObjPtr<art::mirror::Object> name = f->GetObject(peer);
237 std::string name_cpp;
238 const char* name_cstr;
239 if (name != nullptr) {
240 name_cpp = name->AsString()->ToModifiedUtf8();
241 name_cstr = name_cpp.c_str();
242 } else {
243 name_cstr = "";
244 }
Andreas Gampe54711412017-02-21 12:41:43 -0800245 jvmtiError name_result;
246 name_uptr = CopyString(env, name_cstr, &name_result);
247 if (name_uptr == nullptr) {
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800248 return name_result;
249 }
Andreas Gampe54711412017-02-21 12:41:43 -0800250 info_ptr->name = name_uptr.get();
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800251 }
252
253 // Priority.
254 {
255 art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_priority);
256 CHECK(f != nullptr);
257 info_ptr->priority = static_cast<jint>(f->GetInt(peer));
258 }
259
260 // Daemon.
261 {
262 art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_daemon);
263 CHECK(f != nullptr);
264 info_ptr->is_daemon = f->GetBoolean(peer) == 0 ? JNI_FALSE : JNI_TRUE;
265 }
266
267 // ThreadGroup.
268 {
269 art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_group);
270 CHECK(f != nullptr);
271 art::ObjPtr<art::mirror::Object> group = f->GetObject(peer);
272 info_ptr->thread_group = group == nullptr
273 ? nullptr
274 : soa.AddLocalReference<jthreadGroup>(group);
275 }
276
277 // Context classloader.
Andreas Gampedb6c2ab2017-03-28 17:28:32 -0700278 DCHECK(context_class_loader_ != nullptr);
279 art::ObjPtr<art::mirror::Object> ccl = peer != nullptr
280 ? context_class_loader_->GetObject(peer)
281 : nullptr;
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800282 info_ptr->context_class_loader = ccl == nullptr
283 ? nullptr
284 : soa.AddLocalReference<jobject>(ccl);
285 }
286
287 name_uptr.release();
288
289 return ERR(NONE);
290}
291
Alex Light1f0a22f2017-07-17 12:55:59 -0700292struct InternalThreadState {
293 art::Thread* native_thread;
294 art::ThreadState art_state;
295 int thread_user_code_suspend_count;
296};
297
298// Return the thread's (or current thread, if null) thread state.
299static InternalThreadState GetNativeThreadState(jthread thread,
300 const art::ScopedObjectAccessAlreadyRunnable& soa)
301 REQUIRES_SHARED(art::Locks::mutator_lock_)
Alex Light3ae82532017-07-26 13:59:07 -0700302 REQUIRES(art::Locks::thread_list_lock_, art::Locks::user_code_suspension_lock_) {
Andreas Gampe72c19832017-01-12 13:22:16 -0800303 art::Thread* self = nullptr;
Andreas Gampe72c19832017-01-12 13:22:16 -0800304 if (thread == nullptr) {
305 self = art::Thread::Current();
306 } else {
307 self = art::Thread::FromManagedThread(soa, thread);
308 }
Alex Light1f0a22f2017-07-17 12:55:59 -0700309 InternalThreadState thread_state = {};
310 art::MutexLock tscl_mu(soa.Self(), *art::Locks::thread_suspend_count_lock_);
311 thread_state.native_thread = self;
Andreas Gampe72c19832017-01-12 13:22:16 -0800312 if (self == nullptr || self->IsStillStarting()) {
Alex Light1f0a22f2017-07-17 12:55:59 -0700313 thread_state.art_state = art::ThreadState::kStarting;
314 thread_state.thread_user_code_suspend_count = 0;
315 } else {
316 thread_state.art_state = self->GetState();
317 thread_state.thread_user_code_suspend_count = self->GetUserCodeSuspendCount();
Andreas Gampe72c19832017-01-12 13:22:16 -0800318 }
Alex Light1f0a22f2017-07-17 12:55:59 -0700319 return thread_state;
Andreas Gampe72c19832017-01-12 13:22:16 -0800320}
321
Alex Light1f0a22f2017-07-17 12:55:59 -0700322static jint GetJvmtiThreadStateFromInternal(const InternalThreadState& state) {
323 art::ThreadState internal_thread_state = state.art_state;
Andreas Gampe72c19832017-01-12 13:22:16 -0800324 jint jvmti_state = JVMTI_THREAD_STATE_ALIVE;
325
Alex Light1f0a22f2017-07-17 12:55:59 -0700326 if (state.thread_user_code_suspend_count != 0) {
Andreas Gampe72c19832017-01-12 13:22:16 -0800327 jvmti_state |= JVMTI_THREAD_STATE_SUSPENDED;
328 // Note: We do not have data about the previous state. Otherwise we should load the previous
329 // state here.
330 }
331
Alex Light1f0a22f2017-07-17 12:55:59 -0700332 if (state.native_thread->IsInterrupted()) {
333 jvmti_state |= JVMTI_THREAD_STATE_INTERRUPTED;
334 }
335
Andreas Gampe72c19832017-01-12 13:22:16 -0800336 if (internal_thread_state == art::ThreadState::kNative) {
337 jvmti_state |= JVMTI_THREAD_STATE_IN_NATIVE;
338 }
339
340 if (internal_thread_state == art::ThreadState::kRunnable ||
341 internal_thread_state == art::ThreadState::kWaitingWeakGcRootRead ||
342 internal_thread_state == art::ThreadState::kSuspended) {
343 jvmti_state |= JVMTI_THREAD_STATE_RUNNABLE;
344 } else if (internal_thread_state == art::ThreadState::kBlocked) {
345 jvmti_state |= JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER;
346 } else {
347 // Should be in waiting state.
348 jvmti_state |= JVMTI_THREAD_STATE_WAITING;
349
350 if (internal_thread_state == art::ThreadState::kTimedWaiting ||
351 internal_thread_state == art::ThreadState::kSleeping) {
352 jvmti_state |= JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT;
353 } else {
354 jvmti_state |= JVMTI_THREAD_STATE_WAITING_INDEFINITELY;
355 }
356
357 if (internal_thread_state == art::ThreadState::kSleeping) {
358 jvmti_state |= JVMTI_THREAD_STATE_SLEEPING;
359 }
360
361 if (internal_thread_state == art::ThreadState::kTimedWaiting ||
362 internal_thread_state == art::ThreadState::kWaiting) {
363 jvmti_state |= JVMTI_THREAD_STATE_IN_OBJECT_WAIT;
364 }
365
366 // TODO: PARKED. We'll have to inspect the stack.
367 }
368
369 return jvmti_state;
370}
371
Alex Light1f0a22f2017-07-17 12:55:59 -0700372static jint GetJavaStateFromInternal(const InternalThreadState& state) {
373 switch (state.art_state) {
Andreas Gampe72c19832017-01-12 13:22:16 -0800374 case art::ThreadState::kTerminated:
375 return JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED;
376
377 case art::ThreadState::kRunnable:
378 case art::ThreadState::kNative:
379 case art::ThreadState::kWaitingWeakGcRootRead:
380 case art::ThreadState::kSuspended:
381 return JVMTI_JAVA_LANG_THREAD_STATE_RUNNABLE;
382
383 case art::ThreadState::kTimedWaiting:
384 case art::ThreadState::kSleeping:
385 return JVMTI_JAVA_LANG_THREAD_STATE_TIMED_WAITING;
386
387 case art::ThreadState::kBlocked:
388 return JVMTI_JAVA_LANG_THREAD_STATE_BLOCKED;
389
390 case art::ThreadState::kStarting:
391 return JVMTI_JAVA_LANG_THREAD_STATE_NEW;
392
393 case art::ThreadState::kWaiting:
394 case art::ThreadState::kWaitingForGcToComplete:
395 case art::ThreadState::kWaitingPerformingGc:
396 case art::ThreadState::kWaitingForCheckPointsToRun:
397 case art::ThreadState::kWaitingForDebuggerSend:
398 case art::ThreadState::kWaitingForDebuggerToAttach:
399 case art::ThreadState::kWaitingInMainDebuggerLoop:
400 case art::ThreadState::kWaitingForDebuggerSuspension:
401 case art::ThreadState::kWaitingForDeoptimization:
402 case art::ThreadState::kWaitingForGetObjectsAllocated:
403 case art::ThreadState::kWaitingForJniOnLoad:
404 case art::ThreadState::kWaitingForSignalCatcherOutput:
405 case art::ThreadState::kWaitingInMainSignalCatcherLoop:
406 case art::ThreadState::kWaitingForMethodTracingStart:
407 case art::ThreadState::kWaitingForVisitObjects:
408 case art::ThreadState::kWaitingForGcThreadFlip:
409 return JVMTI_JAVA_LANG_THREAD_STATE_WAITING;
410 }
411 LOG(FATAL) << "Unreachable";
412 UNREACHABLE();
413}
414
Alex Light1f0a22f2017-07-17 12:55:59 -0700415// Suspends the current thread if it has any suspend requests on it.
416static void SuspendCheck(art::Thread* self)
417 REQUIRES(!art::Locks::mutator_lock_, !art::Locks::user_code_suspension_lock_) {
418 art::ScopedObjectAccess soa(self);
419 // Really this is only needed if we are in FastJNI and actually have the mutator_lock_ already.
420 self->FullSuspendCheck();
421}
422
Andreas Gampe72c19832017-01-12 13:22:16 -0800423jvmtiError ThreadUtil::GetThreadState(jvmtiEnv* env ATTRIBUTE_UNUSED,
424 jthread thread,
425 jint* thread_state_ptr) {
426 if (thread_state_ptr == nullptr) {
427 return ERR(NULL_POINTER);
428 }
429
Alex Light1f0a22f2017-07-17 12:55:59 -0700430 art::Thread* self = art::Thread::Current();
431 InternalThreadState state = {};
432 // Loop since we need to bail out and try again if we would end up getting suspended while holding
433 // the user_code_suspension_lock_ due to a SuspendReason::kForUserCode. In this situation we
434 // release the lock, wait to get resumed and try again.
435 do {
436 SuspendCheck(self);
437 art::MutexLock ucsl_mu(self, *art::Locks::user_code_suspension_lock_);
438 {
439 art::MutexLock tscl_mu(self, *art::Locks::thread_suspend_count_lock_);
440 if (self->GetUserCodeSuspendCount() != 0) {
441 // Make sure we won't be suspended in the middle of holding the thread_suspend_count_lock_
442 // by a user-code suspension. We retry and do another SuspendCheck to clear this.
443 continue;
444 }
445 }
446 art::ScopedObjectAccess soa(self);
Alex Light3ae82532017-07-26 13:59:07 -0700447 art::MutexLock tll_mu(self, *art::Locks::thread_list_lock_);
Alex Light1f0a22f2017-07-17 12:55:59 -0700448 state = GetNativeThreadState(thread, soa);
Alex Light3ae82532017-07-26 13:59:07 -0700449 if (state.art_state == art::ThreadState::kStarting) {
450 break;
451 }
452 DCHECK(state.native_thread != nullptr);
453
454 // Translate internal thread state to JVMTI and Java state.
455 jint jvmti_state = GetJvmtiThreadStateFromInternal(state);
456
457 // Java state is derived from nativeGetState.
458 // TODO: Our implementation assigns "runnable" to suspended. As such, we will have slightly
459 // different mask if a thread got suspended due to user-code. However, this is for
460 // consistency with the Java view.
461 jint java_state = GetJavaStateFromInternal(state);
462
463 *thread_state_ptr = jvmti_state | java_state;
464
465 return ERR(NONE);
Alex Light1f0a22f2017-07-17 12:55:59 -0700466 } while (true);
Andreas Gampe72c19832017-01-12 13:22:16 -0800467
Alex Light3ae82532017-07-26 13:59:07 -0700468 DCHECK_EQ(state.art_state, art::ThreadState::kStarting);
Andreas Gampe72c19832017-01-12 13:22:16 -0800469
Alex Light3ae82532017-07-26 13:59:07 -0700470 if (thread == nullptr) {
471 // No native thread, and no Java thread? We must be starting up. Report as wrong phase.
472 return ERR(WRONG_PHASE);
Andreas Gampe72c19832017-01-12 13:22:16 -0800473 }
Andreas Gampe72c19832017-01-12 13:22:16 -0800474
Alex Light3ae82532017-07-26 13:59:07 -0700475 art::ScopedObjectAccess soa(self);
Andreas Gampe72c19832017-01-12 13:22:16 -0800476
Alex Light3ae82532017-07-26 13:59:07 -0700477 // Need to read the Java "started" field to know whether this is starting or terminated.
478 art::ObjPtr<art::mirror::Object> peer = soa.Decode<art::mirror::Object>(thread);
479 art::ObjPtr<art::mirror::Class> klass = peer->GetClass();
480 art::ArtField* started_field = klass->FindDeclaredInstanceField("started", "Z");
481 CHECK(started_field != nullptr);
482 bool started = started_field->GetBoolean(peer) != 0;
483 constexpr jint kStartedState = JVMTI_JAVA_LANG_THREAD_STATE_NEW;
484 constexpr jint kTerminatedState = JVMTI_THREAD_STATE_TERMINATED |
485 JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED;
486 *thread_state_ptr = started ? kTerminatedState : kStartedState;
Andreas Gampe72c19832017-01-12 13:22:16 -0800487 return ERR(NONE);
488}
489
Andreas Gampe85807442017-01-13 14:40:58 -0800490jvmtiError ThreadUtil::GetAllThreads(jvmtiEnv* env,
491 jint* threads_count_ptr,
492 jthread** threads_ptr) {
493 if (threads_count_ptr == nullptr || threads_ptr == nullptr) {
494 return ERR(NULL_POINTER);
495 }
496
497 art::Thread* current = art::Thread::Current();
498
499 art::ScopedObjectAccess soa(current);
500
501 art::MutexLock mu(current, *art::Locks::thread_list_lock_);
502 std::list<art::Thread*> thread_list = art::Runtime::Current()->GetThreadList()->GetList();
503
504 std::vector<art::ObjPtr<art::mirror::Object>> peers;
505
506 for (art::Thread* thread : thread_list) {
507 // Skip threads that are still starting.
508 if (thread->IsStillStarting()) {
509 continue;
510 }
511
Andreas Gampe202f85a2017-02-06 10:23:26 -0800512 art::ObjPtr<art::mirror::Object> peer = thread->GetPeerFromOtherThread();
Andreas Gampe85807442017-01-13 14:40:58 -0800513 if (peer != nullptr) {
514 peers.push_back(peer);
515 }
516 }
517
518 if (peers.empty()) {
519 *threads_count_ptr = 0;
520 *threads_ptr = nullptr;
521 } else {
522 unsigned char* data;
523 jvmtiError data_result = env->Allocate(peers.size() * sizeof(jthread), &data);
524 if (data_result != ERR(NONE)) {
525 return data_result;
526 }
527 jthread* threads = reinterpret_cast<jthread*>(data);
528 for (size_t i = 0; i != peers.size(); ++i) {
529 threads[i] = soa.AddLocalReference<jthread>(peers[i]);
530 }
531
532 *threads_count_ptr = static_cast<jint>(peers.size());
533 *threads_ptr = threads;
534 }
Andreas Gampef26bf2d2017-01-13 16:47:14 -0800535 return ERR(NONE);
536}
Andreas Gampe85807442017-01-13 14:40:58 -0800537
Alex Light092a4042017-07-12 08:46:44 -0700538// The struct that we store in the art::Thread::custom_tls_ that maps the jvmtiEnvs to the data
539// stored with that thread. This is needed since different jvmtiEnvs are not supposed to share TLS
540// data but we only have a single slot in Thread objects to store data.
541struct JvmtiGlobalTLSData {
542 std::unordered_map<jvmtiEnv*, const void*> data GUARDED_BY(art::Locks::thread_list_lock_);
543};
544
545static void RemoveTLSData(art::Thread* target, void* ctx) REQUIRES(art::Locks::thread_list_lock_) {
546 jvmtiEnv* env = reinterpret_cast<jvmtiEnv*>(ctx);
547 art::Locks::thread_list_lock_->AssertHeld(art::Thread::Current());
548 JvmtiGlobalTLSData* global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS());
549 if (global_tls != nullptr) {
550 global_tls->data.erase(env);
551 }
552}
553
554void ThreadUtil::RemoveEnvironment(jvmtiEnv* env) {
555 art::Thread* self = art::Thread::Current();
556 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
557 art::ThreadList* list = art::Runtime::Current()->GetThreadList();
558 list->ForEach(RemoveTLSData, env);
559}
560
561jvmtiError ThreadUtil::SetThreadLocalStorage(jvmtiEnv* env, jthread thread, const void* data) {
562 art::Thread* self = art::Thread::Current();
563 art::ScopedObjectAccess soa(self);
564 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
Alex Light3ae82532017-07-26 13:59:07 -0700565 art::Thread* target = GetNativeThread(thread, soa);
Alex Light092a4042017-07-12 08:46:44 -0700566 if (target == nullptr && thread == nullptr) {
Andreas Gampef26bf2d2017-01-13 16:47:14 -0800567 return ERR(INVALID_THREAD);
568 }
Alex Light092a4042017-07-12 08:46:44 -0700569 if (target == nullptr) {
Andreas Gampef26bf2d2017-01-13 16:47:14 -0800570 return ERR(THREAD_NOT_ALIVE);
571 }
572
Alex Light092a4042017-07-12 08:46:44 -0700573 JvmtiGlobalTLSData* global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS());
574 if (global_tls == nullptr) {
575 target->SetCustomTLS(new JvmtiGlobalTLSData);
576 global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS());
577 }
578
579 global_tls->data[env] = data;
Andreas Gampef26bf2d2017-01-13 16:47:14 -0800580
581 return ERR(NONE);
582}
583
Alex Light092a4042017-07-12 08:46:44 -0700584jvmtiError ThreadUtil::GetThreadLocalStorage(jvmtiEnv* env,
Andreas Gampef26bf2d2017-01-13 16:47:14 -0800585 jthread thread,
586 void** data_ptr) {
587 if (data_ptr == nullptr) {
588 return ERR(NULL_POINTER);
589 }
590
Alex Light092a4042017-07-12 08:46:44 -0700591 art::Thread* self = art::Thread::Current();
592 art::ScopedObjectAccess soa(self);
593 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
Alex Light3ae82532017-07-26 13:59:07 -0700594 art::Thread* target = GetNativeThread(thread, soa);
Alex Light092a4042017-07-12 08:46:44 -0700595 if (target == nullptr && thread == nullptr) {
Andreas Gampef26bf2d2017-01-13 16:47:14 -0800596 return ERR(INVALID_THREAD);
597 }
Alex Light092a4042017-07-12 08:46:44 -0700598 if (target == nullptr) {
Andreas Gampef26bf2d2017-01-13 16:47:14 -0800599 return ERR(THREAD_NOT_ALIVE);
600 }
601
Alex Light092a4042017-07-12 08:46:44 -0700602 JvmtiGlobalTLSData* global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS());
603 if (global_tls == nullptr) {
604 *data_ptr = nullptr;
605 return OK;
606 }
607 auto it = global_tls->data.find(env);
608 if (it != global_tls->data.end()) {
609 *data_ptr = const_cast<void*>(it->second);
610 } else {
611 *data_ptr = nullptr;
612 }
613
Andreas Gampe85807442017-01-13 14:40:58 -0800614 return ERR(NONE);
615}
616
Andreas Gampe732b0ac2017-01-18 15:23:39 -0800617struct AgentData {
618 const void* arg;
619 jvmtiStartFunction proc;
620 jthread thread;
621 JavaVM* java_vm;
622 jvmtiEnv* jvmti_env;
623 jint priority;
624};
625
626static void* AgentCallback(void* arg) {
627 std::unique_ptr<AgentData> data(reinterpret_cast<AgentData*>(arg));
628 CHECK(data->thread != nullptr);
629
630 // We already have a peer. So call our special Attach function.
631 art::Thread* self = art::Thread::Attach("JVMTI Agent thread", true, data->thread);
632 CHECK(self != nullptr);
633 // The name in Attach() is only for logging. Set the thread name. This is important so
634 // that the thread is no longer seen as starting up.
635 {
636 art::ScopedObjectAccess soa(self);
637 self->SetThreadName("JVMTI Agent thread");
638 }
639
640 // Release the peer.
641 JNIEnv* env = self->GetJniEnv();
642 env->DeleteGlobalRef(data->thread);
643 data->thread = nullptr;
644
645 // Run the agent code.
646 data->proc(data->jvmti_env, env, const_cast<void*>(data->arg));
647
648 // Detach the thread.
649 int detach_result = data->java_vm->DetachCurrentThread();
650 CHECK_EQ(detach_result, 0);
651
652 return nullptr;
653}
654
655jvmtiError ThreadUtil::RunAgentThread(jvmtiEnv* jvmti_env,
656 jthread thread,
657 jvmtiStartFunction proc,
658 const void* arg,
659 jint priority) {
660 if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
661 return ERR(INVALID_PRIORITY);
662 }
663 JNIEnv* env = art::Thread::Current()->GetJniEnv();
664 if (thread == nullptr || !env->IsInstanceOf(thread, art::WellKnownClasses::java_lang_Thread)) {
665 return ERR(INVALID_THREAD);
666 }
667 if (proc == nullptr) {
668 return ERR(NULL_POINTER);
669 }
670
671 std::unique_ptr<AgentData> data(new AgentData);
672 data->arg = arg;
673 data->proc = proc;
674 // We need a global ref for Java objects, as local refs will be invalid.
675 data->thread = env->NewGlobalRef(thread);
676 data->java_vm = art::Runtime::Current()->GetJavaVM();
677 data->jvmti_env = jvmti_env;
678 data->priority = priority;
679
680 pthread_t pthread;
681 int pthread_create_result = pthread_create(&pthread,
682 nullptr,
683 &AgentCallback,
684 reinterpret_cast<void*>(data.get()));
685 if (pthread_create_result != 0) {
686 return ERR(INTERNAL);
687 }
688 data.release();
689
690 return ERR(NONE);
691}
692
Alex Light88fd7202017-06-30 08:31:59 -0700693jvmtiError ThreadUtil::SuspendOther(art::Thread* self,
Alex Light3ae82532017-07-26 13:59:07 -0700694 jthread target_jthread) {
Alex Light88fd7202017-06-30 08:31:59 -0700695 // Loop since we need to bail out and try again if we would end up getting suspended while holding
696 // the user_code_suspension_lock_ due to a SuspendReason::kForUserCode. In this situation we
697 // release the lock, wait to get resumed and try again.
698 do {
699 // Suspend ourself if we have any outstanding suspends. This is so we won't suspend due to
700 // another SuspendThread in the middle of suspending something else potentially causing a
701 // deadlock. We need to do this in the loop because if we ended up back here then we had
702 // outstanding SuspendReason::kForUserCode suspensions and we should wait for them to be cleared
703 // before continuing.
704 SuspendCheck(self);
705 art::MutexLock mu(self, *art::Locks::user_code_suspension_lock_);
706 {
Alex Light3ae82532017-07-26 13:59:07 -0700707 art::MutexLock thread_suspend_count_mu(self, *art::Locks::thread_suspend_count_lock_);
Alex Light88fd7202017-06-30 08:31:59 -0700708 // Make sure we won't be suspended in the middle of holding the thread_suspend_count_lock_ by
709 // a user-code suspension. We retry and do another SuspendCheck to clear this.
710 if (self->GetUserCodeSuspendCount() != 0) {
711 continue;
Alex Light88fd7202017-06-30 08:31:59 -0700712 }
Alex Light3ae82532017-07-26 13:59:07 -0700713 // We are not going to be suspended by user code from now on.
Alex Light88fd7202017-06-30 08:31:59 -0700714 }
Alex Light3ae82532017-07-26 13:59:07 -0700715 {
716 art::ScopedObjectAccess soa(self);
717 art::MutexLock thread_list_mu(self, *art::Locks::thread_list_lock_);
718 art::Thread* target = GetNativeThread(target_jthread, soa);
Alex Light88fd7202017-06-30 08:31:59 -0700719 art::ThreadState state = target->GetState();
720 if (state == art::ThreadState::kTerminated || state == art::ThreadState::kStarting) {
721 return ERR(THREAD_NOT_ALIVE);
Alex Light3ae82532017-07-26 13:59:07 -0700722 } else {
723 art::MutexLock thread_suspend_count_mu(self, *art::Locks::thread_suspend_count_lock_);
724 if (target->GetUserCodeSuspendCount() != 0) {
725 return ERR(THREAD_SUSPENDED);
726 }
Alex Light88fd7202017-06-30 08:31:59 -0700727 }
728 }
Alex Light3ae82532017-07-26 13:59:07 -0700729 bool timeout = true;
730 art::Thread* ret_target = art::Runtime::Current()->GetThreadList()->SuspendThreadByPeer(
731 target_jthread,
732 /* request_suspension */ true,
733 art::SuspendReason::kForUserCode,
734 &timeout);
735 if (ret_target == nullptr && !timeout) {
736 // TODO It would be good to get more information about why exactly the thread failed to
737 // suspend.
738 return ERR(INTERNAL);
739 } else if (!timeout) {
740 // we didn't time out and got a result.
741 return OK;
742 }
743 // We timed out. Just go around and try again.
Alex Light88fd7202017-06-30 08:31:59 -0700744 } while (true);
745 UNREACHABLE();
746}
747
748jvmtiError ThreadUtil::SuspendSelf(art::Thread* self) {
749 CHECK(self == art::Thread::Current());
750 {
751 art::MutexLock mu(self, *art::Locks::user_code_suspension_lock_);
752 art::MutexLock thread_list_mu(self, *art::Locks::thread_suspend_count_lock_);
753 if (self->GetUserCodeSuspendCount() != 0) {
754 // This can only happen if we race with another thread to suspend 'self' and we lose.
755 return ERR(THREAD_SUSPENDED);
756 }
757 // We shouldn't be able to fail this.
758 if (!self->ModifySuspendCount(self, +1, nullptr, art::SuspendReason::kForUserCode)) {
759 // TODO More specific error would be nice.
760 return ERR(INTERNAL);
761 }
762 }
763 // Once we have requested the suspend we actually go to sleep. We need to do this after releasing
764 // the suspend_lock to make sure we can be woken up. This call gains the mutator lock causing us
765 // to go to sleep until we are resumed.
766 SuspendCheck(self);
767 return OK;
768}
769
770jvmtiError ThreadUtil::SuspendThread(jvmtiEnv* env ATTRIBUTE_UNUSED, jthread thread) {
771 art::Thread* self = art::Thread::Current();
Alex Light3ae82532017-07-26 13:59:07 -0700772 bool target_is_self = false;
Alex Light88fd7202017-06-30 08:31:59 -0700773 {
774 art::ScopedObjectAccess soa(self);
Alex Light3ae82532017-07-26 13:59:07 -0700775 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
776 art::Thread* target = GetNativeThread(thread, soa);
777 if (target == nullptr) {
778 return ERR(INVALID_THREAD);
779 } else if (target == self) {
780 target_is_self = true;
781 }
Alex Light88fd7202017-06-30 08:31:59 -0700782 }
Alex Light3ae82532017-07-26 13:59:07 -0700783 if (target_is_self) {
Alex Light88fd7202017-06-30 08:31:59 -0700784 return SuspendSelf(self);
785 } else {
Alex Light3ae82532017-07-26 13:59:07 -0700786 return SuspendOther(self, thread);
Alex Light88fd7202017-06-30 08:31:59 -0700787 }
788}
789
790jvmtiError ThreadUtil::ResumeThread(jvmtiEnv* env ATTRIBUTE_UNUSED,
791 jthread thread) {
792 if (thread == nullptr) {
793 return ERR(NULL_POINTER);
794 }
795 art::Thread* self = art::Thread::Current();
796 art::Thread* target;
Alex Light3ae82532017-07-26 13:59:07 -0700797 // Retry until we know we won't get suspended by user code while resuming something.
798 do {
799 SuspendCheck(self);
800 art::MutexLock ucsl_mu(self, *art::Locks::user_code_suspension_lock_);
801 {
802 art::MutexLock tscl_mu(self, *art::Locks::thread_suspend_count_lock_);
803 // Make sure we won't be suspended in the middle of holding the thread_suspend_count_lock_ by
804 // a user-code suspension. We retry and do another SuspendCheck to clear this.
805 if (self->GetUserCodeSuspendCount() != 0) {
806 continue;
807 }
Alex Light88fd7202017-06-30 08:31:59 -0700808 }
Alex Light3ae82532017-07-26 13:59:07 -0700809 // From now on we know we cannot get suspended by user-code.
810 {
811 // NB This does a SuspendCheck (during thread state change) so we need to make sure we don't
812 // have the 'suspend_lock' locked here.
813 art::ScopedObjectAccess soa(self);
814 art::MutexLock tll_mu(self, *art::Locks::thread_list_lock_);
815 target = GetNativeThread(thread, soa);
816 if (target == nullptr) {
817 return ERR(INVALID_THREAD);
818 } else if (target == self) {
819 // We would have paused until we aren't suspended anymore due to the ScopedObjectAccess so
820 // we can just return THREAD_NOT_SUSPENDED. Unfortunately we cannot do any real DCHECKs
821 // about current state since it's all concurrent.
822 return ERR(THREAD_NOT_SUSPENDED);
823 } else if (target->GetState() == art::ThreadState::kTerminated) {
824 return ERR(THREAD_NOT_ALIVE);
825 }
826 // The JVMTI spec requires us to return THREAD_NOT_SUSPENDED if it is alive but we really
827 // cannot tell why resume failed.
828 {
829 art::MutexLock thread_suspend_count_mu(self, *art::Locks::thread_suspend_count_lock_);
830 if (target->GetUserCodeSuspendCount() == 0) {
831 return ERR(THREAD_NOT_SUSPENDED);
832 }
833 }
834 }
835 // It is okay that we don't have a thread_list_lock here since we know that the thread cannot
836 // die since it is currently held suspended by a SuspendReason::kForUserCode suspend.
837 DCHECK(target != self);
838 if (!art::Runtime::Current()->GetThreadList()->Resume(target,
839 art::SuspendReason::kForUserCode)) {
840 // TODO Give a better error.
841 // This is most likely THREAD_NOT_SUSPENDED but we cannot really be sure.
842 return ERR(INTERNAL);
843 } else {
844 return OK;
845 }
846 } while (true);
Alex Light88fd7202017-06-30 08:31:59 -0700847}
848
849// Suspends all the threads in the list at the same time. Getting this behavior is a little tricky
850// since we can have threads in the list multiple times. This generally doesn't matter unless the
851// current thread is present multiple times. In that case we need to suspend only once and either
852// return the same error code in all the other slots if it failed or return ERR(THREAD_SUSPENDED) if
853// it didn't. We also want to handle the current thread last to make the behavior of the code
854// simpler to understand.
855jvmtiError ThreadUtil::SuspendThreadList(jvmtiEnv* env,
856 jint request_count,
857 const jthread* threads,
858 jvmtiError* results) {
859 if (request_count == 0) {
860 return ERR(ILLEGAL_ARGUMENT);
861 } else if (results == nullptr || threads == nullptr) {
862 return ERR(NULL_POINTER);
863 }
864 // This is the list of the indexes in 'threads' and 'results' that correspond to the currently
865 // running thread. These indexes we need to handle specially since we need to only actually
866 // suspend a single time.
867 std::vector<jint> current_thread_indexes;
868 art::Thread* self = art::Thread::Current();
869 for (jint i = 0; i < request_count; i++) {
870 {
871 art::ScopedObjectAccess soa(self);
Alex Light3ae82532017-07-26 13:59:07 -0700872 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
Alex Light88fd7202017-06-30 08:31:59 -0700873 if (threads[i] == nullptr || GetNativeThread(threads[i], soa) == self) {
874 current_thread_indexes.push_back(i);
875 continue;
876 }
877 }
878 results[i] = env->SuspendThread(threads[i]);
879 }
880 if (!current_thread_indexes.empty()) {
881 jint first_current_thread_index = current_thread_indexes[0];
882 // Suspend self.
883 jvmtiError res = env->SuspendThread(threads[first_current_thread_index]);
884 results[first_current_thread_index] = res;
885 // Fill in the rest of the error values as appropriate.
886 jvmtiError other_results = (res != OK) ? res : ERR(THREAD_SUSPENDED);
887 for (auto it = ++current_thread_indexes.begin(); it != current_thread_indexes.end(); ++it) {
888 results[*it] = other_results;
889 }
890 }
891 return OK;
892}
893
894jvmtiError ThreadUtil::ResumeThreadList(jvmtiEnv* env,
895 jint request_count,
896 const jthread* threads,
897 jvmtiError* results) {
898 if (request_count == 0) {
899 return ERR(ILLEGAL_ARGUMENT);
900 } else if (results == nullptr || threads == nullptr) {
901 return ERR(NULL_POINTER);
902 }
903 for (jint i = 0; i < request_count; i++) {
904 results[i] = env->ResumeThread(threads[i]);
905 }
906 return OK;
907}
908
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800909} // namespace openjdkjvmti