130fdc8d8SChris Lattner //===-- ThreadList.cpp ------------------------------------------*- C++ -*-===//
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 
930fdc8d8SChris Lattner #include <stdlib.h>
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"
196f9e6901SZachary Turner #include "lldb/Utility/Log.h"
20d821c997SPavel Labath #include "lldb/Utility/State.h"
2130fdc8d8SChris Lattner 
2230fdc8d8SChris Lattner using namespace lldb;
2330fdc8d8SChris Lattner using namespace lldb_private;
2430fdc8d8SChris Lattner 
25b9c1b51eSKate Stone ThreadList::ThreadList(Process *process)
26b9c1b51eSKate Stone     : ThreadCollection(), m_process(process), m_stop_id(0),
27b9c1b51eSKate Stone       m_selected_tid(LLDB_INVALID_THREAD_ID) {}
2830fdc8d8SChris Lattner 
29b9c1b51eSKate Stone ThreadList::ThreadList(const ThreadList &rhs)
30b9c1b51eSKate Stone     : ThreadCollection(), m_process(rhs.m_process), m_stop_id(rhs.m_stop_id),
31b9c1b51eSKate Stone       m_selected_tid() {
3230fdc8d8SChris Lattner   // Use the assignment operator since it uses the mutex
3330fdc8d8SChris Lattner   *this = rhs;
3430fdc8d8SChris Lattner }
3530fdc8d8SChris Lattner 
36b9c1b51eSKate Stone const ThreadList &ThreadList::operator=(const ThreadList &rhs) {
37b9c1b51eSKate Stone   if (this != &rhs) {
3805097246SAdrian Prantl     // Lock both mutexes to make sure neither side changes anyone on us while
3905097246SAdrian Prantl     // the assignment occurs
40cdd4892fSJim Ingham     std::lock(GetMutex(), rhs.GetMutex());
41cdd4892fSJim Ingham     std::lock_guard<std::recursive_mutex> guard(GetMutex(), std::adopt_lock);
42cdd4892fSJim Ingham     std::lock_guard<std::recursive_mutex> rhs_guard(rhs.GetMutex(),
43cdd4892fSJim Ingham                                                     std::adopt_lock);
44bb19a13cSSaleem Abdulrasool 
4530fdc8d8SChris Lattner     m_process = rhs.m_process;
4630fdc8d8SChris Lattner     m_stop_id = rhs.m_stop_id;
4730fdc8d8SChris Lattner     m_threads = rhs.m_threads;
482976d00aSJim Ingham     m_selected_tid = rhs.m_selected_tid;
4930fdc8d8SChris Lattner   }
5030fdc8d8SChris Lattner   return *this;
5130fdc8d8SChris Lattner }
5230fdc8d8SChris Lattner 
53b9c1b51eSKate Stone ThreadList::~ThreadList() {
5405097246SAdrian Prantl   // Clear the thread list. Clear will take the mutex lock which will ensure
5505097246SAdrian Prantl   // that if anyone is using the list they won't get it removed while using it.
56ac358da5SGreg Clayton   Clear();
5730fdc8d8SChris Lattner }
5830fdc8d8SChris Lattner 
59b9c1b51eSKate Stone lldb::ThreadSP ThreadList::GetExpressionExecutionThread() {
608d94ba0fSJim Ingham   if (m_expression_tid_stack.empty())
618d94ba0fSJim Ingham     return GetSelectedThread();
628d94ba0fSJim Ingham   ThreadSP expr_thread_sp = FindThreadByID(m_expression_tid_stack.back());
638d94ba0fSJim Ingham   if (expr_thread_sp)
648d94ba0fSJim Ingham     return expr_thread_sp;
658d94ba0fSJim Ingham   else
668d94ba0fSJim Ingham     return GetSelectedThread();
678d94ba0fSJim Ingham }
688d94ba0fSJim Ingham 
69b9c1b51eSKate Stone void ThreadList::PushExpressionExecutionThread(lldb::tid_t tid) {
708d94ba0fSJim Ingham   m_expression_tid_stack.push_back(tid);
718d94ba0fSJim Ingham }
728d94ba0fSJim Ingham 
73b9c1b51eSKate Stone void ThreadList::PopExpressionExecutionThread(lldb::tid_t tid) {
748d94ba0fSJim Ingham   assert(m_expression_tid_stack.back() == tid);
758d94ba0fSJim Ingham   m_expression_tid_stack.pop_back();
768d94ba0fSJim Ingham }
7730fdc8d8SChris Lattner 
78b9c1b51eSKate Stone uint32_t ThreadList::GetStopID() const { return m_stop_id; }
7930fdc8d8SChris Lattner 
80b9c1b51eSKate Stone void ThreadList::SetStopID(uint32_t stop_id) { m_stop_id = stop_id; }
8130fdc8d8SChris Lattner 
82b9c1b51eSKate Stone uint32_t ThreadList::GetSize(bool can_update) {
83bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
84bb19a13cSSaleem Abdulrasool 
8530fdc8d8SChris Lattner   if (can_update)
8630fdc8d8SChris Lattner     m_process->UpdateThreadListIfNeeded();
8730fdc8d8SChris Lattner   return m_threads.size();
8830fdc8d8SChris Lattner }
8930fdc8d8SChris Lattner 
90b9c1b51eSKate Stone ThreadSP ThreadList::GetThreadAtIndex(uint32_t idx, bool can_update) {
91bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
92bb19a13cSSaleem Abdulrasool 
9330fdc8d8SChris Lattner   if (can_update)
9430fdc8d8SChris Lattner     m_process->UpdateThreadListIfNeeded();
9530fdc8d8SChris Lattner 
9630fdc8d8SChris Lattner   ThreadSP thread_sp;
9730fdc8d8SChris Lattner   if (idx < m_threads.size())
9830fdc8d8SChris Lattner     thread_sp = m_threads[idx];
9930fdc8d8SChris Lattner   return thread_sp;
10030fdc8d8SChris Lattner }
10130fdc8d8SChris Lattner 
102b9c1b51eSKate Stone ThreadSP ThreadList::FindThreadByID(lldb::tid_t tid, bool can_update) {
103bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
10430fdc8d8SChris Lattner 
10530fdc8d8SChris Lattner   if (can_update)
10630fdc8d8SChris Lattner     m_process->UpdateThreadListIfNeeded();
10730fdc8d8SChris Lattner 
10830fdc8d8SChris Lattner   ThreadSP thread_sp;
10930fdc8d8SChris Lattner   uint32_t idx = 0;
11030fdc8d8SChris Lattner   const uint32_t num_threads = m_threads.size();
111b9c1b51eSKate Stone   for (idx = 0; idx < num_threads; ++idx) {
112b9c1b51eSKate Stone     if (m_threads[idx]->GetID() == tid) {
11330fdc8d8SChris Lattner       thread_sp = m_threads[idx];
11430fdc8d8SChris Lattner       break;
11530fdc8d8SChris Lattner     }
11630fdc8d8SChris Lattner   }
11730fdc8d8SChris Lattner   return thread_sp;
11830fdc8d8SChris Lattner }
11930fdc8d8SChris Lattner 
120b9c1b51eSKate Stone ThreadSP ThreadList::FindThreadByProtocolID(lldb::tid_t tid, bool can_update) {
121bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
122160c9d81SGreg Clayton 
123160c9d81SGreg Clayton   if (can_update)
124160c9d81SGreg Clayton     m_process->UpdateThreadListIfNeeded();
125160c9d81SGreg Clayton 
126160c9d81SGreg Clayton   ThreadSP thread_sp;
127160c9d81SGreg Clayton   uint32_t idx = 0;
128160c9d81SGreg Clayton   const uint32_t num_threads = m_threads.size();
129b9c1b51eSKate Stone   for (idx = 0; idx < num_threads; ++idx) {
130b9c1b51eSKate Stone     if (m_threads[idx]->GetProtocolID() == tid) {
131160c9d81SGreg Clayton       thread_sp = m_threads[idx];
132160c9d81SGreg Clayton       break;
133160c9d81SGreg Clayton     }
134160c9d81SGreg Clayton   }
135160c9d81SGreg Clayton   return thread_sp;
136160c9d81SGreg Clayton }
137160c9d81SGreg Clayton 
138b9c1b51eSKate Stone ThreadSP ThreadList::RemoveThreadByID(lldb::tid_t tid, bool can_update) {
139bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
140c2c423eaSHan Ming Ong 
141c2c423eaSHan Ming Ong   if (can_update)
142c2c423eaSHan Ming Ong     m_process->UpdateThreadListIfNeeded();
143c2c423eaSHan Ming Ong 
144c2c423eaSHan Ming Ong   ThreadSP thread_sp;
145c2c423eaSHan Ming Ong   uint32_t idx = 0;
146c2c423eaSHan Ming Ong   const uint32_t num_threads = m_threads.size();
147b9c1b51eSKate Stone   for (idx = 0; idx < num_threads; ++idx) {
148b9c1b51eSKate Stone     if (m_threads[idx]->GetID() == tid) {
149c2c423eaSHan Ming Ong       thread_sp = m_threads[idx];
150c2c423eaSHan Ming Ong       m_threads.erase(m_threads.begin() + idx);
151c2c423eaSHan Ming Ong       break;
152c2c423eaSHan Ming Ong     }
153c2c423eaSHan Ming Ong   }
154c2c423eaSHan Ming Ong   return thread_sp;
155c2c423eaSHan Ming Ong }
156c2c423eaSHan Ming Ong 
157b9c1b51eSKate Stone ThreadSP ThreadList::RemoveThreadByProtocolID(lldb::tid_t tid,
158b9c1b51eSKate Stone                                               bool can_update) {
159bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
160160c9d81SGreg Clayton 
161160c9d81SGreg Clayton   if (can_update)
162160c9d81SGreg Clayton     m_process->UpdateThreadListIfNeeded();
163160c9d81SGreg Clayton 
164160c9d81SGreg Clayton   ThreadSP thread_sp;
165160c9d81SGreg Clayton   uint32_t idx = 0;
166160c9d81SGreg Clayton   const uint32_t num_threads = m_threads.size();
167b9c1b51eSKate Stone   for (idx = 0; idx < num_threads; ++idx) {
168b9c1b51eSKate Stone     if (m_threads[idx]->GetProtocolID() == tid) {
169160c9d81SGreg Clayton       thread_sp = m_threads[idx];
170160c9d81SGreg Clayton       m_threads.erase(m_threads.begin() + idx);
171160c9d81SGreg Clayton       break;
172160c9d81SGreg Clayton     }
173160c9d81SGreg Clayton   }
174160c9d81SGreg Clayton   return thread_sp;
175160c9d81SGreg Clayton }
176160c9d81SGreg Clayton 
177b9c1b51eSKate Stone ThreadSP ThreadList::GetThreadSPForThreadPtr(Thread *thread_ptr) {
17830fdc8d8SChris Lattner   ThreadSP thread_sp;
179b9c1b51eSKate Stone   if (thread_ptr) {
180bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetMutex());
18130fdc8d8SChris Lattner 
18230fdc8d8SChris Lattner     uint32_t idx = 0;
18330fdc8d8SChris Lattner     const uint32_t num_threads = m_threads.size();
184b9c1b51eSKate Stone     for (idx = 0; idx < num_threads; ++idx) {
185b9c1b51eSKate Stone       if (m_threads[idx].get() == thread_ptr) {
18630fdc8d8SChris Lattner         thread_sp = m_threads[idx];
18730fdc8d8SChris Lattner         break;
18830fdc8d8SChris Lattner       }
18930fdc8d8SChris Lattner     }
19030fdc8d8SChris Lattner   }
19130fdc8d8SChris Lattner   return thread_sp;
19230fdc8d8SChris Lattner }
19330fdc8d8SChris Lattner 
1948db3f7edSJonas Devlieghere ThreadSP ThreadList::GetBackingThread(const ThreadSP &real_thread) {
1958db3f7edSJonas Devlieghere   std::lock_guard<std::recursive_mutex> guard(GetMutex());
1968db3f7edSJonas Devlieghere 
1978db3f7edSJonas Devlieghere   ThreadSP thread_sp;
1988db3f7edSJonas Devlieghere   const uint32_t num_threads = m_threads.size();
1998db3f7edSJonas Devlieghere   for (uint32_t idx = 0; idx < num_threads; ++idx) {
2008db3f7edSJonas Devlieghere     if (m_threads[idx]->GetBackingThread() == real_thread) {
2018db3f7edSJonas Devlieghere       thread_sp = m_threads[idx];
2028db3f7edSJonas Devlieghere       break;
2038db3f7edSJonas Devlieghere     }
2048db3f7edSJonas Devlieghere   }
2058db3f7edSJonas Devlieghere   return thread_sp;
2068db3f7edSJonas Devlieghere }
2078db3f7edSJonas Devlieghere 
208b9c1b51eSKate Stone ThreadSP ThreadList::FindThreadByIndexID(uint32_t index_id, bool can_update) {
209bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
21030fdc8d8SChris Lattner 
21130fdc8d8SChris Lattner   if (can_update)
21230fdc8d8SChris Lattner     m_process->UpdateThreadListIfNeeded();
21330fdc8d8SChris Lattner 
21430fdc8d8SChris Lattner   ThreadSP thread_sp;
21530fdc8d8SChris Lattner   const uint32_t num_threads = m_threads.size();
216b9c1b51eSKate Stone   for (uint32_t idx = 0; idx < num_threads; ++idx) {
217b9c1b51eSKate Stone     if (m_threads[idx]->GetIndexID() == index_id) {
21830fdc8d8SChris Lattner       thread_sp = m_threads[idx];
21930fdc8d8SChris Lattner       break;
22030fdc8d8SChris Lattner     }
22130fdc8d8SChris Lattner   }
22230fdc8d8SChris Lattner   return thread_sp;
22330fdc8d8SChris Lattner }
22430fdc8d8SChris Lattner 
225b9c1b51eSKate Stone bool ThreadList::ShouldStop(Event *event_ptr) {
22630fdc8d8SChris Lattner   // Running events should never stop, obviously...
22730fdc8d8SChris Lattner 
2285160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
22930fdc8d8SChris Lattner 
23005097246SAdrian Prantl   // The ShouldStop method of the threads can do a whole lot of work, figuring
23105097246SAdrian Prantl   // out whether the thread plan conditions are met.  So we don't want to keep
23205097246SAdrian Prantl   // the ThreadList locked the whole time we are doing this.
233b42f3af3SJim Ingham   // FIXME: It is possible that running code could cause new threads
23405097246SAdrian Prantl   // to be created.  If that happens, we will miss asking them whether they
23505097246SAdrian Prantl   // should stop.  This is not a big deal since we haven't had a chance to hang
23605097246SAdrian Prantl   // any interesting operations on those threads yet.
23730fdc8d8SChris Lattner 
238b42f3af3SJim Ingham   collection threads_copy;
239b42f3af3SJim Ingham   {
240b42f3af3SJim Ingham     // Scope for locker
241bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetMutex());
242b42f3af3SJim Ingham 
243b42f3af3SJim Ingham     m_process->UpdateThreadListIfNeeded();
244b9c1b51eSKate Stone     for (lldb::ThreadSP thread_sp : m_threads) {
245b9c1b51eSKate Stone       // This is an optimization...  If we didn't let a thread run in between
24605097246SAdrian Prantl       // the previous stop and this one, we shouldn't have to consult it for
24705097246SAdrian Prantl       // ShouldStop.  So just leave it off the list we are going to inspect. On
24805097246SAdrian Prantl       // Linux, if a thread-specific conditional breakpoint was hit, it won't
24905097246SAdrian Prantl       // necessarily be the thread that hit the breakpoint itself that
25005097246SAdrian Prantl       // evaluates the conditional expression, so the thread that hit the
25105097246SAdrian Prantl       // breakpoint could still be asked to stop, even though it hasn't been
25205097246SAdrian Prantl       // allowed to run since the previous stop.
253b9c1b51eSKate Stone       if (thread_sp->GetTemporaryResumeState() != eStateSuspended ||
254b9c1b51eSKate Stone           thread_sp->IsStillAtLastBreakpointHit())
255569aaf9eSJim Ingham         threads_copy.push_back(thread_sp);
256569aaf9eSJim Ingham     }
257569aaf9eSJim Ingham 
258b9c1b51eSKate Stone     // It is possible the threads we were allowing to run all exited and then
25905097246SAdrian Prantl     // maybe the user interrupted or something, then fall back on looking at
26005097246SAdrian Prantl     // all threads:
261569aaf9eSJim Ingham 
262569aaf9eSJim Ingham     if (threads_copy.size() == 0)
263b42f3af3SJim Ingham       threads_copy = m_threads;
264b42f3af3SJim Ingham   }
265b42f3af3SJim Ingham 
266b42f3af3SJim Ingham   collection::iterator pos, end = threads_copy.end();
26730fdc8d8SChris Lattner 
268b9c1b51eSKate Stone   if (log) {
26910c4b249SJim Ingham     log->PutCString("");
270*63e5fb76SJonas Devlieghere     LLDB_LOGF(log,
271*63e5fb76SJonas Devlieghere               "ThreadList::%s: %" PRIu64 " threads, %" PRIu64
272b9c1b51eSKate Stone               " unsuspended threads",
273b9c1b51eSKate Stone               __FUNCTION__, (uint64_t)m_threads.size(),
274569aaf9eSJim Ingham               (uint64_t)threads_copy.size());
27510c4b249SJim Ingham   }
2762cad65a5SGreg Clayton 
277a0079044SJim Ingham   bool did_anybody_stop_for_a_reason = false;
27835878c47SJim Ingham 
279b9c1b51eSKate Stone   // If the event is an Interrupt event, then we're going to stop no matter
280b9c1b51eSKate Stone   // what.  Otherwise, presume we won't stop.
281a0079044SJim Ingham   bool should_stop = false;
282b9c1b51eSKate Stone   if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) {
283*63e5fb76SJonas Devlieghere     LLDB_LOGF(
284*63e5fb76SJonas Devlieghere         log, "ThreadList::%s handling interrupt event, should stop set to true",
285b9c1b51eSKate Stone         __FUNCTION__);
28635878c47SJim Ingham 
28735878c47SJim Ingham     should_stop = true;
28835878c47SJim Ingham   }
2897bc3465fSJim Ingham 
290b9c1b51eSKate Stone   // Now we run through all the threads and get their stop info's.  We want to
29105097246SAdrian Prantl   // make sure to do this first before we start running the ShouldStop, because
29205097246SAdrian Prantl   // one thread's ShouldStop could destroy information (like deleting a thread
29305097246SAdrian Prantl   // specific breakpoint another thread had stopped at) which could lead us to
29405097246SAdrian Prantl   // compute the StopInfo incorrectly. We don't need to use it here, we just
29505097246SAdrian Prantl   // want to make sure it gets computed.
2967bc3465fSJim Ingham 
297b9c1b51eSKate Stone   for (pos = threads_copy.begin(); pos != end; ++pos) {
2987bc3465fSJim Ingham     ThreadSP thread_sp(*pos);
2997bc3465fSJim Ingham     thread_sp->GetStopInfo();
3007bc3465fSJim Ingham   }
301a0079044SJim Ingham 
302b9c1b51eSKate Stone   for (pos = threads_copy.begin(); pos != end; ++pos) {
30330fdc8d8SChris Lattner     ThreadSP thread_sp(*pos);
3042cad65a5SGreg Clayton 
305b9c1b51eSKate Stone     // We should never get a stop for which no thread had a stop reason, but
30605097246SAdrian Prantl     // sometimes we do see this - for instance when we first connect to a
30705097246SAdrian Prantl     // remote stub.  In that case we should stop, since we can't figure out the
30805097246SAdrian Prantl     // right thing to do and stopping gives the user control over what to do in
30905097246SAdrian Prantl     // this instance.
31039fdae7fSJim Ingham     //
311b9c1b51eSKate Stone     // Note, this causes a problem when you have a thread specific breakpoint,
31205097246SAdrian Prantl     // and a bunch of threads hit the breakpoint, but not the thread which we
31305097246SAdrian Prantl     // are waiting for.  All the threads that are not "supposed" to hit the
31405097246SAdrian Prantl     // breakpoint are marked as having no stop reason, which is right, they
31505097246SAdrian Prantl     // should not show a stop reason.  But that triggers this code and causes
31605097246SAdrian Prantl     // us to stop seemingly for no reason.
31739fdae7fSJim Ingham     //
318b9c1b51eSKate Stone     // Since the only way we ever saw this error was on first attach, I'm only
31905097246SAdrian Prantl     // going to trigger set did_anybody_stop_for_a_reason to true unless this
32005097246SAdrian Prantl     // is the first stop.
32139fdae7fSJim Ingham     //
322b9c1b51eSKate Stone     // If this becomes a problem, we'll have to have another StopReason like
32305097246SAdrian Prantl     // "StopInfoHidden" which will look invalid everywhere but at this check.
32439fdae7fSJim Ingham 
32521afbe03SEd Maste     if (thread_sp->GetProcess()->GetStopID() > 1)
32639fdae7fSJim Ingham       did_anybody_stop_for_a_reason = true;
32739fdae7fSJim Ingham     else
328a0079044SJim Ingham       did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
329a0079044SJim Ingham 
33010c4b249SJim Ingham     const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
3312cad65a5SGreg Clayton     if (thread_should_stop)
3322cad65a5SGreg Clayton       should_stop |= true;
3332cad65a5SGreg Clayton   }
3342cad65a5SGreg Clayton 
335b9c1b51eSKate Stone   if (!should_stop && !did_anybody_stop_for_a_reason) {
336a0079044SJim Ingham     should_stop = true;
337*63e5fb76SJonas Devlieghere     LLDB_LOGF(log,
338*63e5fb76SJonas Devlieghere               "ThreadList::%s we stopped but no threads had a stop reason, "
339b9c1b51eSKate Stone               "overriding should_stop and stopping.",
340b9c1b51eSKate Stone               __FUNCTION__);
341a0079044SJim Ingham   }
342a0079044SJim Ingham 
343*63e5fb76SJonas Devlieghere   LLDB_LOGF(log, "ThreadList::%s overall should_stop = %i", __FUNCTION__,
344b9c1b51eSKate Stone             should_stop);
3452cad65a5SGreg Clayton 
346b9c1b51eSKate Stone   if (should_stop) {
347b9c1b51eSKate Stone     for (pos = threads_copy.begin(); pos != end; ++pos) {
34830fdc8d8SChris Lattner       ThreadSP thread_sp(*pos);
34930fdc8d8SChris Lattner       thread_sp->WillStop();
35030fdc8d8SChris Lattner     }
35130fdc8d8SChris Lattner   }
35230fdc8d8SChris Lattner 
35330fdc8d8SChris Lattner   return should_stop;
35430fdc8d8SChris Lattner }
35530fdc8d8SChris Lattner 
356b9c1b51eSKate Stone Vote ThreadList::ShouldReportStop(Event *event_ptr) {
357bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
3582cad65a5SGreg Clayton 
35930fdc8d8SChris Lattner   Vote result = eVoteNoOpinion;
36030fdc8d8SChris Lattner   m_process->UpdateThreadListIfNeeded();
36130fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
36230fdc8d8SChris Lattner 
3635160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3642cad65a5SGreg Clayton 
365*63e5fb76SJonas Devlieghere   LLDB_LOGF(log, "ThreadList::%s %" PRIu64 " threads", __FUNCTION__,
366b9c1b51eSKate Stone             (uint64_t)m_threads.size());
3672cad65a5SGreg Clayton 
36805097246SAdrian Prantl   // Run through the threads and ask whether we should report this event. For
36905097246SAdrian Prantl   // stopping, a YES vote wins over everything.  A NO vote wins over NO
370b9c1b51eSKate Stone   // opinion.
371b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
37230fdc8d8SChris Lattner     ThreadSP thread_sp(*pos);
373e0d378b3SGreg Clayton     const Vote vote = thread_sp->ShouldReportStop(event_ptr);
374b9c1b51eSKate Stone     switch (vote) {
37530fdc8d8SChris Lattner     case eVoteNoOpinion:
37630fdc8d8SChris Lattner       continue;
3772cad65a5SGreg Clayton 
37830fdc8d8SChris Lattner     case eVoteYes:
37930fdc8d8SChris Lattner       result = eVoteYes;
38030fdc8d8SChris Lattner       break;
3812cad65a5SGreg Clayton 
38230fdc8d8SChris Lattner     case eVoteNo:
383b9c1b51eSKate Stone       if (result == eVoteNoOpinion) {
38430fdc8d8SChris Lattner         result = eVoteNo;
385b9c1b51eSKate Stone       } else {
386df44988bSZachary Turner         LLDB_LOG(log,
387df44988bSZachary Turner           "Thread {0:x} voted {1}, but lost out because result was {2}",
388df44988bSZachary Turner           thread_sp->GetID(), vote, result);
3892cad65a5SGreg Clayton       }
39030fdc8d8SChris Lattner       break;
39130fdc8d8SChris Lattner     }
39230fdc8d8SChris Lattner   }
393df44988bSZachary Turner   LLDB_LOG(log, "Returning {0}", result);
39430fdc8d8SChris Lattner   return result;
39530fdc8d8SChris Lattner }
39630fdc8d8SChris Lattner 
397b9c1b51eSKate Stone void ThreadList::SetShouldReportStop(Vote vote) {
398bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
399bb19a13cSSaleem Abdulrasool 
400221d51cfSJim Ingham   m_process->UpdateThreadListIfNeeded();
401221d51cfSJim Ingham   collection::iterator pos, end = m_threads.end();
402b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
403221d51cfSJim Ingham     ThreadSP thread_sp(*pos);
404221d51cfSJim Ingham     thread_sp->SetShouldReportStop(vote);
405221d51cfSJim Ingham   }
406221d51cfSJim Ingham }
407221d51cfSJim Ingham 
408b9c1b51eSKate Stone Vote ThreadList::ShouldReportRun(Event *event_ptr) {
4092cad65a5SGreg Clayton 
410bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
4112cad65a5SGreg Clayton 
41230fdc8d8SChris Lattner   Vote result = eVoteNoOpinion;
41330fdc8d8SChris Lattner   m_process->UpdateThreadListIfNeeded();
41430fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
41530fdc8d8SChris Lattner 
41605097246SAdrian Prantl   // Run through the threads and ask whether we should report this event. The
41705097246SAdrian Prantl   // rule is NO vote wins over everything, a YES vote wins over no opinion.
41830fdc8d8SChris Lattner 
4195160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
420ce579839SJim Ingham 
421b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
422b9c1b51eSKate Stone     if ((*pos)->GetResumeState() != eStateSuspended) {
423b9c1b51eSKate Stone       switch ((*pos)->ShouldReportRun(event_ptr)) {
42430fdc8d8SChris Lattner       case eVoteNoOpinion:
42530fdc8d8SChris Lattner         continue;
42630fdc8d8SChris Lattner       case eVoteYes:
42730fdc8d8SChris Lattner         if (result == eVoteNoOpinion)
42830fdc8d8SChris Lattner           result = eVoteYes;
42930fdc8d8SChris Lattner         break;
43030fdc8d8SChris Lattner       case eVoteNo:
431*63e5fb76SJonas Devlieghere         LLDB_LOGF(log,
432*63e5fb76SJonas Devlieghere                   "ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64
433b9c1b51eSKate Stone                   ") says don't report.",
434b9c1b51eSKate Stone                   (*pos)->GetIndexID(), (*pos)->GetID());
43530fdc8d8SChris Lattner         result = eVoteNo;
43630fdc8d8SChris Lattner         break;
43730fdc8d8SChris Lattner       }
43830fdc8d8SChris Lattner     }
439ce579839SJim Ingham   }
44030fdc8d8SChris Lattner   return result;
44130fdc8d8SChris Lattner }
44230fdc8d8SChris Lattner 
443b9c1b51eSKate Stone void ThreadList::Clear() {
444bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
44530fdc8d8SChris Lattner   m_stop_id = 0;
44630fdc8d8SChris Lattner   m_threads.clear();
4472976d00aSJim Ingham   m_selected_tid = LLDB_INVALID_THREAD_ID;
44830fdc8d8SChris Lattner }
44930fdc8d8SChris Lattner 
450b9c1b51eSKate Stone void ThreadList::Destroy() {
451bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
452e1cd1be6SGreg Clayton   const uint32_t num_threads = m_threads.size();
453b9c1b51eSKate Stone   for (uint32_t idx = 0; idx < num_threads; ++idx) {
454e1cd1be6SGreg Clayton     m_threads[idx]->DestroyThread();
455e1cd1be6SGreg Clayton   }
456e1cd1be6SGreg Clayton }
457e1cd1be6SGreg Clayton 
458b9c1b51eSKate Stone void ThreadList::RefreshStateAfterStop() {
459bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
46030fdc8d8SChris Lattner 
46130fdc8d8SChris Lattner   m_process->UpdateThreadListIfNeeded();
46230fdc8d8SChris Lattner 
4635160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
46410c4b249SJim Ingham   if (log && log->GetVerbose())
465*63e5fb76SJonas Devlieghere     LLDB_LOGF(log,
466*63e5fb76SJonas Devlieghere               "Turning off notification of new threads while single stepping "
467b9c1b51eSKate Stone               "a thread.");
4681c823b43SJim Ingham 
46930fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
47030fdc8d8SChris Lattner   for (pos = m_threads.begin(); pos != end; ++pos)
47130fdc8d8SChris Lattner     (*pos)->RefreshStateAfterStop();
47230fdc8d8SChris Lattner }
47330fdc8d8SChris Lattner 
474b9c1b51eSKate Stone void ThreadList::DiscardThreadPlans() {
47505097246SAdrian Prantl   // You don't need to update the thread list here, because only threads that
47605097246SAdrian Prantl   // you currently know about have any thread plans.
477bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
47830fdc8d8SChris Lattner 
47930fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
48030fdc8d8SChris Lattner   for (pos = m_threads.begin(); pos != end; ++pos)
48130fdc8d8SChris Lattner     (*pos)->DiscardThreadPlans(true);
48230fdc8d8SChris Lattner }
48330fdc8d8SChris Lattner 
484b9c1b51eSKate Stone bool ThreadList::WillResume() {
48505097246SAdrian Prantl   // Run through the threads and perform their momentary actions. But we only
48605097246SAdrian Prantl   // do this for threads that are running, user suspended threads stay where
48705097246SAdrian Prantl   // they are.
48830fdc8d8SChris Lattner 
489bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
49030fdc8d8SChris Lattner   m_process->UpdateThreadListIfNeeded();
49130fdc8d8SChris Lattner 
49230fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
49330fdc8d8SChris Lattner 
494a3241c1bSJim Ingham   // See if any thread wants to run stopping others.  If it does, then we won't
49505097246SAdrian Prantl   // setup the other threads for resume, since they aren't going to get a
49605097246SAdrian Prantl   // chance to run.  This is necessary because the SetupForResume might add
49705097246SAdrian Prantl   // "StopOthers" plans which would then get to be part of the who-gets-to-run
49805097246SAdrian Prantl   // negotiation, but they're coming in after the fact, and the threads that
49905097246SAdrian Prantl   // are already set up should take priority.
500a3241c1bSJim Ingham 
501a3241c1bSJim Ingham   bool wants_solo_run = false;
50230fdc8d8SChris Lattner 
503b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
504b9c1b51eSKate Stone     lldbassert((*pos)->GetCurrentPlan() &&
505b9c1b51eSKate Stone                "thread should not have null thread plan");
5068f186f85SZachary Turner     if ((*pos)->GetResumeState() != eStateSuspended &&
507b9c1b51eSKate Stone         (*pos)->GetCurrentPlan()->StopOthers()) {
508b9c1b51eSKate Stone       if ((*pos)->IsOperatingSystemPluginThread() &&
509b9c1b51eSKate Stone           !(*pos)->GetBackingThread())
5106e0ff1a3SGreg Clayton         continue;
511a3241c1bSJim Ingham       wants_solo_run = true;
512a3241c1bSJim Ingham       break;
513a3241c1bSJim Ingham     }
514a3241c1bSJim Ingham   }
515a3241c1bSJim Ingham 
516b9c1b51eSKate Stone   if (wants_solo_run) {
5175160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
51810c4b249SJim Ingham     if (log && log->GetVerbose())
519*63e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Turning on notification of new threads while single "
520b9c1b51eSKate Stone                      "stepping a thread.");
5211c823b43SJim Ingham     m_process->StartNoticingNewThreads();
522b9c1b51eSKate Stone   } else {
5235160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
52410c4b249SJim Ingham     if (log && log->GetVerbose())
525*63e5fb76SJonas Devlieghere       LLDB_LOGF(log, "Turning off notification of new threads while single "
526b9c1b51eSKate Stone                      "stepping a thread.");
5271c823b43SJim Ingham     m_process->StopNoticingNewThreads();
5281c823b43SJim Ingham   }
529a3241c1bSJim Ingham 
530b9c1b51eSKate Stone   // Give all the threads that are likely to run a last chance to set up their
53105097246SAdrian Prantl   // state before we negotiate who is actually going to get a chance to run...
532b9c1b51eSKate Stone   // Don't set to resume suspended threads, and if any thread wanted to stop
53305097246SAdrian Prantl   // others, only call setup on the threads that request StopOthers...
534a3241c1bSJim Ingham 
535b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
536b9c1b51eSKate Stone     if ((*pos)->GetResumeState() != eStateSuspended &&
537b9c1b51eSKate Stone         (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers())) {
538b9c1b51eSKate Stone       if ((*pos)->IsOperatingSystemPluginThread() &&
539b9c1b51eSKate Stone           !(*pos)->GetBackingThread())
5406e0ff1a3SGreg Clayton         continue;
54130fdc8d8SChris Lattner       (*pos)->SetupForResume();
542a3241c1bSJim Ingham     }
543a3241c1bSJim Ingham   }
54430fdc8d8SChris Lattner 
54530fdc8d8SChris Lattner   // Now go through the threads and see if any thread wants to run just itself.
54630fdc8d8SChris Lattner   // if so then pick one and run it.
547a3241c1bSJim Ingham 
54830fdc8d8SChris Lattner   ThreadList run_me_only_list(m_process);
54930fdc8d8SChris Lattner 
55030fdc8d8SChris Lattner   run_me_only_list.SetStopID(m_process->GetStopID());
55130fdc8d8SChris Lattner 
55230fdc8d8SChris Lattner   bool run_only_current_thread = false;
55330fdc8d8SChris Lattner 
554b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
55530fdc8d8SChris Lattner     ThreadSP thread_sp(*pos);
556b15bfc75SJim Ingham     if (thread_sp->GetResumeState() != eStateSuspended &&
557b9c1b51eSKate Stone         thread_sp->GetCurrentPlan()->StopOthers()) {
558b9c1b51eSKate Stone       if ((*pos)->IsOperatingSystemPluginThread() &&
559b9c1b51eSKate Stone           !(*pos)->GetBackingThread())
5606e0ff1a3SGreg Clayton         continue;
5616e0ff1a3SGreg Clayton 
56230fdc8d8SChris Lattner       // You can't say "stop others" and also want yourself to be suspended.
56330fdc8d8SChris Lattner       assert(thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
56430fdc8d8SChris Lattner 
565b9c1b51eSKate Stone       if (thread_sp == GetSelectedThread()) {
566b9c1b51eSKate Stone         // If the currently selected thread wants to run on its own, always let
567b9c1b51eSKate Stone         // it.
56830fdc8d8SChris Lattner         run_only_current_thread = true;
56930fdc8d8SChris Lattner         run_me_only_list.Clear();
57030fdc8d8SChris Lattner         run_me_only_list.AddThread(thread_sp);
57130fdc8d8SChris Lattner         break;
57230fdc8d8SChris Lattner       }
57330fdc8d8SChris Lattner 
57430fdc8d8SChris Lattner       run_me_only_list.AddThread(thread_sp);
57530fdc8d8SChris Lattner     }
57630fdc8d8SChris Lattner   }
57730fdc8d8SChris Lattner 
578513c6bb8SJim Ingham   bool need_to_resume = true;
579513c6bb8SJim Ingham 
580b9c1b51eSKate Stone   if (run_me_only_list.GetSize(false) == 0) {
58130fdc8d8SChris Lattner     // Everybody runs as they wish:
582b9c1b51eSKate Stone     for (pos = m_threads.begin(); pos != end; ++pos) {
58330fdc8d8SChris Lattner       ThreadSP thread_sp(*pos);
584cb5d5a57SJim Ingham       StateType run_state;
585cb5d5a57SJim Ingham       if (thread_sp->GetResumeState() != eStateSuspended)
586cb5d5a57SJim Ingham         run_state = thread_sp->GetCurrentPlan()->RunState();
587cb5d5a57SJim Ingham       else
588cb5d5a57SJim Ingham         run_state = eStateSuspended;
589160c9d81SGreg Clayton       if (!thread_sp->ShouldResume(run_state))
590513c6bb8SJim Ingham         need_to_resume = false;
59130fdc8d8SChris Lattner     }
592b9c1b51eSKate Stone   } else {
59330fdc8d8SChris Lattner     ThreadSP thread_to_run;
59430fdc8d8SChris Lattner 
595b9c1b51eSKate Stone     if (run_only_current_thread) {
5962976d00aSJim Ingham       thread_to_run = GetSelectedThread();
597b9c1b51eSKate Stone     } else if (run_me_only_list.GetSize(false) == 1) {
59830fdc8d8SChris Lattner       thread_to_run = run_me_only_list.GetThreadAtIndex(0);
599b9c1b51eSKate Stone     } else {
600b9c1b51eSKate Stone       int random_thread =
601b9c1b51eSKate Stone           (int)((run_me_only_list.GetSize(false) * (double)rand()) /
602b9c1b51eSKate Stone                 (RAND_MAX + 1.0));
60330fdc8d8SChris Lattner       thread_to_run = run_me_only_list.GetThreadAtIndex(random_thread);
60430fdc8d8SChris Lattner     }
60530fdc8d8SChris Lattner 
606b9c1b51eSKate Stone     for (pos = m_threads.begin(); pos != end; ++pos) {
60730fdc8d8SChris Lattner       ThreadSP thread_sp(*pos);
608b9c1b51eSKate Stone       if (thread_sp == thread_to_run) {
609160c9d81SGreg Clayton         if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
610513c6bb8SJim Ingham           need_to_resume = false;
611b9c1b51eSKate Stone       } else
612160c9d81SGreg Clayton         thread_sp->ShouldResume(eStateSuspended);
61330fdc8d8SChris Lattner     }
61430fdc8d8SChris Lattner   }
61530fdc8d8SChris Lattner 
616513c6bb8SJim Ingham   return need_to_resume;
61730fdc8d8SChris Lattner }
61830fdc8d8SChris Lattner 
619b9c1b51eSKate Stone void ThreadList::DidResume() {
620bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
62130fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
622b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
62330fdc8d8SChris Lattner     // Don't clear out threads that aren't going to get a chance to run, rather
62430fdc8d8SChris Lattner     // leave their state for the next time around.
62530fdc8d8SChris Lattner     ThreadSP thread_sp(*pos);
62630fdc8d8SChris Lattner     if (thread_sp->GetResumeState() != eStateSuspended)
62730fdc8d8SChris Lattner       thread_sp->DidResume();
62830fdc8d8SChris Lattner   }
62930fdc8d8SChris Lattner }
63030fdc8d8SChris Lattner 
631b9c1b51eSKate Stone void ThreadList::DidStop() {
632bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
63329d65744SAndrew Kaylor   collection::iterator pos, end = m_threads.end();
634b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
63505097246SAdrian Prantl     // Notify threads that the process just stopped. Note, this currently
63605097246SAdrian Prantl     // assumes that all threads in the list stop when the process stops.  In
63705097246SAdrian Prantl     // the future we will want to support a debugging model where some threads
63805097246SAdrian Prantl     // continue to run while others are stopped.  We either need to handle that
63905097246SAdrian Prantl     // somehow here or create a special thread list containing only threads
64005097246SAdrian Prantl     // which will stop in the code that calls this method (currently
64129d65744SAndrew Kaylor     // Process::SetPrivateState).
64229d65744SAndrew Kaylor     ThreadSP thread_sp(*pos);
64329d65744SAndrew Kaylor     if (StateIsRunningState(thread_sp->GetState()))
64429d65744SAndrew Kaylor       thread_sp->DidStop();
64529d65744SAndrew Kaylor   }
64629d65744SAndrew Kaylor }
64729d65744SAndrew Kaylor 
648b9c1b51eSKate Stone ThreadSP ThreadList::GetSelectedThread() {
649bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
650943ddb73SJohnny Chen   ThreadSP thread_sp = FindThreadByID(m_selected_tid);
651b9c1b51eSKate Stone   if (!thread_sp.get()) {
652354b9a65SJason Molenda     if (m_threads.size() == 0)
653354b9a65SJason Molenda       return thread_sp;
654943ddb73SJohnny Chen     m_selected_tid = m_threads[0]->GetID();
655943ddb73SJohnny Chen     thread_sp = m_threads[0];
656943ddb73SJohnny Chen   }
657943ddb73SJohnny Chen   return thread_sp;
65830fdc8d8SChris Lattner }
65930fdc8d8SChris Lattner 
660b9c1b51eSKate Stone bool ThreadList::SetSelectedThreadByID(lldb::tid_t tid, bool notify) {
661bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
662b7f6b2faSJim Ingham   ThreadSP selected_thread_sp(FindThreadByID(tid));
663b9c1b51eSKate Stone   if (selected_thread_sp) {
6642976d00aSJim Ingham     m_selected_tid = tid;
665b7f6b2faSJim Ingham     selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
666b9c1b51eSKate Stone   } else
6672976d00aSJim Ingham     m_selected_tid = LLDB_INVALID_THREAD_ID;
66830fdc8d8SChris Lattner 
669c3faa195SJim Ingham   if (notify)
670c3faa195SJim Ingham     NotifySelectedThreadChanged(m_selected_tid);
671c3faa195SJim Ingham 
6722976d00aSJim Ingham   return m_selected_tid != LLDB_INVALID_THREAD_ID;
67330fdc8d8SChris Lattner }
67430fdc8d8SChris Lattner 
675b9c1b51eSKate Stone bool ThreadList::SetSelectedThreadByIndexID(uint32_t index_id, bool notify) {
676bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
677b7f6b2faSJim Ingham   ThreadSP selected_thread_sp(FindThreadByIndexID(index_id));
678b9c1b51eSKate Stone   if (selected_thread_sp.get()) {
679b7f6b2faSJim Ingham     m_selected_tid = selected_thread_sp->GetID();
680b7f6b2faSJim Ingham     selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
681b9c1b51eSKate Stone   } else
6822976d00aSJim Ingham     m_selected_tid = LLDB_INVALID_THREAD_ID;
68330fdc8d8SChris Lattner 
684c3faa195SJim Ingham   if (notify)
685c3faa195SJim Ingham     NotifySelectedThreadChanged(m_selected_tid);
686c3faa195SJim Ingham 
6872976d00aSJim Ingham   return m_selected_tid != LLDB_INVALID_THREAD_ID;
68830fdc8d8SChris Lattner }
68930fdc8d8SChris Lattner 
690b9c1b51eSKate Stone void ThreadList::NotifySelectedThreadChanged(lldb::tid_t tid) {
691c3faa195SJim Ingham   ThreadSP selected_thread_sp(FindThreadByID(tid));
692b9c1b51eSKate Stone   if (selected_thread_sp->EventTypeHasListeners(
693b9c1b51eSKate Stone           Thread::eBroadcastBitThreadSelected))
694b9c1b51eSKate Stone     selected_thread_sp->BroadcastEvent(
695b9c1b51eSKate Stone         Thread::eBroadcastBitThreadSelected,
696c3faa195SJim Ingham         new Thread::ThreadEventData(selected_thread_sp));
697c3faa195SJim Ingham }
698c3faa195SJim Ingham 
699b9c1b51eSKate Stone void ThreadList::Update(ThreadList &rhs) {
700b9c1b51eSKate Stone   if (this != &rhs) {
70105097246SAdrian Prantl     // Lock both mutexes to make sure neither side changes anyone on us while
70205097246SAdrian Prantl     // the assignment occurs
703bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetMutex());
704bb19a13cSSaleem Abdulrasool 
70556d9a1b3SGreg Clayton     m_process = rhs.m_process;
70656d9a1b3SGreg Clayton     m_stop_id = rhs.m_stop_id;
70756d9a1b3SGreg Clayton     m_threads.swap(rhs.m_threads);
70856d9a1b3SGreg Clayton     m_selected_tid = rhs.m_selected_tid;
709e1cd1be6SGreg Clayton 
71005097246SAdrian Prantl     // Now we look for threads that we are done with and make sure to clear
71105097246SAdrian Prantl     // them up as much as possible so anyone with a shared pointer will still
71205097246SAdrian Prantl     // have a reference, but the thread won't be of much use. Using
71305097246SAdrian Prantl     // std::weak_ptr for all backward references (such as a thread to a
71405097246SAdrian Prantl     // process) will eventually solve this issue for us, but for now, we need
71505097246SAdrian Prantl     // to work around the issue
716e1cd1be6SGreg Clayton     collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
717b9c1b51eSKate Stone     for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos) {
718e1cd1be6SGreg Clayton       const lldb::tid_t tid = (*rhs_pos)->GetID();
719e1cd1be6SGreg Clayton       bool thread_is_alive = false;
720e1cd1be6SGreg Clayton       const uint32_t num_threads = m_threads.size();
721b9c1b51eSKate Stone       for (uint32_t idx = 0; idx < num_threads; ++idx) {
72265d4d5c3SRyan Brown         ThreadSP backing_thread = m_threads[idx]->GetBackingThread();
723b9c1b51eSKate Stone         if (m_threads[idx]->GetID() == tid ||
724b9c1b51eSKate Stone             (backing_thread && backing_thread->GetID() == tid)) {
725e1cd1be6SGreg Clayton           thread_is_alive = true;
726e1cd1be6SGreg Clayton           break;
727e1cd1be6SGreg Clayton         }
728e1cd1be6SGreg Clayton       }
729e1cd1be6SGreg Clayton       if (!thread_is_alive)
730e1cd1be6SGreg Clayton         (*rhs_pos)->DestroyThread();
731e1cd1be6SGreg Clayton     }
73256d9a1b3SGreg Clayton   }
73356d9a1b3SGreg Clayton }
73456d9a1b3SGreg Clayton 
735b9c1b51eSKate Stone void ThreadList::Flush() {
736bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
737fa559e5cSGreg Clayton   collection::iterator pos, end = m_threads.end();
738fa559e5cSGreg Clayton   for (pos = m_threads.begin(); pos != end; ++pos)
739fa559e5cSGreg Clayton     (*pos)->Flush();
740fa559e5cSGreg Clayton }
74156d9a1b3SGreg Clayton 
742b5cd6e7bSGreg Clayton std::recursive_mutex &ThreadList::GetMutex() const {
743ba4e61d3SAndrew Kaylor   return m_process->m_thread_mutex;
744ba4e61d3SAndrew Kaylor }
745ba4e61d3SAndrew Kaylor 
746b9c1b51eSKate Stone ThreadList::ExpressionExecutionThreadPusher::ExpressionExecutionThreadPusher(
747b9c1b51eSKate Stone     lldb::ThreadSP thread_sp)
748b9c1b51eSKate Stone     : m_thread_list(nullptr), m_tid(LLDB_INVALID_THREAD_ID) {
749b9c1b51eSKate Stone   if (thread_sp) {
7508d94ba0fSJim Ingham     m_tid = thread_sp->GetID();
7518d94ba0fSJim Ingham     m_thread_list = &thread_sp->GetProcess()->GetThreadList();
7528d94ba0fSJim Ingham     m_thread_list->PushExpressionExecutionThread(m_tid);
7538d94ba0fSJim Ingham   }
7548d94ba0fSJim Ingham }
755