1 //===-- ThreadList.cpp ----------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include <stdlib.h>
10
11 #include <algorithm>
12
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/Log.h"
20 #include "lldb/Utility/State.h"
21
22 using namespace lldb;
23 using namespace lldb_private;
24
ThreadList(Process * process)25 ThreadList::ThreadList(Process *process)
26 : ThreadCollection(), m_process(process), m_stop_id(0),
27 m_selected_tid(LLDB_INVALID_THREAD_ID) {}
28
ThreadList(const ThreadList & rhs)29 ThreadList::ThreadList(const ThreadList &rhs)
30 : ThreadCollection(), m_process(rhs.m_process), m_stop_id(rhs.m_stop_id),
31 m_selected_tid() {
32 // Use the assignment operator since it uses the mutex
33 *this = rhs;
34 }
35
operator =(const ThreadList & rhs)36 const ThreadList &ThreadList::operator=(const ThreadList &rhs) {
37 if (this != &rhs) {
38 // Lock both mutexes to make sure neither side changes anyone on us while
39 // the assignment occurs
40 std::lock(GetMutex(), rhs.GetMutex());
41 std::lock_guard<std::recursive_mutex> guard(GetMutex(), std::adopt_lock);
42 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.GetMutex(),
43 std::adopt_lock);
44
45 m_process = rhs.m_process;
46 m_stop_id = rhs.m_stop_id;
47 m_threads = rhs.m_threads;
48 m_selected_tid = rhs.m_selected_tid;
49 }
50 return *this;
51 }
52
~ThreadList()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.
56 Clear();
57 }
58
GetExpressionExecutionThread()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());
63 if (expr_thread_sp)
64 return expr_thread_sp;
65 else
66 return GetSelectedThread();
67 }
68
PushExpressionExecutionThread(lldb::tid_t tid)69 void ThreadList::PushExpressionExecutionThread(lldb::tid_t tid) {
70 m_expression_tid_stack.push_back(tid);
71 }
72
PopExpressionExecutionThread(lldb::tid_t tid)73 void ThreadList::PopExpressionExecutionThread(lldb::tid_t tid) {
74 assert(m_expression_tid_stack.back() == tid);
75 m_expression_tid_stack.pop_back();
76 }
77
GetStopID() const78 uint32_t ThreadList::GetStopID() const { return m_stop_id; }
79
SetStopID(uint32_t stop_id)80 void ThreadList::SetStopID(uint32_t stop_id) { m_stop_id = stop_id; }
81
GetSize(bool can_update)82 uint32_t ThreadList::GetSize(bool can_update) {
83 std::lock_guard<std::recursive_mutex> guard(GetMutex());
84
85 if (can_update)
86 m_process->UpdateThreadListIfNeeded();
87 return m_threads.size();
88 }
89
GetThreadAtIndex(uint32_t idx,bool can_update)90 ThreadSP ThreadList::GetThreadAtIndex(uint32_t idx, bool can_update) {
91 std::lock_guard<std::recursive_mutex> guard(GetMutex());
92
93 if (can_update)
94 m_process->UpdateThreadListIfNeeded();
95
96 ThreadSP thread_sp;
97 if (idx < m_threads.size())
98 thread_sp = m_threads[idx];
99 return thread_sp;
100 }
101
FindThreadByID(lldb::tid_t tid,bool can_update)102 ThreadSP ThreadList::FindThreadByID(lldb::tid_t tid, bool can_update) {
103 std::lock_guard<std::recursive_mutex> guard(GetMutex());
104
105 if (can_update)
106 m_process->UpdateThreadListIfNeeded();
107
108 ThreadSP thread_sp;
109 uint32_t idx = 0;
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];
114 break;
115 }
116 }
117 return thread_sp;
118 }
119
FindThreadByProtocolID(lldb::tid_t tid,bool can_update)120 ThreadSP ThreadList::FindThreadByProtocolID(lldb::tid_t tid, bool can_update) {
121 std::lock_guard<std::recursive_mutex> guard(GetMutex());
122
123 if (can_update)
124 m_process->UpdateThreadListIfNeeded();
125
126 ThreadSP thread_sp;
127 uint32_t idx = 0;
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];
132 break;
133 }
134 }
135 return thread_sp;
136 }
137
RemoveThreadByID(lldb::tid_t tid,bool can_update)138 ThreadSP ThreadList::RemoveThreadByID(lldb::tid_t tid, bool can_update) {
139 std::lock_guard<std::recursive_mutex> guard(GetMutex());
140
141 if (can_update)
142 m_process->UpdateThreadListIfNeeded();
143
144 ThreadSP thread_sp;
145 uint32_t idx = 0;
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);
151 break;
152 }
153 }
154 return thread_sp;
155 }
156
RemoveThreadByProtocolID(lldb::tid_t tid,bool can_update)157 ThreadSP ThreadList::RemoveThreadByProtocolID(lldb::tid_t tid,
158 bool can_update) {
159 std::lock_guard<std::recursive_mutex> guard(GetMutex());
160
161 if (can_update)
162 m_process->UpdateThreadListIfNeeded();
163
164 ThreadSP thread_sp;
165 uint32_t idx = 0;
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);
171 break;
172 }
173 }
174 return thread_sp;
175 }
176
GetThreadSPForThreadPtr(Thread * thread_ptr)177 ThreadSP ThreadList::GetThreadSPForThreadPtr(Thread *thread_ptr) {
178 ThreadSP thread_sp;
179 if (thread_ptr) {
180 std::lock_guard<std::recursive_mutex> guard(GetMutex());
181
182 uint32_t idx = 0;
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];
187 break;
188 }
189 }
190 }
191 return thread_sp;
192 }
193
GetBackingThread(const ThreadSP & real_thread)194 ThreadSP ThreadList::GetBackingThread(const ThreadSP &real_thread) {
195 std::lock_guard<std::recursive_mutex> guard(GetMutex());
196
197 ThreadSP thread_sp;
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];
202 break;
203 }
204 }
205 return thread_sp;
206 }
207
FindThreadByIndexID(uint32_t index_id,bool can_update)208 ThreadSP ThreadList::FindThreadByIndexID(uint32_t index_id, bool can_update) {
209 std::lock_guard<std::recursive_mutex> guard(GetMutex());
210
211 if (can_update)
212 m_process->UpdateThreadListIfNeeded();
213
214 ThreadSP thread_sp;
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];
219 break;
220 }
221 }
222 return thread_sp;
223 }
224
ShouldStop(Event * event_ptr)225 bool ThreadList::ShouldStop(Event *event_ptr) {
226 // Running events should never stop, obviously...
227
228 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
229
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.
237
238 collection threads_copy;
239 {
240 // Scope for locker
241 std::lock_guard<std::recursive_mutex> guard(GetMutex());
242
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. On
248 // Linux, if a thread-specific conditional breakpoint was hit, it won't
249 // necessarily be the thread that hit the breakpoint itself that
250 // evaluates the conditional expression, so the thread that hit the
251 // breakpoint could still be asked to stop, even though it hasn't been
252 // allowed to run since the previous stop.
253 if (thread_sp->GetTemporaryResumeState() != eStateSuspended ||
254 thread_sp->IsStillAtLastBreakpointHit())
255 threads_copy.push_back(thread_sp);
256 }
257
258 // It is possible the threads we were allowing to run all exited and then
259 // maybe the user interrupted or something, then fall back on looking at
260 // all threads:
261
262 if (threads_copy.size() == 0)
263 threads_copy = m_threads;
264 }
265
266 collection::iterator pos, end = threads_copy.end();
267
268 if (log) {
269 log->PutCString("");
270 LLDB_LOGF(log,
271 "ThreadList::%s: %" PRIu64 " threads, %" PRIu64
272 " unsuspended threads",
273 __FUNCTION__, (uint64_t)m_threads.size(),
274 (uint64_t)threads_copy.size());
275 }
276
277 bool did_anybody_stop_for_a_reason = false;
278
279 // If the event is an Interrupt event, then we're going to stop no matter
280 // what. Otherwise, presume we won't stop.
281 bool should_stop = false;
282 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) {
283 LLDB_LOGF(
284 log, "ThreadList::%s handling interrupt event, should stop set to true",
285 __FUNCTION__);
286
287 should_stop = true;
288 }
289
290 // Now we run through all the threads and get their stop info's. We want to
291 // make sure to do this first before we start running the ShouldStop, because
292 // one thread's ShouldStop could destroy information (like deleting a thread
293 // specific breakpoint another thread had stopped at) which could lead us to
294 // compute the StopInfo incorrectly. We don't need to use it here, we just
295 // want to make sure it gets computed.
296
297 for (pos = threads_copy.begin(); pos != end; ++pos) {
298 ThreadSP thread_sp(*pos);
299 thread_sp->GetStopInfo();
300 }
301
302 for (pos = threads_copy.begin(); pos != end; ++pos) {
303 ThreadSP thread_sp(*pos);
304
305 // We should never get a stop for which no thread had a stop reason, but
306 // sometimes we do see this - for instance when we first connect to a
307 // remote stub. In that case we should stop, since we can't figure out the
308 // right thing to do and stopping gives the user control over what to do in
309 // this instance.
310 //
311 // Note, this causes a problem when you have a thread specific breakpoint,
312 // and a bunch of threads hit the breakpoint, but not the thread which we
313 // are waiting for. All the threads that are not "supposed" to hit the
314 // breakpoint are marked as having no stop reason, which is right, they
315 // should not show a stop reason. But that triggers this code and causes
316 // us to stop seemingly for no reason.
317 //
318 // Since the only way we ever saw this error was on first attach, I'm only
319 // going to trigger set did_anybody_stop_for_a_reason to true unless this
320 // is the first stop.
321 //
322 // If this becomes a problem, we'll have to have another StopReason like
323 // "StopInfoHidden" which will look invalid everywhere but at this check.
324
325 if (thread_sp->GetProcess()->GetStopID() > 1)
326 did_anybody_stop_for_a_reason = true;
327 else
328 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
329
330 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
331 if (thread_should_stop)
332 should_stop |= true;
333 }
334
335 if (!should_stop && !did_anybody_stop_for_a_reason) {
336 should_stop = true;
337 LLDB_LOGF(log,
338 "ThreadList::%s we stopped but no threads had a stop reason, "
339 "overriding should_stop and stopping.",
340 __FUNCTION__);
341 }
342
343 LLDB_LOGF(log, "ThreadList::%s overall should_stop = %i", __FUNCTION__,
344 should_stop);
345
346 if (should_stop) {
347 for (pos = threads_copy.begin(); pos != end; ++pos) {
348 ThreadSP thread_sp(*pos);
349 thread_sp->WillStop();
350 }
351 }
352
353 return should_stop;
354 }
355
ShouldReportStop(Event * event_ptr)356 Vote ThreadList::ShouldReportStop(Event *event_ptr) {
357 std::lock_guard<std::recursive_mutex> guard(GetMutex());
358
359 Vote result = eVoteNoOpinion;
360 m_process->UpdateThreadListIfNeeded();
361 collection::iterator pos, end = m_threads.end();
362
363 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
364
365 LLDB_LOGF(log, "ThreadList::%s %" PRIu64 " threads", __FUNCTION__,
366 (uint64_t)m_threads.size());
367
368 // Run through the threads and ask whether we should report this event. For
369 // stopping, a YES vote wins over everything. A NO vote wins over NO
370 // opinion.
371 for (pos = m_threads.begin(); pos != end; ++pos) {
372 ThreadSP thread_sp(*pos);
373 const Vote vote = thread_sp->ShouldReportStop(event_ptr);
374 switch (vote) {
375 case eVoteNoOpinion:
376 continue;
377
378 case eVoteYes:
379 result = eVoteYes;
380 break;
381
382 case eVoteNo:
383 if (result == eVoteNoOpinion) {
384 result = eVoteNo;
385 } else {
386 LLDB_LOG(log,
387 "Thread {0:x} voted {1}, but lost out because result was {2}",
388 thread_sp->GetID(), vote, result);
389 }
390 break;
391 }
392 }
393 LLDB_LOG(log, "Returning {0}", result);
394 return result;
395 }
396
SetShouldReportStop(Vote vote)397 void ThreadList::SetShouldReportStop(Vote vote) {
398 std::lock_guard<std::recursive_mutex> guard(GetMutex());
399
400 m_process->UpdateThreadListIfNeeded();
401 collection::iterator pos, end = m_threads.end();
402 for (pos = m_threads.begin(); pos != end; ++pos) {
403 ThreadSP thread_sp(*pos);
404 thread_sp->SetShouldReportStop(vote);
405 }
406 }
407
ShouldReportRun(Event * event_ptr)408 Vote ThreadList::ShouldReportRun(Event *event_ptr) {
409
410 std::lock_guard<std::recursive_mutex> guard(GetMutex());
411
412 Vote result = eVoteNoOpinion;
413 m_process->UpdateThreadListIfNeeded();
414 collection::iterator pos, end = m_threads.end();
415
416 // Run through the threads and ask whether we should report this event. The
417 // rule is NO vote wins over everything, a YES vote wins over no opinion.
418
419 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
420
421 for (pos = m_threads.begin(); pos != end; ++pos) {
422 if ((*pos)->GetResumeState() != eStateSuspended) {
423 switch ((*pos)->ShouldReportRun(event_ptr)) {
424 case eVoteNoOpinion:
425 continue;
426 case eVoteYes:
427 if (result == eVoteNoOpinion)
428 result = eVoteYes;
429 break;
430 case eVoteNo:
431 LLDB_LOGF(log,
432 "ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64
433 ") says don't report.",
434 (*pos)->GetIndexID(), (*pos)->GetID());
435 result = eVoteNo;
436 break;
437 }
438 }
439 }
440 return result;
441 }
442
Clear()443 void ThreadList::Clear() {
444 std::lock_guard<std::recursive_mutex> guard(GetMutex());
445 m_stop_id = 0;
446 m_threads.clear();
447 m_selected_tid = LLDB_INVALID_THREAD_ID;
448 }
449
Destroy()450 void ThreadList::Destroy() {
451 std::lock_guard<std::recursive_mutex> guard(GetMutex());
452 const uint32_t num_threads = m_threads.size();
453 for (uint32_t idx = 0; idx < num_threads; ++idx) {
454 m_threads[idx]->DestroyThread();
455 }
456 }
457
RefreshStateAfterStop()458 void ThreadList::RefreshStateAfterStop() {
459 std::lock_guard<std::recursive_mutex> guard(GetMutex());
460
461 m_process->UpdateThreadListIfNeeded();
462
463 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
464 if (log && log->GetVerbose())
465 LLDB_LOGF(log,
466 "Turning off notification of new threads while single stepping "
467 "a thread.");
468
469 collection::iterator pos, end = m_threads.end();
470 for (pos = m_threads.begin(); pos != end; ++pos)
471 (*pos)->RefreshStateAfterStop();
472 }
473
DiscardThreadPlans()474 void ThreadList::DiscardThreadPlans() {
475 // You don't need to update the thread list here, because only threads that
476 // you currently know about have any thread plans.
477 std::lock_guard<std::recursive_mutex> guard(GetMutex());
478
479 collection::iterator pos, end = m_threads.end();
480 for (pos = m_threads.begin(); pos != end; ++pos)
481 (*pos)->DiscardThreadPlans(true);
482 }
483
WillResume()484 bool ThreadList::WillResume() {
485 // Run through the threads and perform their momentary actions. But we only
486 // do this for threads that are running, user suspended threads stay where
487 // they are.
488
489 std::lock_guard<std::recursive_mutex> guard(GetMutex());
490 m_process->UpdateThreadListIfNeeded();
491
492 collection::iterator pos, end = m_threads.end();
493
494 // See if any thread wants to run stopping others. If it does, then we won't
495 // setup the other threads for resume, since they aren't going to get a
496 // chance to run. This is necessary because the SetupForResume might add
497 // "StopOthers" plans which would then get to be part of the who-gets-to-run
498 // negotiation, but they're coming in after the fact, and the threads that
499 // are already set up should take priority.
500
501 bool wants_solo_run = false;
502
503 for (pos = m_threads.begin(); pos != end; ++pos) {
504 lldbassert((*pos)->GetCurrentPlan() &&
505 "thread should not have null thread plan");
506 if ((*pos)->GetResumeState() != eStateSuspended &&
507 (*pos)->GetCurrentPlan()->StopOthers()) {
508 if ((*pos)->IsOperatingSystemPluginThread() &&
509 !(*pos)->GetBackingThread())
510 continue;
511 wants_solo_run = true;
512 break;
513 }
514 }
515
516 if (wants_solo_run) {
517 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
518 if (log && log->GetVerbose())
519 LLDB_LOGF(log, "Turning on notification of new threads while single "
520 "stepping a thread.");
521 m_process->StartNoticingNewThreads();
522 } else {
523 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
524 if (log && log->GetVerbose())
525 LLDB_LOGF(log, "Turning off notification of new threads while single "
526 "stepping a thread.");
527 m_process->StopNoticingNewThreads();
528 }
529
530 // Give all the threads that are likely to run a last chance to set up their
531 // state before we negotiate who is actually going to get a chance to run...
532 // Don't set to resume suspended threads, and if any thread wanted to stop
533 // others, only call setup on the threads that request StopOthers...
534
535 for (pos = m_threads.begin(); pos != end; ++pos) {
536 if ((*pos)->GetResumeState() != eStateSuspended &&
537 (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers())) {
538 if ((*pos)->IsOperatingSystemPluginThread() &&
539 !(*pos)->GetBackingThread())
540 continue;
541 (*pos)->SetupForResume();
542 }
543 }
544
545 // Now go through the threads and see if any thread wants to run just itself.
546 // if so then pick one and run it.
547
548 ThreadList run_me_only_list(m_process);
549
550 run_me_only_list.SetStopID(m_process->GetStopID());
551
552 bool run_only_current_thread = false;
553
554 for (pos = m_threads.begin(); pos != end; ++pos) {
555 ThreadSP thread_sp(*pos);
556 if (thread_sp->GetResumeState() != eStateSuspended &&
557 thread_sp->GetCurrentPlan()->StopOthers()) {
558 if ((*pos)->IsOperatingSystemPluginThread() &&
559 !(*pos)->GetBackingThread())
560 continue;
561
562 // You can't say "stop others" and also want yourself to be suspended.
563 assert(thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
564
565 if (thread_sp == GetSelectedThread()) {
566 // If the currently selected thread wants to run on its own, always let
567 // it.
568 run_only_current_thread = true;
569 run_me_only_list.Clear();
570 run_me_only_list.AddThread(thread_sp);
571 break;
572 }
573
574 run_me_only_list.AddThread(thread_sp);
575 }
576 }
577
578 bool need_to_resume = true;
579
580 if (run_me_only_list.GetSize(false) == 0) {
581 // Everybody runs as they wish:
582 for (pos = m_threads.begin(); pos != end; ++pos) {
583 ThreadSP thread_sp(*pos);
584 StateType run_state;
585 if (thread_sp->GetResumeState() != eStateSuspended)
586 run_state = thread_sp->GetCurrentPlan()->RunState();
587 else
588 run_state = eStateSuspended;
589 if (!thread_sp->ShouldResume(run_state))
590 need_to_resume = false;
591 }
592 } else {
593 ThreadSP thread_to_run;
594
595 if (run_only_current_thread) {
596 thread_to_run = GetSelectedThread();
597 } else if (run_me_only_list.GetSize(false) == 1) {
598 thread_to_run = run_me_only_list.GetThreadAtIndex(0);
599 } else {
600 int random_thread =
601 (int)((run_me_only_list.GetSize(false) * (double)rand()) /
602 (RAND_MAX + 1.0));
603 thread_to_run = run_me_only_list.GetThreadAtIndex(random_thread);
604 }
605
606 for (pos = m_threads.begin(); pos != end; ++pos) {
607 ThreadSP thread_sp(*pos);
608 if (thread_sp == thread_to_run) {
609 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
610 need_to_resume = false;
611 } else
612 thread_sp->ShouldResume(eStateSuspended);
613 }
614 }
615
616 return need_to_resume;
617 }
618
DidResume()619 void ThreadList::DidResume() {
620 std::lock_guard<std::recursive_mutex> guard(GetMutex());
621 collection::iterator pos, end = m_threads.end();
622 for (pos = m_threads.begin(); pos != end; ++pos) {
623 // Don't clear out threads that aren't going to get a chance to run, rather
624 // leave their state for the next time around.
625 ThreadSP thread_sp(*pos);
626 if (thread_sp->GetResumeState() != eStateSuspended)
627 thread_sp->DidResume();
628 }
629 }
630
DidStop()631 void ThreadList::DidStop() {
632 std::lock_guard<std::recursive_mutex> guard(GetMutex());
633 collection::iterator pos, end = m_threads.end();
634 for (pos = m_threads.begin(); pos != end; ++pos) {
635 // Notify threads that the process just stopped. Note, this currently
636 // assumes that all threads in the list stop when the process stops. In
637 // the future we will want to support a debugging model where some threads
638 // continue to run while others are stopped. We either need to handle that
639 // somehow here or create a special thread list containing only threads
640 // which will stop in the code that calls this method (currently
641 // Process::SetPrivateState).
642 ThreadSP thread_sp(*pos);
643 if (StateIsRunningState(thread_sp->GetState()))
644 thread_sp->DidStop();
645 }
646 }
647
GetSelectedThread()648 ThreadSP ThreadList::GetSelectedThread() {
649 std::lock_guard<std::recursive_mutex> guard(GetMutex());
650 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
651 if (!thread_sp.get()) {
652 if (m_threads.size() == 0)
653 return thread_sp;
654 m_selected_tid = m_threads[0]->GetID();
655 thread_sp = m_threads[0];
656 }
657 return thread_sp;
658 }
659
SetSelectedThreadByID(lldb::tid_t tid,bool notify)660 bool ThreadList::SetSelectedThreadByID(lldb::tid_t tid, bool notify) {
661 std::lock_guard<std::recursive_mutex> guard(GetMutex());
662 ThreadSP selected_thread_sp(FindThreadByID(tid));
663 if (selected_thread_sp) {
664 m_selected_tid = tid;
665 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
666 } else
667 m_selected_tid = LLDB_INVALID_THREAD_ID;
668
669 if (notify)
670 NotifySelectedThreadChanged(m_selected_tid);
671
672 return m_selected_tid != LLDB_INVALID_THREAD_ID;
673 }
674
SetSelectedThreadByIndexID(uint32_t index_id,bool notify)675 bool ThreadList::SetSelectedThreadByIndexID(uint32_t index_id, bool notify) {
676 std::lock_guard<std::recursive_mutex> guard(GetMutex());
677 ThreadSP selected_thread_sp(FindThreadByIndexID(index_id));
678 if (selected_thread_sp.get()) {
679 m_selected_tid = selected_thread_sp->GetID();
680 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
681 } else
682 m_selected_tid = LLDB_INVALID_THREAD_ID;
683
684 if (notify)
685 NotifySelectedThreadChanged(m_selected_tid);
686
687 return m_selected_tid != LLDB_INVALID_THREAD_ID;
688 }
689
NotifySelectedThreadChanged(lldb::tid_t tid)690 void ThreadList::NotifySelectedThreadChanged(lldb::tid_t tid) {
691 ThreadSP selected_thread_sp(FindThreadByID(tid));
692 if (selected_thread_sp->EventTypeHasListeners(
693 Thread::eBroadcastBitThreadSelected))
694 selected_thread_sp->BroadcastEvent(
695 Thread::eBroadcastBitThreadSelected,
696 new Thread::ThreadEventData(selected_thread_sp));
697 }
698
Update(ThreadList & rhs)699 void ThreadList::Update(ThreadList &rhs) {
700 if (this != &rhs) {
701 // Lock both mutexes to make sure neither side changes anyone on us while
702 // the assignment occurs
703 std::lock_guard<std::recursive_mutex> guard(GetMutex());
704
705 m_process = rhs.m_process;
706 m_stop_id = rhs.m_stop_id;
707 m_threads.swap(rhs.m_threads);
708 m_selected_tid = rhs.m_selected_tid;
709
710 // Now we look for threads that we are done with and make sure to clear
711 // them up as much as possible so anyone with a shared pointer will still
712 // have a reference, but the thread won't be of much use. Using
713 // std::weak_ptr for all backward references (such as a thread to a
714 // process) will eventually solve this issue for us, but for now, we need
715 // to work around the issue
716 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
717 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos) {
718 // If this thread has already been destroyed, we don't need to look for
719 // it to destroy it again.
720 if (!(*rhs_pos)->IsValid())
721 continue;
722
723 const lldb::tid_t tid = (*rhs_pos)->GetID();
724 bool thread_is_alive = false;
725 const uint32_t num_threads = m_threads.size();
726 for (uint32_t idx = 0; idx < num_threads; ++idx) {
727 ThreadSP backing_thread = m_threads[idx]->GetBackingThread();
728 if (m_threads[idx]->GetID() == tid ||
729 (backing_thread && backing_thread->GetID() == tid)) {
730 thread_is_alive = true;
731 break;
732 }
733 }
734 if (!thread_is_alive) {
735 (*rhs_pos)->DestroyThread();
736 }
737 }
738 }
739 }
740
Flush()741 void ThreadList::Flush() {
742 std::lock_guard<std::recursive_mutex> guard(GetMutex());
743 collection::iterator pos, end = m_threads.end();
744 for (pos = m_threads.begin(); pos != end; ++pos)
745 (*pos)->Flush();
746 }
747
GetMutex() const748 std::recursive_mutex &ThreadList::GetMutex() const {
749 return m_process->m_thread_mutex;
750 }
751
ExpressionExecutionThreadPusher(lldb::ThreadSP thread_sp)752 ThreadList::ExpressionExecutionThreadPusher::ExpressionExecutionThreadPusher(
753 lldb::ThreadSP thread_sp)
754 : m_thread_list(nullptr), m_tid(LLDB_INVALID_THREAD_ID) {
755 if (thread_sp) {
756 m_tid = thread_sp->GetID();
757 m_thread_list = &thread_sp->GetProcess()->GetThreadList();
758 m_thread_list->PushExpressionExecutionThread(m_tid);
759 }
760 }
761