1254721Semaste//===-- Thread.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
10254721Semaste#include "lldb/lldb-python.h"
11254721Semaste
12254721Semaste#include "lldb/lldb-private-log.h"
13254721Semaste#include "lldb/Breakpoint/BreakpointLocation.h"
14254721Semaste#include "lldb/Core/Debugger.h"
15254721Semaste#include "lldb/Core/Log.h"
16254721Semaste#include "lldb/Core/State.h"
17254721Semaste#include "lldb/Core/Stream.h"
18254721Semaste#include "lldb/Core/StreamString.h"
19254721Semaste#include "lldb/Core/RegularExpression.h"
20254721Semaste#include "lldb/Host/Host.h"
21269024Semaste#include "lldb/Interpreter/OptionValueFileSpecList.h"
22254721Semaste#include "lldb/Symbol/Function.h"
23254721Semaste#include "lldb/Target/DynamicLoader.h"
24254721Semaste#include "lldb/Target/ExecutionContext.h"
25254721Semaste#include "lldb/Target/ObjCLanguageRuntime.h"
26254721Semaste#include "lldb/Target/Process.h"
27254721Semaste#include "lldb/Target/RegisterContext.h"
28254721Semaste#include "lldb/Target/StopInfo.h"
29254721Semaste#include "lldb/Target/Target.h"
30254721Semaste#include "lldb/Target/Thread.h"
31254721Semaste#include "lldb/Target/ThreadPlan.h"
32254721Semaste#include "lldb/Target/ThreadPlanCallFunction.h"
33254721Semaste#include "lldb/Target/ThreadPlanBase.h"
34254721Semaste#include "lldb/Target/ThreadPlanStepInstruction.h"
35254721Semaste#include "lldb/Target/ThreadPlanStepOut.h"
36254721Semaste#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
37254721Semaste#include "lldb/Target/ThreadPlanStepThrough.h"
38254721Semaste#include "lldb/Target/ThreadPlanStepInRange.h"
39254721Semaste#include "lldb/Target/ThreadPlanStepOverRange.h"
40254721Semaste#include "lldb/Target/ThreadPlanRunToAddress.h"
41254721Semaste#include "lldb/Target/ThreadPlanStepUntil.h"
42254721Semaste#include "lldb/Target/ThreadSpec.h"
43254721Semaste#include "lldb/Target/Unwind.h"
44254721Semaste#include "Plugins/Process/Utility/UnwindLLDB.h"
45254721Semaste#include "UnwindMacOSXFrameBackchain.h"
46254721Semaste
47254721Semaste
48254721Semasteusing namespace lldb;
49254721Semasteusing namespace lldb_private;
50254721Semaste
51254721Semaste
52254721Semasteconst ThreadPropertiesSP &
53254721SemasteThread::GetGlobalProperties()
54254721Semaste{
55254721Semaste    static ThreadPropertiesSP g_settings_sp;
56254721Semaste    if (!g_settings_sp)
57254721Semaste        g_settings_sp.reset (new ThreadProperties (true));
58254721Semaste    return g_settings_sp;
59254721Semaste}
60254721Semaste
61254721Semastestatic PropertyDefinition
62254721Semasteg_properties[] =
63254721Semaste{
64254721Semaste    { "step-avoid-regexp",  OptionValue::eTypeRegex  , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
65269024Semaste    { "step-avoid-libraries",  OptionValue::eTypeFileSpecList  , true , REG_EXTENDED, NULL, NULL, "A list of libraries that source stepping won't stop in." },
66254721Semaste    { "trace-thread",       OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
67254721Semaste    {  NULL               , OptionValue::eTypeInvalid, false, 0    , NULL, NULL, NULL  }
68254721Semaste};
69254721Semaste
70254721Semasteenum {
71254721Semaste    ePropertyStepAvoidRegex,
72269024Semaste    ePropertyStepAvoidLibraries,
73254721Semaste    ePropertyEnableThreadTrace
74254721Semaste};
75254721Semaste
76254721Semaste
77254721Semasteclass ThreadOptionValueProperties : public OptionValueProperties
78254721Semaste{
79254721Semastepublic:
80254721Semaste    ThreadOptionValueProperties (const ConstString &name) :
81254721Semaste        OptionValueProperties (name)
82254721Semaste    {
83254721Semaste    }
84254721Semaste
85254721Semaste    // This constructor is used when creating ThreadOptionValueProperties when it
86254721Semaste    // is part of a new lldb_private::Thread instance. It will copy all current
87254721Semaste    // global property values as needed
88254721Semaste    ThreadOptionValueProperties (ThreadProperties *global_properties) :
89254721Semaste        OptionValueProperties(*global_properties->GetValueProperties())
90254721Semaste    {
91254721Semaste    }
92254721Semaste
93254721Semaste    virtual const Property *
94254721Semaste    GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
95254721Semaste    {
96254721Semaste        // When gettings the value for a key from the thread options, we will always
97254721Semaste        // try and grab the setting from the current thread if there is one. Else we just
98254721Semaste        // use the one from this instance.
99254721Semaste        if (exe_ctx)
100254721Semaste        {
101254721Semaste            Thread *thread = exe_ctx->GetThreadPtr();
102254721Semaste            if (thread)
103254721Semaste            {
104254721Semaste                ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
105254721Semaste                if (this != instance_properties)
106254721Semaste                    return instance_properties->ProtectedGetPropertyAtIndex (idx);
107254721Semaste            }
108254721Semaste        }
109254721Semaste        return ProtectedGetPropertyAtIndex (idx);
110254721Semaste    }
111254721Semaste};
112254721Semaste
113254721Semaste
114254721Semaste
115254721SemasteThreadProperties::ThreadProperties (bool is_global) :
116254721Semaste    Properties ()
117254721Semaste{
118254721Semaste    if (is_global)
119254721Semaste    {
120254721Semaste        m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
121254721Semaste        m_collection_sp->Initialize(g_properties);
122254721Semaste    }
123254721Semaste    else
124254721Semaste        m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
125254721Semaste}
126254721Semaste
127254721SemasteThreadProperties::~ThreadProperties()
128254721Semaste{
129254721Semaste}
130254721Semaste
131254721Semasteconst RegularExpression *
132254721SemasteThreadProperties::GetSymbolsToAvoidRegexp()
133254721Semaste{
134254721Semaste    const uint32_t idx = ePropertyStepAvoidRegex;
135254721Semaste    return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
136254721Semaste}
137254721Semaste
138269024SemasteFileSpecList &
139269024SemasteThreadProperties::GetLibrariesToAvoid() const
140269024Semaste{
141269024Semaste    const uint32_t idx = ePropertyStepAvoidLibraries;
142269024Semaste    OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
143269024Semaste    assert(option_value);
144269024Semaste    return option_value->GetCurrentValue();
145269024Semaste}
146269024Semaste
147254721Semastebool
148254721SemasteThreadProperties::GetTraceEnabledState() const
149254721Semaste{
150254721Semaste    const uint32_t idx = ePropertyEnableThreadTrace;
151254721Semaste    return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
152254721Semaste}
153254721Semaste
154254721Semaste//------------------------------------------------------------------
155254721Semaste// Thread Event Data
156254721Semaste//------------------------------------------------------------------
157254721Semaste
158254721Semaste
159254721Semasteconst ConstString &
160254721SemasteThread::ThreadEventData::GetFlavorString ()
161254721Semaste{
162254721Semaste    static ConstString g_flavor ("Thread::ThreadEventData");
163254721Semaste    return g_flavor;
164254721Semaste}
165254721Semaste
166254721SemasteThread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
167254721Semaste    m_thread_sp (thread_sp),
168254721Semaste    m_stack_id ()
169254721Semaste{
170254721Semaste}
171254721Semaste
172254721SemasteThread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
173254721Semaste    m_thread_sp (thread_sp),
174254721Semaste    m_stack_id (stack_id)
175254721Semaste{
176254721Semaste}
177254721Semaste
178254721SemasteThread::ThreadEventData::ThreadEventData () :
179254721Semaste    m_thread_sp (),
180254721Semaste    m_stack_id ()
181254721Semaste{
182254721Semaste}
183254721Semaste
184254721SemasteThread::ThreadEventData::~ThreadEventData ()
185254721Semaste{
186254721Semaste}
187254721Semaste
188254721Semastevoid
189254721SemasteThread::ThreadEventData::Dump (Stream *s) const
190254721Semaste{
191254721Semaste
192254721Semaste}
193254721Semaste
194254721Semasteconst Thread::ThreadEventData *
195254721SemasteThread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
196254721Semaste{
197254721Semaste    if (event_ptr)
198254721Semaste    {
199254721Semaste        const EventData *event_data = event_ptr->GetData();
200254721Semaste        if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
201254721Semaste            return static_cast <const ThreadEventData *> (event_ptr->GetData());
202254721Semaste    }
203254721Semaste    return NULL;
204254721Semaste}
205254721Semaste
206254721SemasteThreadSP
207254721SemasteThread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
208254721Semaste{
209254721Semaste    ThreadSP thread_sp;
210254721Semaste    const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
211254721Semaste    if (event_data)
212254721Semaste        thread_sp = event_data->GetThread();
213254721Semaste    return thread_sp;
214254721Semaste}
215254721Semaste
216254721SemasteStackID
217254721SemasteThread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
218254721Semaste{
219254721Semaste    StackID stack_id;
220254721Semaste    const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
221254721Semaste    if (event_data)
222254721Semaste        stack_id = event_data->GetStackID();
223254721Semaste    return stack_id;
224254721Semaste}
225254721Semaste
226254721SemasteStackFrameSP
227254721SemasteThread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
228254721Semaste{
229254721Semaste    const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
230254721Semaste    StackFrameSP frame_sp;
231254721Semaste    if (event_data)
232254721Semaste    {
233254721Semaste        ThreadSP thread_sp = event_data->GetThread();
234254721Semaste        if (thread_sp)
235254721Semaste        {
236254721Semaste            frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
237254721Semaste        }
238254721Semaste    }
239254721Semaste    return frame_sp;
240254721Semaste}
241254721Semaste
242254721Semaste//------------------------------------------------------------------
243254721Semaste// Thread class
244254721Semaste//------------------------------------------------------------------
245254721Semaste
246254721SemasteConstString &
247254721SemasteThread::GetStaticBroadcasterClass ()
248254721Semaste{
249254721Semaste    static ConstString class_name ("lldb.thread");
250254721Semaste    return class_name;
251254721Semaste}
252254721Semaste
253254721SemasteThread::Thread (Process &process, lldb::tid_t tid) :
254254721Semaste    ThreadProperties (false),
255254721Semaste    UserID (tid),
256254721Semaste    Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
257254721Semaste    m_process_wp (process.shared_from_this()),
258254721Semaste    m_stop_info_sp (),
259254721Semaste    m_stop_info_stop_id (0),
260254721Semaste    m_index_id (process.GetNextThreadIndexID(tid)),
261254721Semaste    m_reg_context_sp (),
262254721Semaste    m_state (eStateUnloaded),
263254721Semaste    m_state_mutex (Mutex::eMutexTypeRecursive),
264254721Semaste    m_plan_stack (),
265254721Semaste    m_completed_plan_stack(),
266254721Semaste    m_frame_mutex (Mutex::eMutexTypeRecursive),
267254721Semaste    m_curr_frames_sp (),
268254721Semaste    m_prev_frames_sp (),
269254721Semaste    m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
270254721Semaste    m_resume_state (eStateRunning),
271254721Semaste    m_temporary_resume_state (eStateRunning),
272254721Semaste    m_unwinder_ap (),
273254721Semaste    m_destroy_called (false),
274254721Semaste    m_override_should_notify (eLazyBoolCalculate)
275254721Semaste{
276254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
277254721Semaste    if (log)
278254721Semaste        log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
279254721Semaste
280254721Semaste    CheckInWithManager();
281254721Semaste    QueueFundamentalPlan(true);
282254721Semaste}
283254721Semaste
284254721Semaste
285254721SemasteThread::~Thread()
286254721Semaste{
287254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
288254721Semaste    if (log)
289254721Semaste        log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
290254721Semaste    /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
291254721Semaste    assert (m_destroy_called);
292254721Semaste}
293254721Semaste
294254721Semastevoid
295254721SemasteThread::DestroyThread ()
296254721Semaste{
297263363Semaste    // Tell any plans on the plan stacks that the thread is being destroyed since
298263363Semaste    // any plans that have a thread go away in the middle of might need
299263363Semaste    // to do cleanup, or in some cases NOT do cleanup...
300254721Semaste    for (auto plan : m_plan_stack)
301254721Semaste        plan->ThreadDestroyed();
302254721Semaste
303263363Semaste    for (auto plan : m_discarded_plan_stack)
304263363Semaste        plan->ThreadDestroyed();
305263363Semaste
306263363Semaste    for (auto plan : m_completed_plan_stack)
307263363Semaste        plan->ThreadDestroyed();
308263363Semaste
309254721Semaste    m_destroy_called = true;
310254721Semaste    m_plan_stack.clear();
311254721Semaste    m_discarded_plan_stack.clear();
312254721Semaste    m_completed_plan_stack.clear();
313254721Semaste
314254721Semaste    // Push a ThreadPlanNull on the plan stack.  That way we can continue assuming that the
315254721Semaste    // plan stack is never empty, but if somebody errantly asks questions of a destroyed thread
316254721Semaste    // without checking first whether it is destroyed, they won't crash.
317254721Semaste    ThreadPlanSP null_plan_sp(new ThreadPlanNull (*this));
318254721Semaste    m_plan_stack.push_back (null_plan_sp);
319254721Semaste
320254721Semaste    m_stop_info_sp.reset();
321254721Semaste    m_reg_context_sp.reset();
322254721Semaste    m_unwinder_ap.reset();
323254721Semaste    Mutex::Locker locker(m_frame_mutex);
324254721Semaste    m_curr_frames_sp.reset();
325254721Semaste    m_prev_frames_sp.reset();
326254721Semaste}
327254721Semaste
328254721Semastevoid
329254721SemasteThread::BroadcastSelectedFrameChange(StackID &new_frame_id)
330254721Semaste{
331254721Semaste    if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
332254721Semaste        BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
333254721Semaste}
334254721Semaste
335254721Semasteuint32_t
336254721SemasteThread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
337254721Semaste{
338254721Semaste    uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
339254721Semaste    if (broadcast)
340254721Semaste        BroadcastSelectedFrameChange(frame->GetStackID());
341254721Semaste    return ret_value;
342254721Semaste}
343254721Semaste
344254721Semastebool
345254721SemasteThread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
346254721Semaste{
347254721Semaste    StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
348254721Semaste    if (frame_sp)
349254721Semaste    {
350254721Semaste        GetStackFrameList()->SetSelectedFrame(frame_sp.get());
351254721Semaste        if (broadcast)
352254721Semaste            BroadcastSelectedFrameChange(frame_sp->GetStackID());
353254721Semaste        return true;
354254721Semaste    }
355254721Semaste    else
356254721Semaste        return false;
357254721Semaste}
358254721Semaste
359254721Semastebool
360254721SemasteThread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
361254721Semaste{
362254721Semaste    const bool broadcast = true;
363254721Semaste    bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
364254721Semaste    if (success)
365254721Semaste    {
366254721Semaste        StackFrameSP frame_sp = GetSelectedFrame();
367254721Semaste        if (frame_sp)
368254721Semaste        {
369254721Semaste            bool already_shown = false;
370254721Semaste            SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
371254721Semaste            if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
372254721Semaste            {
373254721Semaste                already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
374254721Semaste            }
375254721Semaste
376254721Semaste            bool show_frame_info = true;
377254721Semaste            bool show_source = !already_shown;
378254721Semaste            return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
379254721Semaste        }
380254721Semaste        return false;
381254721Semaste    }
382254721Semaste    else
383254721Semaste        return false;
384254721Semaste}
385254721Semaste
386254721Semaste
387254721Semastelldb::StopInfoSP
388254721SemasteThread::GetStopInfo ()
389254721Semaste{
390254721Semaste    if (m_destroy_called)
391254721Semaste        return m_stop_info_sp;
392254721Semaste
393254721Semaste    ThreadPlanSP plan_sp (GetCompletedPlan());
394254721Semaste    ProcessSP process_sp (GetProcess());
395254721Semaste    const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
396254721Semaste    if (plan_sp && plan_sp->PlanSucceeded())
397254721Semaste    {
398254721Semaste        return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
399254721Semaste    }
400254721Semaste    else
401254721Semaste    {
402254721Semaste        if ((m_stop_info_stop_id == stop_id) ||   // Stop info is valid, just return what we have (even if empty)
403254721Semaste            (m_stop_info_sp && m_stop_info_sp->IsValid()))  // Stop info is valid, just return what we have
404254721Semaste        {
405254721Semaste            return m_stop_info_sp;
406254721Semaste        }
407254721Semaste        else
408254721Semaste        {
409254721Semaste            GetPrivateStopInfo ();
410254721Semaste            return m_stop_info_sp;
411254721Semaste        }
412254721Semaste    }
413254721Semaste}
414254721Semaste
415254721Semastelldb::StopInfoSP
416254721SemasteThread::GetPrivateStopInfo ()
417254721Semaste{
418254721Semaste    if (m_destroy_called)
419254721Semaste        return m_stop_info_sp;
420254721Semaste
421254721Semaste    ProcessSP process_sp (GetProcess());
422254721Semaste    if (process_sp)
423254721Semaste    {
424254721Semaste        const uint32_t process_stop_id = process_sp->GetStopID();
425254721Semaste        if (m_stop_info_stop_id != process_stop_id)
426254721Semaste        {
427254721Semaste            if (m_stop_info_sp)
428254721Semaste            {
429254721Semaste                if (m_stop_info_sp->IsValid()
430254721Semaste                    || IsStillAtLastBreakpointHit()
431254721Semaste                    || GetCurrentPlan()->IsVirtualStep())
432254721Semaste                    SetStopInfo (m_stop_info_sp);
433254721Semaste                else
434254721Semaste                    m_stop_info_sp.reset();
435254721Semaste            }
436254721Semaste
437254721Semaste            if (!m_stop_info_sp)
438254721Semaste            {
439254721Semaste                if (CalculateStopInfo() == false)
440254721Semaste                    SetStopInfo (StopInfoSP());
441254721Semaste            }
442254721Semaste        }
443254721Semaste    }
444254721Semaste    return m_stop_info_sp;
445254721Semaste}
446254721Semaste
447254721Semaste
448254721Semastelldb::StopReason
449254721SemasteThread::GetStopReason()
450254721Semaste{
451254721Semaste    lldb::StopInfoSP stop_info_sp (GetStopInfo ());
452254721Semaste    if (stop_info_sp)
453254721Semaste        return stop_info_sp->GetStopReason();
454254721Semaste    return eStopReasonNone;
455254721Semaste}
456254721Semaste
457254721Semaste
458254721Semaste
459254721Semastevoid
460254721SemasteThread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
461254721Semaste{
462254721Semaste    m_stop_info_sp = stop_info_sp;
463254721Semaste    if (m_stop_info_sp)
464254721Semaste    {
465254721Semaste        m_stop_info_sp->MakeStopInfoValid();
466254721Semaste        // If we are overriding the ShouldReportStop, do that here:
467254721Semaste        if (m_override_should_notify != eLazyBoolCalculate)
468254721Semaste            m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
469254721Semaste    }
470254721Semaste
471254721Semaste    ProcessSP process_sp (GetProcess());
472254721Semaste    if (process_sp)
473254721Semaste        m_stop_info_stop_id = process_sp->GetStopID();
474254721Semaste    else
475254721Semaste        m_stop_info_stop_id = UINT32_MAX;
476254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
477254721Semaste    if (log)
478254721Semaste        log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)\n", this, GetID(), stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>", m_stop_info_stop_id);
479254721Semaste}
480254721Semaste
481254721Semastevoid
482254721SemasteThread::SetShouldReportStop (Vote vote)
483254721Semaste{
484254721Semaste    if (vote == eVoteNoOpinion)
485254721Semaste        return;
486254721Semaste    else
487254721Semaste    {
488254721Semaste        m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
489254721Semaste        if (m_stop_info_sp)
490254721Semaste            m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
491254721Semaste    }
492254721Semaste}
493254721Semaste
494254721Semastevoid
495254721SemasteThread::SetStopInfoToNothing()
496254721Semaste{
497254721Semaste    // Note, we can't just NULL out the private reason, or the native thread implementation will try to
498254721Semaste    // go calculate it again.  For now, just set it to a Unix Signal with an invalid signal number.
499254721Semaste    SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this,  LLDB_INVALID_SIGNAL_NUMBER));
500254721Semaste}
501254721Semaste
502254721Semastebool
503254721SemasteThread::ThreadStoppedForAReason (void)
504254721Semaste{
505254721Semaste    return (bool) GetPrivateStopInfo ();
506254721Semaste}
507254721Semaste
508254721Semastebool
509254721SemasteThread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
510254721Semaste{
511263367Semaste    saved_state.register_backup_sp.reset();
512263367Semaste    lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
513263367Semaste    if (frame_sp)
514263367Semaste    {
515263367Semaste        lldb::RegisterCheckpointSP reg_checkpoint_sp(new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
516263367Semaste        if (reg_checkpoint_sp)
517263367Semaste        {
518263367Semaste            lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
519263367Semaste            if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues (*reg_checkpoint_sp))
520263367Semaste                saved_state.register_backup_sp = reg_checkpoint_sp;
521263367Semaste        }
522263367Semaste    }
523263367Semaste    if (!saved_state.register_backup_sp)
524254721Semaste        return false;
525254721Semaste
526254721Semaste    saved_state.stop_info_sp = GetStopInfo();
527254721Semaste    ProcessSP process_sp (GetProcess());
528254721Semaste    if (process_sp)
529254721Semaste        saved_state.orig_stop_id = process_sp->GetStopID();
530254721Semaste    saved_state.current_inlined_depth = GetCurrentInlinedDepth();
531254721Semaste
532254721Semaste    return true;
533254721Semaste}
534254721Semaste
535254721Semastebool
536254721SemasteThread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
537254721Semaste{
538263367Semaste    if (saved_state.register_backup_sp)
539263367Semaste    {
540263367Semaste        lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
541263367Semaste        if (frame_sp)
542263367Semaste        {
543263367Semaste            lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
544263367Semaste            if (reg_ctx_sp)
545263367Semaste            {
546263367Semaste                bool ret = reg_ctx_sp->WriteAllRegisterValues (*saved_state.register_backup_sp);
547263367Semaste
548263367Semaste                // Clear out all stack frames as our world just changed.
549263367Semaste                ClearStackFrames();
550263367Semaste                reg_ctx_sp->InvalidateIfNeeded(true);
551263367Semaste                if (m_unwinder_ap.get())
552263367Semaste                    m_unwinder_ap->Clear();
553263367Semaste                return ret;
554263367Semaste            }
555263367Semaste        }
556263367Semaste    }
557263367Semaste    return false;
558254721Semaste}
559254721Semaste
560254721Semastebool
561254721SemasteThread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
562254721Semaste{
563254721Semaste    if (saved_state.stop_info_sp)
564254721Semaste        saved_state.stop_info_sp->MakeStopInfoValid();
565254721Semaste    SetStopInfo(saved_state.stop_info_sp);
566254721Semaste    GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
567254721Semaste    return true;
568254721Semaste}
569254721Semaste
570254721SemasteStateType
571254721SemasteThread::GetState() const
572254721Semaste{
573254721Semaste    // If any other threads access this we will need a mutex for it
574254721Semaste    Mutex::Locker locker(m_state_mutex);
575254721Semaste    return m_state;
576254721Semaste}
577254721Semaste
578254721Semastevoid
579254721SemasteThread::SetState(StateType state)
580254721Semaste{
581254721Semaste    Mutex::Locker locker(m_state_mutex);
582254721Semaste    m_state = state;
583254721Semaste}
584254721Semaste
585254721Semastevoid
586254721SemasteThread::WillStop()
587254721Semaste{
588254721Semaste    ThreadPlan *current_plan = GetCurrentPlan();
589254721Semaste
590254721Semaste    // FIXME: I may decide to disallow threads with no plans.  In which
591254721Semaste    // case this should go to an assert.
592254721Semaste
593254721Semaste    if (!current_plan)
594254721Semaste        return;
595254721Semaste
596254721Semaste    current_plan->WillStop();
597254721Semaste}
598254721Semaste
599254721Semastevoid
600254721SemasteThread::SetupForResume ()
601254721Semaste{
602254721Semaste    if (GetResumeState() != eStateSuspended)
603254721Semaste    {
604254721Semaste
605254721Semaste        // If we're at a breakpoint push the step-over breakpoint plan.  Do this before
606254721Semaste        // telling the current plan it will resume, since we might change what the current
607254721Semaste        // plan is.
608254721Semaste
609254721Semaste//      StopReason stop_reason = lldb::eStopReasonInvalid;
610254721Semaste//      StopInfoSP stop_info_sp = GetStopInfo();
611254721Semaste//      if (stop_info_sp.get())
612254721Semaste//          stop_reason = stop_info_sp->GetStopReason();
613254721Semaste//      if (stop_reason == lldb::eStopReasonBreakpoint)
614254721Semaste        lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
615254721Semaste        if (reg_ctx_sp)
616254721Semaste        {
617254721Semaste            BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(reg_ctx_sp->GetPC());
618254721Semaste            if (bp_site_sp)
619254721Semaste            {
620254721Semaste                // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
621254721Semaste                // special to step over a breakpoint.
622254721Semaste
623254721Semaste                ThreadPlan *cur_plan = GetCurrentPlan();
624254721Semaste
625254721Semaste                if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
626254721Semaste                {
627254721Semaste                    ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
628254721Semaste                    if (step_bp_plan)
629254721Semaste                    {
630254721Semaste                        ThreadPlanSP step_bp_plan_sp;
631254721Semaste                        step_bp_plan->SetPrivate (true);
632254721Semaste
633254721Semaste                        if (GetCurrentPlan()->RunState() != eStateStepping)
634254721Semaste                        {
635254721Semaste                            step_bp_plan->SetAutoContinue(true);
636254721Semaste                        }
637254721Semaste                        step_bp_plan_sp.reset (step_bp_plan);
638254721Semaste                        QueueThreadPlan (step_bp_plan_sp, false);
639254721Semaste                    }
640254721Semaste                }
641254721Semaste            }
642254721Semaste        }
643254721Semaste    }
644254721Semaste}
645254721Semaste
646254721Semastebool
647254721SemasteThread::ShouldResume (StateType resume_state)
648254721Semaste{
649254721Semaste    // At this point clear the completed plan stack.
650254721Semaste    m_completed_plan_stack.clear();
651254721Semaste    m_discarded_plan_stack.clear();
652254721Semaste    m_override_should_notify = eLazyBoolCalculate;
653254721Semaste
654254721Semaste    m_temporary_resume_state = resume_state;
655254721Semaste
656254721Semaste    lldb::ThreadSP backing_thread_sp (GetBackingThread ());
657254721Semaste    if (backing_thread_sp)
658254721Semaste        backing_thread_sp->m_temporary_resume_state = resume_state;
659254721Semaste
660254721Semaste    // Make sure m_stop_info_sp is valid
661254721Semaste    GetPrivateStopInfo();
662254721Semaste
663254721Semaste    // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
664254721Semaste    // the target, 'cause that slows down single stepping.  So assume that if we got to the point where
665254721Semaste    // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
666254721Semaste    // about the fact that we are resuming...
667254721Semaste        const uint32_t process_stop_id = GetProcess()->GetStopID();
668254721Semaste    if (m_stop_info_stop_id == process_stop_id &&
669254721Semaste        (m_stop_info_sp && m_stop_info_sp->IsValid()))
670254721Semaste    {
671254721Semaste        StopInfo *stop_info = GetPrivateStopInfo().get();
672254721Semaste        if (stop_info)
673254721Semaste            stop_info->WillResume (resume_state);
674254721Semaste    }
675254721Semaste
676254721Semaste    // Tell all the plans that we are about to resume in case they need to clear any state.
677254721Semaste    // We distinguish between the plan on the top of the stack and the lower
678254721Semaste    // plans in case a plan needs to do any special business before it runs.
679254721Semaste
680254721Semaste    bool need_to_resume = false;
681254721Semaste    ThreadPlan *plan_ptr = GetCurrentPlan();
682254721Semaste    if (plan_ptr)
683254721Semaste    {
684254721Semaste        need_to_resume = plan_ptr->WillResume(resume_state, true);
685254721Semaste
686254721Semaste        while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
687254721Semaste        {
688254721Semaste            plan_ptr->WillResume (resume_state, false);
689254721Semaste        }
690254721Semaste
691254721Semaste        // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
692254721Semaste        // In that case, don't reset it here.
693254721Semaste
694254721Semaste        if (need_to_resume && resume_state != eStateSuspended)
695254721Semaste        {
696254721Semaste            m_stop_info_sp.reset();
697254721Semaste        }
698254721Semaste    }
699254721Semaste
700254721Semaste    if (need_to_resume)
701254721Semaste    {
702254721Semaste        ClearStackFrames();
703254721Semaste        // Let Thread subclasses do any special work they need to prior to resuming
704254721Semaste        WillResume (resume_state);
705254721Semaste    }
706254721Semaste
707254721Semaste    return need_to_resume;
708254721Semaste}
709254721Semaste
710254721Semastevoid
711254721SemasteThread::DidResume ()
712254721Semaste{
713254721Semaste    SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
714254721Semaste}
715254721Semaste
716254721Semastevoid
717254721SemasteThread::DidStop ()
718254721Semaste{
719254721Semaste    SetState (eStateStopped);
720254721Semaste}
721254721Semaste
722254721Semastebool
723254721SemasteThread::ShouldStop (Event* event_ptr)
724254721Semaste{
725254721Semaste    ThreadPlan *current_plan = GetCurrentPlan();
726254721Semaste
727254721Semaste    bool should_stop = true;
728254721Semaste
729254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
730254721Semaste
731254721Semaste    if (GetResumeState () == eStateSuspended)
732254721Semaste    {
733254721Semaste        if (log)
734254721Semaste            log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
735254721Semaste                         __FUNCTION__,
736254721Semaste                         GetID (),
737254721Semaste                         GetProtocolID());
738254721Semaste        return false;
739254721Semaste    }
740254721Semaste
741254721Semaste    if (GetTemporaryResumeState () == eStateSuspended)
742254721Semaste    {
743254721Semaste        if (log)
744254721Semaste            log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
745254721Semaste                         __FUNCTION__,
746254721Semaste                         GetID (),
747254721Semaste                         GetProtocolID());
748254721Semaste        return false;
749254721Semaste    }
750254721Semaste
751254721Semaste    // Based on the current thread plan and process stop info, check if this
752254721Semaste    // thread caused the process to stop. NOTE: this must take place before
753254721Semaste    // the plan is moved from the current plan stack to the completed plan
754254721Semaste    // stack.
755254721Semaste    if (ThreadStoppedForAReason() == false)
756254721Semaste    {
757254721Semaste        if (log)
758254721Semaste            log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64 ", should_stop = 0 (ignore since no stop reason)",
759254721Semaste                         __FUNCTION__,
760254721Semaste                         GetID (),
761254721Semaste                         GetProtocolID(),
762254721Semaste                         GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
763254721Semaste        return false;
764254721Semaste    }
765254721Semaste
766254721Semaste    if (log)
767254721Semaste    {
768254721Semaste        log->Printf ("Thread::%s(%p) for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64,
769254721Semaste                     __FUNCTION__,
770254721Semaste                     this,
771254721Semaste                     GetID (),
772254721Semaste                     GetProtocolID (),
773254721Semaste                     GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
774254721Semaste        log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
775254721Semaste        StreamString s;
776254721Semaste        s.IndentMore();
777254721Semaste        DumpThreadPlans(&s);
778254721Semaste        log->Printf ("Plan stack initial state:\n%s", s.GetData());
779254721Semaste    }
780254721Semaste
781254721Semaste    // The top most plan always gets to do the trace log...
782254721Semaste    current_plan->DoTraceLog ();
783254721Semaste
784254721Semaste    // First query the stop info's ShouldStopSynchronous.  This handles "synchronous" stop reasons, for example the breakpoint
785254721Semaste    // command on internal breakpoints.  If a synchronous stop reason says we should not stop, then we don't have to
786254721Semaste    // do any more work on this stop.
787254721Semaste    StopInfoSP private_stop_info (GetPrivateStopInfo());
788254721Semaste    if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
789254721Semaste    {
790254721Semaste        if (log)
791254721Semaste            log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
792254721Semaste        return false;
793254721Semaste    }
794254721Semaste
795254721Semaste    // If we've already been restarted, don't query the plans since the state they would examine is not current.
796254721Semaste    if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
797254721Semaste        return false;
798254721Semaste
799254721Semaste    // Before the plans see the state of the world, calculate the current inlined depth.
800254721Semaste    GetStackFrameList()->CalculateCurrentInlinedDepth();
801254721Semaste
802254721Semaste    // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
803254721Semaste    // If that plan is still working, then we don't need to do any more work.  If the plan that explains
804254721Semaste    // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
805254721Semaste    // whether they still need to do more work.
806254721Semaste
807254721Semaste    bool done_processing_current_plan = false;
808254721Semaste
809254721Semaste    if (!current_plan->PlanExplainsStop(event_ptr))
810254721Semaste    {
811254721Semaste        if (current_plan->TracerExplainsStop())
812254721Semaste        {
813254721Semaste            done_processing_current_plan = true;
814254721Semaste            should_stop = false;
815254721Semaste        }
816254721Semaste        else
817254721Semaste        {
818254721Semaste            // If the current plan doesn't explain the stop, then find one that
819254721Semaste            // does and let it handle the situation.
820254721Semaste            ThreadPlan *plan_ptr = current_plan;
821254721Semaste            while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
822254721Semaste            {
823254721Semaste                if (plan_ptr->PlanExplainsStop(event_ptr))
824254721Semaste                {
825254721Semaste                    should_stop = plan_ptr->ShouldStop (event_ptr);
826254721Semaste
827254721Semaste                    // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
828254721Semaste                    // and all the plans below it off the stack.
829254721Semaste
830254721Semaste                    if (plan_ptr->MischiefManaged())
831254721Semaste                    {
832254721Semaste                        // We're going to pop the plans up to and including the plan that explains the stop.
833254721Semaste                        ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
834254721Semaste
835254721Semaste                        do
836254721Semaste                        {
837254721Semaste                            if (should_stop)
838254721Semaste                                current_plan->WillStop();
839254721Semaste                            PopPlan();
840254721Semaste                        }
841254721Semaste                        while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
842254721Semaste                        // Now, if the responsible plan was not "Okay to discard" then we're done,
843254721Semaste                        // otherwise we forward this to the next plan in the stack below.
844254721Semaste                        if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
845254721Semaste                            done_processing_current_plan = true;
846254721Semaste                        else
847254721Semaste                            done_processing_current_plan = false;
848254721Semaste                    }
849254721Semaste                    else
850254721Semaste                        done_processing_current_plan = true;
851254721Semaste
852254721Semaste                    break;
853254721Semaste                }
854254721Semaste
855254721Semaste            }
856254721Semaste        }
857254721Semaste    }
858254721Semaste
859254721Semaste    if (!done_processing_current_plan)
860254721Semaste    {
861254721Semaste        bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
862254721Semaste
863254721Semaste        if (log)
864254721Semaste            log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
865254721Semaste
866254721Semaste        // We're starting from the base plan, so just let it decide;
867254721Semaste        if (PlanIsBasePlan(current_plan))
868254721Semaste        {
869254721Semaste            should_stop = current_plan->ShouldStop (event_ptr);
870254721Semaste            if (log)
871254721Semaste                log->Printf("Base plan says should stop: %i.", should_stop);
872254721Semaste        }
873254721Semaste        else
874254721Semaste        {
875254721Semaste            // Otherwise, don't let the base plan override what the other plans say to do, since
876254721Semaste            // presumably if there were other plans they would know what to do...
877254721Semaste            while (1)
878254721Semaste            {
879254721Semaste                if (PlanIsBasePlan(current_plan))
880254721Semaste                    break;
881254721Semaste
882254721Semaste                should_stop = current_plan->ShouldStop(event_ptr);
883254721Semaste                if (log)
884254721Semaste                    log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
885254721Semaste                if (current_plan->MischiefManaged())
886254721Semaste                {
887254721Semaste                    if (should_stop)
888254721Semaste                        current_plan->WillStop();
889254721Semaste
890254721Semaste                    // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
891254721Semaste                    // Otherwise, see if the plan's parent wants to stop.
892254721Semaste
893254721Semaste                    if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
894254721Semaste                    {
895254721Semaste                        PopPlan();
896254721Semaste                        break;
897254721Semaste                    }
898254721Semaste                    else
899254721Semaste                    {
900254721Semaste
901254721Semaste                        PopPlan();
902254721Semaste
903254721Semaste                        current_plan = GetCurrentPlan();
904254721Semaste                        if (current_plan == NULL)
905254721Semaste                        {
906254721Semaste                            break;
907254721Semaste                        }
908254721Semaste                    }
909254721Semaste                }
910254721Semaste                else
911254721Semaste                {
912254721Semaste                    break;
913254721Semaste                }
914254721Semaste            }
915254721Semaste        }
916254721Semaste
917254721Semaste        if (over_ride_stop)
918254721Semaste            should_stop = false;
919254721Semaste
920254721Semaste        // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
921254721Semaste        // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
922254721Semaste        // past the end point condition of the initial plan.  We don't want to strand the original plan on the stack,
923254721Semaste        // This code clears stale plans off the stack.
924254721Semaste
925254721Semaste        if (should_stop)
926254721Semaste        {
927254721Semaste            ThreadPlan *plan_ptr = GetCurrentPlan();
928254721Semaste            while (!PlanIsBasePlan(plan_ptr))
929254721Semaste            {
930254721Semaste                bool stale = plan_ptr->IsPlanStale ();
931254721Semaste                ThreadPlan *examined_plan = plan_ptr;
932254721Semaste                plan_ptr = GetPreviousPlan (examined_plan);
933254721Semaste
934254721Semaste                if (stale)
935254721Semaste                {
936254721Semaste                    if (log)
937254721Semaste                        log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
938254721Semaste                    DiscardThreadPlansUpToPlan(examined_plan);
939254721Semaste                }
940254721Semaste            }
941254721Semaste        }
942254721Semaste
943254721Semaste    }
944254721Semaste
945254721Semaste    if (log)
946254721Semaste    {
947254721Semaste        StreamString s;
948254721Semaste        s.IndentMore();
949254721Semaste        DumpThreadPlans(&s);
950254721Semaste        log->Printf ("Plan stack final state:\n%s", s.GetData());
951254721Semaste        log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
952254721Semaste    }
953254721Semaste    return should_stop;
954254721Semaste}
955254721Semaste
956254721SemasteVote
957254721SemasteThread::ShouldReportStop (Event* event_ptr)
958254721Semaste{
959254721Semaste    StateType thread_state = GetResumeState ();
960254721Semaste    StateType temp_thread_state = GetTemporaryResumeState();
961254721Semaste
962254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
963254721Semaste
964254721Semaste    if (thread_state == eStateSuspended || thread_state == eStateInvalid)
965254721Semaste    {
966254721Semaste        if (log)
967254721Semaste            log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (state was suspended or invalid)", GetID(), eVoteNoOpinion);
968254721Semaste        return eVoteNoOpinion;
969254721Semaste    }
970254721Semaste
971254721Semaste    if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
972254721Semaste    {
973254721Semaste        if (log)
974254721Semaste            log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (temporary state was suspended or invalid)", GetID(), eVoteNoOpinion);
975254721Semaste        return eVoteNoOpinion;
976254721Semaste    }
977254721Semaste
978254721Semaste    if (!ThreadStoppedForAReason())
979254721Semaste    {
980254721Semaste        if (log)
981254721Semaste            log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (thread didn't stop for a reason.)", GetID(), eVoteNoOpinion);
982254721Semaste        return eVoteNoOpinion;
983254721Semaste    }
984254721Semaste
985254721Semaste    if (m_completed_plan_stack.size() > 0)
986254721Semaste    {
987254721Semaste        // Don't use GetCompletedPlan here, since that suppresses private plans.
988254721Semaste        if (log)
989254721Semaste            log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote  for complete stack's back plan", GetID());
990254721Semaste        return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
991254721Semaste    }
992254721Semaste    else
993254721Semaste    {
994254721Semaste        Vote thread_vote = eVoteNoOpinion;
995254721Semaste        ThreadPlan *plan_ptr = GetCurrentPlan();
996254721Semaste        while (1)
997254721Semaste        {
998254721Semaste            if (plan_ptr->PlanExplainsStop(event_ptr))
999254721Semaste            {
1000254721Semaste                thread_vote = plan_ptr->ShouldReportStop(event_ptr);
1001254721Semaste                break;
1002254721Semaste            }
1003254721Semaste            if (PlanIsBasePlan(plan_ptr))
1004254721Semaste                break;
1005254721Semaste            else
1006254721Semaste                plan_ptr = GetPreviousPlan(plan_ptr);
1007254721Semaste        }
1008254721Semaste        if (log)
1009254721Semaste            log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i for current plan", GetID(), thread_vote);
1010254721Semaste
1011254721Semaste        return thread_vote;
1012254721Semaste    }
1013254721Semaste}
1014254721Semaste
1015254721SemasteVote
1016254721SemasteThread::ShouldReportRun (Event* event_ptr)
1017254721Semaste{
1018254721Semaste    StateType thread_state = GetResumeState ();
1019254721Semaste
1020254721Semaste    if (thread_state == eStateSuspended
1021254721Semaste            || thread_state == eStateInvalid)
1022254721Semaste    {
1023254721Semaste        return eVoteNoOpinion;
1024254721Semaste    }
1025254721Semaste
1026254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1027254721Semaste    if (m_completed_plan_stack.size() > 0)
1028254721Semaste    {
1029254721Semaste        // Don't use GetCompletedPlan here, since that suppresses private plans.
1030254721Semaste        if (log)
1031254721Semaste            log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
1032254721Semaste                         GetIndexID(),
1033254721Semaste                         this,
1034254721Semaste                         GetID(),
1035254721Semaste                         StateAsCString(GetTemporaryResumeState()),
1036254721Semaste                         m_completed_plan_stack.back()->GetName());
1037254721Semaste
1038254721Semaste        return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
1039254721Semaste    }
1040254721Semaste    else
1041254721Semaste    {
1042254721Semaste        if (log)
1043254721Semaste            log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
1044254721Semaste                         GetIndexID(),
1045254721Semaste                         this,
1046254721Semaste                         GetID(),
1047254721Semaste                         StateAsCString(GetTemporaryResumeState()),
1048254721Semaste                         GetCurrentPlan()->GetName());
1049254721Semaste
1050254721Semaste        return GetCurrentPlan()->ShouldReportRun (event_ptr);
1051254721Semaste     }
1052254721Semaste}
1053254721Semaste
1054254721Semastebool
1055254721SemasteThread::MatchesSpec (const ThreadSpec *spec)
1056254721Semaste{
1057254721Semaste    if (spec == NULL)
1058254721Semaste        return true;
1059254721Semaste
1060254721Semaste    return spec->ThreadPassesBasicTests(*this);
1061254721Semaste}
1062254721Semaste
1063254721Semastevoid
1064254721SemasteThread::PushPlan (ThreadPlanSP &thread_plan_sp)
1065254721Semaste{
1066254721Semaste    if (thread_plan_sp)
1067254721Semaste    {
1068254721Semaste        // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1069254721Semaste        if (!thread_plan_sp->GetThreadPlanTracer())
1070254721Semaste            thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
1071254721Semaste        m_plan_stack.push_back (thread_plan_sp);
1072254721Semaste
1073254721Semaste        thread_plan_sp->DidPush();
1074254721Semaste
1075254721Semaste        Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1076254721Semaste        if (log)
1077254721Semaste        {
1078254721Semaste            StreamString s;
1079254721Semaste            thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
1080254721Semaste            log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
1081254721Semaste                        this,
1082254721Semaste                        s.GetData(),
1083254721Semaste                        thread_plan_sp->GetThread().GetID());
1084254721Semaste        }
1085254721Semaste    }
1086254721Semaste}
1087254721Semaste
1088254721Semastevoid
1089254721SemasteThread::PopPlan ()
1090254721Semaste{
1091254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1092254721Semaste
1093254721Semaste    if (m_plan_stack.size() <= 1)
1094254721Semaste        return;
1095254721Semaste    else
1096254721Semaste    {
1097254721Semaste        ThreadPlanSP &plan = m_plan_stack.back();
1098254721Semaste        if (log)
1099254721Semaste        {
1100254721Semaste            log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1101254721Semaste        }
1102254721Semaste        m_completed_plan_stack.push_back (plan);
1103254721Semaste        plan->WillPop();
1104254721Semaste        m_plan_stack.pop_back();
1105254721Semaste    }
1106254721Semaste}
1107254721Semaste
1108254721Semastevoid
1109254721SemasteThread::DiscardPlan ()
1110254721Semaste{
1111254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1112254721Semaste    if (m_plan_stack.size() > 1)
1113254721Semaste    {
1114254721Semaste        ThreadPlanSP &plan = m_plan_stack.back();
1115254721Semaste        if (log)
1116254721Semaste            log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1117254721Semaste
1118254721Semaste        m_discarded_plan_stack.push_back (plan);
1119254721Semaste        plan->WillPop();
1120254721Semaste        m_plan_stack.pop_back();
1121254721Semaste    }
1122254721Semaste}
1123254721Semaste
1124254721SemasteThreadPlan *
1125254721SemasteThread::GetCurrentPlan ()
1126254721Semaste{
1127254721Semaste    // There will always be at least the base plan.  If somebody is mucking with a
1128254721Semaste    // thread with an empty plan stack, we should assert right away.
1129254721Semaste    if (m_plan_stack.empty())
1130254721Semaste        return NULL;
1131254721Semaste    return m_plan_stack.back().get();
1132254721Semaste}
1133254721Semaste
1134254721SemasteThreadPlanSP
1135254721SemasteThread::GetCompletedPlan ()
1136254721Semaste{
1137254721Semaste    ThreadPlanSP empty_plan_sp;
1138254721Semaste    if (!m_completed_plan_stack.empty())
1139254721Semaste    {
1140254721Semaste        for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1141254721Semaste        {
1142254721Semaste            ThreadPlanSP completed_plan_sp;
1143254721Semaste            completed_plan_sp = m_completed_plan_stack[i];
1144254721Semaste            if (!completed_plan_sp->GetPrivate ())
1145254721Semaste            return completed_plan_sp;
1146254721Semaste        }
1147254721Semaste    }
1148254721Semaste    return empty_plan_sp;
1149254721Semaste}
1150254721Semaste
1151254721SemasteValueObjectSP
1152254721SemasteThread::GetReturnValueObject ()
1153254721Semaste{
1154254721Semaste    if (!m_completed_plan_stack.empty())
1155254721Semaste    {
1156254721Semaste        for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1157254721Semaste        {
1158254721Semaste            ValueObjectSP return_valobj_sp;
1159254721Semaste            return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1160254721Semaste            if (return_valobj_sp)
1161254721Semaste            return return_valobj_sp;
1162254721Semaste        }
1163254721Semaste    }
1164254721Semaste    return ValueObjectSP();
1165254721Semaste}
1166254721Semaste
1167254721Semastebool
1168254721SemasteThread::IsThreadPlanDone (ThreadPlan *plan)
1169254721Semaste{
1170254721Semaste    if (!m_completed_plan_stack.empty())
1171254721Semaste    {
1172254721Semaste        for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1173254721Semaste        {
1174254721Semaste            if (m_completed_plan_stack[i].get() == plan)
1175254721Semaste                return true;
1176254721Semaste        }
1177254721Semaste    }
1178254721Semaste    return false;
1179254721Semaste}
1180254721Semaste
1181254721Semastebool
1182254721SemasteThread::WasThreadPlanDiscarded (ThreadPlan *plan)
1183254721Semaste{
1184254721Semaste    if (!m_discarded_plan_stack.empty())
1185254721Semaste    {
1186254721Semaste        for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1187254721Semaste        {
1188254721Semaste            if (m_discarded_plan_stack[i].get() == plan)
1189254721Semaste                return true;
1190254721Semaste        }
1191254721Semaste    }
1192254721Semaste    return false;
1193254721Semaste}
1194254721Semaste
1195254721SemasteThreadPlan *
1196254721SemasteThread::GetPreviousPlan (ThreadPlan *current_plan)
1197254721Semaste{
1198254721Semaste    if (current_plan == NULL)
1199254721Semaste        return NULL;
1200254721Semaste
1201254721Semaste    int stack_size = m_completed_plan_stack.size();
1202254721Semaste    for (int i = stack_size - 1; i > 0; i--)
1203254721Semaste    {
1204254721Semaste        if (current_plan == m_completed_plan_stack[i].get())
1205254721Semaste            return m_completed_plan_stack[i-1].get();
1206254721Semaste    }
1207254721Semaste
1208254721Semaste    if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1209254721Semaste    {
1210254721Semaste        if (m_plan_stack.size() > 0)
1211254721Semaste            return m_plan_stack.back().get();
1212254721Semaste        else
1213254721Semaste            return NULL;
1214254721Semaste    }
1215254721Semaste
1216254721Semaste    stack_size = m_plan_stack.size();
1217254721Semaste    for (int i = stack_size - 1; i > 0; i--)
1218254721Semaste    {
1219254721Semaste        if (current_plan == m_plan_stack[i].get())
1220254721Semaste            return m_plan_stack[i-1].get();
1221254721Semaste    }
1222254721Semaste    return NULL;
1223254721Semaste}
1224254721Semaste
1225254721Semastevoid
1226254721SemasteThread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1227254721Semaste{
1228254721Semaste    if (abort_other_plans)
1229254721Semaste       DiscardThreadPlans(true);
1230254721Semaste
1231254721Semaste    PushPlan (thread_plan_sp);
1232254721Semaste}
1233254721Semaste
1234254721Semaste
1235254721Semastevoid
1236254721SemasteThread::EnableTracer (bool value, bool single_stepping)
1237254721Semaste{
1238254721Semaste    int stack_size = m_plan_stack.size();
1239254721Semaste    for (int i = 0; i < stack_size; i++)
1240254721Semaste    {
1241254721Semaste        if (m_plan_stack[i]->GetThreadPlanTracer())
1242254721Semaste        {
1243254721Semaste            m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1244254721Semaste            m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1245254721Semaste        }
1246254721Semaste    }
1247254721Semaste}
1248254721Semaste
1249254721Semastevoid
1250254721SemasteThread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1251254721Semaste{
1252254721Semaste    int stack_size = m_plan_stack.size();
1253254721Semaste    for (int i = 0; i < stack_size; i++)
1254254721Semaste        m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1255254721Semaste}
1256254721Semaste
1257254721Semastevoid
1258254721SemasteThread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1259254721Semaste{
1260254721Semaste    DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1261254721Semaste}
1262254721Semaste
1263254721Semastevoid
1264254721SemasteThread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1265254721Semaste{
1266254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1267254721Semaste    if (log)
1268254721Semaste    {
1269254721Semaste        log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p", GetID(), up_to_plan_ptr);
1270254721Semaste    }
1271254721Semaste
1272254721Semaste    int stack_size = m_plan_stack.size();
1273254721Semaste
1274254721Semaste    // If the input plan is NULL, discard all plans.  Otherwise make sure this plan is in the
1275254721Semaste    // stack, and if so discard up to and including it.
1276254721Semaste
1277254721Semaste    if (up_to_plan_ptr == NULL)
1278254721Semaste    {
1279254721Semaste        for (int i = stack_size - 1; i > 0; i--)
1280254721Semaste            DiscardPlan();
1281254721Semaste    }
1282254721Semaste    else
1283254721Semaste    {
1284254721Semaste        bool found_it = false;
1285254721Semaste        for (int i = stack_size - 1; i > 0; i--)
1286254721Semaste        {
1287254721Semaste            if (m_plan_stack[i].get() == up_to_plan_ptr)
1288254721Semaste                found_it = true;
1289254721Semaste        }
1290254721Semaste        if (found_it)
1291254721Semaste        {
1292254721Semaste            bool last_one = false;
1293254721Semaste            for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1294254721Semaste            {
1295254721Semaste                if (GetCurrentPlan() == up_to_plan_ptr)
1296254721Semaste                    last_one = true;
1297254721Semaste                DiscardPlan();
1298254721Semaste            }
1299254721Semaste        }
1300254721Semaste    }
1301254721Semaste    return;
1302254721Semaste}
1303254721Semaste
1304254721Semastevoid
1305254721SemasteThread::DiscardThreadPlans(bool force)
1306254721Semaste{
1307254721Semaste    Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
1308254721Semaste    if (log)
1309254721Semaste    {
1310254721Semaste        log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
1311254721Semaste    }
1312254721Semaste
1313254721Semaste    if (force)
1314254721Semaste    {
1315254721Semaste        int stack_size = m_plan_stack.size();
1316254721Semaste        for (int i = stack_size - 1; i > 0; i--)
1317254721Semaste        {
1318254721Semaste            DiscardPlan();
1319254721Semaste        }
1320254721Semaste        return;
1321254721Semaste    }
1322254721Semaste
1323254721Semaste    while (1)
1324254721Semaste    {
1325254721Semaste
1326254721Semaste        int master_plan_idx;
1327254721Semaste        bool discard = true;
1328254721Semaste
1329254721Semaste        // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1330254721Semaste        for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1331254721Semaste        {
1332254721Semaste            if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1333254721Semaste            {
1334254721Semaste                discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1335254721Semaste                break;
1336254721Semaste            }
1337254721Semaste        }
1338254721Semaste
1339254721Semaste        if (discard)
1340254721Semaste        {
1341254721Semaste            // First pop all the dependent plans:
1342254721Semaste            for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1343254721Semaste            {
1344254721Semaste
1345254721Semaste                // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1346254721Semaste                // for the plan leaves it in a state that it is safe to pop the plan
1347254721Semaste                // with no more notice?
1348254721Semaste                DiscardPlan();
1349254721Semaste            }
1350254721Semaste
1351254721Semaste            // Now discard the master plan itself.
1352254721Semaste            // The bottom-most plan never gets discarded.  "OkayToDiscard" for it means
1353254721Semaste            // discard it's dependent plans, but not it...
1354254721Semaste            if (master_plan_idx > 0)
1355254721Semaste            {
1356254721Semaste                DiscardPlan();
1357254721Semaste            }
1358254721Semaste        }
1359254721Semaste        else
1360254721Semaste        {
1361254721Semaste            // If the master plan doesn't want to get discarded, then we're done.
1362254721Semaste            break;
1363254721Semaste        }
1364254721Semaste
1365254721Semaste    }
1366254721Semaste}
1367254721Semaste
1368254721Semastebool
1369254721SemasteThread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1370254721Semaste{
1371254721Semaste    if (plan_ptr->IsBasePlan())
1372254721Semaste        return true;
1373254721Semaste    else if (m_plan_stack.size() == 0)
1374254721Semaste        return false;
1375254721Semaste    else
1376254721Semaste       return m_plan_stack[0].get() == plan_ptr;
1377254721Semaste}
1378254721Semaste
1379254721SemasteError
1380254721SemasteThread::UnwindInnermostExpression()
1381254721Semaste{
1382254721Semaste    Error error;
1383254721Semaste    int stack_size = m_plan_stack.size();
1384254721Semaste
1385254721Semaste    // If the input plan is NULL, discard all plans.  Otherwise make sure this plan is in the
1386254721Semaste    // stack, and if so discard up to and including it.
1387254721Semaste
1388254721Semaste    for (int i = stack_size - 1; i > 0; i--)
1389254721Semaste    {
1390254721Semaste        if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1391254721Semaste        {
1392254721Semaste            DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1393254721Semaste            return error;
1394254721Semaste        }
1395254721Semaste    }
1396254721Semaste    error.SetErrorString("No expressions currently active on this thread");
1397254721Semaste    return error;
1398254721Semaste}
1399254721Semaste
1400254721Semaste
1401254721SemasteThreadPlanSP
1402254721SemasteThread::QueueFundamentalPlan (bool abort_other_plans)
1403254721Semaste{
1404254721Semaste    ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1405254721Semaste    QueueThreadPlan (thread_plan_sp, abort_other_plans);
1406254721Semaste    return thread_plan_sp;
1407254721Semaste}
1408254721Semaste
1409254721SemasteThreadPlanSP
1410254721SemasteThread::QueueThreadPlanForStepSingleInstruction
1411254721Semaste(
1412254721Semaste    bool step_over,
1413254721Semaste    bool abort_other_plans,
1414254721Semaste    bool stop_other_threads
1415254721Semaste)
1416254721Semaste{
1417254721Semaste    ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1418254721Semaste    QueueThreadPlan (thread_plan_sp, abort_other_plans);
1419254721Semaste    return thread_plan_sp;
1420254721Semaste}
1421254721Semaste
1422254721SemasteThreadPlanSP
1423254721SemasteThread::QueueThreadPlanForStepOverRange
1424254721Semaste(
1425254721Semaste    bool abort_other_plans,
1426254721Semaste    const AddressRange &range,
1427254721Semaste    const SymbolContext &addr_context,
1428254721Semaste    lldb::RunMode stop_other_threads
1429254721Semaste)
1430254721Semaste{
1431254721Semaste    ThreadPlanSP thread_plan_sp;
1432254721Semaste    thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1433254721Semaste
1434254721Semaste    QueueThreadPlan (thread_plan_sp, abort_other_plans);
1435254721Semaste    return thread_plan_sp;
1436254721Semaste}
1437254721Semaste
1438254721SemasteThreadPlanSP
1439254721SemasteThread::QueueThreadPlanForStepInRange
1440254721Semaste(
1441254721Semaste    bool abort_other_plans,
1442254721Semaste    const AddressRange &range,
1443254721Semaste    const SymbolContext &addr_context,
1444254721Semaste    const char *step_in_target,
1445254721Semaste    lldb::RunMode stop_other_threads,
1446254721Semaste    bool avoid_code_without_debug_info
1447254721Semaste)
1448254721Semaste{
1449254721Semaste    ThreadPlanSP thread_plan_sp;
1450254721Semaste    ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1451254721Semaste    if (avoid_code_without_debug_info)
1452254721Semaste        plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1453254721Semaste    else
1454254721Semaste        plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1455254721Semaste    if (step_in_target)
1456254721Semaste        plan->SetStepInTarget(step_in_target);
1457254721Semaste    thread_plan_sp.reset (plan);
1458254721Semaste
1459254721Semaste    QueueThreadPlan (thread_plan_sp, abort_other_plans);
1460254721Semaste    return thread_plan_sp;
1461254721Semaste}
1462254721Semaste
1463254721Semaste
1464254721SemasteThreadPlanSP
1465254721SemasteThread::QueueThreadPlanForStepOut
1466254721Semaste(
1467254721Semaste    bool abort_other_plans,
1468254721Semaste    SymbolContext *addr_context,
1469254721Semaste    bool first_insn,
1470254721Semaste    bool stop_other_threads,
1471254721Semaste    Vote stop_vote,
1472254721Semaste    Vote run_vote,
1473254721Semaste    uint32_t frame_idx
1474254721Semaste)
1475254721Semaste{
1476254721Semaste    ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1477254721Semaste                                                        addr_context,
1478254721Semaste                                                        first_insn,
1479254721Semaste                                                        stop_other_threads,
1480254721Semaste                                                        stop_vote,
1481254721Semaste                                                        run_vote,
1482254721Semaste                                                        frame_idx));
1483254721Semaste
1484254721Semaste    if (thread_plan_sp->ValidatePlan(NULL))
1485254721Semaste    {
1486254721Semaste        QueueThreadPlan (thread_plan_sp, abort_other_plans);
1487254721Semaste        return thread_plan_sp;
1488254721Semaste    }
1489254721Semaste    else
1490254721Semaste    {
1491254721Semaste        return ThreadPlanSP();
1492254721Semaste    }
1493254721Semaste}
1494254721Semaste
1495254721SemasteThreadPlanSP
1496254721SemasteThread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
1497254721Semaste{
1498254721Semaste    ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
1499254721Semaste    if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1500254721Semaste        return ThreadPlanSP();
1501254721Semaste
1502254721Semaste    QueueThreadPlan (thread_plan_sp, abort_other_plans);
1503254721Semaste    return thread_plan_sp;
1504254721Semaste}
1505254721Semaste
1506254721SemasteThreadPlanSP
1507254721SemasteThread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1508254721Semaste                                        Address &target_addr,
1509254721Semaste                                        bool stop_other_threads)
1510254721Semaste{
1511254721Semaste    ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1512254721Semaste    QueueThreadPlan (thread_plan_sp, abort_other_plans);
1513254721Semaste    return thread_plan_sp;
1514254721Semaste}
1515254721Semaste
1516254721SemasteThreadPlanSP
1517254721SemasteThread::QueueThreadPlanForStepUntil (bool abort_other_plans,
1518254721Semaste                                     lldb::addr_t *address_list,
1519254721Semaste                                     size_t num_addresses,
1520254721Semaste                                     bool stop_other_threads,
1521254721Semaste                                     uint32_t frame_idx)
1522254721Semaste{
1523254721Semaste    ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
1524254721Semaste    QueueThreadPlan (thread_plan_sp, abort_other_plans);
1525254721Semaste    return thread_plan_sp;
1526254721Semaste
1527254721Semaste}
1528254721Semaste
1529254721Semasteuint32_t
1530254721SemasteThread::GetIndexID () const
1531254721Semaste{
1532254721Semaste    return m_index_id;
1533254721Semaste}
1534254721Semaste
1535254721Semastevoid
1536254721SemasteThread::DumpThreadPlans (lldb_private::Stream *s) const
1537254721Semaste{
1538254721Semaste    uint32_t stack_size = m_plan_stack.size();
1539254721Semaste    int i;
1540254721Semaste    s->Indent();
1541254721Semaste    s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4" PRIx64 ", stack_size = %d\n", GetIndexID(), GetID(), stack_size);
1542254721Semaste    for (i = stack_size - 1; i >= 0; i--)
1543254721Semaste    {
1544254721Semaste        s->IndentMore();
1545254721Semaste        s->Indent();
1546254721Semaste        s->Printf ("Element %d: ", i);
1547254721Semaste        m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1548254721Semaste        s->EOL();
1549254721Semaste        s->IndentLess();
1550254721Semaste    }
1551254721Semaste
1552254721Semaste    stack_size = m_completed_plan_stack.size();
1553254721Semaste    if (stack_size > 0)
1554254721Semaste    {
1555254721Semaste        s->Indent();
1556254721Semaste        s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1557254721Semaste        for (i = stack_size - 1; i >= 0; i--)
1558254721Semaste        {
1559254721Semaste            s->IndentMore();
1560254721Semaste            s->Indent();
1561254721Semaste            s->Printf ("Element %d: ", i);
1562254721Semaste            m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1563254721Semaste            s->EOL();
1564254721Semaste            s->IndentLess();
1565254721Semaste        }
1566254721Semaste    }
1567254721Semaste
1568254721Semaste    stack_size = m_discarded_plan_stack.size();
1569254721Semaste    if (stack_size > 0)
1570254721Semaste    {
1571254721Semaste        s->Indent();
1572254721Semaste        s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1573254721Semaste        for (i = stack_size - 1; i >= 0; i--)
1574254721Semaste        {
1575254721Semaste            s->IndentMore();
1576254721Semaste            s->Indent();
1577254721Semaste            s->Printf ("Element %d: ", i);
1578254721Semaste            m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1579254721Semaste            s->EOL();
1580254721Semaste            s->IndentLess();
1581254721Semaste        }
1582254721Semaste    }
1583254721Semaste
1584254721Semaste}
1585254721Semaste
1586254721SemasteTargetSP
1587254721SemasteThread::CalculateTarget ()
1588254721Semaste{
1589254721Semaste    TargetSP target_sp;
1590254721Semaste    ProcessSP process_sp(GetProcess());
1591254721Semaste    if (process_sp)
1592254721Semaste        target_sp = process_sp->CalculateTarget();
1593254721Semaste    return target_sp;
1594254721Semaste
1595254721Semaste}
1596254721Semaste
1597254721SemasteProcessSP
1598254721SemasteThread::CalculateProcess ()
1599254721Semaste{
1600254721Semaste    return GetProcess();
1601254721Semaste}
1602254721Semaste
1603254721SemasteThreadSP
1604254721SemasteThread::CalculateThread ()
1605254721Semaste{
1606254721Semaste    return shared_from_this();
1607254721Semaste}
1608254721Semaste
1609254721SemasteStackFrameSP
1610254721SemasteThread::CalculateStackFrame ()
1611254721Semaste{
1612254721Semaste    return StackFrameSP();
1613254721Semaste}
1614254721Semaste
1615254721Semastevoid
1616254721SemasteThread::CalculateExecutionContext (ExecutionContext &exe_ctx)
1617254721Semaste{
1618254721Semaste    exe_ctx.SetContext (shared_from_this());
1619254721Semaste}
1620254721Semaste
1621254721Semaste
1622254721SemasteStackFrameListSP
1623254721SemasteThread::GetStackFrameList ()
1624254721Semaste{
1625254721Semaste    StackFrameListSP frame_list_sp;
1626254721Semaste    Mutex::Locker locker(m_frame_mutex);
1627254721Semaste    if (m_curr_frames_sp)
1628254721Semaste    {
1629254721Semaste        frame_list_sp = m_curr_frames_sp;
1630254721Semaste    }
1631254721Semaste    else
1632254721Semaste    {
1633254721Semaste        frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1634254721Semaste        m_curr_frames_sp = frame_list_sp;
1635254721Semaste    }
1636254721Semaste    return frame_list_sp;
1637254721Semaste}
1638254721Semaste
1639254721Semastevoid
1640254721SemasteThread::ClearStackFrames ()
1641254721Semaste{
1642254721Semaste    Mutex::Locker locker(m_frame_mutex);
1643254721Semaste
1644254721Semaste    Unwind *unwinder = GetUnwinder ();
1645254721Semaste    if (unwinder)
1646254721Semaste        unwinder->Clear();
1647254721Semaste
1648254721Semaste    // Only store away the old "reference" StackFrameList if we got all its frames:
1649254721Semaste    // FIXME: At some point we can try to splice in the frames we have fetched into
1650254721Semaste    // the new frame as we make it, but let's not try that now.
1651254721Semaste    if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
1652254721Semaste        m_prev_frames_sp.swap (m_curr_frames_sp);
1653254721Semaste    m_curr_frames_sp.reset();
1654254721Semaste}
1655254721Semaste
1656254721Semastelldb::StackFrameSP
1657254721SemasteThread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1658254721Semaste{
1659254721Semaste    return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
1660254721Semaste}
1661254721Semaste
1662254721Semaste
1663254721SemasteError
1664254721SemasteThread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
1665254721Semaste{
1666254721Semaste    StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1667254721Semaste    Error return_error;
1668254721Semaste
1669254721Semaste    if (!frame_sp)
1670254721Semaste    {
1671254721Semaste        return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
1672254721Semaste    }
1673254721Semaste
1674254721Semaste    return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
1675254721Semaste}
1676254721Semaste
1677254721SemasteError
1678254721SemasteThread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
1679254721Semaste{
1680254721Semaste    Error return_error;
1681254721Semaste
1682254721Semaste    if (!frame_sp)
1683254721Semaste    {
1684254721Semaste        return_error.SetErrorString("Can't return to a null frame.");
1685254721Semaste        return return_error;
1686254721Semaste    }
1687254721Semaste
1688254721Semaste    Thread *thread = frame_sp->GetThread().get();
1689254721Semaste    uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1690254721Semaste    StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
1691254721Semaste    if (!older_frame_sp)
1692254721Semaste    {
1693254721Semaste        return_error.SetErrorString("No older frame to return to.");
1694254721Semaste        return return_error;
1695254721Semaste    }
1696254721Semaste
1697254721Semaste    if (return_value_sp)
1698254721Semaste    {
1699254721Semaste        lldb::ABISP abi = thread->GetProcess()->GetABI();
1700254721Semaste        if (!abi)
1701254721Semaste        {
1702254721Semaste            return_error.SetErrorString("Could not find ABI to set return value.");
1703254721Semaste            return return_error;
1704254721Semaste        }
1705254721Semaste        SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1706254721Semaste
1707254721Semaste        // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1708254721Semaste        // Turn that back on when that works.
1709254721Semaste        if (0 && sc.function != NULL)
1710254721Semaste        {
1711254721Semaste            Type *function_type = sc.function->GetType();
1712254721Semaste            if (function_type)
1713254721Semaste            {
1714254721Semaste                ClangASTType return_type = sc.function->GetClangType().GetFunctionReturnType();
1715254721Semaste                if (return_type)
1716254721Semaste                {
1717254721Semaste                    StreamString s;
1718254721Semaste                    return_type.DumpTypeDescription(&s);
1719254721Semaste                    ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
1720254721Semaste                    if (cast_value_sp)
1721254721Semaste                    {
1722254721Semaste                        cast_value_sp->SetFormat(eFormatHex);
1723254721Semaste                        return_value_sp = cast_value_sp;
1724254721Semaste                    }
1725254721Semaste                }
1726254721Semaste            }
1727254721Semaste        }
1728254721Semaste
1729254721Semaste        return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
1730254721Semaste        if (!return_error.Success())
1731254721Semaste            return return_error;
1732254721Semaste    }
1733254721Semaste
1734254721Semaste    // Now write the return registers for the chosen frame:
1735254721Semaste    // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
1736254721Semaste    // cook their data
1737254721Semaste
1738254721Semaste    StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
1739254721Semaste    if (youngest_frame_sp)
1740254721Semaste    {
1741254721Semaste        lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1742254721Semaste        if (reg_ctx_sp)
1743254721Semaste        {
1744254721Semaste            bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1745254721Semaste            if (copy_success)
1746254721Semaste            {
1747254721Semaste                thread->DiscardThreadPlans(true);
1748254721Semaste                thread->ClearStackFrames();
1749254721Semaste                if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1750254721Semaste                    BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1751254721Semaste            }
1752254721Semaste            else
1753254721Semaste            {
1754254721Semaste                return_error.SetErrorString("Could not reset register values.");
1755254721Semaste            }
1756254721Semaste        }
1757254721Semaste        else
1758254721Semaste        {
1759254721Semaste            return_error.SetErrorString("Frame has no register context.");
1760254721Semaste        }
1761254721Semaste    }
1762254721Semaste    else
1763254721Semaste    {
1764254721Semaste        return_error.SetErrorString("Returned past top frame.");
1765254721Semaste    }
1766254721Semaste    return return_error;
1767254721Semaste}
1768254721Semaste
1769263363Semastestatic void DumpAddressList (Stream &s, const std::vector<Address> &list, ExecutionContextScope *exe_scope)
1770263363Semaste{
1771263363Semaste    for (size_t n=0;n<list.size();n++)
1772263363Semaste    {
1773263363Semaste        s << "\t";
1774263363Semaste        list[n].Dump (&s, exe_scope, Address::DumpStyleResolvedDescription, Address::DumpStyleSectionNameOffset);
1775263363Semaste        s << "\n";
1776263363Semaste    }
1777263363Semaste}
1778263363Semaste
1779263363SemasteError
1780263363SemasteThread::JumpToLine (const FileSpec &file, uint32_t line, bool can_leave_function, std::string *warnings)
1781263363Semaste{
1782263363Semaste    ExecutionContext exe_ctx (GetStackFrameAtIndex(0));
1783263363Semaste    Target *target = exe_ctx.GetTargetPtr();
1784263363Semaste    TargetSP target_sp = exe_ctx.GetTargetSP();
1785263363Semaste    RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
1786263363Semaste    StackFrame *frame = exe_ctx.GetFramePtr();
1787263363Semaste    const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
1788263363Semaste
1789263363Semaste    // Find candidate locations.
1790263363Semaste    std::vector<Address> candidates, within_function, outside_function;
1791263363Semaste    target->GetImages().FindAddressesForLine (target_sp, file, line, sc.function, within_function, outside_function);
1792263363Semaste
1793263363Semaste    // If possible, we try and stay within the current function.
1794263363Semaste    // Within a function, we accept multiple locations (optimized code may do this,
1795263363Semaste    // there's no solution here so we do the best we can).
1796263363Semaste    // However if we're trying to leave the function, we don't know how to pick the
1797263363Semaste    // right location, so if there's more than one then we bail.
1798263363Semaste    if (!within_function.empty())
1799263363Semaste        candidates = within_function;
1800263363Semaste    else if (outside_function.size() == 1 && can_leave_function)
1801263363Semaste        candidates = outside_function;
1802263363Semaste
1803263363Semaste    // Check if we got anything.
1804263363Semaste    if (candidates.empty())
1805263363Semaste    {
1806263363Semaste        if (outside_function.empty())
1807263363Semaste        {
1808263363Semaste            return Error("Cannot locate an address for %s:%i.",
1809263363Semaste                         file.GetFilename().AsCString(), line);
1810263363Semaste        }
1811263363Semaste        else if (outside_function.size() == 1)
1812263363Semaste        {
1813263363Semaste            return Error("%s:%i is outside the current function.",
1814263363Semaste                         file.GetFilename().AsCString(), line);
1815263363Semaste        }
1816263363Semaste        else
1817263363Semaste        {
1818263363Semaste            StreamString sstr;
1819263363Semaste            DumpAddressList(sstr, outside_function, target);
1820263363Semaste            return Error("%s:%i has multiple candidate locations:\n%s",
1821263363Semaste                         file.GetFilename().AsCString(), line, sstr.GetString().c_str());
1822263363Semaste        }
1823263363Semaste    }
1824263363Semaste
1825263363Semaste    // Accept the first location, warn about any others.
1826263363Semaste    Address dest = candidates[0];
1827263363Semaste    if (warnings && candidates.size() > 1)
1828263363Semaste    {
1829263363Semaste        StreamString sstr;
1830263363Semaste        sstr.Printf("%s:%i appears multiple times in this function, selecting the first location:\n",
1831263363Semaste                     file.GetFilename().AsCString(), line);
1832263363Semaste        DumpAddressList(sstr, candidates, target);
1833263363Semaste        *warnings = sstr.GetString();
1834263363Semaste    }
1835263363Semaste
1836263363Semaste    if (!reg_ctx->SetPC (dest))
1837263363Semaste        return Error("Cannot change PC to target address.");
1838263363Semaste
1839263363Semaste    return Error();
1840263363Semaste}
1841263363Semaste
1842254721Semastevoid
1843254721SemasteThread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
1844254721Semaste{
1845254721Semaste    ExecutionContext exe_ctx (shared_from_this());
1846254721Semaste    Process *process = exe_ctx.GetProcessPtr();
1847254721Semaste    if (process == NULL)
1848254721Semaste        return;
1849254721Semaste
1850254721Semaste    StackFrameSP frame_sp;
1851254721Semaste    SymbolContext frame_sc;
1852254721Semaste    if (frame_idx != LLDB_INVALID_INDEX32)
1853254721Semaste    {
1854254721Semaste        frame_sp = GetStackFrameAtIndex (frame_idx);
1855254721Semaste        if (frame_sp)
1856254721Semaste        {
1857254721Semaste            exe_ctx.SetFrameSP(frame_sp);
1858254721Semaste            frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
1859254721Semaste        }
1860254721Semaste    }
1861254721Semaste
1862254721Semaste    const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
1863254721Semaste    assert (thread_format);
1864254721Semaste    Debugger::FormatPrompt (thread_format,
1865254721Semaste                            frame_sp ? &frame_sc : NULL,
1866254721Semaste                            &exe_ctx,
1867254721Semaste                            NULL,
1868254721Semaste                            strm);
1869254721Semaste}
1870254721Semaste
1871254721Semastevoid
1872254721SemasteThread::SettingsInitialize ()
1873254721Semaste{
1874254721Semaste}
1875254721Semaste
1876254721Semastevoid
1877254721SemasteThread::SettingsTerminate ()
1878254721Semaste{
1879254721Semaste}
1880254721Semaste
1881263363Semastelldb::addr_t
1882263363SemasteThread::GetThreadPointer ()
1883263363Semaste{
1884263363Semaste    return LLDB_INVALID_ADDRESS;
1885263363Semaste}
1886263363Semaste
1887263363Semasteaddr_t
1888263363SemasteThread::GetThreadLocalData (const ModuleSP module)
1889263363Semaste{
1890263363Semaste    // The default implementation is to ask the dynamic loader for it.
1891263363Semaste    // This can be overridden for specific platforms.
1892263363Semaste    DynamicLoader *loader = GetProcess()->GetDynamicLoader();
1893263363Semaste    if (loader)
1894263363Semaste        return loader->GetThreadLocalData (module, shared_from_this());
1895263363Semaste    else
1896263363Semaste        return LLDB_INVALID_ADDRESS;
1897263363Semaste}
1898263363Semaste
1899254721Semastelldb::StackFrameSP
1900254721SemasteThread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1901254721Semaste{
1902254721Semaste    return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
1903254721Semaste}
1904254721Semaste
1905254721Semasteconst char *
1906254721SemasteThread::StopReasonAsCString (lldb::StopReason reason)
1907254721Semaste{
1908254721Semaste    switch (reason)
1909254721Semaste    {
1910254721Semaste    case eStopReasonInvalid:       return "invalid";
1911254721Semaste    case eStopReasonNone:          return "none";
1912254721Semaste    case eStopReasonTrace:         return "trace";
1913254721Semaste    case eStopReasonBreakpoint:    return "breakpoint";
1914254721Semaste    case eStopReasonWatchpoint:    return "watchpoint";
1915254721Semaste    case eStopReasonSignal:        return "signal";
1916254721Semaste    case eStopReasonException:     return "exception";
1917254721Semaste    case eStopReasonExec:          return "exec";
1918254721Semaste    case eStopReasonPlanComplete:  return "plan complete";
1919254721Semaste    case eStopReasonThreadExiting: return "thread exiting";
1920254721Semaste    }
1921254721Semaste
1922254721Semaste
1923254721Semaste    static char unknown_state_string[64];
1924254721Semaste    snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1925254721Semaste    return unknown_state_string;
1926254721Semaste}
1927254721Semaste
1928254721Semasteconst char *
1929254721SemasteThread::RunModeAsCString (lldb::RunMode mode)
1930254721Semaste{
1931254721Semaste    switch (mode)
1932254721Semaste    {
1933254721Semaste    case eOnlyThisThread:     return "only this thread";
1934254721Semaste    case eAllThreads:         return "all threads";
1935254721Semaste    case eOnlyDuringStepping: return "only during stepping";
1936254721Semaste    }
1937254721Semaste
1938254721Semaste    static char unknown_state_string[64];
1939254721Semaste    snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1940254721Semaste    return unknown_state_string;
1941254721Semaste}
1942254721Semaste
1943254721Semastesize_t
1944254721SemasteThread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1945254721Semaste{
1946254721Semaste    ExecutionContext exe_ctx (shared_from_this());
1947254721Semaste    Target *target = exe_ctx.GetTargetPtr();
1948254721Semaste    Process *process = exe_ctx.GetProcessPtr();
1949254721Semaste    size_t num_frames_shown = 0;
1950254721Semaste    strm.Indent();
1951254721Semaste    bool is_selected = false;
1952254721Semaste    if (process)
1953254721Semaste    {
1954254721Semaste        if (process->GetThreadList().GetSelectedThread().get() == this)
1955254721Semaste            is_selected = true;
1956254721Semaste    }
1957254721Semaste    strm.Printf("%c ", is_selected ? '*' : ' ');
1958254721Semaste    if (target && target->GetDebugger().GetUseExternalEditor())
1959254721Semaste    {
1960254721Semaste        StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
1961254721Semaste        if (frame_sp)
1962254721Semaste        {
1963254721Semaste            SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1964254721Semaste            if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1965254721Semaste            {
1966254721Semaste                Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1967254721Semaste            }
1968254721Semaste        }
1969254721Semaste    }
1970254721Semaste
1971254721Semaste    DumpUsingSettingsFormat (strm, start_frame);
1972254721Semaste
1973254721Semaste    if (num_frames > 0)
1974254721Semaste    {
1975254721Semaste        strm.IndentMore();
1976254721Semaste
1977254721Semaste        const bool show_frame_info = true;
1978263363Semaste
1979263363Semaste        const char *selected_frame_marker = NULL;
1980263363Semaste        if (num_frames == 1 || (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
1981263363Semaste            strm.IndentMore ();
1982263363Semaste        else
1983263363Semaste            selected_frame_marker = "* ";
1984263363Semaste
1985254721Semaste        num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1986254721Semaste                                                            start_frame,
1987254721Semaste                                                            num_frames,
1988254721Semaste                                                            show_frame_info,
1989263363Semaste                                                            num_frames_with_source,
1990263363Semaste                                                            selected_frame_marker);
1991263363Semaste        if (num_frames == 1)
1992263363Semaste            strm.IndentLess();
1993254721Semaste        strm.IndentLess();
1994254721Semaste    }
1995254721Semaste    return num_frames_shown;
1996254721Semaste}
1997254721Semaste
1998254721Semastesize_t
1999254721SemasteThread::GetStackFrameStatus (Stream& strm,
2000254721Semaste                             uint32_t first_frame,
2001254721Semaste                             uint32_t num_frames,
2002254721Semaste                             bool show_frame_info,
2003254721Semaste                             uint32_t num_frames_with_source)
2004254721Semaste{
2005254721Semaste    return GetStackFrameList()->GetStatus (strm,
2006254721Semaste                                           first_frame,
2007254721Semaste                                           num_frames,
2008254721Semaste                                           show_frame_info,
2009254721Semaste                                           num_frames_with_source);
2010254721Semaste}
2011254721Semaste
2012254721SemasteUnwind *
2013254721SemasteThread::GetUnwinder ()
2014254721Semaste{
2015254721Semaste    if (m_unwinder_ap.get() == NULL)
2016254721Semaste    {
2017254721Semaste        const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
2018254721Semaste        const llvm::Triple::ArchType machine = target_arch.GetMachine();
2019254721Semaste        switch (machine)
2020254721Semaste        {
2021254721Semaste            case llvm::Triple::x86_64:
2022254721Semaste            case llvm::Triple::x86:
2023254721Semaste            case llvm::Triple::arm:
2024254721Semaste            case llvm::Triple::thumb:
2025263363Semaste            case llvm::Triple::mips64:
2026269024Semaste            case llvm::Triple::hexagon:
2027254721Semaste                m_unwinder_ap.reset (new UnwindLLDB (*this));
2028254721Semaste                break;
2029254721Semaste
2030254721Semaste            default:
2031254721Semaste                if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2032254721Semaste                    m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
2033254721Semaste                break;
2034254721Semaste        }
2035254721Semaste    }
2036254721Semaste    return m_unwinder_ap.get();
2037254721Semaste}
2038254721Semaste
2039254721Semaste
2040254721Semastevoid
2041254721SemasteThread::Flush ()
2042254721Semaste{
2043254721Semaste    ClearStackFrames ();
2044254721Semaste    m_reg_context_sp.reset();
2045254721Semaste}
2046254721Semaste
2047254721Semastebool
2048254721SemasteThread::IsStillAtLastBreakpointHit ()
2049254721Semaste{
2050254721Semaste    // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
2051254721Semaste    // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
2052254721Semaste    // multithreaded programs.
2053254721Semaste    if (m_stop_info_sp) {
2054254721Semaste        StopReason stop_reason = m_stop_info_sp->GetStopReason();
2055254721Semaste        if (stop_reason == lldb::eStopReasonBreakpoint) {
2056254721Semaste            uint64_t value = m_stop_info_sp->GetValue();
2057254721Semaste            lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
2058254721Semaste            if (reg_ctx_sp)
2059254721Semaste            {
2060254721Semaste                lldb::addr_t pc = reg_ctx_sp->GetPC();
2061254721Semaste                BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
2062254721Semaste                if (bp_site_sp && value == bp_site_sp->GetID())
2063254721Semaste                    return true;
2064254721Semaste            }
2065254721Semaste        }
2066254721Semaste    }
2067254721Semaste    return false;
2068254721Semaste}
2069269024Semaste
2070269024Semaste
2071269024SemasteError
2072269024SemasteThread::StepIn (bool source_step,
2073269024Semaste                bool avoid_code_without_debug_info)
2074269024Semaste
2075269024Semaste{
2076269024Semaste    Error error;
2077269024Semaste    Process *process = GetProcess().get();
2078269024Semaste    if (StateIsStoppedState (process->GetState(), true))
2079269024Semaste    {
2080269024Semaste        StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2081269024Semaste        ThreadPlanSP new_plan_sp;
2082269024Semaste        const lldb::RunMode run_mode = eOnlyThisThread;
2083269024Semaste        const bool abort_other_plans = false;
2084269024Semaste
2085269024Semaste        if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2086269024Semaste        {
2087269024Semaste            SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2088269024Semaste            new_plan_sp = QueueThreadPlanForStepInRange (abort_other_plans,
2089269024Semaste                                                         sc.line_entry.range,
2090269024Semaste                                                         sc,
2091269024Semaste                                                         NULL,
2092269024Semaste                                                         run_mode,
2093269024Semaste                                                         avoid_code_without_debug_info);
2094269024Semaste        }
2095269024Semaste        else
2096269024Semaste        {
2097269024Semaste            new_plan_sp = QueueThreadPlanForStepSingleInstruction (false,
2098269024Semaste                                                                   abort_other_plans,
2099269024Semaste                                                                   run_mode);
2100269024Semaste        }
2101269024Semaste
2102269024Semaste        new_plan_sp->SetIsMasterPlan(true);
2103269024Semaste        new_plan_sp->SetOkayToDiscard(false);
2104269024Semaste
2105269024Semaste        // Why do we need to set the current thread by ID here???
2106269024Semaste        process->GetThreadList().SetSelectedThreadByID (GetID());
2107269024Semaste        error = process->Resume();
2108269024Semaste    }
2109269024Semaste    else
2110269024Semaste    {
2111269024Semaste        error.SetErrorString("process not stopped");
2112269024Semaste    }
2113269024Semaste    return error;
2114269024Semaste}
2115269024Semaste
2116269024SemasteError
2117269024SemasteThread::StepOver (bool source_step)
2118269024Semaste
2119269024Semaste{
2120269024Semaste    Error error;
2121269024Semaste    Process *process = GetProcess().get();
2122269024Semaste    if (StateIsStoppedState (process->GetState(), true))
2123269024Semaste    {
2124269024Semaste        StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2125269024Semaste        ThreadPlanSP new_plan_sp;
2126269024Semaste
2127269024Semaste        const lldb::RunMode run_mode = eOnlyThisThread;
2128269024Semaste        const bool abort_other_plans = false;
2129269024Semaste
2130269024Semaste        if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2131269024Semaste        {
2132269024Semaste            SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2133269024Semaste            new_plan_sp = QueueThreadPlanForStepOverRange (abort_other_plans,
2134269024Semaste                                                           sc.line_entry.range,
2135269024Semaste                                                           sc,
2136269024Semaste                                                           run_mode);
2137269024Semaste        }
2138269024Semaste        else
2139269024Semaste        {
2140269024Semaste            new_plan_sp = QueueThreadPlanForStepSingleInstruction (true,
2141269024Semaste                                                                   abort_other_plans,
2142269024Semaste                                                                   run_mode);
2143269024Semaste        }
2144269024Semaste
2145269024Semaste        new_plan_sp->SetIsMasterPlan(true);
2146269024Semaste        new_plan_sp->SetOkayToDiscard(false);
2147269024Semaste
2148269024Semaste        // Why do we need to set the current thread by ID here???
2149269024Semaste        process->GetThreadList().SetSelectedThreadByID (GetID());
2150269024Semaste        error = process->Resume();
2151269024Semaste    }
2152269024Semaste    else
2153269024Semaste    {
2154269024Semaste        error.SetErrorString("process not stopped");
2155269024Semaste    }
2156269024Semaste    return error;
2157269024Semaste}
2158269024Semaste
2159269024SemasteError
2160269024SemasteThread::StepOut ()
2161269024Semaste{
2162269024Semaste    Error error;
2163269024Semaste    Process *process = GetProcess().get();
2164269024Semaste    if (StateIsStoppedState (process->GetState(), true))
2165269024Semaste    {
2166269024Semaste        const bool first_instruction = false;
2167269024Semaste        const bool stop_other_threads = false;
2168269024Semaste        const bool abort_other_plans = false;
2169269024Semaste
2170269024Semaste        ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut (abort_other_plans,
2171269024Semaste                                                            NULL,
2172269024Semaste                                                            first_instruction,
2173269024Semaste                                                            stop_other_threads,
2174269024Semaste                                                            eVoteYes,
2175269024Semaste                                                            eVoteNoOpinion,
2176269024Semaste                                                            0));
2177269024Semaste
2178269024Semaste        new_plan_sp->SetIsMasterPlan(true);
2179269024Semaste        new_plan_sp->SetOkayToDiscard(false);
2180269024Semaste
2181269024Semaste        // Why do we need to set the current thread by ID here???
2182269024Semaste        process->GetThreadList().SetSelectedThreadByID (GetID());
2183269024Semaste        error = process->Resume();
2184269024Semaste    }
2185269024Semaste    else
2186269024Semaste    {
2187269024Semaste        error.SetErrorString("process not stopped");
2188269024Semaste    }
2189269024Semaste    return error;
2190269024Semaste}