1 //===-- ThreadList.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
22 using namespace lldb_private;
24 ThreadList::ThreadList (Process *process) :
28 m_selected_tid (LLDB_INVALID_THREAD_ID)
32 ThreadList::ThreadList (const ThreadList &rhs) :
33 m_process (rhs.m_process),
34 m_stop_id (rhs.m_stop_id),
38 // Use the assignment operator since it uses the mutex
43 ThreadList::operator = (const ThreadList& rhs)
47 // Lock both mutexes to make sure neither side changes anyone on us
48 // while the assignement occurs
49 Mutex::Locker locker(GetMutex());
50 m_process = rhs.m_process;
51 m_stop_id = rhs.m_stop_id;
52 m_threads = rhs.m_threads;
53 m_selected_tid = rhs.m_selected_tid;
59 ThreadList::~ThreadList()
61 // Clear the thread list. Clear will take the mutex lock
62 // which will ensure that if anyone is using the list
63 // they won't get it removed while using it.
69 ThreadList::GetStopID () const
75 ThreadList::SetStopID (uint32_t stop_id)
82 ThreadList::AddThread (const ThreadSP &thread_sp)
84 Mutex::Locker locker(GetMutex());
85 m_threads.push_back(thread_sp);
89 ThreadList::InsertThread (const lldb::ThreadSP &thread_sp, uint32_t idx)
91 Mutex::Locker locker(GetMutex());
92 if (idx < m_threads.size())
93 m_threads.insert(m_threads.begin() + idx, thread_sp);
95 m_threads.push_back (thread_sp);
100 ThreadList::GetSize (bool can_update)
102 Mutex::Locker locker(GetMutex());
104 m_process->UpdateThreadListIfNeeded();
105 return m_threads.size();
109 ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
111 Mutex::Locker locker(GetMutex());
113 m_process->UpdateThreadListIfNeeded();
116 if (idx < m_threads.size())
117 thread_sp = m_threads[idx];
122 ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
124 Mutex::Locker locker(GetMutex());
127 m_process->UpdateThreadListIfNeeded();
131 const uint32_t num_threads = m_threads.size();
132 for (idx = 0; idx < num_threads; ++idx)
134 if (m_threads[idx]->GetID() == tid)
136 thread_sp = m_threads[idx];
144 ThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update)
146 Mutex::Locker locker(GetMutex());
149 m_process->UpdateThreadListIfNeeded();
153 const uint32_t num_threads = m_threads.size();
154 for (idx = 0; idx < num_threads; ++idx)
156 if (m_threads[idx]->GetProtocolID() == tid)
158 thread_sp = m_threads[idx];
167 ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
169 Mutex::Locker locker(GetMutex());
172 m_process->UpdateThreadListIfNeeded();
176 const uint32_t num_threads = m_threads.size();
177 for (idx = 0; idx < num_threads; ++idx)
179 if (m_threads[idx]->GetID() == tid)
181 thread_sp = m_threads[idx];
182 m_threads.erase(m_threads.begin()+idx);
190 ThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update)
192 Mutex::Locker locker(GetMutex());
195 m_process->UpdateThreadListIfNeeded();
199 const uint32_t num_threads = m_threads.size();
200 for (idx = 0; idx < num_threads; ++idx)
202 if (m_threads[idx]->GetProtocolID() == tid)
204 thread_sp = m_threads[idx];
205 m_threads.erase(m_threads.begin()+idx);
213 ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
218 Mutex::Locker locker(GetMutex());
221 const uint32_t num_threads = m_threads.size();
222 for (idx = 0; idx < num_threads; ++idx)
224 if (m_threads[idx].get() == thread_ptr)
226 thread_sp = m_threads[idx];
237 ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
239 Mutex::Locker locker(GetMutex());
242 m_process->UpdateThreadListIfNeeded();
245 const uint32_t num_threads = m_threads.size();
246 for (uint32_t idx = 0; idx < num_threads; ++idx)
248 if (m_threads[idx]->GetIndexID() == index_id)
250 thread_sp = m_threads[idx];
258 ThreadList::ShouldStop (Event *event_ptr)
260 // Running events should never stop, obviously...
262 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
264 // The ShouldStop method of the threads can do a whole lot of work,
265 // figuring out whether the thread plan conditions are met. So we don't want
266 // to keep the ThreadList locked the whole time we are doing this.
267 // FIXME: It is possible that running code could cause new threads
268 // to be created. If that happens we will miss asking them whether
269 // then should stop. This is not a big deal, since we haven't had
270 // a chance to hang any interesting operations on those threads yet.
272 collection threads_copy;
275 Mutex::Locker locker(GetMutex());
277 m_process->UpdateThreadListIfNeeded();
278 threads_copy = m_threads;
281 collection::iterator pos, end = threads_copy.end();
286 log->Printf ("ThreadList::%s: %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
289 bool did_anybody_stop_for_a_reason = false;
291 // If the event is an Interrupt event, then we're going to stop no matter what. Otherwise, presume we won't stop.
292 bool should_stop = false;
293 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
296 log->Printf("ThreadList::%s handling interrupt event, should stop set to true", __FUNCTION__);
301 // Now we run through all the threads and get their stop info's. We want to make sure to do this first before
302 // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a
303 // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly.
304 // We don't need to use it here, we just want to make sure it gets computed.
306 for (pos = threads_copy.begin(); pos != end; ++pos)
308 ThreadSP thread_sp(*pos);
309 thread_sp->GetStopInfo();
312 for (pos = threads_copy.begin(); pos != end; ++pos)
314 ThreadSP thread_sp(*pos);
316 // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
317 // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out
318 // the right thing to do and stopping gives the user control over what to do in this instance.
320 // Note, this causes a problem when you have a thread specific breakpoint, and a bunch of threads hit the breakpoint,
321 // but not the thread which we are waiting for. All the threads that are not "supposed" to hit the breakpoint
322 // are marked as having no stop reason, which is right, they should not show a stop reason. But that triggers this
323 // code and causes us to stop seemingly for no reason.
325 // 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
326 // to true unless this is the first stop.
328 // If this becomes a problem, we'll have to have another StopReason like "StopInfoHidden" which will look invalid
329 // everywhere but at this check.
331 if (thread_sp->GetProcess()->GetStopID() > 1)
332 did_anybody_stop_for_a_reason = true;
334 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
336 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
337 if (thread_should_stop)
341 if (!should_stop && !did_anybody_stop_for_a_reason)
345 log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
349 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
353 for (pos = threads_copy.begin(); pos != end; ++pos)
355 ThreadSP thread_sp(*pos);
356 thread_sp->WillStop ();
364 ThreadList::ShouldReportStop (Event *event_ptr)
366 Mutex::Locker locker(GetMutex());
368 Vote result = eVoteNoOpinion;
369 m_process->UpdateThreadListIfNeeded();
370 collection::iterator pos, end = m_threads.end();
372 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
375 log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
377 // Run through the threads and ask whether we should report this event.
378 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
379 for (pos = m_threads.begin(); pos != end; ++pos)
381 ThreadSP thread_sp(*pos);
382 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
393 if (result == eVoteNoOpinion)
400 log->Printf ("ThreadList::%s thread 0x%4.4" PRIx64 ": voted %s, but lost out because result was %s",
403 GetVoteAsCString (vote),
404 GetVoteAsCString (result));
410 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
415 ThreadList::SetShouldReportStop (Vote vote)
417 Mutex::Locker locker(GetMutex());
418 m_process->UpdateThreadListIfNeeded();
419 collection::iterator pos, end = m_threads.end();
420 for (pos = m_threads.begin(); pos != end; ++pos)
422 ThreadSP thread_sp(*pos);
423 thread_sp->SetShouldReportStop (vote);
428 ThreadList::ShouldReportRun (Event *event_ptr)
431 Mutex::Locker locker(GetMutex());
433 Vote result = eVoteNoOpinion;
434 m_process->UpdateThreadListIfNeeded();
435 collection::iterator pos, end = m_threads.end();
437 // Run through the threads and ask whether we should report this event.
438 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
440 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
442 for (pos = m_threads.begin(); pos != end; ++pos)
444 if ((*pos)->GetResumeState () != eStateSuspended)
446 switch ((*pos)->ShouldReportRun (event_ptr))
451 if (result == eVoteNoOpinion)
456 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
457 (*pos)->GetIndexID(),
470 Mutex::Locker locker(GetMutex());
473 m_selected_tid = LLDB_INVALID_THREAD_ID;
477 ThreadList::Destroy()
479 Mutex::Locker locker(GetMutex());
480 const uint32_t num_threads = m_threads.size();
481 for (uint32_t idx = 0; idx < num_threads; ++idx)
483 m_threads[idx]->DestroyThread();
488 ThreadList::RefreshStateAfterStop ()
490 Mutex::Locker locker(GetMutex());
492 m_process->UpdateThreadListIfNeeded();
494 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
495 if (log && log->GetVerbose())
496 log->Printf ("Turning off notification of new threads while single stepping a thread.");
498 collection::iterator pos, end = m_threads.end();
499 for (pos = m_threads.begin(); pos != end; ++pos)
500 (*pos)->RefreshStateAfterStop ();
504 ThreadList::DiscardThreadPlans ()
506 // You don't need to update the thread list here, because only threads
507 // that you currently know about have any thread plans.
508 Mutex::Locker locker(GetMutex());
510 collection::iterator pos, end = m_threads.end();
511 for (pos = m_threads.begin(); pos != end; ++pos)
512 (*pos)->DiscardThreadPlans (true);
517 ThreadList::WillResume ()
519 // Run through the threads and perform their momentary actions.
520 // But we only do this for threads that are running, user suspended
521 // threads stay where they are.
523 Mutex::Locker locker(GetMutex());
524 m_process->UpdateThreadListIfNeeded();
526 collection::iterator pos, end = m_threads.end();
528 // See if any thread wants to run stopping others. If it does, then we won't
529 // setup the other threads for resume, since they aren't going to get a chance
530 // to run. This is necessary because the SetupForResume might add "StopOthers"
531 // plans which would then get to be part of the who-gets-to-run negotiation, but
532 // they're coming in after the fact, and the threads that are already set up should
535 bool wants_solo_run = false;
537 for (pos = m_threads.begin(); pos != end; ++pos)
539 if ((*pos)->GetResumeState() != eStateSuspended &&
540 (*pos)->GetCurrentPlan()->StopOthers())
542 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
544 wants_solo_run = true;
551 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
552 if (log && log->GetVerbose())
553 log->Printf ("Turning on notification of new threads while single stepping a thread.");
554 m_process->StartNoticingNewThreads();
558 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
559 if (log && log->GetVerbose())
560 log->Printf ("Turning off notification of new threads while single stepping a thread.");
561 m_process->StopNoticingNewThreads();
564 // Give all the threads that are likely to run a last chance to set up their state before we
565 // negotiate who is actually going to get a chance to run...
566 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
567 // call setup on the threads that request StopOthers...
569 for (pos = m_threads.begin(); pos != end; ++pos)
571 if ((*pos)->GetResumeState() != eStateSuspended
572 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
574 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
576 (*pos)->SetupForResume ();
580 // Now go through the threads and see if any thread wants to run just itself.
581 // if so then pick one and run it.
583 ThreadList run_me_only_list (m_process);
585 run_me_only_list.SetStopID(m_process->GetStopID());
587 bool run_only_current_thread = false;
589 for (pos = m_threads.begin(); pos != end; ++pos)
591 ThreadSP thread_sp(*pos);
592 if (thread_sp->GetResumeState() != eStateSuspended &&
593 thread_sp->GetCurrentPlan()->StopOthers())
595 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
598 // You can't say "stop others" and also want yourself to be suspended.
599 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
601 if (thread_sp == GetSelectedThread())
603 run_only_current_thread = true;
604 run_me_only_list.Clear();
605 run_me_only_list.AddThread (thread_sp);
609 run_me_only_list.AddThread (thread_sp);
614 bool need_to_resume = true;
616 if (run_me_only_list.GetSize (false) == 0)
618 // Everybody runs as they wish:
619 for (pos = m_threads.begin(); pos != end; ++pos)
621 ThreadSP thread_sp(*pos);
623 if (thread_sp->GetResumeState() != eStateSuspended)
624 run_state = thread_sp->GetCurrentPlan()->RunState();
626 run_state = eStateSuspended;
627 if (!thread_sp->ShouldResume(run_state))
628 need_to_resume = false;
633 ThreadSP thread_to_run;
635 if (run_only_current_thread)
637 thread_to_run = GetSelectedThread();
639 else if (run_me_only_list.GetSize (false) == 1)
641 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
645 int random_thread = (int)
646 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
647 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
650 for (pos = m_threads.begin(); pos != end; ++pos)
652 ThreadSP thread_sp(*pos);
653 if (thread_sp == thread_to_run)
655 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
656 need_to_resume = false;
659 thread_sp->ShouldResume (eStateSuspended);
663 return need_to_resume;
667 ThreadList::DidResume ()
669 Mutex::Locker locker(GetMutex());
670 collection::iterator pos, end = m_threads.end();
671 for (pos = m_threads.begin(); pos != end; ++pos)
673 // Don't clear out threads that aren't going to get a chance to run, rather
674 // leave their state for the next time around.
675 ThreadSP thread_sp(*pos);
676 if (thread_sp->GetResumeState() != eStateSuspended)
677 thread_sp->DidResume ();
682 ThreadList::DidStop ()
684 Mutex::Locker locker(GetMutex());
685 collection::iterator pos, end = m_threads.end();
686 for (pos = m_threads.begin(); pos != end; ++pos)
688 // Notify threads that the process just stopped.
689 // Note, this currently assumes that all threads in the list
690 // stop when the process stops. In the future we will want to support
691 // a debugging model where some threads continue to run while others
692 // are stopped. We either need to handle that somehow here or
693 // create a special thread list containing only threads which will
694 // stop in the code that calls this method (currently
695 // Process::SetPrivateState).
696 ThreadSP thread_sp(*pos);
697 if (StateIsRunningState(thread_sp->GetState()))
698 thread_sp->DidStop ();
703 ThreadList::GetSelectedThread ()
705 Mutex::Locker locker(GetMutex());
706 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
707 if (!thread_sp.get())
709 if (m_threads.size() == 0)
711 m_selected_tid = m_threads[0]->GetID();
712 thread_sp = m_threads[0];
718 ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
720 Mutex::Locker locker(GetMutex());
721 ThreadSP selected_thread_sp(FindThreadByID(tid));
722 if (selected_thread_sp)
724 m_selected_tid = tid;
725 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
728 m_selected_tid = LLDB_INVALID_THREAD_ID;
731 NotifySelectedThreadChanged(m_selected_tid);
733 return m_selected_tid != LLDB_INVALID_THREAD_ID;
737 ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify)
739 Mutex::Locker locker(GetMutex());
740 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
741 if (selected_thread_sp.get())
743 m_selected_tid = selected_thread_sp->GetID();
744 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
747 m_selected_tid = LLDB_INVALID_THREAD_ID;
750 NotifySelectedThreadChanged(m_selected_tid);
752 return m_selected_tid != LLDB_INVALID_THREAD_ID;
756 ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid)
758 ThreadSP selected_thread_sp (FindThreadByID(tid));
759 if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
760 selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
761 new Thread::ThreadEventData(selected_thread_sp));
765 ThreadList::Update (ThreadList &rhs)
769 // Lock both mutexes to make sure neither side changes anyone on us
770 // while the assignement occurs
771 Mutex::Locker locker(GetMutex());
772 m_process = rhs.m_process;
773 m_stop_id = rhs.m_stop_id;
774 m_threads.swap(rhs.m_threads);
775 m_selected_tid = rhs.m_selected_tid;
778 // Now we look for threads that we are done with and
779 // make sure to clear them up as much as possible so
780 // anyone with a shared pointer will still have a reference,
781 // but the thread won't be of much use. Using std::weak_ptr
782 // for all backward references (such as a thread to a process)
783 // will eventually solve this issue for us, but for now, we
784 // need to work around the issue
785 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
786 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
788 const lldb::tid_t tid = (*rhs_pos)->GetID();
789 bool thread_is_alive = false;
790 const uint32_t num_threads = m_threads.size();
791 for (uint32_t idx = 0; idx < num_threads; ++idx)
793 if (m_threads[idx]->GetID() == tid)
795 thread_is_alive = true;
799 if (!thread_is_alive)
800 (*rhs_pos)->DestroyThread();
808 Mutex::Locker locker(GetMutex());
809 collection::iterator pos, end = m_threads.end();
810 for (pos = m_threads.begin(); pos != end; ++pos)
815 ThreadList::GetMutex ()
817 return m_process->m_thread_mutex;