130fdc8d8SChris Lattner //===-- ThreadList.cpp ------------------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler 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
40bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetMutex());
41b5cd6e7bSGreg Clayton     std::lock_guard<std::recursive_mutex> rhs_guard(rhs.GetMutex());
42bb19a13cSSaleem Abdulrasool 
4330fdc8d8SChris Lattner     m_process = rhs.m_process;
4430fdc8d8SChris Lattner     m_stop_id = rhs.m_stop_id;
4530fdc8d8SChris Lattner     m_threads = rhs.m_threads;
462976d00aSJim Ingham     m_selected_tid = rhs.m_selected_tid;
4730fdc8d8SChris Lattner   }
4830fdc8d8SChris Lattner   return *this;
4930fdc8d8SChris Lattner }
5030fdc8d8SChris Lattner 
51b9c1b51eSKate Stone ThreadList::~ThreadList() {
5205097246SAdrian Prantl   // Clear the thread list. Clear will take the mutex lock which will ensure
5305097246SAdrian Prantl   // that if anyone is using the list they won't get it removed while using it.
54ac358da5SGreg Clayton   Clear();
5530fdc8d8SChris Lattner }
5630fdc8d8SChris Lattner 
57b9c1b51eSKate Stone lldb::ThreadSP ThreadList::GetExpressionExecutionThread() {
588d94ba0fSJim Ingham   if (m_expression_tid_stack.empty())
598d94ba0fSJim Ingham     return GetSelectedThread();
608d94ba0fSJim Ingham   ThreadSP expr_thread_sp = FindThreadByID(m_expression_tid_stack.back());
618d94ba0fSJim Ingham   if (expr_thread_sp)
628d94ba0fSJim Ingham     return expr_thread_sp;
638d94ba0fSJim Ingham   else
648d94ba0fSJim Ingham     return GetSelectedThread();
658d94ba0fSJim Ingham }
668d94ba0fSJim Ingham 
67b9c1b51eSKate Stone void ThreadList::PushExpressionExecutionThread(lldb::tid_t tid) {
688d94ba0fSJim Ingham   m_expression_tid_stack.push_back(tid);
698d94ba0fSJim Ingham }
708d94ba0fSJim Ingham 
71b9c1b51eSKate Stone void ThreadList::PopExpressionExecutionThread(lldb::tid_t tid) {
728d94ba0fSJim Ingham   assert(m_expression_tid_stack.back() == tid);
738d94ba0fSJim Ingham   m_expression_tid_stack.pop_back();
748d94ba0fSJim Ingham }
7530fdc8d8SChris Lattner 
76b9c1b51eSKate Stone uint32_t ThreadList::GetStopID() const { return m_stop_id; }
7730fdc8d8SChris Lattner 
78b9c1b51eSKate Stone void ThreadList::SetStopID(uint32_t stop_id) { m_stop_id = stop_id; }
7930fdc8d8SChris Lattner 
80b9c1b51eSKate Stone uint32_t ThreadList::GetSize(bool can_update) {
81bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
82bb19a13cSSaleem Abdulrasool 
8330fdc8d8SChris Lattner   if (can_update)
8430fdc8d8SChris Lattner     m_process->UpdateThreadListIfNeeded();
8530fdc8d8SChris Lattner   return m_threads.size();
8630fdc8d8SChris Lattner }
8730fdc8d8SChris Lattner 
88b9c1b51eSKate Stone ThreadSP ThreadList::GetThreadAtIndex(uint32_t idx, bool can_update) {
89bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
90bb19a13cSSaleem Abdulrasool 
9130fdc8d8SChris Lattner   if (can_update)
9230fdc8d8SChris Lattner     m_process->UpdateThreadListIfNeeded();
9330fdc8d8SChris Lattner 
9430fdc8d8SChris Lattner   ThreadSP thread_sp;
9530fdc8d8SChris Lattner   if (idx < m_threads.size())
9630fdc8d8SChris Lattner     thread_sp = m_threads[idx];
9730fdc8d8SChris Lattner   return thread_sp;
9830fdc8d8SChris Lattner }
9930fdc8d8SChris Lattner 
100b9c1b51eSKate Stone ThreadSP ThreadList::FindThreadByID(lldb::tid_t tid, bool can_update) {
101bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
10230fdc8d8SChris Lattner 
10330fdc8d8SChris Lattner   if (can_update)
10430fdc8d8SChris Lattner     m_process->UpdateThreadListIfNeeded();
10530fdc8d8SChris Lattner 
10630fdc8d8SChris Lattner   ThreadSP thread_sp;
10730fdc8d8SChris Lattner   uint32_t idx = 0;
10830fdc8d8SChris Lattner   const uint32_t num_threads = m_threads.size();
109b9c1b51eSKate Stone   for (idx = 0; idx < num_threads; ++idx) {
110b9c1b51eSKate Stone     if (m_threads[idx]->GetID() == tid) {
11130fdc8d8SChris Lattner       thread_sp = m_threads[idx];
11230fdc8d8SChris Lattner       break;
11330fdc8d8SChris Lattner     }
11430fdc8d8SChris Lattner   }
11530fdc8d8SChris Lattner   return thread_sp;
11630fdc8d8SChris Lattner }
11730fdc8d8SChris Lattner 
118b9c1b51eSKate Stone ThreadSP ThreadList::FindThreadByProtocolID(lldb::tid_t tid, bool can_update) {
119bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
120160c9d81SGreg Clayton 
121160c9d81SGreg Clayton   if (can_update)
122160c9d81SGreg Clayton     m_process->UpdateThreadListIfNeeded();
123160c9d81SGreg Clayton 
124160c9d81SGreg Clayton   ThreadSP thread_sp;
125160c9d81SGreg Clayton   uint32_t idx = 0;
126160c9d81SGreg Clayton   const uint32_t num_threads = m_threads.size();
127b9c1b51eSKate Stone   for (idx = 0; idx < num_threads; ++idx) {
128b9c1b51eSKate Stone     if (m_threads[idx]->GetProtocolID() == tid) {
129160c9d81SGreg Clayton       thread_sp = m_threads[idx];
130160c9d81SGreg Clayton       break;
131160c9d81SGreg Clayton     }
132160c9d81SGreg Clayton   }
133160c9d81SGreg Clayton   return thread_sp;
134160c9d81SGreg Clayton }
135160c9d81SGreg Clayton 
136b9c1b51eSKate Stone ThreadSP ThreadList::RemoveThreadByID(lldb::tid_t tid, bool can_update) {
137bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
138c2c423eaSHan Ming Ong 
139c2c423eaSHan Ming Ong   if (can_update)
140c2c423eaSHan Ming Ong     m_process->UpdateThreadListIfNeeded();
141c2c423eaSHan Ming Ong 
142c2c423eaSHan Ming Ong   ThreadSP thread_sp;
143c2c423eaSHan Ming Ong   uint32_t idx = 0;
144c2c423eaSHan Ming Ong   const uint32_t num_threads = m_threads.size();
145b9c1b51eSKate Stone   for (idx = 0; idx < num_threads; ++idx) {
146b9c1b51eSKate Stone     if (m_threads[idx]->GetID() == tid) {
147c2c423eaSHan Ming Ong       thread_sp = m_threads[idx];
148c2c423eaSHan Ming Ong       m_threads.erase(m_threads.begin() + idx);
149c2c423eaSHan Ming Ong       break;
150c2c423eaSHan Ming Ong     }
151c2c423eaSHan Ming Ong   }
152c2c423eaSHan Ming Ong   return thread_sp;
153c2c423eaSHan Ming Ong }
154c2c423eaSHan Ming Ong 
155b9c1b51eSKate Stone ThreadSP ThreadList::RemoveThreadByProtocolID(lldb::tid_t tid,
156b9c1b51eSKate Stone                                               bool can_update) {
157bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
158160c9d81SGreg Clayton 
159160c9d81SGreg Clayton   if (can_update)
160160c9d81SGreg Clayton     m_process->UpdateThreadListIfNeeded();
161160c9d81SGreg Clayton 
162160c9d81SGreg Clayton   ThreadSP thread_sp;
163160c9d81SGreg Clayton   uint32_t idx = 0;
164160c9d81SGreg Clayton   const uint32_t num_threads = m_threads.size();
165b9c1b51eSKate Stone   for (idx = 0; idx < num_threads; ++idx) {
166b9c1b51eSKate Stone     if (m_threads[idx]->GetProtocolID() == tid) {
167160c9d81SGreg Clayton       thread_sp = m_threads[idx];
168160c9d81SGreg Clayton       m_threads.erase(m_threads.begin() + idx);
169160c9d81SGreg Clayton       break;
170160c9d81SGreg Clayton     }
171160c9d81SGreg Clayton   }
172160c9d81SGreg Clayton   return thread_sp;
173160c9d81SGreg Clayton }
174160c9d81SGreg Clayton 
175b9c1b51eSKate Stone ThreadSP ThreadList::GetThreadSPForThreadPtr(Thread *thread_ptr) {
17630fdc8d8SChris Lattner   ThreadSP thread_sp;
177b9c1b51eSKate Stone   if (thread_ptr) {
178bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetMutex());
17930fdc8d8SChris Lattner 
18030fdc8d8SChris Lattner     uint32_t idx = 0;
18130fdc8d8SChris Lattner     const uint32_t num_threads = m_threads.size();
182b9c1b51eSKate Stone     for (idx = 0; idx < num_threads; ++idx) {
183b9c1b51eSKate Stone       if (m_threads[idx].get() == thread_ptr) {
18430fdc8d8SChris Lattner         thread_sp = m_threads[idx];
18530fdc8d8SChris Lattner         break;
18630fdc8d8SChris Lattner       }
18730fdc8d8SChris Lattner     }
18830fdc8d8SChris Lattner   }
18930fdc8d8SChris Lattner   return thread_sp;
19030fdc8d8SChris Lattner }
19130fdc8d8SChris Lattner 
1928db3f7edSJonas Devlieghere ThreadSP ThreadList::GetBackingThread(const ThreadSP &real_thread) {
1938db3f7edSJonas Devlieghere   std::lock_guard<std::recursive_mutex> guard(GetMutex());
1948db3f7edSJonas Devlieghere 
1958db3f7edSJonas Devlieghere   ThreadSP thread_sp;
1968db3f7edSJonas Devlieghere   const uint32_t num_threads = m_threads.size();
1978db3f7edSJonas Devlieghere   for (uint32_t idx = 0; idx < num_threads; ++idx) {
1988db3f7edSJonas Devlieghere     if (m_threads[idx]->GetBackingThread() == real_thread) {
1998db3f7edSJonas Devlieghere       thread_sp = m_threads[idx];
2008db3f7edSJonas Devlieghere       break;
2018db3f7edSJonas Devlieghere     }
2028db3f7edSJonas Devlieghere   }
2038db3f7edSJonas Devlieghere   return thread_sp;
2048db3f7edSJonas Devlieghere }
2058db3f7edSJonas Devlieghere 
206b9c1b51eSKate Stone ThreadSP ThreadList::FindThreadByIndexID(uint32_t index_id, bool can_update) {
207bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
20830fdc8d8SChris Lattner 
20930fdc8d8SChris Lattner   if (can_update)
21030fdc8d8SChris Lattner     m_process->UpdateThreadListIfNeeded();
21130fdc8d8SChris Lattner 
21230fdc8d8SChris Lattner   ThreadSP thread_sp;
21330fdc8d8SChris Lattner   const uint32_t num_threads = m_threads.size();
214b9c1b51eSKate Stone   for (uint32_t idx = 0; idx < num_threads; ++idx) {
215b9c1b51eSKate Stone     if (m_threads[idx]->GetIndexID() == index_id) {
21630fdc8d8SChris Lattner       thread_sp = m_threads[idx];
21730fdc8d8SChris Lattner       break;
21830fdc8d8SChris Lattner     }
21930fdc8d8SChris Lattner   }
22030fdc8d8SChris Lattner   return thread_sp;
22130fdc8d8SChris Lattner }
22230fdc8d8SChris Lattner 
223b9c1b51eSKate Stone bool ThreadList::ShouldStop(Event *event_ptr) {
22430fdc8d8SChris Lattner   // Running events should never stop, obviously...
22530fdc8d8SChris Lattner 
2265160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
22730fdc8d8SChris Lattner 
22805097246SAdrian Prantl   // The ShouldStop method of the threads can do a whole lot of work, figuring
22905097246SAdrian Prantl   // out whether the thread plan conditions are met.  So we don't want to keep
23005097246SAdrian Prantl   // the ThreadList locked the whole time we are doing this.
231b42f3af3SJim Ingham   // FIXME: It is possible that running code could cause new threads
23205097246SAdrian Prantl   // to be created.  If that happens, we will miss asking them whether they
23305097246SAdrian Prantl   // should stop.  This is not a big deal since we haven't had a chance to hang
23405097246SAdrian Prantl   // any interesting operations on those threads yet.
23530fdc8d8SChris Lattner 
236b42f3af3SJim Ingham   collection threads_copy;
237b42f3af3SJim Ingham   {
238b42f3af3SJim Ingham     // Scope for locker
239bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetMutex());
240b42f3af3SJim Ingham 
241b42f3af3SJim Ingham     m_process->UpdateThreadListIfNeeded();
242b9c1b51eSKate Stone     for (lldb::ThreadSP thread_sp : m_threads) {
243b9c1b51eSKate Stone       // This is an optimization...  If we didn't let a thread run in between
24405097246SAdrian Prantl       // the previous stop and this one, we shouldn't have to consult it for
24505097246SAdrian Prantl       // ShouldStop.  So just leave it off the list we are going to inspect. On
24605097246SAdrian Prantl       // Linux, if a thread-specific conditional breakpoint was hit, it won't
24705097246SAdrian Prantl       // necessarily be the thread that hit the breakpoint itself that
24805097246SAdrian Prantl       // evaluates the conditional expression, so the thread that hit the
24905097246SAdrian Prantl       // breakpoint could still be asked to stop, even though it hasn't been
25005097246SAdrian Prantl       // allowed to run since the previous stop.
251b9c1b51eSKate Stone       if (thread_sp->GetTemporaryResumeState() != eStateSuspended ||
252b9c1b51eSKate Stone           thread_sp->IsStillAtLastBreakpointHit())
253569aaf9eSJim Ingham         threads_copy.push_back(thread_sp);
254569aaf9eSJim Ingham     }
255569aaf9eSJim Ingham 
256b9c1b51eSKate Stone     // It is possible the threads we were allowing to run all exited and then
25705097246SAdrian Prantl     // maybe the user interrupted or something, then fall back on looking at
25805097246SAdrian Prantl     // all threads:
259569aaf9eSJim Ingham 
260569aaf9eSJim Ingham     if (threads_copy.size() == 0)
261b42f3af3SJim Ingham       threads_copy = m_threads;
262b42f3af3SJim Ingham   }
263b42f3af3SJim Ingham 
264b42f3af3SJim Ingham   collection::iterator pos, end = threads_copy.end();
26530fdc8d8SChris Lattner 
266b9c1b51eSKate Stone   if (log) {
26710c4b249SJim Ingham     log->PutCString("");
268b9c1b51eSKate Stone     log->Printf("ThreadList::%s: %" PRIu64 " threads, %" PRIu64
269b9c1b51eSKate Stone                 " unsuspended threads",
270b9c1b51eSKate Stone                 __FUNCTION__, (uint64_t)m_threads.size(),
271569aaf9eSJim Ingham                 (uint64_t)threads_copy.size());
27210c4b249SJim Ingham   }
2732cad65a5SGreg Clayton 
274a0079044SJim Ingham   bool did_anybody_stop_for_a_reason = false;
27535878c47SJim Ingham 
276b9c1b51eSKate Stone   // If the event is an Interrupt event, then we're going to stop no matter
277b9c1b51eSKate Stone   // what.  Otherwise, presume we won't stop.
278a0079044SJim Ingham   bool should_stop = false;
279b9c1b51eSKate Stone   if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) {
28035878c47SJim Ingham     if (log)
281b9c1b51eSKate Stone       log->Printf(
282b9c1b51eSKate Stone           "ThreadList::%s handling interrupt event, should stop set to true",
283b9c1b51eSKate Stone           __FUNCTION__);
28435878c47SJim Ingham 
28535878c47SJim Ingham     should_stop = true;
28635878c47SJim Ingham   }
2877bc3465fSJim Ingham 
288b9c1b51eSKate Stone   // Now we run through all the threads and get their stop info's.  We want to
28905097246SAdrian Prantl   // make sure to do this first before we start running the ShouldStop, because
29005097246SAdrian Prantl   // one thread's ShouldStop could destroy information (like deleting a thread
29105097246SAdrian Prantl   // specific breakpoint another thread had stopped at) which could lead us to
29205097246SAdrian Prantl   // compute the StopInfo incorrectly. We don't need to use it here, we just
29305097246SAdrian Prantl   // want to make sure it gets computed.
2947bc3465fSJim Ingham 
295b9c1b51eSKate Stone   for (pos = threads_copy.begin(); pos != end; ++pos) {
2967bc3465fSJim Ingham     ThreadSP thread_sp(*pos);
2977bc3465fSJim Ingham     thread_sp->GetStopInfo();
2987bc3465fSJim Ingham   }
299a0079044SJim Ingham 
300b9c1b51eSKate Stone   for (pos = threads_copy.begin(); pos != end; ++pos) {
30130fdc8d8SChris Lattner     ThreadSP thread_sp(*pos);
3022cad65a5SGreg Clayton 
303b9c1b51eSKate Stone     // We should never get a stop for which no thread had a stop reason, but
30405097246SAdrian Prantl     // sometimes we do see this - for instance when we first connect to a
30505097246SAdrian Prantl     // remote stub.  In that case we should stop, since we can't figure out the
30605097246SAdrian Prantl     // right thing to do and stopping gives the user control over what to do in
30705097246SAdrian Prantl     // this instance.
30839fdae7fSJim Ingham     //
309b9c1b51eSKate Stone     // Note, this causes a problem when you have a thread specific breakpoint,
31005097246SAdrian Prantl     // and a bunch of threads hit the breakpoint, but not the thread which we
31105097246SAdrian Prantl     // are waiting for.  All the threads that are not "supposed" to hit the
31205097246SAdrian Prantl     // breakpoint are marked as having no stop reason, which is right, they
31305097246SAdrian Prantl     // should not show a stop reason.  But that triggers this code and causes
31405097246SAdrian Prantl     // us to stop seemingly for no reason.
31539fdae7fSJim Ingham     //
316b9c1b51eSKate Stone     // Since the only way we ever saw this error was on first attach, I'm only
31705097246SAdrian Prantl     // going to trigger set did_anybody_stop_for_a_reason to true unless this
31805097246SAdrian Prantl     // is the first stop.
31939fdae7fSJim Ingham     //
320b9c1b51eSKate Stone     // If this becomes a problem, we'll have to have another StopReason like
32105097246SAdrian Prantl     // "StopInfoHidden" which will look invalid everywhere but at this check.
32239fdae7fSJim Ingham 
32321afbe03SEd Maste     if (thread_sp->GetProcess()->GetStopID() > 1)
32439fdae7fSJim Ingham       did_anybody_stop_for_a_reason = true;
32539fdae7fSJim Ingham     else
326a0079044SJim Ingham       did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
327a0079044SJim Ingham 
32810c4b249SJim Ingham     const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
3292cad65a5SGreg Clayton     if (thread_should_stop)
3302cad65a5SGreg Clayton       should_stop |= true;
3312cad65a5SGreg Clayton   }
3322cad65a5SGreg Clayton 
333b9c1b51eSKate Stone   if (!should_stop && !did_anybody_stop_for_a_reason) {
334a0079044SJim Ingham     should_stop = true;
335a0079044SJim Ingham     if (log)
336b9c1b51eSKate Stone       log->Printf("ThreadList::%s we stopped but no threads had a stop reason, "
337b9c1b51eSKate Stone                   "overriding should_stop and stopping.",
338b9c1b51eSKate Stone                   __FUNCTION__);
339a0079044SJim Ingham   }
340a0079044SJim Ingham 
3412cad65a5SGreg Clayton   if (log)
342b9c1b51eSKate Stone     log->Printf("ThreadList::%s overall should_stop = %i", __FUNCTION__,
343b9c1b51eSKate Stone                 should_stop);
3442cad65a5SGreg Clayton 
345b9c1b51eSKate Stone   if (should_stop) {
346b9c1b51eSKate Stone     for (pos = threads_copy.begin(); pos != end; ++pos) {
34730fdc8d8SChris Lattner       ThreadSP thread_sp(*pos);
34830fdc8d8SChris Lattner       thread_sp->WillStop();
34930fdc8d8SChris Lattner     }
35030fdc8d8SChris Lattner   }
35130fdc8d8SChris Lattner 
35230fdc8d8SChris Lattner   return should_stop;
35330fdc8d8SChris Lattner }
35430fdc8d8SChris Lattner 
355b9c1b51eSKate Stone Vote ThreadList::ShouldReportStop(Event *event_ptr) {
356bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
3572cad65a5SGreg Clayton 
35830fdc8d8SChris Lattner   Vote result = eVoteNoOpinion;
35930fdc8d8SChris Lattner   m_process->UpdateThreadListIfNeeded();
36030fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
36130fdc8d8SChris Lattner 
3625160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3632cad65a5SGreg Clayton 
3642cad65a5SGreg Clayton   if (log)
365b9c1b51eSKate Stone     log->Printf("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:
431abcbc8acSGreg Clayton         if (log)
432b9c1b51eSKate Stone           log->Printf("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())
465b9c1b51eSKate Stone     log->Printf("Turning off notification of new threads while single stepping "
466b9c1b51eSKate Stone                 "a thread.");
4671c823b43SJim Ingham 
46830fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
46930fdc8d8SChris Lattner   for (pos = m_threads.begin(); pos != end; ++pos)
47030fdc8d8SChris Lattner     (*pos)->RefreshStateAfterStop();
47130fdc8d8SChris Lattner }
47230fdc8d8SChris Lattner 
473b9c1b51eSKate Stone void ThreadList::DiscardThreadPlans() {
47405097246SAdrian Prantl   // You don't need to update the thread list here, because only threads that
47505097246SAdrian Prantl   // you currently know about have any thread plans.
476bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
47730fdc8d8SChris Lattner 
47830fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
47930fdc8d8SChris Lattner   for (pos = m_threads.begin(); pos != end; ++pos)
48030fdc8d8SChris Lattner     (*pos)->DiscardThreadPlans(true);
48130fdc8d8SChris Lattner }
48230fdc8d8SChris Lattner 
483b9c1b51eSKate Stone bool ThreadList::WillResume() {
48405097246SAdrian Prantl   // Run through the threads and perform their momentary actions. But we only
48505097246SAdrian Prantl   // do this for threads that are running, user suspended threads stay where
48605097246SAdrian Prantl   // they are.
48730fdc8d8SChris Lattner 
488bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
48930fdc8d8SChris Lattner   m_process->UpdateThreadListIfNeeded();
49030fdc8d8SChris Lattner 
49130fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
49230fdc8d8SChris Lattner 
493a3241c1bSJim Ingham   // See if any thread wants to run stopping others.  If it does, then we won't
49405097246SAdrian Prantl   // setup the other threads for resume, since they aren't going to get a
49505097246SAdrian Prantl   // chance to run.  This is necessary because the SetupForResume might add
49605097246SAdrian Prantl   // "StopOthers" plans which would then get to be part of the who-gets-to-run
49705097246SAdrian Prantl   // negotiation, but they're coming in after the fact, and the threads that
49805097246SAdrian Prantl   // are already set up should take priority.
499a3241c1bSJim Ingham 
500a3241c1bSJim Ingham   bool wants_solo_run = false;
50130fdc8d8SChris Lattner 
502b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
503b9c1b51eSKate Stone     lldbassert((*pos)->GetCurrentPlan() &&
504b9c1b51eSKate Stone                "thread should not have null thread plan");
5058f186f85SZachary Turner     if ((*pos)->GetResumeState() != eStateSuspended &&
506b9c1b51eSKate Stone         (*pos)->GetCurrentPlan()->StopOthers()) {
507b9c1b51eSKate Stone       if ((*pos)->IsOperatingSystemPluginThread() &&
508b9c1b51eSKate Stone           !(*pos)->GetBackingThread())
5096e0ff1a3SGreg Clayton         continue;
510a3241c1bSJim Ingham       wants_solo_run = true;
511a3241c1bSJim Ingham       break;
512a3241c1bSJim Ingham     }
513a3241c1bSJim Ingham   }
514a3241c1bSJim Ingham 
515b9c1b51eSKate Stone   if (wants_solo_run) {
5165160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
51710c4b249SJim Ingham     if (log && log->GetVerbose())
518b9c1b51eSKate Stone       log->Printf("Turning on notification of new threads while single "
519b9c1b51eSKate Stone                   "stepping a thread.");
5201c823b43SJim Ingham     m_process->StartNoticingNewThreads();
521b9c1b51eSKate Stone   } else {
5225160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
52310c4b249SJim Ingham     if (log && log->GetVerbose())
524b9c1b51eSKate Stone       log->Printf("Turning off notification of new threads while single "
525b9c1b51eSKate Stone                   "stepping a thread.");
5261c823b43SJim Ingham     m_process->StopNoticingNewThreads();
5271c823b43SJim Ingham   }
528a3241c1bSJim Ingham 
529b9c1b51eSKate Stone   // Give all the threads that are likely to run a last chance to set up their
53005097246SAdrian Prantl   // state before we negotiate who is actually going to get a chance to run...
531b9c1b51eSKate Stone   // Don't set to resume suspended threads, and if any thread wanted to stop
53205097246SAdrian Prantl   // others, only call setup on the threads that request StopOthers...
533a3241c1bSJim Ingham 
534b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
535b9c1b51eSKate Stone     if ((*pos)->GetResumeState() != eStateSuspended &&
536b9c1b51eSKate Stone         (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers())) {
537b9c1b51eSKate Stone       if ((*pos)->IsOperatingSystemPluginThread() &&
538b9c1b51eSKate Stone           !(*pos)->GetBackingThread())
5396e0ff1a3SGreg Clayton         continue;
54030fdc8d8SChris Lattner       (*pos)->SetupForResume();
541a3241c1bSJim Ingham     }
542a3241c1bSJim Ingham   }
54330fdc8d8SChris Lattner 
54430fdc8d8SChris Lattner   // Now go through the threads and see if any thread wants to run just itself.
54530fdc8d8SChris Lattner   // if so then pick one and run it.
546a3241c1bSJim Ingham 
54730fdc8d8SChris Lattner   ThreadList run_me_only_list(m_process);
54830fdc8d8SChris Lattner 
54930fdc8d8SChris Lattner   run_me_only_list.SetStopID(m_process->GetStopID());
55030fdc8d8SChris Lattner 
55130fdc8d8SChris Lattner   bool run_only_current_thread = false;
55230fdc8d8SChris Lattner 
553b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
55430fdc8d8SChris Lattner     ThreadSP thread_sp(*pos);
555b15bfc75SJim Ingham     if (thread_sp->GetResumeState() != eStateSuspended &&
556b9c1b51eSKate Stone         thread_sp->GetCurrentPlan()->StopOthers()) {
557b9c1b51eSKate Stone       if ((*pos)->IsOperatingSystemPluginThread() &&
558b9c1b51eSKate Stone           !(*pos)->GetBackingThread())
5596e0ff1a3SGreg Clayton         continue;
5606e0ff1a3SGreg Clayton 
56130fdc8d8SChris Lattner       // You can't say "stop others" and also want yourself to be suspended.
56230fdc8d8SChris Lattner       assert(thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
56330fdc8d8SChris Lattner 
564b9c1b51eSKate Stone       if (thread_sp == GetSelectedThread()) {
565b9c1b51eSKate Stone         // If the currently selected thread wants to run on its own, always let
566b9c1b51eSKate Stone         // it.
56730fdc8d8SChris Lattner         run_only_current_thread = true;
56830fdc8d8SChris Lattner         run_me_only_list.Clear();
56930fdc8d8SChris Lattner         run_me_only_list.AddThread(thread_sp);
57030fdc8d8SChris Lattner         break;
57130fdc8d8SChris Lattner       }
57230fdc8d8SChris Lattner 
57330fdc8d8SChris Lattner       run_me_only_list.AddThread(thread_sp);
57430fdc8d8SChris Lattner     }
57530fdc8d8SChris Lattner   }
57630fdc8d8SChris Lattner 
577513c6bb8SJim Ingham   bool need_to_resume = true;
578513c6bb8SJim Ingham 
579b9c1b51eSKate Stone   if (run_me_only_list.GetSize(false) == 0) {
58030fdc8d8SChris Lattner     // Everybody runs as they wish:
581b9c1b51eSKate Stone     for (pos = m_threads.begin(); pos != end; ++pos) {
58230fdc8d8SChris Lattner       ThreadSP thread_sp(*pos);
583cb5d5a57SJim Ingham       StateType run_state;
584cb5d5a57SJim Ingham       if (thread_sp->GetResumeState() != eStateSuspended)
585cb5d5a57SJim Ingham         run_state = thread_sp->GetCurrentPlan()->RunState();
586cb5d5a57SJim Ingham       else
587cb5d5a57SJim Ingham         run_state = eStateSuspended;
588160c9d81SGreg Clayton       if (!thread_sp->ShouldResume(run_state))
589513c6bb8SJim Ingham         need_to_resume = false;
59030fdc8d8SChris Lattner     }
591b9c1b51eSKate Stone   } else {
59230fdc8d8SChris Lattner     ThreadSP thread_to_run;
59330fdc8d8SChris Lattner 
594b9c1b51eSKate Stone     if (run_only_current_thread) {
5952976d00aSJim Ingham       thread_to_run = GetSelectedThread();
596b9c1b51eSKate Stone     } else if (run_me_only_list.GetSize(false) == 1) {
59730fdc8d8SChris Lattner       thread_to_run = run_me_only_list.GetThreadAtIndex(0);
598b9c1b51eSKate Stone     } else {
599b9c1b51eSKate Stone       int random_thread =
600b9c1b51eSKate Stone           (int)((run_me_only_list.GetSize(false) * (double)rand()) /
601b9c1b51eSKate Stone                 (RAND_MAX + 1.0));
60230fdc8d8SChris Lattner       thread_to_run = run_me_only_list.GetThreadAtIndex(random_thread);
60330fdc8d8SChris Lattner     }
60430fdc8d8SChris Lattner 
605b9c1b51eSKate Stone     for (pos = m_threads.begin(); pos != end; ++pos) {
60630fdc8d8SChris Lattner       ThreadSP thread_sp(*pos);
607b9c1b51eSKate Stone       if (thread_sp == thread_to_run) {
608160c9d81SGreg Clayton         if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
609513c6bb8SJim Ingham           need_to_resume = false;
610b9c1b51eSKate Stone       } else
611160c9d81SGreg Clayton         thread_sp->ShouldResume(eStateSuspended);
61230fdc8d8SChris Lattner     }
61330fdc8d8SChris Lattner   }
61430fdc8d8SChris Lattner 
615513c6bb8SJim Ingham   return need_to_resume;
61630fdc8d8SChris Lattner }
61730fdc8d8SChris Lattner 
618b9c1b51eSKate Stone void ThreadList::DidResume() {
619bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
62030fdc8d8SChris Lattner   collection::iterator pos, end = m_threads.end();
621b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
62230fdc8d8SChris Lattner     // Don't clear out threads that aren't going to get a chance to run, rather
62330fdc8d8SChris Lattner     // leave their state for the next time around.
62430fdc8d8SChris Lattner     ThreadSP thread_sp(*pos);
62530fdc8d8SChris Lattner     if (thread_sp->GetResumeState() != eStateSuspended)
62630fdc8d8SChris Lattner       thread_sp->DidResume();
62730fdc8d8SChris Lattner   }
62830fdc8d8SChris Lattner }
62930fdc8d8SChris Lattner 
630b9c1b51eSKate Stone void ThreadList::DidStop() {
631bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
63229d65744SAndrew Kaylor   collection::iterator pos, end = m_threads.end();
633b9c1b51eSKate Stone   for (pos = m_threads.begin(); pos != end; ++pos) {
63405097246SAdrian Prantl     // Notify threads that the process just stopped. Note, this currently
63505097246SAdrian Prantl     // assumes that all threads in the list stop when the process stops.  In
63605097246SAdrian Prantl     // the future we will want to support a debugging model where some threads
63705097246SAdrian Prantl     // continue to run while others are stopped.  We either need to handle that
63805097246SAdrian Prantl     // somehow here or create a special thread list containing only threads
63905097246SAdrian Prantl     // which will stop in the code that calls this method (currently
64029d65744SAndrew Kaylor     // Process::SetPrivateState).
64129d65744SAndrew Kaylor     ThreadSP thread_sp(*pos);
64229d65744SAndrew Kaylor     if (StateIsRunningState(thread_sp->GetState()))
64329d65744SAndrew Kaylor       thread_sp->DidStop();
64429d65744SAndrew Kaylor   }
64529d65744SAndrew Kaylor }
64629d65744SAndrew Kaylor 
647b9c1b51eSKate Stone ThreadSP ThreadList::GetSelectedThread() {
648bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
649943ddb73SJohnny Chen   ThreadSP thread_sp = FindThreadByID(m_selected_tid);
650b9c1b51eSKate Stone   if (!thread_sp.get()) {
651354b9a65SJason Molenda     if (m_threads.size() == 0)
652354b9a65SJason Molenda       return thread_sp;
653943ddb73SJohnny Chen     m_selected_tid = m_threads[0]->GetID();
654943ddb73SJohnny Chen     thread_sp = m_threads[0];
655943ddb73SJohnny Chen   }
656943ddb73SJohnny Chen   return thread_sp;
65730fdc8d8SChris Lattner }
65830fdc8d8SChris Lattner 
659b9c1b51eSKate Stone bool ThreadList::SetSelectedThreadByID(lldb::tid_t tid, bool notify) {
660bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
661b7f6b2faSJim Ingham   ThreadSP selected_thread_sp(FindThreadByID(tid));
662b9c1b51eSKate Stone   if (selected_thread_sp) {
6632976d00aSJim Ingham     m_selected_tid = tid;
664b7f6b2faSJim Ingham     selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
665b9c1b51eSKate Stone   } else
6662976d00aSJim Ingham     m_selected_tid = LLDB_INVALID_THREAD_ID;
66730fdc8d8SChris Lattner 
668c3faa195SJim Ingham   if (notify)
669c3faa195SJim Ingham     NotifySelectedThreadChanged(m_selected_tid);
670c3faa195SJim Ingham 
6712976d00aSJim Ingham   return m_selected_tid != LLDB_INVALID_THREAD_ID;
67230fdc8d8SChris Lattner }
67330fdc8d8SChris Lattner 
674b9c1b51eSKate Stone bool ThreadList::SetSelectedThreadByIndexID(uint32_t index_id, bool notify) {
675bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
676b7f6b2faSJim Ingham   ThreadSP selected_thread_sp(FindThreadByIndexID(index_id));
677b9c1b51eSKate Stone   if (selected_thread_sp.get()) {
678b7f6b2faSJim Ingham     m_selected_tid = selected_thread_sp->GetID();
679b7f6b2faSJim Ingham     selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
680b9c1b51eSKate Stone   } else
6812976d00aSJim Ingham     m_selected_tid = LLDB_INVALID_THREAD_ID;
68230fdc8d8SChris Lattner 
683c3faa195SJim Ingham   if (notify)
684c3faa195SJim Ingham     NotifySelectedThreadChanged(m_selected_tid);
685c3faa195SJim Ingham 
6862976d00aSJim Ingham   return m_selected_tid != LLDB_INVALID_THREAD_ID;
68730fdc8d8SChris Lattner }
68830fdc8d8SChris Lattner 
689b9c1b51eSKate Stone void ThreadList::NotifySelectedThreadChanged(lldb::tid_t tid) {
690c3faa195SJim Ingham   ThreadSP selected_thread_sp(FindThreadByID(tid));
691b9c1b51eSKate Stone   if (selected_thread_sp->EventTypeHasListeners(
692b9c1b51eSKate Stone           Thread::eBroadcastBitThreadSelected))
693b9c1b51eSKate Stone     selected_thread_sp->BroadcastEvent(
694b9c1b51eSKate Stone         Thread::eBroadcastBitThreadSelected,
695c3faa195SJim Ingham         new Thread::ThreadEventData(selected_thread_sp));
696c3faa195SJim Ingham }
697c3faa195SJim Ingham 
698b9c1b51eSKate Stone void ThreadList::Update(ThreadList &rhs) {
699b9c1b51eSKate Stone   if (this != &rhs) {
70005097246SAdrian Prantl     // Lock both mutexes to make sure neither side changes anyone on us while
70105097246SAdrian Prantl     // the assignment occurs
702bb19a13cSSaleem Abdulrasool     std::lock_guard<std::recursive_mutex> guard(GetMutex());
703bb19a13cSSaleem Abdulrasool 
70456d9a1b3SGreg Clayton     m_process = rhs.m_process;
70556d9a1b3SGreg Clayton     m_stop_id = rhs.m_stop_id;
70656d9a1b3SGreg Clayton     m_threads.swap(rhs.m_threads);
70756d9a1b3SGreg Clayton     m_selected_tid = rhs.m_selected_tid;
708e1cd1be6SGreg Clayton 
70905097246SAdrian Prantl     // Now we look for threads that we are done with and make sure to clear
71005097246SAdrian Prantl     // them up as much as possible so anyone with a shared pointer will still
71105097246SAdrian Prantl     // have a reference, but the thread won't be of much use. Using
71205097246SAdrian Prantl     // std::weak_ptr for all backward references (such as a thread to a
71305097246SAdrian Prantl     // process) will eventually solve this issue for us, but for now, we need
71405097246SAdrian Prantl     // to work around the issue
715e1cd1be6SGreg Clayton     collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
716b9c1b51eSKate Stone     for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos) {
717e1cd1be6SGreg Clayton       const lldb::tid_t tid = (*rhs_pos)->GetID();
718e1cd1be6SGreg Clayton       bool thread_is_alive = false;
719e1cd1be6SGreg Clayton       const uint32_t num_threads = m_threads.size();
720b9c1b51eSKate Stone       for (uint32_t idx = 0; idx < num_threads; ++idx) {
72165d4d5c3SRyan Brown         ThreadSP backing_thread = m_threads[idx]->GetBackingThread();
722b9c1b51eSKate Stone         if (m_threads[idx]->GetID() == tid ||
723b9c1b51eSKate Stone             (backing_thread && backing_thread->GetID() == tid)) {
724e1cd1be6SGreg Clayton           thread_is_alive = true;
725e1cd1be6SGreg Clayton           break;
726e1cd1be6SGreg Clayton         }
727e1cd1be6SGreg Clayton       }
728e1cd1be6SGreg Clayton       if (!thread_is_alive)
729e1cd1be6SGreg Clayton         (*rhs_pos)->DestroyThread();
730e1cd1be6SGreg Clayton     }
73156d9a1b3SGreg Clayton   }
73256d9a1b3SGreg Clayton }
73356d9a1b3SGreg Clayton 
734b9c1b51eSKate Stone void ThreadList::Flush() {
735bb19a13cSSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(GetMutex());
736fa559e5cSGreg Clayton   collection::iterator pos, end = m_threads.end();
737fa559e5cSGreg Clayton   for (pos = m_threads.begin(); pos != end; ++pos)
738fa559e5cSGreg Clayton     (*pos)->Flush();
739fa559e5cSGreg Clayton }
74056d9a1b3SGreg Clayton 
741b5cd6e7bSGreg Clayton std::recursive_mutex &ThreadList::GetMutex() const {
742ba4e61d3SAndrew Kaylor   return m_process->m_thread_mutex;
743ba4e61d3SAndrew Kaylor }
744ba4e61d3SAndrew Kaylor 
745b9c1b51eSKate Stone ThreadList::ExpressionExecutionThreadPusher::ExpressionExecutionThreadPusher(
746b9c1b51eSKate Stone     lldb::ThreadSP thread_sp)
747b9c1b51eSKate Stone     : m_thread_list(nullptr), m_tid(LLDB_INVALID_THREAD_ID) {
748b9c1b51eSKate Stone   if (thread_sp) {
7498d94ba0fSJim Ingham     m_tid = thread_sp->GetID();
7508d94ba0fSJim Ingham     m_thread_list = &thread_sp->GetProcess()->GetThreadList();
7518d94ba0fSJim Ingham     m_thread_list->PushExpressionExecutionThread(m_tid);
7528d94ba0fSJim Ingham   }
7538d94ba0fSJim Ingham }
754