1 //===-- ThreadPlanStepOverRange.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/ThreadPlanStepOverRange.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/Target.h"
23 #include "lldb/Target/Thread.h"
24 #include "lldb/Target/ThreadPlanStepOut.h"
25 #include "lldb/Target/ThreadPlanStepThrough.h"
26 
27 using namespace lldb_private;
28 using namespace lldb;
29 
30 
31 //----------------------------------------------------------------------
32 // ThreadPlanStepOverRange: Step through a stack range, either stepping over or into
33 // based on the value of \a type.
34 //----------------------------------------------------------------------
35 
36 ThreadPlanStepOverRange::ThreadPlanStepOverRange
37 (
38     Thread &thread,
39     const AddressRange &range,
40     const SymbolContext &addr_context,
41     lldb::RunMode stop_others
42 ) :
43     ThreadPlanStepRange (ThreadPlan::eKindStepOverRange, "Step range stepping over", thread, range, addr_context, stop_others)
44 {
45 }
46 
47 ThreadPlanStepOverRange::~ThreadPlanStepOverRange ()
48 {
49 }
50 
51 void
52 ThreadPlanStepOverRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
53 {
54     if (level == lldb::eDescriptionLevelBrief)
55         s->Printf("step over");
56     else
57     {
58         s->Printf ("stepping through range (stepping over functions): ");
59         DumpRanges(s);
60     }
61 }
62 
63 bool
64 ThreadPlanStepOverRange::ShouldStop (Event *event_ptr)
65 {
66     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
67 
68     if (log)
69     {
70         StreamString s;
71         s.Address (m_thread.GetRegisterContext()->GetPC(),
72                    m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
73         log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
74     }
75 
76     // If we're out of the range but in the same frame or in our caller's frame
77     // then we should stop.
78     // When stepping out we only step if we are forcing running one thread.
79     bool stop_others;
80     if (m_stop_others == lldb::eOnlyThisThread)
81         stop_others = true;
82     else
83         stop_others = false;
84 
85     ThreadPlan* new_plan = NULL;
86 
87     FrameComparison frame_order = CompareCurrentFrameToStartFrame();
88 
89     if (frame_order == eFrameCompareOlder)
90     {
91         // If we're in an older frame then we should stop.
92         //
93         // A caveat to this is if we think the frame is older but we're actually in a trampoline.
94         // I'm going to make the assumption that you wouldn't RETURN to a trampoline.  So if we are
95         // in a trampoline we think the frame is older because the trampoline confused the backtracer.
96         // As below, we step through first, and then try to figure out how to get back out again.
97 
98         new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
99 
100         if (new_plan != NULL && log)
101             log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
102     }
103     else if (frame_order == eFrameCompareYounger)
104     {
105         // Make sure we really are in a new frame.  Do that by unwinding and seeing if the
106         // start function really is our start function...
107         StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(1);
108 
109         // But if we can't even unwind one frame we should just get out of here & stop...
110         if (older_frame_sp)
111         {
112             const SymbolContext &older_context = older_frame_sp->GetSymbolContext(eSymbolContextEverything);
113             if (older_context == m_addr_context)
114             {
115                 new_plan = m_thread.QueueThreadPlanForStepOut (false,
116                                                            NULL,
117                                                            true,
118                                                            stop_others,
119                                                            eVoteNo,
120                                                            eVoteNoOpinion,
121                                                            0);
122             }
123             else
124             {
125                 new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
126 
127             }
128         }
129     }
130     else
131     {
132         // If we're still in the range, keep going.
133         if (InRange())
134         {
135             SetNextBranchBreakpoint();
136             return false;
137         }
138 
139 
140         if (!InSymbol())
141         {
142             // This one is a little tricky.  Sometimes we may be in a stub or something similar,
143             // in which case we need to get out of there.  But if we are in a stub then it's
144             // likely going to be hard to get out from here.  It is probably easiest to step into the
145             // stub, and then it will be straight-forward to step out.
146             new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
147         }
148     }
149 
150     // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
151     ClearNextBranchBreakpoint();
152 
153     if (new_plan == NULL)
154         m_no_more_plans = true;
155     else
156         m_no_more_plans = false;
157 
158     if (new_plan == NULL)
159     {
160         // For efficiencies sake, we know we're done here so we don't have to do this
161         // calculation again in MischiefManaged.
162         SetPlanComplete();
163         return true;
164     }
165     else
166         return false;
167 }
168 
169 bool
170 ThreadPlanStepOverRange::PlanExplainsStop ()
171 {
172     // For crashes, breakpoint hits, signals, etc, let the base plan (or some plan above us)
173     // handle the stop.  That way the user can see the stop, step around, and then when they
174     // are done, continue and have their step complete.  The exception is if we've hit our
175     // "run to next branch" breakpoint.
176     // Note, unlike the step in range plan, we don't mark ourselves complete if we hit an
177     // unexplained breakpoint/crash.
178 
179     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
180     StopInfoSP stop_info_sp = GetPrivateStopReason();
181     if (stop_info_sp)
182     {
183         StopReason reason = stop_info_sp->GetStopReason();
184 
185         switch (reason)
186         {
187         case eStopReasonBreakpoint:
188             if (NextRangeBreakpointExplainsStop(stop_info_sp))
189                 return true;
190             else
191                 return false;
192             break;
193         case eStopReasonWatchpoint:
194         case eStopReasonSignal:
195         case eStopReasonException:
196             if (log)
197                 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
198             return false;
199             break;
200         default:
201             break;
202         }
203     }
204     return true;
205 }
206