blob: da1c1bccc7f91cb7e5c14cf08befa35f797aee26 [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
Alex Light1d8a9742017-08-17 11:12:06 -070060struct ThreadCallback : public art::ThreadLifecycleCallback {
Andreas Gampeeafaf572017-01-20 12:34:15 -080061 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 }
Alex Light1d8a9742017-08-17 11:12:06 -070067
Andreas Gampe983c1752017-01-23 19:46:56 -080068 template <ArtJvmtiEvent kEvent>
69 void Post(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampeeafaf572017-01-20 12:34:15 -080070 DCHECK_EQ(self, art::Thread::Current());
71 ScopedLocalRef<jthread> thread(self->GetJniEnv(), GetThreadObject(self));
Andreas Gampee6377462017-01-20 17:37:50 -080072 art::ScopedThreadSuspension sts(self, art::ThreadState::kNative);
Andreas Gampe983c1752017-01-23 19:46:56 -080073 event_handler->DispatchEvent<kEvent>(self,
74 reinterpret_cast<JNIEnv*>(self->GetJniEnv()),
75 thread.get());
Andreas Gampeeafaf572017-01-20 12:34:15 -080076 }
77
78 void ThreadStart(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
79 if (!started) {
80 // Runtime isn't started. We only expect at most the signal handler or JIT threads to be
81 // started here.
82 if (art::kIsDebugBuild) {
83 std::string name;
84 self->GetThreadName(name);
Alex Light5bd09542017-02-09 16:01:32 -080085 if (name != "JDWP" &&
86 name != "Signal Catcher" &&
87 !android::base::StartsWith(name, "Jit thread pool")) {
Alex Light23aa7482017-08-16 10:01:13 -070088 LOG(FATAL) << "Unexpected thread before start: " << name << " id: "
89 << self->GetThreadId();
Andreas Gampeeafaf572017-01-20 12:34:15 -080090 }
91 }
92 return;
93 }
Andreas Gampe983c1752017-01-23 19:46:56 -080094 Post<ArtJvmtiEvent::kThreadStart>(self);
Andreas Gampeeafaf572017-01-20 12:34:15 -080095 }
96
97 void ThreadDeath(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) {
Andreas Gampe983c1752017-01-23 19:46:56 -080098 Post<ArtJvmtiEvent::kThreadEnd>(self);
Andreas Gampeeafaf572017-01-20 12:34:15 -080099 }
100
Andreas Gampeeafaf572017-01-20 12:34:15 -0800101 EventHandler* event_handler = nullptr;
102 bool started = false;
103};
104
105ThreadCallback gThreadCallback;
106
107void ThreadUtil::Register(EventHandler* handler) {
108 art::Runtime* runtime = art::Runtime::Current();
109
110 gThreadCallback.started = runtime->IsStarted();
111 gThreadCallback.event_handler = handler;
112
113 art::ScopedThreadStateChange stsc(art::Thread::Current(),
114 art::ThreadState::kWaitingForDebuggerToAttach);
115 art::ScopedSuspendAll ssa("Add thread callback");
116 runtime->GetRuntimeCallbacks()->AddThreadLifecycleCallback(&gThreadCallback);
Alex Light1d8a9742017-08-17 11:12:06 -0700117}
118
119void ThreadUtil::VMInitEventSent() {
120 // We should have already started.
121 DCHECK(gThreadCallback.started);
122 // We moved to VMInit. Report the main thread as started (it was attached early, and must not be
123 // reported until Init.
124 gThreadCallback.Post<ArtJvmtiEvent::kThreadStart>(art::Thread::Current());
Andreas Gampeeafaf572017-01-20 12:34:15 -0800125}
126
Andreas Gampedb6c2ab2017-03-28 17:28:32 -0700127void ThreadUtil::CacheData() {
Alex Light1d8a9742017-08-17 11:12:06 -0700128 // We must have started since it is now safe to cache our data;
129 gThreadCallback.started = true;
Andreas Gampedb6c2ab2017-03-28 17:28:32 -0700130 art::ScopedObjectAccess soa(art::Thread::Current());
131 art::ObjPtr<art::mirror::Class> thread_class =
132 soa.Decode<art::mirror::Class>(art::WellKnownClasses::java_lang_Thread);
133 CHECK(thread_class != nullptr);
134 context_class_loader_ = thread_class->FindDeclaredInstanceField("contextClassLoader",
135 "Ljava/lang/ClassLoader;");
136 CHECK(context_class_loader_ != nullptr);
137}
138
Andreas Gampeeafaf572017-01-20 12:34:15 -0800139void ThreadUtil::Unregister() {
140 art::ScopedThreadStateChange stsc(art::Thread::Current(),
141 art::ThreadState::kWaitingForDebuggerToAttach);
142 art::ScopedSuspendAll ssa("Remove thread callback");
143 art::Runtime* runtime = art::Runtime::Current();
144 runtime->GetRuntimeCallbacks()->RemoveThreadLifecycleCallback(&gThreadCallback);
Andreas Gampeeafaf572017-01-20 12:34:15 -0800145}
146
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800147jvmtiError ThreadUtil::GetCurrentThread(jvmtiEnv* env ATTRIBUTE_UNUSED, jthread* thread_ptr) {
148 art::Thread* self = art::Thread::Current();
149
150 art::ScopedObjectAccess soa(self);
151
152 jthread thread_peer;
153 if (self->IsStillStarting()) {
154 thread_peer = nullptr;
155 } else {
156 thread_peer = soa.AddLocalReference<jthread>(self->GetPeer());
157 }
158
159 *thread_ptr = thread_peer;
160 return ERR(NONE);
161}
162
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800163// Get the native thread. The spec says a null object denotes the current thread.
Alex Lightbebd7bd2017-07-25 14:05:52 -0700164art::Thread* ThreadUtil::GetNativeThread(jthread thread,
165 const art::ScopedObjectAccessAlreadyRunnable& soa) {
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800166 if (thread == nullptr) {
167 return art::Thread::Current();
168 }
169
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800170 return art::Thread::FromManagedThread(soa, thread);
171}
172
173jvmtiError ThreadUtil::GetThreadInfo(jvmtiEnv* env, jthread thread, jvmtiThreadInfo* info_ptr) {
174 if (info_ptr == nullptr) {
175 return ERR(NULL_POINTER);
176 }
Andreas Gampedb6c2ab2017-03-28 17:28:32 -0700177 if (!PhaseUtil::IsLivePhase()) {
178 return JVMTI_ERROR_WRONG_PHASE;
179 }
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800180
Alex Light3ae82532017-07-26 13:59:07 -0700181 art::Thread* self = art::Thread::Current();
182 art::ScopedObjectAccess soa(self);
183 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800184
Alex Light3ae82532017-07-26 13:59:07 -0700185 art::Thread* target = GetNativeThread(thread, soa);
186 if (target == nullptr && thread == nullptr) {
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800187 return ERR(INVALID_THREAD);
188 }
189
Andreas Gampe54711412017-02-21 12:41:43 -0800190 JvmtiUniquePtr<char[]> name_uptr;
Alex Light3ae82532017-07-26 13:59:07 -0700191 if (target != nullptr) {
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800192 // Have a native thread object, this thread is alive.
193 std::string name;
Alex Light3ae82532017-07-26 13:59:07 -0700194 target->GetThreadName(name);
Andreas Gampe54711412017-02-21 12:41:43 -0800195 jvmtiError name_result;
196 name_uptr = CopyString(env, name.c_str(), &name_result);
197 if (name_uptr == nullptr) {
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800198 return name_result;
199 }
Andreas Gampe54711412017-02-21 12:41:43 -0800200 info_ptr->name = name_uptr.get();
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800201
Alex Light3ae82532017-07-26 13:59:07 -0700202 info_ptr->priority = target->GetNativePriority();
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800203
Alex Light3ae82532017-07-26 13:59:07 -0700204 info_ptr->is_daemon = target->IsDaemon();
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800205
Alex Light3ae82532017-07-26 13:59:07 -0700206 art::ObjPtr<art::mirror::Object> peer = target->GetPeerFromOtherThread();
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800207
208 // ThreadGroup.
209 if (peer != nullptr) {
210 art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_group);
211 CHECK(f != nullptr);
212 art::ObjPtr<art::mirror::Object> group = f->GetObject(peer);
213 info_ptr->thread_group = group == nullptr
214 ? nullptr
215 : soa.AddLocalReference<jthreadGroup>(group);
216 } else {
217 info_ptr->thread_group = nullptr;
218 }
219
220 // Context classloader.
Andreas Gampedb6c2ab2017-03-28 17:28:32 -0700221 DCHECK(context_class_loader_ != nullptr);
222 art::ObjPtr<art::mirror::Object> ccl = peer != nullptr
223 ? context_class_loader_->GetObject(peer)
224 : nullptr;
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800225 info_ptr->context_class_loader = ccl == nullptr
226 ? nullptr
227 : soa.AddLocalReference<jobject>(ccl);
228 } else {
229 // Only the peer. This thread has either not been started, or is dead. Read things from
230 // the Java side.
231 art::ObjPtr<art::mirror::Object> peer = soa.Decode<art::mirror::Object>(thread);
232
233 // Name.
234 {
235 art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_name);
236 CHECK(f != nullptr);
237 art::ObjPtr<art::mirror::Object> name = f->GetObject(peer);
238 std::string name_cpp;
239 const char* name_cstr;
240 if (name != nullptr) {
241 name_cpp = name->AsString()->ToModifiedUtf8();
242 name_cstr = name_cpp.c_str();
243 } else {
244 name_cstr = "";
245 }
Andreas Gampe54711412017-02-21 12:41:43 -0800246 jvmtiError name_result;
247 name_uptr = CopyString(env, name_cstr, &name_result);
248 if (name_uptr == nullptr) {
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800249 return name_result;
250 }
Andreas Gampe54711412017-02-21 12:41:43 -0800251 info_ptr->name = name_uptr.get();
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800252 }
253
254 // Priority.
255 {
256 art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_priority);
257 CHECK(f != nullptr);
258 info_ptr->priority = static_cast<jint>(f->GetInt(peer));
259 }
260
261 // Daemon.
262 {
263 art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_daemon);
264 CHECK(f != nullptr);
265 info_ptr->is_daemon = f->GetBoolean(peer) == 0 ? JNI_FALSE : JNI_TRUE;
266 }
267
268 // ThreadGroup.
269 {
270 art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_group);
271 CHECK(f != nullptr);
272 art::ObjPtr<art::mirror::Object> group = f->GetObject(peer);
273 info_ptr->thread_group = group == nullptr
274 ? nullptr
275 : soa.AddLocalReference<jthreadGroup>(group);
276 }
277
278 // Context classloader.
Andreas Gampedb6c2ab2017-03-28 17:28:32 -0700279 DCHECK(context_class_loader_ != nullptr);
280 art::ObjPtr<art::mirror::Object> ccl = peer != nullptr
281 ? context_class_loader_->GetObject(peer)
282 : nullptr;
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800283 info_ptr->context_class_loader = ccl == nullptr
284 ? nullptr
285 : soa.AddLocalReference<jobject>(ccl);
286 }
287
288 name_uptr.release();
289
290 return ERR(NONE);
291}
292
Alex Light1f0a22f2017-07-17 12:55:59 -0700293struct InternalThreadState {
294 art::Thread* native_thread;
295 art::ThreadState art_state;
296 int thread_user_code_suspend_count;
297};
298
299// Return the thread's (or current thread, if null) thread state.
300static InternalThreadState GetNativeThreadState(jthread thread,
301 const art::ScopedObjectAccessAlreadyRunnable& soa)
302 REQUIRES_SHARED(art::Locks::mutator_lock_)
Alex Light3ae82532017-07-26 13:59:07 -0700303 REQUIRES(art::Locks::thread_list_lock_, art::Locks::user_code_suspension_lock_) {
Andreas Gampe72c19832017-01-12 13:22:16 -0800304 art::Thread* self = nullptr;
Andreas Gampe72c19832017-01-12 13:22:16 -0800305 if (thread == nullptr) {
306 self = art::Thread::Current();
307 } else {
308 self = art::Thread::FromManagedThread(soa, thread);
309 }
Alex Light1f0a22f2017-07-17 12:55:59 -0700310 InternalThreadState thread_state = {};
311 art::MutexLock tscl_mu(soa.Self(), *art::Locks::thread_suspend_count_lock_);
312 thread_state.native_thread = self;
Andreas Gampe72c19832017-01-12 13:22:16 -0800313 if (self == nullptr || self->IsStillStarting()) {
Alex Light1f0a22f2017-07-17 12:55:59 -0700314 thread_state.art_state = art::ThreadState::kStarting;
315 thread_state.thread_user_code_suspend_count = 0;
316 } else {
317 thread_state.art_state = self->GetState();
318 thread_state.thread_user_code_suspend_count = self->GetUserCodeSuspendCount();
Andreas Gampe72c19832017-01-12 13:22:16 -0800319 }
Alex Light1f0a22f2017-07-17 12:55:59 -0700320 return thread_state;
Andreas Gampe72c19832017-01-12 13:22:16 -0800321}
322
Alex Light1f0a22f2017-07-17 12:55:59 -0700323static jint GetJvmtiThreadStateFromInternal(const InternalThreadState& state) {
324 art::ThreadState internal_thread_state = state.art_state;
Andreas Gampe72c19832017-01-12 13:22:16 -0800325 jint jvmti_state = JVMTI_THREAD_STATE_ALIVE;
326
Alex Light1f0a22f2017-07-17 12:55:59 -0700327 if (state.thread_user_code_suspend_count != 0) {
Andreas Gampe72c19832017-01-12 13:22:16 -0800328 jvmti_state |= JVMTI_THREAD_STATE_SUSPENDED;
329 // Note: We do not have data about the previous state. Otherwise we should load the previous
330 // state here.
331 }
332
Alex Light1f0a22f2017-07-17 12:55:59 -0700333 if (state.native_thread->IsInterrupted()) {
334 jvmti_state |= JVMTI_THREAD_STATE_INTERRUPTED;
335 }
336
Andreas Gampe72c19832017-01-12 13:22:16 -0800337 if (internal_thread_state == art::ThreadState::kNative) {
338 jvmti_state |= JVMTI_THREAD_STATE_IN_NATIVE;
339 }
340
341 if (internal_thread_state == art::ThreadState::kRunnable ||
342 internal_thread_state == art::ThreadState::kWaitingWeakGcRootRead ||
343 internal_thread_state == art::ThreadState::kSuspended) {
344 jvmti_state |= JVMTI_THREAD_STATE_RUNNABLE;
345 } else if (internal_thread_state == art::ThreadState::kBlocked) {
346 jvmti_state |= JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER;
347 } else {
348 // Should be in waiting state.
349 jvmti_state |= JVMTI_THREAD_STATE_WAITING;
350
351 if (internal_thread_state == art::ThreadState::kTimedWaiting ||
352 internal_thread_state == art::ThreadState::kSleeping) {
353 jvmti_state |= JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT;
354 } else {
355 jvmti_state |= JVMTI_THREAD_STATE_WAITING_INDEFINITELY;
356 }
357
358 if (internal_thread_state == art::ThreadState::kSleeping) {
359 jvmti_state |= JVMTI_THREAD_STATE_SLEEPING;
360 }
361
362 if (internal_thread_state == art::ThreadState::kTimedWaiting ||
363 internal_thread_state == art::ThreadState::kWaiting) {
364 jvmti_state |= JVMTI_THREAD_STATE_IN_OBJECT_WAIT;
365 }
366
367 // TODO: PARKED. We'll have to inspect the stack.
368 }
369
370 return jvmti_state;
371}
372
Alex Light1f0a22f2017-07-17 12:55:59 -0700373static jint GetJavaStateFromInternal(const InternalThreadState& state) {
374 switch (state.art_state) {
Andreas Gampe72c19832017-01-12 13:22:16 -0800375 case art::ThreadState::kTerminated:
376 return JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED;
377
378 case art::ThreadState::kRunnable:
379 case art::ThreadState::kNative:
380 case art::ThreadState::kWaitingWeakGcRootRead:
381 case art::ThreadState::kSuspended:
382 return JVMTI_JAVA_LANG_THREAD_STATE_RUNNABLE;
383
384 case art::ThreadState::kTimedWaiting:
385 case art::ThreadState::kSleeping:
386 return JVMTI_JAVA_LANG_THREAD_STATE_TIMED_WAITING;
387
388 case art::ThreadState::kBlocked:
389 return JVMTI_JAVA_LANG_THREAD_STATE_BLOCKED;
390
391 case art::ThreadState::kStarting:
392 return JVMTI_JAVA_LANG_THREAD_STATE_NEW;
393
394 case art::ThreadState::kWaiting:
Alex Light77fee872017-09-05 14:51:49 -0700395 case art::ThreadState::kWaitingForTaskProcessor:
396 case art::ThreadState::kWaitingForLockInflation:
Andreas Gampe72c19832017-01-12 13:22:16 -0800397 case art::ThreadState::kWaitingForGcToComplete:
398 case art::ThreadState::kWaitingPerformingGc:
399 case art::ThreadState::kWaitingForCheckPointsToRun:
400 case art::ThreadState::kWaitingForDebuggerSend:
401 case art::ThreadState::kWaitingForDebuggerToAttach:
402 case art::ThreadState::kWaitingInMainDebuggerLoop:
403 case art::ThreadState::kWaitingForDebuggerSuspension:
404 case art::ThreadState::kWaitingForDeoptimization:
405 case art::ThreadState::kWaitingForGetObjectsAllocated:
406 case art::ThreadState::kWaitingForJniOnLoad:
407 case art::ThreadState::kWaitingForSignalCatcherOutput:
408 case art::ThreadState::kWaitingInMainSignalCatcherLoop:
409 case art::ThreadState::kWaitingForMethodTracingStart:
410 case art::ThreadState::kWaitingForVisitObjects:
411 case art::ThreadState::kWaitingForGcThreadFlip:
412 return JVMTI_JAVA_LANG_THREAD_STATE_WAITING;
413 }
414 LOG(FATAL) << "Unreachable";
415 UNREACHABLE();
416}
417
Alex Light1f0a22f2017-07-17 12:55:59 -0700418// Suspends the current thread if it has any suspend requests on it.
Alex Light23aa7482017-08-16 10:01:13 -0700419void ThreadUtil::SuspendCheck(art::Thread* self) {
Alex Light1f0a22f2017-07-17 12:55:59 -0700420 art::ScopedObjectAccess soa(self);
421 // Really this is only needed if we are in FastJNI and actually have the mutator_lock_ already.
422 self->FullSuspendCheck();
423}
424
Alex Light23aa7482017-08-16 10:01:13 -0700425bool ThreadUtil::WouldSuspendForUserCodeLocked(art::Thread* self) {
426 DCHECK(self == art::Thread::Current());
427 art::MutexLock tscl_mu(self, *art::Locks::thread_suspend_count_lock_);
428 return self->GetUserCodeSuspendCount() != 0;
429}
430
431bool ThreadUtil::WouldSuspendForUserCode(art::Thread* self) {
432 DCHECK(self == art::Thread::Current());
433 art::MutexLock ucsl_mu(self, *art::Locks::user_code_suspension_lock_);
434 return WouldSuspendForUserCodeLocked(self);
435}
436
Andreas Gampe72c19832017-01-12 13:22:16 -0800437jvmtiError ThreadUtil::GetThreadState(jvmtiEnv* env ATTRIBUTE_UNUSED,
438 jthread thread,
439 jint* thread_state_ptr) {
440 if (thread_state_ptr == nullptr) {
441 return ERR(NULL_POINTER);
442 }
443
Alex Light1f0a22f2017-07-17 12:55:59 -0700444 art::Thread* self = art::Thread::Current();
445 InternalThreadState state = {};
446 // Loop since we need to bail out and try again if we would end up getting suspended while holding
447 // the user_code_suspension_lock_ due to a SuspendReason::kForUserCode. In this situation we
448 // release the lock, wait to get resumed and try again.
449 do {
450 SuspendCheck(self);
451 art::MutexLock ucsl_mu(self, *art::Locks::user_code_suspension_lock_);
Alex Light23aa7482017-08-16 10:01:13 -0700452 if (WouldSuspendForUserCodeLocked(self)) {
453 // Make sure we won't be suspended in the middle of holding the thread_suspend_count_lock_ by
454 // a user-code suspension. We retry and do another SuspendCheck to clear this.
455 continue;
Alex Light1f0a22f2017-07-17 12:55:59 -0700456 }
457 art::ScopedObjectAccess soa(self);
Alex Light3ae82532017-07-26 13:59:07 -0700458 art::MutexLock tll_mu(self, *art::Locks::thread_list_lock_);
Alex Light1f0a22f2017-07-17 12:55:59 -0700459 state = GetNativeThreadState(thread, soa);
Alex Light3ae82532017-07-26 13:59:07 -0700460 if (state.art_state == art::ThreadState::kStarting) {
461 break;
462 }
463 DCHECK(state.native_thread != nullptr);
464
465 // Translate internal thread state to JVMTI and Java state.
466 jint jvmti_state = GetJvmtiThreadStateFromInternal(state);
467
468 // Java state is derived from nativeGetState.
469 // TODO: Our implementation assigns "runnable" to suspended. As such, we will have slightly
470 // different mask if a thread got suspended due to user-code. However, this is for
471 // consistency with the Java view.
472 jint java_state = GetJavaStateFromInternal(state);
473
474 *thread_state_ptr = jvmti_state | java_state;
475
476 return ERR(NONE);
Alex Light1f0a22f2017-07-17 12:55:59 -0700477 } while (true);
Andreas Gampe72c19832017-01-12 13:22:16 -0800478
Alex Light3ae82532017-07-26 13:59:07 -0700479 DCHECK_EQ(state.art_state, art::ThreadState::kStarting);
Andreas Gampe72c19832017-01-12 13:22:16 -0800480
Alex Light3ae82532017-07-26 13:59:07 -0700481 if (thread == nullptr) {
482 // No native thread, and no Java thread? We must be starting up. Report as wrong phase.
483 return ERR(WRONG_PHASE);
Andreas Gampe72c19832017-01-12 13:22:16 -0800484 }
Andreas Gampe72c19832017-01-12 13:22:16 -0800485
Alex Light3ae82532017-07-26 13:59:07 -0700486 art::ScopedObjectAccess soa(self);
Alex Lightba461c32017-09-22 14:19:18 -0700487 art::StackHandleScope<1> hs(self);
Andreas Gampe72c19832017-01-12 13:22:16 -0800488
Alex Light3ae82532017-07-26 13:59:07 -0700489 // Need to read the Java "started" field to know whether this is starting or terminated.
Alex Lightba461c32017-09-22 14:19:18 -0700490 art::Handle<art::mirror::Object> peer(hs.NewHandle(soa.Decode<art::mirror::Object>(thread)));
491 art::ObjPtr<art::mirror::Class> thread_klass =
492 soa.Decode<art::mirror::Class>(art::WellKnownClasses::java_lang_Thread);
493 if (!thread_klass->IsAssignableFrom(peer->GetClass())) {
494 return ERR(INVALID_THREAD);
495 }
496 art::ArtField* started_field = thread_klass->FindDeclaredInstanceField("started", "Z");
Alex Light3ae82532017-07-26 13:59:07 -0700497 CHECK(started_field != nullptr);
Alex Lightba461c32017-09-22 14:19:18 -0700498 bool started = started_field->GetBoolean(peer.Get()) != 0;
Alex Light3ae82532017-07-26 13:59:07 -0700499 constexpr jint kStartedState = JVMTI_JAVA_LANG_THREAD_STATE_NEW;
500 constexpr jint kTerminatedState = JVMTI_THREAD_STATE_TERMINATED |
501 JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED;
502 *thread_state_ptr = started ? kTerminatedState : kStartedState;
Andreas Gampe72c19832017-01-12 13:22:16 -0800503 return ERR(NONE);
504}
505
Andreas Gampe85807442017-01-13 14:40:58 -0800506jvmtiError ThreadUtil::GetAllThreads(jvmtiEnv* env,
507 jint* threads_count_ptr,
508 jthread** threads_ptr) {
509 if (threads_count_ptr == nullptr || threads_ptr == nullptr) {
510 return ERR(NULL_POINTER);
511 }
512
513 art::Thread* current = art::Thread::Current();
514
515 art::ScopedObjectAccess soa(current);
516
517 art::MutexLock mu(current, *art::Locks::thread_list_lock_);
518 std::list<art::Thread*> thread_list = art::Runtime::Current()->GetThreadList()->GetList();
519
520 std::vector<art::ObjPtr<art::mirror::Object>> peers;
521
522 for (art::Thread* thread : thread_list) {
523 // Skip threads that are still starting.
524 if (thread->IsStillStarting()) {
525 continue;
526 }
527
Andreas Gampe202f85a2017-02-06 10:23:26 -0800528 art::ObjPtr<art::mirror::Object> peer = thread->GetPeerFromOtherThread();
Andreas Gampe85807442017-01-13 14:40:58 -0800529 if (peer != nullptr) {
530 peers.push_back(peer);
531 }
532 }
533
534 if (peers.empty()) {
535 *threads_count_ptr = 0;
536 *threads_ptr = nullptr;
537 } else {
538 unsigned char* data;
539 jvmtiError data_result = env->Allocate(peers.size() * sizeof(jthread), &data);
540 if (data_result != ERR(NONE)) {
541 return data_result;
542 }
543 jthread* threads = reinterpret_cast<jthread*>(data);
544 for (size_t i = 0; i != peers.size(); ++i) {
545 threads[i] = soa.AddLocalReference<jthread>(peers[i]);
546 }
547
548 *threads_count_ptr = static_cast<jint>(peers.size());
549 *threads_ptr = threads;
550 }
Andreas Gampef26bf2d2017-01-13 16:47:14 -0800551 return ERR(NONE);
552}
Andreas Gampe85807442017-01-13 14:40:58 -0800553
Alex Light092a4042017-07-12 08:46:44 -0700554// The struct that we store in the art::Thread::custom_tls_ that maps the jvmtiEnvs to the data
555// stored with that thread. This is needed since different jvmtiEnvs are not supposed to share TLS
556// data but we only have a single slot in Thread objects to store data.
557struct JvmtiGlobalTLSData {
558 std::unordered_map<jvmtiEnv*, const void*> data GUARDED_BY(art::Locks::thread_list_lock_);
559};
560
561static void RemoveTLSData(art::Thread* target, void* ctx) REQUIRES(art::Locks::thread_list_lock_) {
562 jvmtiEnv* env = reinterpret_cast<jvmtiEnv*>(ctx);
563 art::Locks::thread_list_lock_->AssertHeld(art::Thread::Current());
564 JvmtiGlobalTLSData* global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS());
565 if (global_tls != nullptr) {
566 global_tls->data.erase(env);
567 }
568}
569
570void ThreadUtil::RemoveEnvironment(jvmtiEnv* env) {
571 art::Thread* self = art::Thread::Current();
572 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
573 art::ThreadList* list = art::Runtime::Current()->GetThreadList();
574 list->ForEach(RemoveTLSData, env);
575}
576
577jvmtiError ThreadUtil::SetThreadLocalStorage(jvmtiEnv* env, jthread thread, const void* data) {
578 art::Thread* self = art::Thread::Current();
579 art::ScopedObjectAccess soa(self);
580 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
Alex Light3ae82532017-07-26 13:59:07 -0700581 art::Thread* target = GetNativeThread(thread, soa);
Alex Light092a4042017-07-12 08:46:44 -0700582 if (target == nullptr && thread == nullptr) {
Andreas Gampef26bf2d2017-01-13 16:47:14 -0800583 return ERR(INVALID_THREAD);
584 }
Alex Light092a4042017-07-12 08:46:44 -0700585 if (target == nullptr) {
Andreas Gampef26bf2d2017-01-13 16:47:14 -0800586 return ERR(THREAD_NOT_ALIVE);
587 }
588
Alex Light092a4042017-07-12 08:46:44 -0700589 JvmtiGlobalTLSData* global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS());
590 if (global_tls == nullptr) {
591 target->SetCustomTLS(new JvmtiGlobalTLSData);
592 global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS());
593 }
594
595 global_tls->data[env] = data;
Andreas Gampef26bf2d2017-01-13 16:47:14 -0800596
597 return ERR(NONE);
598}
599
Alex Light092a4042017-07-12 08:46:44 -0700600jvmtiError ThreadUtil::GetThreadLocalStorage(jvmtiEnv* env,
Andreas Gampef26bf2d2017-01-13 16:47:14 -0800601 jthread thread,
602 void** data_ptr) {
603 if (data_ptr == nullptr) {
604 return ERR(NULL_POINTER);
605 }
606
Alex Light092a4042017-07-12 08:46:44 -0700607 art::Thread* self = art::Thread::Current();
608 art::ScopedObjectAccess soa(self);
609 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
Alex Light3ae82532017-07-26 13:59:07 -0700610 art::Thread* target = GetNativeThread(thread, soa);
Alex Light092a4042017-07-12 08:46:44 -0700611 if (target == nullptr && thread == nullptr) {
Andreas Gampef26bf2d2017-01-13 16:47:14 -0800612 return ERR(INVALID_THREAD);
613 }
Alex Light092a4042017-07-12 08:46:44 -0700614 if (target == nullptr) {
Andreas Gampef26bf2d2017-01-13 16:47:14 -0800615 return ERR(THREAD_NOT_ALIVE);
616 }
617
Alex Light092a4042017-07-12 08:46:44 -0700618 JvmtiGlobalTLSData* global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS());
619 if (global_tls == nullptr) {
620 *data_ptr = nullptr;
621 return OK;
622 }
623 auto it = global_tls->data.find(env);
624 if (it != global_tls->data.end()) {
625 *data_ptr = const_cast<void*>(it->second);
626 } else {
627 *data_ptr = nullptr;
628 }
629
Andreas Gampe85807442017-01-13 14:40:58 -0800630 return ERR(NONE);
631}
632
Andreas Gampe732b0ac2017-01-18 15:23:39 -0800633struct AgentData {
634 const void* arg;
635 jvmtiStartFunction proc;
636 jthread thread;
637 JavaVM* java_vm;
638 jvmtiEnv* jvmti_env;
639 jint priority;
640};
641
642static void* AgentCallback(void* arg) {
643 std::unique_ptr<AgentData> data(reinterpret_cast<AgentData*>(arg));
644 CHECK(data->thread != nullptr);
645
646 // We already have a peer. So call our special Attach function.
647 art::Thread* self = art::Thread::Attach("JVMTI Agent thread", true, data->thread);
648 CHECK(self != nullptr);
649 // The name in Attach() is only for logging. Set the thread name. This is important so
650 // that the thread is no longer seen as starting up.
651 {
652 art::ScopedObjectAccess soa(self);
653 self->SetThreadName("JVMTI Agent thread");
654 }
655
656 // Release the peer.
657 JNIEnv* env = self->GetJniEnv();
658 env->DeleteGlobalRef(data->thread);
659 data->thread = nullptr;
660
661 // Run the agent code.
662 data->proc(data->jvmti_env, env, const_cast<void*>(data->arg));
663
664 // Detach the thread.
665 int detach_result = data->java_vm->DetachCurrentThread();
666 CHECK_EQ(detach_result, 0);
667
668 return nullptr;
669}
670
671jvmtiError ThreadUtil::RunAgentThread(jvmtiEnv* jvmti_env,
672 jthread thread,
673 jvmtiStartFunction proc,
674 const void* arg,
675 jint priority) {
Alex Light23aa7482017-08-16 10:01:13 -0700676 if (!PhaseUtil::IsLivePhase()) {
677 return ERR(WRONG_PHASE);
678 }
Andreas Gampe732b0ac2017-01-18 15:23:39 -0800679 if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) {
680 return ERR(INVALID_PRIORITY);
681 }
682 JNIEnv* env = art::Thread::Current()->GetJniEnv();
683 if (thread == nullptr || !env->IsInstanceOf(thread, art::WellKnownClasses::java_lang_Thread)) {
684 return ERR(INVALID_THREAD);
685 }
686 if (proc == nullptr) {
687 return ERR(NULL_POINTER);
688 }
689
690 std::unique_ptr<AgentData> data(new AgentData);
691 data->arg = arg;
692 data->proc = proc;
693 // We need a global ref for Java objects, as local refs will be invalid.
694 data->thread = env->NewGlobalRef(thread);
695 data->java_vm = art::Runtime::Current()->GetJavaVM();
696 data->jvmti_env = jvmti_env;
697 data->priority = priority;
698
699 pthread_t pthread;
700 int pthread_create_result = pthread_create(&pthread,
701 nullptr,
702 &AgentCallback,
703 reinterpret_cast<void*>(data.get()));
704 if (pthread_create_result != 0) {
705 return ERR(INTERNAL);
706 }
707 data.release();
708
709 return ERR(NONE);
710}
711
Alex Light88fd7202017-06-30 08:31:59 -0700712jvmtiError ThreadUtil::SuspendOther(art::Thread* self,
Alex Light3ae82532017-07-26 13:59:07 -0700713 jthread target_jthread) {
Alex Light88fd7202017-06-30 08:31:59 -0700714 // Loop since we need to bail out and try again if we would end up getting suspended while holding
715 // the user_code_suspension_lock_ due to a SuspendReason::kForUserCode. In this situation we
716 // release the lock, wait to get resumed and try again.
717 do {
718 // Suspend ourself if we have any outstanding suspends. This is so we won't suspend due to
719 // another SuspendThread in the middle of suspending something else potentially causing a
720 // deadlock. We need to do this in the loop because if we ended up back here then we had
721 // outstanding SuspendReason::kForUserCode suspensions and we should wait for them to be cleared
722 // before continuing.
723 SuspendCheck(self);
724 art::MutexLock mu(self, *art::Locks::user_code_suspension_lock_);
Alex Light23aa7482017-08-16 10:01:13 -0700725 if (WouldSuspendForUserCodeLocked(self)) {
Alex Light88fd7202017-06-30 08:31:59 -0700726 // Make sure we won't be suspended in the middle of holding the thread_suspend_count_lock_ by
727 // a user-code suspension. We retry and do another SuspendCheck to clear this.
Alex Light23aa7482017-08-16 10:01:13 -0700728 continue;
Alex Light88fd7202017-06-30 08:31:59 -0700729 }
Alex Light23aa7482017-08-16 10:01:13 -0700730 // We are not going to be suspended by user code from now on.
Alex Light3ae82532017-07-26 13:59:07 -0700731 {
732 art::ScopedObjectAccess soa(self);
733 art::MutexLock thread_list_mu(self, *art::Locks::thread_list_lock_);
734 art::Thread* target = GetNativeThread(target_jthread, soa);
Alex Light88fd7202017-06-30 08:31:59 -0700735 art::ThreadState state = target->GetState();
736 if (state == art::ThreadState::kTerminated || state == art::ThreadState::kStarting) {
737 return ERR(THREAD_NOT_ALIVE);
Alex Light3ae82532017-07-26 13:59:07 -0700738 } else {
739 art::MutexLock thread_suspend_count_mu(self, *art::Locks::thread_suspend_count_lock_);
740 if (target->GetUserCodeSuspendCount() != 0) {
741 return ERR(THREAD_SUSPENDED);
742 }
Alex Light88fd7202017-06-30 08:31:59 -0700743 }
744 }
Alex Light3ae82532017-07-26 13:59:07 -0700745 bool timeout = true;
746 art::Thread* ret_target = art::Runtime::Current()->GetThreadList()->SuspendThreadByPeer(
747 target_jthread,
748 /* request_suspension */ true,
749 art::SuspendReason::kForUserCode,
750 &timeout);
751 if (ret_target == nullptr && !timeout) {
752 // TODO It would be good to get more information about why exactly the thread failed to
753 // suspend.
754 return ERR(INTERNAL);
755 } else if (!timeout) {
756 // we didn't time out and got a result.
757 return OK;
758 }
759 // We timed out. Just go around and try again.
Alex Light88fd7202017-06-30 08:31:59 -0700760 } while (true);
761 UNREACHABLE();
762}
763
764jvmtiError ThreadUtil::SuspendSelf(art::Thread* self) {
765 CHECK(self == art::Thread::Current());
766 {
767 art::MutexLock mu(self, *art::Locks::user_code_suspension_lock_);
768 art::MutexLock thread_list_mu(self, *art::Locks::thread_suspend_count_lock_);
769 if (self->GetUserCodeSuspendCount() != 0) {
770 // This can only happen if we race with another thread to suspend 'self' and we lose.
771 return ERR(THREAD_SUSPENDED);
772 }
773 // We shouldn't be able to fail this.
774 if (!self->ModifySuspendCount(self, +1, nullptr, art::SuspendReason::kForUserCode)) {
775 // TODO More specific error would be nice.
776 return ERR(INTERNAL);
777 }
778 }
779 // Once we have requested the suspend we actually go to sleep. We need to do this after releasing
780 // the suspend_lock to make sure we can be woken up. This call gains the mutator lock causing us
781 // to go to sleep until we are resumed.
782 SuspendCheck(self);
783 return OK;
784}
785
786jvmtiError ThreadUtil::SuspendThread(jvmtiEnv* env ATTRIBUTE_UNUSED, jthread thread) {
787 art::Thread* self = art::Thread::Current();
Alex Light3ae82532017-07-26 13:59:07 -0700788 bool target_is_self = false;
Alex Light88fd7202017-06-30 08:31:59 -0700789 {
790 art::ScopedObjectAccess soa(self);
Alex Light3ae82532017-07-26 13:59:07 -0700791 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
792 art::Thread* target = GetNativeThread(thread, soa);
793 if (target == nullptr) {
794 return ERR(INVALID_THREAD);
795 } else if (target == self) {
796 target_is_self = true;
797 }
Alex Light88fd7202017-06-30 08:31:59 -0700798 }
Alex Light3ae82532017-07-26 13:59:07 -0700799 if (target_is_self) {
Alex Light88fd7202017-06-30 08:31:59 -0700800 return SuspendSelf(self);
801 } else {
Alex Light3ae82532017-07-26 13:59:07 -0700802 return SuspendOther(self, thread);
Alex Light88fd7202017-06-30 08:31:59 -0700803 }
804}
805
806jvmtiError ThreadUtil::ResumeThread(jvmtiEnv* env ATTRIBUTE_UNUSED,
807 jthread thread) {
808 if (thread == nullptr) {
809 return ERR(NULL_POINTER);
810 }
811 art::Thread* self = art::Thread::Current();
812 art::Thread* target;
Alex Light3ae82532017-07-26 13:59:07 -0700813 // Retry until we know we won't get suspended by user code while resuming something.
814 do {
815 SuspendCheck(self);
816 art::MutexLock ucsl_mu(self, *art::Locks::user_code_suspension_lock_);
Alex Light23aa7482017-08-16 10:01:13 -0700817 if (WouldSuspendForUserCodeLocked(self)) {
Alex Light3ae82532017-07-26 13:59:07 -0700818 // Make sure we won't be suspended in the middle of holding the thread_suspend_count_lock_ by
819 // a user-code suspension. We retry and do another SuspendCheck to clear this.
Alex Light23aa7482017-08-16 10:01:13 -0700820 continue;
Alex Light88fd7202017-06-30 08:31:59 -0700821 }
Alex Light3ae82532017-07-26 13:59:07 -0700822 // From now on we know we cannot get suspended by user-code.
823 {
824 // NB This does a SuspendCheck (during thread state change) so we need to make sure we don't
825 // have the 'suspend_lock' locked here.
826 art::ScopedObjectAccess soa(self);
827 art::MutexLock tll_mu(self, *art::Locks::thread_list_lock_);
828 target = GetNativeThread(thread, soa);
829 if (target == nullptr) {
830 return ERR(INVALID_THREAD);
831 } else if (target == self) {
832 // We would have paused until we aren't suspended anymore due to the ScopedObjectAccess so
833 // we can just return THREAD_NOT_SUSPENDED. Unfortunately we cannot do any real DCHECKs
834 // about current state since it's all concurrent.
835 return ERR(THREAD_NOT_SUSPENDED);
836 } else if (target->GetState() == art::ThreadState::kTerminated) {
837 return ERR(THREAD_NOT_ALIVE);
838 }
839 // The JVMTI spec requires us to return THREAD_NOT_SUSPENDED if it is alive but we really
840 // cannot tell why resume failed.
841 {
842 art::MutexLock thread_suspend_count_mu(self, *art::Locks::thread_suspend_count_lock_);
843 if (target->GetUserCodeSuspendCount() == 0) {
844 return ERR(THREAD_NOT_SUSPENDED);
845 }
846 }
847 }
848 // It is okay that we don't have a thread_list_lock here since we know that the thread cannot
849 // die since it is currently held suspended by a SuspendReason::kForUserCode suspend.
850 DCHECK(target != self);
851 if (!art::Runtime::Current()->GetThreadList()->Resume(target,
852 art::SuspendReason::kForUserCode)) {
853 // TODO Give a better error.
854 // This is most likely THREAD_NOT_SUSPENDED but we cannot really be sure.
855 return ERR(INTERNAL);
856 } else {
857 return OK;
858 }
859 } while (true);
Alex Light88fd7202017-06-30 08:31:59 -0700860}
861
862// Suspends all the threads in the list at the same time. Getting this behavior is a little tricky
863// since we can have threads in the list multiple times. This generally doesn't matter unless the
864// current thread is present multiple times. In that case we need to suspend only once and either
865// return the same error code in all the other slots if it failed or return ERR(THREAD_SUSPENDED) if
866// it didn't. We also want to handle the current thread last to make the behavior of the code
867// simpler to understand.
868jvmtiError ThreadUtil::SuspendThreadList(jvmtiEnv* env,
869 jint request_count,
870 const jthread* threads,
871 jvmtiError* results) {
872 if (request_count == 0) {
873 return ERR(ILLEGAL_ARGUMENT);
874 } else if (results == nullptr || threads == nullptr) {
875 return ERR(NULL_POINTER);
876 }
877 // This is the list of the indexes in 'threads' and 'results' that correspond to the currently
878 // running thread. These indexes we need to handle specially since we need to only actually
879 // suspend a single time.
880 std::vector<jint> current_thread_indexes;
881 art::Thread* self = art::Thread::Current();
882 for (jint i = 0; i < request_count; i++) {
883 {
884 art::ScopedObjectAccess soa(self);
Alex Light3ae82532017-07-26 13:59:07 -0700885 art::MutexLock mu(self, *art::Locks::thread_list_lock_);
Alex Light88fd7202017-06-30 08:31:59 -0700886 if (threads[i] == nullptr || GetNativeThread(threads[i], soa) == self) {
887 current_thread_indexes.push_back(i);
888 continue;
889 }
890 }
891 results[i] = env->SuspendThread(threads[i]);
892 }
893 if (!current_thread_indexes.empty()) {
894 jint first_current_thread_index = current_thread_indexes[0];
895 // Suspend self.
896 jvmtiError res = env->SuspendThread(threads[first_current_thread_index]);
897 results[first_current_thread_index] = res;
898 // Fill in the rest of the error values as appropriate.
899 jvmtiError other_results = (res != OK) ? res : ERR(THREAD_SUSPENDED);
900 for (auto it = ++current_thread_indexes.begin(); it != current_thread_indexes.end(); ++it) {
901 results[*it] = other_results;
902 }
903 }
904 return OK;
905}
906
907jvmtiError ThreadUtil::ResumeThreadList(jvmtiEnv* env,
908 jint request_count,
909 const jthread* threads,
910 jvmtiError* results) {
911 if (request_count == 0) {
912 return ERR(ILLEGAL_ARGUMENT);
913 } else if (results == nullptr || threads == nullptr) {
914 return ERR(NULL_POINTER);
915 }
916 for (jint i = 0; i < request_count; i++) {
917 results[i] = env->ResumeThread(threads[i]);
918 }
919 return OK;
920}
921
Andreas Gampeaf13ab92017-01-11 20:57:40 -0800922} // namespace openjdkjvmti