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     bool okay_to_discard
43 ) :
44     ThreadPlanStepRange (ThreadPlan::eKindStepOverRange, "Step range stepping over", thread, range, addr_context, stop_others)
45 {
46     SetOkayToDiscard (okay_to_discard);
47 }
48 
49 ThreadPlanStepOverRange::~ThreadPlanStepOverRange ()
50 {
51 }
52 
53 void
54 ThreadPlanStepOverRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
55 {
56     if (level == lldb::eDescriptionLevelBrief)
57         s->Printf("step over");
58     else
59     {
60         s->Printf ("stepping through range (stepping over functions): ");
61         DumpRanges(s);
62     }
63 }
64 
65 bool
66 ThreadPlanStepOverRange::PlanExplainsStop ()
67 {
68     // We don't explain signals or breakpoints (breakpoints that handle stepping in or
69     // out will be handled by a child plan.
70     StopInfoSP stop_info_sp = GetPrivateStopReason();
71     if (stop_info_sp)
72     {
73         StopReason reason = stop_info_sp->GetStopReason();
74 
75         switch (reason)
76         {
77         case eStopReasonBreakpoint:
78         case eStopReasonWatchpoint:
79         case eStopReasonSignal:
80         case eStopReasonException:
81             return false;
82         default:
83             return true;
84         }
85     }
86     return true;
87 }
88 
89 
90 bool
91 ThreadPlanStepOverRange::ShouldStop (Event *event_ptr)
92 {
93     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
94 
95     if (log)
96     {
97         StreamString s;
98         s.Address (m_thread.GetRegisterContext()->GetPC(),
99                    m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
100         log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
101     }
102 
103     // If we're still in the range, keep going.
104     if (InRange())
105         return false;
106 
107     // If we're out of the range but in the same frame or in our caller's frame
108     // then we should stop.
109     // When stepping out we only step if we are forcing running one thread.
110     bool stop_others;
111     if (m_stop_others == lldb::eOnlyThisThread)
112         stop_others = true;
113     else
114         stop_others = false;
115 
116     ThreadPlan* new_plan = NULL;
117 
118     FrameComparison frame_order = CompareCurrentFrameToStartFrame();
119 
120     if (frame_order == eFrameCompareOlder)
121     {
122         // If we're in an older frame then we should stop.
123         //
124         // A caveat to this is if we think the frame is older but we're actually in a trampoline.
125         // I'm going to make the assumption that you wouldn't RETURN to a trampoline.  So if we are
126         // in a trampoline we think the frame is older because the trampoline confused the backtracer.
127         // As below, we step through first, and then try to figure out how to get back out again.
128 
129         new_plan = m_thread.QueueThreadPlanForStepThrough (false, stop_others);
130 
131         if (new_plan != NULL && log)
132             log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
133     }
134     else if (frame_order == eFrameCompareYounger)
135     {
136         // Make sure we really are in a new frame.  Do that by unwinding and seeing if the
137         // start function really is our start function...
138         StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(1);
139 
140         // But if we can't even unwind one frame we should just get out of here & stop...
141         if (older_frame_sp)
142         {
143             const SymbolContext &older_context = older_frame_sp->GetSymbolContext(eSymbolContextEverything);
144             if (older_context == m_addr_context)
145             {
146                 new_plan = m_thread.QueueThreadPlanForStepOut (false,
147                                                            NULL,
148                                                            true,
149                                                            stop_others,
150                                                            eVoteNo,
151                                                            eVoteNoOpinion,
152                                                            0);
153             }
154             else
155             {
156                 new_plan = m_thread.QueueThreadPlanForStepThrough (false, stop_others);
157 
158             }
159         }
160     }
161     else if (!InSymbol())
162     {
163         // This one is a little tricky.  Sometimes we may be in a stub or something similar,
164         // in which case we need to get out of there.  But if we are in a stub then it's
165         // likely going to be hard to get out from here.  It is probably easiest to step into the
166         // stub, and then it will be straight-forward to step out.
167         new_plan = m_thread.QueueThreadPlanForStepThrough (false, stop_others);
168     }
169 
170     if (new_plan == NULL)
171         m_no_more_plans = true;
172     else
173         m_no_more_plans = false;
174 
175     if (new_plan == NULL)
176     {
177         // For efficiencies sake, we know we're done here so we don't have to do this
178         // calculation again in MischiefManaged.
179         SetPlanComplete();
180         return true;
181     }
182     else
183         return false;
184 }
185