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::GetSize (bool can_update)
91 Mutex::Locker locker(GetMutex());
93 m_process->UpdateThreadListIfNeeded();
94 return m_threads.size();
98 ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
100 Mutex::Locker locker(GetMutex());
102 m_process->UpdateThreadListIfNeeded();
105 if (idx < m_threads.size())
106 thread_sp = m_threads[idx];
111 ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
113 Mutex::Locker locker(GetMutex());
116 m_process->UpdateThreadListIfNeeded();
120 const uint32_t num_threads = m_threads.size();
121 for (idx = 0; idx < num_threads; ++idx)
123 if (m_threads[idx]->GetID() == tid)
125 thread_sp = m_threads[idx];
133 ThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update)
135 Mutex::Locker locker(GetMutex());
138 m_process->UpdateThreadListIfNeeded();
142 const uint32_t num_threads = m_threads.size();
143 for (idx = 0; idx < num_threads; ++idx)
145 if (m_threads[idx]->GetProtocolID() == tid)
147 thread_sp = m_threads[idx];
156 ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
158 Mutex::Locker locker(GetMutex());
161 m_process->UpdateThreadListIfNeeded();
165 const uint32_t num_threads = m_threads.size();
166 for (idx = 0; idx < num_threads; ++idx)
168 if (m_threads[idx]->GetID() == tid)
170 thread_sp = m_threads[idx];
171 m_threads.erase(m_threads.begin()+idx);
179 ThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update)
181 Mutex::Locker locker(GetMutex());
184 m_process->UpdateThreadListIfNeeded();
188 const uint32_t num_threads = m_threads.size();
189 for (idx = 0; idx < num_threads; ++idx)
191 if (m_threads[idx]->GetProtocolID() == tid)
193 thread_sp = m_threads[idx];
194 m_threads.erase(m_threads.begin()+idx);
202 ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
207 Mutex::Locker locker(GetMutex());
210 const uint32_t num_threads = m_threads.size();
211 for (idx = 0; idx < num_threads; ++idx)
213 if (m_threads[idx].get() == thread_ptr)
215 thread_sp = m_threads[idx];
226 ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
228 Mutex::Locker locker(GetMutex());
231 m_process->UpdateThreadListIfNeeded();
234 const uint32_t num_threads = m_threads.size();
235 for (uint32_t idx = 0; idx < num_threads; ++idx)
237 if (m_threads[idx]->GetIndexID() == index_id)
239 thread_sp = m_threads[idx];
247 ThreadList::ShouldStop (Event *event_ptr)
249 // Running events should never stop, obviously...
251 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
253 // The ShouldStop method of the threads can do a whole lot of work,
254 // running breakpoint commands & conditions, etc. So we don't want
255 // to keep the ThreadList locked the whole time we are doing this.
256 // FIXME: It is possible that running code could cause new threads
257 // to be created. If that happens we will miss asking them whether
258 // then should stop. This is not a big deal, since we haven't had
259 // a chance to hang any interesting operations on those threads yet.
261 collection threads_copy;
264 Mutex::Locker locker(GetMutex());
266 m_process->UpdateThreadListIfNeeded();
267 threads_copy = m_threads;
270 collection::iterator pos, end = threads_copy.end();
275 log->Printf ("ThreadList::%s: %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
278 bool did_anybody_stop_for_a_reason = false;
279 bool should_stop = false;
281 // Now we run through all the threads and get their stop info's. We want to make sure to do this first before
282 // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a
283 // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly.
284 // We don't need to use it here, we just want to make sure it gets computed.
286 for (pos = threads_copy.begin(); pos != end; ++pos)
288 ThreadSP thread_sp(*pos);
289 thread_sp->GetStopInfo();
292 for (pos = threads_copy.begin(); pos != end; ++pos)
294 ThreadSP thread_sp(*pos);
296 did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
298 const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
299 if (thread_should_stop)
303 // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
304 // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out
305 // the right thing to do and stopping gives the user control over what to do in this instance.
307 if (!should_stop && !did_anybody_stop_for_a_reason)
311 log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
315 log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
319 for (pos = threads_copy.begin(); pos != end; ++pos)
321 ThreadSP thread_sp(*pos);
322 thread_sp->WillStop ();
330 ThreadList::ShouldReportStop (Event *event_ptr)
332 Mutex::Locker locker(GetMutex());
334 Vote result = eVoteNoOpinion;
335 m_process->UpdateThreadListIfNeeded();
336 collection::iterator pos, end = m_threads.end();
338 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
341 log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
343 // Run through the threads and ask whether we should report this event.
344 // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion.
345 for (pos = m_threads.begin(); pos != end; ++pos)
347 ThreadSP thread_sp(*pos);
348 const Vote vote = thread_sp->ShouldReportStop (event_ptr);
359 if (result == eVoteNoOpinion)
366 log->Printf ("ThreadList::%s thread 0x%4.4" PRIx64 ": voted %s, but lost out because result was %s",
369 GetVoteAsCString (vote),
370 GetVoteAsCString (result));
376 log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
381 ThreadList::SetShouldReportStop (Vote vote)
383 Mutex::Locker locker(GetMutex());
384 m_process->UpdateThreadListIfNeeded();
385 collection::iterator pos, end = m_threads.end();
386 for (pos = m_threads.begin(); pos != end; ++pos)
388 ThreadSP thread_sp(*pos);
389 thread_sp->SetShouldReportStop (vote);
394 ThreadList::ShouldReportRun (Event *event_ptr)
397 Mutex::Locker locker(GetMutex());
399 Vote result = eVoteNoOpinion;
400 m_process->UpdateThreadListIfNeeded();
401 collection::iterator pos, end = m_threads.end();
403 // Run through the threads and ask whether we should report this event.
404 // The rule is NO vote wins over everything, a YES vote wins over no opinion.
406 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
408 for (pos = m_threads.begin(); pos != end; ++pos)
410 if ((*pos)->GetResumeState () != eStateSuspended)
412 switch ((*pos)->ShouldReportRun (event_ptr))
417 if (result == eVoteNoOpinion)
422 log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
423 (*pos)->GetIndexID(),
436 Mutex::Locker locker(GetMutex());
439 m_selected_tid = LLDB_INVALID_THREAD_ID;
443 ThreadList::Destroy()
445 Mutex::Locker locker(GetMutex());
446 const uint32_t num_threads = m_threads.size();
447 for (uint32_t idx = 0; idx < num_threads; ++idx)
449 m_threads[idx]->DestroyThread();
454 ThreadList::RefreshStateAfterStop ()
456 Mutex::Locker locker(GetMutex());
458 m_process->UpdateThreadListIfNeeded();
460 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
461 if (log && log->GetVerbose())
462 log->Printf ("Turning off notification of new threads while single stepping a thread.");
464 collection::iterator pos, end = m_threads.end();
465 for (pos = m_threads.begin(); pos != end; ++pos)
466 (*pos)->RefreshStateAfterStop ();
470 ThreadList::DiscardThreadPlans ()
472 // You don't need to update the thread list here, because only threads
473 // that you currently know about have any thread plans.
474 Mutex::Locker locker(GetMutex());
476 collection::iterator pos, end = m_threads.end();
477 for (pos = m_threads.begin(); pos != end; ++pos)
478 (*pos)->DiscardThreadPlans (true);
483 ThreadList::WillResume ()
485 // Run through the threads and perform their momentary actions.
486 // But we only do this for threads that are running, user suspended
487 // threads stay where they are.
489 Mutex::Locker locker(GetMutex());
490 m_process->UpdateThreadListIfNeeded();
492 collection::iterator pos, end = m_threads.end();
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 chance
496 // to run. This is necessary because the SetupForResume might add "StopOthers"
497 // plans which would then get to be part of the who-gets-to-run negotiation, but
498 // they're coming in after the fact, and the threads that are already set up should
501 bool wants_solo_run = false;
503 for (pos = m_threads.begin(); pos != end; ++pos)
505 if ((*pos)->GetResumeState() != eStateSuspended &&
506 (*pos)->GetCurrentPlan()->StopOthers())
508 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
510 wants_solo_run = true;
517 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
518 if (log && log->GetVerbose())
519 log->Printf ("Turning on notification of new threads while single stepping a thread.");
520 m_process->StartNoticingNewThreads();
524 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
525 if (log && log->GetVerbose())
526 log->Printf ("Turning off notification of new threads while single stepping a thread.");
527 m_process->StopNoticingNewThreads();
530 // Give all the threads that are likely to run a last chance to set up their state before we
531 // 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 others, only
533 // call setup on the threads that request StopOthers...
535 for (pos = m_threads.begin(); pos != end; ++pos)
537 if ((*pos)->GetResumeState() != eStateSuspended
538 && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
540 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
542 (*pos)->SetupForResume ();
546 // Now go through the threads and see if any thread wants to run just itself.
547 // if so then pick one and run it.
549 ThreadList run_me_only_list (m_process);
551 run_me_only_list.SetStopID(m_process->GetStopID());
553 bool run_only_current_thread = false;
555 for (pos = m_threads.begin(); pos != end; ++pos)
557 ThreadSP thread_sp(*pos);
558 if (thread_sp->GetResumeState() != eStateSuspended &&
559 thread_sp->GetCurrentPlan()->StopOthers())
561 if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
564 // You can't say "stop others" and also want yourself to be suspended.
565 assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
567 if (thread_sp == GetSelectedThread())
569 run_only_current_thread = true;
570 run_me_only_list.Clear();
571 run_me_only_list.AddThread (thread_sp);
575 run_me_only_list.AddThread (thread_sp);
580 bool need_to_resume = true;
582 if (run_me_only_list.GetSize (false) == 0)
584 // Everybody runs as they wish:
585 for (pos = m_threads.begin(); pos != end; ++pos)
587 ThreadSP thread_sp(*pos);
589 if (thread_sp->GetResumeState() != eStateSuspended)
590 run_state = thread_sp->GetCurrentPlan()->RunState();
592 run_state = eStateSuspended;
593 if (!thread_sp->ShouldResume(run_state))
594 need_to_resume = false;
599 ThreadSP thread_to_run;
601 if (run_only_current_thread)
603 thread_to_run = GetSelectedThread();
605 else if (run_me_only_list.GetSize (false) == 1)
607 thread_to_run = run_me_only_list.GetThreadAtIndex (0);
611 int random_thread = (int)
612 ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
613 thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
616 for (pos = m_threads.begin(); pos != end; ++pos)
618 ThreadSP thread_sp(*pos);
619 if (thread_sp == thread_to_run)
621 if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
622 need_to_resume = false;
625 thread_sp->ShouldResume (eStateSuspended);
629 return need_to_resume;
633 ThreadList::DidResume ()
635 Mutex::Locker locker(GetMutex());
636 collection::iterator pos, end = m_threads.end();
637 for (pos = m_threads.begin(); pos != end; ++pos)
639 // Don't clear out threads that aren't going to get a chance to run, rather
640 // leave their state for the next time around.
641 ThreadSP thread_sp(*pos);
642 if (thread_sp->GetResumeState() != eStateSuspended)
643 thread_sp->DidResume ();
648 ThreadList::DidStop ()
650 Mutex::Locker locker(GetMutex());
651 collection::iterator pos, end = m_threads.end();
652 for (pos = m_threads.begin(); pos != end; ++pos)
654 // Notify threads that the process just stopped.
655 // Note, this currently assumes that all threads in the list
656 // stop when the process stops. In the future we will want to support
657 // a debugging model where some threads continue to run while others
658 // are stopped. We either need to handle that somehow here or
659 // create a special thread list containing only threads which will
660 // stop in the code that calls this method (currently
661 // Process::SetPrivateState).
662 ThreadSP thread_sp(*pos);
663 if (StateIsRunningState(thread_sp->GetState()))
664 thread_sp->DidStop ();
669 ThreadList::GetSelectedThread ()
671 Mutex::Locker locker(GetMutex());
672 ThreadSP thread_sp = FindThreadByID(m_selected_tid);
673 if (!thread_sp.get())
675 if (m_threads.size() == 0)
677 m_selected_tid = m_threads[0]->GetID();
678 thread_sp = m_threads[0];
684 ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
686 Mutex::Locker locker(GetMutex());
687 ThreadSP selected_thread_sp(FindThreadByID(tid));
688 if (selected_thread_sp)
690 m_selected_tid = tid;
691 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
694 m_selected_tid = LLDB_INVALID_THREAD_ID;
697 NotifySelectedThreadChanged(m_selected_tid);
699 return m_selected_tid != LLDB_INVALID_THREAD_ID;
703 ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify)
705 Mutex::Locker locker(GetMutex());
706 ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
707 if (selected_thread_sp.get())
709 m_selected_tid = selected_thread_sp->GetID();
710 selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
713 m_selected_tid = LLDB_INVALID_THREAD_ID;
716 NotifySelectedThreadChanged(m_selected_tid);
718 return m_selected_tid != LLDB_INVALID_THREAD_ID;
722 ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid)
724 ThreadSP selected_thread_sp (FindThreadByID(tid));
725 if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
726 selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
727 new Thread::ThreadEventData(selected_thread_sp));
731 ThreadList::Update (ThreadList &rhs)
735 // Lock both mutexes to make sure neither side changes anyone on us
736 // while the assignement occurs
737 Mutex::Locker locker(GetMutex());
738 m_process = rhs.m_process;
739 m_stop_id = rhs.m_stop_id;
740 m_threads.swap(rhs.m_threads);
741 m_selected_tid = rhs.m_selected_tid;
744 // Now we look for threads that we are done with and
745 // make sure to clear them up as much as possible so
746 // anyone with a shared pointer will still have a reference,
747 // but the thread won't be of much use. Using std::weak_ptr
748 // for all backward references (such as a thread to a process)
749 // will eventually solve this issue for us, but for now, we
750 // need to work around the issue
751 collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
752 for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
754 const lldb::tid_t tid = (*rhs_pos)->GetID();
755 bool thread_is_alive = false;
756 const uint32_t num_threads = m_threads.size();
757 for (uint32_t idx = 0; idx < num_threads; ++idx)
759 if (m_threads[idx]->GetID() == tid)
761 thread_is_alive = true;
765 if (!thread_is_alive)
766 (*rhs_pos)->DestroyThread();
774 Mutex::Locker locker(GetMutex());
775 collection::iterator pos, end = m_threads.end();
776 for (pos = m_threads.begin(); pos != end; ++pos)
781 ThreadList::GetMutex ()
783 return m_process->m_thread_mutex;