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 | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 35 | #include "art_field.h" |
| 36 | #include "art_jvmti.h" |
| 37 | #include "base/logging.h" |
| 38 | #include "base/mutex.h" |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame^] | 39 | #include "events-inl.h" |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 40 | #include "gc/system_weak.h" |
| 41 | #include "gc_root-inl.h" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 42 | #include "jni_internal.h" |
| 43 | #include "mirror/class.h" |
| 44 | #include "mirror/object-inl.h" |
| 45 | #include "mirror/string.h" |
| 46 | #include "obj_ptr.h" |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 47 | #include "runtime.h" |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame^] | 48 | #include "runtime_callbacks.h" |
| 49 | #include "ScopedLocalRef.h" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 50 | #include "scoped_thread_state_change-inl.h" |
| 51 | #include "thread-inl.h" |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 52 | #include "thread_list.h" |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 53 | #include "well_known_classes.h" |
| 54 | |
| 55 | namespace openjdkjvmti { |
| 56 | |
Andreas Gampe | eafaf57 | 2017-01-20 12:34:15 -0800 | [diff] [blame^] | 57 | struct ThreadCallback : public art::ThreadLifecycleCallback, public art::RuntimePhaseCallback { |
| 58 | jthread GetThreadObject(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 59 | if (self->GetPeer() == nullptr) { |
| 60 | return nullptr; |
| 61 | } |
| 62 | return self->GetJniEnv()->AddLocalReference<jthread>(self->GetPeer()); |
| 63 | } |
| 64 | void Post(art::Thread* self, ArtJvmtiEvent type) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 65 | DCHECK_EQ(self, art::Thread::Current()); |
| 66 | ScopedLocalRef<jthread> thread(self->GetJniEnv(), GetThreadObject(self)); |
| 67 | event_handler->DispatchEvent(self, type, self->GetJniEnv(), thread.get()); |
| 68 | } |
| 69 | |
| 70 | void ThreadStart(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 71 | if (!started) { |
| 72 | // Runtime isn't started. We only expect at most the signal handler or JIT threads to be |
| 73 | // started here. |
| 74 | if (art::kIsDebugBuild) { |
| 75 | std::string name; |
| 76 | self->GetThreadName(name); |
| 77 | if (name != "Signal Catcher" && !android::base::StartsWith(name, "Jit thread pool")) { |
| 78 | LOG(FATAL) << "Unexpected thread before start: " << name; |
| 79 | } |
| 80 | } |
| 81 | return; |
| 82 | } |
| 83 | Post(self, ArtJvmtiEvent::kThreadStart); |
| 84 | } |
| 85 | |
| 86 | void ThreadDeath(art::Thread* self) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 87 | Post(self, ArtJvmtiEvent::kThreadEnd); |
| 88 | } |
| 89 | |
| 90 | void NextRuntimePhase(RuntimePhase phase) OVERRIDE REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 91 | if (phase == RuntimePhase::kInit) { |
| 92 | // We moved to VMInit. Report the main thread as started (it was attached early, and must |
| 93 | // not be reported until Init. |
| 94 | started = true; |
| 95 | Post(art::Thread::Current(), ArtJvmtiEvent::kThreadStart); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | EventHandler* event_handler = nullptr; |
| 100 | bool started = false; |
| 101 | }; |
| 102 | |
| 103 | ThreadCallback gThreadCallback; |
| 104 | |
| 105 | void ThreadUtil::Register(EventHandler* handler) { |
| 106 | art::Runtime* runtime = art::Runtime::Current(); |
| 107 | |
| 108 | gThreadCallback.started = runtime->IsStarted(); |
| 109 | gThreadCallback.event_handler = handler; |
| 110 | |
| 111 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 112 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 113 | art::ScopedSuspendAll ssa("Add thread callback"); |
| 114 | runtime->GetRuntimeCallbacks()->AddThreadLifecycleCallback(&gThreadCallback); |
| 115 | runtime->GetRuntimeCallbacks()->AddRuntimePhaseCallback(&gThreadCallback); |
| 116 | } |
| 117 | |
| 118 | void ThreadUtil::Unregister() { |
| 119 | art::ScopedThreadStateChange stsc(art::Thread::Current(), |
| 120 | art::ThreadState::kWaitingForDebuggerToAttach); |
| 121 | art::ScopedSuspendAll ssa("Remove thread callback"); |
| 122 | art::Runtime* runtime = art::Runtime::Current(); |
| 123 | runtime->GetRuntimeCallbacks()->RemoveThreadLifecycleCallback(&gThreadCallback); |
| 124 | runtime->GetRuntimeCallbacks()->RemoveRuntimePhaseCallback(&gThreadCallback); |
| 125 | } |
| 126 | |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 127 | jvmtiError ThreadUtil::GetCurrentThread(jvmtiEnv* env ATTRIBUTE_UNUSED, jthread* thread_ptr) { |
| 128 | art::Thread* self = art::Thread::Current(); |
| 129 | |
| 130 | art::ScopedObjectAccess soa(self); |
| 131 | |
| 132 | jthread thread_peer; |
| 133 | if (self->IsStillStarting()) { |
| 134 | thread_peer = nullptr; |
| 135 | } else { |
| 136 | thread_peer = soa.AddLocalReference<jthread>(self->GetPeer()); |
| 137 | } |
| 138 | |
| 139 | *thread_ptr = thread_peer; |
| 140 | return ERR(NONE); |
| 141 | } |
| 142 | |
| 143 | // Read the context classloader from a Java thread object. This is a lazy implementation |
| 144 | // that assumes GetThreadInfo isn't called too often. If we instead cache the ArtField, |
| 145 | // we will have to add synchronization as this can't be cached on startup (which is |
| 146 | // potentially runtime startup). |
| 147 | static art::ObjPtr<art::mirror::Object> GetContextClassLoader(art::ObjPtr<art::mirror::Object> peer) |
| 148 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 149 | if (peer == nullptr) { |
| 150 | return nullptr; |
| 151 | } |
| 152 | art::ObjPtr<art::mirror::Class> klass = peer->GetClass(); |
| 153 | art::ArtField* cc_field = klass->FindDeclaredInstanceField("contextClassLoader", |
| 154 | "Ljava/lang/ClassLoader;"); |
| 155 | CHECK(cc_field != nullptr); |
| 156 | return cc_field->GetObject(peer); |
| 157 | } |
| 158 | |
| 159 | // Get the native thread. The spec says a null object denotes the current thread. |
| 160 | static art::Thread* GetNativeThread(jthread thread, |
| 161 | const art::ScopedObjectAccessAlreadyRunnable& soa) |
| 162 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 163 | if (thread == nullptr) { |
| 164 | return art::Thread::Current(); |
| 165 | } |
| 166 | |
| 167 | art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); |
| 168 | return art::Thread::FromManagedThread(soa, thread); |
| 169 | } |
| 170 | |
| 171 | jvmtiError ThreadUtil::GetThreadInfo(jvmtiEnv* env, jthread thread, jvmtiThreadInfo* info_ptr) { |
| 172 | if (info_ptr == nullptr) { |
| 173 | return ERR(NULL_POINTER); |
| 174 | } |
| 175 | |
| 176 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 177 | |
| 178 | art::Thread* self = GetNativeThread(thread, soa); |
| 179 | if (self == nullptr && thread == nullptr) { |
| 180 | return ERR(INVALID_THREAD); |
| 181 | } |
| 182 | |
| 183 | JvmtiUniquePtr name_uptr; |
| 184 | if (self != nullptr) { |
| 185 | // Have a native thread object, this thread is alive. |
| 186 | std::string name; |
| 187 | self->GetThreadName(name); |
| 188 | jvmtiError name_result = CopyString( |
| 189 | env, name.c_str(), reinterpret_cast<unsigned char**>(&info_ptr->name)); |
| 190 | if (name_result != ERR(NONE)) { |
| 191 | return name_result; |
| 192 | } |
| 193 | name_uptr = MakeJvmtiUniquePtr(env, info_ptr->name); |
| 194 | |
| 195 | info_ptr->priority = self->GetNativePriority(); |
| 196 | |
| 197 | info_ptr->is_daemon = self->IsDaemon(); |
| 198 | |
| 199 | art::ObjPtr<art::mirror::Object> peer = self->GetPeer(); |
| 200 | |
| 201 | // ThreadGroup. |
| 202 | if (peer != nullptr) { |
| 203 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_group); |
| 204 | CHECK(f != nullptr); |
| 205 | art::ObjPtr<art::mirror::Object> group = f->GetObject(peer); |
| 206 | info_ptr->thread_group = group == nullptr |
| 207 | ? nullptr |
| 208 | : soa.AddLocalReference<jthreadGroup>(group); |
| 209 | } else { |
| 210 | info_ptr->thread_group = nullptr; |
| 211 | } |
| 212 | |
| 213 | // Context classloader. |
| 214 | art::ObjPtr<art::mirror::Object> ccl = GetContextClassLoader(peer); |
| 215 | info_ptr->context_class_loader = ccl == nullptr |
| 216 | ? nullptr |
| 217 | : soa.AddLocalReference<jobject>(ccl); |
| 218 | } else { |
| 219 | // Only the peer. This thread has either not been started, or is dead. Read things from |
| 220 | // the Java side. |
| 221 | art::ObjPtr<art::mirror::Object> peer = soa.Decode<art::mirror::Object>(thread); |
| 222 | |
| 223 | // Name. |
| 224 | { |
| 225 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_name); |
| 226 | CHECK(f != nullptr); |
| 227 | art::ObjPtr<art::mirror::Object> name = f->GetObject(peer); |
| 228 | std::string name_cpp; |
| 229 | const char* name_cstr; |
| 230 | if (name != nullptr) { |
| 231 | name_cpp = name->AsString()->ToModifiedUtf8(); |
| 232 | name_cstr = name_cpp.c_str(); |
| 233 | } else { |
| 234 | name_cstr = ""; |
| 235 | } |
| 236 | jvmtiError name_result = CopyString( |
| 237 | env, name_cstr, reinterpret_cast<unsigned char**>(&info_ptr->name)); |
| 238 | if (name_result != ERR(NONE)) { |
| 239 | return name_result; |
| 240 | } |
| 241 | name_uptr = MakeJvmtiUniquePtr(env, info_ptr->name); |
| 242 | } |
| 243 | |
| 244 | // Priority. |
| 245 | { |
| 246 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_priority); |
| 247 | CHECK(f != nullptr); |
| 248 | info_ptr->priority = static_cast<jint>(f->GetInt(peer)); |
| 249 | } |
| 250 | |
| 251 | // Daemon. |
| 252 | { |
| 253 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_daemon); |
| 254 | CHECK(f != nullptr); |
| 255 | info_ptr->is_daemon = f->GetBoolean(peer) == 0 ? JNI_FALSE : JNI_TRUE; |
| 256 | } |
| 257 | |
| 258 | // ThreadGroup. |
| 259 | { |
| 260 | art::ArtField* f = art::jni::DecodeArtField(art::WellKnownClasses::java_lang_Thread_group); |
| 261 | CHECK(f != nullptr); |
| 262 | art::ObjPtr<art::mirror::Object> group = f->GetObject(peer); |
| 263 | info_ptr->thread_group = group == nullptr |
| 264 | ? nullptr |
| 265 | : soa.AddLocalReference<jthreadGroup>(group); |
| 266 | } |
| 267 | |
| 268 | // Context classloader. |
| 269 | art::ObjPtr<art::mirror::Object> ccl = GetContextClassLoader(peer); |
| 270 | info_ptr->context_class_loader = ccl == nullptr |
| 271 | ? nullptr |
| 272 | : soa.AddLocalReference<jobject>(ccl); |
| 273 | } |
| 274 | |
| 275 | name_uptr.release(); |
| 276 | |
| 277 | return ERR(NONE); |
| 278 | } |
| 279 | |
Andreas Gampe | 72c1983 | 2017-01-12 13:22:16 -0800 | [diff] [blame] | 280 | // Return the thread's (or current thread, if null) thread state. Return kStarting in case |
| 281 | // there's no native counterpart (thread hasn't been started, yet, or is dead). |
| 282 | static art::ThreadState GetNativeThreadState(jthread thread, |
| 283 | const art::ScopedObjectAccessAlreadyRunnable& soa, |
| 284 | art::Thread** native_thread) |
| 285 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 286 | art::Thread* self = nullptr; |
| 287 | art::MutexLock mu(soa.Self(), *art::Locks::thread_list_lock_); |
| 288 | if (thread == nullptr) { |
| 289 | self = art::Thread::Current(); |
| 290 | } else { |
| 291 | self = art::Thread::FromManagedThread(soa, thread); |
| 292 | } |
| 293 | *native_thread = self; |
| 294 | if (self == nullptr || self->IsStillStarting()) { |
| 295 | return art::ThreadState::kStarting; |
| 296 | } |
| 297 | return self->GetState(); |
| 298 | } |
| 299 | |
| 300 | static jint GetJvmtiThreadStateFromInternal(art::ThreadState internal_thread_state) { |
| 301 | jint jvmti_state = JVMTI_THREAD_STATE_ALIVE; |
| 302 | |
| 303 | if (internal_thread_state == art::ThreadState::kSuspended) { |
| 304 | jvmti_state |= JVMTI_THREAD_STATE_SUSPENDED; |
| 305 | // Note: We do not have data about the previous state. Otherwise we should load the previous |
| 306 | // state here. |
| 307 | } |
| 308 | |
| 309 | if (internal_thread_state == art::ThreadState::kNative) { |
| 310 | jvmti_state |= JVMTI_THREAD_STATE_IN_NATIVE; |
| 311 | } |
| 312 | |
| 313 | if (internal_thread_state == art::ThreadState::kRunnable || |
| 314 | internal_thread_state == art::ThreadState::kWaitingWeakGcRootRead || |
| 315 | internal_thread_state == art::ThreadState::kSuspended) { |
| 316 | jvmti_state |= JVMTI_THREAD_STATE_RUNNABLE; |
| 317 | } else if (internal_thread_state == art::ThreadState::kBlocked) { |
| 318 | jvmti_state |= JVMTI_THREAD_STATE_BLOCKED_ON_MONITOR_ENTER; |
| 319 | } else { |
| 320 | // Should be in waiting state. |
| 321 | jvmti_state |= JVMTI_THREAD_STATE_WAITING; |
| 322 | |
| 323 | if (internal_thread_state == art::ThreadState::kTimedWaiting || |
| 324 | internal_thread_state == art::ThreadState::kSleeping) { |
| 325 | jvmti_state |= JVMTI_THREAD_STATE_WAITING_WITH_TIMEOUT; |
| 326 | } else { |
| 327 | jvmti_state |= JVMTI_THREAD_STATE_WAITING_INDEFINITELY; |
| 328 | } |
| 329 | |
| 330 | if (internal_thread_state == art::ThreadState::kSleeping) { |
| 331 | jvmti_state |= JVMTI_THREAD_STATE_SLEEPING; |
| 332 | } |
| 333 | |
| 334 | if (internal_thread_state == art::ThreadState::kTimedWaiting || |
| 335 | internal_thread_state == art::ThreadState::kWaiting) { |
| 336 | jvmti_state |= JVMTI_THREAD_STATE_IN_OBJECT_WAIT; |
| 337 | } |
| 338 | |
| 339 | // TODO: PARKED. We'll have to inspect the stack. |
| 340 | } |
| 341 | |
| 342 | return jvmti_state; |
| 343 | } |
| 344 | |
| 345 | static jint GetJavaStateFromInternal(art::ThreadState internal_thread_state) { |
| 346 | switch (internal_thread_state) { |
| 347 | case art::ThreadState::kTerminated: |
| 348 | return JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED; |
| 349 | |
| 350 | case art::ThreadState::kRunnable: |
| 351 | case art::ThreadState::kNative: |
| 352 | case art::ThreadState::kWaitingWeakGcRootRead: |
| 353 | case art::ThreadState::kSuspended: |
| 354 | return JVMTI_JAVA_LANG_THREAD_STATE_RUNNABLE; |
| 355 | |
| 356 | case art::ThreadState::kTimedWaiting: |
| 357 | case art::ThreadState::kSleeping: |
| 358 | return JVMTI_JAVA_LANG_THREAD_STATE_TIMED_WAITING; |
| 359 | |
| 360 | case art::ThreadState::kBlocked: |
| 361 | return JVMTI_JAVA_LANG_THREAD_STATE_BLOCKED; |
| 362 | |
| 363 | case art::ThreadState::kStarting: |
| 364 | return JVMTI_JAVA_LANG_THREAD_STATE_NEW; |
| 365 | |
| 366 | case art::ThreadState::kWaiting: |
| 367 | case art::ThreadState::kWaitingForGcToComplete: |
| 368 | case art::ThreadState::kWaitingPerformingGc: |
| 369 | case art::ThreadState::kWaitingForCheckPointsToRun: |
| 370 | case art::ThreadState::kWaitingForDebuggerSend: |
| 371 | case art::ThreadState::kWaitingForDebuggerToAttach: |
| 372 | case art::ThreadState::kWaitingInMainDebuggerLoop: |
| 373 | case art::ThreadState::kWaitingForDebuggerSuspension: |
| 374 | case art::ThreadState::kWaitingForDeoptimization: |
| 375 | case art::ThreadState::kWaitingForGetObjectsAllocated: |
| 376 | case art::ThreadState::kWaitingForJniOnLoad: |
| 377 | case art::ThreadState::kWaitingForSignalCatcherOutput: |
| 378 | case art::ThreadState::kWaitingInMainSignalCatcherLoop: |
| 379 | case art::ThreadState::kWaitingForMethodTracingStart: |
| 380 | case art::ThreadState::kWaitingForVisitObjects: |
| 381 | case art::ThreadState::kWaitingForGcThreadFlip: |
| 382 | return JVMTI_JAVA_LANG_THREAD_STATE_WAITING; |
| 383 | } |
| 384 | LOG(FATAL) << "Unreachable"; |
| 385 | UNREACHABLE(); |
| 386 | } |
| 387 | |
| 388 | jvmtiError ThreadUtil::GetThreadState(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 389 | jthread thread, |
| 390 | jint* thread_state_ptr) { |
| 391 | if (thread_state_ptr == nullptr) { |
| 392 | return ERR(NULL_POINTER); |
| 393 | } |
| 394 | |
| 395 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 396 | art::Thread* native_thread = nullptr; |
| 397 | art::ThreadState internal_thread_state = GetNativeThreadState(thread, soa, &native_thread); |
| 398 | |
| 399 | if (internal_thread_state == art::ThreadState::kStarting) { |
| 400 | if (thread == nullptr) { |
| 401 | // No native thread, and no Java thread? We must be starting up. Report as wrong phase. |
| 402 | return ERR(WRONG_PHASE); |
| 403 | } |
| 404 | |
| 405 | // Need to read the Java "started" field to know whether this is starting or terminated. |
| 406 | art::ObjPtr<art::mirror::Object> peer = soa.Decode<art::mirror::Object>(thread); |
| 407 | art::ObjPtr<art::mirror::Class> klass = peer->GetClass(); |
| 408 | art::ArtField* started_field = klass->FindDeclaredInstanceField("started", "Z"); |
| 409 | CHECK(started_field != nullptr); |
| 410 | bool started = started_field->GetBoolean(peer) != 0; |
| 411 | constexpr jint kStartedState = JVMTI_JAVA_LANG_THREAD_STATE_NEW; |
| 412 | constexpr jint kTerminatedState = JVMTI_THREAD_STATE_TERMINATED | |
| 413 | JVMTI_JAVA_LANG_THREAD_STATE_TERMINATED; |
| 414 | *thread_state_ptr = started ? kTerminatedState : kStartedState; |
| 415 | return ERR(NONE); |
| 416 | } |
| 417 | DCHECK(native_thread != nullptr); |
| 418 | |
| 419 | // Translate internal thread state to JVMTI and Java state. |
| 420 | jint jvmti_state = GetJvmtiThreadStateFromInternal(internal_thread_state); |
| 421 | if (native_thread->IsInterrupted()) { |
| 422 | jvmti_state |= JVMTI_THREAD_STATE_INTERRUPTED; |
| 423 | } |
| 424 | |
| 425 | // Java state is derived from nativeGetState. |
| 426 | // Note: Our implementation assigns "runnable" to suspended. As such, we will have slightly |
| 427 | // different mask. However, this is for consistency with the Java view. |
| 428 | jint java_state = GetJavaStateFromInternal(internal_thread_state); |
| 429 | |
| 430 | *thread_state_ptr = jvmti_state | java_state; |
| 431 | |
| 432 | return ERR(NONE); |
| 433 | } |
| 434 | |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 435 | jvmtiError ThreadUtil::GetAllThreads(jvmtiEnv* env, |
| 436 | jint* threads_count_ptr, |
| 437 | jthread** threads_ptr) { |
| 438 | if (threads_count_ptr == nullptr || threads_ptr == nullptr) { |
| 439 | return ERR(NULL_POINTER); |
| 440 | } |
| 441 | |
| 442 | art::Thread* current = art::Thread::Current(); |
| 443 | |
| 444 | art::ScopedObjectAccess soa(current); |
| 445 | |
| 446 | art::MutexLock mu(current, *art::Locks::thread_list_lock_); |
| 447 | std::list<art::Thread*> thread_list = art::Runtime::Current()->GetThreadList()->GetList(); |
| 448 | |
| 449 | std::vector<art::ObjPtr<art::mirror::Object>> peers; |
| 450 | |
| 451 | for (art::Thread* thread : thread_list) { |
| 452 | // Skip threads that are still starting. |
| 453 | if (thread->IsStillStarting()) { |
| 454 | continue; |
| 455 | } |
| 456 | |
| 457 | art::ObjPtr<art::mirror::Object> peer = thread->GetPeer(); |
| 458 | if (peer != nullptr) { |
| 459 | peers.push_back(peer); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | if (peers.empty()) { |
| 464 | *threads_count_ptr = 0; |
| 465 | *threads_ptr = nullptr; |
| 466 | } else { |
| 467 | unsigned char* data; |
| 468 | jvmtiError data_result = env->Allocate(peers.size() * sizeof(jthread), &data); |
| 469 | if (data_result != ERR(NONE)) { |
| 470 | return data_result; |
| 471 | } |
| 472 | jthread* threads = reinterpret_cast<jthread*>(data); |
| 473 | for (size_t i = 0; i != peers.size(); ++i) { |
| 474 | threads[i] = soa.AddLocalReference<jthread>(peers[i]); |
| 475 | } |
| 476 | |
| 477 | *threads_count_ptr = static_cast<jint>(peers.size()); |
| 478 | *threads_ptr = threads; |
| 479 | } |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 480 | return ERR(NONE); |
| 481 | } |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 482 | |
Andreas Gampe | f26bf2d | 2017-01-13 16:47:14 -0800 | [diff] [blame] | 483 | jvmtiError ThreadUtil::SetThreadLocalStorage(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 484 | jthread thread, |
| 485 | const void* data) { |
| 486 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 487 | art::Thread* self = GetNativeThread(thread, soa); |
| 488 | if (self == nullptr && thread == nullptr) { |
| 489 | return ERR(INVALID_THREAD); |
| 490 | } |
| 491 | if (self == nullptr) { |
| 492 | return ERR(THREAD_NOT_ALIVE); |
| 493 | } |
| 494 | |
| 495 | self->SetCustomTLS(data); |
| 496 | |
| 497 | return ERR(NONE); |
| 498 | } |
| 499 | |
| 500 | jvmtiError ThreadUtil::GetThreadLocalStorage(jvmtiEnv* env ATTRIBUTE_UNUSED, |
| 501 | jthread thread, |
| 502 | void** data_ptr) { |
| 503 | if (data_ptr == nullptr) { |
| 504 | return ERR(NULL_POINTER); |
| 505 | } |
| 506 | |
| 507 | art::ScopedObjectAccess soa(art::Thread::Current()); |
| 508 | art::Thread* self = GetNativeThread(thread, soa); |
| 509 | if (self == nullptr && thread == nullptr) { |
| 510 | return ERR(INVALID_THREAD); |
| 511 | } |
| 512 | if (self == nullptr) { |
| 513 | return ERR(THREAD_NOT_ALIVE); |
| 514 | } |
| 515 | |
| 516 | *data_ptr = const_cast<void*>(self->GetCustomTLS()); |
Andreas Gampe | 8580744 | 2017-01-13 14:40:58 -0800 | [diff] [blame] | 517 | return ERR(NONE); |
| 518 | } |
| 519 | |
Andreas Gampe | 732b0ac | 2017-01-18 15:23:39 -0800 | [diff] [blame] | 520 | struct AgentData { |
| 521 | const void* arg; |
| 522 | jvmtiStartFunction proc; |
| 523 | jthread thread; |
| 524 | JavaVM* java_vm; |
| 525 | jvmtiEnv* jvmti_env; |
| 526 | jint priority; |
| 527 | }; |
| 528 | |
| 529 | static void* AgentCallback(void* arg) { |
| 530 | std::unique_ptr<AgentData> data(reinterpret_cast<AgentData*>(arg)); |
| 531 | CHECK(data->thread != nullptr); |
| 532 | |
| 533 | // We already have a peer. So call our special Attach function. |
| 534 | art::Thread* self = art::Thread::Attach("JVMTI Agent thread", true, data->thread); |
| 535 | CHECK(self != nullptr); |
| 536 | // The name in Attach() is only for logging. Set the thread name. This is important so |
| 537 | // that the thread is no longer seen as starting up. |
| 538 | { |
| 539 | art::ScopedObjectAccess soa(self); |
| 540 | self->SetThreadName("JVMTI Agent thread"); |
| 541 | } |
| 542 | |
| 543 | // Release the peer. |
| 544 | JNIEnv* env = self->GetJniEnv(); |
| 545 | env->DeleteGlobalRef(data->thread); |
| 546 | data->thread = nullptr; |
| 547 | |
| 548 | // Run the agent code. |
| 549 | data->proc(data->jvmti_env, env, const_cast<void*>(data->arg)); |
| 550 | |
| 551 | // Detach the thread. |
| 552 | int detach_result = data->java_vm->DetachCurrentThread(); |
| 553 | CHECK_EQ(detach_result, 0); |
| 554 | |
| 555 | return nullptr; |
| 556 | } |
| 557 | |
| 558 | jvmtiError ThreadUtil::RunAgentThread(jvmtiEnv* jvmti_env, |
| 559 | jthread thread, |
| 560 | jvmtiStartFunction proc, |
| 561 | const void* arg, |
| 562 | jint priority) { |
| 563 | if (priority < JVMTI_THREAD_MIN_PRIORITY || priority > JVMTI_THREAD_MAX_PRIORITY) { |
| 564 | return ERR(INVALID_PRIORITY); |
| 565 | } |
| 566 | JNIEnv* env = art::Thread::Current()->GetJniEnv(); |
| 567 | if (thread == nullptr || !env->IsInstanceOf(thread, art::WellKnownClasses::java_lang_Thread)) { |
| 568 | return ERR(INVALID_THREAD); |
| 569 | } |
| 570 | if (proc == nullptr) { |
| 571 | return ERR(NULL_POINTER); |
| 572 | } |
| 573 | |
| 574 | std::unique_ptr<AgentData> data(new AgentData); |
| 575 | data->arg = arg; |
| 576 | data->proc = proc; |
| 577 | // We need a global ref for Java objects, as local refs will be invalid. |
| 578 | data->thread = env->NewGlobalRef(thread); |
| 579 | data->java_vm = art::Runtime::Current()->GetJavaVM(); |
| 580 | data->jvmti_env = jvmti_env; |
| 581 | data->priority = priority; |
| 582 | |
| 583 | pthread_t pthread; |
| 584 | int pthread_create_result = pthread_create(&pthread, |
| 585 | nullptr, |
| 586 | &AgentCallback, |
| 587 | reinterpret_cast<void*>(data.get())); |
| 588 | if (pthread_create_result != 0) { |
| 589 | return ERR(INTERNAL); |
| 590 | } |
| 591 | data.release(); |
| 592 | |
| 593 | return ERR(NONE); |
| 594 | } |
| 595 | |
Andreas Gampe | af13ab9 | 2017-01-11 20:57:40 -0800 | [diff] [blame] | 596 | } // namespace openjdkjvmti |