1 //===-- ThreadPlanStepInRange.cpp -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/ThreadPlanStepInRange.h"
14 // Other libraries and framework includes
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"
31 using namespace lldb_private;
33 uint32_t ThreadPlanStepInRange::s_default_flag_values = ThreadPlanShouldStopHere::eAvoidNoDebug;
35 //----------------------------------------------------------------------
36 // ThreadPlanStepInRange: Step through a stack range, either stepping over or into
37 // based on the value of \a type.
38 //----------------------------------------------------------------------
40 ThreadPlanStepInRange::ThreadPlanStepInRange
43 const AddressRange &range,
44 const SymbolContext &addr_context,
45 lldb::RunMode stop_others
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 m_virtual_step (false)
55 ThreadPlanStepInRange::ThreadPlanStepInRange
58 const AddressRange &range,
59 const SymbolContext &addr_context,
60 const char *step_into_target,
61 lldb::RunMode stop_others
63 ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others),
64 ThreadPlanShouldStopHere (this, ThreadPlanStepInRange::DefaultShouldStopHereCallback, NULL),
65 m_step_past_prologue (true),
66 m_virtual_step (false),
67 m_step_into_target (step_into_target)
72 ThreadPlanStepInRange::~ThreadPlanStepInRange ()
77 ThreadPlanStepInRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
79 if (level == lldb::eDescriptionLevelBrief)
83 s->Printf ("Stepping through range (stepping into functions): ");
85 const char *step_into_target = m_step_into_target.AsCString();
86 if (step_into_target && step_into_target[0] != '\0')
87 s->Printf (" targeting %s.", m_step_into_target.AsCString());
94 ThreadPlanStepInRange::ShouldStop (Event *event_ptr)
96 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
101 s.Address (m_thread.GetRegisterContext()->GetPC(),
102 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
103 log->Printf("ThreadPlanStepInRange reached %s.", s.GetData());
106 if (IsPlanComplete())
109 m_no_more_plans = false;
110 if (m_sub_plan_sp && m_sub_plan_sp->IsPlanComplete())
112 if (!m_sub_plan_sp->PlanSucceeded())
115 m_no_more_plans = true;
119 m_sub_plan_sp.reset();
124 // If we've just completed a virtual step, all we need to do is check for a ShouldStopHere plan, and otherwise
126 m_sub_plan_sp = InvokeShouldStopHereCallback();
130 // Stepping through should be done running other threads in general, since we're setting a breakpoint and
131 // continuing. So only stop others if we are explicitly told to do so.
134 if (m_stop_others == lldb::eOnlyThisThread)
139 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
141 if (frame_order == eFrameCompareOlder)
143 // If we're in an older frame then we should stop.
145 // A caveat to this is if we think the frame is older but we're actually in a trampoline.
146 // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are
147 // in a trampoline we think the frame is older because the trampoline confused the backtracer.
148 m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
153 log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
157 else if (frame_order == eFrameCompareEqual && InSymbol())
159 // If we are not in a place we should step through, we're done.
160 // One tricky bit here is that some stubs don't push a frame, so we have to check
161 // both the case of a frame that is younger, or the same as this frame.
162 // However, if the frame is the same, and we are still in the symbol we started
163 // in, the we don't need to do this. This first check isn't strictly necessary,
164 // but it is more efficient.
166 // If we're still in the range, keep going, either by running to the next branch breakpoint, or by
170 SetNextBranchBreakpoint();
175 m_no_more_plans = true;
179 // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
180 ClearNextBranchBreakpoint();
182 // We may have set the plan up above in the FrameIsOlder section:
185 m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
190 log->Printf ("Found a step through plan: %s", m_sub_plan_sp->GetName());
192 log->Printf ("No step through plan found.");
195 // If not, give the "should_stop" callback a chance to push a plan to get us out of here.
196 // But only do that if we actually have stepped in.
197 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
198 m_sub_plan_sp = InvokeShouldStopHereCallback();
200 // If we've stepped in and we are going to stop here, check to see if we were asked to
201 // run past the prologue, and if so do that.
203 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger && m_step_past_prologue)
205 lldb::StackFrameSP curr_frame = m_thread.GetStackFrameAtIndex(0);
208 size_t bytes_to_skip = 0;
209 lldb::addr_t curr_addr = m_thread.GetRegisterContext()->GetPC();
210 Address func_start_address;
212 SymbolContext sc = curr_frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextSymbol);
216 func_start_address = sc.function->GetAddressRange().GetBaseAddress();
217 if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get()))
218 bytes_to_skip = sc.function->GetPrologueByteSize();
222 func_start_address = sc.symbol->GetAddress();
223 if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get()))
224 bytes_to_skip = sc.symbol->GetPrologueByteSize();
227 if (bytes_to_skip != 0)
229 func_start_address.Slide (bytes_to_skip);
230 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
232 log->Printf ("Pushing past prologue ");
234 m_sub_plan_sp = m_thread.QueueThreadPlanForRunToAddress(false, func_start_address,true);
242 m_no_more_plans = true;
248 m_no_more_plans = false;
254 ThreadPlanStepInRange::SetFlagsToDefault ()
256 GetFlags().Set(ThreadPlanStepInRange::s_default_flag_values);
260 ThreadPlanStepInRange::SetAvoidRegexp(const char *name)
262 if (m_avoid_regexp_ap.get() == NULL)
263 m_avoid_regexp_ap.reset (new RegularExpression(name));
265 m_avoid_regexp_ap->Compile (name);
269 ThreadPlanStepInRange::SetDefaultFlagValue (uint32_t new_value)
271 // TODO: Should we test this for sanity?
272 ThreadPlanStepInRange::s_default_flag_values = new_value;
276 ThreadPlanStepInRange::FrameMatchesAvoidRegexp ()
278 StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
280 const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_ap.get();
281 if (avoid_regexp_to_use == NULL)
282 avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
284 if (avoid_regexp_to_use != NULL)
286 SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock|eSymbolContextSymbol);
287 if (sc.symbol != NULL)
289 const char *frame_function_name = sc.GetFunctionName().GetCString();
290 if (frame_function_name)
292 size_t num_matches = 0;
293 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
297 RegularExpression::Match regex_match(num_matches);
299 bool return_value = avoid_regexp_to_use->Execute(frame_function_name, ®ex_match);
305 regex_match.GetMatchAtIndex(frame_function_name,0, match);
306 log->Printf ("Stepping out of function \"%s\" because it matches the avoid regexp \"%s\" - match substring: \"%s\".",
308 avoid_regexp_to_use->GetText(),
321 ThreadPlanStepInRange::DefaultShouldStopHereCallback (ThreadPlan *current_plan, Flags &flags, void *baton)
323 bool should_step_out = false;
324 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
325 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
327 if (flags.Test(eAvoidNoDebug))
329 if (!frame->HasDebugInformation())
332 log->Printf ("Stepping out of frame with no debug info");
334 should_step_out = true;
338 if (current_plan->GetKind() == eKindStepInRange)
340 ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan);
341 if (step_in_range_plan->m_step_into_target)
343 SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock|eSymbolContextSymbol);
344 if (sc.symbol != NULL)
346 // First try an exact match, since that's cheap with ConstStrings. Then do a strstr compare.
347 if (step_in_range_plan->m_step_into_target == sc.GetFunctionName())
349 should_step_out = false;
353 const char *target_name = step_in_range_plan->m_step_into_target.AsCString();
354 const char *function_name = sc.GetFunctionName().AsCString();
356 if (function_name == NULL)
357 should_step_out = true;
358 else if (strstr (function_name, target_name) == NULL)
359 should_step_out = true;
361 if (log && should_step_out)
362 log->Printf("Stepping out of frame %s which did not match step into target %s.",
363 sc.GetFunctionName().AsCString(),
364 step_in_range_plan->m_step_into_target.AsCString());
368 if (!should_step_out)
370 ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan);
371 // Don't log the should_step_out here, it's easier to do it in FrameMatchesAvoidRegexp.
372 should_step_out = step_in_range_plan->FrameMatchesAvoidRegexp ();
379 // FIXME: Make sure the ThreadPlanForStepOut does the right thing with inlined functions.
380 // We really should have all plans take the tri-state for "stop others" so we can do the right
381 // thing. For now let's be safe and always run others when we are likely to run arbitrary code.
382 const bool stop_others = false;
383 return current_plan->GetThread().QueueThreadPlanForStepOut (false,
392 return ThreadPlanSP();
396 ThreadPlanStepInRange::DoPlanExplainsStop (Event *event_ptr)
398 // We always explain a stop. Either we've just done a single step, in which
399 // case we'll do our ordinary processing, or we stopped for some
400 // reason that isn't handled by our sub-plans, in which case we want to just stop right
402 // In general, we don't want to mark the plan as complete for unexplained stops.
403 // For instance, if you step in to some code with no debug info, so you step out
404 // and in the course of that hit a breakpoint, then you want to stop & show the user
405 // the breakpoint, but not unship the step in plan, since you still may want to complete that
406 // plan when you continue. This is particularly true when doing "step in to target function."
409 // The only variation is that if we are doing "step by running to next branch" in which case
410 // if we hit our branch breakpoint we don't set the plan to complete.
420 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
423 StopReason reason = stop_info_sp->GetStopReason();
427 case eStopReasonBreakpoint:
428 if (NextRangeBreakpointExplainsStop(stop_info_sp))
433 case eStopReasonWatchpoint:
434 case eStopReasonSignal:
435 case eStopReasonException:
436 case eStopReasonExec:
437 case eStopReasonThreadExiting:
439 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
441 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
443 return_value = false;
458 ThreadPlanStepInRange::DoWillResume (lldb::StateType resume_state, bool current_plan)
460 if (resume_state == eStateStepping && current_plan)
462 // See if we are about to step over a virtual inlined call.
463 bool step_without_resume = m_thread.DecrementCurrentInlinedDepth();
464 if (step_without_resume)
466 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
468 log->Printf ("ThreadPlanStepInRange::DoWillResume: returning false, inline_depth: %d",
469 m_thread.GetCurrentInlinedDepth());
470 SetStopInfo(StopInfo::CreateStopReasonToTrace(m_thread));
472 // FIXME: Maybe it would be better to create a InlineStep stop reason, but then
473 // the whole rest of the world would have to handle that stop reason.
474 m_virtual_step = true;
476 return !step_without_resume;
482 ThreadPlanStepInRange::IsVirtualStep()
484 return m_virtual_step;