Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 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 | #include "ti_thread.h" |
| 33 | |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 34 | #include "android-base/strings.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 35 | #include "art_field-inl.h" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 36 | #include "art_jvmti.h" |
| 37 | #include "base/logging.h" |
| 38 | #include "base/mutex.h" |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 39 | #include "events-inl.h" |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 40 | #include "gc/system_weak.h" |
| 41 | #include "gc_root-inl.h" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 42 | #include "jni_internal.h" |
| 43 | #include "mirror/class.h" |
| 44 | #include "mirror/object-inl.h" |
| 45 | #include "mirror/string.h" |
| 46 | #include "obj_ptr.h" |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 47 | #include "ti_phase.h" |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 48 | #include "runtime.h" |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 49 | #include "runtime_callbacks.h" |
| 50 | #include "ScopedLocalRef.h" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 51 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 52 | #include "thread-current-inl.h" |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 53 | #include "thread_list.h" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 54 | #include "well_known_classes.h" |
| 55 | |
| 56 | namespace openjdkjvmti { |
| 57 | |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 58 | art::ArtField* ThreadUtil::context_class_loader_ = nullptr; |
| 59 | |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 60 | struct 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 Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 67 | template <ArtJvmtiEvent kEvent> |
| 68 | void Post(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 69 | DCHECK_EQ(self, art::Thread::Current()); |
| 70 | ScopedLocalRef<jthread> thread(self->GetJniEnv(), GetThreadObject(self)); |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 71 | art::ScopedThreadSuspension sts(self, art::ThreadState::kNative); |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 72 | event_handler->DispatchEvent<kEvent>(self, |
| 73 | reinterpret_cast<JNIEnv*>(self->GetJniEnv()), |
| 74 | thread.get()); |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 75 | } |
| 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 Light | 5bd0954 | 2017-02-09 16:01:32 -0800 | [diff] [blame] | 84 | if (name != "JDWP" && |
| 85 | name != "Signal Catcher" && |
| 86 | !android::base::StartsWith(name, "Jit thread pool")) { |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 87 | LOG(FATAL) << "Unexpected thread before start: " << name; |
| 88 | } |
| 89 | } |
| 90 | return; |
| 91 | } |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 92 | Post<ArtJvmtiEvent::kThreadStart>(self); |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void ThreadDeath(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 96 | Post<ArtJvmtiEvent::kThreadEnd>(self); |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 97 | } |
| 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 Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 104 | Post<ArtJvmtiEvent::kThreadStart>(art::Thread::Current()); |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | EventHandler* event_handler = nullptr; |
| 109 | bool started = false; |
| 110 | }; |
| 111 | |
| 112 | ThreadCallback gThreadCallback; |
| 113 | |
| 114 | void 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 Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 127 | void 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 Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 137 | void 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 Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 146 | jvmtiError 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 | |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame^] | 162 | static art::Thread* GetNativeThreadLocked(jthread thread, |
| 163 | const art::ScopedObjectAccessAlreadyRunnable& soa) |
| 164 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 165 | REQUIRES(art::Locks::thread_list_lock_) { |
| 166 | if (thread == nullptr) { |
| 167 | return art::Thread::Current(); |
| 168 | } |
| 169 | |
| 170 | return art::Thread::FromManagedThread(soa, thread); |
| 171 | } |
| 172 | |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 173 | // Get the native thread. The spec says a null object denotes the current thread. |
| 174 | static art::Thread* GetNativeThread(jthread thread, |
| 175 | const art::ScopedObjectAccessAlreadyRunnable& soa) |
| 176 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 177 | if (thread == nullptr) { |
| 178 | return art::Thread::Current(); |
| 179 | } |
| 180 | |
| 181 | art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); |
| 182 | return art::Thread::FromManagedThread(soa, thread); |
| 183 | } |
| 184 | |
| 185 | jvmtiError ThreadUtil::GetThreadInfo(jvmtiEnv* env, jthread thread, jvmtiThreadInfo* info_ptr) { |
| 186 | if (info_ptr == nullptr) { |
| 187 | return ERR(NULL_POINTER); |
| 188 | } |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 189 | if (!PhaseUtil::IsLivePhase()) { |
| 190 | return JVMTI_ERROR_WRONG_PHASE; |
| 191 | } |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 192 | |
| 193 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 194 | |
| 195 | art::Thread* self = GetNativeThread(thread, soa); |
| 196 | if (self == nullptr && thread == nullptr) { |
| 197 | return ERR(INVALID_THREAD); |
| 198 | } |
| 199 | |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 200 | JvmtiUniquePtr<char[]> name_uptr; |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 201 | if (self != nullptr) { |
| 202 | // Have a native thread object, this thread is alive. |
| 203 | std::string name; |
| 204 | self->GetThreadName(name); |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 205 | jvmtiError name_result; |
| 206 | name_uptr = CopyString(env, name.c_str(), &name_result); |
| 207 | if (name_uptr == nullptr) { |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 208 | return name_result; |
| 209 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 210 | info_ptr->name = name_uptr.get(); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 211 | |
| 212 | info_ptr->priority = self->GetNativePriority(); |
| 213 | |
| 214 | info_ptr->is_daemon = self->IsDaemon(); |
| 215 | |
Andreas Gampe | 202f85a | 2017-02-06 10:23:26 -0800 | [diff] [blame] | 216 | art::ObjPtr<art::mirror::Object> peer = self->GetPeerFromOtherThread(); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 217 | |
| 218 | // ThreadGroup. |
| 219 | if (peer != nullptr) { |
| 220 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_group); |
| 221 | CHECK(f != nullptr); |
| 222 | art::ObjPtr<art::mirror::Object> group = f->GetObject(peer); |
| 223 | info_ptr->thread_group = group == nullptr |
| 224 | ? nullptr |
| 225 | : soa.AddLocalReference<jthreadGroup>(group); |
| 226 | } else { |
| 227 | info_ptr->thread_group = nullptr; |
| 228 | } |
| 229 | |
| 230 | // Context classloader. |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 231 | DCHECK(context_class_loader_ != nullptr); |
| 232 | art::ObjPtr<art::mirror::Object> ccl = peer != nullptr |
| 233 | ? context_class_loader_->GetObject(peer) |
| 234 | : nullptr; |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 235 | info_ptr->context_class_loader = ccl == nullptr |
| 236 | ? nullptr |
| 237 | : soa.AddLocalReference<jobject>(ccl); |
| 238 | } else { |
| 239 | // Only the peer. This thread has either not been started, or is dead. Read things from |
| 240 | // the Java side. |
| 241 | art::ObjPtr<art::mirror::Object> peer = soa.Decode<art::mirror::Object>(thread); |
| 242 | |
| 243 | // Name. |
| 244 | { |
| 245 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_name); |
| 246 | CHECK(f != nullptr); |
| 247 | art::ObjPtr<art::mirror::Object> name = f->GetObject(peer); |
| 248 | std::string name_cpp; |
| 249 | const char* name_cstr; |
| 250 | if (name != nullptr) { |
| 251 | name_cpp = name->AsString()->ToModifiedUtf8(); |
| 252 | name_cstr = name_cpp.c_str(); |
| 253 | } else { |
| 254 | name_cstr = ""; |
| 255 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 256 | jvmtiError name_result; |
| 257 | name_uptr = CopyString(env, name_cstr, &name_result); |
| 258 | if (name_uptr == nullptr) { |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 259 | return name_result; |
| 260 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 261 | info_ptr->name = name_uptr.get(); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | // Priority. |
| 265 | { |
| 266 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_priority); |
| 267 | CHECK(f != nullptr); |
| 268 | info_ptr->priority = static_cast<jint>(f->GetInt(peer)); |
| 269 | } |
| 270 | |
| 271 | // Daemon. |
| 272 | { |
| 273 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_daemon); |
| 274 | CHECK(f != nullptr); |
| 275 | info_ptr->is_daemon = f->GetBoolean(peer) == 0 ? JNI_FALSE : JNI_TRUE; |
| 276 | } |
| 277 | |
| 278 | // ThreadGroup. |
| 279 | { |
| 280 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_group); |
| 281 | CHECK(f != nullptr); |
| 282 | art::ObjPtr<art::mirror::Object> group = f->GetObject(peer); |
| 283 | info_ptr->thread_group = group == nullptr |
| 284 | ? nullptr |
| 285 | : soa.AddLocalReference<jthreadGroup>(group); |
| 286 | } |
| 287 | |
| 288 | // Context classloader. |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 289 | DCHECK(context_class_loader_ != nullptr); |
| 290 | art::ObjPtr<art::mirror::Object> ccl = peer != nullptr |
| 291 | ? context_class_loader_->GetObject(peer) |
| 292 | : nullptr; |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 293 | info_ptr->context_class_loader = ccl == nullptr |
| 294 | ? nullptr |
| 295 | : soa.AddLocalReference<jobject>(ccl); |
| 296 | } |
| 297 | |
| 298 | name_uptr.release(); |
| 299 | |
| 300 | return ERR(NONE); |
| 301 | } |
| 302 | |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 303 | // Return the thread's (or current thread, if null) thread state. Return kStarting in case |
| 304 | // there's no native counterpart (thread hasn't been started, yet, or is dead). |
| 305 | static art::ThreadState GetNativeThreadState(jthread thread, |
| 306 | const art::ScopedObjectAccessAlreadyRunnable& soa, |
| 307 | art::Thread** native_thread) |
| 308 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 309 | art::Thread* self = nullptr; |
| 310 | art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); |
| 311 | if (thread == nullptr) { |
| 312 | self = art::Thread::Current(); |
| 313 | } else { |
| 314 | self = art::Thread::FromManagedThread(soa, thread); |
| 315 | } |
| 316 | *native_thread = self; |
| 317 | if (self == nullptr || self->IsStillStarting()) { |
| 318 | return art::ThreadState::kStarting; |
| 319 | } |
| 320 | return self->GetState(); |
| 321 | } |
| 322 | |
| 323 | static jint GetJvmtiThreadStateFromInternal(art::ThreadState internal_thread_state) { |
| 324 | jint jvmti_state = JVMTI_THREAD_STATE_ALIVE; |
| 325 | |
| 326 | if (internal_thread_state == art::ThreadState::kSuspended) { |
| 327 | 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 | |
| 332 | if (internal_thread_state == art::ThreadState::kNative) { |
| 333 | jvmti_state |= JVMTI_THREAD_STATE_IN_NATIVE; |
| 334 | } |
| 335 | |
| 336 | if (internal_thread_state == art::ThreadState::kRunnable || |
| 337 | internal_thread_state == art::ThreadState::kWaitingWeakGcRootRead || |
| 338 | internal_thread_state == art::ThreadState::kSuspended) { |
| 339 | jvmti_state |= JVMTI_THREAD_STATE_RUNNABLE; |
| 340 | } else if (internal_thread_state == art::ThreadState::kBlocked) { |
| 341 | jvmti_state |= JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER; |
| 342 | } else { |
| 343 | // Should be in waiting state. |
| 344 | jvmti_state |= JVMTI_THREAD_STATE_WAITING; |
| 345 | |
| 346 | if (internal_thread_state == art::ThreadState::kTimedWaiting || |
| 347 | internal_thread_state == art::ThreadState::kSleeping) { |
| 348 | jvmti_state |= JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT; |
| 349 | } else { |
| 350 | jvmti_state |= JVMTI_THREAD_STATE_WAITING_INDEFINITELY; |
| 351 | } |
| 352 | |
| 353 | if (internal_thread_state == art::ThreadState::kSleeping) { |
| 354 | jvmti_state |= JVMTI_THREAD_STATE_SLEEPING; |
| 355 | } |
| 356 | |
| 357 | if (internal_thread_state == art::ThreadState::kTimedWaiting || |
| 358 | internal_thread_state == art::ThreadState::kWaiting) { |
| 359 | jvmti_state |= JVMTI_THREAD_STATE_IN_OBJECT_WAIT; |
| 360 | } |
| 361 | |
| 362 | // TODO: PARKED. We'll have to inspect the stack. |
| 363 | } |
| 364 | |
| 365 | return jvmti_state; |
| 366 | } |
| 367 | |
| 368 | static jint GetJavaStateFromInternal(art::ThreadState internal_thread_state) { |
| 369 | switch (internal_thread_state) { |
| 370 | case art::ThreadState::kTerminated: |
| 371 | return JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED; |
| 372 | |
| 373 | case art::ThreadState::kRunnable: |
| 374 | case art::ThreadState::kNative: |
| 375 | case art::ThreadState::kWaitingWeakGcRootRead: |
| 376 | case art::ThreadState::kSuspended: |
| 377 | return JVMTI_JAVA_LANG_THREAD_STATE_RUNNABLE; |
| 378 | |
| 379 | case art::ThreadState::kTimedWaiting: |
| 380 | case art::ThreadState::kSleeping: |
| 381 | return JVMTI_JAVA_LANG_THREAD_STATE_TIMED_WAITING; |
| 382 | |
| 383 | case art::ThreadState::kBlocked: |
| 384 | return JVMTI_JAVA_LANG_THREAD_STATE_BLOCKED; |
| 385 | |
| 386 | case art::ThreadState::kStarting: |
| 387 | return JVMTI_JAVA_LANG_THREAD_STATE_NEW; |
| 388 | |
| 389 | case art::ThreadState::kWaiting: |
| 390 | case art::ThreadState::kWaitingForGcToComplete: |
| 391 | case art::ThreadState::kWaitingPerformingGc: |
| 392 | case art::ThreadState::kWaitingForCheckPointsToRun: |
| 393 | case art::ThreadState::kWaitingForDebuggerSend: |
| 394 | case art::ThreadState::kWaitingForDebuggerToAttach: |
| 395 | case art::ThreadState::kWaitingInMainDebuggerLoop: |
| 396 | case art::ThreadState::kWaitingForDebuggerSuspension: |
| 397 | case art::ThreadState::kWaitingForDeoptimization: |
| 398 | case art::ThreadState::kWaitingForGetObjectsAllocated: |
| 399 | case art::ThreadState::kWaitingForJniOnLoad: |
| 400 | case art::ThreadState::kWaitingForSignalCatcherOutput: |
| 401 | case art::ThreadState::kWaitingInMainSignalCatcherLoop: |
| 402 | case art::ThreadState::kWaitingForMethodTracingStart: |
| 403 | case art::ThreadState::kWaitingForVisitObjects: |
| 404 | case art::ThreadState::kWaitingForGcThreadFlip: |
| 405 | return JVMTI_JAVA_LANG_THREAD_STATE_WAITING; |
| 406 | } |
| 407 | LOG(FATAL) << "Unreachable"; |
| 408 | UNREACHABLE(); |
| 409 | } |
| 410 | |
| 411 | jvmtiError ThreadUtil::GetThreadState(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 412 | jthread thread, |
| 413 | jint* thread_state_ptr) { |
| 414 | if (thread_state_ptr == nullptr) { |
| 415 | return ERR(NULL_POINTER); |
| 416 | } |
| 417 | |
| 418 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 419 | art::Thread* native_thread = nullptr; |
| 420 | art::ThreadState internal_thread_state = GetNativeThreadState(thread, soa, &native_thread); |
| 421 | |
| 422 | if (internal_thread_state == art::ThreadState::kStarting) { |
| 423 | if (thread == nullptr) { |
| 424 | // No native thread, and no Java thread? We must be starting up. Report as wrong phase. |
| 425 | return ERR(WRONG_PHASE); |
| 426 | } |
| 427 | |
| 428 | // Need to read the Java "started" field to know whether this is starting or terminated. |
| 429 | art::ObjPtr<art::mirror::Object> peer = soa.Decode<art::mirror::Object>(thread); |
| 430 | art::ObjPtr<art::mirror::Class> klass = peer->GetClass(); |
| 431 | art::ArtField* started_field = klass->FindDeclaredInstanceField("started", "Z"); |
| 432 | CHECK(started_field != nullptr); |
| 433 | bool started = started_field->GetBoolean(peer) != 0; |
| 434 | constexpr jint kStartedState = JVMTI_JAVA_LANG_THREAD_STATE_NEW; |
| 435 | constexpr jint kTerminatedState = JVMTI_THREAD_STATE_TERMINATED | |
| 436 | JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED; |
| 437 | *thread_state_ptr = started ? kTerminatedState : kStartedState; |
| 438 | return ERR(NONE); |
| 439 | } |
| 440 | DCHECK(native_thread != nullptr); |
| 441 | |
| 442 | // Translate internal thread state to JVMTI and Java state. |
| 443 | jint jvmti_state = GetJvmtiThreadStateFromInternal(internal_thread_state); |
| 444 | if (native_thread->IsInterrupted()) { |
| 445 | jvmti_state |= JVMTI_THREAD_STATE_INTERRUPTED; |
| 446 | } |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 447 | if (native_thread->IsSuspended()) { |
| 448 | jvmti_state |= JVMTI_THREAD_STATE_SUSPENDED; |
| 449 | } |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 450 | |
| 451 | // Java state is derived from nativeGetState. |
| 452 | // Note: Our implementation assigns "runnable" to suspended. As such, we will have slightly |
| 453 | // different mask. However, this is for consistency with the Java view. |
| 454 | jint java_state = GetJavaStateFromInternal(internal_thread_state); |
| 455 | |
| 456 | *thread_state_ptr = jvmti_state | java_state; |
| 457 | |
| 458 | return ERR(NONE); |
| 459 | } |
| 460 | |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 461 | jvmtiError ThreadUtil::GetAllThreads(jvmtiEnv* env, |
| 462 | jint* threads_count_ptr, |
| 463 | jthread** threads_ptr) { |
| 464 | if (threads_count_ptr == nullptr || threads_ptr == nullptr) { |
| 465 | return ERR(NULL_POINTER); |
| 466 | } |
| 467 | |
| 468 | art::Thread* current = art::Thread::Current(); |
| 469 | |
| 470 | art::ScopedObjectAccess soa(current); |
| 471 | |
| 472 | art::MutexLock mu(current, *art::Locks::thread_list_lock_); |
| 473 | std::list<art::Thread*> thread_list = art::Runtime::Current()->GetThreadList()->GetList(); |
| 474 | |
| 475 | std::vector<art::ObjPtr<art::mirror::Object>> peers; |
| 476 | |
| 477 | for (art::Thread* thread : thread_list) { |
| 478 | // Skip threads that are still starting. |
| 479 | if (thread->IsStillStarting()) { |
| 480 | continue; |
| 481 | } |
| 482 | |
Andreas Gampe | 202f85a | 2017-02-06 10:23:26 -0800 | [diff] [blame] | 483 | art::ObjPtr<art::mirror::Object> peer = thread->GetPeerFromOtherThread(); |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 484 | if (peer != nullptr) { |
| 485 | peers.push_back(peer); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | if (peers.empty()) { |
| 490 | *threads_count_ptr = 0; |
| 491 | *threads_ptr = nullptr; |
| 492 | } else { |
| 493 | unsigned char* data; |
| 494 | jvmtiError data_result = env->Allocate(peers.size() * sizeof(jthread), &data); |
| 495 | if (data_result != ERR(NONE)) { |
| 496 | return data_result; |
| 497 | } |
| 498 | jthread* threads = reinterpret_cast<jthread*>(data); |
| 499 | for (size_t i = 0; i != peers.size(); ++i) { |
| 500 | threads[i] = soa.AddLocalReference<jthread>(peers[i]); |
| 501 | } |
| 502 | |
| 503 | *threads_count_ptr = static_cast<jint>(peers.size()); |
| 504 | *threads_ptr = threads; |
| 505 | } |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 506 | return ERR(NONE); |
| 507 | } |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 508 | |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame^] | 509 | // The struct that we store in the art::Thread::custom_tls_ that maps the jvmtiEnvs to the data |
| 510 | // stored with that thread. This is needed since different jvmtiEnvs are not supposed to share TLS |
| 511 | // data but we only have a single slot in Thread objects to store data. |
| 512 | struct JvmtiGlobalTLSData { |
| 513 | std::unordered_map<jvmtiEnv*, const void*> data GUARDED_BY(art::Locks::thread_list_lock_); |
| 514 | }; |
| 515 | |
| 516 | static void RemoveTLSData(art::Thread* target, void* ctx) REQUIRES(art::Locks::thread_list_lock_) { |
| 517 | jvmtiEnv* env = reinterpret_cast<jvmtiEnv*>(ctx); |
| 518 | art::Locks::thread_list_lock_->AssertHeld(art::Thread::Current()); |
| 519 | JvmtiGlobalTLSData* global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS()); |
| 520 | if (global_tls != nullptr) { |
| 521 | global_tls->data.erase(env); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | void ThreadUtil::RemoveEnvironment(jvmtiEnv* env) { |
| 526 | art::Thread* self = art::Thread::Current(); |
| 527 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
| 528 | art::ThreadList* list = art::Runtime::Current()->GetThreadList(); |
| 529 | list->ForEach(RemoveTLSData, env); |
| 530 | } |
| 531 | |
| 532 | jvmtiError ThreadUtil::SetThreadLocalStorage(jvmtiEnv* env, jthread thread, const void* data) { |
| 533 | art::Thread* self = art::Thread::Current(); |
| 534 | art::ScopedObjectAccess soa(self); |
| 535 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
| 536 | art::Thread* target = GetNativeThreadLocked(thread, soa); |
| 537 | if (target == nullptr && thread == nullptr) { |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 538 | return ERR(INVALID_THREAD); |
| 539 | } |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame^] | 540 | if (target == nullptr) { |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 541 | return ERR(THREAD_NOT_ALIVE); |
| 542 | } |
| 543 | |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame^] | 544 | JvmtiGlobalTLSData* global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS()); |
| 545 | if (global_tls == nullptr) { |
| 546 | target->SetCustomTLS(new JvmtiGlobalTLSData); |
| 547 | global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS()); |
| 548 | } |
| 549 | |
| 550 | global_tls->data[env] = data; |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 551 | |
| 552 | return ERR(NONE); |
| 553 | } |
| 554 | |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame^] | 555 | jvmtiError ThreadUtil::GetThreadLocalStorage(jvmtiEnv* env, |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 556 | jthread thread, |
| 557 | void** data_ptr) { |
| 558 | if (data_ptr == nullptr) { |
| 559 | return ERR(NULL_POINTER); |
| 560 | } |
| 561 | |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame^] | 562 | art::Thread* self = art::Thread::Current(); |
| 563 | art::ScopedObjectAccess soa(self); |
| 564 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
| 565 | art::Thread* target = GetNativeThreadLocked(thread, soa); |
| 566 | if (target == nullptr && thread == nullptr) { |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 567 | return ERR(INVALID_THREAD); |
| 568 | } |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame^] | 569 | if (target == nullptr) { |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 570 | return ERR(THREAD_NOT_ALIVE); |
| 571 | } |
| 572 | |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame^] | 573 | JvmtiGlobalTLSData* global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS()); |
| 574 | if (global_tls == nullptr) { |
| 575 | *data_ptr = nullptr; |
| 576 | return OK; |
| 577 | } |
| 578 | auto it = global_tls->data.find(env); |
| 579 | if (it != global_tls->data.end()) { |
| 580 | *data_ptr = const_cast<void*>(it->second); |
| 581 | } else { |
| 582 | *data_ptr = nullptr; |
| 583 | } |
| 584 | |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 585 | return ERR(NONE); |
| 586 | } |
| 587 | |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 588 | struct AgentData { |
| 589 | const void* arg; |
| 590 | jvmtiStartFunction proc; |
| 591 | jthread thread; |
| 592 | JavaVM* java_vm; |
| 593 | jvmtiEnv* jvmti_env; |
| 594 | jint priority; |
| 595 | }; |
| 596 | |
| 597 | static void* AgentCallback(void* arg) { |
| 598 | std::unique_ptr<AgentData> data(reinterpret_cast<AgentData*>(arg)); |
| 599 | CHECK(data->thread != nullptr); |
| 600 | |
| 601 | // We already have a peer. So call our special Attach function. |
| 602 | art::Thread* self = art::Thread::Attach("JVMTI Agent thread", true, data->thread); |
| 603 | CHECK(self != nullptr); |
| 604 | // The name in Attach() is only for logging. Set the thread name. This is important so |
| 605 | // that the thread is no longer seen as starting up. |
| 606 | { |
| 607 | art::ScopedObjectAccess soa(self); |
| 608 | self->SetThreadName("JVMTI Agent thread"); |
| 609 | } |
| 610 | |
| 611 | // Release the peer. |
| 612 | JNIEnv* env = self->GetJniEnv(); |
| 613 | env->DeleteGlobalRef(data->thread); |
| 614 | data->thread = nullptr; |
| 615 | |
| 616 | // Run the agent code. |
| 617 | data->proc(data->jvmti_env, env, const_cast<void*>(data->arg)); |
| 618 | |
| 619 | // Detach the thread. |
| 620 | int detach_result = data->java_vm->DetachCurrentThread(); |
| 621 | CHECK_EQ(detach_result, 0); |
| 622 | |
| 623 | return nullptr; |
| 624 | } |
| 625 | |
| 626 | jvmtiError ThreadUtil::RunAgentThread(jvmtiEnv* jvmti_env, |
| 627 | jthread thread, |
| 628 | jvmtiStartFunction proc, |
| 629 | const void* arg, |
| 630 | jint priority) { |
| 631 | if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) { |
| 632 | return ERR(INVALID_PRIORITY); |
| 633 | } |
| 634 | JNIEnv* env = art::Thread::Current()->GetJniEnv(); |
| 635 | if (thread == nullptr || !env->IsInstanceOf(thread, art::WellKnownClasses::java_lang_Thread)) { |
| 636 | return ERR(INVALID_THREAD); |
| 637 | } |
| 638 | if (proc == nullptr) { |
| 639 | return ERR(NULL_POINTER); |
| 640 | } |
| 641 | |
| 642 | std::unique_ptr<AgentData> data(new AgentData); |
| 643 | data->arg = arg; |
| 644 | data->proc = proc; |
| 645 | // We need a global ref for Java objects, as local refs will be invalid. |
| 646 | data->thread = env->NewGlobalRef(thread); |
| 647 | data->java_vm = art::Runtime::Current()->GetJavaVM(); |
| 648 | data->jvmti_env = jvmti_env; |
| 649 | data->priority = priority; |
| 650 | |
| 651 | pthread_t pthread; |
| 652 | int pthread_create_result = pthread_create(&pthread, |
| 653 | nullptr, |
| 654 | &AgentCallback, |
| 655 | reinterpret_cast<void*>(data.get())); |
| 656 | if (pthread_create_result != 0) { |
| 657 | return ERR(INTERNAL); |
| 658 | } |
| 659 | data.release(); |
| 660 | |
| 661 | return ERR(NONE); |
| 662 | } |
| 663 | |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 664 | // Suspends the current thread if it has any suspend requests on it. |
| 665 | static void SuspendCheck(art::Thread* self) |
| 666 | REQUIRES(!art::Locks::mutator_lock_, !art::Locks::user_code_suspension_lock_) { |
| 667 | art::ScopedObjectAccess soa(self); |
| 668 | // Really this is only needed if we are in FastJNI and actually have the mutator_lock_ already. |
| 669 | self->FullSuspendCheck(); |
| 670 | } |
| 671 | |
| 672 | jvmtiError ThreadUtil::SuspendOther(art::Thread* self, |
| 673 | jthread target_jthread, |
| 674 | art::Thread* target) { |
| 675 | // Loop since we need to bail out and try again if we would end up getting suspended while holding |
| 676 | // the user_code_suspension_lock_ due to a SuspendReason::kForUserCode. In this situation we |
| 677 | // release the lock, wait to get resumed and try again. |
| 678 | do { |
| 679 | // Suspend ourself if we have any outstanding suspends. This is so we won't suspend due to |
| 680 | // another SuspendThread in the middle of suspending something else potentially causing a |
| 681 | // deadlock. We need to do this in the loop because if we ended up back here then we had |
| 682 | // outstanding SuspendReason::kForUserCode suspensions and we should wait for them to be cleared |
| 683 | // before continuing. |
| 684 | SuspendCheck(self); |
| 685 | art::MutexLock mu(self, *art::Locks::user_code_suspension_lock_); |
| 686 | { |
| 687 | art::MutexLock thread_list_mu(self, *art::Locks::thread_suspend_count_lock_); |
| 688 | // Make sure we won't be suspended in the middle of holding the thread_suspend_count_lock_ by |
| 689 | // a user-code suspension. We retry and do another SuspendCheck to clear this. |
| 690 | if (self->GetUserCodeSuspendCount() != 0) { |
| 691 | continue; |
| 692 | } else if (target->GetUserCodeSuspendCount() != 0) { |
| 693 | return ERR(THREAD_SUSPENDED); |
| 694 | } |
| 695 | } |
| 696 | bool timeout = true; |
| 697 | while (timeout) { |
| 698 | art::ThreadState state = target->GetState(); |
| 699 | if (state == art::ThreadState::kTerminated || state == art::ThreadState::kStarting) { |
| 700 | return ERR(THREAD_NOT_ALIVE); |
| 701 | } |
| 702 | target = art::Runtime::Current()->GetThreadList()->SuspendThreadByPeer( |
| 703 | target_jthread, |
| 704 | /* request_suspension */ true, |
| 705 | art::SuspendReason::kForUserCode, |
| 706 | &timeout); |
| 707 | if (target == nullptr && !timeout) { |
| 708 | // TODO It would be good to get more information about why exactly the thread failed to |
| 709 | // suspend. |
| 710 | return ERR(INTERNAL); |
| 711 | } |
| 712 | } |
| 713 | return OK; |
| 714 | } while (true); |
| 715 | UNREACHABLE(); |
| 716 | } |
| 717 | |
| 718 | jvmtiError ThreadUtil::SuspendSelf(art::Thread* self) { |
| 719 | CHECK(self == art::Thread::Current()); |
| 720 | { |
| 721 | art::MutexLock mu(self, *art::Locks::user_code_suspension_lock_); |
| 722 | art::MutexLock thread_list_mu(self, *art::Locks::thread_suspend_count_lock_); |
| 723 | if (self->GetUserCodeSuspendCount() != 0) { |
| 724 | // This can only happen if we race with another thread to suspend 'self' and we lose. |
| 725 | return ERR(THREAD_SUSPENDED); |
| 726 | } |
| 727 | // We shouldn't be able to fail this. |
| 728 | if (!self->ModifySuspendCount(self, +1, nullptr, art::SuspendReason::kForUserCode)) { |
| 729 | // TODO More specific error would be nice. |
| 730 | return ERR(INTERNAL); |
| 731 | } |
| 732 | } |
| 733 | // Once we have requested the suspend we actually go to sleep. We need to do this after releasing |
| 734 | // the suspend_lock to make sure we can be woken up. This call gains the mutator lock causing us |
| 735 | // to go to sleep until we are resumed. |
| 736 | SuspendCheck(self); |
| 737 | return OK; |
| 738 | } |
| 739 | |
| 740 | jvmtiError ThreadUtil::SuspendThread(jvmtiEnv* env ATTRIBUTE_UNUSED, jthread thread) { |
| 741 | art::Thread* self = art::Thread::Current(); |
| 742 | art::Thread* target; |
| 743 | { |
| 744 | art::ScopedObjectAccess soa(self); |
| 745 | target = GetNativeThread(thread, soa); |
| 746 | } |
| 747 | if (target == nullptr) { |
| 748 | return ERR(INVALID_THREAD); |
| 749 | } |
| 750 | if (target == self) { |
| 751 | return SuspendSelf(self); |
| 752 | } else { |
| 753 | return SuspendOther(self, thread, target); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | jvmtiError ThreadUtil::ResumeThread(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 758 | jthread thread) { |
| 759 | if (thread == nullptr) { |
| 760 | return ERR(NULL_POINTER); |
| 761 | } |
| 762 | art::Thread* self = art::Thread::Current(); |
| 763 | art::Thread* target; |
| 764 | { |
| 765 | // NB This does a SuspendCheck (during thread state change) so we need to make sure we don't |
| 766 | // have the 'suspend_lock' locked here. |
| 767 | art::ScopedObjectAccess soa(self); |
| 768 | target = GetNativeThread(thread, soa); |
| 769 | } |
| 770 | if (target == nullptr) { |
| 771 | return ERR(INVALID_THREAD); |
| 772 | } else if (target == self) { |
| 773 | // We would have paused until we aren't suspended anymore due to the ScopedObjectAccess so we |
| 774 | // can just return THREAD_NOT_SUSPENDED. Unfortunately we cannot do any real DCHECKs about |
| 775 | // current state since it's all concurrent. |
| 776 | return ERR(THREAD_NOT_SUSPENDED); |
| 777 | } |
| 778 | // Now that we know we aren't getting suspended ourself (since we have a mutator lock) we lock the |
| 779 | // suspend_lock to start suspending. |
| 780 | art::MutexLock mu(self, *art::Locks::user_code_suspension_lock_); |
| 781 | { |
| 782 | // The JVMTI spec requires us to return THREAD_NOT_SUSPENDED if it is alive but we really cannot |
| 783 | // tell why resume failed. |
| 784 | art::MutexLock thread_list_mu(self, *art::Locks::thread_suspend_count_lock_); |
| 785 | if (target->GetUserCodeSuspendCount() == 0) { |
| 786 | return ERR(THREAD_NOT_SUSPENDED); |
| 787 | } |
| 788 | } |
| 789 | if (target->GetState() == art::ThreadState::kTerminated) { |
| 790 | return ERR(THREAD_NOT_ALIVE); |
| 791 | } |
| 792 | DCHECK(target != self); |
| 793 | if (!art::Runtime::Current()->GetThreadList()->Resume(target, art::SuspendReason::kForUserCode)) { |
| 794 | // TODO Give a better error. |
| 795 | // This is most likely THREAD_NOT_SUSPENDED but we cannot really be sure. |
| 796 | return ERR(INTERNAL); |
| 797 | } |
| 798 | return OK; |
| 799 | } |
| 800 | |
| 801 | // Suspends all the threads in the list at the same time. Getting this behavior is a little tricky |
| 802 | // since we can have threads in the list multiple times. This generally doesn't matter unless the |
| 803 | // current thread is present multiple times. In that case we need to suspend only once and either |
| 804 | // return the same error code in all the other slots if it failed or return ERR(THREAD_SUSPENDED) if |
| 805 | // it didn't. We also want to handle the current thread last to make the behavior of the code |
| 806 | // simpler to understand. |
| 807 | jvmtiError ThreadUtil::SuspendThreadList(jvmtiEnv* env, |
| 808 | jint request_count, |
| 809 | const jthread* threads, |
| 810 | jvmtiError* results) { |
| 811 | if (request_count == 0) { |
| 812 | return ERR(ILLEGAL_ARGUMENT); |
| 813 | } else if (results == nullptr || threads == nullptr) { |
| 814 | return ERR(NULL_POINTER); |
| 815 | } |
| 816 | // This is the list of the indexes in 'threads' and 'results' that correspond to the currently |
| 817 | // running thread. These indexes we need to handle specially since we need to only actually |
| 818 | // suspend a single time. |
| 819 | std::vector<jint> current_thread_indexes; |
| 820 | art::Thread* self = art::Thread::Current(); |
| 821 | for (jint i = 0; i < request_count; i++) { |
| 822 | { |
| 823 | art::ScopedObjectAccess soa(self); |
| 824 | if (threads[i] == nullptr || GetNativeThread(threads[i], soa) == self) { |
| 825 | current_thread_indexes.push_back(i); |
| 826 | continue; |
| 827 | } |
| 828 | } |
| 829 | results[i] = env->SuspendThread(threads[i]); |
| 830 | } |
| 831 | if (!current_thread_indexes.empty()) { |
| 832 | jint first_current_thread_index = current_thread_indexes[0]; |
| 833 | // Suspend self. |
| 834 | jvmtiError res = env->SuspendThread(threads[first_current_thread_index]); |
| 835 | results[first_current_thread_index] = res; |
| 836 | // Fill in the rest of the error values as appropriate. |
| 837 | jvmtiError other_results = (res != OK) ? res : ERR(THREAD_SUSPENDED); |
| 838 | for (auto it = ++current_thread_indexes.begin(); it != current_thread_indexes.end(); ++it) { |
| 839 | results[*it] = other_results; |
| 840 | } |
| 841 | } |
| 842 | return OK; |
| 843 | } |
| 844 | |
| 845 | jvmtiError ThreadUtil::ResumeThreadList(jvmtiEnv* env, |
| 846 | jint request_count, |
| 847 | const jthread* threads, |
| 848 | jvmtiError* results) { |
| 849 | if (request_count == 0) { |
| 850 | return ERR(ILLEGAL_ARGUMENT); |
| 851 | } else if (results == nullptr || threads == nullptr) { |
| 852 | return ERR(NULL_POINTER); |
| 853 | } |
| 854 | for (jint i = 0; i < request_count; i++) { |
| 855 | results[i] = env->ResumeThread(threads[i]); |
| 856 | } |
| 857 | return OK; |
| 858 | } |
| 859 | |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 860 | } // namespace openjdkjvmti |