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/Symbol/Symbol.h"
21 #include "lldb/Symbol/Function.h"
22 #include "lldb/Target/Process.h"
23 #include "lldb/Target/RegisterContext.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Target/Thread.h"
26 #include "lldb/Target/ThreadPlanStepOut.h"
27 #include "lldb/Target/ThreadPlanStepThrough.h"
28 #include "lldb/Core/RegularExpression.h"
29 
30 using namespace lldb;
31 using namespace lldb_private;
32 
33 uint32_t ThreadPlanStepInRange::s_default_flag_values = ThreadPlanShouldStopHere::eAvoidNoDebug;
34 
35 //----------------------------------------------------------------------
36 // ThreadPlanStepInRange: Step through a stack range, either stepping over or into
37 // based on the value of \a type.
38 //----------------------------------------------------------------------
39 
40 ThreadPlanStepInRange::ThreadPlanStepInRange
41 (
42     Thread &thread,
43     const AddressRange &range,
44     const SymbolContext &addr_context,
45     lldb::RunMode stop_others
46 ) :
47     ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others),
48     ThreadPlanShouldStopHere (this, ThreadPlanStepInRange::DefaultShouldStopHereCallback, NULL),
49     m_step_past_prologue (true)
50 {
51     SetFlagsToDefault ();
52 }
53 
54 ThreadPlanStepInRange::~ThreadPlanStepInRange ()
55 {
56 }
57 
58 void
59 ThreadPlanStepInRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
60 {
61     if (level == lldb::eDescriptionLevelBrief)
62         s->Printf("step in");
63     else
64     {
65         s->Printf ("Stepping through range (stepping into functions): ");
66         DumpRanges(s);
67     }
68 }
69 
70 bool
71 ThreadPlanStepInRange::PlanExplainsStop ()
72 {
73     // We always explain a stop.  Either we've just done a single step, in which
74     // case we'll do our ordinary processing, or we stopped for some
75     // reason that isn't handled by our sub-plans, in which case we want to just stop right
76     // away.
77 
78     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
79     StopInfoSP stop_info_sp = GetPrivateStopReason();
80     if (stop_info_sp)
81     {
82         StopReason reason = stop_info_sp->GetStopReason();
83 
84         switch (reason)
85         {
86         case eStopReasonBreakpoint:
87         case eStopReasonWatchpoint:
88         case eStopReasonSignal:
89         case eStopReasonException:
90             if (log)
91                 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
92             SetPlanComplete();
93             break;
94         default:
95             break;
96         }
97     }
98     return true;
99 }
100 
101 bool
102 ThreadPlanStepInRange::ShouldStop (Event *event_ptr)
103 {
104     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
105     m_no_more_plans = false;
106 
107     if (log)
108     {
109         StreamString s;
110         s.Address (m_thread.GetRegisterContext()->GetPC(),
111                    m_thread.GetProcess().GetTarget().GetArchitecture().GetAddressByteSize());
112         log->Printf("ThreadPlanStepInRange reached %s.", s.GetData());
113     }
114 
115     if (IsPlanComplete())
116         return true;
117 
118     // If we're still in the range, keep going.
119     if (InRange())
120         return false;
121 
122     ThreadPlan* new_plan = NULL;
123 
124     // Stepping through should be done stopping other threads in general, since we're setting a breakpoint and
125     // continuing...
126 
127     bool stop_others;
128     if (m_stop_others != lldb::eAllThreads)
129         stop_others = true;
130     else
131         stop_others = false;
132 
133     if (FrameIsOlder())
134     {
135         // If we're in an older frame then we should stop.
136         //
137         // A caveat to this is if we think the frame is older but we're actually in a trampoline.
138         // I'm going to make the assumption that you wouldn't RETURN to a trampoline.  So if we are
139         // in a trampoline we think the frame is older because the trampoline confused the backtracer.
140         new_plan = m_thread.QueueThreadPlanForStepThrough (false, stop_others);
141         if (new_plan == NULL)
142             return true;
143         else if (log)
144         {
145             log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
146         }
147 
148     }
149     else if (!FrameIsYounger() && InSymbol())
150     {
151         // If we are not in a place we should step through, we're done.
152         // One tricky bit here is that some stubs don't push a frame, so we have to check
153         // both the case of a frame that is younger, or the same as this frame.
154         // However, if the frame is the same, and we are still in the symbol we started
155         // in, the we don't need to do this.  This first check isn't strictly necessary,
156         // but it is more efficient.
157 
158         SetPlanComplete();
159         return true;
160     }
161 
162     // We may have set the plan up above in the FrameIsOlder section:
163 
164     if (new_plan == NULL)
165         new_plan = m_thread.QueueThreadPlanForStepThrough (false, stop_others);
166 
167     if (log)
168     {
169         if (new_plan != NULL)
170             log->Printf ("Found a step through plan: %s", new_plan->GetName());
171         else
172             log->Printf ("No step through plan found.");
173     }
174 
175     // If not, give the "should_stop" callback a chance to push a plan to get us out of here.
176     // But only do that if we actually have stepped in.
177     if (!new_plan && FrameIsYounger())
178         new_plan = InvokeShouldStopHereCallback();
179 
180     // If we've stepped in and we are going to stop here, check to see if we were asked to
181     // run past the prologue, and if so do that.
182 
183     if (new_plan == NULL && FrameIsYounger() && m_step_past_prologue)
184     {
185         lldb::StackFrameSP curr_frame = m_thread.GetStackFrameAtIndex(0);
186         if (curr_frame)
187         {
188             size_t bytes_to_skip = 0;
189             lldb::addr_t curr_addr = m_thread.GetRegisterContext()->GetPC();
190             Address func_start_address;
191 
192             SymbolContext sc = curr_frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextSymbol);
193 
194             if (sc.function)
195             {
196                 func_start_address = sc.function->GetAddressRange().GetBaseAddress();
197                 if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget()))
198                     bytes_to_skip = sc.function->GetPrologueByteSize();
199             }
200             else if (sc.symbol)
201             {
202                 func_start_address = sc.symbol->GetValue();
203                 if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget()))
204                     bytes_to_skip = sc.symbol->GetPrologueByteSize();
205             }
206 
207             if (bytes_to_skip != 0)
208             {
209                 func_start_address.Slide (bytes_to_skip);
210                 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
211                 if (log)
212                     log->Printf ("Pushing past prologue ");
213 
214                 new_plan = m_thread.QueueThreadPlanForRunToAddress(false, func_start_address,true);
215             }
216         }
217     }
218 
219      if (new_plan == NULL)
220      {
221         m_no_more_plans = true;
222         SetPlanComplete();
223         return true;
224     }
225     else
226     {
227         m_no_more_plans = false;
228         return false;
229     }
230 }
231 
232 void
233 ThreadPlanStepInRange::SetFlagsToDefault ()
234 {
235     GetFlags().Set(ThreadPlanStepInRange::s_default_flag_values);
236 }
237 
238 void
239 ThreadPlanStepInRange::SetAvoidRegexp(const char *name)
240 {
241     if (m_avoid_regexp_ap.get() == NULL)
242         m_avoid_regexp_ap.reset (new RegularExpression(name));
243 
244     m_avoid_regexp_ap->Compile (name);
245 }
246 
247 void
248 ThreadPlanStepInRange::SetDefaultFlagValue (uint32_t new_value)
249 {
250     // TODO: Should we test this for sanity?
251     ThreadPlanStepInRange::s_default_flag_values = new_value;
252 }
253 
254 bool
255 ThreadPlanStepInRange::FrameMatchesAvoidRegexp ()
256 {
257     StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
258 
259     RegularExpression *avoid_regexp_to_use;
260 
261     avoid_regexp_to_use = m_avoid_regexp_ap.get();
262     if (avoid_regexp_to_use == NULL)
263         avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
264 
265     if (avoid_regexp_to_use != NULL)
266     {
267         SymbolContext sc = frame->GetSymbolContext(eSymbolContextSymbol);
268         if (sc.symbol != NULL)
269         {
270             const char *unnamed_symbol = "<UNKNOWN>";
271             const char *sym_name = sc.symbol->GetMangled().GetName().AsCString(unnamed_symbol);
272             if (strcmp (sym_name, unnamed_symbol) != 0)
273                return avoid_regexp_to_use->Execute(sym_name);
274         }
275     }
276     return false;
277 }
278 
279 ThreadPlan *
280 ThreadPlanStepInRange::DefaultShouldStopHereCallback (ThreadPlan *current_plan, Flags &flags, void *baton)
281 {
282     bool should_step_out = false;
283     StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
284     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
285 
286     if (flags.Test(eAvoidNoDebug))
287     {
288         if (!frame->HasDebugInformation())
289         {
290             if (log)
291                 log->Printf ("Stepping out of frame with no debug info");
292 
293             should_step_out = true;
294         }
295     }
296 
297     if (!should_step_out)
298     {
299         if (current_plan->GetKind() == eKindStepInRange)
300         {
301             ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan);
302             should_step_out = step_in_range_plan->FrameMatchesAvoidRegexp ();
303         }
304     }
305 
306     if (should_step_out)
307     {
308         // FIXME: Make sure the ThreadPlanForStepOut does the right thing with inlined functions.
309         return current_plan->GetThread().QueueThreadPlanForStepOut (false,
310                                                                     NULL,
311                                                                     true,
312                                                                     current_plan->StopOthers(),
313                                                                     eVoteNo,
314                                                                     eVoteNoOpinion,
315                                                                     0); // Frame index
316     }
317 
318     return NULL;
319 }
320