180814287SRaphael Isemann //===-- ThreadList.cpp ----------------------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
8e65b2cf2SEugene Zelenko 
976e47d48SRaphael Isemann #include <cstdlib>
1030fdc8d8SChris Lattner 
1130fdc8d8SChris Lattner #include <algorithm>
1230fdc8d8SChris Lattner 
1330fdc8d8SChris Lattner #include "lldb/Target/Process.h"
14b9c1b51eSKate Stone #include "lldb/Target/RegisterContext.h"
15b9c1b51eSKate Stone #include "lldb/Target/Thread.h"
16b9c1b51eSKate Stone #include "lldb/Target/ThreadList.h"
17b9c1b51eSKate Stone #include "lldb/Target/ThreadPlan.h"
188f186f85SZachary Turner #include "lldb/Utility/LLDBAssert.h"
19*c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
206f9e6901SZachary Turner #include "lldb/Utility/Log.h"
21d821c997SPavel Labath #include "lldb/Utility/State.h"
2230fdc8d8SChris Lattner 
2330fdc8d8SChris Lattner using namespace lldb;
2430fdc8d8SChris Lattner using namespace lldb_private;
2530fdc8d8SChris Lattner 
ThreadList(Process * process)26b9c1b51eSKate Stone ThreadList::ThreadList(Process *process)
27b9c1b51eSKate Stone     : ThreadCollection(), m_process(process), m_stop_id(0),
28b9c1b51eSKate Stone       m_selected_tid(LLDB_INVALID_THREAD_ID) {}
2930fdc8d8SChris Lattner 
ThreadList(const ThreadList & rhs)30b9c1b51eSKate Stone ThreadList::ThreadList(const ThreadList &rhs)
31b9c1b51eSKate Stone     : ThreadCollection(), m_process(rhs.m_process), m_stop_id(rhs.m_stop_id),
32b9c1b51eSKate Stone       m_selected_tid() {
3330fdc8d8SChris Lattner   // Use the assignment operator since it uses the mutex
3430fdc8d8SChris Lattner   *this = rhs;
3530fdc8d8SChris Lattner }
3630fdc8d8SChris Lattner 
operator =(const ThreadList & rhs)37b9c1b51eSKate Stone const ThreadList &ThreadList::operator=(const ThreadList &rhs) {
38b9c1b51eSKate Stone   if (this != &rhs) {
3905097246SAdrian Prantl     // Lock both mutexes to make sure neither side changes anyone on us while
4005097246SAdrian Prantl     // the assignment occurs
41cdd4892fSJim Ingham     std::lock(GetMutex(), rhs.GetMutex());
42cdd4892fSJim Ingham     std::lock_guard<std::recursive_mutex> guard(GetMutex(), std::adopt_lock);
43cdd4892fSJim Ingham     std::lock_guard<std::recursive_mutex> rhs_guard(rhs.GetMutex(),
44cdd4892fSJim Ingham                                                     std::adopt_lock);
45bb19a13cSSaleem Abdulrasool 
4630fdc8d8SChris Lattner     m_process = rhs.m_process;
4730fdc8d8SChris Lattner     m_stop_id = rhs.m_stop_id;
4830fdc8d8SChris Lattner     m_threads = rhs.m_threads;
492976d00aSJim Ingham     m_selected_tid = rhs.m_selected_tid;
5030fdc8d8SChris Lattner   }
5130fdc8d8SChris Lattner   return *this;
5230fdc8d8SChris Lattner }
5330fdc8d8SChris Lattner 
~ThreadList()54b9c1b51eSKate Stone ThreadList::~ThreadList() {
5505097246SAdrian Prantl   // Clear the thread list. Clear will take the mutex lock which will ensure
5605097246SAdrian Prantl   // that if anyone is using the list they won't get it removed while using it.
57ac358da5SGreg Clayton   Clear();
5830fdc8d8SChris Lattner }
5930fdc8d8SChris Lattner 
GetExpressionExecutionThread()60b9c1b51eSKate Stone lldb::ThreadSP ThreadList::GetExpressionExecutionThread() {
618d94ba0fSJim Ingham   if (m_expression_tid_stack.empty())
628d94ba0fSJim Ingham     return GetSelectedThread();
638d94ba0fSJim Ingham   ThreadSP expr_thread_sp = FindThreadByID(m_expression_tid_stack.back());
648d94ba0fSJim Ingham   if (expr_thread_sp)
658d94ba0fSJim Ingham     return expr_thread_sp;
668d94ba0fSJim Ingham   else
678d94ba0fSJim Ingham     return GetSelectedThread();
688d94ba0fSJim Ingham }
698d94ba0fSJim Ingham 
PushExpressionExecutionThread(lldb::tid_t tid)70b9c1b51eSKate Stone void ThreadList::PushExpressionExecutionThread(lldb::tid_t tid) {
718d94ba0fSJim Ingham   m_expression_tid_stack.push_back(tid);
728d94ba0fSJim Ingham }
738d94ba0fSJim Ingham 
PopExpressionExecutionThread(lldb::tid_t tid)74b9c1b51eSKate Stone void ThreadList::PopExpressionExecutionThread(lldb::tid_t tid) {
758d94ba0fSJim Ingham   assert(m_expression_tid_stack.back() == tid);
768d94ba0fSJim Ingham   m_expression_tid_stack.pop_back();
778d94ba0fSJim Ingham }
7830fdc8d8SChris Lattner 
GetStopID() const79b9c1b51eSKate Stone uint32_t ThreadList::GetStopID() const { return m_stop_id; }
8030fdc8d8SChris Lattner 
SetStopID(uint32_t stop_id)81b9c1b51eSKate Stone void ThreadList::SetStopID(uint32_t stop_id) { m_stop_id = stop_id; }
8230fdc8d8SChris Lattner 
GetSize(bool can_update)83b9c1b51eSKate Stone uint32_t ThreadList::GetSize(bool can_update) {
84bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
85bb19a13cSSaleem Abdulrasool 
8630fdc8d8SChris Lattner   if (can_update)
8730fdc8d8SChris Lattner     m_process->UpdateThreadListIfNeeded();
8830fdc8d8SChris Lattner   return m_threads.size();
8930fdc8d8SChris Lattner }
9030fdc8d8SChris Lattner 
GetThreadAtIndex(uint32_t idx,bool can_update)91b9c1b51eSKate Stone ThreadSP ThreadList::GetThreadAtIndex(uint32_t idx, bool can_update) {
92bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
93bb19a13cSSaleem Abdulrasool 
9430fdc8d8SChris Lattner   if (can_update)
9530fdc8d8SChris Lattner     m_process->UpdateThreadListIfNeeded();
9630fdc8d8SChris Lattner 
9730fdc8d8SChris Lattner   ThreadSP thread_sp;
9830fdc8d8SChris Lattner   if (idx < m_threads.size())
9930fdc8d8SChris Lattner     thread_sp = m_threads[idx];
10030fdc8d8SChris Lattner   return thread_sp;
10130fdc8d8SChris Lattner }
10230fdc8d8SChris Lattner 
FindThreadByID(lldb::tid_t tid,bool can_update)103b9c1b51eSKate Stone ThreadSP ThreadList::FindThreadByID(lldb::tid_t tid, bool can_update) {
104bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
10530fdc8d8SChris Lattner 
10630fdc8d8SChris Lattner   if (can_update)
10730fdc8d8SChris Lattner     m_process->UpdateThreadListIfNeeded();
10830fdc8d8SChris Lattner 
10930fdc8d8SChris Lattner   ThreadSP thread_sp;
11030fdc8d8SChris Lattner   uint32_t idx = 0;
11130fdc8d8SChris Lattner   const uint32_t num_threads = m_threads.size();
112b9c1b51eSKate Stone   for (idx = 0; idx < num_threads; ++idx) {
113b9c1b51eSKate Stone     if (m_threads[idx]->GetID() == tid) {
11430fdc8d8SChris Lattner       thread_sp = m_threads[idx];
11530fdc8d8SChris Lattner       break;
11630fdc8d8SChris Lattner     }
11730fdc8d8SChris Lattner   }
11830fdc8d8SChris Lattner   return thread_sp;
11930fdc8d8SChris Lattner }
12030fdc8d8SChris Lattner 
FindThreadByProtocolID(lldb::tid_t tid,bool can_update)121b9c1b51eSKate Stone ThreadSP ThreadList::FindThreadByProtocolID(lldb::tid_t tid, bool can_update) {
122bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
123160c9d81SGreg Clayton 
124160c9d81SGreg Clayton   if (can_update)
125160c9d81SGreg Clayton     m_process->UpdateThreadListIfNeeded();
126160c9d81SGreg Clayton 
127160c9d81SGreg Clayton   ThreadSP thread_sp;
128160c9d81SGreg Clayton   uint32_t idx = 0;
129160c9d81SGreg Clayton   const uint32_t num_threads = m_threads.size();
130b9c1b51eSKate Stone   for (idx = 0; idx < num_threads; ++idx) {
131b9c1b51eSKate Stone     if (m_threads[idx]->GetProtocolID() == tid) {
132160c9d81SGreg Clayton       thread_sp = m_threads[idx];
133160c9d81SGreg Clayton       break;
134160c9d81SGreg Clayton     }
135160c9d81SGreg Clayton   }
136160c9d81SGreg Clayton   return thread_sp;
137160c9d81SGreg Clayton }
138160c9d81SGreg Clayton 
RemoveThreadByID(lldb::tid_t tid,bool can_update)139b9c1b51eSKate Stone ThreadSP ThreadList::RemoveThreadByID(lldb::tid_t tid, bool can_update) {
140bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
141c2c423eaSHan Ming Ong 
142c2c423eaSHan Ming Ong   if (can_update)
143c2c423eaSHan Ming Ong     m_process->UpdateThreadListIfNeeded();
144c2c423eaSHan Ming Ong 
145c2c423eaSHan Ming Ong   ThreadSP thread_sp;
146c2c423eaSHan Ming Ong   uint32_t idx = 0;
147c2c423eaSHan Ming Ong   const uint32_t num_threads = m_threads.size();
148b9c1b51eSKate Stone   for (idx = 0; idx < num_threads; ++idx) {
149b9c1b51eSKate Stone     if (m_threads[idx]->GetID() == tid) {
150c2c423eaSHan Ming Ong       thread_sp = m_threads[idx];
151c2c423eaSHan Ming Ong       m_threads.erase(m_threads.begin() + idx);
152c2c423eaSHan Ming Ong       break;
153c2c423eaSHan Ming Ong     }
154c2c423eaSHan Ming Ong   }
155c2c423eaSHan Ming Ong   return thread_sp;
156c2c423eaSHan Ming Ong }
157c2c423eaSHan Ming Ong 
RemoveThreadByProtocolID(lldb::tid_t tid,bool can_update)158b9c1b51eSKate Stone ThreadSP ThreadList::RemoveThreadByProtocolID(lldb::tid_t tid,
159b9c1b51eSKate Stone                                               bool can_update) {
160bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
161160c9d81SGreg Clayton 
162160c9d81SGreg Clayton   if (can_update)
163160c9d81SGreg Clayton     m_process->UpdateThreadListIfNeeded();
164160c9d81SGreg Clayton 
165160c9d81SGreg Clayton   ThreadSP thread_sp;
166160c9d81SGreg Clayton   uint32_t idx = 0;
167160c9d81SGreg Clayton   const uint32_t num_threads = m_threads.size();
168b9c1b51eSKate Stone   for (idx = 0; idx < num_threads; ++idx) {
169b9c1b51eSKate Stone     if (m_threads[idx]->GetProtocolID() == tid) {
170160c9d81SGreg Clayton       thread_sp = m_threads[idx];
171160c9d81SGreg Clayton       m_threads.erase(m_threads.begin() + idx);
172160c9d81SGreg Clayton       break;
173160c9d81SGreg Clayton     }
174160c9d81SGreg Clayton   }
175160c9d81SGreg Clayton   return thread_sp;
176160c9d81SGreg Clayton }
177160c9d81SGreg Clayton 
GetThreadSPForThreadPtr(Thread * thread_ptr)178b9c1b51eSKate Stone ThreadSP ThreadList::GetThreadSPForThreadPtr(Thread *thread_ptr) {
17930fdc8d8SChris Lattner   ThreadSP thread_sp;
180b9c1b51eSKate Stone   if (thread_ptr) {
181bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetMutex());
18230fdc8d8SChris Lattner 
18330fdc8d8SChris Lattner     uint32_t idx = 0;
18430fdc8d8SChris Lattner     const uint32_t num_threads = m_threads.size();
185b9c1b51eSKate Stone     for (idx = 0; idx < num_threads; ++idx) {
186b9c1b51eSKate Stone       if (m_threads[idx].get() == thread_ptr) {
18730fdc8d8SChris Lattner         thread_sp = m_threads[idx];
18830fdc8d8SChris Lattner         break;
18930fdc8d8SChris Lattner       }
19030fdc8d8SChris Lattner     }
19130fdc8d8SChris Lattner   }
19230fdc8d8SChris Lattner   return thread_sp;
19330fdc8d8SChris Lattner }
19430fdc8d8SChris Lattner 
GetBackingThread(const ThreadSP & real_thread)1958db3f7edSJonas Devlieghere ThreadSP ThreadList::GetBackingThread(const ThreadSP &real_thread) {
1968db3f7edSJonas Devlieghere   std::lock_guard<std::recursive_mutex> guard(GetMutex());
1978db3f7edSJonas Devlieghere 
1988db3f7edSJonas Devlieghere   ThreadSP thread_sp;
1998db3f7edSJonas Devlieghere   const uint32_t num_threads = m_threads.size();
2008db3f7edSJonas Devlieghere   for (uint32_t idx = 0; idx < num_threads; ++idx) {
2018db3f7edSJonas Devlieghere     if (m_threads[idx]->GetBackingThread() == real_thread) {
2028db3f7edSJonas Devlieghere       thread_sp = m_threads[idx];
2038db3f7edSJonas Devlieghere       break;
2048db3f7edSJonas Devlieghere     }
2058db3f7edSJonas Devlieghere   }
2068db3f7edSJonas Devlieghere   return thread_sp;
2078db3f7edSJonas Devlieghere }
2088db3f7edSJonas Devlieghere 
FindThreadByIndexID(uint32_t index_id,bool can_update)209b9c1b51eSKate Stone ThreadSP ThreadList::FindThreadByIndexID(uint32_t index_id, bool can_update) {
210bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
21130fdc8d8SChris Lattner 
21230fdc8d8SChris Lattner   if (can_update)
21330fdc8d8SChris Lattner     m_process->UpdateThreadListIfNeeded();
21430fdc8d8SChris Lattner 
21530fdc8d8SChris Lattner   ThreadSP thread_sp;
21630fdc8d8SChris Lattner   const uint32_t num_threads = m_threads.size();
217b9c1b51eSKate Stone   for (uint32_t idx = 0; idx < num_threads; ++idx) {
218b9c1b51eSKate Stone     if (m_threads[idx]->GetIndexID() == index_id) {
21930fdc8d8SChris Lattner       thread_sp = m_threads[idx];
22030fdc8d8SChris Lattner       break;
22130fdc8d8SChris Lattner     }
22230fdc8d8SChris Lattner   }
22330fdc8d8SChris Lattner   return thread_sp;
22430fdc8d8SChris Lattner }
22530fdc8d8SChris Lattner 
ShouldStop(Event * event_ptr)226b9c1b51eSKate Stone bool ThreadList::ShouldStop(Event *event_ptr) {
22730fdc8d8SChris Lattner   // Running events should never stop, obviously...
22830fdc8d8SChris Lattner 
229a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Step);
23030fdc8d8SChris Lattner 
23105097246SAdrian Prantl   // The ShouldStop method of the threads can do a whole lot of work, figuring
23205097246SAdrian Prantl   // out whether the thread plan conditions are met.  So we don't want to keep
23305097246SAdrian Prantl   // the ThreadList locked the whole time we are doing this.
234b42f3af3SJim Ingham   // FIXME: It is possible that running code could cause new threads
23505097246SAdrian Prantl   // to be created.  If that happens, we will miss asking them whether they
23605097246SAdrian Prantl   // should stop.  This is not a big deal since we haven't had a chance to hang
23705097246SAdrian Prantl   // any interesting operations on those threads yet.
23830fdc8d8SChris Lattner 
239b42f3af3SJim Ingham   collection threads_copy;
240b42f3af3SJim Ingham   {
241b42f3af3SJim Ingham     // Scope for locker
242bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetMutex());
243b42f3af3SJim Ingham 
244b42f3af3SJim Ingham     m_process->UpdateThreadListIfNeeded();
245b9c1b51eSKate Stone     for (lldb::ThreadSP thread_sp : m_threads) {
246b9c1b51eSKate Stone       // This is an optimization...  If we didn't let a thread run in between
24705097246SAdrian Prantl       // the previous stop and this one, we shouldn't have to consult it for
24805097246SAdrian Prantl       // ShouldStop.  So just leave it off the list we are going to inspect. On
24905097246SAdrian Prantl       // Linux, if a thread-specific conditional breakpoint was hit, it won't
25005097246SAdrian Prantl       // necessarily be the thread that hit the breakpoint itself that
25105097246SAdrian Prantl       // evaluates the conditional expression, so the thread that hit the
25205097246SAdrian Prantl       // breakpoint could still be asked to stop, even though it hasn't been
25305097246SAdrian Prantl       // allowed to run since the previous stop.
254b9c1b51eSKate Stone       if (thread_sp->GetTemporaryResumeState() != eStateSuspended ||
255b9c1b51eSKate Stone           thread_sp->IsStillAtLastBreakpointHit())
256569aaf9eSJim Ingham         threads_copy.push_back(thread_sp);
257569aaf9eSJim Ingham     }
258569aaf9eSJim Ingham 
259b9c1b51eSKate Stone     // It is possible the threads we were allowing to run all exited and then
26005097246SAdrian Prantl     // maybe the user interrupted or something, then fall back on looking at
26105097246SAdrian Prantl     // all threads:
262569aaf9eSJim Ingham 
263569aaf9eSJim Ingham     if (threads_copy.size() == 0)
264b42f3af3SJim Ingham       threads_copy = m_threads;
265b42f3af3SJim Ingham   }
266b42f3af3SJim Ingham 
267b42f3af3SJim Ingham   collection::iterator pos, end = threads_copy.end();
26830fdc8d8SChris Lattner 
269b9c1b51eSKate Stone   if (log) {
27010c4b249SJim Ingham     log->PutCString("");
27163e5fb76SJonas Devlieghere     LLDB_LOGF(log,
27263e5fb76SJonas Devlieghere               "ThreadList::%s: %" PRIu64 " threads, %" PRIu64
273b9c1b51eSKate Stone               " unsuspended threads",
274b9c1b51eSKate Stone               __FUNCTION__, (uint64_t)m_threads.size(),
275569aaf9eSJim Ingham               (uint64_t)threads_copy.size());
27610c4b249SJim Ingham   }
2772cad65a5SGreg Clayton 
278a0079044SJim Ingham   bool did_anybody_stop_for_a_reason = false;
27935878c47SJim Ingham 
280b9c1b51eSKate Stone   // If the event is an Interrupt event, then we're going to stop no matter
281b9c1b51eSKate Stone   // what.  Otherwise, presume we won't stop.
282a0079044SJim Ingham   bool should_stop = false;
283b9c1b51eSKate Stone   if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) {
28463e5fb76SJonas Devlieghere     LLDB_LOGF(
28563e5fb76SJonas Devlieghere         log, "ThreadList::%s handling interrupt event, should stop set to true",
286b9c1b51eSKate Stone         __FUNCTION__);
28735878c47SJim Ingham 
28835878c47SJim Ingham     should_stop = true;
28935878c47SJim Ingham   }
2907bc3465fSJim Ingham 
291b9c1b51eSKate Stone   // Now we run through all the threads and get their stop info's.  We want to
29205097246SAdrian Prantl   // make sure to do this first before we start running the ShouldStop, because
29305097246SAdrian Prantl   // one thread's ShouldStop could destroy information (like deleting a thread
29405097246SAdrian Prantl   // specific breakpoint another thread had stopped at) which could lead us to
29505097246SAdrian Prantl   // compute the StopInfo incorrectly. We don't need to use it here, we just
29605097246SAdrian Prantl   // want to make sure it gets computed.
2977bc3465fSJim Ingham 
298b9c1b51eSKate Stone   for (pos = threads_copy.begin(); pos != end; ++pos) {
2997bc3465fSJim Ingham     ThreadSP thread_sp(*pos);
3007bc3465fSJim Ingham     thread_sp->GetStopInfo();
3017bc3465fSJim Ingham   }
302a0079044SJim Ingham 
303b9c1b51eSKate Stone   for (pos = threads_copy.begin(); pos != end; ++pos) {
30430fdc8d8SChris Lattner     ThreadSP thread_sp(*pos);
3052cad65a5SGreg Clayton 
306b9c1b51eSKate Stone     // We should never get a stop for which no thread had a stop reason, but
30705097246SAdrian Prantl     // sometimes we do see this - for instance when we first connect to a
30805097246SAdrian Prantl     // remote stub.  In that case we should stop, since we can't figure out the
30905097246SAdrian Prantl     // right thing to do and stopping gives the user control over what to do in
31005097246SAdrian Prantl     // this instance.
31139fdae7fSJim Ingham     //
312b9c1b51eSKate Stone     // Note, this causes a problem when you have a thread specific breakpoint,
31305097246SAdrian Prantl     // and a bunch of threads hit the breakpoint, but not the thread which we
31405097246SAdrian Prantl     // are waiting for.  All the threads that are not "supposed" to hit the
31505097246SAdrian Prantl     // breakpoint are marked as having no stop reason, which is right, they
31605097246SAdrian Prantl     // should not show a stop reason.  But that triggers this code and causes
31705097246SAdrian Prantl     // us to stop seemingly for no reason.
31839fdae7fSJim Ingham     //
319b9c1b51eSKate Stone     // Since the only way we ever saw this error was on first attach, I'm only
32005097246SAdrian Prantl     // going to trigger set did_anybody_stop_for_a_reason to true unless this
32105097246SAdrian Prantl     // is the first stop.
32239fdae7fSJim Ingham     //
323b9c1b51eSKate Stone     // If this becomes a problem, we'll have to have another StopReason like
32405097246SAdrian Prantl     // "StopInfoHidden" which will look invalid everywhere but at this check.
32539fdae7fSJim Ingham 
32621afbe03SEd Maste     if (thread_sp->GetProcess()->GetStopID() > 1)
32739fdae7fSJim Ingham       did_anybody_stop_for_a_reason = true;
32839fdae7fSJim Ingham     else
329a0079044SJim Ingham       did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
330a0079044SJim Ingham 
33110c4b249SJim Ingham     const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
3322cad65a5SGreg Clayton     if (thread_should_stop)
3332cad65a5SGreg Clayton       should_stop |= true;
3342cad65a5SGreg Clayton   }
3352cad65a5SGreg Clayton 
336b9c1b51eSKate Stone   if (!should_stop && !did_anybody_stop_for_a_reason) {
337a0079044SJim Ingham     should_stop = true;
33863e5fb76SJonas Devlieghere     LLDB_LOGF(log,
33963e5fb76SJonas Devlieghere               "ThreadList::%s we stopped but no threads had a stop reason, "
340b9c1b51eSKate Stone               "overriding should_stop and stopping.",
341b9c1b51eSKate Stone               __FUNCTION__);
342a0079044SJim Ingham   }
343a0079044SJim Ingham 
34463e5fb76SJonas Devlieghere   LLDB_LOGF(log, "ThreadList::%s overall should_stop = %i", __FUNCTION__,
345b9c1b51eSKate Stone             should_stop);
3462cad65a5SGreg Clayton 
347b9c1b51eSKate Stone   if (should_stop) {
348b9c1b51eSKate Stone     for (pos = threads_copy.begin(); pos != end; ++pos) {
34930fdc8d8SChris Lattner       ThreadSP thread_sp(*pos);
35030fdc8d8SChris Lattner       thread_sp->WillStop();
35130fdc8d8SChris Lattner     }
35230fdc8d8SChris Lattner   }
35330fdc8d8SChris Lattner 
35430fdc8d8SChris Lattner   return should_stop;
35530fdc8d8SChris Lattner }
35630fdc8d8SChris Lattner 
ShouldReportStop(Event * event_ptr)357b9c1b51eSKate Stone Vote ThreadList::ShouldReportStop(Event *event_ptr) {
358bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
3592cad65a5SGreg Clayton 
36030fdc8d8SChris Lattner   Vote result = eVoteNoOpinion;
36130fdc8d8SChris Lattner   m_process->UpdateThreadListIfNeeded();
36230fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
36330fdc8d8SChris Lattner 
364a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Step);
3652cad65a5SGreg Clayton 
36663e5fb76SJonas Devlieghere   LLDB_LOGF(log, "ThreadList::%s %" PRIu64 " threads", __FUNCTION__,
367b9c1b51eSKate Stone             (uint64_t)m_threads.size());
3682cad65a5SGreg Clayton 
36905097246SAdrian Prantl   // Run through the threads and ask whether we should report this event. For
37005097246SAdrian Prantl   // stopping, a YES vote wins over everything.  A NO vote wins over NO
371b9c1b51eSKate Stone   // opinion.
372b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
37330fdc8d8SChris Lattner     ThreadSP thread_sp(*pos);
374e0d378b3SGreg Clayton     const Vote vote = thread_sp->ShouldReportStop(event_ptr);
375b9c1b51eSKate Stone     switch (vote) {
37630fdc8d8SChris Lattner     case eVoteNoOpinion:
37730fdc8d8SChris Lattner       continue;
3782cad65a5SGreg Clayton 
37930fdc8d8SChris Lattner     case eVoteYes:
38030fdc8d8SChris Lattner       result = eVoteYes;
38130fdc8d8SChris Lattner       break;
3822cad65a5SGreg Clayton 
38330fdc8d8SChris Lattner     case eVoteNo:
384b9c1b51eSKate Stone       if (result == eVoteNoOpinion) {
38530fdc8d8SChris Lattner         result = eVoteNo;
386b9c1b51eSKate Stone       } else {
387df44988bSZachary Turner         LLDB_LOG(log,
388df44988bSZachary Turner           "Thread {0:x} voted {1}, but lost out because result was {2}",
389df44988bSZachary Turner           thread_sp->GetID(), vote, result);
3902cad65a5SGreg Clayton       }
39130fdc8d8SChris Lattner       break;
39230fdc8d8SChris Lattner     }
39330fdc8d8SChris Lattner   }
394df44988bSZachary Turner   LLDB_LOG(log, "Returning {0}", result);
39530fdc8d8SChris Lattner   return result;
39630fdc8d8SChris Lattner }
39730fdc8d8SChris Lattner 
SetShouldReportStop(Vote vote)398b9c1b51eSKate Stone void ThreadList::SetShouldReportStop(Vote vote) {
399bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
400bb19a13cSSaleem Abdulrasool 
401221d51cfSJim Ingham   m_process->UpdateThreadListIfNeeded();
402221d51cfSJim Ingham   collection::iterator pos, end = m_threads.end();
403b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
404221d51cfSJim Ingham     ThreadSP thread_sp(*pos);
405221d51cfSJim Ingham     thread_sp->SetShouldReportStop(vote);
406221d51cfSJim Ingham   }
407221d51cfSJim Ingham }
408221d51cfSJim Ingham 
ShouldReportRun(Event * event_ptr)409b9c1b51eSKate Stone Vote ThreadList::ShouldReportRun(Event *event_ptr) {
4102cad65a5SGreg Clayton 
411bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
4122cad65a5SGreg Clayton 
41330fdc8d8SChris Lattner   Vote result = eVoteNoOpinion;
41430fdc8d8SChris Lattner   m_process->UpdateThreadListIfNeeded();
41530fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
41630fdc8d8SChris Lattner 
41705097246SAdrian Prantl   // Run through the threads and ask whether we should report this event. The
41805097246SAdrian Prantl   // rule is NO vote wins over everything, a YES vote wins over no opinion.
41930fdc8d8SChris Lattner 
420a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Step);
421ce579839SJim Ingham 
422b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
423b9c1b51eSKate Stone     if ((*pos)->GetResumeState() != eStateSuspended) {
424b9c1b51eSKate Stone       switch ((*pos)->ShouldReportRun(event_ptr)) {
42530fdc8d8SChris Lattner       case eVoteNoOpinion:
42630fdc8d8SChris Lattner         continue;
42730fdc8d8SChris Lattner       case eVoteYes:
42830fdc8d8SChris Lattner         if (result == eVoteNoOpinion)
42930fdc8d8SChris Lattner           result = eVoteYes;
43030fdc8d8SChris Lattner         break;
43130fdc8d8SChris Lattner       case eVoteNo:
43263e5fb76SJonas Devlieghere         LLDB_LOGF(log,
43363e5fb76SJonas Devlieghere                   "ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64
434b9c1b51eSKate Stone                   ") says don't report.",
435b9c1b51eSKate Stone                   (*pos)->GetIndexID(), (*pos)->GetID());
43630fdc8d8SChris Lattner         result = eVoteNo;
43730fdc8d8SChris Lattner         break;
43830fdc8d8SChris Lattner       }
43930fdc8d8SChris Lattner     }
440ce579839SJim Ingham   }
44130fdc8d8SChris Lattner   return result;
44230fdc8d8SChris Lattner }
44330fdc8d8SChris Lattner 
Clear()444b9c1b51eSKate Stone void ThreadList::Clear() {
445bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
44630fdc8d8SChris Lattner   m_stop_id = 0;
44730fdc8d8SChris Lattner   m_threads.clear();
4482976d00aSJim Ingham   m_selected_tid = LLDB_INVALID_THREAD_ID;
44930fdc8d8SChris Lattner }
45030fdc8d8SChris Lattner 
Destroy()451b9c1b51eSKate Stone void ThreadList::Destroy() {
452bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
453e1cd1be6SGreg Clayton   const uint32_t num_threads = m_threads.size();
454b9c1b51eSKate Stone   for (uint32_t idx = 0; idx < num_threads; ++idx) {
455e1cd1be6SGreg Clayton     m_threads[idx]->DestroyThread();
456e1cd1be6SGreg Clayton   }
457e1cd1be6SGreg Clayton }
458e1cd1be6SGreg Clayton 
RefreshStateAfterStop()459b9c1b51eSKate Stone void ThreadList::RefreshStateAfterStop() {
460bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
46130fdc8d8SChris Lattner 
46230fdc8d8SChris Lattner   m_process->UpdateThreadListIfNeeded();
46330fdc8d8SChris Lattner 
464a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Step);
46510c4b249SJim Ingham   if (log && log->GetVerbose())
46663e5fb76SJonas Devlieghere     LLDB_LOGF(log,
46763e5fb76SJonas Devlieghere               "Turning off notification of new threads while single stepping "
468b9c1b51eSKate Stone               "a thread.");
4691c823b43SJim Ingham 
47030fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
47130fdc8d8SChris Lattner   for (pos = m_threads.begin(); pos != end; ++pos)
47230fdc8d8SChris Lattner     (*pos)->RefreshStateAfterStop();
47330fdc8d8SChris Lattner }
47430fdc8d8SChris Lattner 
DiscardThreadPlans()475b9c1b51eSKate Stone void ThreadList::DiscardThreadPlans() {
47605097246SAdrian Prantl   // You don't need to update the thread list here, because only threads that
47705097246SAdrian Prantl   // you currently know about have any thread plans.
478bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
47930fdc8d8SChris Lattner 
48030fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
48130fdc8d8SChris Lattner   for (pos = m_threads.begin(); pos != end; ++pos)
48230fdc8d8SChris Lattner     (*pos)->DiscardThreadPlans(true);
48330fdc8d8SChris Lattner }
48430fdc8d8SChris Lattner 
WillResume()485b9c1b51eSKate Stone bool ThreadList::WillResume() {
48605097246SAdrian Prantl   // Run through the threads and perform their momentary actions. But we only
48705097246SAdrian Prantl   // do this for threads that are running, user suspended threads stay where
48805097246SAdrian Prantl   // they are.
48930fdc8d8SChris Lattner 
490bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
49130fdc8d8SChris Lattner   m_process->UpdateThreadListIfNeeded();
49230fdc8d8SChris Lattner 
49330fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
49430fdc8d8SChris Lattner 
495a3241c1bSJim Ingham   // See if any thread wants to run stopping others.  If it does, then we won't
49605097246SAdrian Prantl   // setup the other threads for resume, since they aren't going to get a
49705097246SAdrian Prantl   // chance to run.  This is necessary because the SetupForResume might add
49805097246SAdrian Prantl   // "StopOthers" plans which would then get to be part of the who-gets-to-run
49905097246SAdrian Prantl   // negotiation, but they're coming in after the fact, and the threads that
50005097246SAdrian Prantl   // are already set up should take priority.
501a3241c1bSJim Ingham 
502a3241c1bSJim Ingham   bool wants_solo_run = false;
50330fdc8d8SChris Lattner 
504b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
505b9c1b51eSKate Stone     lldbassert((*pos)->GetCurrentPlan() &&
506b9c1b51eSKate Stone                "thread should not have null thread plan");
5078f186f85SZachary Turner     if ((*pos)->GetResumeState() != eStateSuspended &&
508b9c1b51eSKate Stone         (*pos)->GetCurrentPlan()->StopOthers()) {
509b9c1b51eSKate Stone       if ((*pos)->IsOperatingSystemPluginThread() &&
510b9c1b51eSKate Stone           !(*pos)->GetBackingThread())
5116e0ff1a3SGreg Clayton         continue;
512a3241c1bSJim Ingham       wants_solo_run = true;
513a3241c1bSJim Ingham       break;
514a3241c1bSJim Ingham     }
515a3241c1bSJim Ingham   }
516a3241c1bSJim Ingham 
517b9c1b51eSKate Stone   if (wants_solo_run) {
518a007a6d8SPavel Labath     Log *log = GetLog(LLDBLog::Step);
51910c4b249SJim Ingham     if (log && log->GetVerbose())
52063e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Turning on notification of new threads while single "
521b9c1b51eSKate Stone                      "stepping a thread.");
5221c823b43SJim Ingham     m_process->StartNoticingNewThreads();
523b9c1b51eSKate Stone   } else {
524a007a6d8SPavel Labath     Log *log = GetLog(LLDBLog::Step);
52510c4b249SJim Ingham     if (log && log->GetVerbose())
52663e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Turning off notification of new threads while single "
527b9c1b51eSKate Stone                      "stepping a thread.");
5281c823b43SJim Ingham     m_process->StopNoticingNewThreads();
5291c823b43SJim Ingham   }
530a3241c1bSJim Ingham 
531b9c1b51eSKate Stone   // Give all the threads that are likely to run a last chance to set up their
53205097246SAdrian Prantl   // state before we negotiate who is actually going to get a chance to run...
533b9c1b51eSKate Stone   // Don't set to resume suspended threads, and if any thread wanted to stop
53405097246SAdrian Prantl   // others, only call setup on the threads that request StopOthers...
535a3241c1bSJim Ingham 
536b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
537b9c1b51eSKate Stone     if ((*pos)->GetResumeState() != eStateSuspended &&
538b9c1b51eSKate Stone         (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers())) {
539b9c1b51eSKate Stone       if ((*pos)->IsOperatingSystemPluginThread() &&
540b9c1b51eSKate Stone           !(*pos)->GetBackingThread())
5416e0ff1a3SGreg Clayton         continue;
54230fdc8d8SChris Lattner       (*pos)->SetupForResume();
543a3241c1bSJim Ingham     }
544a3241c1bSJim Ingham   }
54530fdc8d8SChris Lattner 
54630fdc8d8SChris Lattner   // Now go through the threads and see if any thread wants to run just itself.
54730fdc8d8SChris Lattner   // if so then pick one and run it.
548a3241c1bSJim Ingham 
54930fdc8d8SChris Lattner   ThreadList run_me_only_list(m_process);
55030fdc8d8SChris Lattner 
55130fdc8d8SChris Lattner   run_me_only_list.SetStopID(m_process->GetStopID());
55230fdc8d8SChris Lattner 
55330fdc8d8SChris Lattner   bool run_only_current_thread = false;
55430fdc8d8SChris Lattner 
555b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
55630fdc8d8SChris Lattner     ThreadSP thread_sp(*pos);
557b15bfc75SJim Ingham     if (thread_sp->GetResumeState() != eStateSuspended &&
558b9c1b51eSKate Stone         thread_sp->GetCurrentPlan()->StopOthers()) {
559b9c1b51eSKate Stone       if ((*pos)->IsOperatingSystemPluginThread() &&
560b9c1b51eSKate Stone           !(*pos)->GetBackingThread())
5616e0ff1a3SGreg Clayton         continue;
5626e0ff1a3SGreg Clayton 
56330fdc8d8SChris Lattner       // You can't say "stop others" and also want yourself to be suspended.
56430fdc8d8SChris Lattner       assert(thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
56530fdc8d8SChris Lattner 
566b9c1b51eSKate Stone       if (thread_sp == GetSelectedThread()) {
567b9c1b51eSKate Stone         // If the currently selected thread wants to run on its own, always let
568b9c1b51eSKate Stone         // it.
56930fdc8d8SChris Lattner         run_only_current_thread = true;
57030fdc8d8SChris Lattner         run_me_only_list.Clear();
57130fdc8d8SChris Lattner         run_me_only_list.AddThread(thread_sp);
57230fdc8d8SChris Lattner         break;
57330fdc8d8SChris Lattner       }
57430fdc8d8SChris Lattner 
57530fdc8d8SChris Lattner       run_me_only_list.AddThread(thread_sp);
57630fdc8d8SChris Lattner     }
57730fdc8d8SChris Lattner   }
57830fdc8d8SChris Lattner 
579513c6bb8SJim Ingham   bool need_to_resume = true;
580513c6bb8SJim Ingham 
581b9c1b51eSKate Stone   if (run_me_only_list.GetSize(false) == 0) {
58230fdc8d8SChris Lattner     // Everybody runs as they wish:
583b9c1b51eSKate Stone     for (pos = m_threads.begin(); pos != end; ++pos) {
58430fdc8d8SChris Lattner       ThreadSP thread_sp(*pos);
585cb5d5a57SJim Ingham       StateType run_state;
586cb5d5a57SJim Ingham       if (thread_sp->GetResumeState() != eStateSuspended)
587cb5d5a57SJim Ingham         run_state = thread_sp->GetCurrentPlan()->RunState();
588cb5d5a57SJim Ingham       else
589cb5d5a57SJim Ingham         run_state = eStateSuspended;
590160c9d81SGreg Clayton       if (!thread_sp->ShouldResume(run_state))
591513c6bb8SJim Ingham         need_to_resume = false;
59230fdc8d8SChris Lattner     }
593b9c1b51eSKate Stone   } else {
59430fdc8d8SChris Lattner     ThreadSP thread_to_run;
59530fdc8d8SChris Lattner 
596b9c1b51eSKate Stone     if (run_only_current_thread) {
5972976d00aSJim Ingham       thread_to_run = GetSelectedThread();
598b9c1b51eSKate Stone     } else if (run_me_only_list.GetSize(false) == 1) {
59930fdc8d8SChris Lattner       thread_to_run = run_me_only_list.GetThreadAtIndex(0);
600b9c1b51eSKate Stone     } else {
601b9c1b51eSKate Stone       int random_thread =
602b9c1b51eSKate Stone           (int)((run_me_only_list.GetSize(false) * (double)rand()) /
603b9c1b51eSKate Stone                 (RAND_MAX + 1.0));
60430fdc8d8SChris Lattner       thread_to_run = run_me_only_list.GetThreadAtIndex(random_thread);
60530fdc8d8SChris Lattner     }
60630fdc8d8SChris Lattner 
607b9c1b51eSKate Stone     for (pos = m_threads.begin(); pos != end; ++pos) {
60830fdc8d8SChris Lattner       ThreadSP thread_sp(*pos);
609b9c1b51eSKate Stone       if (thread_sp == thread_to_run) {
610160c9d81SGreg Clayton         if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
611513c6bb8SJim Ingham           need_to_resume = false;
612b9c1b51eSKate Stone       } else
613160c9d81SGreg Clayton         thread_sp->ShouldResume(eStateSuspended);
61430fdc8d8SChris Lattner     }
61530fdc8d8SChris Lattner   }
61630fdc8d8SChris Lattner 
617513c6bb8SJim Ingham   return need_to_resume;
61830fdc8d8SChris Lattner }
61930fdc8d8SChris Lattner 
DidResume()620b9c1b51eSKate Stone void ThreadList::DidResume() {
621bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
62230fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
623b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
62430fdc8d8SChris Lattner     // Don't clear out threads that aren't going to get a chance to run, rather
62530fdc8d8SChris Lattner     // leave their state for the next time around.
62630fdc8d8SChris Lattner     ThreadSP thread_sp(*pos);
62730fdc8d8SChris Lattner     if (thread_sp->GetResumeState() != eStateSuspended)
62830fdc8d8SChris Lattner       thread_sp->DidResume();
62930fdc8d8SChris Lattner   }
63030fdc8d8SChris Lattner }
63130fdc8d8SChris Lattner 
DidStop()632b9c1b51eSKate Stone void ThreadList::DidStop() {
633bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
63429d65744SAndrew Kaylor   collection::iterator pos, end = m_threads.end();
635b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
63605097246SAdrian Prantl     // Notify threads that the process just stopped. Note, this currently
63705097246SAdrian Prantl     // assumes that all threads in the list stop when the process stops.  In
63805097246SAdrian Prantl     // the future we will want to support a debugging model where some threads
63905097246SAdrian Prantl     // continue to run while others are stopped.  We either need to handle that
64005097246SAdrian Prantl     // somehow here or create a special thread list containing only threads
64105097246SAdrian Prantl     // which will stop in the code that calls this method (currently
64229d65744SAndrew Kaylor     // Process::SetPrivateState).
64329d65744SAndrew Kaylor     ThreadSP thread_sp(*pos);
64429d65744SAndrew Kaylor     if (StateIsRunningState(thread_sp->GetState()))
64529d65744SAndrew Kaylor       thread_sp->DidStop();
64629d65744SAndrew Kaylor   }
64729d65744SAndrew Kaylor }
64829d65744SAndrew Kaylor 
GetSelectedThread()649b9c1b51eSKate Stone ThreadSP ThreadList::GetSelectedThread() {
650bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
651943ddb73SJohnny Chen   ThreadSP thread_sp = FindThreadByID(m_selected_tid);
652b9c1b51eSKate Stone   if (!thread_sp.get()) {
653354b9a65SJason Molenda     if (m_threads.size() == 0)
654354b9a65SJason Molenda       return thread_sp;
655943ddb73SJohnny Chen     m_selected_tid = m_threads[0]->GetID();
656943ddb73SJohnny Chen     thread_sp = m_threads[0];
657943ddb73SJohnny Chen   }
658943ddb73SJohnny Chen   return thread_sp;
65930fdc8d8SChris Lattner }
66030fdc8d8SChris Lattner 
SetSelectedThreadByID(lldb::tid_t tid,bool notify)661b9c1b51eSKate Stone bool ThreadList::SetSelectedThreadByID(lldb::tid_t tid, bool notify) {
662bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
663b7f6b2faSJim Ingham   ThreadSP selected_thread_sp(FindThreadByID(tid));
664b9c1b51eSKate Stone   if (selected_thread_sp) {
6652976d00aSJim Ingham     m_selected_tid = tid;
666b7f6b2faSJim Ingham     selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
667b9c1b51eSKate Stone   } else
6682976d00aSJim Ingham     m_selected_tid = LLDB_INVALID_THREAD_ID;
66930fdc8d8SChris Lattner 
670c3faa195SJim Ingham   if (notify)
671c3faa195SJim Ingham     NotifySelectedThreadChanged(m_selected_tid);
672c3faa195SJim Ingham 
6732976d00aSJim Ingham   return m_selected_tid != LLDB_INVALID_THREAD_ID;
67430fdc8d8SChris Lattner }
67530fdc8d8SChris Lattner 
SetSelectedThreadByIndexID(uint32_t index_id,bool notify)676b9c1b51eSKate Stone bool ThreadList::SetSelectedThreadByIndexID(uint32_t index_id, bool notify) {
677bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
678b7f6b2faSJim Ingham   ThreadSP selected_thread_sp(FindThreadByIndexID(index_id));
679b9c1b51eSKate Stone   if (selected_thread_sp.get()) {
680b7f6b2faSJim Ingham     m_selected_tid = selected_thread_sp->GetID();
681b7f6b2faSJim Ingham     selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
682b9c1b51eSKate Stone   } else
6832976d00aSJim Ingham     m_selected_tid = LLDB_INVALID_THREAD_ID;
68430fdc8d8SChris Lattner 
685c3faa195SJim Ingham   if (notify)
686c3faa195SJim Ingham     NotifySelectedThreadChanged(m_selected_tid);
687c3faa195SJim Ingham 
6882976d00aSJim Ingham   return m_selected_tid != LLDB_INVALID_THREAD_ID;
68930fdc8d8SChris Lattner }
69030fdc8d8SChris Lattner 
NotifySelectedThreadChanged(lldb::tid_t tid)691b9c1b51eSKate Stone void ThreadList::NotifySelectedThreadChanged(lldb::tid_t tid) {
692c3faa195SJim Ingham   ThreadSP selected_thread_sp(FindThreadByID(tid));
693b9c1b51eSKate Stone   if (selected_thread_sp->EventTypeHasListeners(
694b9c1b51eSKate Stone           Thread::eBroadcastBitThreadSelected))
695b9c1b51eSKate Stone     selected_thread_sp->BroadcastEvent(
696b9c1b51eSKate Stone         Thread::eBroadcastBitThreadSelected,
697c3faa195SJim Ingham         new Thread::ThreadEventData(selected_thread_sp));
698c3faa195SJim Ingham }
699c3faa195SJim Ingham 
Update(ThreadList & rhs)700b9c1b51eSKate Stone void ThreadList::Update(ThreadList &rhs) {
701b9c1b51eSKate Stone   if (this != &rhs) {
70205097246SAdrian Prantl     // Lock both mutexes to make sure neither side changes anyone on us while
70305097246SAdrian Prantl     // the assignment occurs
704bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetMutex());
705bb19a13cSSaleem Abdulrasool 
70656d9a1b3SGreg Clayton     m_process = rhs.m_process;
70756d9a1b3SGreg Clayton     m_stop_id = rhs.m_stop_id;
70856d9a1b3SGreg Clayton     m_threads.swap(rhs.m_threads);
70956d9a1b3SGreg Clayton     m_selected_tid = rhs.m_selected_tid;
710e1cd1be6SGreg Clayton 
71105097246SAdrian Prantl     // Now we look for threads that we are done with and make sure to clear
71205097246SAdrian Prantl     // them up as much as possible so anyone with a shared pointer will still
71305097246SAdrian Prantl     // have a reference, but the thread won't be of much use. Using
71405097246SAdrian Prantl     // std::weak_ptr for all backward references (such as a thread to a
71505097246SAdrian Prantl     // process) will eventually solve this issue for us, but for now, we need
71605097246SAdrian Prantl     // to work around the issue
717e1cd1be6SGreg Clayton     collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
718b9c1b51eSKate Stone     for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos) {
7191893065dSJim Ingham       // If this thread has already been destroyed, we don't need to look for
7201893065dSJim Ingham       // it to destroy it again.
7211893065dSJim Ingham       if (!(*rhs_pos)->IsValid())
7221893065dSJim Ingham         continue;
7231893065dSJim Ingham 
724e1cd1be6SGreg Clayton       const lldb::tid_t tid = (*rhs_pos)->GetID();
725e1cd1be6SGreg Clayton       bool thread_is_alive = false;
726e1cd1be6SGreg Clayton       const uint32_t num_threads = m_threads.size();
727b9c1b51eSKate Stone       for (uint32_t idx = 0; idx < num_threads; ++idx) {
72865d4d5c3SRyan Brown         ThreadSP backing_thread = m_threads[idx]->GetBackingThread();
729b9c1b51eSKate Stone         if (m_threads[idx]->GetID() == tid ||
730b9c1b51eSKate Stone             (backing_thread && backing_thread->GetID() == tid)) {
731e1cd1be6SGreg Clayton           thread_is_alive = true;
732e1cd1be6SGreg Clayton           break;
733e1cd1be6SGreg Clayton         }
734e1cd1be6SGreg Clayton       }
73561e8e688SJim Ingham       if (!thread_is_alive) {
736e1cd1be6SGreg Clayton         (*rhs_pos)->DestroyThread();
73761e8e688SJim Ingham       }
738e1cd1be6SGreg Clayton     }
73956d9a1b3SGreg Clayton   }
74056d9a1b3SGreg Clayton }
74156d9a1b3SGreg Clayton 
Flush()742b9c1b51eSKate Stone void ThreadList::Flush() {
743bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
744fa559e5cSGreg Clayton   collection::iterator pos, end = m_threads.end();
745fa559e5cSGreg Clayton   for (pos = m_threads.begin(); pos != end; ++pos)
746fa559e5cSGreg Clayton     (*pos)->Flush();
747fa559e5cSGreg Clayton }
74856d9a1b3SGreg Clayton 
GetMutex() const749b5cd6e7bSGreg Clayton std::recursive_mutex &ThreadList::GetMutex() const {
750ba4e61d3SAndrew Kaylor   return m_process->m_thread_mutex;
751ba4e61d3SAndrew Kaylor }
752ba4e61d3SAndrew Kaylor 
ExpressionExecutionThreadPusher(lldb::ThreadSP thread_sp)753b9c1b51eSKate Stone ThreadList::ExpressionExecutionThreadPusher::ExpressionExecutionThreadPusher(
754b9c1b51eSKate Stone     lldb::ThreadSP thread_sp)
755b9c1b51eSKate Stone     : m_thread_list(nullptr), m_tid(LLDB_INVALID_THREAD_ID) {
756b9c1b51eSKate Stone   if (thread_sp) {
7578d94ba0fSJim Ingham     m_tid = thread_sp->GetID();
7588d94ba0fSJim Ingham     m_thread_list = &thread_sp->GetProcess()->GetThreadList();
7598d94ba0fSJim Ingham     m_thread_list->PushExpressionExecutionThread(m_tid);
7608d94ba0fSJim Ingham   }
7618d94ba0fSJim Ingham }
762