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" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 46 | #include "nativehelper/ScopedLocalRef.h" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 47 | #include "obj_ptr.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" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 50 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 51 | #include "thread-current-inl.h" |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 52 | #include "thread_list.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 53 | #include "ti_phase.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 | |
Alex Light | 1d8a974 | 2017-08-17 11:12:06 -0700 | [diff] [blame] | 60 | struct ThreadCallback : public art::ThreadLifecycleCallback { |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 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 | } |
Alex Light | 1d8a974 | 2017-08-17 11:12:06 -0700 | [diff] [blame] | 67 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 68 | template <ArtJvmtiEvent kEvent> |
| 69 | void Post(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 70 | DCHECK_EQ(self, art::Thread::Current()); |
| 71 | ScopedLocalRef<jthread> thread(self->GetJniEnv(), GetThreadObject(self)); |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 72 | art::ScopedThreadSuspension sts(self, art::ThreadState::kNative); |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 73 | event_handler->DispatchEvent<kEvent>(self, |
| 74 | reinterpret_cast<JNIEnv*>(self->GetJniEnv()), |
| 75 | thread.get()); |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 76 | } |
| 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 Light | 5bd0954 | 2017-02-09 16:01:32 -0800 | [diff] [blame] | 85 | if (name != "JDWP" && |
| 86 | name != "Signal Catcher" && |
| 87 | !android::base::StartsWith(name, "Jit thread pool")) { |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame^] | 88 | LOG(FATAL) << "Unexpected thread before start: " << name << " id: " |
| 89 | << self->GetThreadId(); |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | return; |
| 93 | } |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 94 | Post<ArtJvmtiEvent::kThreadStart>(self); |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void ThreadDeath(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 98 | Post<ArtJvmtiEvent::kThreadEnd>(self); |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 101 | EventHandler* event_handler = nullptr; |
| 102 | bool started = false; |
| 103 | }; |
| 104 | |
| 105 | ThreadCallback gThreadCallback; |
| 106 | |
| 107 | void 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 Light | 1d8a974 | 2017-08-17 11:12:06 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void 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 Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 127 | void ThreadUtil::CacheData() { |
Alex Light | 1d8a974 | 2017-08-17 11:12:06 -0700 | [diff] [blame] | 128 | // We must have started since it is now safe to cache our data; |
| 129 | gThreadCallback.started = true; |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 130 | 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 Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 139 | void 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 Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 147 | jvmtiError 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 Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 163 | // Get the native thread. The spec says a null object denotes the current thread. |
Alex Light | bebd7bd | 2017-07-25 14:05:52 -0700 | [diff] [blame] | 164 | art::Thread* ThreadUtil::GetNativeThread(jthread thread, |
| 165 | const art::ScopedObjectAccessAlreadyRunnable& soa) { |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 166 | if (thread == nullptr) { |
| 167 | return art::Thread::Current(); |
| 168 | } |
| 169 | |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 170 | return art::Thread::FromManagedThread(soa, thread); |
| 171 | } |
| 172 | |
| 173 | jvmtiError ThreadUtil::GetThreadInfo(jvmtiEnv* env, jthread thread, jvmtiThreadInfo* info_ptr) { |
| 174 | if (info_ptr == nullptr) { |
| 175 | return ERR(NULL_POINTER); |
| 176 | } |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 177 | if (!PhaseUtil::IsLivePhase()) { |
| 178 | return JVMTI_ERROR_WRONG_PHASE; |
| 179 | } |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 180 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 181 | art::Thread* self = art::Thread::Current(); |
| 182 | art::ScopedObjectAccess soa(self); |
| 183 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 184 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 185 | art::Thread* target = GetNativeThread(thread, soa); |
| 186 | if (target == nullptr && thread == nullptr) { |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 187 | return ERR(INVALID_THREAD); |
| 188 | } |
| 189 | |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 190 | JvmtiUniquePtr<char[]> name_uptr; |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 191 | if (target != nullptr) { |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 192 | // Have a native thread object, this thread is alive. |
| 193 | std::string name; |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 194 | target->GetThreadName(name); |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 195 | jvmtiError name_result; |
| 196 | name_uptr = CopyString(env, name.c_str(), &name_result); |
| 197 | if (name_uptr == nullptr) { |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 198 | return name_result; |
| 199 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 200 | info_ptr->name = name_uptr.get(); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 201 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 202 | info_ptr->priority = target->GetNativePriority(); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 203 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 204 | info_ptr->is_daemon = target->IsDaemon(); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 205 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 206 | art::ObjPtr<art::mirror::Object> peer = target->GetPeerFromOtherThread(); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 207 | |
| 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 Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 221 | DCHECK(context_class_loader_ != nullptr); |
| 222 | art::ObjPtr<art::mirror::Object> ccl = peer != nullptr |
| 223 | ? context_class_loader_->GetObject(peer) |
| 224 | : nullptr; |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 225 | 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 Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 246 | jvmtiError name_result; |
| 247 | name_uptr = CopyString(env, name_cstr, &name_result); |
| 248 | if (name_uptr == nullptr) { |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 249 | return name_result; |
| 250 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 251 | info_ptr->name = name_uptr.get(); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 252 | } |
| 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 Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 279 | DCHECK(context_class_loader_ != nullptr); |
| 280 | art::ObjPtr<art::mirror::Object> ccl = peer != nullptr |
| 281 | ? context_class_loader_->GetObject(peer) |
| 282 | : nullptr; |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 283 | 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 Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 293 | struct 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. |
| 300 | static InternalThreadState GetNativeThreadState(jthread thread, |
| 301 | const art::ScopedObjectAccessAlreadyRunnable& soa) |
| 302 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 303 | REQUIRES(art::Locks::thread_list_lock_, art::Locks::user_code_suspension_lock_) { |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 304 | art::Thread* self = nullptr; |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 305 | if (thread == nullptr) { |
| 306 | self = art::Thread::Current(); |
| 307 | } else { |
| 308 | self = art::Thread::FromManagedThread(soa, thread); |
| 309 | } |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 310 | InternalThreadState thread_state = {}; |
| 311 | art::MutexLock tscl_mu(soa.Self(), *art::Locks::thread_suspend_count_lock_); |
| 312 | thread_state.native_thread = self; |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 313 | if (self == nullptr || self->IsStillStarting()) { |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 314 | 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 Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 319 | } |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 320 | return thread_state; |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 323 | static jint GetJvmtiThreadStateFromInternal(const InternalThreadState& state) { |
| 324 | art::ThreadState internal_thread_state = state.art_state; |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 325 | jint jvmti_state = JVMTI_THREAD_STATE_ALIVE; |
| 326 | |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 327 | if (state.thread_user_code_suspend_count != 0) { |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 328 | 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 Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 333 | if (state.native_thread->IsInterrupted()) { |
| 334 | jvmti_state |= JVMTI_THREAD_STATE_INTERRUPTED; |
| 335 | } |
| 336 | |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 337 | 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 Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 373 | static jint GetJavaStateFromInternal(const InternalThreadState& state) { |
| 374 | switch (state.art_state) { |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 375 | 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: |
| 395 | case art::ThreadState::kWaitingForGcToComplete: |
| 396 | case art::ThreadState::kWaitingPerformingGc: |
| 397 | case art::ThreadState::kWaitingForCheckPointsToRun: |
| 398 | case art::ThreadState::kWaitingForDebuggerSend: |
| 399 | case art::ThreadState::kWaitingForDebuggerToAttach: |
| 400 | case art::ThreadState::kWaitingInMainDebuggerLoop: |
| 401 | case art::ThreadState::kWaitingForDebuggerSuspension: |
| 402 | case art::ThreadState::kWaitingForDeoptimization: |
| 403 | case art::ThreadState::kWaitingForGetObjectsAllocated: |
| 404 | case art::ThreadState::kWaitingForJniOnLoad: |
| 405 | case art::ThreadState::kWaitingForSignalCatcherOutput: |
| 406 | case art::ThreadState::kWaitingInMainSignalCatcherLoop: |
| 407 | case art::ThreadState::kWaitingForMethodTracingStart: |
| 408 | case art::ThreadState::kWaitingForVisitObjects: |
| 409 | case art::ThreadState::kWaitingForGcThreadFlip: |
| 410 | return JVMTI_JAVA_LANG_THREAD_STATE_WAITING; |
| 411 | } |
| 412 | LOG(FATAL) << "Unreachable"; |
| 413 | UNREACHABLE(); |
| 414 | } |
| 415 | |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 416 | // Suspends the current thread if it has any suspend requests on it. |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame^] | 417 | void ThreadUtil::SuspendCheck(art::Thread* self) { |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 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 | |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame^] | 423 | bool ThreadUtil::WouldSuspendForUserCodeLocked(art::Thread* self) { |
| 424 | DCHECK(self == art::Thread::Current()); |
| 425 | art::MutexLock tscl_mu(self, *art::Locks::thread_suspend_count_lock_); |
| 426 | return self->GetUserCodeSuspendCount() != 0; |
| 427 | } |
| 428 | |
| 429 | bool ThreadUtil::WouldSuspendForUserCode(art::Thread* self) { |
| 430 | DCHECK(self == art::Thread::Current()); |
| 431 | art::MutexLock ucsl_mu(self, *art::Locks::user_code_suspension_lock_); |
| 432 | return WouldSuspendForUserCodeLocked(self); |
| 433 | } |
| 434 | |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 435 | jvmtiError ThreadUtil::GetThreadState(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 436 | jthread thread, |
| 437 | jint* thread_state_ptr) { |
| 438 | if (thread_state_ptr == nullptr) { |
| 439 | return ERR(NULL_POINTER); |
| 440 | } |
| 441 | |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 442 | art::Thread* self = art::Thread::Current(); |
| 443 | InternalThreadState state = {}; |
| 444 | // Loop since we need to bail out and try again if we would end up getting suspended while holding |
| 445 | // the user_code_suspension_lock_ due to a SuspendReason::kForUserCode. In this situation we |
| 446 | // release the lock, wait to get resumed and try again. |
| 447 | do { |
| 448 | SuspendCheck(self); |
| 449 | art::MutexLock ucsl_mu(self, *art::Locks::user_code_suspension_lock_); |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame^] | 450 | if (WouldSuspendForUserCodeLocked(self)) { |
| 451 | // Make sure we won't be suspended in the middle of holding the thread_suspend_count_lock_ by |
| 452 | // a user-code suspension. We retry and do another SuspendCheck to clear this. |
| 453 | continue; |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 454 | } |
| 455 | art::ScopedObjectAccess soa(self); |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 456 | art::MutexLock tll_mu(self, *art::Locks::thread_list_lock_); |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 457 | state = GetNativeThreadState(thread, soa); |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 458 | if (state.art_state == art::ThreadState::kStarting) { |
| 459 | break; |
| 460 | } |
| 461 | DCHECK(state.native_thread != nullptr); |
| 462 | |
| 463 | // Translate internal thread state to JVMTI and Java state. |
| 464 | jint jvmti_state = GetJvmtiThreadStateFromInternal(state); |
| 465 | |
| 466 | // Java state is derived from nativeGetState. |
| 467 | // TODO: Our implementation assigns "runnable" to suspended. As such, we will have slightly |
| 468 | // different mask if a thread got suspended due to user-code. However, this is for |
| 469 | // consistency with the Java view. |
| 470 | jint java_state = GetJavaStateFromInternal(state); |
| 471 | |
| 472 | *thread_state_ptr = jvmti_state | java_state; |
| 473 | |
| 474 | return ERR(NONE); |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 475 | } while (true); |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 476 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 477 | DCHECK_EQ(state.art_state, art::ThreadState::kStarting); |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 478 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 479 | if (thread == nullptr) { |
| 480 | // No native thread, and no Java thread? We must be starting up. Report as wrong phase. |
| 481 | return ERR(WRONG_PHASE); |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 482 | } |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 483 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 484 | art::ScopedObjectAccess soa(self); |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 485 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 486 | // Need to read the Java "started" field to know whether this is starting or terminated. |
| 487 | art::ObjPtr<art::mirror::Object> peer = soa.Decode<art::mirror::Object>(thread); |
| 488 | art::ObjPtr<art::mirror::Class> klass = peer->GetClass(); |
| 489 | art::ArtField* started_field = klass->FindDeclaredInstanceField("started", "Z"); |
| 490 | CHECK(started_field != nullptr); |
| 491 | bool started = started_field->GetBoolean(peer) != 0; |
| 492 | constexpr jint kStartedState = JVMTI_JAVA_LANG_THREAD_STATE_NEW; |
| 493 | constexpr jint kTerminatedState = JVMTI_THREAD_STATE_TERMINATED | |
| 494 | JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED; |
| 495 | *thread_state_ptr = started ? kTerminatedState : kStartedState; |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 496 | return ERR(NONE); |
| 497 | } |
| 498 | |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 499 | jvmtiError ThreadUtil::GetAllThreads(jvmtiEnv* env, |
| 500 | jint* threads_count_ptr, |
| 501 | jthread** threads_ptr) { |
| 502 | if (threads_count_ptr == nullptr || threads_ptr == nullptr) { |
| 503 | return ERR(NULL_POINTER); |
| 504 | } |
| 505 | |
| 506 | art::Thread* current = art::Thread::Current(); |
| 507 | |
| 508 | art::ScopedObjectAccess soa(current); |
| 509 | |
| 510 | art::MutexLock mu(current, *art::Locks::thread_list_lock_); |
| 511 | std::list<art::Thread*> thread_list = art::Runtime::Current()->GetThreadList()->GetList(); |
| 512 | |
| 513 | std::vector<art::ObjPtr<art::mirror::Object>> peers; |
| 514 | |
| 515 | for (art::Thread* thread : thread_list) { |
| 516 | // Skip threads that are still starting. |
| 517 | if (thread->IsStillStarting()) { |
| 518 | continue; |
| 519 | } |
| 520 | |
Andreas Gampe | 202f85a | 2017-02-06 10:23:26 -0800 | [diff] [blame] | 521 | art::ObjPtr<art::mirror::Object> peer = thread->GetPeerFromOtherThread(); |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 522 | if (peer != nullptr) { |
| 523 | peers.push_back(peer); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | if (peers.empty()) { |
| 528 | *threads_count_ptr = 0; |
| 529 | *threads_ptr = nullptr; |
| 530 | } else { |
| 531 | unsigned char* data; |
| 532 | jvmtiError data_result = env->Allocate(peers.size() * sizeof(jthread), &data); |
| 533 | if (data_result != ERR(NONE)) { |
| 534 | return data_result; |
| 535 | } |
| 536 | jthread* threads = reinterpret_cast<jthread*>(data); |
| 537 | for (size_t i = 0; i != peers.size(); ++i) { |
| 538 | threads[i] = soa.AddLocalReference<jthread>(peers[i]); |
| 539 | } |
| 540 | |
| 541 | *threads_count_ptr = static_cast<jint>(peers.size()); |
| 542 | *threads_ptr = threads; |
| 543 | } |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 544 | return ERR(NONE); |
| 545 | } |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 546 | |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 547 | // The struct that we store in the art::Thread::custom_tls_ that maps the jvmtiEnvs to the data |
| 548 | // stored with that thread. This is needed since different jvmtiEnvs are not supposed to share TLS |
| 549 | // data but we only have a single slot in Thread objects to store data. |
| 550 | struct JvmtiGlobalTLSData { |
| 551 | std::unordered_map<jvmtiEnv*, const void*> data GUARDED_BY(art::Locks::thread_list_lock_); |
| 552 | }; |
| 553 | |
| 554 | static void RemoveTLSData(art::Thread* target, void* ctx) REQUIRES(art::Locks::thread_list_lock_) { |
| 555 | jvmtiEnv* env = reinterpret_cast<jvmtiEnv*>(ctx); |
| 556 | art::Locks::thread_list_lock_->AssertHeld(art::Thread::Current()); |
| 557 | JvmtiGlobalTLSData* global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS()); |
| 558 | if (global_tls != nullptr) { |
| 559 | global_tls->data.erase(env); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | void ThreadUtil::RemoveEnvironment(jvmtiEnv* env) { |
| 564 | art::Thread* self = art::Thread::Current(); |
| 565 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
| 566 | art::ThreadList* list = art::Runtime::Current()->GetThreadList(); |
| 567 | list->ForEach(RemoveTLSData, env); |
| 568 | } |
| 569 | |
| 570 | jvmtiError ThreadUtil::SetThreadLocalStorage(jvmtiEnv* env, jthread thread, const void* data) { |
| 571 | art::Thread* self = art::Thread::Current(); |
| 572 | art::ScopedObjectAccess soa(self); |
| 573 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 574 | art::Thread* target = GetNativeThread(thread, soa); |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 575 | if (target == nullptr && thread == nullptr) { |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 576 | return ERR(INVALID_THREAD); |
| 577 | } |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 578 | if (target == nullptr) { |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 579 | return ERR(THREAD_NOT_ALIVE); |
| 580 | } |
| 581 | |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 582 | JvmtiGlobalTLSData* global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS()); |
| 583 | if (global_tls == nullptr) { |
| 584 | target->SetCustomTLS(new JvmtiGlobalTLSData); |
| 585 | global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS()); |
| 586 | } |
| 587 | |
| 588 | global_tls->data[env] = data; |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 589 | |
| 590 | return ERR(NONE); |
| 591 | } |
| 592 | |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 593 | jvmtiError ThreadUtil::GetThreadLocalStorage(jvmtiEnv* env, |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 594 | jthread thread, |
| 595 | void** data_ptr) { |
| 596 | if (data_ptr == nullptr) { |
| 597 | return ERR(NULL_POINTER); |
| 598 | } |
| 599 | |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 600 | art::Thread* self = art::Thread::Current(); |
| 601 | art::ScopedObjectAccess soa(self); |
| 602 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 603 | art::Thread* target = GetNativeThread(thread, soa); |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 604 | if (target == nullptr && thread == nullptr) { |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 605 | return ERR(INVALID_THREAD); |
| 606 | } |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 607 | if (target == nullptr) { |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 608 | return ERR(THREAD_NOT_ALIVE); |
| 609 | } |
| 610 | |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 611 | JvmtiGlobalTLSData* global_tls = reinterpret_cast<JvmtiGlobalTLSData*>(target->GetCustomTLS()); |
| 612 | if (global_tls == nullptr) { |
| 613 | *data_ptr = nullptr; |
| 614 | return OK; |
| 615 | } |
| 616 | auto it = global_tls->data.find(env); |
| 617 | if (it != global_tls->data.end()) { |
| 618 | *data_ptr = const_cast<void*>(it->second); |
| 619 | } else { |
| 620 | *data_ptr = nullptr; |
| 621 | } |
| 622 | |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 623 | return ERR(NONE); |
| 624 | } |
| 625 | |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 626 | struct AgentData { |
| 627 | const void* arg; |
| 628 | jvmtiStartFunction proc; |
| 629 | jthread thread; |
| 630 | JavaVM* java_vm; |
| 631 | jvmtiEnv* jvmti_env; |
| 632 | jint priority; |
| 633 | }; |
| 634 | |
| 635 | static void* AgentCallback(void* arg) { |
| 636 | std::unique_ptr<AgentData> data(reinterpret_cast<AgentData*>(arg)); |
| 637 | CHECK(data->thread != nullptr); |
| 638 | |
| 639 | // We already have a peer. So call our special Attach function. |
| 640 | art::Thread* self = art::Thread::Attach("JVMTI Agent thread", true, data->thread); |
| 641 | CHECK(self != nullptr); |
| 642 | // The name in Attach() is only for logging. Set the thread name. This is important so |
| 643 | // that the thread is no longer seen as starting up. |
| 644 | { |
| 645 | art::ScopedObjectAccess soa(self); |
| 646 | self->SetThreadName("JVMTI Agent thread"); |
| 647 | } |
| 648 | |
| 649 | // Release the peer. |
| 650 | JNIEnv* env = self->GetJniEnv(); |
| 651 | env->DeleteGlobalRef(data->thread); |
| 652 | data->thread = nullptr; |
| 653 | |
| 654 | // Run the agent code. |
| 655 | data->proc(data->jvmti_env, env, const_cast<void*>(data->arg)); |
| 656 | |
| 657 | // Detach the thread. |
| 658 | int detach_result = data->java_vm->DetachCurrentThread(); |
| 659 | CHECK_EQ(detach_result, 0); |
| 660 | |
| 661 | return nullptr; |
| 662 | } |
| 663 | |
| 664 | jvmtiError ThreadUtil::RunAgentThread(jvmtiEnv* jvmti_env, |
| 665 | jthread thread, |
| 666 | jvmtiStartFunction proc, |
| 667 | const void* arg, |
| 668 | jint priority) { |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame^] | 669 | if (!PhaseUtil::IsLivePhase()) { |
| 670 | return ERR(WRONG_PHASE); |
| 671 | } |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 672 | if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) { |
| 673 | return ERR(INVALID_PRIORITY); |
| 674 | } |
| 675 | JNIEnv* env = art::Thread::Current()->GetJniEnv(); |
| 676 | if (thread == nullptr || !env->IsInstanceOf(thread, art::WellKnownClasses::java_lang_Thread)) { |
| 677 | return ERR(INVALID_THREAD); |
| 678 | } |
| 679 | if (proc == nullptr) { |
| 680 | return ERR(NULL_POINTER); |
| 681 | } |
| 682 | |
| 683 | std::unique_ptr<AgentData> data(new AgentData); |
| 684 | data->arg = arg; |
| 685 | data->proc = proc; |
| 686 | // We need a global ref for Java objects, as local refs will be invalid. |
| 687 | data->thread = env->NewGlobalRef(thread); |
| 688 | data->java_vm = art::Runtime::Current()->GetJavaVM(); |
| 689 | data->jvmti_env = jvmti_env; |
| 690 | data->priority = priority; |
| 691 | |
| 692 | pthread_t pthread; |
| 693 | int pthread_create_result = pthread_create(&pthread, |
| 694 | nullptr, |
| 695 | &AgentCallback, |
| 696 | reinterpret_cast<void*>(data.get())); |
| 697 | if (pthread_create_result != 0) { |
| 698 | return ERR(INTERNAL); |
| 699 | } |
| 700 | data.release(); |
| 701 | |
| 702 | return ERR(NONE); |
| 703 | } |
| 704 | |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 705 | jvmtiError ThreadUtil::SuspendOther(art::Thread* self, |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 706 | jthread target_jthread) { |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 707 | // Loop since we need to bail out and try again if we would end up getting suspended while holding |
| 708 | // the user_code_suspension_lock_ due to a SuspendReason::kForUserCode. In this situation we |
| 709 | // release the lock, wait to get resumed and try again. |
| 710 | do { |
| 711 | // Suspend ourself if we have any outstanding suspends. This is so we won't suspend due to |
| 712 | // another SuspendThread in the middle of suspending something else potentially causing a |
| 713 | // deadlock. We need to do this in the loop because if we ended up back here then we had |
| 714 | // outstanding SuspendReason::kForUserCode suspensions and we should wait for them to be cleared |
| 715 | // before continuing. |
| 716 | SuspendCheck(self); |
| 717 | art::MutexLock mu(self, *art::Locks::user_code_suspension_lock_); |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame^] | 718 | if (WouldSuspendForUserCodeLocked(self)) { |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 719 | // Make sure we won't be suspended in the middle of holding the thread_suspend_count_lock_ by |
| 720 | // a user-code suspension. We retry and do another SuspendCheck to clear this. |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame^] | 721 | continue; |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 722 | } |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame^] | 723 | // We are not going to be suspended by user code from now on. |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 724 | { |
| 725 | art::ScopedObjectAccess soa(self); |
| 726 | art::MutexLock thread_list_mu(self, *art::Locks::thread_list_lock_); |
| 727 | art::Thread* target = GetNativeThread(target_jthread, soa); |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 728 | art::ThreadState state = target->GetState(); |
| 729 | if (state == art::ThreadState::kTerminated || state == art::ThreadState::kStarting) { |
| 730 | return ERR(THREAD_NOT_ALIVE); |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 731 | } else { |
| 732 | art::MutexLock thread_suspend_count_mu(self, *art::Locks::thread_suspend_count_lock_); |
| 733 | if (target->GetUserCodeSuspendCount() != 0) { |
| 734 | return ERR(THREAD_SUSPENDED); |
| 735 | } |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 736 | } |
| 737 | } |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 738 | bool timeout = true; |
| 739 | art::Thread* ret_target = art::Runtime::Current()->GetThreadList()->SuspendThreadByPeer( |
| 740 | target_jthread, |
| 741 | /* request_suspension */ true, |
| 742 | art::SuspendReason::kForUserCode, |
| 743 | &timeout); |
| 744 | if (ret_target == nullptr && !timeout) { |
| 745 | // TODO It would be good to get more information about why exactly the thread failed to |
| 746 | // suspend. |
| 747 | return ERR(INTERNAL); |
| 748 | } else if (!timeout) { |
| 749 | // we didn't time out and got a result. |
| 750 | return OK; |
| 751 | } |
| 752 | // We timed out. Just go around and try again. |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 753 | } while (true); |
| 754 | UNREACHABLE(); |
| 755 | } |
| 756 | |
| 757 | jvmtiError ThreadUtil::SuspendSelf(art::Thread* self) { |
| 758 | CHECK(self == art::Thread::Current()); |
| 759 | { |
| 760 | art::MutexLock mu(self, *art::Locks::user_code_suspension_lock_); |
| 761 | art::MutexLock thread_list_mu(self, *art::Locks::thread_suspend_count_lock_); |
| 762 | if (self->GetUserCodeSuspendCount() != 0) { |
| 763 | // This can only happen if we race with another thread to suspend 'self' and we lose. |
| 764 | return ERR(THREAD_SUSPENDED); |
| 765 | } |
| 766 | // We shouldn't be able to fail this. |
| 767 | if (!self->ModifySuspendCount(self, +1, nullptr, art::SuspendReason::kForUserCode)) { |
| 768 | // TODO More specific error would be nice. |
| 769 | return ERR(INTERNAL); |
| 770 | } |
| 771 | } |
| 772 | // Once we have requested the suspend we actually go to sleep. We need to do this after releasing |
| 773 | // the suspend_lock to make sure we can be woken up. This call gains the mutator lock causing us |
| 774 | // to go to sleep until we are resumed. |
| 775 | SuspendCheck(self); |
| 776 | return OK; |
| 777 | } |
| 778 | |
| 779 | jvmtiError ThreadUtil::SuspendThread(jvmtiEnv* env ATTRIBUTE_UNUSED, jthread thread) { |
| 780 | art::Thread* self = art::Thread::Current(); |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 781 | bool target_is_self = false; |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 782 | { |
| 783 | art::ScopedObjectAccess soa(self); |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 784 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
| 785 | art::Thread* target = GetNativeThread(thread, soa); |
| 786 | if (target == nullptr) { |
| 787 | return ERR(INVALID_THREAD); |
| 788 | } else if (target == self) { |
| 789 | target_is_self = true; |
| 790 | } |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 791 | } |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 792 | if (target_is_self) { |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 793 | return SuspendSelf(self); |
| 794 | } else { |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 795 | return SuspendOther(self, thread); |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
| 799 | jvmtiError ThreadUtil::ResumeThread(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 800 | jthread thread) { |
| 801 | if (thread == nullptr) { |
| 802 | return ERR(NULL_POINTER); |
| 803 | } |
| 804 | art::Thread* self = art::Thread::Current(); |
| 805 | art::Thread* target; |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 806 | // Retry until we know we won't get suspended by user code while resuming something. |
| 807 | do { |
| 808 | SuspendCheck(self); |
| 809 | art::MutexLock ucsl_mu(self, *art::Locks::user_code_suspension_lock_); |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame^] | 810 | if (WouldSuspendForUserCodeLocked(self)) { |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 811 | // Make sure we won't be suspended in the middle of holding the thread_suspend_count_lock_ by |
| 812 | // a user-code suspension. We retry and do another SuspendCheck to clear this. |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame^] | 813 | continue; |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 814 | } |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 815 | // From now on we know we cannot get suspended by user-code. |
| 816 | { |
| 817 | // NB This does a SuspendCheck (during thread state change) so we need to make sure we don't |
| 818 | // have the 'suspend_lock' locked here. |
| 819 | art::ScopedObjectAccess soa(self); |
| 820 | art::MutexLock tll_mu(self, *art::Locks::thread_list_lock_); |
| 821 | target = GetNativeThread(thread, soa); |
| 822 | if (target == nullptr) { |
| 823 | return ERR(INVALID_THREAD); |
| 824 | } else if (target == self) { |
| 825 | // We would have paused until we aren't suspended anymore due to the ScopedObjectAccess so |
| 826 | // we can just return THREAD_NOT_SUSPENDED. Unfortunately we cannot do any real DCHECKs |
| 827 | // about current state since it's all concurrent. |
| 828 | return ERR(THREAD_NOT_SUSPENDED); |
| 829 | } else if (target->GetState() == art::ThreadState::kTerminated) { |
| 830 | return ERR(THREAD_NOT_ALIVE); |
| 831 | } |
| 832 | // The JVMTI spec requires us to return THREAD_NOT_SUSPENDED if it is alive but we really |
| 833 | // cannot tell why resume failed. |
| 834 | { |
| 835 | art::MutexLock thread_suspend_count_mu(self, *art::Locks::thread_suspend_count_lock_); |
| 836 | if (target->GetUserCodeSuspendCount() == 0) { |
| 837 | return ERR(THREAD_NOT_SUSPENDED); |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | // It is okay that we don't have a thread_list_lock here since we know that the thread cannot |
| 842 | // die since it is currently held suspended by a SuspendReason::kForUserCode suspend. |
| 843 | DCHECK(target != self); |
| 844 | if (!art::Runtime::Current()->GetThreadList()->Resume(target, |
| 845 | art::SuspendReason::kForUserCode)) { |
| 846 | // TODO Give a better error. |
| 847 | // This is most likely THREAD_NOT_SUSPENDED but we cannot really be sure. |
| 848 | return ERR(INTERNAL); |
| 849 | } else { |
| 850 | return OK; |
| 851 | } |
| 852 | } while (true); |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | // Suspends all the threads in the list at the same time. Getting this behavior is a little tricky |
| 856 | // since we can have threads in the list multiple times. This generally doesn't matter unless the |
| 857 | // current thread is present multiple times. In that case we need to suspend only once and either |
| 858 | // return the same error code in all the other slots if it failed or return ERR(THREAD_SUSPENDED) if |
| 859 | // it didn't. We also want to handle the current thread last to make the behavior of the code |
| 860 | // simpler to understand. |
| 861 | jvmtiError ThreadUtil::SuspendThreadList(jvmtiEnv* env, |
| 862 | jint request_count, |
| 863 | const jthread* threads, |
| 864 | jvmtiError* results) { |
| 865 | if (request_count == 0) { |
| 866 | return ERR(ILLEGAL_ARGUMENT); |
| 867 | } else if (results == nullptr || threads == nullptr) { |
| 868 | return ERR(NULL_POINTER); |
| 869 | } |
| 870 | // This is the list of the indexes in 'threads' and 'results' that correspond to the currently |
| 871 | // running thread. These indexes we need to handle specially since we need to only actually |
| 872 | // suspend a single time. |
| 873 | std::vector<jint> current_thread_indexes; |
| 874 | art::Thread* self = art::Thread::Current(); |
| 875 | for (jint i = 0; i < request_count; i++) { |
| 876 | { |
| 877 | art::ScopedObjectAccess soa(self); |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 878 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 879 | if (threads[i] == nullptr || GetNativeThread(threads[i], soa) == self) { |
| 880 | current_thread_indexes.push_back(i); |
| 881 | continue; |
| 882 | } |
| 883 | } |
| 884 | results[i] = env->SuspendThread(threads[i]); |
| 885 | } |
| 886 | if (!current_thread_indexes.empty()) { |
| 887 | jint first_current_thread_index = current_thread_indexes[0]; |
| 888 | // Suspend self. |
| 889 | jvmtiError res = env->SuspendThread(threads[first_current_thread_index]); |
| 890 | results[first_current_thread_index] = res; |
| 891 | // Fill in the rest of the error values as appropriate. |
| 892 | jvmtiError other_results = (res != OK) ? res : ERR(THREAD_SUSPENDED); |
| 893 | for (auto it = ++current_thread_indexes.begin(); it != current_thread_indexes.end(); ++it) { |
| 894 | results[*it] = other_results; |
| 895 | } |
| 896 | } |
| 897 | return OK; |
| 898 | } |
| 899 | |
| 900 | jvmtiError ThreadUtil::ResumeThreadList(jvmtiEnv* env, |
| 901 | jint request_count, |
| 902 | const jthread* threads, |
| 903 | jvmtiError* results) { |
| 904 | if (request_count == 0) { |
| 905 | return ERR(ILLEGAL_ARGUMENT); |
| 906 | } else if (results == nullptr || threads == nullptr) { |
| 907 | return ERR(NULL_POINTER); |
| 908 | } |
| 909 | for (jint i = 0; i < request_count; i++) { |
| 910 | results[i] = env->ResumeThread(threads[i]); |
| 911 | } |
| 912 | return OK; |
| 913 | } |
| 914 | |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 915 | } // namespace openjdkjvmti |