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 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Target/ThreadPlanStepInRange.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/Stream.h"
18 #include "lldb/Symbol/Symbol.h"
19 #include "lldb/Symbol/Function.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 #include "lldb/Core/RegularExpression.h"
29 using namespace lldb_private;
31 uint32_t ThreadPlanStepInRange::s_default_flag_values = ThreadPlanShouldStopHere::eStepInAvoidNoDebug;
33 //----------------------------------------------------------------------
34 // ThreadPlanStepInRange: Step through a stack range, either stepping over or into
35 // based on the value of \a type.
36 //----------------------------------------------------------------------
38 ThreadPlanStepInRange::ThreadPlanStepInRange
41 const AddressRange &range,
42 const SymbolContext &addr_context,
43 lldb::RunMode stop_others,
44 LazyBool step_in_avoids_code_without_debug_info,
45 LazyBool step_out_avoids_code_without_debug_info
47 ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others),
48 ThreadPlanShouldStopHere (this),
49 m_step_past_prologue (true),
50 m_virtual_step (false)
54 SetupAvoidNoDebug(step_in_avoids_code_without_debug_info, step_out_avoids_code_without_debug_info);
57 ThreadPlanStepInRange::ThreadPlanStepInRange
60 const AddressRange &range,
61 const SymbolContext &addr_context,
62 const char *step_into_target,
63 lldb::RunMode stop_others,
64 LazyBool step_in_avoids_code_without_debug_info,
65 LazyBool step_out_avoids_code_without_debug_info
67 ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others),
68 ThreadPlanShouldStopHere (this),
69 m_step_past_prologue (true),
70 m_virtual_step (false),
71 m_step_into_target (step_into_target)
75 SetupAvoidNoDebug(step_in_avoids_code_without_debug_info, step_out_avoids_code_without_debug_info);
78 ThreadPlanStepInRange::~ThreadPlanStepInRange() = default;
81 ThreadPlanStepInRange::SetupAvoidNoDebug(LazyBool step_in_avoids_code_without_debug_info,
82 LazyBool step_out_avoids_code_without_debug_info)
84 bool avoid_nodebug = true;
86 switch (step_in_avoids_code_without_debug_info)
92 avoid_nodebug = false;
94 case eLazyBoolCalculate:
95 avoid_nodebug = m_thread.GetStepInAvoidsNoDebug();
99 GetFlags().Set (ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
101 GetFlags().Clear (ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
103 switch (step_out_avoids_code_without_debug_info)
106 avoid_nodebug = true;
109 avoid_nodebug = false;
111 case eLazyBoolCalculate:
112 avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
116 GetFlags().Set (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
118 GetFlags().Clear (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
122 ThreadPlanStepInRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
124 if (level == lldb::eDescriptionLevelBrief)
126 s->Printf("step in");
130 s->Printf ("Stepping in");
131 bool printed_line_info = false;
132 if (m_addr_context.line_entry.IsValid())
134 s->Printf (" through line ");
135 m_addr_context.line_entry.DumpStopContext (s, false);
136 printed_line_info = true;
139 const char *step_into_target = m_step_into_target.AsCString();
140 if (step_into_target && step_into_target[0] != '\0')
141 s->Printf (" targeting %s", m_step_into_target.AsCString());
143 if (!printed_line_info || level == eDescriptionLevelVerbose)
145 s->Printf (" using ranges:");
153 ThreadPlanStepInRange::ShouldStop (Event *event_ptr)
155 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
160 s.Address (m_thread.GetRegisterContext()->GetPC(),
161 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
162 log->Printf("ThreadPlanStepInRange reached %s.", s.GetData());
165 if (IsPlanComplete())
168 m_no_more_plans = false;
169 if (m_sub_plan_sp && m_sub_plan_sp->IsPlanComplete())
171 if (!m_sub_plan_sp->PlanSucceeded())
174 m_no_more_plans = true;
178 m_sub_plan_sp.reset();
183 // If we've just completed a virtual step, all we need to do is check for a ShouldStopHere plan, and otherwise
185 // FIXME - This can be both a step in and a step out. Probably should record which in the m_virtual_step.
186 m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(eFrameCompareYounger);
190 // Stepping through should be done running other threads in general, since we're setting a breakpoint and
191 // continuing. So only stop others if we are explicitly told to do so.
193 bool stop_others = (m_stop_others == lldb::eOnlyThisThread);
195 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
197 if (frame_order == eFrameCompareOlder || frame_order == eFrameCompareSameParent)
199 // If we're in an older frame then we should stop.
201 // A caveat to this is if we think the frame is older but we're actually in a trampoline.
202 // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are
203 // in a trampoline we think the frame is older because the trampoline confused the backtracer.
204 m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
207 // Otherwise check the ShouldStopHere for step out:
208 m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order);
210 log->Printf ("ShouldStopHere says we should step out of this frame.");
214 log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
217 else if (frame_order == eFrameCompareEqual && InSymbol())
219 // If we are not in a place we should step through, we're done.
220 // One tricky bit here is that some stubs don't push a frame, so we have to check
221 // both the case of a frame that is younger, or the same as this frame.
222 // However, if the frame is the same, and we are still in the symbol we started
223 // in, the we don't need to do this. This first check isn't strictly necessary,
224 // but it is more efficient.
226 // If we're still in the range, keep going, either by running to the next branch breakpoint, or by
230 SetNextBranchBreakpoint();
235 m_no_more_plans = true;
239 // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
240 ClearNextBranchBreakpoint();
242 // We may have set the plan up above in the FrameIsOlder section:
245 m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
250 log->Printf ("Found a step through plan: %s", m_sub_plan_sp->GetName());
252 log->Printf ("No step through plan found.");
255 // If not, give the "should_stop" callback a chance to push a plan to get us out of here.
256 // But only do that if we actually have stepped in.
257 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
258 m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order);
260 // If we've stepped in and we are going to stop here, check to see if we were asked to
261 // run past the prologue, and if so do that.
263 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger && m_step_past_prologue)
265 lldb::StackFrameSP curr_frame = m_thread.GetStackFrameAtIndex(0);
268 size_t bytes_to_skip = 0;
269 lldb::addr_t curr_addr = m_thread.GetRegisterContext()->GetPC();
270 Address func_start_address;
272 SymbolContext sc = curr_frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextSymbol);
276 func_start_address = sc.function->GetAddressRange().GetBaseAddress();
277 if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get()))
278 bytes_to_skip = sc.function->GetPrologueByteSize();
282 func_start_address = sc.symbol->GetAddress();
283 if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get()))
284 bytes_to_skip = sc.symbol->GetPrologueByteSize();
287 if (bytes_to_skip != 0)
289 func_start_address.Slide (bytes_to_skip);
290 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
292 log->Printf ("Pushing past prologue ");
294 m_sub_plan_sp = m_thread.QueueThreadPlanForRunToAddress(false, func_start_address,true);
302 m_no_more_plans = true;
308 m_no_more_plans = false;
309 m_sub_plan_sp->SetPrivate(true);
315 ThreadPlanStepInRange::SetAvoidRegexp(const char *name)
317 if (!m_avoid_regexp_ap)
318 m_avoid_regexp_ap.reset (new RegularExpression(name));
320 m_avoid_regexp_ap->Compile (name);
324 ThreadPlanStepInRange::SetDefaultFlagValue (uint32_t new_value)
326 // TODO: Should we test this for sanity?
327 ThreadPlanStepInRange::s_default_flag_values = new_value;
331 ThreadPlanStepInRange::FrameMatchesAvoidCriteria ()
333 StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
335 // Check the library list first, as that's cheapest:
336 bool libraries_say_avoid = false;
338 FileSpecList libraries_to_avoid (GetThread().GetLibrariesToAvoid());
339 size_t num_libraries = libraries_to_avoid.GetSize();
340 if (num_libraries > 0)
342 SymbolContext sc(frame->GetSymbolContext(eSymbolContextModule));
343 FileSpec frame_library(sc.module_sp->GetFileSpec());
347 for (size_t i = 0; i < num_libraries; i++)
349 const FileSpec &file_spec(libraries_to_avoid.GetFileSpecAtIndex(i));
350 if (FileSpec::Equal (file_spec, frame_library, false))
352 libraries_say_avoid = true;
358 if (libraries_say_avoid)
361 const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_ap.get();
362 if (avoid_regexp_to_use == nullptr)
363 avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
365 if (avoid_regexp_to_use != nullptr)
367 SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock|eSymbolContextSymbol);
368 if (sc.symbol != nullptr)
370 const char *frame_function_name = sc.GetFunctionName(Mangled::ePreferDemangledWithoutArguments).GetCString();
371 if (frame_function_name)
373 size_t num_matches = 0;
374 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
378 RegularExpression::Match regex_match(num_matches);
380 bool return_value = avoid_regexp_to_use->Execute(frame_function_name, ®ex_match);
386 regex_match.GetMatchAtIndex(frame_function_name,0, match);
387 log->Printf ("Stepping out of function \"%s\" because it matches the avoid regexp \"%s\" - match substring: \"%s\".",
389 avoid_regexp_to_use->GetText(),
402 ThreadPlanStepInRange::DefaultShouldStopHereCallback (ThreadPlan *current_plan, Flags &flags, FrameComparison operation, void *baton)
404 bool should_stop_here = true;
405 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
406 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
408 // First see if the ThreadPlanShouldStopHere default implementation thinks we should get out of here:
409 should_stop_here = ThreadPlanShouldStopHere::DefaultShouldStopHereCallback (current_plan, flags, operation, baton);
410 if (!should_stop_here)
411 return should_stop_here;
413 if (should_stop_here && current_plan->GetKind() == eKindStepInRange && operation == eFrameCompareYounger)
415 ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan);
416 if (step_in_range_plan->m_step_into_target)
418 SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock|eSymbolContextSymbol);
419 if (sc.symbol != nullptr)
421 // First try an exact match, since that's cheap with ConstStrings. Then do a strstr compare.
422 if (step_in_range_plan->m_step_into_target == sc.GetFunctionName())
424 should_stop_here = true;
428 const char *target_name = step_in_range_plan->m_step_into_target.AsCString();
429 const char *function_name = sc.GetFunctionName().AsCString();
431 if (function_name == nullptr)
432 should_stop_here = false;
433 else if (strstr(function_name, target_name) == nullptr)
434 should_stop_here = false;
436 if (log && !should_stop_here)
437 log->Printf("Stepping out of frame %s which did not match step into target %s.",
438 sc.GetFunctionName().AsCString(),
439 step_in_range_plan->m_step_into_target.AsCString());
443 if (should_stop_here)
445 ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan);
446 // Don't log the should_step_out here, it's easier to do it in FrameMatchesAvoidCriteria.
447 should_stop_here = !step_in_range_plan->FrameMatchesAvoidCriteria ();
451 return should_stop_here;
455 ThreadPlanStepInRange::DoPlanExplainsStop (Event *event_ptr)
457 // We always explain a stop. Either we've just done a single step, in which
458 // case we'll do our ordinary processing, or we stopped for some
459 // reason that isn't handled by our sub-plans, in which case we want to just stop right
461 // In general, we don't want to mark the plan as complete for unexplained stops.
462 // For instance, if you step in to some code with no debug info, so you step out
463 // and in the course of that hit a breakpoint, then you want to stop & show the user
464 // the breakpoint, but not unship the step in plan, since you still may want to complete that
465 // plan when you continue. This is particularly true when doing "step in to target function."
468 // The only variation is that if we are doing "step by running to next branch" in which case
469 // if we hit our branch breakpoint we don't set the plan to complete.
471 bool return_value = false;
479 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
482 StopReason reason = stop_info_sp->GetStopReason();
484 if (reason == eStopReasonBreakpoint)
486 if (NextRangeBreakpointExplainsStop(stop_info_sp))
491 else if (IsUsuallyUnexplainedStopReason(reason))
493 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
495 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
496 return_value = false;
511 ThreadPlanStepInRange::DoWillResume (lldb::StateType resume_state, bool current_plan)
513 m_virtual_step = false;
514 if (resume_state == eStateStepping && current_plan)
516 // See if we are about to step over a virtual inlined call.
517 bool step_without_resume = m_thread.DecrementCurrentInlinedDepth();
518 if (step_without_resume)
520 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
522 log->Printf ("ThreadPlanStepInRange::DoWillResume: returning false, inline_depth: %d",
523 m_thread.GetCurrentInlinedDepth());
524 SetStopInfo(StopInfo::CreateStopReasonToTrace(m_thread));
526 // FIXME: Maybe it would be better to create a InlineStep stop reason, but then
527 // the whole rest of the world would have to handle that stop reason.
528 m_virtual_step = true;
530 return !step_without_resume;
536 ThreadPlanStepInRange::IsVirtualStep()
538 return m_virtual_step;