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"
11 #include "lldb/Core/Architecture.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Symbol/Function.h"
14 #include "lldb/Symbol/Symbol.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Target/RegisterContext.h"
17 #include "lldb/Target/SectionLoadList.h"
18 #include "lldb/Target/Target.h"
19 #include "lldb/Target/Thread.h"
20 #include "lldb/Target/ThreadPlanStepOut.h"
21 #include "lldb/Target/ThreadPlanStepThrough.h"
22 #include "lldb/Utility/Log.h"
23 #include "lldb/Utility/RegularExpression.h"
24 #include "lldb/Utility/Stream.h"
27 using namespace lldb_private;
29 uint32_t ThreadPlanStepInRange::s_default_flag_values =
30 ThreadPlanShouldStopHere::eStepInAvoidNoDebug;
32 //----------------------------------------------------------------------
33 // ThreadPlanStepInRange: Step through a stack range, either stepping over or
34 // into based on the value of \a type.
35 //----------------------------------------------------------------------
37 ThreadPlanStepInRange::ThreadPlanStepInRange(
38 Thread &thread, const AddressRange &range,
39 const SymbolContext &addr_context, lldb::RunMode stop_others,
40 LazyBool step_in_avoids_code_without_debug_info,
41 LazyBool step_out_avoids_code_without_debug_info)
42 : ThreadPlanStepRange(ThreadPlan::eKindStepInRange,
43 "Step Range stepping in", thread, range, addr_context,
45 ThreadPlanShouldStopHere(this), m_step_past_prologue(true),
46 m_virtual_step(false) {
49 SetupAvoidNoDebug(step_in_avoids_code_without_debug_info,
50 step_out_avoids_code_without_debug_info);
53 ThreadPlanStepInRange::ThreadPlanStepInRange(
54 Thread &thread, const AddressRange &range,
55 const SymbolContext &addr_context, const char *step_into_target,
56 lldb::RunMode stop_others, LazyBool step_in_avoids_code_without_debug_info,
57 LazyBool step_out_avoids_code_without_debug_info)
58 : ThreadPlanStepRange(ThreadPlan::eKindStepInRange,
59 "Step Range stepping in", thread, range, addr_context,
61 ThreadPlanShouldStopHere(this), m_step_past_prologue(true),
62 m_virtual_step(false), m_step_into_target(step_into_target) {
65 SetupAvoidNoDebug(step_in_avoids_code_without_debug_info,
66 step_out_avoids_code_without_debug_info);
69 ThreadPlanStepInRange::~ThreadPlanStepInRange() = default;
71 void ThreadPlanStepInRange::SetupAvoidNoDebug(
72 LazyBool step_in_avoids_code_without_debug_info,
73 LazyBool step_out_avoids_code_without_debug_info) {
74 bool avoid_nodebug = true;
76 switch (step_in_avoids_code_without_debug_info) {
81 avoid_nodebug = false;
83 case eLazyBoolCalculate:
84 avoid_nodebug = m_thread.GetStepInAvoidsNoDebug();
88 GetFlags().Set(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
90 GetFlags().Clear(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
92 switch (step_out_avoids_code_without_debug_info) {
97 avoid_nodebug = false;
99 case eLazyBoolCalculate:
100 avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
104 GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
106 GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
109 void ThreadPlanStepInRange::GetDescription(Stream *s,
110 lldb::DescriptionLevel level) {
112 auto PrintFailureIfAny = [&]() {
113 if (m_status.Success())
115 s->Printf(" failed (%s)", m_status.AsCString());
118 if (level == lldb::eDescriptionLevelBrief) {
119 s->Printf("step in");
124 s->Printf("Stepping in");
125 bool printed_line_info = false;
126 if (m_addr_context.line_entry.IsValid()) {
127 s->Printf(" through line ");
128 m_addr_context.line_entry.DumpStopContext(s, false);
129 printed_line_info = true;
132 const char *step_into_target = m_step_into_target.AsCString();
133 if (step_into_target && step_into_target[0] != '\0')
134 s->Printf(" targeting %s", m_step_into_target.AsCString());
136 if (!printed_line_info || level == eDescriptionLevelVerbose) {
137 s->Printf(" using ranges:");
146 bool ThreadPlanStepInRange::ShouldStop(Event *event_ptr) {
147 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
152 m_thread.GetRegisterContext()->GetPC(),
153 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
154 log->Printf("ThreadPlanStepInRange reached %s.", s.GetData());
157 if (IsPlanComplete())
160 m_no_more_plans = false;
161 if (m_sub_plan_sp && m_sub_plan_sp->IsPlanComplete()) {
162 if (!m_sub_plan_sp->PlanSucceeded()) {
164 m_no_more_plans = true;
167 m_sub_plan_sp.reset();
170 if (m_virtual_step) {
171 // If we've just completed a virtual step, all we need to do is check for a
172 // ShouldStopHere plan, and otherwise we're done.
173 // FIXME - This can be both a step in and a step out. Probably should
174 // record which in the m_virtual_step.
176 CheckShouldStopHereAndQueueStepOut(eFrameCompareYounger, m_status);
178 // Stepping through should be done running other threads in general, since
179 // we're setting a breakpoint and continuing. So only stop others if we
180 // are explicitly told to do so.
182 bool stop_others = (m_stop_others == lldb::eOnlyThisThread);
184 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
186 if (frame_order == eFrameCompareOlder ||
187 frame_order == eFrameCompareSameParent) {
188 // If we're in an older frame then we should stop.
190 // A caveat to this is if we think the frame is older but we're actually
192 // I'm going to make the assumption that you wouldn't RETURN to a
193 // trampoline. So if we are in a trampoline we think the frame is older
194 // because the trampoline confused the backtracer.
195 m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough(
196 m_stack_id, false, stop_others, m_status);
197 if (!m_sub_plan_sp) {
198 // Otherwise check the ShouldStopHere for step out:
200 CheckShouldStopHereAndQueueStepOut(frame_order, m_status);
203 log->Printf("ShouldStopHere found plan to step out of this frame.");
205 log->Printf("ShouldStopHere no plan to step out of this frame.");
209 "Thought I stepped out, but in fact arrived at a trampoline.");
211 } else if (frame_order == eFrameCompareEqual && InSymbol()) {
212 // If we are not in a place we should step through, we're done. One
213 // tricky bit here is that some stubs don't push a frame, so we have to
214 // check both the case of a frame that is younger, or the same as this
215 // frame. However, if the frame is the same, and we are still in the
216 // symbol we started in, the we don't need to do this. This first check
217 // isn't strictly necessary, but it is more efficient.
219 // If we're still in the range, keep going, either by running to the next
220 // branch breakpoint, or by stepping.
222 SetNextBranchBreakpoint();
227 m_no_more_plans = true;
231 // If we get to this point, we're not going to use a previously set "next
232 // branch" breakpoint, so delete it:
233 ClearNextBranchBreakpoint();
235 // We may have set the plan up above in the FrameIsOlder section:
238 m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough(
239 m_stack_id, false, stop_others, m_status);
243 log->Printf("Found a step through plan: %s", m_sub_plan_sp->GetName());
245 log->Printf("No step through plan found.");
248 // If not, give the "should_stop" callback a chance to push a plan to get
249 // us out of here. But only do that if we actually have stepped in.
250 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
251 m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order, m_status);
253 // If we've stepped in and we are going to stop here, check to see if we
254 // were asked to run past the prologue, and if so do that.
256 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger &&
257 m_step_past_prologue) {
258 lldb::StackFrameSP curr_frame = m_thread.GetStackFrameAtIndex(0);
260 size_t bytes_to_skip = 0;
261 lldb::addr_t curr_addr = m_thread.GetRegisterContext()->GetPC();
262 Address func_start_address;
264 SymbolContext sc = curr_frame->GetSymbolContext(eSymbolContextFunction |
265 eSymbolContextSymbol);
268 func_start_address = sc.function->GetAddressRange().GetBaseAddress();
270 func_start_address.GetLoadAddress(
271 m_thread.CalculateTarget().get()))
272 bytes_to_skip = sc.function->GetPrologueByteSize();
273 } else if (sc.symbol) {
274 func_start_address = sc.symbol->GetAddress();
276 func_start_address.GetLoadAddress(
277 m_thread.CalculateTarget().get()))
278 bytes_to_skip = sc.symbol->GetPrologueByteSize();
281 if (bytes_to_skip == 0 && sc.symbol) {
282 TargetSP target = m_thread.CalculateTarget();
283 const Architecture *arch = target->GetArchitecturePlugin();
285 Address curr_sec_addr;
286 target->GetSectionLoadList().ResolveLoadAddress(curr_addr,
288 bytes_to_skip = arch->GetBytesToSkip(*sc.symbol, curr_sec_addr);
292 if (bytes_to_skip != 0) {
293 func_start_address.Slide(bytes_to_skip);
294 log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP);
296 log->Printf("Pushing past prologue ");
298 m_sub_plan_sp = m_thread.QueueThreadPlanForRunToAddress(
299 false, func_start_address, true, m_status);
305 if (!m_sub_plan_sp) {
306 m_no_more_plans = true;
310 m_no_more_plans = false;
311 m_sub_plan_sp->SetPrivate(true);
316 void ThreadPlanStepInRange::SetAvoidRegexp(const char *name) {
317 auto name_ref = llvm::StringRef::withNullAsEmpty(name);
318 if (!m_avoid_regexp_ap)
319 m_avoid_regexp_ap.reset(new RegularExpression(name_ref));
321 m_avoid_regexp_ap->Compile(name_ref);
324 void ThreadPlanStepInRange::SetDefaultFlagValue(uint32_t new_value) {
325 // TODO: Should we test this for sanity?
326 ThreadPlanStepInRange::s_default_flag_values = new_value;
329 bool ThreadPlanStepInRange::FrameMatchesAvoidCriteria() {
330 StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
332 // Check the library list first, as that's cheapest:
333 bool libraries_say_avoid = false;
335 FileSpecList libraries_to_avoid(GetThread().GetLibrariesToAvoid());
336 size_t num_libraries = libraries_to_avoid.GetSize();
337 if (num_libraries > 0) {
338 SymbolContext sc(frame->GetSymbolContext(eSymbolContextModule));
339 FileSpec frame_library(sc.module_sp->GetFileSpec());
342 for (size_t i = 0; i < num_libraries; i++) {
343 const FileSpec &file_spec(libraries_to_avoid.GetFileSpecAtIndex(i));
344 if (FileSpec::Equal(file_spec, frame_library, false)) {
345 libraries_say_avoid = true;
351 if (libraries_say_avoid)
354 const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_ap.get();
355 if (avoid_regexp_to_use == nullptr)
356 avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
358 if (avoid_regexp_to_use != nullptr) {
359 SymbolContext sc = frame->GetSymbolContext(
360 eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
361 if (sc.symbol != nullptr) {
362 const char *frame_function_name =
363 sc.GetFunctionName(Mangled::ePreferDemangledWithoutArguments)
365 if (frame_function_name) {
366 size_t num_matches = 0;
367 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
371 RegularExpression::Match regex_match(num_matches);
374 avoid_regexp_to_use->Execute(frame_function_name, ®ex_match);
378 regex_match.GetMatchAtIndex(frame_function_name, 0, match);
379 log->Printf("Stepping out of function \"%s\" because it matches "
380 "the avoid regexp \"%s\" - match substring: \"%s\".",
382 avoid_regexp_to_use->GetText().str().c_str(),
393 bool ThreadPlanStepInRange::DefaultShouldStopHereCallback(
394 ThreadPlan *current_plan, Flags &flags, FrameComparison operation,
395 Status &status, void *baton) {
396 bool should_stop_here = true;
397 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
398 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
400 // First see if the ThreadPlanShouldStopHere default implementation thinks we
401 // should get out of here:
402 should_stop_here = ThreadPlanShouldStopHere::DefaultShouldStopHereCallback(
403 current_plan, flags, operation, status, baton);
404 if (!should_stop_here)
405 return should_stop_here;
407 if (should_stop_here && current_plan->GetKind() == eKindStepInRange &&
408 operation == eFrameCompareYounger) {
409 ThreadPlanStepInRange *step_in_range_plan =
410 static_cast<ThreadPlanStepInRange *>(current_plan);
411 if (step_in_range_plan->m_step_into_target) {
412 SymbolContext sc = frame->GetSymbolContext(
413 eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
414 if (sc.symbol != nullptr) {
415 // First try an exact match, since that's cheap with ConstStrings.
416 // Then do a strstr compare.
417 if (step_in_range_plan->m_step_into_target == sc.GetFunctionName()) {
418 should_stop_here = true;
420 const char *target_name =
421 step_in_range_plan->m_step_into_target.AsCString();
422 const char *function_name = sc.GetFunctionName().AsCString();
424 if (function_name == nullptr)
425 should_stop_here = false;
426 else if (strstr(function_name, target_name) == nullptr)
427 should_stop_here = false;
429 if (log && !should_stop_here)
430 log->Printf("Stepping out of frame %s which did not match step into "
432 sc.GetFunctionName().AsCString(),
433 step_in_range_plan->m_step_into_target.AsCString());
437 if (should_stop_here) {
438 ThreadPlanStepInRange *step_in_range_plan =
439 static_cast<ThreadPlanStepInRange *>(current_plan);
440 // Don't log the should_step_out here, it's easier to do it in
441 // FrameMatchesAvoidCriteria.
442 should_stop_here = !step_in_range_plan->FrameMatchesAvoidCriteria();
446 return should_stop_here;
449 bool ThreadPlanStepInRange::DoPlanExplainsStop(Event *event_ptr) {
450 // We always explain a stop. Either we've just done a single step, in which
451 // case we'll do our ordinary processing, or we stopped for some reason that
452 // isn't handled by our sub-plans, in which case we want to just stop right
453 // away. In general, we don't want to mark the plan as complete for
454 // unexplained stops. For instance, if you step in to some code with no debug
455 // info, so you step out and in the course of that hit a breakpoint, then you
456 // want to stop & show the user the breakpoint, but not unship the step in
457 // plan, since you still may want to complete that plan when you continue.
458 // This is particularly true when doing "step in to target function."
461 // The only variation is that if we are doing "step by running to next
462 // branch" in which case if we hit our branch breakpoint we don't set the
465 bool return_value = false;
467 if (m_virtual_step) {
470 StopInfoSP stop_info_sp = GetPrivateStopInfo();
472 StopReason reason = stop_info_sp->GetStopReason();
474 if (reason == eStopReasonBreakpoint) {
475 if (NextRangeBreakpointExplainsStop(stop_info_sp)) {
478 } else if (IsUsuallyUnexplainedStopReason(reason)) {
479 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
481 log->PutCString("ThreadPlanStepInRange got asked if it explains the "
482 "stop for some reason other than step.");
483 return_value = false;
494 bool ThreadPlanStepInRange::DoWillResume(lldb::StateType resume_state,
496 m_virtual_step = false;
497 if (resume_state == eStateStepping && current_plan) {
498 // See if we are about to step over a virtual inlined call.
499 bool step_without_resume = m_thread.DecrementCurrentInlinedDepth();
500 if (step_without_resume) {
501 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
503 log->Printf("ThreadPlanStepInRange::DoWillResume: returning false, "
505 m_thread.GetCurrentInlinedDepth());
506 SetStopInfo(StopInfo::CreateStopReasonToTrace(m_thread));
508 // FIXME: Maybe it would be better to create a InlineStep stop reason, but
510 // the whole rest of the world would have to handle that stop reason.
511 m_virtual_step = true;
513 return !step_without_resume;
518 bool ThreadPlanStepInRange::IsVirtualStep() { return m_virtual_step; }