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) :
34 m_process (rhs.m_process),
35 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)
81 ThreadList::GetSize (bool can_update)
83 Mutex::Locker locker(GetMutex());
85 m_process->UpdateThreadListIfNeeded();
86 return m_threads.size();
90 ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
92 Mutex::Locker locker(GetMutex());
94 m_process->UpdateThreadListIfNeeded();
97 if (idx < m_threads.size())
98 thread_sp = m_threads[idx];
103 ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
105 Mutex::Locker locker(GetMutex());
108 m_process->UpdateThreadListIfNeeded();
112 const uint32_t num_threads = m_threads.size();
113 for (idx = 0; idx < num_threads; ++idx)
115 if (m_threads[idx]->GetID() == tid)
117 thread_sp = m_threads[idx];
125 ThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update)
127 Mutex::Locker locker(GetMutex());
130 m_process->UpdateThreadListIfNeeded();
134 const uint32_t num_threads = m_threads.size();
135 for (idx = 0; idx < num_threads; ++idx)
137 if (m_threads[idx]->GetProtocolID() == tid)
139 thread_sp = m_threads[idx];
148 ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
150 Mutex::Locker locker(GetMutex());
153 m_process->UpdateThreadListIfNeeded();
157 const uint32_t num_threads = m_threads.size();
158 for (idx = 0; idx < num_threads; ++idx)
160 if (m_threads[idx]->GetID() == tid)
162 thread_sp = m_threads[idx];
163 m_threads.erase(m_threads.begin()+idx);
171 ThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update)
173 Mutex::Locker locker(GetMutex());
176 m_process->UpdateThreadListIfNeeded();
180 const uint32_t num_threads = m_threads.size();
181 for (idx = 0; idx < num_threads; ++idx)
183 if (m_threads[idx]->GetProtocolID() == tid)
185 thread_sp = m_threads[idx];
186 m_threads.erase(m_threads.begin()+idx);
194 ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
199 Mutex::Locker locker(GetMutex());
202 const uint32_t num_threads = m_threads.size();
203 for (idx = 0; idx < num_threads; ++idx)
205 if (m_threads[idx].get() == thread_ptr)
207 thread_sp = m_threads[idx];
218 ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
220 Mutex::Locker locker(GetMutex());
223 m_process->UpdateThreadListIfNeeded();
226 const uint32_t num_threads = m_threads.size();
227 for (uint32_t idx = 0; idx < num_threads; ++idx)
229 if (m_threads[idx]->GetIndexID() == index_id)
231 thread_sp = m_threads[idx];
239 ThreadList::ShouldStop (Event *event_ptr)
241 // Running events should never stop, obviously...
243 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
245 // The ShouldStop method of the threads can do a whole lot of work,
246 // figuring out whether the thread plan conditions are met. So we don't want
247 // to keep the ThreadList locked the whole time we are doing this.
248 // FIXME: It is possible that running code could cause new threads
249 // to be created. If that happens we will miss asking them whether
250 // then should stop. This is not a big deal, since we haven't had
251 // a chance to hang any interesting operations on those threads yet.
253 collection threads_copy;
256 Mutex::Locker locker(GetMutex());
258 m_process->UpdateThreadListIfNeeded();
259 threads_copy = m_threads;
262 collection::iterator pos, end = threads_copy.end();
267 log->Printf ("ThreadList::%s: %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
270 bool did_anybody_stop_for_a_reason = false;
272 // If the event is an Interrupt event, then we're going to stop no matter what. Otherwise, presume we won't stop.
273 bool should_stop = false;
274 if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr))
277 log->Printf("ThreadList::%s handling interrupt event, should stop set to true", __FUNCTION__);
282 // Now we run through all the threads and get their stop info's. We want to make sure to do this first before
283 // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a
284 // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly.
285 // We don't need to use it here, we just want to make sure it gets computed.
287 for (pos = threads_copy.begin(); pos != end; ++pos)
289 ThreadSP thread_sp(*pos);
290 thread_sp->GetStopInfo();
293 for (pos = threads_copy.begin(); pos != end; ++pos)
295 ThreadSP thread_sp(*pos);
297 // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
298 // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out
299 // the right thing to do and stopping gives the user control over what to do in this instance.
301 // Note, this causes a problem when you have a thread specific breakpoint, and a bunch of threads hit the breakpoint,
302 // but not the thread which we are waiting for. All the threads that are not "supposed" to hit the breakpoint
303 // are marked as having no stop reason, which is right, they should not show a stop reason. But that triggers this
304 // code and causes us to stop seemingly for no reason.
306 // 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
307 // to true unless this is the first stop.
309 // If this becomes a problem, we'll have to have another StopReason like "StopInfoHidden" which will look invalid
310 // everywhere but at this check.
312 if (thread_sp->GetProcess()->GetStopID() > 1)
313 did_anybody_stop_for_a_reason = true;
315 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
317 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
318 if (thread_should_stop)
322 if (!should_stop && !did_anybody_stop_for_a_reason)
326 log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
330 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
334 for (pos = threads_copy.begin(); pos != end; ++pos)
336 ThreadSP thread_sp(*pos);
337 thread_sp->WillStop ();
345 ThreadList::ShouldReportStop (Event *event_ptr)
347 Mutex::Locker locker(GetMutex());
349 Vote result = eVoteNoOpinion;
350 m_process->UpdateThreadListIfNeeded();
351 collection::iterator pos, end = m_threads.end();
353 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
356 log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
358 // Run through the threads and ask whether we should report this event.
359 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
360 for (pos = m_threads.begin(); pos != end; ++pos)
362 ThreadSP thread_sp(*pos);
363 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
374 if (result == eVoteNoOpinion)
381 log->Printf ("ThreadList::%s thread 0x%4.4" PRIx64 ": voted %s, but lost out because result was %s",
384 GetVoteAsCString (vote),
385 GetVoteAsCString (result));
391 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
396 ThreadList::SetShouldReportStop (Vote vote)
398 Mutex::Locker locker(GetMutex());
399 m_process->UpdateThreadListIfNeeded();
400 collection::iterator pos, end = m_threads.end();
401 for (pos = m_threads.begin(); pos != end; ++pos)
403 ThreadSP thread_sp(*pos);
404 thread_sp->SetShouldReportStop (vote);
409 ThreadList::ShouldReportRun (Event *event_ptr)
412 Mutex::Locker locker(GetMutex());
414 Vote result = eVoteNoOpinion;
415 m_process->UpdateThreadListIfNeeded();
416 collection::iterator pos, end = m_threads.end();
418 // Run through the threads and ask whether we should report this event.
419 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
421 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
423 for (pos = m_threads.begin(); pos != end; ++pos)
425 if ((*pos)->GetResumeState () != eStateSuspended)
427 switch ((*pos)->ShouldReportRun (event_ptr))
432 if (result == eVoteNoOpinion)
437 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
438 (*pos)->GetIndexID(),
451 Mutex::Locker locker(GetMutex());
454 m_selected_tid = LLDB_INVALID_THREAD_ID;
458 ThreadList::Destroy()
460 Mutex::Locker locker(GetMutex());
461 const uint32_t num_threads = m_threads.size();
462 for (uint32_t idx = 0; idx < num_threads; ++idx)
464 m_threads[idx]->DestroyThread();
469 ThreadList::RefreshStateAfterStop ()
471 Mutex::Locker locker(GetMutex());
473 m_process->UpdateThreadListIfNeeded();
475 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
476 if (log && log->GetVerbose())
477 log->Printf ("Turning off notification of new threads while single stepping a thread.");
479 collection::iterator pos, end = m_threads.end();
480 for (pos = m_threads.begin(); pos != end; ++pos)
481 (*pos)->RefreshStateAfterStop ();
485 ThreadList::DiscardThreadPlans ()
487 // You don't need to update the thread list here, because only threads
488 // that you currently know about have any thread plans.
489 Mutex::Locker locker(GetMutex());
491 collection::iterator pos, end = m_threads.end();
492 for (pos = m_threads.begin(); pos != end; ++pos)
493 (*pos)->DiscardThreadPlans (true);
498 ThreadList::WillResume ()
500 // Run through the threads and perform their momentary actions.
501 // But we only do this for threads that are running, user suspended
502 // threads stay where they are.
504 Mutex::Locker locker(GetMutex());
505 m_process->UpdateThreadListIfNeeded();
507 collection::iterator pos, end = m_threads.end();
509 // See if any thread wants to run stopping others. If it does, then we won't
510 // setup the other threads for resume, since they aren't going to get a chance
511 // to run. This is necessary because the SetupForResume might add "StopOthers"
512 // plans which would then get to be part of the who-gets-to-run negotiation, but
513 // they're coming in after the fact, and the threads that are already set up should
516 bool wants_solo_run = false;
518 for (pos = m_threads.begin(); pos != end; ++pos)
520 if ((*pos)->GetResumeState() != eStateSuspended &&
521 (*pos)->GetCurrentPlan()->StopOthers())
523 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
525 wants_solo_run = true;
532 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
533 if (log && log->GetVerbose())
534 log->Printf ("Turning on notification of new threads while single stepping a thread.");
535 m_process->StartNoticingNewThreads();
539 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
540 if (log && log->GetVerbose())
541 log->Printf ("Turning off notification of new threads while single stepping a thread.");
542 m_process->StopNoticingNewThreads();
545 // Give all the threads that are likely to run a last chance to set up their state before we
546 // negotiate who is actually going to get a chance to run...
547 // Don't set to resume suspended threads, and if any thread wanted to stop others, only
548 // call setup on the threads that request StopOthers...
550 for (pos = m_threads.begin(); pos != end; ++pos)
552 if ((*pos)->GetResumeState() != eStateSuspended
553 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
555 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
557 (*pos)->SetupForResume ();
561 // Now go through the threads and see if any thread wants to run just itself.
562 // if so then pick one and run it.
564 ThreadList run_me_only_list (m_process);
566 run_me_only_list.SetStopID(m_process->GetStopID());
568 bool run_only_current_thread = false;
570 for (pos = m_threads.begin(); pos != end; ++pos)
572 ThreadSP thread_sp(*pos);
573 if (thread_sp->GetResumeState() != eStateSuspended &&
574 thread_sp->GetCurrentPlan()->StopOthers())
576 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
579 // You can't say "stop others" and also want yourself to be suspended.
580 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
582 if (thread_sp == GetSelectedThread())
584 run_only_current_thread = true;
585 run_me_only_list.Clear();
586 run_me_only_list.AddThread (thread_sp);
590 run_me_only_list.AddThread (thread_sp);
595 bool need_to_resume = true;
597 if (run_me_only_list.GetSize (false) == 0)
599 // Everybody runs as they wish:
600 for (pos = m_threads.begin(); pos != end; ++pos)
602 ThreadSP thread_sp(*pos);
604 if (thread_sp->GetResumeState() != eStateSuspended)
605 run_state = thread_sp->GetCurrentPlan()->RunState();
607 run_state = eStateSuspended;
608 if (!thread_sp->ShouldResume(run_state))
609 need_to_resume = false;
614 ThreadSP thread_to_run;
616 if (run_only_current_thread)
618 thread_to_run = GetSelectedThread();
620 else if (run_me_only_list.GetSize (false) == 1)
622 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
626 int random_thread = (int)
627 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
628 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
631 for (pos = m_threads.begin(); pos != end; ++pos)
633 ThreadSP thread_sp(*pos);
634 if (thread_sp == thread_to_run)
636 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
637 need_to_resume = false;
640 thread_sp->ShouldResume (eStateSuspended);
644 return need_to_resume;
648 ThreadList::DidResume ()
650 Mutex::Locker locker(GetMutex());
651 collection::iterator pos, end = m_threads.end();
652 for (pos = m_threads.begin(); pos != end; ++pos)
654 // Don't clear out threads that aren't going to get a chance to run, rather
655 // leave their state for the next time around.
656 ThreadSP thread_sp(*pos);
657 if (thread_sp->GetResumeState() != eStateSuspended)
658 thread_sp->DidResume ();
663 ThreadList::DidStop ()
665 Mutex::Locker locker(GetMutex());
666 collection::iterator pos, end = m_threads.end();
667 for (pos = m_threads.begin(); pos != end; ++pos)
669 // Notify threads that the process just stopped.
670 // Note, this currently assumes that all threads in the list
671 // stop when the process stops. In the future we will want to support
672 // a debugging model where some threads continue to run while others
673 // are stopped. We either need to handle that somehow here or
674 // create a special thread list containing only threads which will
675 // 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 ();
684 ThreadList::GetSelectedThread ()
686 Mutex::Locker locker(GetMutex());
687 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
688 if (!thread_sp.get())
690 if (m_threads.size() == 0)
692 m_selected_tid = m_threads[0]->GetID();
693 thread_sp = m_threads[0];
699 ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
701 Mutex::Locker locker(GetMutex());
702 ThreadSP selected_thread_sp(FindThreadByID(tid));
703 if (selected_thread_sp)
705 m_selected_tid = tid;
706 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
709 m_selected_tid = LLDB_INVALID_THREAD_ID;
712 NotifySelectedThreadChanged(m_selected_tid);
714 return m_selected_tid != LLDB_INVALID_THREAD_ID;
718 ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify)
720 Mutex::Locker locker(GetMutex());
721 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
722 if (selected_thread_sp.get())
724 m_selected_tid = selected_thread_sp->GetID();
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::NotifySelectedThreadChanged (lldb::tid_t tid)
739 ThreadSP selected_thread_sp (FindThreadByID(tid));
740 if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
741 selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
742 new Thread::ThreadEventData(selected_thread_sp));
746 ThreadList::Update (ThreadList &rhs)
750 // Lock both mutexes to make sure neither side changes anyone on us
751 // while the assignement occurs
752 Mutex::Locker locker(GetMutex());
753 m_process = rhs.m_process;
754 m_stop_id = rhs.m_stop_id;
755 m_threads.swap(rhs.m_threads);
756 m_selected_tid = rhs.m_selected_tid;
759 // Now we look for threads that we are done with and
760 // make sure to clear them up as much as possible so
761 // anyone with a shared pointer will still have a reference,
762 // but the thread won't be of much use. Using std::weak_ptr
763 // for all backward references (such as a thread to a process)
764 // will eventually solve this issue for us, but for now, we
765 // need to work around the issue
766 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
767 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
769 const lldb::tid_t tid = (*rhs_pos)->GetID();
770 bool thread_is_alive = false;
771 const uint32_t num_threads = m_threads.size();
772 for (uint32_t idx = 0; idx < num_threads; ++idx)
774 if (m_threads[idx]->GetID() == tid)
776 thread_is_alive = true;
780 if (!thread_is_alive)
781 (*rhs_pos)->DestroyThread();
789 Mutex::Locker locker(GetMutex());
790 collection::iterator pos, end = m_threads.end();
791 for (pos = m_threads.begin(); pos != end; ++pos)
796 ThreadList::GetMutex ()
798 return m_process->m_thread_mutex;