1 //===-- ThreadPlanStepInRange.cpp -------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Target/ThreadPlanStepInRange.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 
17 #include "lldb/lldb-private-log.h"
18 #include "lldb/Core/Log.h"
19 #include "lldb/Core/Stream.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Target/RegisterContext.h"
22 #include "lldb/Target/Thread.h"
23 #include "lldb/Target/ThreadPlanStepOut.h"
24 #include "lldb/Target/ThreadPlanStepThrough.h"
25 
26 using namespace lldb;
27 using namespace lldb_private;
28 
29 uint32_t ThreadPlanStepInRange::s_default_flag_values = ThreadPlanShouldStopHere::eAvoidNoDebug;
30 
31 //----------------------------------------------------------------------
32 // ThreadPlanStepInRange: Step through a stack range, either stepping over or into
33 // based on the value of \a type.
34 //----------------------------------------------------------------------
35 
36 ThreadPlanStepInRange::ThreadPlanStepInRange
37 (
38     Thread &thread,
39     const AddressRange &range,
40     const SymbolContext &addr_context,
41     lldb::RunMode stop_others
42 ) :
43     ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others),
44     ThreadPlanShouldStopHere (this, ThreadPlanStepInRange::DefaultShouldStopHereCallback, NULL)
45 {
46     SetFlagsToDefault ();
47 }
48 
49 ThreadPlanStepInRange::~ThreadPlanStepInRange ()
50 {
51 }
52 
53 void
54 ThreadPlanStepInRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
55 {
56     if (level == lldb::eDescriptionLevelBrief)
57         s->Printf("step in");
58     else
59     {
60         s->Printf ("Stepping through range (stepping into functions): ");
61         m_address_range.Dump (s, &m_thread.GetProcess(), Address::DumpStyleLoadAddress);
62     }
63 }
64 
65 bool
66 ThreadPlanStepInRange::ShouldStop (Event *event_ptr)
67 {
68     Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
69     m_no_more_plans = false;
70 
71     if (log)
72     {
73         StreamString s;
74         s.Address (m_thread.GetRegisterContext()->GetPC(), m_thread.GetProcess().GetAddressByteSize());
75         log->Printf("ThreadPlanStepInRange reached %s.", s.GetData());
76     }
77 
78     // If we're still in the range, keep going.
79     if (InRange())
80         return false;
81 
82     // If we're in an older frame then we should stop.
83     if (FrameIsOlder())
84         return true;
85 
86     // See if we are in a place we should step through (i.e. a trampoline of some sort):
87     // One tricky bit here is that some stubs don't push a frame, so we have to check
88     // both the case of a frame that is younger, or the same as this frame.
89     // However, if the frame is the same, and we are still in the symbol we started
90     // in, the we don't need to do this.  This first check isn't strictly necessary,
91     // but it is more efficient.
92 
93     if (!FrameIsYounger() && InSymbol())
94     {
95         SetPlanComplete();
96         return true;
97     }
98 
99     ThreadPlan* new_plan = NULL;
100 
101     bool stop_others;
102     if (m_stop_others == lldb::eOnlyThisThread)
103         stop_others = true;
104     else
105         stop_others = false;
106 
107     new_plan = m_thread.QueueThreadPlanForStepThrough (false, stop_others);
108     // If not, give the "should_stop" callback a chance to push a plan to get us out of here.
109     // But only do that if we actually have stepped in.
110     if (!new_plan && FrameIsYounger())
111         new_plan = InvokeShouldStopHereCallback();
112 
113     if (new_plan == NULL)
114     {
115         m_no_more_plans = true;
116         SetPlanComplete();
117         return true;
118     }
119     else
120     {
121         m_no_more_plans = false;
122         return false;
123     }
124 }
125 
126 void
127 ThreadPlanStepInRange::SetFlagsToDefault ()
128 {
129     GetFlags().Set(ThreadPlanStepInRange::s_default_flag_values);
130 }
131 
132 void
133 ThreadPlanStepInRange::SetDefaultFlagValue (uint32_t new_value)
134 {
135     // TODO: Should we test this for sanity?
136     ThreadPlanStepInRange::s_default_flag_values = new_value;
137 }
138 
139 ThreadPlan *
140 ThreadPlanStepInRange::DefaultShouldStopHereCallback (ThreadPlan *current_plan, Flags &flags, void *baton)
141 {
142     if (flags.IsSet(eAvoidNoDebug))
143     {
144         StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
145 
146         if (!frame->HasDebugInformation())
147         {
148             // FIXME: Make sure the ThreadPlanForStepOut does the right thing with inlined functions.
149             return current_plan->GetThread().QueueThreadPlanForStepOut (false, NULL, true, current_plan->StopOthers(), eVoteNo, eVoteNoOpinion);
150         }
151     }
152 
153     return NULL;
154 }
155