1254721Semaste//===-- ThreadList.cpp ------------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste#include <stdlib.h>
10254721Semaste
11254721Semaste#include <algorithm>
12254721Semaste
13254721Semaste#include "lldb/Core/Log.h"
14254721Semaste#include "lldb/Core/State.h"
15254721Semaste#include "lldb/Target/RegisterContext.h"
16254721Semaste#include "lldb/Target/ThreadList.h"
17254721Semaste#include "lldb/Target/Thread.h"
18254721Semaste#include "lldb/Target/ThreadPlan.h"
19254721Semaste#include "lldb/Target/Process.h"
20254721Semaste
21254721Semasteusing namespace lldb;
22254721Semasteusing namespace lldb_private;
23254721Semaste
24254721SemasteThreadList::ThreadList (Process *process) :
25254721Semaste    m_process (process),
26254721Semaste    m_stop_id (0),
27254721Semaste    m_threads(),
28254721Semaste    m_selected_tid (LLDB_INVALID_THREAD_ID)
29254721Semaste{
30254721Semaste}
31254721Semaste
32254721SemasteThreadList::ThreadList (const ThreadList &rhs) :
33254721Semaste    m_process (rhs.m_process),
34254721Semaste    m_stop_id (rhs.m_stop_id),
35254721Semaste    m_threads (),
36254721Semaste    m_selected_tid ()
37254721Semaste{
38254721Semaste    // Use the assignment operator since it uses the mutex
39254721Semaste    *this = rhs;
40254721Semaste}
41254721Semaste
42254721Semasteconst ThreadList&
43254721SemasteThreadList::operator = (const ThreadList& rhs)
44254721Semaste{
45254721Semaste    if (this != &rhs)
46254721Semaste    {
47254721Semaste        // Lock both mutexes to make sure neither side changes anyone on us
48254721Semaste        // while the assignement occurs
49254721Semaste        Mutex::Locker locker(GetMutex());
50254721Semaste        m_process = rhs.m_process;
51254721Semaste        m_stop_id = rhs.m_stop_id;
52254721Semaste        m_threads = rhs.m_threads;
53254721Semaste        m_selected_tid = rhs.m_selected_tid;
54254721Semaste    }
55254721Semaste    return *this;
56254721Semaste}
57254721Semaste
58254721Semaste
59254721SemasteThreadList::~ThreadList()
60254721Semaste{
61254721Semaste    // Clear the thread list. Clear will take the mutex lock
62254721Semaste    // which will ensure that if anyone is using the list
63254721Semaste    // they won't get it removed while using it.
64254721Semaste    Clear();
65254721Semaste}
66254721Semaste
67254721Semaste
68254721Semasteuint32_t
69254721SemasteThreadList::GetStopID () const
70254721Semaste{
71254721Semaste    return m_stop_id;
72254721Semaste}
73254721Semaste
74254721Semastevoid
75254721SemasteThreadList::SetStopID (uint32_t stop_id)
76254721Semaste{
77254721Semaste    m_stop_id = stop_id;
78254721Semaste}
79254721Semaste
80254721Semaste
81254721Semastevoid
82254721SemasteThreadList::AddThread (const ThreadSP &thread_sp)
83254721Semaste{
84254721Semaste    Mutex::Locker locker(GetMutex());
85254721Semaste    m_threads.push_back(thread_sp);
86254721Semaste}
87254721Semaste
88269024Semastevoid
89269024SemasteThreadList::InsertThread (const lldb::ThreadSP &thread_sp, uint32_t idx)
90269024Semaste{
91269024Semaste    Mutex::Locker locker(GetMutex());
92269024Semaste    if (idx < m_threads.size())
93269024Semaste        m_threads.insert(m_threads.begin() + idx, thread_sp);
94269024Semaste    else
95269024Semaste        m_threads.push_back (thread_sp);
96269024Semaste}
97269024Semaste
98269024Semaste
99254721Semasteuint32_t
100254721SemasteThreadList::GetSize (bool can_update)
101254721Semaste{
102254721Semaste    Mutex::Locker locker(GetMutex());
103254721Semaste    if (can_update)
104254721Semaste        m_process->UpdateThreadListIfNeeded();
105254721Semaste    return m_threads.size();
106254721Semaste}
107254721Semaste
108254721SemasteThreadSP
109254721SemasteThreadList::GetThreadAtIndex (uint32_t idx, bool can_update)
110254721Semaste{
111254721Semaste    Mutex::Locker locker(GetMutex());
112254721Semaste    if (can_update)
113254721Semaste        m_process->UpdateThreadListIfNeeded();
114254721Semaste
115254721Semaste    ThreadSP thread_sp;
116254721Semaste    if (idx < m_threads.size())
117254721Semaste        thread_sp = m_threads[idx];
118254721Semaste    return thread_sp;
119254721Semaste}
120254721Semaste
121254721SemasteThreadSP
122254721SemasteThreadList::FindThreadByID (lldb::tid_t tid, bool can_update)
123254721Semaste{
124254721Semaste    Mutex::Locker locker(GetMutex());
125254721Semaste
126254721Semaste    if (can_update)
127254721Semaste        m_process->UpdateThreadListIfNeeded();
128254721Semaste
129254721Semaste    ThreadSP thread_sp;
130254721Semaste    uint32_t idx = 0;
131254721Semaste    const uint32_t num_threads = m_threads.size();
132254721Semaste    for (idx = 0; idx < num_threads; ++idx)
133254721Semaste    {
134254721Semaste        if (m_threads[idx]->GetID() == tid)
135254721Semaste        {
136254721Semaste            thread_sp = m_threads[idx];
137254721Semaste            break;
138254721Semaste        }
139254721Semaste    }
140254721Semaste    return thread_sp;
141254721Semaste}
142254721Semaste
143254721SemasteThreadSP
144254721SemasteThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update)
145254721Semaste{
146254721Semaste    Mutex::Locker locker(GetMutex());
147254721Semaste
148254721Semaste    if (can_update)
149254721Semaste        m_process->UpdateThreadListIfNeeded();
150254721Semaste
151254721Semaste    ThreadSP thread_sp;
152254721Semaste    uint32_t idx = 0;
153254721Semaste    const uint32_t num_threads = m_threads.size();
154254721Semaste    for (idx = 0; idx < num_threads; ++idx)
155254721Semaste    {
156254721Semaste        if (m_threads[idx]->GetProtocolID() == tid)
157254721Semaste        {
158254721Semaste            thread_sp = m_threads[idx];
159254721Semaste            break;
160254721Semaste        }
161254721Semaste    }
162254721Semaste    return thread_sp;
163254721Semaste}
164254721Semaste
165254721Semaste
166254721SemasteThreadSP
167254721SemasteThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update)
168254721Semaste{
169254721Semaste    Mutex::Locker locker(GetMutex());
170254721Semaste
171254721Semaste    if (can_update)
172254721Semaste        m_process->UpdateThreadListIfNeeded();
173254721Semaste
174254721Semaste    ThreadSP thread_sp;
175254721Semaste    uint32_t idx = 0;
176254721Semaste    const uint32_t num_threads = m_threads.size();
177254721Semaste    for (idx = 0; idx < num_threads; ++idx)
178254721Semaste    {
179254721Semaste        if (m_threads[idx]->GetID() == tid)
180254721Semaste        {
181254721Semaste            thread_sp = m_threads[idx];
182254721Semaste            m_threads.erase(m_threads.begin()+idx);
183254721Semaste            break;
184254721Semaste        }
185254721Semaste    }
186254721Semaste    return thread_sp;
187254721Semaste}
188254721Semaste
189254721SemasteThreadSP
190254721SemasteThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update)
191254721Semaste{
192254721Semaste    Mutex::Locker locker(GetMutex());
193254721Semaste
194254721Semaste    if (can_update)
195254721Semaste        m_process->UpdateThreadListIfNeeded();
196254721Semaste
197254721Semaste    ThreadSP thread_sp;
198254721Semaste    uint32_t idx = 0;
199254721Semaste    const uint32_t num_threads = m_threads.size();
200254721Semaste    for (idx = 0; idx < num_threads; ++idx)
201254721Semaste    {
202254721Semaste        if (m_threads[idx]->GetProtocolID() == tid)
203254721Semaste        {
204254721Semaste            thread_sp = m_threads[idx];
205254721Semaste            m_threads.erase(m_threads.begin()+idx);
206254721Semaste            break;
207254721Semaste        }
208254721Semaste    }
209254721Semaste    return thread_sp;
210254721Semaste}
211254721Semaste
212254721SemasteThreadSP
213254721SemasteThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr)
214254721Semaste{
215254721Semaste    ThreadSP thread_sp;
216254721Semaste    if (thread_ptr)
217254721Semaste    {
218254721Semaste        Mutex::Locker locker(GetMutex());
219254721Semaste
220254721Semaste        uint32_t idx = 0;
221254721Semaste        const uint32_t num_threads = m_threads.size();
222254721Semaste        for (idx = 0; idx < num_threads; ++idx)
223254721Semaste        {
224254721Semaste            if (m_threads[idx].get() == thread_ptr)
225254721Semaste            {
226254721Semaste                thread_sp = m_threads[idx];
227254721Semaste                break;
228254721Semaste            }
229254721Semaste        }
230254721Semaste    }
231254721Semaste    return thread_sp;
232254721Semaste}
233254721Semaste
234254721Semaste
235254721Semaste
236254721SemasteThreadSP
237254721SemasteThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update)
238254721Semaste{
239254721Semaste    Mutex::Locker locker(GetMutex());
240254721Semaste
241254721Semaste    if (can_update)
242254721Semaste        m_process->UpdateThreadListIfNeeded();
243254721Semaste
244254721Semaste    ThreadSP thread_sp;
245254721Semaste    const uint32_t num_threads = m_threads.size();
246254721Semaste    for (uint32_t idx = 0; idx < num_threads; ++idx)
247254721Semaste    {
248254721Semaste        if (m_threads[idx]->GetIndexID() == index_id)
249254721Semaste        {
250254721Semaste            thread_sp = m_threads[idx];
251254721Semaste            break;
252254721Semaste        }
253254721Semaste    }
254254721Semaste    return thread_sp;
255254721Semaste}
256254721Semaste
257254721Semastebool
258254721SemasteThreadList::ShouldStop (Event *event_ptr)
259254721Semaste{
260254721Semaste    // Running events should never stop, obviously...
261254721Semaste
262254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
263254721Semaste
264254721Semaste    // The ShouldStop method of the threads can do a whole lot of work,
265254721Semaste    // running breakpoint commands & conditions, etc.  So we don't want
266254721Semaste    // to keep the ThreadList locked the whole time we are doing this.
267254721Semaste    // FIXME: It is possible that running code could cause new threads
268254721Semaste    // to be created.  If that happens we will miss asking them whether
269254721Semaste    // then should stop.  This is not a big deal, since we haven't had
270254721Semaste    // a chance to hang any interesting operations on those threads yet.
271254721Semaste
272254721Semaste    collection threads_copy;
273254721Semaste    {
274254721Semaste        // Scope for locker
275254721Semaste        Mutex::Locker locker(GetMutex());
276254721Semaste
277254721Semaste        m_process->UpdateThreadListIfNeeded();
278254721Semaste        threads_copy = m_threads;
279254721Semaste    }
280254721Semaste
281254721Semaste    collection::iterator pos, end = threads_copy.end();
282254721Semaste
283254721Semaste    if (log)
284254721Semaste    {
285254721Semaste        log->PutCString("");
286254721Semaste        log->Printf ("ThreadList::%s: %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
287254721Semaste    }
288254721Semaste
289254721Semaste    bool did_anybody_stop_for_a_reason = false;
290254721Semaste    bool should_stop = false;
291254721Semaste
292254721Semaste    // Now we run through all the threads and get their stop info's.  We want to make sure to do this first before
293254721Semaste    // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a
294254721Semaste    // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly.
295254721Semaste    // We don't need to use it here, we just want to make sure it gets computed.
296254721Semaste
297254721Semaste    for (pos = threads_copy.begin(); pos != end; ++pos)
298254721Semaste    {
299254721Semaste        ThreadSP thread_sp(*pos);
300254721Semaste        thread_sp->GetStopInfo();
301254721Semaste    }
302254721Semaste
303254721Semaste    for (pos = threads_copy.begin(); pos != end; ++pos)
304254721Semaste    {
305254721Semaste        ThreadSP thread_sp(*pos);
306254721Semaste
307269024Semaste        // We should never get a stop for which no thread had a stop reason, but sometimes we do see this -
308269024Semaste        // for instance when we first connect to a remote stub.  In that case we should stop, since we can't figure out
309269024Semaste        // the right thing to do and stopping gives the user control over what to do in this instance.
310269024Semaste        //
311269024Semaste        // Note, this causes a problem when you have a thread specific breakpoint, and a bunch of threads hit the breakpoint,
312269024Semaste        // but not the thread which we are waiting for.  All the threads that are not "supposed" to hit the breakpoint
313269024Semaste        // are marked as having no stop reason, which is right, they should not show a stop reason.  But that triggers this
314269024Semaste        // code and causes us to stop seemingly for no reason.
315269024Semaste        //
316269024Semaste        // 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
317269024Semaste        // to true unless this is the first stop.
318269024Semaste        //
319269024Semaste        // If this becomes a problem, we'll have to have another StopReason like "StopInfoHidden" which will look invalid
320269024Semaste        // everywhere but at this check.
321269024Semaste
322269024Semaste        if (thread_sp->GetProcess()->GetStopID() > 1)
323269024Semaste            did_anybody_stop_for_a_reason = true;
324269024Semaste        else
325269024Semaste            did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason();
326254721Semaste
327254721Semaste        const bool thread_should_stop = thread_sp->ShouldStop(event_ptr);
328254721Semaste        if (thread_should_stop)
329254721Semaste            should_stop |= true;
330254721Semaste    }
331254721Semaste
332254721Semaste    if (!should_stop && !did_anybody_stop_for_a_reason)
333254721Semaste    {
334254721Semaste        should_stop = true;
335254721Semaste        if (log)
336254721Semaste            log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__);
337254721Semaste    }
338254721Semaste
339254721Semaste    if (log)
340254721Semaste        log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop);
341254721Semaste
342254721Semaste    if (should_stop)
343254721Semaste    {
344254721Semaste        for (pos = threads_copy.begin(); pos != end; ++pos)
345254721Semaste        {
346254721Semaste            ThreadSP thread_sp(*pos);
347254721Semaste            thread_sp->WillStop ();
348254721Semaste        }
349254721Semaste    }
350254721Semaste
351254721Semaste    return should_stop;
352254721Semaste}
353254721Semaste
354254721SemasteVote
355254721SemasteThreadList::ShouldReportStop (Event *event_ptr)
356254721Semaste{
357254721Semaste    Mutex::Locker locker(GetMutex());
358254721Semaste
359254721Semaste    Vote result = eVoteNoOpinion;
360254721Semaste    m_process->UpdateThreadListIfNeeded();
361254721Semaste    collection::iterator pos, end = m_threads.end();
362254721Semaste
363254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
364254721Semaste
365254721Semaste    if (log)
366254721Semaste        log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size());
367254721Semaste
368254721Semaste    // Run through the threads and ask whether we should report this event.
369254721Semaste    // For stopping, a YES vote wins over everything.  A NO vote wins over NO opinion.
370254721Semaste    for (pos = m_threads.begin(); pos != end; ++pos)
371254721Semaste    {
372254721Semaste        ThreadSP thread_sp(*pos);
373254721Semaste        const Vote vote = thread_sp->ShouldReportStop (event_ptr);
374254721Semaste        switch (vote)
375254721Semaste        {
376254721Semaste        case eVoteNoOpinion:
377254721Semaste            continue;
378254721Semaste
379254721Semaste        case eVoteYes:
380254721Semaste            result = eVoteYes;
381254721Semaste            break;
382254721Semaste
383254721Semaste        case eVoteNo:
384254721Semaste            if (result == eVoteNoOpinion)
385254721Semaste            {
386254721Semaste                result = eVoteNo;
387254721Semaste            }
388254721Semaste            else
389254721Semaste            {
390254721Semaste                if (log)
391254721Semaste                    log->Printf ("ThreadList::%s thread 0x%4.4" PRIx64 ": voted %s, but lost out because result was %s",
392254721Semaste                                 __FUNCTION__,
393254721Semaste                                 thread_sp->GetID (),
394254721Semaste                                 GetVoteAsCString (vote),
395254721Semaste                                 GetVoteAsCString (result));
396254721Semaste            }
397254721Semaste            break;
398254721Semaste        }
399254721Semaste    }
400254721Semaste    if (log)
401254721Semaste        log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result));
402254721Semaste    return result;
403254721Semaste}
404254721Semaste
405254721Semastevoid
406254721SemasteThreadList::SetShouldReportStop (Vote vote)
407254721Semaste{
408254721Semaste    Mutex::Locker locker(GetMutex());
409254721Semaste    m_process->UpdateThreadListIfNeeded();
410254721Semaste    collection::iterator pos, end = m_threads.end();
411254721Semaste    for (pos = m_threads.begin(); pos != end; ++pos)
412254721Semaste    {
413254721Semaste        ThreadSP thread_sp(*pos);
414254721Semaste        thread_sp->SetShouldReportStop (vote);
415254721Semaste    }
416254721Semaste}
417254721Semaste
418254721SemasteVote
419254721SemasteThreadList::ShouldReportRun (Event *event_ptr)
420254721Semaste{
421254721Semaste
422254721Semaste    Mutex::Locker locker(GetMutex());
423254721Semaste
424254721Semaste    Vote result = eVoteNoOpinion;
425254721Semaste    m_process->UpdateThreadListIfNeeded();
426254721Semaste    collection::iterator pos, end = m_threads.end();
427254721Semaste
428254721Semaste    // Run through the threads and ask whether we should report this event.
429254721Semaste    // The rule is NO vote wins over everything, a YES vote wins over no opinion.
430254721Semaste
431254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
432254721Semaste
433254721Semaste    for (pos = m_threads.begin(); pos != end; ++pos)
434254721Semaste    {
435254721Semaste        if ((*pos)->GetResumeState () != eStateSuspended)
436254721Semaste        {
437254721Semaste            switch ((*pos)->ShouldReportRun (event_ptr))
438254721Semaste            {
439254721Semaste                case eVoteNoOpinion:
440254721Semaste                    continue;
441254721Semaste                case eVoteYes:
442254721Semaste                    if (result == eVoteNoOpinion)
443254721Semaste                        result = eVoteYes;
444254721Semaste                    break;
445254721Semaste                case eVoteNo:
446254721Semaste                    if (log)
447254721Semaste                        log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.",
448254721Semaste                                     (*pos)->GetIndexID(),
449254721Semaste                                     (*pos)->GetID());
450254721Semaste                    result = eVoteNo;
451254721Semaste                    break;
452254721Semaste            }
453254721Semaste        }
454254721Semaste    }
455254721Semaste    return result;
456254721Semaste}
457254721Semaste
458254721Semastevoid
459254721SemasteThreadList::Clear()
460254721Semaste{
461254721Semaste    Mutex::Locker locker(GetMutex());
462254721Semaste    m_stop_id = 0;
463254721Semaste    m_threads.clear();
464254721Semaste    m_selected_tid = LLDB_INVALID_THREAD_ID;
465254721Semaste}
466254721Semaste
467254721Semastevoid
468254721SemasteThreadList::Destroy()
469254721Semaste{
470254721Semaste    Mutex::Locker locker(GetMutex());
471254721Semaste    const uint32_t num_threads = m_threads.size();
472254721Semaste    for (uint32_t idx = 0; idx < num_threads; ++idx)
473254721Semaste    {
474254721Semaste        m_threads[idx]->DestroyThread();
475254721Semaste    }
476254721Semaste}
477254721Semaste
478254721Semastevoid
479254721SemasteThreadList::RefreshStateAfterStop ()
480254721Semaste{
481254721Semaste    Mutex::Locker locker(GetMutex());
482254721Semaste
483254721Semaste    m_process->UpdateThreadListIfNeeded();
484254721Semaste
485254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
486254721Semaste    if (log && log->GetVerbose())
487254721Semaste        log->Printf ("Turning off notification of new threads while single stepping a thread.");
488254721Semaste
489254721Semaste    collection::iterator pos, end = m_threads.end();
490254721Semaste    for (pos = m_threads.begin(); pos != end; ++pos)
491254721Semaste        (*pos)->RefreshStateAfterStop ();
492254721Semaste}
493254721Semaste
494254721Semastevoid
495254721SemasteThreadList::DiscardThreadPlans ()
496254721Semaste{
497254721Semaste    // You don't need to update the thread list here, because only threads
498254721Semaste    // that you currently know about have any thread plans.
499254721Semaste    Mutex::Locker locker(GetMutex());
500254721Semaste
501254721Semaste    collection::iterator pos, end = m_threads.end();
502254721Semaste    for (pos = m_threads.begin(); pos != end; ++pos)
503254721Semaste        (*pos)->DiscardThreadPlans (true);
504254721Semaste
505254721Semaste}
506254721Semaste
507254721Semastebool
508254721SemasteThreadList::WillResume ()
509254721Semaste{
510254721Semaste    // Run through the threads and perform their momentary actions.
511254721Semaste    // But we only do this for threads that are running, user suspended
512254721Semaste    // threads stay where they are.
513254721Semaste
514254721Semaste    Mutex::Locker locker(GetMutex());
515254721Semaste    m_process->UpdateThreadListIfNeeded();
516254721Semaste
517254721Semaste    collection::iterator pos, end = m_threads.end();
518254721Semaste
519254721Semaste    // See if any thread wants to run stopping others.  If it does, then we won't
520254721Semaste    // setup the other threads for resume, since they aren't going to get a chance
521254721Semaste    // to run.  This is necessary because the SetupForResume might add "StopOthers"
522254721Semaste    // plans which would then get to be part of the who-gets-to-run negotiation, but
523254721Semaste    // they're coming in after the fact, and the threads that are already set up should
524254721Semaste    // take priority.
525254721Semaste
526254721Semaste    bool wants_solo_run = false;
527254721Semaste
528254721Semaste    for (pos = m_threads.begin(); pos != end; ++pos)
529254721Semaste    {
530254721Semaste        if ((*pos)->GetResumeState() != eStateSuspended &&
531254721Semaste                 (*pos)->GetCurrentPlan()->StopOthers())
532254721Semaste        {
533254721Semaste            if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
534254721Semaste                continue;
535254721Semaste            wants_solo_run = true;
536254721Semaste            break;
537254721Semaste        }
538254721Semaste    }
539254721Semaste
540254721Semaste    if (wants_solo_run)
541254721Semaste    {
542254721Semaste        Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
543254721Semaste        if (log && log->GetVerbose())
544254721Semaste            log->Printf ("Turning on notification of new threads while single stepping a thread.");
545254721Semaste        m_process->StartNoticingNewThreads();
546254721Semaste    }
547254721Semaste    else
548254721Semaste    {
549254721Semaste        Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
550254721Semaste        if (log && log->GetVerbose())
551254721Semaste            log->Printf ("Turning off notification of new threads while single stepping a thread.");
552254721Semaste        m_process->StopNoticingNewThreads();
553254721Semaste    }
554254721Semaste
555254721Semaste    // Give all the threads that are likely to run a last chance to set up their state before we
556254721Semaste    // negotiate who is actually going to get a chance to run...
557254721Semaste    // Don't set to resume suspended threads, and if any thread wanted to stop others, only
558254721Semaste    // call setup on the threads that request StopOthers...
559254721Semaste
560254721Semaste    for (pos = m_threads.begin(); pos != end; ++pos)
561254721Semaste    {
562254721Semaste        if ((*pos)->GetResumeState() != eStateSuspended
563254721Semaste            && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers()))
564254721Semaste        {
565254721Semaste            if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
566254721Semaste                continue;
567254721Semaste            (*pos)->SetupForResume ();
568254721Semaste        }
569254721Semaste    }
570254721Semaste
571254721Semaste    // Now go through the threads and see if any thread wants to run just itself.
572254721Semaste    // if so then pick one and run it.
573254721Semaste
574254721Semaste    ThreadList run_me_only_list (m_process);
575254721Semaste
576254721Semaste    run_me_only_list.SetStopID(m_process->GetStopID());
577254721Semaste
578254721Semaste    bool run_only_current_thread = false;
579254721Semaste
580254721Semaste    for (pos = m_threads.begin(); pos != end; ++pos)
581254721Semaste    {
582254721Semaste        ThreadSP thread_sp(*pos);
583254721Semaste        if (thread_sp->GetResumeState() != eStateSuspended &&
584254721Semaste                 thread_sp->GetCurrentPlan()->StopOthers())
585254721Semaste        {
586254721Semaste            if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread())
587254721Semaste                continue;
588254721Semaste
589254721Semaste            // You can't say "stop others" and also want yourself to be suspended.
590254721Semaste            assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended);
591254721Semaste
592254721Semaste            if (thread_sp == GetSelectedThread())
593254721Semaste            {
594254721Semaste                run_only_current_thread = true;
595254721Semaste                run_me_only_list.Clear();
596254721Semaste                run_me_only_list.AddThread (thread_sp);
597254721Semaste                break;
598254721Semaste            }
599254721Semaste
600254721Semaste            run_me_only_list.AddThread (thread_sp);
601254721Semaste        }
602254721Semaste
603254721Semaste    }
604254721Semaste
605254721Semaste    bool need_to_resume = true;
606254721Semaste
607254721Semaste    if (run_me_only_list.GetSize (false) == 0)
608254721Semaste    {
609254721Semaste        // Everybody runs as they wish:
610254721Semaste        for (pos = m_threads.begin(); pos != end; ++pos)
611254721Semaste        {
612254721Semaste            ThreadSP thread_sp(*pos);
613254721Semaste            StateType run_state;
614254721Semaste            if (thread_sp->GetResumeState() != eStateSuspended)
615254721Semaste                run_state = thread_sp->GetCurrentPlan()->RunState();
616254721Semaste            else
617254721Semaste                run_state = eStateSuspended;
618254721Semaste            if (!thread_sp->ShouldResume(run_state))
619254721Semaste                need_to_resume = false;
620254721Semaste        }
621254721Semaste    }
622254721Semaste    else
623254721Semaste    {
624254721Semaste        ThreadSP thread_to_run;
625254721Semaste
626254721Semaste        if (run_only_current_thread)
627254721Semaste        {
628254721Semaste            thread_to_run = GetSelectedThread();
629254721Semaste        }
630254721Semaste        else if (run_me_only_list.GetSize (false) == 1)
631254721Semaste        {
632254721Semaste            thread_to_run = run_me_only_list.GetThreadAtIndex (0);
633254721Semaste        }
634254721Semaste        else
635254721Semaste        {
636254721Semaste            int random_thread = (int)
637254721Semaste                    ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0));
638254721Semaste            thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread);
639254721Semaste        }
640254721Semaste
641254721Semaste        for (pos = m_threads.begin(); pos != end; ++pos)
642254721Semaste        {
643254721Semaste            ThreadSP thread_sp(*pos);
644254721Semaste            if (thread_sp == thread_to_run)
645254721Semaste            {
646254721Semaste                if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()))
647254721Semaste                    need_to_resume = false;
648254721Semaste            }
649254721Semaste            else
650254721Semaste                thread_sp->ShouldResume (eStateSuspended);
651254721Semaste        }
652254721Semaste    }
653254721Semaste
654254721Semaste    return need_to_resume;
655254721Semaste}
656254721Semaste
657254721Semastevoid
658254721SemasteThreadList::DidResume ()
659254721Semaste{
660254721Semaste    Mutex::Locker locker(GetMutex());
661254721Semaste    collection::iterator pos, end = m_threads.end();
662254721Semaste    for (pos = m_threads.begin(); pos != end; ++pos)
663254721Semaste    {
664254721Semaste        // Don't clear out threads that aren't going to get a chance to run, rather
665254721Semaste        // leave their state for the next time around.
666254721Semaste        ThreadSP thread_sp(*pos);
667254721Semaste        if (thread_sp->GetResumeState() != eStateSuspended)
668254721Semaste            thread_sp->DidResume ();
669254721Semaste    }
670254721Semaste}
671254721Semaste
672254721Semastevoid
673254721SemasteThreadList::DidStop ()
674254721Semaste{
675254721Semaste    Mutex::Locker locker(GetMutex());
676254721Semaste    collection::iterator pos, end = m_threads.end();
677254721Semaste    for (pos = m_threads.begin(); pos != end; ++pos)
678254721Semaste    {
679254721Semaste        // Notify threads that the process just stopped.
680254721Semaste        // Note, this currently assumes that all threads in the list
681254721Semaste        // stop when the process stops.  In the future we will want to support
682254721Semaste        // a debugging model where some threads continue to run while others
683254721Semaste        // are stopped.  We either need to handle that somehow here or
684254721Semaste        // create a special thread list containing only threads which will
685254721Semaste        // stop in the code that calls this method (currently
686254721Semaste        // Process::SetPrivateState).
687254721Semaste        ThreadSP thread_sp(*pos);
688254721Semaste        if (StateIsRunningState(thread_sp->GetState()))
689254721Semaste            thread_sp->DidStop ();
690254721Semaste    }
691254721Semaste}
692254721Semaste
693254721SemasteThreadSP
694254721SemasteThreadList::GetSelectedThread ()
695254721Semaste{
696254721Semaste    Mutex::Locker locker(GetMutex());
697254721Semaste    ThreadSP thread_sp = FindThreadByID(m_selected_tid);
698254721Semaste    if (!thread_sp.get())
699254721Semaste    {
700254721Semaste        if (m_threads.size() == 0)
701254721Semaste            return thread_sp;
702254721Semaste        m_selected_tid = m_threads[0]->GetID();
703254721Semaste        thread_sp = m_threads[0];
704254721Semaste    }
705254721Semaste    return thread_sp;
706254721Semaste}
707254721Semaste
708254721Semastebool
709254721SemasteThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify)
710254721Semaste{
711254721Semaste    Mutex::Locker locker(GetMutex());
712254721Semaste    ThreadSP selected_thread_sp(FindThreadByID(tid));
713254721Semaste    if  (selected_thread_sp)
714254721Semaste    {
715254721Semaste        m_selected_tid = tid;
716254721Semaste        selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
717254721Semaste    }
718254721Semaste    else
719254721Semaste        m_selected_tid = LLDB_INVALID_THREAD_ID;
720254721Semaste
721254721Semaste    if (notify)
722254721Semaste        NotifySelectedThreadChanged(m_selected_tid);
723254721Semaste
724254721Semaste    return m_selected_tid != LLDB_INVALID_THREAD_ID;
725254721Semaste}
726254721Semaste
727254721Semastebool
728254721SemasteThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify)
729254721Semaste{
730254721Semaste    Mutex::Locker locker(GetMutex());
731254721Semaste    ThreadSP selected_thread_sp (FindThreadByIndexID(index_id));
732254721Semaste    if  (selected_thread_sp.get())
733254721Semaste    {
734254721Semaste        m_selected_tid = selected_thread_sp->GetID();
735254721Semaste        selected_thread_sp->SetDefaultFileAndLineToSelectedFrame();
736254721Semaste    }
737254721Semaste    else
738254721Semaste        m_selected_tid = LLDB_INVALID_THREAD_ID;
739254721Semaste
740254721Semaste    if (notify)
741254721Semaste        NotifySelectedThreadChanged(m_selected_tid);
742254721Semaste
743254721Semaste    return m_selected_tid != LLDB_INVALID_THREAD_ID;
744254721Semaste}
745254721Semaste
746254721Semastevoid
747254721SemasteThreadList::NotifySelectedThreadChanged (lldb::tid_t tid)
748254721Semaste{
749254721Semaste    ThreadSP selected_thread_sp (FindThreadByID(tid));
750254721Semaste    if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected))
751254721Semaste        selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected,
752254721Semaste                                           new Thread::ThreadEventData(selected_thread_sp));
753254721Semaste}
754254721Semaste
755254721Semastevoid
756254721SemasteThreadList::Update (ThreadList &rhs)
757254721Semaste{
758254721Semaste    if (this != &rhs)
759254721Semaste    {
760254721Semaste        // Lock both mutexes to make sure neither side changes anyone on us
761254721Semaste        // while the assignement occurs
762254721Semaste        Mutex::Locker locker(GetMutex());
763254721Semaste        m_process = rhs.m_process;
764254721Semaste        m_stop_id = rhs.m_stop_id;
765254721Semaste        m_threads.swap(rhs.m_threads);
766254721Semaste        m_selected_tid = rhs.m_selected_tid;
767254721Semaste
768254721Semaste
769254721Semaste        // Now we look for threads that we are done with and
770254721Semaste        // make sure to clear them up as much as possible so
771254721Semaste        // anyone with a shared pointer will still have a reference,
772254721Semaste        // but the thread won't be of much use. Using std::weak_ptr
773254721Semaste        // for all backward references (such as a thread to a process)
774254721Semaste        // will eventually solve this issue for us, but for now, we
775254721Semaste        // need to work around the issue
776254721Semaste        collection::iterator rhs_pos, rhs_end = rhs.m_threads.end();
777254721Semaste        for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos)
778254721Semaste        {
779254721Semaste            const lldb::tid_t tid = (*rhs_pos)->GetID();
780254721Semaste            bool thread_is_alive = false;
781254721Semaste            const uint32_t num_threads = m_threads.size();
782254721Semaste            for (uint32_t idx = 0; idx < num_threads; ++idx)
783254721Semaste            {
784254721Semaste                if (m_threads[idx]->GetID() == tid)
785254721Semaste                {
786254721Semaste                    thread_is_alive = true;
787254721Semaste                    break;
788254721Semaste                }
789254721Semaste            }
790254721Semaste            if (!thread_is_alive)
791254721Semaste                (*rhs_pos)->DestroyThread();
792254721Semaste        }
793254721Semaste    }
794254721Semaste}
795254721Semaste
796254721Semastevoid
797254721SemasteThreadList::Flush ()
798254721Semaste{
799254721Semaste    Mutex::Locker locker(GetMutex());
800254721Semaste    collection::iterator pos, end = m_threads.end();
801254721Semaste    for (pos = m_threads.begin(); pos != end; ++pos)
802254721Semaste        (*pos)->Flush ();
803254721Semaste}
804254721Semaste
805254721SemasteMutex &
806254721SemasteThreadList::GetMutex ()
807254721Semaste{
808254721Semaste    return m_process->m_thread_mutex;
809254721Semaste}
810254721Semaste
811