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