Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "thread_list.h"
     18 
     19 #define ATRACE_TAG ATRACE_TAG_DALVIK
     20 
     21 #include <cutils/trace.h>
     22 #include <dirent.h>
     23 #include <ScopedLocalRef.h>
     24 #include <ScopedUtfChars.h>
     25 #include <sys/types.h>
     26 #include <unistd.h>
     27 
     28 #include "base/mutex.h"
     29 #include "base/mutex-inl.h"
     30 #include "base/timing_logger.h"
     31 #include "debugger.h"
     32 #include "jni_internal.h"
     33 #include "lock_word.h"
     34 #include "monitor.h"
     35 #include "scoped_thread_state_change.h"
     36 #include "thread.h"
     37 #include "utils.h"
     38 #include "well_known_classes.h"
     39 
     40 namespace art {
     41 
     42 static constexpr uint64_t kLongThreadSuspendThreshold = MsToNs(5);
     43 
     44 ThreadList::ThreadList()
     45     : suspend_all_count_(0), debug_suspend_all_count_(0),
     46       thread_exit_cond_("thread exit condition variable", *Locks::thread_list_lock_) {
     47   CHECK(Monitor::IsValidLockWord(LockWord::FromThinLockId(kMaxThreadId, 1)));
     48 }
     49 
     50 ThreadList::~ThreadList() {
     51   // Detach the current thread if necessary. If we failed to start, there might not be any threads.
     52   // We need to detach the current thread here in case there's another thread waiting to join with
     53   // us.
     54   bool contains = false;
     55   {
     56     Thread* self = Thread::Current();
     57     MutexLock mu(self, *Locks::thread_list_lock_);
     58     contains = Contains(self);
     59   }
     60   if (contains) {
     61     Runtime::Current()->DetachCurrentThread();
     62   }
     63 
     64   WaitForOtherNonDaemonThreadsToExit();
     65   // TODO: there's an unaddressed race here where a thread may attach during shutdown, see
     66   //       Thread::Init.
     67   SuspendAllDaemonThreads();
     68 }
     69 
     70 bool ThreadList::Contains(Thread* thread) {
     71   return find(list_.begin(), list_.end(), thread) != list_.end();
     72 }
     73 
     74 bool ThreadList::Contains(pid_t tid) {
     75   for (const auto& thread : list_) {
     76     if (thread->GetTid() == tid) {
     77       return true;
     78     }
     79   }
     80   return false;
     81 }
     82 
     83 pid_t ThreadList::GetLockOwner() {
     84   return Locks::thread_list_lock_->GetExclusiveOwnerTid();
     85 }
     86 
     87 void ThreadList::DumpNativeStacks(std::ostream& os) {
     88   MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
     89   for (const auto& thread : list_) {
     90     os << "DUMPING THREAD " << thread->GetTid() << "\n";
     91     DumpNativeStack(os, thread->GetTid(), "\t");
     92     os << "\n";
     93   }
     94 }
     95 
     96 void ThreadList::DumpForSigQuit(std::ostream& os) {
     97   {
     98     MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
     99     DumpLocked(os);
    100   }
    101   DumpUnattachedThreads(os);
    102 }
    103 
    104 static void DumpUnattachedThread(std::ostream& os, pid_t tid) NO_THREAD_SAFETY_ANALYSIS {
    105   // TODO: No thread safety analysis as DumpState with a NULL thread won't access fields, should
    106   // refactor DumpState to avoid skipping analysis.
    107   Thread::DumpState(os, NULL, tid);
    108   DumpKernelStack(os, tid, "  kernel: ", false);
    109   // TODO: Reenable this when the native code in system_server can handle it.
    110   // Currently "adb shell kill -3 `pid system_server`" will cause it to exit.
    111   if (false) {
    112     DumpNativeStack(os, tid, "  native: ");
    113   }
    114   os << "\n";
    115 }
    116 
    117 void ThreadList::DumpUnattachedThreads(std::ostream& os) {
    118   DIR* d = opendir("/proc/self/task");
    119   if (!d) {
    120     return;
    121   }
    122 
    123   Thread* self = Thread::Current();
    124   dirent* e;
    125   while ((e = readdir(d)) != NULL) {
    126     char* end;
    127     pid_t tid = strtol(e->d_name, &end, 10);
    128     if (!*end) {
    129       bool contains;
    130       {
    131         MutexLock mu(self, *Locks::thread_list_lock_);
    132         contains = Contains(tid);
    133       }
    134       if (!contains) {
    135         DumpUnattachedThread(os, tid);
    136       }
    137     }
    138   }
    139   closedir(d);
    140 }
    141 
    142 void ThreadList::DumpLocked(std::ostream& os) {
    143   os << "DALVIK THREADS (" << list_.size() << "):\n";
    144   for (const auto& thread : list_) {
    145     thread->Dump(os);
    146     os << "\n";
    147   }
    148 }
    149 
    150 void ThreadList::AssertThreadsAreSuspended(Thread* self, Thread* ignore1, Thread* ignore2) {
    151   MutexLock mu(self, *Locks::thread_list_lock_);
    152   MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
    153   for (const auto& thread : list_) {
    154     if (thread != ignore1 && thread != ignore2) {
    155       CHECK(thread->IsSuspended())
    156             << "\nUnsuspended thread: <<" << *thread << "\n"
    157             << "self: <<" << *Thread::Current();
    158     }
    159   }
    160 }
    161 
    162 #if HAVE_TIMED_RWLOCK
    163 // Attempt to rectify locks so that we dump thread list with required locks before exiting.
    164 static void UnsafeLogFatalForThreadSuspendAllTimeout() NO_THREAD_SAFETY_ANALYSIS __attribute__((noreturn));
    165 static void UnsafeLogFatalForThreadSuspendAllTimeout() {
    166   Runtime* runtime = Runtime::Current();
    167   std::ostringstream ss;
    168   ss << "Thread suspend timeout\n";
    169   runtime->GetThreadList()->DumpLocked(ss);
    170   LOG(FATAL) << ss.str();
    171   exit(0);
    172 }
    173 #endif
    174 
    175 // Unlike suspending all threads where we can wait to acquire the mutator_lock_, suspending an
    176 // individual thread requires polling. delay_us is the requested sleep and total_delay_us
    177 // accumulates the total time spent sleeping for timeouts. The first sleep is just a yield,
    178 // subsequently sleeps increase delay_us from 1ms to 500ms by doubling.
    179 static void ThreadSuspendSleep(Thread* self, useconds_t* delay_us, useconds_t* total_delay_us) {
    180   useconds_t new_delay_us = (*delay_us) * 2;
    181   CHECK_GE(new_delay_us, *delay_us);
    182   if (new_delay_us < 500000) {  // Don't allow sleeping to be more than 0.5s.
    183     *delay_us = new_delay_us;
    184   }
    185   if (*delay_us == 0) {
    186     sched_yield();
    187     // Default to 1 milliseconds (note that this gets multiplied by 2 before the first sleep).
    188     *delay_us = 500;
    189   } else {
    190     usleep(*delay_us);
    191     *total_delay_us += *delay_us;
    192   }
    193 }
    194 
    195 size_t ThreadList::RunCheckpoint(Closure* checkpoint_function) {
    196   Thread* self = Thread::Current();
    197   Locks::mutator_lock_->AssertNotExclusiveHeld(self);
    198   Locks::thread_list_lock_->AssertNotHeld(self);
    199   Locks::thread_suspend_count_lock_->AssertNotHeld(self);
    200   if (kDebugLocking) {
    201     CHECK_NE(self->GetState(), kRunnable);
    202   }
    203 
    204   std::vector<Thread*> suspended_count_modified_threads;
    205   size_t count = 0;
    206   {
    207     // Call a checkpoint function for each thread, threads which are suspend get their checkpoint
    208     // manually called.
    209     MutexLock mu(self, *Locks::thread_list_lock_);
    210     MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
    211     for (const auto& thread : list_) {
    212       if (thread != self) {
    213         while (true) {
    214           if (thread->RequestCheckpoint(checkpoint_function)) {
    215             // This thread will run its checkpoint some time in the near future.
    216             count++;
    217             break;
    218           } else {
    219             // We are probably suspended, try to make sure that we stay suspended.
    220             // The thread switched back to runnable.
    221             if (thread->GetState() == kRunnable) {
    222               // Spurious fail, try again.
    223               continue;
    224             }
    225             thread->ModifySuspendCount(self, +1, false);
    226             suspended_count_modified_threads.push_back(thread);
    227             break;
    228           }
    229         }
    230       }
    231     }
    232   }
    233 
    234   // Run the checkpoint on ourself while we wait for threads to suspend.
    235   checkpoint_function->Run(self);
    236 
    237   // Run the checkpoint on the suspended threads.
    238   for (const auto& thread : suspended_count_modified_threads) {
    239     if (!thread->IsSuspended()) {
    240       // Wait until the thread is suspended.
    241       useconds_t total_delay_us = 0;
    242       do {
    243         useconds_t delay_us = 100;
    244         ThreadSuspendSleep(self, &delay_us, &total_delay_us);
    245       } while (!thread->IsSuspended());
    246       // Shouldn't need to wait for longer than 1000 microseconds.
    247       constexpr useconds_t kLongWaitThresholdUS = 1000;
    248       if (UNLIKELY(total_delay_us > kLongWaitThresholdUS)) {
    249         LOG(WARNING) << "Waited " << total_delay_us << " us for thread suspend!";
    250       }
    251     }
    252     // We know for sure that the thread is suspended at this point.
    253     checkpoint_function->Run(thread);
    254     {
    255       MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
    256       thread->ModifySuspendCount(self, -1, false);
    257     }
    258   }
    259 
    260   {
    261     // Imitate ResumeAll, threads may be waiting on Thread::resume_cond_ since we raised their
    262     // suspend count. Now the suspend_count_ is lowered so we must do the broadcast.
    263     MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
    264     Thread::resume_cond_->Broadcast(self);
    265   }
    266 
    267   // Add one for self.
    268   return count + suspended_count_modified_threads.size() + 1;
    269 }
    270 
    271 // Request that a checkpoint function be run on all active (non-suspended)
    272 // threads.  Returns the number of successful requests.
    273 size_t ThreadList::RunCheckpointOnRunnableThreads(Closure* checkpoint_function) {
    274   Thread* self = Thread::Current();
    275   if (kIsDebugBuild) {
    276     Locks::mutator_lock_->AssertNotExclusiveHeld(self);
    277     Locks::thread_list_lock_->AssertNotHeld(self);
    278     Locks::thread_suspend_count_lock_->AssertNotHeld(self);
    279     CHECK_NE(self->GetState(), kRunnable);
    280   }
    281 
    282   size_t count = 0;
    283   {
    284     // Call a checkpoint function for each non-suspended thread.
    285     MutexLock mu(self, *Locks::thread_list_lock_);
    286     MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
    287     for (const auto& thread : list_) {
    288       if (thread != self) {
    289         if (thread->RequestCheckpoint(checkpoint_function)) {
    290           // This thread will run its checkpoint some time in the near future.
    291           count++;
    292         }
    293       }
    294     }
    295   }
    296 
    297   // Return the number of threads that will run the checkpoint function.
    298   return count;
    299 }
    300 
    301 void ThreadList::SuspendAll() {
    302   Thread* self = Thread::Current();
    303 
    304   if (self != nullptr) {
    305     VLOG(threads) << *self << " SuspendAll starting...";
    306   } else {
    307     VLOG(threads) << "Thread[null] SuspendAll starting...";
    308   }
    309   ATRACE_BEGIN("Suspending mutator threads");
    310   uint64_t start_time = NanoTime();
    311 
    312   Locks::mutator_lock_->AssertNotHeld(self);
    313   Locks::thread_list_lock_->AssertNotHeld(self);
    314   Locks::thread_suspend_count_lock_->AssertNotHeld(self);
    315   if (kDebugLocking && self != nullptr) {
    316     CHECK_NE(self->GetState(), kRunnable);
    317   }
    318   {
    319     MutexLock mu(self, *Locks::thread_list_lock_);
    320     MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
    321     // Update global suspend all state for attaching threads.
    322     ++suspend_all_count_;
    323     // Increment everybody's suspend count (except our own).
    324     for (const auto& thread : list_) {
    325       if (thread == self) {
    326         continue;
    327       }
    328       VLOG(threads) << "requesting thread suspend: " << *thread;
    329       thread->ModifySuspendCount(self, +1, false);
    330     }
    331   }
    332 
    333   // Block on the mutator lock until all Runnable threads release their share of access.
    334 #if HAVE_TIMED_RWLOCK
    335   // Timeout if we wait more than 30 seconds.
    336   if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) {
    337     UnsafeLogFatalForThreadSuspendAllTimeout();
    338   }
    339 #else
    340   Locks::mutator_lock_->ExclusiveLock(self);
    341 #endif
    342 
    343   uint64_t end_time = NanoTime();
    344   if (end_time - start_time > kLongThreadSuspendThreshold) {
    345     LOG(WARNING) << "Suspending all threads took: " << PrettyDuration(end_time - start_time);
    346   }
    347 
    348   if (kDebugLocking) {
    349     // Debug check that all threads are suspended.
    350     AssertThreadsAreSuspended(self, self);
    351   }
    352 
    353   ATRACE_END();
    354   ATRACE_BEGIN("Mutator threads suspended");
    355 
    356   if (self != nullptr) {
    357     VLOG(threads) << *self << " SuspendAll complete";
    358   } else {
    359     VLOG(threads) << "Thread[null] SuspendAll complete";
    360   }
    361 }
    362 
    363 void ThreadList::ResumeAll() {
    364   Thread* self = Thread::Current();
    365 
    366   if (self != nullptr) {
    367     VLOG(threads) << *self << " ResumeAll starting";
    368   } else {
    369     VLOG(threads) << "Thread[null] ResumeAll starting";
    370   }
    371 
    372   ATRACE_END();
    373   ATRACE_BEGIN("Resuming mutator threads");
    374 
    375   if (kDebugLocking) {
    376     // Debug check that all threads are suspended.
    377     AssertThreadsAreSuspended(self, self);
    378   }
    379 
    380   Locks::mutator_lock_->ExclusiveUnlock(self);
    381   {
    382     MutexLock mu(self, *Locks::thread_list_lock_);
    383     MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
    384     // Update global suspend all state for attaching threads.
    385     --suspend_all_count_;
    386     // Decrement the suspend counts for all threads.
    387     for (const auto& thread : list_) {
    388       if (thread == self) {
    389         continue;
    390       }
    391       thread->ModifySuspendCount(self, -1, false);
    392     }
    393 
    394     // Broadcast a notification to all suspended threads, some or all of
    395     // which may choose to wake up.  No need to wait for them.
    396     if (self != nullptr) {
    397       VLOG(threads) << *self << " ResumeAll waking others";
    398     } else {
    399       VLOG(threads) << "Thread[null] ResumeAll waking others";
    400     }
    401     Thread::resume_cond_->Broadcast(self);
    402   }
    403   ATRACE_END();
    404 
    405   if (self != nullptr) {
    406     VLOG(threads) << *self << " ResumeAll complete";
    407   } else {
    408     VLOG(threads) << "Thread[null] ResumeAll complete";
    409   }
    410 }
    411 
    412 void ThreadList::Resume(Thread* thread, bool for_debugger) {
    413   Thread* self = Thread::Current();
    414   DCHECK_NE(thread, self);
    415   VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") starting..."
    416       << (for_debugger ? " (debugger)" : "");
    417 
    418   {
    419     // To check Contains.
    420     MutexLock mu(self, *Locks::thread_list_lock_);
    421     // To check IsSuspended.
    422     MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
    423     DCHECK(thread->IsSuspended());
    424     if (!Contains(thread)) {
    425       // We only expect threads within the thread-list to have been suspended otherwise we can't
    426       // stop such threads from delete-ing themselves.
    427       LOG(ERROR) << "Resume(" << reinterpret_cast<void*>(thread)
    428           << ") thread not within thread list";
    429       return;
    430     }
    431     thread->ModifySuspendCount(self, -1, for_debugger);
    432   }
    433 
    434   {
    435     VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") waking others";
    436     MutexLock mu(self, *Locks::thread_suspend_count_lock_);
    437     Thread::resume_cond_->Broadcast(self);
    438   }
    439 
    440   VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") complete";
    441 }
    442 
    443 static void ThreadSuspendByPeerWarning(Thread* self, int level, const char* message, jobject peer) {
    444   JNIEnvExt* env = self->GetJniEnv();
    445   ScopedLocalRef<jstring>
    446       scoped_name_string(env, (jstring)env->GetObjectField(peer,
    447                                                           WellKnownClasses::java_lang_Thread_name));
    448   ScopedUtfChars scoped_name_chars(env, scoped_name_string.get());
    449   if (scoped_name_chars.c_str() == NULL) {
    450       LOG(level) << message << ": " << peer;
    451       env->ExceptionClear();
    452   } else {
    453       LOG(level) << message << ": " << peer << ":" << scoped_name_chars.c_str();
    454   }
    455 }
    456 
    457 Thread* ThreadList::SuspendThreadByPeer(jobject peer, bool request_suspension,
    458                                         bool debug_suspension, bool* timed_out) {
    459   static const useconds_t kTimeoutUs = 30 * 1000000;  // 30s.
    460   useconds_t total_delay_us = 0;
    461   useconds_t delay_us = 0;
    462   bool did_suspend_request = false;
    463   *timed_out = false;
    464   Thread* self = Thread::Current();
    465   VLOG(threads) << "SuspendThreadByPeer starting";
    466   while (true) {
    467     Thread* thread;
    468     {
    469       // Note: this will transition to runnable and potentially suspend. We ensure only one thread
    470       // is requesting another suspend, to avoid deadlock, by requiring this function be called
    471       // holding Locks::thread_list_suspend_thread_lock_. Its important this thread suspend rather
    472       // than request thread suspension, to avoid potential cycles in threads requesting each other
    473       // suspend.
    474       ScopedObjectAccess soa(self);
    475       MutexLock mu(self, *Locks::thread_list_lock_);
    476       thread = Thread::FromManagedThread(soa, peer);
    477       if (thread == nullptr) {
    478         ThreadSuspendByPeerWarning(self, WARNING, "No such thread for suspend", peer);
    479         return nullptr;
    480       }
    481       if (!Contains(thread)) {
    482         VLOG(threads) << "SuspendThreadByPeer failed for unattached thread: "
    483             << reinterpret_cast<void*>(thread);
    484         return nullptr;
    485       }
    486       VLOG(threads) << "SuspendThreadByPeer found thread: " << *thread;
    487       {
    488         MutexLock mu(self, *Locks::thread_suspend_count_lock_);
    489         if (request_suspension) {
    490           thread->ModifySuspendCount(self, +1, debug_suspension);
    491           request_suspension = false;
    492           did_suspend_request = true;
    493         } else {
    494           // If the caller isn't requesting suspension, a suspension should have already occurred.
    495           CHECK_GT(thread->GetSuspendCount(), 0);
    496         }
    497         // IsSuspended on the current thread will fail as the current thread is changed into
    498         // Runnable above. As the suspend count is now raised if this is the current thread
    499         // it will self suspend on transition to Runnable, making it hard to work with. It's simpler
    500         // to just explicitly handle the current thread in the callers to this code.
    501         CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger";
    502         // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend
    503         // count, or else we've waited and it has self suspended) or is the current thread, we're
    504         // done.
    505         if (thread->IsSuspended()) {
    506           VLOG(threads) << "SuspendThreadByPeer thread suspended: " << *thread;
    507           return thread;
    508         }
    509         if (total_delay_us >= kTimeoutUs) {
    510           ThreadSuspendByPeerWarning(self, FATAL, "Thread suspension timed out", peer);
    511           if (did_suspend_request) {
    512             thread->ModifySuspendCount(soa.Self(), -1, debug_suspension);
    513           }
    514           *timed_out = true;
    515           return nullptr;
    516         }
    517       }
    518       // Release locks and come out of runnable state.
    519     }
    520     VLOG(threads) << "SuspendThreadByPeer sleeping to allow thread chance to suspend";
    521     ThreadSuspendSleep(self, &delay_us, &total_delay_us);
    522   }
    523 }
    524 
    525 static void ThreadSuspendByThreadIdWarning(int level, const char* message, uint32_t thread_id) {
    526   LOG(level) << StringPrintf("%s: %d", message, thread_id);
    527 }
    528 
    529 Thread* ThreadList::SuspendThreadByThreadId(uint32_t thread_id, bool debug_suspension,
    530                                             bool* timed_out) {
    531   static const useconds_t kTimeoutUs = 30 * 1000000;  // 30s.
    532   useconds_t total_delay_us = 0;
    533   useconds_t delay_us = 0;
    534   *timed_out = false;
    535   Thread* suspended_thread = nullptr;
    536   Thread* self = Thread::Current();
    537   CHECK_NE(thread_id, kInvalidThreadId);
    538   VLOG(threads) << "SuspendThreadByThreadId starting";
    539   while (true) {
    540     {
    541       // Note: this will transition to runnable and potentially suspend. We ensure only one thread
    542       // is requesting another suspend, to avoid deadlock, by requiring this function be called
    543       // holding Locks::thread_list_suspend_thread_lock_. Its important this thread suspend rather
    544       // than request thread suspension, to avoid potential cycles in threads requesting each other
    545       // suspend.
    546       ScopedObjectAccess soa(self);
    547       MutexLock mu(self, *Locks::thread_list_lock_);
    548       Thread* thread = nullptr;
    549       for (const auto& it : list_) {
    550         if (it->GetThreadId() == thread_id) {
    551           thread = it;
    552           break;
    553         }
    554       }
    555       if (thread == nullptr) {
    556         CHECK(suspended_thread == nullptr) << "Suspended thread " << suspended_thread
    557             << " no longer in thread list";
    558         // There's a race in inflating a lock and the owner giving up ownership and then dying.
    559         ThreadSuspendByThreadIdWarning(WARNING, "No such thread id for suspend", thread_id);
    560         return nullptr;
    561       }
    562       VLOG(threads) << "SuspendThreadByThreadId found thread: " << *thread;
    563       DCHECK(Contains(thread));
    564       {
    565         MutexLock mu(self, *Locks::thread_suspend_count_lock_);
    566         if (suspended_thread == nullptr) {
    567           thread->ModifySuspendCount(self, +1, debug_suspension);
    568           suspended_thread = thread;
    569         } else {
    570           CHECK_EQ(suspended_thread, thread);
    571           // If the caller isn't requesting suspension, a suspension should have already occurred.
    572           CHECK_GT(thread->GetSuspendCount(), 0);
    573         }
    574         // IsSuspended on the current thread will fail as the current thread is changed into
    575         // Runnable above. As the suspend count is now raised if this is the current thread
    576         // it will self suspend on transition to Runnable, making it hard to work with. It's simpler
    577         // to just explicitly handle the current thread in the callers to this code.
    578         CHECK_NE(thread, self) << "Attempt to suspend the current thread for the debugger";
    579         // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend
    580         // count, or else we've waited and it has self suspended) or is the current thread, we're
    581         // done.
    582         if (thread->IsSuspended()) {
    583           VLOG(threads) << "SuspendThreadByThreadId thread suspended: " << *thread;
    584           return thread;
    585         }
    586         if (total_delay_us >= kTimeoutUs) {
    587           ThreadSuspendByThreadIdWarning(WARNING, "Thread suspension timed out", thread_id);
    588           if (suspended_thread != nullptr) {
    589             thread->ModifySuspendCount(soa.Self(), -1, debug_suspension);
    590           }
    591           *timed_out = true;
    592           return nullptr;
    593         }
    594       }
    595       // Release locks and come out of runnable state.
    596     }
    597     VLOG(threads) << "SuspendThreadByThreadId sleeping to allow thread chance to suspend";
    598     ThreadSuspendSleep(self, &delay_us, &total_delay_us);
    599   }
    600 }
    601 
    602 Thread* ThreadList::FindThreadByThreadId(uint32_t thin_lock_id) {
    603   Thread* self = Thread::Current();
    604   MutexLock mu(self, *Locks::thread_list_lock_);
    605   for (const auto& thread : list_) {
    606     if (thread->GetThreadId() == thin_lock_id) {
    607       CHECK(thread == self || thread->IsSuspended());
    608       return thread;
    609     }
    610   }
    611   return NULL;
    612 }
    613 
    614 void ThreadList::SuspendAllForDebugger() {
    615   Thread* self = Thread::Current();
    616   Thread* debug_thread = Dbg::GetDebugThread();
    617 
    618   VLOG(threads) << *self << " SuspendAllForDebugger starting...";
    619 
    620   {
    621     MutexLock mu(self, *Locks::thread_list_lock_);
    622     {
    623       MutexLock mu(self, *Locks::thread_suspend_count_lock_);
    624       // Update global suspend all state for attaching threads.
    625       ++suspend_all_count_;
    626       ++debug_suspend_all_count_;
    627       // Increment everybody's suspend count (except our own).
    628       for (const auto& thread : list_) {
    629         if (thread == self || thread == debug_thread) {
    630           continue;
    631         }
    632         VLOG(threads) << "requesting thread suspend: " << *thread;
    633         thread->ModifySuspendCount(self, +1, true);
    634       }
    635     }
    636   }
    637 
    638   // Block on the mutator lock until all Runnable threads release their share of access then
    639   // immediately unlock again.
    640 #if HAVE_TIMED_RWLOCK
    641   // Timeout if we wait more than 30 seconds.
    642   if (!Locks::mutator_lock_->ExclusiveLockWithTimeout(self, 30 * 1000, 0)) {
    643     UnsafeLogFatalForThreadSuspendAllTimeout();
    644   } else {
    645     Locks::mutator_lock_->ExclusiveUnlock(self);
    646   }
    647 #else
    648   Locks::mutator_lock_->ExclusiveLock(self);
    649   Locks::mutator_lock_->ExclusiveUnlock(self);
    650 #endif
    651   AssertThreadsAreSuspended(self, self, debug_thread);
    652 
    653   VLOG(threads) << *self << " SuspendAllForDebugger complete";
    654 }
    655 
    656 void ThreadList::SuspendSelfForDebugger() {
    657   Thread* self = Thread::Current();
    658 
    659   // The debugger thread must not suspend itself due to debugger activity!
    660   Thread* debug_thread = Dbg::GetDebugThread();
    661   CHECK(debug_thread != NULL);
    662   CHECK(self != debug_thread);
    663   CHECK_NE(self->GetState(), kRunnable);
    664   Locks::mutator_lock_->AssertNotHeld(self);
    665 
    666   {
    667     // Collisions with other suspends aren't really interesting. We want
    668     // to ensure that we're the only one fiddling with the suspend count
    669     // though.
    670     MutexLock mu(self, *Locks::thread_suspend_count_lock_);
    671     self->ModifySuspendCount(self, +1, true);
    672     CHECK_GT(self->GetSuspendCount(), 0);
    673   }
    674 
    675   VLOG(threads) << *self << " self-suspending (debugger)";
    676 
    677   // Tell JDWP we've completed invocation and are ready to suspend.
    678   DebugInvokeReq* pReq = self->GetInvokeReq();
    679   DCHECK(pReq != NULL);
    680   if (pReq->invoke_needed) {
    681     // Clear this before signaling.
    682     pReq->Clear();
    683 
    684     VLOG(jdwp) << "invoke complete, signaling";
    685     MutexLock mu(self, pReq->lock);
    686     pReq->cond.Signal(self);
    687   }
    688 
    689   // Tell JDWP that we've completed suspension. The JDWP thread can't
    690   // tell us to resume before we're fully asleep because we hold the
    691   // suspend count lock.
    692   Dbg::ClearWaitForEventThread();
    693 
    694   {
    695     MutexLock mu(self, *Locks::thread_suspend_count_lock_);
    696     while (self->GetSuspendCount() != 0) {
    697       Thread::resume_cond_->Wait(self);
    698       if (self->GetSuspendCount() != 0) {
    699         // The condition was signaled but we're still suspended. This
    700         // can happen when we suspend then resume all threads to
    701         // update instrumentation or compute monitor info. This can
    702         // also happen if the debugger lets go while a SIGQUIT thread
    703         // dump event is pending (assuming SignalCatcher was resumed for
    704         // just long enough to try to grab the thread-suspend lock).
    705         VLOG(jdwp) << *self << " still suspended after undo "
    706                    << "(suspend count=" << self->GetSuspendCount() << ", "
    707                    << "debug suspend count=" << self->GetDebugSuspendCount() << ")";
    708       }
    709     }
    710     CHECK_EQ(self->GetSuspendCount(), 0);
    711   }
    712 
    713   VLOG(threads) << *self << " self-reviving (debugger)";
    714 }
    715 
    716 void ThreadList::UndoDebuggerSuspensions() {
    717   Thread* self = Thread::Current();
    718 
    719   VLOG(threads) << *self << " UndoDebuggerSuspensions starting";
    720 
    721   {
    722     MutexLock mu(self, *Locks::thread_list_lock_);
    723     MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
    724     // Update global suspend all state for attaching threads.
    725     suspend_all_count_ -= debug_suspend_all_count_;
    726     debug_suspend_all_count_ = 0;
    727     // Update running threads.
    728     for (const auto& thread : list_) {
    729       if (thread == self || thread->GetDebugSuspendCount() == 0) {
    730         continue;
    731       }
    732       thread->ModifySuspendCount(self, -thread->GetDebugSuspendCount(), true);
    733     }
    734   }
    735 
    736   {
    737     MutexLock mu(self, *Locks::thread_suspend_count_lock_);
    738     Thread::resume_cond_->Broadcast(self);
    739   }
    740 
    741   VLOG(threads) << "UndoDebuggerSuspensions(" << *self << ") complete";
    742 }
    743 
    744 void ThreadList::WaitForOtherNonDaemonThreadsToExit() {
    745   Thread* self = Thread::Current();
    746   Locks::mutator_lock_->AssertNotHeld(self);
    747   bool all_threads_are_daemons;
    748   do {
    749     {
    750       // No more threads can be born after we start to shutdown.
    751       MutexLock mu(self, *Locks::runtime_shutdown_lock_);
    752       CHECK(Runtime::Current()->IsShuttingDownLocked());
    753       CHECK_EQ(Runtime::Current()->NumberOfThreadsBeingBorn(), 0U);
    754     }
    755     all_threads_are_daemons = true;
    756     MutexLock mu(self, *Locks::thread_list_lock_);
    757     for (const auto& thread : list_) {
    758       if (thread != self && !thread->IsDaemon()) {
    759         all_threads_are_daemons = false;
    760         break;
    761       }
    762     }
    763     if (!all_threads_are_daemons) {
    764       // Wait for another thread to exit before re-checking.
    765       thread_exit_cond_.Wait(self);
    766     }
    767   } while (!all_threads_are_daemons);
    768 }
    769 
    770 void ThreadList::SuspendAllDaemonThreads() {
    771   Thread* self = Thread::Current();
    772   MutexLock mu(self, *Locks::thread_list_lock_);
    773   {  // Tell all the daemons it's time to suspend.
    774     MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
    775     for (const auto& thread : list_) {
    776       // This is only run after all non-daemon threads have exited, so the remainder should all be
    777       // daemons.
    778       CHECK(thread->IsDaemon()) << *thread;
    779       if (thread != self) {
    780         thread->ModifySuspendCount(self, +1, false);
    781       }
    782     }
    783   }
    784   // Give the threads a chance to suspend, complaining if they're slow.
    785   bool have_complained = false;
    786   for (int i = 0; i < 10; ++i) {
    787     usleep(200 * 1000);
    788     bool all_suspended = true;
    789     for (const auto& thread : list_) {
    790       if (thread != self && thread->GetState() == kRunnable) {
    791         if (!have_complained) {
    792           LOG(WARNING) << "daemon thread not yet suspended: " << *thread;
    793           have_complained = true;
    794         }
    795         all_suspended = false;
    796       }
    797     }
    798     if (all_suspended) {
    799       return;
    800     }
    801   }
    802   LOG(ERROR) << "suspend all daemons failed";
    803 }
    804 void ThreadList::Register(Thread* self) {
    805   DCHECK_EQ(self, Thread::Current());
    806 
    807   if (VLOG_IS_ON(threads)) {
    808     std::ostringstream oss;
    809     self->ShortDump(oss);  // We don't hold the mutator_lock_ yet and so cannot call Dump.
    810     LOG(INFO) << "ThreadList::Register() " << *self  << "\n" << oss.str();
    811   }
    812 
    813   // Atomically add self to the thread list and make its thread_suspend_count_ reflect ongoing
    814   // SuspendAll requests.
    815   MutexLock mu(self, *Locks::thread_list_lock_);
    816   MutexLock mu2(self, *Locks::thread_suspend_count_lock_);
    817   CHECK_GE(suspend_all_count_, debug_suspend_all_count_);
    818   // Modify suspend count in increments of 1 to maintain invariants in ModifySuspendCount. While
    819   // this isn't particularly efficient the suspend counts are most commonly 0 or 1.
    820   for (int delta = debug_suspend_all_count_; delta > 0; delta--) {
    821     self->ModifySuspendCount(self, +1, true);
    822   }
    823   for (int delta = suspend_all_count_ - debug_suspend_all_count_; delta > 0; delta--) {
    824     self->ModifySuspendCount(self, +1, false);
    825   }
    826   CHECK(!Contains(self));
    827   list_.push_back(self);
    828 }
    829 
    830 void ThreadList::Unregister(Thread* self) {
    831   DCHECK_EQ(self, Thread::Current());
    832 
    833   VLOG(threads) << "ThreadList::Unregister() " << *self;
    834 
    835   // Any time-consuming destruction, plus anything that can call back into managed code or
    836   // suspend and so on, must happen at this point, and not in ~Thread.
    837   self->Destroy();
    838 
    839   uint32_t thin_lock_id = self->GetThreadId();
    840   while (self != nullptr) {
    841     // Remove and delete the Thread* while holding the thread_list_lock_ and
    842     // thread_suspend_count_lock_ so that the unregistering thread cannot be suspended.
    843     // Note: deliberately not using MutexLock that could hold a stale self pointer.
    844     Locks::thread_list_lock_->ExclusiveLock(self);
    845     if (!Contains(self)) {
    846       std::ostringstream os;
    847       DumpNativeStack(os, GetTid(), "  native: ", nullptr);
    848       LOG(ERROR) << "Request to unregister unattached thread\n" << os.str();
    849       self = nullptr;
    850     } else {
    851       // Note: we don't take the thread_suspend_count_lock_ here as to be suspending a thread other
    852       // than yourself you need to hold the thread_list_lock_ (see Thread::ModifySuspendCount).
    853       if (!self->IsSuspended()) {
    854         list_.remove(self);
    855         delete self;
    856         self = nullptr;
    857       }
    858     }
    859     Locks::thread_list_lock_->ExclusiveUnlock(self);
    860   }
    861   // Release the thread ID after the thread is finished and deleted to avoid cases where we can
    862   // temporarily have multiple threads with the same thread id. When this occurs, it causes
    863   // problems in FindThreadByThreadId / SuspendThreadByThreadId.
    864   ReleaseThreadId(nullptr, thin_lock_id);
    865 
    866   // Clear the TLS data, so that the underlying native thread is recognizably detached.
    867   // (It may wish to reattach later.)
    868   CHECK_PTHREAD_CALL(pthread_setspecific, (Thread::pthread_key_self_, NULL), "detach self");
    869 
    870   // Signal that a thread just detached.
    871   MutexLock mu(NULL, *Locks::thread_list_lock_);
    872   thread_exit_cond_.Signal(NULL);
    873 }
    874 
    875 void ThreadList::ForEach(void (*callback)(Thread*, void*), void* context) {
    876   for (const auto& thread : list_) {
    877     callback(thread, context);
    878   }
    879 }
    880 
    881 void ThreadList::VisitRoots(RootCallback* callback, void* arg) const {
    882   MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
    883   for (const auto& thread : list_) {
    884     thread->VisitRoots(callback, arg);
    885   }
    886 }
    887 
    888 class VerifyRootWrapperArg {
    889  public:
    890   VerifyRootWrapperArg(VerifyRootCallback* callback, void* arg) : callback_(callback), arg_(arg) {
    891   }
    892   VerifyRootCallback* const callback_;
    893   void* const arg_;
    894 };
    895 
    896 static void VerifyRootWrapperCallback(mirror::Object** root, void* arg, uint32_t /*thread_id*/,
    897                                       RootType root_type) {
    898   VerifyRootWrapperArg* wrapperArg = reinterpret_cast<VerifyRootWrapperArg*>(arg);
    899   wrapperArg->callback_(*root, wrapperArg->arg_, 0, NULL, root_type);
    900 }
    901 
    902 void ThreadList::VerifyRoots(VerifyRootCallback* callback, void* arg) const {
    903   VerifyRootWrapperArg wrapper(callback, arg);
    904   MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
    905   for (const auto& thread : list_) {
    906     thread->VisitRoots(VerifyRootWrapperCallback, &wrapper);
    907   }
    908 }
    909 
    910 uint32_t ThreadList::AllocThreadId(Thread* self) {
    911   MutexLock mu(self, *Locks::allocated_thread_ids_lock_);
    912   for (size_t i = 0; i < allocated_ids_.size(); ++i) {
    913     if (!allocated_ids_[i]) {
    914       allocated_ids_.set(i);
    915       return i + 1;  // Zero is reserved to mean "invalid".
    916     }
    917   }
    918   LOG(FATAL) << "Out of internal thread ids";
    919   return 0;
    920 }
    921 
    922 void ThreadList::ReleaseThreadId(Thread* self, uint32_t id) {
    923   MutexLock mu(self, *Locks::allocated_thread_ids_lock_);
    924   --id;  // Zero is reserved to mean "invalid".
    925   DCHECK(allocated_ids_[id]) << id;
    926   allocated_ids_.reset(id);
    927 }
    928 
    929 }  // namespace art
    930