1 //===-- ThreadList.cpp ----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
13 #include "lldb/Target/Process.h"
14 #include "lldb/Target/RegisterContext.h"
15 #include "lldb/Target/Thread.h"
16 #include "lldb/Target/ThreadList.h"
17 #include "lldb/Target/ThreadPlan.h"
18 #include "lldb/Utility/LLDBAssert.h"
19 #include "lldb/Utility/LLDBLog.h"
20 #include "lldb/Utility/Log.h"
21 #include "lldb/Utility/State.h"
24 using namespace lldb_private
;
26 ThreadList::ThreadList(Process
&process
)
27 : ThreadCollection(), m_process(process
), m_stop_id(0),
28 m_selected_tid(LLDB_INVALID_THREAD_ID
) {}
30 ThreadList::ThreadList(const ThreadList
&rhs
)
31 : ThreadCollection(), m_process(rhs
.m_process
), m_stop_id(rhs
.m_stop_id
),
33 // Use the assignment operator since it uses the mutex
37 const ThreadList
&ThreadList::operator=(const ThreadList
&rhs
) {
39 // We only allow assignments between thread lists describing the same
40 // process. Same process implies same mutex, which means it's enough to lock
41 // just the current object.
42 assert(&m_process
== &rhs
.m_process
);
43 assert(&GetMutex() == &rhs
.GetMutex());
44 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
46 m_stop_id
= rhs
.m_stop_id
;
47 m_threads
= rhs
.m_threads
;
48 m_selected_tid
= rhs
.m_selected_tid
;
53 ThreadList::~ThreadList() {
54 // Clear the thread list. Clear will take the mutex lock which will ensure
55 // that if anyone is using the list they won't get it removed while using it.
59 lldb::ThreadSP
ThreadList::GetExpressionExecutionThread() {
60 if (m_expression_tid_stack
.empty())
61 return GetSelectedThread();
62 ThreadSP expr_thread_sp
= FindThreadByID(m_expression_tid_stack
.back());
64 return expr_thread_sp
;
66 return GetSelectedThread();
69 void ThreadList::PushExpressionExecutionThread(lldb::tid_t tid
) {
70 m_expression_tid_stack
.push_back(tid
);
73 void ThreadList::PopExpressionExecutionThread(lldb::tid_t tid
) {
74 assert(m_expression_tid_stack
.back() == tid
);
75 m_expression_tid_stack
.pop_back();
78 uint32_t ThreadList::GetStopID() const { return m_stop_id
; }
80 void ThreadList::SetStopID(uint32_t stop_id
) { m_stop_id
= stop_id
; }
82 uint32_t ThreadList::GetSize(bool can_update
) {
83 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
86 m_process
.UpdateThreadListIfNeeded();
87 return m_threads
.size();
90 ThreadSP
ThreadList::GetThreadAtIndex(uint32_t idx
, bool can_update
) {
91 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
94 m_process
.UpdateThreadListIfNeeded();
97 if (idx
< m_threads
.size())
98 thread_sp
= m_threads
[idx
];
102 ThreadSP
ThreadList::FindThreadByID(lldb::tid_t tid
, bool can_update
) {
103 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
106 m_process
.UpdateThreadListIfNeeded();
110 const uint32_t num_threads
= m_threads
.size();
111 for (idx
= 0; idx
< num_threads
; ++idx
) {
112 if (m_threads
[idx
]->GetID() == tid
) {
113 thread_sp
= m_threads
[idx
];
120 ThreadSP
ThreadList::FindThreadByProtocolID(lldb::tid_t tid
, bool can_update
) {
121 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
124 m_process
.UpdateThreadListIfNeeded();
128 const uint32_t num_threads
= m_threads
.size();
129 for (idx
= 0; idx
< num_threads
; ++idx
) {
130 if (m_threads
[idx
]->GetProtocolID() == tid
) {
131 thread_sp
= m_threads
[idx
];
138 ThreadSP
ThreadList::RemoveThreadByID(lldb::tid_t tid
, bool can_update
) {
139 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
142 m_process
.UpdateThreadListIfNeeded();
146 const uint32_t num_threads
= m_threads
.size();
147 for (idx
= 0; idx
< num_threads
; ++idx
) {
148 if (m_threads
[idx
]->GetID() == tid
) {
149 thread_sp
= m_threads
[idx
];
150 m_threads
.erase(m_threads
.begin() + idx
);
157 ThreadSP
ThreadList::RemoveThreadByProtocolID(lldb::tid_t tid
,
159 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
162 m_process
.UpdateThreadListIfNeeded();
166 const uint32_t num_threads
= m_threads
.size();
167 for (idx
= 0; idx
< num_threads
; ++idx
) {
168 if (m_threads
[idx
]->GetProtocolID() == tid
) {
169 thread_sp
= m_threads
[idx
];
170 m_threads
.erase(m_threads
.begin() + idx
);
177 ThreadSP
ThreadList::GetThreadSPForThreadPtr(Thread
*thread_ptr
) {
180 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
183 const uint32_t num_threads
= m_threads
.size();
184 for (idx
= 0; idx
< num_threads
; ++idx
) {
185 if (m_threads
[idx
].get() == thread_ptr
) {
186 thread_sp
= m_threads
[idx
];
194 ThreadSP
ThreadList::GetBackingThread(const ThreadSP
&real_thread
) {
195 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
198 const uint32_t num_threads
= m_threads
.size();
199 for (uint32_t idx
= 0; idx
< num_threads
; ++idx
) {
200 if (m_threads
[idx
]->GetBackingThread() == real_thread
) {
201 thread_sp
= m_threads
[idx
];
208 ThreadSP
ThreadList::FindThreadByIndexID(uint32_t index_id
, bool can_update
) {
209 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
212 m_process
.UpdateThreadListIfNeeded();
215 const uint32_t num_threads
= m_threads
.size();
216 for (uint32_t idx
= 0; idx
< num_threads
; ++idx
) {
217 if (m_threads
[idx
]->GetIndexID() == index_id
) {
218 thread_sp
= m_threads
[idx
];
225 bool ThreadList::ShouldStop(Event
*event_ptr
) {
226 // Running events should never stop, obviously...
228 Log
*log
= GetLog(LLDBLog::Step
);
230 // The ShouldStop method of the threads can do a whole lot of work, figuring
231 // out whether the thread plan conditions are met. So we don't want to keep
232 // the ThreadList locked the whole time we are doing this.
233 // FIXME: It is possible that running code could cause new threads
234 // to be created. If that happens, we will miss asking them whether they
235 // should stop. This is not a big deal since we haven't had a chance to hang
236 // any interesting operations on those threads yet.
238 collection threads_copy
;
241 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
243 m_process
.UpdateThreadListIfNeeded();
244 for (lldb::ThreadSP thread_sp
: m_threads
) {
245 // This is an optimization... If we didn't let a thread run in between
246 // the previous stop and this one, we shouldn't have to consult it for
247 // ShouldStop. So just leave it off the list we are going to inspect.
248 // If the thread didn't run but had work to do before declaring a public
249 // stop, then also include it.
250 // On Linux, if a thread-specific conditional breakpoint was hit, it won't
251 // necessarily be the thread that hit the breakpoint itself that
252 // evaluates the conditional expression, so the thread that hit the
253 // breakpoint could still be asked to stop, even though it hasn't been
254 // allowed to run since the previous stop.
255 if (thread_sp
->GetTemporaryResumeState() != eStateSuspended
||
256 thread_sp
->IsStillAtLastBreakpointHit()
257 || thread_sp
->ShouldRunBeforePublicStop())
258 threads_copy
.push_back(thread_sp
);
261 // It is possible the threads we were allowing to run all exited and then
262 // maybe the user interrupted or something, then fall back on looking at
265 if (threads_copy
.size() == 0)
266 threads_copy
= m_threads
;
269 collection::iterator pos
, end
= threads_copy
.end();
274 "ThreadList::%s: %" PRIu64
" threads, %" PRIu64
275 " unsuspended threads",
276 __FUNCTION__
, (uint64_t)m_threads
.size(),
277 (uint64_t)threads_copy
.size());
280 bool did_anybody_stop_for_a_reason
= false;
282 // If the event is an Interrupt event, then we're going to stop no matter
283 // what. Otherwise, presume we won't stop.
284 bool should_stop
= false;
285 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr
)) {
287 log
, "ThreadList::%s handling interrupt event, should stop set to true",
293 // Now we run through all the threads and get their stop info's. We want to
294 // make sure to do this first before we start running the ShouldStop, because
295 // one thread's ShouldStop could destroy information (like deleting a thread
296 // specific breakpoint another thread had stopped at) which could lead us to
297 // compute the StopInfo incorrectly. We don't need to use it here, we just
298 // want to make sure it gets computed.
300 for (pos
= threads_copy
.begin(); pos
!= end
; ++pos
) {
301 ThreadSP
thread_sp(*pos
);
302 thread_sp
->GetStopInfo();
305 // If a thread needs to finish some job that can be done just on this thread
306 // before broadcastion the stop, it will signal that by returning true for
307 // ShouldRunBeforePublicStop. This variable gathers the results from that.
308 bool a_thread_needs_to_run
= false;
309 for (pos
= threads_copy
.begin(); pos
!= end
; ++pos
) {
310 ThreadSP
thread_sp(*pos
);
312 // We should never get a stop for which no thread had a stop reason, but
313 // sometimes we do see this - for instance when we first connect to a
314 // remote stub. In that case we should stop, since we can't figure out the
315 // right thing to do and stopping gives the user control over what to do in
318 // Note, this causes a problem when you have a thread specific breakpoint,
319 // and a bunch of threads hit the breakpoint, but not the thread which we
320 // are waiting for. All the threads that are not "supposed" to hit the
321 // breakpoint are marked as having no stop reason, which is right, they
322 // should not show a stop reason. But that triggers this code and causes
323 // us to stop seemingly for no reason.
325 // Since the only way we ever saw this error was on first attach, I'm only
326 // going to trigger set did_anybody_stop_for_a_reason to true unless this
327 // is the first stop.
329 // If this becomes a problem, we'll have to have another StopReason like
330 // "StopInfoHidden" which will look invalid everywhere but at this check.
332 if (thread_sp
->GetProcess()->GetStopID() > 1)
333 did_anybody_stop_for_a_reason
= true;
335 did_anybody_stop_for_a_reason
|= thread_sp
->ThreadStoppedForAReason();
337 const bool thread_should_stop
= thread_sp
->ShouldStop(event_ptr
);
339 if (thread_should_stop
)
342 bool this_thread_forces_run
= thread_sp
->ShouldRunBeforePublicStop();
343 a_thread_needs_to_run
|= this_thread_forces_run
;
344 if (this_thread_forces_run
)
346 "ThreadList::{0} thread: {1:x}, "
347 "says it needs to run before public stop.",
348 __FUNCTION__
, thread_sp
->GetID());
352 if (a_thread_needs_to_run
) {
354 } else if (!should_stop
&& !did_anybody_stop_for_a_reason
) {
357 "ThreadList::%s we stopped but no threads had a stop reason, "
358 "overriding should_stop and stopping.",
362 LLDB_LOGF(log
, "ThreadList::%s overall should_stop = %i", __FUNCTION__
,
366 for (pos
= threads_copy
.begin(); pos
!= end
; ++pos
) {
367 ThreadSP
thread_sp(*pos
);
368 thread_sp
->WillStop();
375 Vote
ThreadList::ShouldReportStop(Event
*event_ptr
) {
376 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
378 Vote result
= eVoteNoOpinion
;
379 m_process
.UpdateThreadListIfNeeded();
380 collection::iterator pos
, end
= m_threads
.end();
382 Log
*log
= GetLog(LLDBLog::Step
);
384 LLDB_LOGF(log
, "ThreadList::%s %" PRIu64
" threads", __FUNCTION__
,
385 (uint64_t)m_threads
.size());
387 // Run through the threads and ask whether we should report this event. For
388 // stopping, a YES vote wins over everything. A NO vote wins over NO
389 // opinion. The exception is if a thread has work it needs to force before
390 // a public stop, which overrides everyone else's opinion:
391 for (pos
= m_threads
.begin(); pos
!= end
; ++pos
) {
392 ThreadSP
thread_sp(*pos
);
393 if (thread_sp
->ShouldRunBeforePublicStop()) {
394 LLDB_LOG(log
, "Thread {0:x} has private business to complete, overrode "
395 "the should report stop.", thread_sp
->GetID());
400 const Vote vote
= thread_sp
->ShouldReportStop(event_ptr
);
410 if (result
== eVoteNoOpinion
) {
414 "Thread {0:x} voted {1}, but lost out because result was {2}",
415 thread_sp
->GetID(), vote
, result
);
420 LLDB_LOG(log
, "Returning {0}", result
);
424 void ThreadList::SetShouldReportStop(Vote vote
) {
425 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
427 m_process
.UpdateThreadListIfNeeded();
428 collection::iterator pos
, end
= m_threads
.end();
429 for (pos
= m_threads
.begin(); pos
!= end
; ++pos
) {
430 ThreadSP
thread_sp(*pos
);
431 thread_sp
->SetShouldReportStop(vote
);
435 Vote
ThreadList::ShouldReportRun(Event
*event_ptr
) {
437 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
439 Vote result
= eVoteNoOpinion
;
440 m_process
.UpdateThreadListIfNeeded();
441 collection::iterator pos
, end
= m_threads
.end();
443 // Run through the threads and ask whether we should report this event. The
444 // rule is NO vote wins over everything, a YES vote wins over no opinion.
446 Log
*log
= GetLog(LLDBLog::Step
);
448 for (pos
= m_threads
.begin(); pos
!= end
; ++pos
) {
449 if ((*pos
)->GetResumeState() != eStateSuspended
) {
450 switch ((*pos
)->ShouldReportRun(event_ptr
)) {
454 if (result
== eVoteNoOpinion
)
459 "ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64
460 ") says don't report.",
461 (*pos
)->GetIndexID(), (*pos
)->GetID());
470 void ThreadList::Clear() {
471 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
474 m_selected_tid
= LLDB_INVALID_THREAD_ID
;
477 void ThreadList::Destroy() {
478 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
479 const uint32_t num_threads
= m_threads
.size();
480 for (uint32_t idx
= 0; idx
< num_threads
; ++idx
) {
481 m_threads
[idx
]->DestroyThread();
485 void ThreadList::RefreshStateAfterStop() {
486 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
488 m_process
.UpdateThreadListIfNeeded();
490 Log
*log
= GetLog(LLDBLog::Step
);
491 if (log
&& log
->GetVerbose())
493 "Turning off notification of new threads while single stepping "
496 collection::iterator pos
, end
= m_threads
.end();
497 for (pos
= m_threads
.begin(); pos
!= end
; ++pos
)
498 (*pos
)->RefreshStateAfterStop();
501 void ThreadList::DiscardThreadPlans() {
502 // You don't need to update the thread list here, because only threads that
503 // you currently know about have any thread plans.
504 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
506 collection::iterator pos
, end
= m_threads
.end();
507 for (pos
= m_threads
.begin(); pos
!= end
; ++pos
)
508 (*pos
)->DiscardThreadPlans(true);
511 bool ThreadList::WillResume() {
512 // Run through the threads and perform their momentary actions. But we only
513 // do this for threads that are running, user suspended threads stay where
516 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
517 m_process
.UpdateThreadListIfNeeded();
519 collection::iterator pos
, end
= m_threads
.end();
521 // See if any thread wants to run stopping others. If it does, then we won't
522 // setup the other threads for resume, since they aren't going to get a
523 // chance to run. This is necessary because the SetupForResume might add
524 // "StopOthers" plans which would then get to be part of the who-gets-to-run
525 // negotiation, but they're coming in after the fact, and the threads that
526 // are already set up should take priority.
528 bool wants_solo_run
= false;
530 for (pos
= m_threads
.begin(); pos
!= end
; ++pos
) {
531 lldbassert((*pos
)->GetCurrentPlan() &&
532 "thread should not have null thread plan");
533 if ((*pos
)->GetResumeState() != eStateSuspended
&&
534 (*pos
)->GetCurrentPlan()->StopOthers()) {
535 if ((*pos
)->IsOperatingSystemPluginThread() &&
536 !(*pos
)->GetBackingThread())
538 wants_solo_run
= true;
543 if (wants_solo_run
) {
544 Log
*log
= GetLog(LLDBLog::Step
);
545 if (log
&& log
->GetVerbose())
546 LLDB_LOGF(log
, "Turning on notification of new threads while single "
547 "stepping a thread.");
548 m_process
.StartNoticingNewThreads();
550 Log
*log
= GetLog(LLDBLog::Step
);
551 if (log
&& log
->GetVerbose())
552 LLDB_LOGF(log
, "Turning off notification of new threads while single "
553 "stepping a thread.");
554 m_process
.StopNoticingNewThreads();
557 // Give all the threads that are likely to run a last chance to set up their
558 // state before we negotiate who is actually going to get a chance to run...
559 // Don't set to resume suspended threads, and if any thread wanted to stop
560 // others, only call setup on the threads that request StopOthers...
562 for (pos
= m_threads
.begin(); pos
!= end
; ++pos
) {
563 if ((*pos
)->GetResumeState() != eStateSuspended
&&
564 (!wants_solo_run
|| (*pos
)->GetCurrentPlan()->StopOthers())) {
565 if ((*pos
)->IsOperatingSystemPluginThread() &&
566 !(*pos
)->GetBackingThread())
568 (*pos
)->SetupForResume();
572 // Now go through the threads and see if any thread wants to run just itself.
573 // if so then pick one and run it.
575 ThreadList
run_me_only_list(m_process
);
577 run_me_only_list
.SetStopID(m_process
.GetStopID());
579 // One or more threads might want to "Stop Others". We want to handle all
580 // those requests first. And if there is a thread that wanted to "resume
581 // before a public stop", let it get the first crack:
582 // There are two special kinds of thread that have priority for "StopOthers":
583 // a "ShouldRunBeforePublicStop thread, or the currently selected thread. If
584 // we find one satisfying that critereon, put it here.
585 ThreadSP stop_others_thread_sp
;
587 for (pos
= m_threads
.begin(); pos
!= end
; ++pos
) {
588 ThreadSP
thread_sp(*pos
);
589 if (thread_sp
->GetResumeState() != eStateSuspended
&&
590 thread_sp
->GetCurrentPlan()->StopOthers()) {
591 if ((*pos
)->IsOperatingSystemPluginThread() &&
592 !(*pos
)->GetBackingThread())
595 // You can't say "stop others" and also want yourself to be suspended.
596 assert(thread_sp
->GetCurrentPlan()->RunState() != eStateSuspended
);
597 run_me_only_list
.AddThread(thread_sp
);
599 if (thread_sp
== GetSelectedThread())
600 stop_others_thread_sp
= thread_sp
;
602 if (thread_sp
->ShouldRunBeforePublicStop()) {
603 // This takes precedence, so if we find one of these, service it:
604 stop_others_thread_sp
= thread_sp
;
610 bool need_to_resume
= true;
612 if (run_me_only_list
.GetSize(false) == 0) {
613 // Everybody runs as they wish:
614 for (pos
= m_threads
.begin(); pos
!= end
; ++pos
) {
615 ThreadSP
thread_sp(*pos
);
617 if (thread_sp
->GetResumeState() != eStateSuspended
)
618 run_state
= thread_sp
->GetCurrentPlan()->RunState();
620 run_state
= eStateSuspended
;
621 if (!thread_sp
->ShouldResume(run_state
))
622 need_to_resume
= false;
625 ThreadSP thread_to_run
;
627 if (stop_others_thread_sp
) {
628 thread_to_run
= stop_others_thread_sp
;
629 } else if (run_me_only_list
.GetSize(false) == 1) {
630 thread_to_run
= run_me_only_list
.GetThreadAtIndex(0);
633 (int)((run_me_only_list
.GetSize(false) * (double)rand()) /
635 thread_to_run
= run_me_only_list
.GetThreadAtIndex(random_thread
);
638 for (pos
= m_threads
.begin(); pos
!= end
; ++pos
) {
639 ThreadSP
thread_sp(*pos
);
640 if (thread_sp
== thread_to_run
) {
641 // Note, a thread might be able to fulfil it's plan w/o actually
642 // resuming. An example of this is a step that changes the current
643 // inlined function depth w/o moving the PC. Check that here:
644 if (!thread_sp
->ShouldResume(thread_sp
->GetCurrentPlan()->RunState()))
645 need_to_resume
= false;
647 thread_sp
->ShouldResume(eStateSuspended
);
651 return need_to_resume
;
654 void ThreadList::DidResume() {
655 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
656 collection::iterator pos
, end
= m_threads
.end();
657 for (pos
= m_threads
.begin(); pos
!= end
; ++pos
) {
658 // Don't clear out threads that aren't going to get a chance to run, rather
659 // leave their state for the next time around.
660 ThreadSP
thread_sp(*pos
);
661 if (thread_sp
->GetTemporaryResumeState() != eStateSuspended
)
662 thread_sp
->DidResume();
666 void ThreadList::DidStop() {
667 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
668 collection::iterator pos
, end
= m_threads
.end();
669 for (pos
= m_threads
.begin(); pos
!= end
; ++pos
) {
670 // Notify threads that the process just stopped. Note, this currently
671 // assumes that all threads in the list stop when the process stops. In
672 // the future we will want to support a debugging model where some threads
673 // continue to run while others are stopped. We either need to handle that
674 // somehow here or create a special thread list containing only threads
675 // which will stop in the code that calls this method (currently
676 // Process::SetPrivateState).
677 ThreadSP
thread_sp(*pos
);
678 if (StateIsRunningState(thread_sp
->GetState()))
679 thread_sp
->DidStop();
683 ThreadSP
ThreadList::GetSelectedThread() {
684 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
685 ThreadSP thread_sp
= FindThreadByID(m_selected_tid
);
686 if (!thread_sp
.get()) {
687 if (m_threads
.size() == 0)
689 m_selected_tid
= m_threads
[0]->GetID();
690 thread_sp
= m_threads
[0];
695 bool ThreadList::SetSelectedThreadByID(lldb::tid_t tid
, bool notify
) {
696 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
697 ThreadSP
selected_thread_sp(FindThreadByID(tid
));
698 if (selected_thread_sp
) {
699 m_selected_tid
= tid
;
700 selected_thread_sp
->SetDefaultFileAndLineToSelectedFrame();
702 m_selected_tid
= LLDB_INVALID_THREAD_ID
;
705 NotifySelectedThreadChanged(m_selected_tid
);
707 return m_selected_tid
!= LLDB_INVALID_THREAD_ID
;
710 bool ThreadList::SetSelectedThreadByIndexID(uint32_t index_id
, bool notify
) {
711 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
712 ThreadSP
selected_thread_sp(FindThreadByIndexID(index_id
));
713 if (selected_thread_sp
.get()) {
714 m_selected_tid
= selected_thread_sp
->GetID();
715 selected_thread_sp
->SetDefaultFileAndLineToSelectedFrame();
717 m_selected_tid
= LLDB_INVALID_THREAD_ID
;
720 NotifySelectedThreadChanged(m_selected_tid
);
722 return m_selected_tid
!= LLDB_INVALID_THREAD_ID
;
725 void ThreadList::NotifySelectedThreadChanged(lldb::tid_t tid
) {
726 ThreadSP
selected_thread_sp(FindThreadByID(tid
));
727 if (selected_thread_sp
->EventTypeHasListeners(
728 Thread::eBroadcastBitThreadSelected
)) {
730 std::make_shared
<Thread::ThreadEventData
>(selected_thread_sp
);
731 selected_thread_sp
->BroadcastEvent(Thread::eBroadcastBitThreadSelected
,
736 void ThreadList::Update(ThreadList
&rhs
) {
738 // We only allow assignments between thread lists describing the same
739 // process. Same process implies same mutex, which means it's enough to lock
740 // just the current object.
741 assert(&m_process
== &rhs
.m_process
);
742 assert(&GetMutex() == &rhs
.GetMutex());
743 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
745 m_stop_id
= rhs
.m_stop_id
;
746 m_threads
.swap(rhs
.m_threads
);
747 m_selected_tid
= rhs
.m_selected_tid
;
749 // Now we look for threads that we are done with and make sure to clear
750 // them up as much as possible so anyone with a shared pointer will still
751 // have a reference, but the thread won't be of much use. Using
752 // std::weak_ptr for all backward references (such as a thread to a
753 // process) will eventually solve this issue for us, but for now, we need
754 // to work around the issue
755 collection::iterator rhs_pos
, rhs_end
= rhs
.m_threads
.end();
756 for (rhs_pos
= rhs
.m_threads
.begin(); rhs_pos
!= rhs_end
; ++rhs_pos
) {
757 // If this thread has already been destroyed, we don't need to look for
758 // it to destroy it again.
759 if (!(*rhs_pos
)->IsValid())
762 const lldb::tid_t tid
= (*rhs_pos
)->GetID();
763 bool thread_is_alive
= false;
764 const uint32_t num_threads
= m_threads
.size();
765 for (uint32_t idx
= 0; idx
< num_threads
; ++idx
) {
766 ThreadSP backing_thread
= m_threads
[idx
]->GetBackingThread();
767 if (m_threads
[idx
]->GetID() == tid
||
768 (backing_thread
&& backing_thread
->GetID() == tid
)) {
769 thread_is_alive
= true;
773 if (!thread_is_alive
) {
774 (*rhs_pos
)->DestroyThread();
780 void ThreadList::Flush() {
781 std::lock_guard
<std::recursive_mutex
> guard(GetMutex());
782 collection::iterator pos
, end
= m_threads
.end();
783 for (pos
= m_threads
.begin(); pos
!= end
; ++pos
)
787 std::recursive_mutex
&ThreadList::GetMutex() const {
788 return m_process
.m_thread_mutex
;
791 ThreadList::ExpressionExecutionThreadPusher::ExpressionExecutionThreadPusher(
792 lldb::ThreadSP thread_sp
)
793 : m_thread_list(nullptr), m_tid(LLDB_INVALID_THREAD_ID
) {
795 m_tid
= thread_sp
->GetID();
796 m_thread_list
= &thread_sp
->GetProcess()->GetThreadList();
797 m_thread_list
->PushExpressionExecutionThread(m_tid
);