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 | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 34 | #include <android-base/logging.h> |
| 35 | #include <android-base/strings.h> |
| 36 | |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 37 | #include "art_field-inl.h" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 38 | #include "art_jvmti.h" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 39 | #include "base/mutex.h" |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 40 | #include "events-inl.h" |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 41 | #include "gc/system_weak.h" |
Alex Light | d9aff13 | 2017-10-31 22:30:05 +0000 | [diff] [blame] | 42 | #include "gc/collector_type.h" |
| 43 | #include "gc/gc_cause.h" |
| 44 | #include "gc/scoped_gc_critical_section.h" |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 45 | #include "gc_root-inl.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 46 | #include "jni/jni_internal.h" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 47 | #include "mirror/class.h" |
| 48 | #include "mirror/object-inl.h" |
| 49 | #include "mirror/string.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 50 | #include "nativehelper/scoped_local_ref.h" |
Alex Light | 9311297 | 2017-11-13 10:38:59 -0800 | [diff] [blame] | 51 | #include "nativehelper/scoped_utf_chars.h" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 52 | #include "obj_ptr.h" |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 53 | #include "runtime.h" |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 54 | #include "runtime_callbacks.h" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 55 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 56 | #include "thread-current-inl.h" |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 57 | #include "thread_list.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 58 | #include "ti_phase.h" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 59 | #include "well_known_classes.h" |
| 60 | |
| 61 | namespace openjdkjvmti { |
| 62 | |
Alex Light | 184f075 | 2018-07-13 11:18:22 -0700 | [diff] [blame] | 63 | static const char* kJvmtiTlsKey = "JvmtiTlsKey"; |
| 64 | |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 65 | art::ArtField* ThreadUtil::context_class_loader_ = nullptr; |
| 66 | |
Alex Light | 1d8a974 | 2017-08-17 11:12:06 -0700 | [diff] [blame] | 67 | struct ThreadCallback : public art::ThreadLifecycleCallback { |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 68 | jthread GetThreadObject(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 69 | if (self->GetPeer() == nullptr) { |
| 70 | return nullptr; |
| 71 | } |
| 72 | return self->GetJniEnv()->AddLocalReference<jthread>(self->GetPeer()); |
| 73 | } |
Alex Light | 1d8a974 | 2017-08-17 11:12:06 -0700 | [diff] [blame] | 74 | |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 75 | template <ArtJvmtiEvent kEvent> |
| 76 | void Post(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 77 | DCHECK_EQ(self, art::Thread::Current()); |
| 78 | ScopedLocalRef<jthread> thread(self->GetJniEnv(), GetThreadObject(self)); |
Andreas Gampe | e637746 | 2017-01-20 17:37:50 -0800 | [diff] [blame] | 79 | art::ScopedThreadSuspension sts(self, art::ThreadState::kNative); |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 80 | event_handler->DispatchEvent<kEvent>(self, |
| 81 | reinterpret_cast<JNIEnv*>(self->GetJniEnv()), |
| 82 | thread.get()); |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 85 | void ThreadStart(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 86 | if (!started) { |
| 87 | // Runtime isn't started. We only expect at most the signal handler or JIT threads to be |
| 88 | // started here. |
| 89 | if (art::kIsDebugBuild) { |
| 90 | std::string name; |
| 91 | self->GetThreadName(name); |
Alex Light | 5bd0954 | 2017-02-09 16:01:32 -0800 | [diff] [blame] | 92 | if (name != "JDWP" && |
| 93 | name != "Signal Catcher" && |
| 94 | !android::base::StartsWith(name, "Jit thread pool")) { |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame] | 95 | LOG(FATAL) << "Unexpected thread before start: " << name << " id: " |
| 96 | << self->GetThreadId(); |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | return; |
| 100 | } |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 101 | Post<ArtJvmtiEvent::kThreadStart>(self); |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 104 | void ThreadDeath(art::Thread* self) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Andreas Gampe | 983c175 | 2017-01-23 19:46:56 -0800 | [diff] [blame] | 105 | Post<ArtJvmtiEvent::kThreadEnd>(self); |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 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); |
Alex Light | 1d8a974 | 2017-08-17 11:12:06 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void ThreadUtil::VMInitEventSent() { |
| 127 | // We should have already started. |
| 128 | DCHECK(gThreadCallback.started); |
| 129 | // We moved to VMInit. Report the main thread as started (it was attached early, and must not be |
| 130 | // reported until Init. |
| 131 | gThreadCallback.Post<ArtJvmtiEvent::kThreadStart>(art::Thread::Current()); |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 134 | void ThreadUtil::CacheData() { |
Alex Light | 1d8a974 | 2017-08-17 11:12:06 -0700 | [diff] [blame] | 135 | // We must have started since it is now safe to cache our data; |
| 136 | gThreadCallback.started = true; |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 137 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 138 | art::ObjPtr<art::mirror::Class> thread_class = |
| 139 | soa.Decode<art::mirror::Class>(art::WellKnownClasses::java_lang_Thread); |
| 140 | CHECK(thread_class != nullptr); |
| 141 | context_class_loader_ = thread_class->FindDeclaredInstanceField("contextClassLoader", |
| 142 | "Ljava/lang/ClassLoader;"); |
| 143 | CHECK(context_class_loader_ != nullptr); |
| 144 | } |
| 145 | |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 146 | void ThreadUtil::Unregister() { |
| 147 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 148 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 149 | art::ScopedSuspendAll ssa("Remove thread callback"); |
| 150 | art::Runtime* runtime = art::Runtime::Current(); |
| 151 | runtime->GetRuntimeCallbacks()->RemoveThreadLifecycleCallback(&gThreadCallback); |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 154 | jvmtiError ThreadUtil::GetCurrentThread(jvmtiEnv* env ATTRIBUTE_UNUSED, jthread* thread_ptr) { |
| 155 | art::Thread* self = art::Thread::Current(); |
| 156 | |
| 157 | art::ScopedObjectAccess soa(self); |
| 158 | |
| 159 | jthread thread_peer; |
| 160 | if (self->IsStillStarting()) { |
| 161 | thread_peer = nullptr; |
| 162 | } else { |
| 163 | thread_peer = soa.AddLocalReference<jthread>(self->GetPeer()); |
| 164 | } |
| 165 | |
| 166 | *thread_ptr = thread_peer; |
| 167 | return ERR(NONE); |
| 168 | } |
| 169 | |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 170 | // Get the native thread. The spec says a null object denotes the current thread. |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 171 | bool ThreadUtil::GetNativeThread(jthread thread, |
| 172 | const art::ScopedObjectAccessAlreadyRunnable& soa, |
| 173 | /*out*/ art::Thread** thr, |
| 174 | /*out*/ jvmtiError* err) { |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 175 | if (thread == nullptr) { |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 176 | *thr = art::Thread::Current(); |
| 177 | return true; |
| 178 | } else if (!soa.Env()->IsInstanceOf(thread, art::WellKnownClasses::java_lang_Thread)) { |
| 179 | *err = ERR(INVALID_THREAD); |
| 180 | return false; |
| 181 | } else { |
| 182 | *thr = art::Thread::FromManagedThread(soa, thread); |
| 183 | return true; |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 184 | } |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 185 | } |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 186 | |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 187 | bool ThreadUtil::GetAliveNativeThread(jthread thread, |
| 188 | const art::ScopedObjectAccessAlreadyRunnable& soa, |
| 189 | /*out*/ art::Thread** thr, |
| 190 | /*out*/ jvmtiError* err) { |
| 191 | if (!GetNativeThread(thread, soa, thr, err)) { |
| 192 | return false; |
| 193 | } else if (*thr == nullptr || (*thr)->GetState() == art::ThreadState::kTerminated) { |
| 194 | *err = ERR(THREAD_NOT_ALIVE); |
| 195 | return false; |
| 196 | } else { |
| 197 | return true; |
| 198 | } |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | jvmtiError ThreadUtil::GetThreadInfo(jvmtiEnv* env, jthread thread, jvmtiThreadInfo* info_ptr) { |
| 202 | if (info_ptr == nullptr) { |
| 203 | return ERR(NULL_POINTER); |
| 204 | } |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 205 | if (!PhaseUtil::IsLivePhase()) { |
| 206 | return JVMTI_ERROR_WRONG_PHASE; |
| 207 | } |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 208 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 209 | art::Thread* self = art::Thread::Current(); |
| 210 | art::ScopedObjectAccess soa(self); |
| 211 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 212 | |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 213 | art::Thread* target; |
| 214 | jvmtiError err = ERR(INTERNAL); |
| 215 | if (!GetNativeThread(thread, soa, &target, &err)) { |
| 216 | return err; |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 219 | JvmtiUniquePtr<char[]> name_uptr; |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 220 | if (target != nullptr) { |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 221 | // Have a native thread object, this thread is alive. |
| 222 | std::string name; |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 223 | target->GetThreadName(name); |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 224 | jvmtiError name_result; |
| 225 | name_uptr = CopyString(env, name.c_str(), &name_result); |
| 226 | if (name_uptr == nullptr) { |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 227 | return name_result; |
| 228 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 229 | info_ptr->name = name_uptr.get(); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 230 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 231 | info_ptr->priority = target->GetNativePriority(); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 232 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 233 | info_ptr->is_daemon = target->IsDaemon(); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 234 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 235 | art::ObjPtr<art::mirror::Object> peer = target->GetPeerFromOtherThread(); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 236 | |
| 237 | // ThreadGroup. |
| 238 | if (peer != nullptr) { |
| 239 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_group); |
| 240 | CHECK(f != nullptr); |
| 241 | art::ObjPtr<art::mirror::Object> group = f->GetObject(peer); |
| 242 | info_ptr->thread_group = group == nullptr |
| 243 | ? nullptr |
| 244 | : soa.AddLocalReference<jthreadGroup>(group); |
| 245 | } else { |
| 246 | info_ptr->thread_group = nullptr; |
| 247 | } |
| 248 | |
| 249 | // Context classloader. |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 250 | DCHECK(context_class_loader_ != nullptr); |
| 251 | art::ObjPtr<art::mirror::Object> ccl = peer != nullptr |
| 252 | ? context_class_loader_->GetObject(peer) |
| 253 | : nullptr; |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 254 | info_ptr->context_class_loader = ccl == nullptr |
| 255 | ? nullptr |
| 256 | : soa.AddLocalReference<jobject>(ccl); |
| 257 | } else { |
| 258 | // Only the peer. This thread has either not been started, or is dead. Read things from |
| 259 | // the Java side. |
| 260 | art::ObjPtr<art::mirror::Object> peer = soa.Decode<art::mirror::Object>(thread); |
| 261 | |
| 262 | // Name. |
| 263 | { |
| 264 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_name); |
| 265 | CHECK(f != nullptr); |
| 266 | art::ObjPtr<art::mirror::Object> name = f->GetObject(peer); |
| 267 | std::string name_cpp; |
| 268 | const char* name_cstr; |
| 269 | if (name != nullptr) { |
| 270 | name_cpp = name->AsString()->ToModifiedUtf8(); |
| 271 | name_cstr = name_cpp.c_str(); |
| 272 | } else { |
| 273 | name_cstr = ""; |
| 274 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 275 | jvmtiError name_result; |
| 276 | name_uptr = CopyString(env, name_cstr, &name_result); |
| 277 | if (name_uptr == nullptr) { |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 278 | return name_result; |
| 279 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 280 | info_ptr->name = name_uptr.get(); |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // Priority. |
| 284 | { |
| 285 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_priority); |
| 286 | CHECK(f != nullptr); |
| 287 | info_ptr->priority = static_cast<jint>(f->GetInt(peer)); |
| 288 | } |
| 289 | |
| 290 | // Daemon. |
| 291 | { |
| 292 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_daemon); |
| 293 | CHECK(f != nullptr); |
| 294 | info_ptr->is_daemon = f->GetBoolean(peer) == 0 ? JNI_FALSE : JNI_TRUE; |
| 295 | } |
| 296 | |
| 297 | // ThreadGroup. |
| 298 | { |
| 299 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_group); |
| 300 | CHECK(f != nullptr); |
| 301 | art::ObjPtr<art::mirror::Object> group = f->GetObject(peer); |
| 302 | info_ptr->thread_group = group == nullptr |
| 303 | ? nullptr |
| 304 | : soa.AddLocalReference<jthreadGroup>(group); |
| 305 | } |
| 306 | |
| 307 | // Context classloader. |
Andreas Gampe | db6c2ab | 2017-03-28 17:28:32 -0700 | [diff] [blame] | 308 | DCHECK(context_class_loader_ != nullptr); |
| 309 | art::ObjPtr<art::mirror::Object> ccl = peer != nullptr |
| 310 | ? context_class_loader_->GetObject(peer) |
| 311 | : nullptr; |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 312 | info_ptr->context_class_loader = ccl == nullptr |
| 313 | ? nullptr |
| 314 | : soa.AddLocalReference<jobject>(ccl); |
| 315 | } |
| 316 | |
| 317 | name_uptr.release(); |
| 318 | |
| 319 | return ERR(NONE); |
| 320 | } |
| 321 | |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 322 | struct InternalThreadState { |
| 323 | art::Thread* native_thread; |
| 324 | art::ThreadState art_state; |
| 325 | int thread_user_code_suspend_count; |
| 326 | }; |
| 327 | |
| 328 | // Return the thread's (or current thread, if null) thread state. |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 329 | static InternalThreadState GetNativeThreadState(art::Thread* target) |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 330 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 331 | REQUIRES(art::Locks::thread_list_lock_, art::Locks::user_code_suspension_lock_) { |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 332 | InternalThreadState thread_state = {}; |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 333 | art::MutexLock tscl_mu(art::Thread::Current(), *art::Locks::thread_suspend_count_lock_); |
| 334 | thread_state.native_thread = target; |
| 335 | if (target == nullptr || target->IsStillStarting()) { |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 336 | thread_state.art_state = art::ThreadState::kStarting; |
| 337 | thread_state.thread_user_code_suspend_count = 0; |
| 338 | } else { |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 339 | thread_state.art_state = target->GetState(); |
| 340 | thread_state.thread_user_code_suspend_count = target->GetUserCodeSuspendCount(); |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 341 | } |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 342 | return thread_state; |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 345 | static jint GetJvmtiThreadStateFromInternal(const InternalThreadState& state) { |
| 346 | art::ThreadState internal_thread_state = state.art_state; |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 347 | jint jvmti_state = JVMTI_THREAD_STATE_ALIVE; |
| 348 | |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 349 | if (state.thread_user_code_suspend_count != 0) { |
Alex Light | 597adad | 2017-10-16 16:11:42 -0700 | [diff] [blame] | 350 | // Suspended can be set with any thread state so check it here. Even if the thread isn't in |
| 351 | // kSuspended state it will move to that once it hits a checkpoint so we can still set this. |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 352 | jvmti_state |= JVMTI_THREAD_STATE_SUSPENDED; |
| 353 | // Note: We do not have data about the previous state. Otherwise we should load the previous |
| 354 | // state here. |
| 355 | } |
| 356 | |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 357 | if (state.native_thread->IsInterrupted()) { |
Alex Light | 597adad | 2017-10-16 16:11:42 -0700 | [diff] [blame] | 358 | // Interrupted can be set with any thread state so check it here. |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 359 | jvmti_state |= JVMTI_THREAD_STATE_INTERRUPTED; |
| 360 | } |
| 361 | |
Alex Light | 597adad | 2017-10-16 16:11:42 -0700 | [diff] [blame] | 362 | // Enumerate all the thread states and fill in the other bits. This contains the results of |
| 363 | // following the decision tree in the JVMTI spec GetThreadState documentation. |
| 364 | switch (internal_thread_state) { |
| 365 | case art::ThreadState::kRunnable: |
| 366 | case art::ThreadState::kWaitingWeakGcRootRead: |
| 367 | case art::ThreadState::kSuspended: |
| 368 | // These are all simply runnable. |
| 369 | // kRunnable is self-explanatory. |
| 370 | // kWaitingWeakGcRootRead is set during some operations with strings due to the intern-table |
| 371 | // so we want to keep it marked as runnable. |
| 372 | // kSuspended we don't mark since if we don't have a user_code_suspend_count then it is done |
| 373 | // by the GC and not a JVMTI suspension, which means it cannot be removed by ResumeThread. |
| 374 | jvmti_state |= JVMTI_THREAD_STATE_RUNNABLE; |
| 375 | break; |
| 376 | case art::ThreadState::kNative: |
| 377 | // kNative means native and runnable. Technically THREAD_STATE_IN_NATIVE can be set with any |
| 378 | // state but we don't have the information to know if it should be present for any but the |
| 379 | // kNative state. |
| 380 | jvmti_state |= (JVMTI_THREAD_STATE_IN_NATIVE | |
| 381 | JVMTI_THREAD_STATE_RUNNABLE); |
| 382 | break; |
| 383 | case art::ThreadState::kBlocked: |
| 384 | // Blocked is one of the top level states so it sits alone. |
| 385 | jvmti_state |= JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER; |
| 386 | break; |
| 387 | case art::ThreadState::kWaiting: |
| 388 | // Object.wait() so waiting, indefinitely, in object.wait. |
| 389 | jvmti_state |= (JVMTI_THREAD_STATE_WAITING | |
| 390 | JVMTI_THREAD_STATE_WAITING_INDEFINITELY | |
| 391 | JVMTI_THREAD_STATE_IN_OBJECT_WAIT); |
| 392 | break; |
| 393 | case art::ThreadState::kTimedWaiting: |
| 394 | // Object.wait(long) so waiting, with timeout, in object.wait. |
| 395 | jvmti_state |= (JVMTI_THREAD_STATE_WAITING | |
| 396 | JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT | |
| 397 | JVMTI_THREAD_STATE_IN_OBJECT_WAIT); |
| 398 | break; |
| 399 | case art::ThreadState::kSleeping: |
| 400 | // In object.sleep. This is a timed wait caused by sleep. |
| 401 | jvmti_state |= (JVMTI_THREAD_STATE_WAITING | |
| 402 | JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT | |
| 403 | JVMTI_THREAD_STATE_SLEEPING); |
| 404 | break; |
| 405 | // TODO We might want to print warnings if we have the debugger running while JVMTI agents are |
| 406 | // attached. |
| 407 | case art::ThreadState::kWaitingForDebuggerSend: |
| 408 | case art::ThreadState::kWaitingForDebuggerToAttach: |
| 409 | case art::ThreadState::kWaitingInMainDebuggerLoop: |
| 410 | case art::ThreadState::kWaitingForDebuggerSuspension: |
| 411 | case art::ThreadState::kWaitingForLockInflation: |
| 412 | case art::ThreadState::kWaitingForTaskProcessor: |
| 413 | case art::ThreadState::kWaitingForGcToComplete: |
| 414 | case art::ThreadState::kWaitingForCheckPointsToRun: |
| 415 | case art::ThreadState::kWaitingPerformingGc: |
| 416 | case art::ThreadState::kWaitingForJniOnLoad: |
| 417 | case art::ThreadState::kWaitingInMainSignalCatcherLoop: |
| 418 | case art::ThreadState::kWaitingForSignalCatcherOutput: |
| 419 | case art::ThreadState::kWaitingForDeoptimization: |
| 420 | case art::ThreadState::kWaitingForMethodTracingStart: |
| 421 | case art::ThreadState::kWaitingForVisitObjects: |
| 422 | case art::ThreadState::kWaitingForGetObjectsAllocated: |
| 423 | case art::ThreadState::kWaitingForGcThreadFlip: |
| 424 | // All of these are causing the thread to wait for an indeterminate amount of time but isn't |
| 425 | // caused by sleep, park, or object#wait. |
| 426 | jvmti_state |= (JVMTI_THREAD_STATE_WAITING | |
| 427 | JVMTI_THREAD_STATE_WAITING_INDEFINITELY); |
| 428 | break; |
| 429 | case art::ThreadState::kStarting: |
| 430 | case art::ThreadState::kTerminated: |
| 431 | // We only call this if we are alive so we shouldn't see either of these states. |
| 432 | LOG(FATAL) << "Should not be in state " << internal_thread_state; |
| 433 | UNREACHABLE(); |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 434 | } |
Alex Light | 597adad | 2017-10-16 16:11:42 -0700 | [diff] [blame] | 435 | // TODO: PARKED. We'll have to inspect the stack. |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 436 | |
| 437 | return jvmti_state; |
| 438 | } |
| 439 | |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 440 | static jint GetJavaStateFromInternal(const InternalThreadState& state) { |
| 441 | switch (state.art_state) { |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 442 | case art::ThreadState::kTerminated: |
| 443 | return JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED; |
| 444 | |
| 445 | case art::ThreadState::kRunnable: |
| 446 | case art::ThreadState::kNative: |
| 447 | case art::ThreadState::kWaitingWeakGcRootRead: |
| 448 | case art::ThreadState::kSuspended: |
| 449 | return JVMTI_JAVA_LANG_THREAD_STATE_RUNNABLE; |
| 450 | |
| 451 | case art::ThreadState::kTimedWaiting: |
| 452 | case art::ThreadState::kSleeping: |
| 453 | return JVMTI_JAVA_LANG_THREAD_STATE_TIMED_WAITING; |
| 454 | |
| 455 | case art::ThreadState::kBlocked: |
| 456 | return JVMTI_JAVA_LANG_THREAD_STATE_BLOCKED; |
| 457 | |
| 458 | case art::ThreadState::kStarting: |
| 459 | return JVMTI_JAVA_LANG_THREAD_STATE_NEW; |
| 460 | |
| 461 | case art::ThreadState::kWaiting: |
Alex Light | 77fee87 | 2017-09-05 14:51:49 -0700 | [diff] [blame] | 462 | case art::ThreadState::kWaitingForTaskProcessor: |
| 463 | case art::ThreadState::kWaitingForLockInflation: |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 464 | case art::ThreadState::kWaitingForGcToComplete: |
| 465 | case art::ThreadState::kWaitingPerformingGc: |
| 466 | case art::ThreadState::kWaitingForCheckPointsToRun: |
| 467 | case art::ThreadState::kWaitingForDebuggerSend: |
| 468 | case art::ThreadState::kWaitingForDebuggerToAttach: |
| 469 | case art::ThreadState::kWaitingInMainDebuggerLoop: |
| 470 | case art::ThreadState::kWaitingForDebuggerSuspension: |
| 471 | case art::ThreadState::kWaitingForDeoptimization: |
| 472 | case art::ThreadState::kWaitingForGetObjectsAllocated: |
| 473 | case art::ThreadState::kWaitingForJniOnLoad: |
| 474 | case art::ThreadState::kWaitingForSignalCatcherOutput: |
| 475 | case art::ThreadState::kWaitingInMainSignalCatcherLoop: |
| 476 | case art::ThreadState::kWaitingForMethodTracingStart: |
| 477 | case art::ThreadState::kWaitingForVisitObjects: |
| 478 | case art::ThreadState::kWaitingForGcThreadFlip: |
| 479 | return JVMTI_JAVA_LANG_THREAD_STATE_WAITING; |
| 480 | } |
| 481 | LOG(FATAL) << "Unreachable"; |
| 482 | UNREACHABLE(); |
| 483 | } |
| 484 | |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 485 | // Suspends the current thread if it has any suspend requests on it. |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame] | 486 | void ThreadUtil::SuspendCheck(art::Thread* self) { |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 487 | art::ScopedObjectAccess soa(self); |
| 488 | // Really this is only needed if we are in FastJNI and actually have the mutator_lock_ already. |
| 489 | self->FullSuspendCheck(); |
| 490 | } |
| 491 | |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame] | 492 | bool ThreadUtil::WouldSuspendForUserCodeLocked(art::Thread* self) { |
| 493 | DCHECK(self == art::Thread::Current()); |
| 494 | art::MutexLock tscl_mu(self, *art::Locks::thread_suspend_count_lock_); |
| 495 | return self->GetUserCodeSuspendCount() != 0; |
| 496 | } |
| 497 | |
| 498 | bool ThreadUtil::WouldSuspendForUserCode(art::Thread* self) { |
| 499 | DCHECK(self == art::Thread::Current()); |
| 500 | art::MutexLock ucsl_mu(self, *art::Locks::user_code_suspension_lock_); |
| 501 | return WouldSuspendForUserCodeLocked(self); |
| 502 | } |
| 503 | |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 504 | jvmtiError ThreadUtil::GetThreadState(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 505 | jthread thread, |
| 506 | jint* thread_state_ptr) { |
| 507 | if (thread_state_ptr == nullptr) { |
| 508 | return ERR(NULL_POINTER); |
| 509 | } |
| 510 | |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 511 | art::Thread* self = art::Thread::Current(); |
| 512 | InternalThreadState state = {}; |
| 513 | // Loop since we need to bail out and try again if we would end up getting suspended while holding |
| 514 | // the user_code_suspension_lock_ due to a SuspendReason::kForUserCode. In this situation we |
| 515 | // release the lock, wait to get resumed and try again. |
| 516 | do { |
| 517 | SuspendCheck(self); |
| 518 | art::MutexLock ucsl_mu(self, *art::Locks::user_code_suspension_lock_); |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame] | 519 | if (WouldSuspendForUserCodeLocked(self)) { |
| 520 | // Make sure we won't be suspended in the middle of holding the thread_suspend_count_lock_ by |
| 521 | // a user-code suspension. We retry and do another SuspendCheck to clear this. |
| 522 | continue; |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 523 | } |
| 524 | art::ScopedObjectAccess soa(self); |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 525 | art::MutexLock tll_mu(self, *art::Locks::thread_list_lock_); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 526 | jvmtiError err = ERR(INTERNAL); |
| 527 | art::Thread* target = nullptr; |
| 528 | if (!GetNativeThread(thread, soa, &target, &err)) { |
| 529 | return err; |
| 530 | } |
| 531 | state = GetNativeThreadState(target); |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 532 | if (state.art_state == art::ThreadState::kStarting) { |
| 533 | break; |
| 534 | } |
| 535 | DCHECK(state.native_thread != nullptr); |
| 536 | |
| 537 | // Translate internal thread state to JVMTI and Java state. |
| 538 | jint jvmti_state = GetJvmtiThreadStateFromInternal(state); |
| 539 | |
| 540 | // Java state is derived from nativeGetState. |
| 541 | // TODO: Our implementation assigns "runnable" to suspended. As such, we will have slightly |
| 542 | // different mask if a thread got suspended due to user-code. However, this is for |
| 543 | // consistency with the Java view. |
| 544 | jint java_state = GetJavaStateFromInternal(state); |
| 545 | |
| 546 | *thread_state_ptr = jvmti_state | java_state; |
| 547 | |
| 548 | return ERR(NONE); |
Alex Light | 1f0a22f | 2017-07-17 12:55:59 -0700 | [diff] [blame] | 549 | } while (true); |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 550 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 551 | DCHECK_EQ(state.art_state, art::ThreadState::kStarting); |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 552 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 553 | if (thread == nullptr) { |
| 554 | // No native thread, and no Java thread? We must be starting up. Report as wrong phase. |
| 555 | return ERR(WRONG_PHASE); |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 556 | } |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 557 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 558 | art::ScopedObjectAccess soa(self); |
Alex Light | ba461c3 | 2017-09-22 14:19:18 -0700 | [diff] [blame] | 559 | art::StackHandleScope<1> hs(self); |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 560 | |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 561 | // Need to read the Java "started" field to know whether this is starting or terminated. |
Alex Light | ba461c3 | 2017-09-22 14:19:18 -0700 | [diff] [blame] | 562 | art::Handle<art::mirror::Object> peer(hs.NewHandle(soa.Decode<art::mirror::Object>(thread))); |
| 563 | art::ObjPtr<art::mirror::Class> thread_klass = |
| 564 | soa.Decode<art::mirror::Class>(art::WellKnownClasses::java_lang_Thread); |
| 565 | if (!thread_klass->IsAssignableFrom(peer->GetClass())) { |
| 566 | return ERR(INVALID_THREAD); |
| 567 | } |
| 568 | art::ArtField* started_field = thread_klass->FindDeclaredInstanceField("started", "Z"); |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 569 | CHECK(started_field != nullptr); |
Alex Light | ba461c3 | 2017-09-22 14:19:18 -0700 | [diff] [blame] | 570 | bool started = started_field->GetBoolean(peer.Get()) != 0; |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 571 | constexpr jint kStartedState = JVMTI_JAVA_LANG_THREAD_STATE_NEW; |
| 572 | constexpr jint kTerminatedState = JVMTI_THREAD_STATE_TERMINATED | |
| 573 | JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED; |
| 574 | *thread_state_ptr = started ? kTerminatedState : kStartedState; |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 575 | return ERR(NONE); |
| 576 | } |
| 577 | |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 578 | jvmtiError ThreadUtil::GetAllThreads(jvmtiEnv* env, |
| 579 | jint* threads_count_ptr, |
| 580 | jthread** threads_ptr) { |
| 581 | if (threads_count_ptr == nullptr || threads_ptr == nullptr) { |
| 582 | return ERR(NULL_POINTER); |
| 583 | } |
| 584 | |
| 585 | art::Thread* current = art::Thread::Current(); |
| 586 | |
| 587 | art::ScopedObjectAccess soa(current); |
| 588 | |
| 589 | art::MutexLock mu(current, *art::Locks::thread_list_lock_); |
| 590 | std::list<art::Thread*> thread_list = art::Runtime::Current()->GetThreadList()->GetList(); |
| 591 | |
| 592 | std::vector<art::ObjPtr<art::mirror::Object>> peers; |
| 593 | |
| 594 | for (art::Thread* thread : thread_list) { |
| 595 | // Skip threads that are still starting. |
| 596 | if (thread->IsStillStarting()) { |
| 597 | continue; |
| 598 | } |
| 599 | |
Andreas Gampe | 202f85a | 2017-02-06 10:23:26 -0800 | [diff] [blame] | 600 | art::ObjPtr<art::mirror::Object> peer = thread->GetPeerFromOtherThread(); |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 601 | if (peer != nullptr) { |
| 602 | peers.push_back(peer); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if (peers.empty()) { |
| 607 | *threads_count_ptr = 0; |
| 608 | *threads_ptr = nullptr; |
| 609 | } else { |
| 610 | unsigned char* data; |
| 611 | jvmtiError data_result = env->Allocate(peers.size() * sizeof(jthread), &data); |
| 612 | if (data_result != ERR(NONE)) { |
| 613 | return data_result; |
| 614 | } |
| 615 | jthread* threads = reinterpret_cast<jthread*>(data); |
| 616 | for (size_t i = 0; i != peers.size(); ++i) { |
| 617 | threads[i] = soa.AddLocalReference<jthread>(peers[i]); |
| 618 | } |
| 619 | |
| 620 | *threads_count_ptr = static_cast<jint>(peers.size()); |
| 621 | *threads_ptr = threads; |
| 622 | } |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 623 | return ERR(NONE); |
| 624 | } |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 625 | |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 626 | static void RemoveTLSData(art::Thread* target, void* ctx) REQUIRES(art::Locks::thread_list_lock_) { |
| 627 | jvmtiEnv* env = reinterpret_cast<jvmtiEnv*>(ctx); |
| 628 | art::Locks::thread_list_lock_->AssertHeld(art::Thread::Current()); |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 629 | JvmtiGlobalTLSData* global_tls = ThreadUtil::GetGlobalTLSData(target); |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 630 | if (global_tls != nullptr) { |
| 631 | global_tls->data.erase(env); |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | void ThreadUtil::RemoveEnvironment(jvmtiEnv* env) { |
| 636 | art::Thread* self = art::Thread::Current(); |
| 637 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
| 638 | art::ThreadList* list = art::Runtime::Current()->GetThreadList(); |
| 639 | list->ForEach(RemoveTLSData, env); |
| 640 | } |
| 641 | |
| 642 | jvmtiError ThreadUtil::SetThreadLocalStorage(jvmtiEnv* env, jthread thread, const void* data) { |
| 643 | art::Thread* self = art::Thread::Current(); |
| 644 | art::ScopedObjectAccess soa(self); |
| 645 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 646 | art::Thread* target = nullptr; |
| 647 | jvmtiError err = ERR(INTERNAL); |
| 648 | if (!GetAliveNativeThread(thread, soa, &target, &err)) { |
| 649 | return err; |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 650 | } |
| 651 | |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 652 | JvmtiGlobalTLSData* global_tls = GetOrCreateGlobalTLSData(target); |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 653 | |
| 654 | global_tls->data[env] = data; |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 655 | |
| 656 | return ERR(NONE); |
| 657 | } |
| 658 | |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 659 | JvmtiGlobalTLSData* ThreadUtil::GetOrCreateGlobalTLSData(art::Thread* thread) { |
| 660 | JvmtiGlobalTLSData* data = GetGlobalTLSData(thread); |
| 661 | if (data != nullptr) { |
| 662 | return data; |
| 663 | } else { |
| 664 | thread->SetCustomTLS(kJvmtiTlsKey, new JvmtiGlobalTLSData); |
| 665 | return GetGlobalTLSData(thread); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | JvmtiGlobalTLSData* ThreadUtil::GetGlobalTLSData(art::Thread* thread) { |
| 670 | return reinterpret_cast<JvmtiGlobalTLSData*>(thread->GetCustomTLS(kJvmtiTlsKey)); |
| 671 | } |
| 672 | |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 673 | jvmtiError ThreadUtil::GetThreadLocalStorage(jvmtiEnv* env, |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 674 | jthread thread, |
| 675 | void** data_ptr) { |
| 676 | if (data_ptr == nullptr) { |
| 677 | return ERR(NULL_POINTER); |
| 678 | } |
| 679 | |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 680 | art::Thread* self = art::Thread::Current(); |
| 681 | art::ScopedObjectAccess soa(self); |
| 682 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 683 | art::Thread* target = nullptr; |
| 684 | jvmtiError err = ERR(INTERNAL); |
| 685 | if (!GetAliveNativeThread(thread, soa, &target, &err)) { |
| 686 | return err; |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Alex Light | 0aa7a5a | 2018-10-10 15:58:14 +0000 | [diff] [blame] | 689 | JvmtiGlobalTLSData* global_tls = GetGlobalTLSData(target); |
Alex Light | 092a404 | 2017-07-12 08:46:44 -0700 | [diff] [blame] | 690 | if (global_tls == nullptr) { |
| 691 | *data_ptr = nullptr; |
| 692 | return OK; |
| 693 | } |
| 694 | auto it = global_tls->data.find(env); |
| 695 | if (it != global_tls->data.end()) { |
| 696 | *data_ptr = const_cast<void*>(it->second); |
| 697 | } else { |
| 698 | *data_ptr = nullptr; |
| 699 | } |
| 700 | |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 701 | return ERR(NONE); |
| 702 | } |
| 703 | |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 704 | struct AgentData { |
| 705 | const void* arg; |
| 706 | jvmtiStartFunction proc; |
| 707 | jthread thread; |
| 708 | JavaVM* java_vm; |
| 709 | jvmtiEnv* jvmti_env; |
| 710 | jint priority; |
Alex Light | 9311297 | 2017-11-13 10:38:59 -0800 | [diff] [blame] | 711 | std::string name; |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 712 | }; |
| 713 | |
| 714 | static void* AgentCallback(void* arg) { |
| 715 | std::unique_ptr<AgentData> data(reinterpret_cast<AgentData*>(arg)); |
| 716 | CHECK(data->thread != nullptr); |
| 717 | |
| 718 | // We already have a peer. So call our special Attach function. |
Alex Light | 9311297 | 2017-11-13 10:38:59 -0800 | [diff] [blame] | 719 | art::Thread* self = art::Thread::Attach(data->name.c_str(), true, data->thread); |
Alex Light | 739bf72 | 2017-10-20 13:14:24 -0700 | [diff] [blame] | 720 | CHECK(self != nullptr) << "threads_being_born_ should have ensured thread could be attached."; |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 721 | // The name in Attach() is only for logging. Set the thread name. This is important so |
| 722 | // that the thread is no longer seen as starting up. |
| 723 | { |
| 724 | art::ScopedObjectAccess soa(self); |
Alex Light | 9311297 | 2017-11-13 10:38:59 -0800 | [diff] [blame] | 725 | self->SetThreadName(data->name.c_str()); |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | // Release the peer. |
| 729 | JNIEnv* env = self->GetJniEnv(); |
| 730 | env->DeleteGlobalRef(data->thread); |
| 731 | data->thread = nullptr; |
| 732 | |
Alex Light | 739bf72 | 2017-10-20 13:14:24 -0700 | [diff] [blame] | 733 | { |
| 734 | // The StartThreadBirth was called in the parent thread. We let the runtime know we are up |
| 735 | // before going into the provided code. |
| 736 | art::MutexLock mu(art::Thread::Current(), *art::Locks::runtime_shutdown_lock_); |
| 737 | art::Runtime::Current()->EndThreadBirth(); |
| 738 | } |
| 739 | |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 740 | // Run the agent code. |
| 741 | data->proc(data->jvmti_env, env, const_cast<void*>(data->arg)); |
| 742 | |
| 743 | // Detach the thread. |
| 744 | int detach_result = data->java_vm->DetachCurrentThread(); |
| 745 | CHECK_EQ(detach_result, 0); |
| 746 | |
| 747 | return nullptr; |
| 748 | } |
| 749 | |
| 750 | jvmtiError ThreadUtil::RunAgentThread(jvmtiEnv* jvmti_env, |
| 751 | jthread thread, |
| 752 | jvmtiStartFunction proc, |
| 753 | const void* arg, |
| 754 | jint priority) { |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame] | 755 | if (!PhaseUtil::IsLivePhase()) { |
| 756 | return ERR(WRONG_PHASE); |
| 757 | } |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 758 | if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) { |
| 759 | return ERR(INVALID_PRIORITY); |
| 760 | } |
| 761 | JNIEnv* env = art::Thread::Current()->GetJniEnv(); |
| 762 | if (thread == nullptr || !env->IsInstanceOf(thread, art::WellKnownClasses::java_lang_Thread)) { |
| 763 | return ERR(INVALID_THREAD); |
| 764 | } |
| 765 | if (proc == nullptr) { |
| 766 | return ERR(NULL_POINTER); |
| 767 | } |
| 768 | |
Alex Light | 739bf72 | 2017-10-20 13:14:24 -0700 | [diff] [blame] | 769 | { |
| 770 | art::Runtime* runtime = art::Runtime::Current(); |
| 771 | art::MutexLock mu(art::Thread::Current(), *art::Locks::runtime_shutdown_lock_); |
| 772 | if (runtime->IsShuttingDownLocked()) { |
| 773 | // The runtime is shutting down so we cannot create new threads. |
| 774 | // TODO It's not fully clear from the spec what we should do here. We aren't yet in |
| 775 | // JVMTI_PHASE_DEAD so we cannot return ERR(WRONG_PHASE) but creating new threads is now |
| 776 | // impossible. Existing agents don't seem to generally do anything with this return value so |
| 777 | // it doesn't matter too much. We could do something like sending a fake ThreadStart event |
| 778 | // even though code is never actually run. |
| 779 | return ERR(INTERNAL); |
| 780 | } |
| 781 | runtime->StartThreadBirth(); |
| 782 | } |
| 783 | |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 784 | std::unique_ptr<AgentData> data(new AgentData); |
| 785 | data->arg = arg; |
| 786 | data->proc = proc; |
| 787 | // We need a global ref for Java objects, as local refs will be invalid. |
| 788 | data->thread = env->NewGlobalRef(thread); |
| 789 | data->java_vm = art::Runtime::Current()->GetJavaVM(); |
| 790 | data->jvmti_env = jvmti_env; |
| 791 | data->priority = priority; |
Alex Light | 9311297 | 2017-11-13 10:38:59 -0800 | [diff] [blame] | 792 | ScopedLocalRef<jstring> s( |
| 793 | env, |
| 794 | reinterpret_cast<jstring>( |
| 795 | env->GetObjectField(thread, art::WellKnownClasses::java_lang_Thread_name))); |
| 796 | if (s == nullptr) { |
| 797 | data->name = "JVMTI Agent Thread"; |
| 798 | } else { |
| 799 | ScopedUtfChars name(env, s.get()); |
| 800 | data->name = name.c_str(); |
| 801 | } |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 802 | |
| 803 | pthread_t pthread; |
| 804 | int pthread_create_result = pthread_create(&pthread, |
Alex Light | 739bf72 | 2017-10-20 13:14:24 -0700 | [diff] [blame] | 805 | nullptr, |
| 806 | &AgentCallback, |
| 807 | reinterpret_cast<void*>(data.get())); |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 808 | if (pthread_create_result != 0) { |
Alex Light | 739bf72 | 2017-10-20 13:14:24 -0700 | [diff] [blame] | 809 | // If the create succeeded the other thread will call EndThreadBirth. |
| 810 | art::Runtime* runtime = art::Runtime::Current(); |
| 811 | art::MutexLock mu(art::Thread::Current(), *art::Locks::runtime_shutdown_lock_); |
| 812 | runtime->EndThreadBirth(); |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 813 | return ERR(INTERNAL); |
| 814 | } |
Andreas Gampe | afaf7f8 | 2018-10-16 11:32:38 -0700 | [diff] [blame] | 815 | data.release(); // NOLINT pthreads API. |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 816 | |
| 817 | return ERR(NONE); |
| 818 | } |
| 819 | |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 820 | jvmtiError ThreadUtil::SuspendOther(art::Thread* self, |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 821 | jthread target_jthread) { |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 822 | // Loop since we need to bail out and try again if we would end up getting suspended while holding |
| 823 | // the user_code_suspension_lock_ due to a SuspendReason::kForUserCode. In this situation we |
| 824 | // release the lock, wait to get resumed and try again. |
| 825 | do { |
| 826 | // Suspend ourself if we have any outstanding suspends. This is so we won't suspend due to |
| 827 | // another SuspendThread in the middle of suspending something else potentially causing a |
| 828 | // deadlock. We need to do this in the loop because if we ended up back here then we had |
| 829 | // outstanding SuspendReason::kForUserCode suspensions and we should wait for them to be cleared |
| 830 | // before continuing. |
| 831 | SuspendCheck(self); |
| 832 | art::MutexLock mu(self, *art::Locks::user_code_suspension_lock_); |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame] | 833 | if (WouldSuspendForUserCodeLocked(self)) { |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 834 | // Make sure we won't be suspended in the middle of holding the thread_suspend_count_lock_ by |
| 835 | // 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] | 836 | continue; |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 837 | } |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame] | 838 | // 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] | 839 | { |
| 840 | art::ScopedObjectAccess soa(self); |
| 841 | art::MutexLock thread_list_mu(self, *art::Locks::thread_list_lock_); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 842 | art::Thread* target = nullptr; |
| 843 | jvmtiError err = ERR(INTERNAL); |
| 844 | if (!GetAliveNativeThread(target_jthread, soa, &target, &err)) { |
| 845 | return err; |
| 846 | } |
Alex Light | cea4215 | 2018-09-18 22:51:55 +0000 | [diff] [blame] | 847 | art::ThreadState state = target->GetState(); |
| 848 | if (state == art::ThreadState::kStarting || target->IsStillStarting()) { |
| 849 | return ERR(THREAD_NOT_ALIVE); |
| 850 | } else { |
| 851 | art::MutexLock thread_suspend_count_mu(self, *art::Locks::thread_suspend_count_lock_); |
| 852 | if (target->GetUserCodeSuspendCount() != 0) { |
| 853 | return ERR(THREAD_SUSPENDED); |
| 854 | } |
| 855 | } |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 856 | } |
Alex Light | cea4215 | 2018-09-18 22:51:55 +0000 | [diff] [blame] | 857 | bool timeout = true; |
| 858 | art::Thread* ret_target = art::Runtime::Current()->GetThreadList()->SuspendThreadByPeer( |
| 859 | target_jthread, |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 860 | /* request_suspension= */ true, |
Alex Light | cea4215 | 2018-09-18 22:51:55 +0000 | [diff] [blame] | 861 | art::SuspendReason::kForUserCode, |
| 862 | &timeout); |
| 863 | if (ret_target == nullptr && !timeout) { |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 864 | // TODO It would be good to get more information about why exactly the thread failed to |
| 865 | // suspend. |
| 866 | return ERR(INTERNAL); |
Alex Light | cea4215 | 2018-09-18 22:51:55 +0000 | [diff] [blame] | 867 | } else if (!timeout) { |
| 868 | // we didn't time out and got a result. |
| 869 | return OK; |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 870 | } |
| 871 | // We timed out. Just go around and try again. |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 872 | } while (true); |
| 873 | UNREACHABLE(); |
| 874 | } |
| 875 | |
| 876 | jvmtiError ThreadUtil::SuspendSelf(art::Thread* self) { |
| 877 | CHECK(self == art::Thread::Current()); |
| 878 | { |
| 879 | art::MutexLock mu(self, *art::Locks::user_code_suspension_lock_); |
| 880 | art::MutexLock thread_list_mu(self, *art::Locks::thread_suspend_count_lock_); |
| 881 | if (self->GetUserCodeSuspendCount() != 0) { |
| 882 | // This can only happen if we race with another thread to suspend 'self' and we lose. |
| 883 | return ERR(THREAD_SUSPENDED); |
| 884 | } |
| 885 | // We shouldn't be able to fail this. |
| 886 | if (!self->ModifySuspendCount(self, +1, nullptr, art::SuspendReason::kForUserCode)) { |
| 887 | // TODO More specific error would be nice. |
| 888 | return ERR(INTERNAL); |
| 889 | } |
| 890 | } |
| 891 | // Once we have requested the suspend we actually go to sleep. We need to do this after releasing |
| 892 | // the suspend_lock to make sure we can be woken up. This call gains the mutator lock causing us |
| 893 | // to go to sleep until we are resumed. |
| 894 | SuspendCheck(self); |
| 895 | return OK; |
| 896 | } |
| 897 | |
| 898 | jvmtiError ThreadUtil::SuspendThread(jvmtiEnv* env ATTRIBUTE_UNUSED, jthread thread) { |
| 899 | art::Thread* self = art::Thread::Current(); |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 900 | bool target_is_self = false; |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 901 | { |
| 902 | art::ScopedObjectAccess soa(self); |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 903 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 904 | art::Thread* target = nullptr; |
| 905 | jvmtiError err = ERR(INTERNAL); |
| 906 | if (!GetAliveNativeThread(thread, soa, &target, &err)) { |
| 907 | return err; |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 908 | } else if (target == self) { |
| 909 | target_is_self = true; |
| 910 | } |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 911 | } |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 912 | if (target_is_self) { |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 913 | return SuspendSelf(self); |
| 914 | } else { |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 915 | return SuspendOther(self, thread); |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | |
| 919 | jvmtiError ThreadUtil::ResumeThread(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 920 | jthread thread) { |
| 921 | if (thread == nullptr) { |
| 922 | return ERR(NULL_POINTER); |
| 923 | } |
| 924 | art::Thread* self = art::Thread::Current(); |
Alex Light | cea4215 | 2018-09-18 22:51:55 +0000 | [diff] [blame] | 925 | art::Thread* target; |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 926 | // Retry until we know we won't get suspended by user code while resuming something. |
| 927 | do { |
| 928 | SuspendCheck(self); |
| 929 | art::MutexLock ucsl_mu(self, *art::Locks::user_code_suspension_lock_); |
Alex Light | 23aa748 | 2017-08-16 10:01:13 -0700 | [diff] [blame] | 930 | if (WouldSuspendForUserCodeLocked(self)) { |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 931 | // Make sure we won't be suspended in the middle of holding the thread_suspend_count_lock_ by |
| 932 | // 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] | 933 | continue; |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 934 | } |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 935 | // From now on we know we cannot get suspended by user-code. |
Alex Light | cea4215 | 2018-09-18 22:51:55 +0000 | [diff] [blame] | 936 | { |
| 937 | // NB This does a SuspendCheck (during thread state change) so we need to make sure we don't |
| 938 | // have the 'suspend_lock' locked here. |
| 939 | art::ScopedObjectAccess soa(self); |
| 940 | art::MutexLock tll_mu(self, *art::Locks::thread_list_lock_); |
| 941 | jvmtiError err = ERR(INTERNAL); |
| 942 | if (!GetAliveNativeThread(thread, soa, &target, &err)) { |
| 943 | return err; |
| 944 | } else if (target == self) { |
| 945 | // We would have paused until we aren't suspended anymore due to the ScopedObjectAccess so |
| 946 | // we can just return THREAD_NOT_SUSPENDED. Unfortunately we cannot do any real DCHECKs |
| 947 | // about current state since it's all concurrent. |
| 948 | return ERR(THREAD_NOT_SUSPENDED); |
| 949 | } |
| 950 | // The JVMTI spec requires us to return THREAD_NOT_SUSPENDED if it is alive but we really |
| 951 | // cannot tell why resume failed. |
| 952 | { |
| 953 | art::MutexLock thread_suspend_count_mu(self, *art::Locks::thread_suspend_count_lock_); |
| 954 | if (target->GetUserCodeSuspendCount() == 0) { |
| 955 | return ERR(THREAD_NOT_SUSPENDED); |
| 956 | } |
| 957 | } |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 958 | } |
Alex Light | cea4215 | 2018-09-18 22:51:55 +0000 | [diff] [blame] | 959 | // It is okay that we don't have a thread_list_lock here since we know that the thread cannot |
| 960 | // die since it is currently held suspended by a SuspendReason::kForUserCode suspend. |
| 961 | DCHECK(target != self); |
| 962 | if (!art::Runtime::Current()->GetThreadList()->Resume(target, |
| 963 | art::SuspendReason::kForUserCode)) { |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 964 | // TODO Give a better error. |
Alex Light | cea4215 | 2018-09-18 22:51:55 +0000 | [diff] [blame] | 965 | // This is most likely THREAD_NOT_SUSPENDED but we cannot really be sure. |
Alex Light | 3ae8253 | 2017-07-26 13:59:07 -0700 | [diff] [blame] | 966 | return ERR(INTERNAL); |
| 967 | } else { |
| 968 | return OK; |
| 969 | } |
| 970 | } while (true); |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 973 | static bool IsCurrentThread(jthread thr) { |
| 974 | if (thr == nullptr) { |
| 975 | return true; |
| 976 | } |
| 977 | art::Thread* self = art::Thread::Current(); |
| 978 | art::ScopedObjectAccess soa(self); |
| 979 | art::MutexLock mu(self, *art::Locks::thread_list_lock_); |
| 980 | art::Thread* target = nullptr; |
| 981 | jvmtiError err_unused = ERR(INTERNAL); |
| 982 | if (ThreadUtil::GetNativeThread(thr, soa, &target, &err_unused)) { |
| 983 | return target == self; |
| 984 | } else { |
| 985 | return false; |
| 986 | } |
| 987 | } |
| 988 | |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 989 | // Suspends all the threads in the list at the same time. Getting this behavior is a little tricky |
| 990 | // since we can have threads in the list multiple times. This generally doesn't matter unless the |
| 991 | // current thread is present multiple times. In that case we need to suspend only once and either |
| 992 | // return the same error code in all the other slots if it failed or return ERR(THREAD_SUSPENDED) if |
| 993 | // it didn't. We also want to handle the current thread last to make the behavior of the code |
| 994 | // simpler to understand. |
| 995 | jvmtiError ThreadUtil::SuspendThreadList(jvmtiEnv* env, |
| 996 | jint request_count, |
| 997 | const jthread* threads, |
| 998 | jvmtiError* results) { |
| 999 | if (request_count == 0) { |
| 1000 | return ERR(ILLEGAL_ARGUMENT); |
| 1001 | } else if (results == nullptr || threads == nullptr) { |
| 1002 | return ERR(NULL_POINTER); |
| 1003 | } |
| 1004 | // This is the list of the indexes in 'threads' and 'results' that correspond to the currently |
| 1005 | // running thread. These indexes we need to handle specially since we need to only actually |
| 1006 | // suspend a single time. |
| 1007 | std::vector<jint> current_thread_indexes; |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 1008 | for (jint i = 0; i < request_count; i++) { |
Alex Light | 7ddc23d | 2017-09-22 15:33:41 -0700 | [diff] [blame] | 1009 | if (IsCurrentThread(threads[i])) { |
| 1010 | current_thread_indexes.push_back(i); |
| 1011 | } else { |
| 1012 | results[i] = env->SuspendThread(threads[i]); |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 1013 | } |
Alex Light | 88fd720 | 2017-06-30 08:31:59 -0700 | [diff] [blame] | 1014 | } |
| 1015 | if (!current_thread_indexes.empty()) { |
| 1016 | jint first_current_thread_index = current_thread_indexes[0]; |
| 1017 | // Suspend self. |
| 1018 | jvmtiError res = env->SuspendThread(threads[first_current_thread_index]); |
| 1019 | results[first_current_thread_index] = res; |
| 1020 | // Fill in the rest of the error values as appropriate. |
| 1021 | jvmtiError other_results = (res != OK) ? res : ERR(THREAD_SUSPENDED); |
| 1022 | for (auto it = ++current_thread_indexes.begin(); it != current_thread_indexes.end(); ++it) { |
| 1023 | results[*it] = other_results; |
| 1024 | } |
| 1025 | } |
| 1026 | return OK; |
| 1027 | } |
| 1028 | |
| 1029 | jvmtiError ThreadUtil::ResumeThreadList(jvmtiEnv* env, |
| 1030 | jint request_count, |
| 1031 | const jthread* threads, |
| 1032 | jvmtiError* results) { |
| 1033 | if (request_count == 0) { |
| 1034 | return ERR(ILLEGAL_ARGUMENT); |
| 1035 | } else if (results == nullptr || threads == nullptr) { |
| 1036 | return ERR(NULL_POINTER); |
| 1037 | } |
| 1038 | for (jint i = 0; i < request_count; i++) { |
| 1039 | results[i] = env->ResumeThread(threads[i]); |
| 1040 | } |
| 1041 | return OK; |
| 1042 | } |
| 1043 | |
Alex Light | 54d39dc | 2017-09-25 17:00:16 -0700 | [diff] [blame] | 1044 | jvmtiError ThreadUtil::StopThread(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 1045 | jthread thread, |
| 1046 | jobject exception) { |
| 1047 | art::Thread* self = art::Thread::Current(); |
| 1048 | art::ScopedObjectAccess soa(self); |
| 1049 | art::StackHandleScope<1> hs(self); |
| 1050 | if (exception == nullptr) { |
| 1051 | return ERR(INVALID_OBJECT); |
| 1052 | } |
| 1053 | art::ObjPtr<art::mirror::Object> obj(soa.Decode<art::mirror::Object>(exception)); |
| 1054 | if (!obj->GetClass()->IsThrowableClass()) { |
| 1055 | return ERR(INVALID_OBJECT); |
| 1056 | } |
| 1057 | art::Handle<art::mirror::Throwable> exc(hs.NewHandle(obj->AsThrowable())); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 1058 | art::Locks::thread_list_lock_->ExclusiveLock(self); |
Alex Light | 54d39dc | 2017-09-25 17:00:16 -0700 | [diff] [blame] | 1059 | art::Thread* target = nullptr; |
| 1060 | jvmtiError err = ERR(INTERNAL); |
| 1061 | if (!GetAliveNativeThread(thread, soa, &target, &err)) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 1062 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 54d39dc | 2017-09-25 17:00:16 -0700 | [diff] [blame] | 1063 | return err; |
| 1064 | } else if (target->GetState() == art::ThreadState::kStarting || target->IsStillStarting()) { |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 1065 | art::Locks::thread_list_lock_->ExclusiveUnlock(self); |
Alex Light | 54d39dc | 2017-09-25 17:00:16 -0700 | [diff] [blame] | 1066 | return ERR(THREAD_NOT_ALIVE); |
| 1067 | } |
| 1068 | struct StopThreadClosure : public art::Closure { |
| 1069 | public: |
| 1070 | explicit StopThreadClosure(art::Handle<art::mirror::Throwable> except) : exception_(except) { } |
| 1071 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 1072 | void Run(art::Thread* me) override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 54d39dc | 2017-09-25 17:00:16 -0700 | [diff] [blame] | 1073 | // Make sure the thread is prepared to notice the exception. |
| 1074 | art::Runtime::Current()->GetInstrumentation()->InstrumentThreadStack(me); |
| 1075 | me->SetAsyncException(exception_.Get()); |
| 1076 | // Wake up the thread if it is sleeping. |
| 1077 | me->Notify(); |
| 1078 | } |
| 1079 | |
| 1080 | private: |
| 1081 | art::Handle<art::mirror::Throwable> exception_; |
| 1082 | }; |
| 1083 | StopThreadClosure c(exc); |
Alex Light | b1e31a8 | 2017-10-04 16:57:36 -0700 | [diff] [blame] | 1084 | // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its execution. |
Alex Light | 318afe6 | 2018-03-22 16:50:10 -0700 | [diff] [blame] | 1085 | if (target->RequestSynchronousCheckpoint(&c)) { |
Alex Light | 54d39dc | 2017-09-25 17:00:16 -0700 | [diff] [blame] | 1086 | return OK; |
| 1087 | } else { |
| 1088 | // Something went wrong, probably the thread died. |
| 1089 | return ERR(THREAD_NOT_ALIVE); |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | jvmtiError ThreadUtil::InterruptThread(jvmtiEnv* env ATTRIBUTE_UNUSED, jthread thread) { |
| 1094 | art::Thread* self = art::Thread::Current(); |
| 1095 | art::ScopedObjectAccess soa(self); |
| 1096 | art::MutexLock tll_mu(self, *art::Locks::thread_list_lock_); |
| 1097 | art::Thread* target = nullptr; |
| 1098 | jvmtiError err = ERR(INTERNAL); |
| 1099 | if (!GetAliveNativeThread(thread, soa, &target, &err)) { |
| 1100 | return err; |
| 1101 | } else if (target->GetState() == art::ThreadState::kStarting || target->IsStillStarting()) { |
| 1102 | return ERR(THREAD_NOT_ALIVE); |
| 1103 | } |
| 1104 | target->Interrupt(self); |
| 1105 | return OK; |
| 1106 | } |
| 1107 | |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 1108 | } // namespace openjdkjvmti |