1 //===-- ThreadPlanStepInRange.cpp -----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Target/ThreadPlanStepInRange.h"
10 #include "lldb/Core/Architecture.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Symbol/Function.h"
13 #include "lldb/Symbol/Symbol.h"
14 #include "lldb/Target/Process.h"
15 #include "lldb/Target/RegisterContext.h"
16 #include "lldb/Target/SectionLoadList.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Target/Thread.h"
19 #include "lldb/Target/ThreadPlanStepOut.h"
20 #include "lldb/Target/ThreadPlanStepThrough.h"
21 #include "lldb/Utility/LLDBLog.h"
22 #include "lldb/Utility/Log.h"
23 #include "lldb/Utility/RegularExpression.h"
24 #include "lldb/Utility/Stream.h"
25 
26 using namespace lldb;
27 using namespace lldb_private;
28 
29 uint32_t ThreadPlanStepInRange::s_default_flag_values =
30     ThreadPlanShouldStopHere::eStepInAvoidNoDebug;
31 
32 // ThreadPlanStepInRange: Step through a stack range, either stepping over or
33 // into based on the value of \a type.
34 
35 ThreadPlanStepInRange::ThreadPlanStepInRange(
36     Thread &thread, const AddressRange &range,
37     const SymbolContext &addr_context, const char *step_into_target,
38     lldb::RunMode stop_others, LazyBool step_in_avoids_code_without_debug_info,
39     LazyBool step_out_avoids_code_without_debug_info)
40     : ThreadPlanStepRange(ThreadPlan::eKindStepInRange,
41                           "Step Range stepping in", thread, range, addr_context,
42                           stop_others),
43       ThreadPlanShouldStopHere(this), m_step_past_prologue(true),
44       m_virtual_step(false), m_step_into_target(step_into_target) {
45   SetCallbacks();
46   SetFlagsToDefault();
47   SetupAvoidNoDebug(step_in_avoids_code_without_debug_info,
48                     step_out_avoids_code_without_debug_info);
49 }
50 
51 ThreadPlanStepInRange::~ThreadPlanStepInRange() = default;
52 
53 void ThreadPlanStepInRange::SetupAvoidNoDebug(
54     LazyBool step_in_avoids_code_without_debug_info,
55     LazyBool step_out_avoids_code_without_debug_info) {
56   bool avoid_nodebug = true;
57   Thread &thread = GetThread();
58   switch (step_in_avoids_code_without_debug_info) {
59   case eLazyBoolYes:
60     avoid_nodebug = true;
61     break;
62   case eLazyBoolNo:
63     avoid_nodebug = false;
64     break;
65   case eLazyBoolCalculate:
66     avoid_nodebug = thread.GetStepInAvoidsNoDebug();
67     break;
68   }
69   if (avoid_nodebug)
70     GetFlags().Set(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
71   else
72     GetFlags().Clear(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
73 
74   switch (step_out_avoids_code_without_debug_info) {
75   case eLazyBoolYes:
76     avoid_nodebug = true;
77     break;
78   case eLazyBoolNo:
79     avoid_nodebug = false;
80     break;
81   case eLazyBoolCalculate:
82     avoid_nodebug = thread.GetStepOutAvoidsNoDebug();
83     break;
84   }
85   if (avoid_nodebug)
86     GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
87   else
88     GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
89 }
90 
91 void ThreadPlanStepInRange::GetDescription(Stream *s,
92                                            lldb::DescriptionLevel level) {
93 
94   auto PrintFailureIfAny = [&]() {
95     if (m_status.Success())
96       return;
97     s->Printf(" failed (%s)", m_status.AsCString());
98   };
99 
100   if (level == lldb::eDescriptionLevelBrief) {
101     s->Printf("step in");
102     PrintFailureIfAny();
103     return;
104   }
105 
106   s->Printf("Stepping in");
107   bool printed_line_info = false;
108   if (m_addr_context.line_entry.IsValid()) {
109     s->Printf(" through line ");
110     m_addr_context.line_entry.DumpStopContext(s, false);
111     printed_line_info = true;
112   }
113 
114   const char *step_into_target = m_step_into_target.AsCString();
115   if (step_into_target && step_into_target[0] != '\0')
116     s->Printf(" targeting %s", m_step_into_target.AsCString());
117 
118   if (!printed_line_info || level == eDescriptionLevelVerbose) {
119     s->Printf(" using ranges:");
120     DumpRanges(s);
121   }
122 
123   PrintFailureIfAny();
124 
125   s->PutChar('.');
126 }
127 
128 bool ThreadPlanStepInRange::ShouldStop(Event *event_ptr) {
129   Log *log = GetLog(LLDBLog::Step);
130 
131   if (log) {
132     StreamString s;
133     DumpAddress(s.AsRawOstream(), GetThread().GetRegisterContext()->GetPC(),
134                 GetTarget().GetArchitecture().GetAddressByteSize());
135     LLDB_LOGF(log, "ThreadPlanStepInRange reached %s.", s.GetData());
136   }
137 
138   if (IsPlanComplete())
139     return true;
140 
141   m_no_more_plans = false;
142   if (m_sub_plan_sp && m_sub_plan_sp->IsPlanComplete()) {
143     if (!m_sub_plan_sp->PlanSucceeded()) {
144       SetPlanComplete();
145       m_no_more_plans = true;
146       return true;
147     } else
148       m_sub_plan_sp.reset();
149   }
150 
151   if (m_virtual_step) {
152     // If we've just completed a virtual step, all we need to do is check for a
153     // ShouldStopHere plan, and otherwise we're done.
154     // FIXME - This can be both a step in and a step out.  Probably should
155     // record which in the m_virtual_step.
156     m_sub_plan_sp =
157         CheckShouldStopHereAndQueueStepOut(eFrameCompareYounger, m_status);
158   } else {
159     // Stepping through should be done running other threads in general, since
160     // we're setting a breakpoint and continuing.  So only stop others if we
161     // are explicitly told to do so.
162 
163     bool stop_others = (m_stop_others == lldb::eOnlyThisThread);
164 
165     FrameComparison frame_order = CompareCurrentFrameToStartFrame();
166 
167     Thread &thread = GetThread();
168     if (frame_order == eFrameCompareOlder ||
169         frame_order == eFrameCompareSameParent) {
170       // If we're in an older frame then we should stop.
171       //
172       // A caveat to this is if we think the frame is older but we're actually
173       // in a trampoline.
174       // I'm going to make the assumption that you wouldn't RETURN to a
175       // trampoline.  So if we are in a trampoline we think the frame is older
176       // because the trampoline confused the backtracer.
177       m_sub_plan_sp = thread.QueueThreadPlanForStepThrough(
178           m_stack_id, false, stop_others, m_status);
179       if (!m_sub_plan_sp) {
180         // Otherwise check the ShouldStopHere for step out:
181         m_sub_plan_sp =
182             CheckShouldStopHereAndQueueStepOut(frame_order, m_status);
183         if (log) {
184           if (m_sub_plan_sp)
185             LLDB_LOGF(log,
186                       "ShouldStopHere found plan to step out of this frame.");
187           else
188             LLDB_LOGF(log, "ShouldStopHere no plan to step out of this frame.");
189         }
190       } else if (log) {
191         LLDB_LOGF(
192             log, "Thought I stepped out, but in fact arrived at a trampoline.");
193       }
194     } else if (frame_order == eFrameCompareEqual && InSymbol()) {
195       // If we are not in a place we should step through, we're done. One
196       // tricky bit here is that some stubs don't push a frame, so we have to
197       // check both the case of a frame that is younger, or the same as this
198       // frame. However, if the frame is the same, and we are still in the
199       // symbol we started in, the we don't need to do this.  This first check
200       // isn't strictly necessary, but it is more efficient.
201 
202       // If we're still in the range, keep going, either by running to the next
203       // branch breakpoint, or by stepping.
204       if (InRange()) {
205         SetNextBranchBreakpoint();
206         return false;
207       }
208 
209       SetPlanComplete();
210       m_no_more_plans = true;
211       return true;
212     }
213 
214     // If we get to this point, we're not going to use a previously set "next
215     // branch" breakpoint, so delete it:
216     ClearNextBranchBreakpoint();
217 
218     // We may have set the plan up above in the FrameIsOlder section:
219 
220     if (!m_sub_plan_sp)
221       m_sub_plan_sp = thread.QueueThreadPlanForStepThrough(
222           m_stack_id, false, stop_others, m_status);
223 
224     if (log) {
225       if (m_sub_plan_sp)
226         LLDB_LOGF(log, "Found a step through plan: %s",
227                   m_sub_plan_sp->GetName());
228       else
229         LLDB_LOGF(log, "No step through plan found.");
230     }
231 
232     // If not, give the "should_stop" callback a chance to push a plan to get
233     // us out of here. But only do that if we actually have stepped in.
234     if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
235       m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order, m_status);
236 
237     // If we've stepped in and we are going to stop here, check to see if we
238     // were asked to run past the prologue, and if so do that.
239 
240     if (!m_sub_plan_sp && frame_order == eFrameCompareYounger &&
241         m_step_past_prologue) {
242       lldb::StackFrameSP curr_frame = thread.GetStackFrameAtIndex(0);
243       if (curr_frame) {
244         size_t bytes_to_skip = 0;
245         lldb::addr_t curr_addr = thread.GetRegisterContext()->GetPC();
246         Address func_start_address;
247 
248         SymbolContext sc = curr_frame->GetSymbolContext(eSymbolContextFunction |
249                                                         eSymbolContextSymbol);
250 
251         if (sc.function) {
252           func_start_address = sc.function->GetAddressRange().GetBaseAddress();
253           if (curr_addr == func_start_address.GetLoadAddress(&GetTarget()))
254             bytes_to_skip = sc.function->GetPrologueByteSize();
255         } else if (sc.symbol) {
256           func_start_address = sc.symbol->GetAddress();
257           if (curr_addr == func_start_address.GetLoadAddress(&GetTarget()))
258             bytes_to_skip = sc.symbol->GetPrologueByteSize();
259         }
260 
261         if (bytes_to_skip == 0 && sc.symbol) {
262           const Architecture *arch = GetTarget().GetArchitecturePlugin();
263           if (arch) {
264             Address curr_sec_addr;
265             GetTarget().GetSectionLoadList().ResolveLoadAddress(curr_addr,
266                                                                 curr_sec_addr);
267             bytes_to_skip = arch->GetBytesToSkip(*sc.symbol, curr_sec_addr);
268           }
269         }
270 
271         if (bytes_to_skip != 0) {
272           func_start_address.Slide(bytes_to_skip);
273           log = GetLog(LLDBLog::Step);
274           LLDB_LOGF(log, "Pushing past prologue ");
275 
276           m_sub_plan_sp = thread.QueueThreadPlanForRunToAddress(
277               false, func_start_address, true, m_status);
278         }
279       }
280     }
281   }
282 
283   if (!m_sub_plan_sp) {
284     m_no_more_plans = true;
285     SetPlanComplete();
286     return true;
287   } else {
288     m_no_more_plans = false;
289     m_sub_plan_sp->SetPrivate(true);
290     return false;
291   }
292 }
293 
294 void ThreadPlanStepInRange::SetAvoidRegexp(const char *name) {
295   if (m_avoid_regexp_up)
296     *m_avoid_regexp_up = RegularExpression(name);
297   else
298     m_avoid_regexp_up = std::make_unique<RegularExpression>(name);
299 }
300 
301 void ThreadPlanStepInRange::SetDefaultFlagValue(uint32_t new_value) {
302   // TODO: Should we test this for sanity?
303   ThreadPlanStepInRange::s_default_flag_values = new_value;
304 }
305 
306 bool ThreadPlanStepInRange::FrameMatchesAvoidCriteria() {
307   StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
308 
309   // Check the library list first, as that's cheapest:
310   bool libraries_say_avoid = false;
311 
312   FileSpecList libraries_to_avoid(GetThread().GetLibrariesToAvoid());
313   size_t num_libraries = libraries_to_avoid.GetSize();
314   if (num_libraries > 0) {
315     SymbolContext sc(frame->GetSymbolContext(eSymbolContextModule));
316     FileSpec frame_library(sc.module_sp->GetFileSpec());
317 
318     if (frame_library) {
319       for (size_t i = 0; i < num_libraries; i++) {
320         const FileSpec &file_spec(libraries_to_avoid.GetFileSpecAtIndex(i));
321         if (FileSpec::Match(file_spec, frame_library)) {
322           libraries_say_avoid = true;
323           break;
324         }
325       }
326     }
327   }
328   if (libraries_say_avoid)
329     return true;
330 
331   const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_up.get();
332   if (avoid_regexp_to_use == nullptr)
333     avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
334 
335   if (avoid_regexp_to_use != nullptr) {
336     SymbolContext sc = frame->GetSymbolContext(
337         eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
338     if (sc.symbol != nullptr) {
339       const char *frame_function_name =
340           sc.GetFunctionName(Mangled::ePreferDemangledWithoutArguments)
341               .GetCString();
342       if (frame_function_name) {
343         llvm::SmallVector<llvm::StringRef, 2> matches;
344         bool return_value =
345             avoid_regexp_to_use->Execute(frame_function_name, &matches);
346         if (return_value && matches.size() > 1) {
347           std::string match = matches[1].str();
348           LLDB_LOGF(GetLog(LLDBLog::Step),
349                     "Stepping out of function \"%s\" because it matches "
350                     "the avoid regexp \"%s\" - match substring: \"%s\".",
351                     frame_function_name,
352                     avoid_regexp_to_use->GetText().str().c_str(),
353                     match.c_str());
354         }
355         return return_value;
356       }
357     }
358   }
359   return false;
360 }
361 
362 bool ThreadPlanStepInRange::DefaultShouldStopHereCallback(
363     ThreadPlan *current_plan, Flags &flags, FrameComparison operation,
364     Status &status, void *baton) {
365   bool should_stop_here = true;
366   StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
367   Log *log = GetLog(LLDBLog::Step);
368 
369   // First see if the ThreadPlanShouldStopHere default implementation thinks we
370   // should get out of here:
371   should_stop_here = ThreadPlanShouldStopHere::DefaultShouldStopHereCallback(
372       current_plan, flags, operation, status, baton);
373   if (!should_stop_here)
374     return false;
375 
376   if (should_stop_here && current_plan->GetKind() == eKindStepInRange &&
377       operation == eFrameCompareYounger) {
378     ThreadPlanStepInRange *step_in_range_plan =
379         static_cast<ThreadPlanStepInRange *>(current_plan);
380     if (step_in_range_plan->m_step_into_target) {
381       SymbolContext sc = frame->GetSymbolContext(
382           eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
383       if (sc.symbol != nullptr) {
384         // First try an exact match, since that's cheap with ConstStrings.
385         // Then do a strstr compare.
386         if (step_in_range_plan->m_step_into_target == sc.GetFunctionName()) {
387           should_stop_here = true;
388         } else {
389           const char *target_name =
390               step_in_range_plan->m_step_into_target.AsCString();
391           const char *function_name = sc.GetFunctionName().AsCString();
392 
393           if (function_name == nullptr)
394             should_stop_here = false;
395           else if (strstr(function_name, target_name) == nullptr)
396             should_stop_here = false;
397         }
398         if (log && !should_stop_here)
399           LLDB_LOGF(log,
400                     "Stepping out of frame %s which did not match step into "
401                     "target %s.",
402                     sc.GetFunctionName().AsCString(),
403                     step_in_range_plan->m_step_into_target.AsCString());
404       }
405     }
406 
407     if (should_stop_here) {
408       ThreadPlanStepInRange *step_in_range_plan =
409           static_cast<ThreadPlanStepInRange *>(current_plan);
410       // Don't log the should_step_out here, it's easier to do it in
411       // FrameMatchesAvoidCriteria.
412       should_stop_here = !step_in_range_plan->FrameMatchesAvoidCriteria();
413     }
414   }
415 
416   return should_stop_here;
417 }
418 
419 bool ThreadPlanStepInRange::DoPlanExplainsStop(Event *event_ptr) {
420   // We always explain a stop.  Either we've just done a single step, in which
421   // case we'll do our ordinary processing, or we stopped for some reason that
422   // isn't handled by our sub-plans, in which case we want to just stop right
423   // away. In general, we don't want to mark the plan as complete for
424   // unexplained stops. For instance, if you step in to some code with no debug
425   // info, so you step out and in the course of that hit a breakpoint, then you
426   // want to stop & show the user the breakpoint, but not unship the step in
427   // plan, since you still may want to complete that plan when you continue.
428   // This is particularly true when doing "step in to target function."
429   // stepping.
430   //
431   // The only variation is that if we are doing "step by running to next
432   // branch" in which case if we hit our branch breakpoint we don't set the
433   // plan to complete.
434 
435   bool return_value = false;
436 
437   if (m_virtual_step) {
438     return_value = true;
439   } else {
440     StopInfoSP stop_info_sp = GetPrivateStopInfo();
441     if (stop_info_sp) {
442       StopReason reason = stop_info_sp->GetStopReason();
443 
444       if (reason == eStopReasonBreakpoint) {
445         if (NextRangeBreakpointExplainsStop(stop_info_sp)) {
446           return_value = true;
447         }
448       } else if (IsUsuallyUnexplainedStopReason(reason)) {
449         Log *log = GetLog(LLDBLog::Step);
450         if (log)
451           log->PutCString("ThreadPlanStepInRange got asked if it explains the "
452                           "stop for some reason other than step.");
453         return_value = false;
454       } else {
455         return_value = true;
456       }
457     } else
458       return_value = true;
459   }
460 
461   return return_value;
462 }
463 
464 bool ThreadPlanStepInRange::DoWillResume(lldb::StateType resume_state,
465                                          bool current_plan) {
466   m_virtual_step = false;
467   if (resume_state == eStateStepping && current_plan) {
468     Thread &thread = GetThread();
469     // See if we are about to step over a virtual inlined call.
470     bool step_without_resume = thread.DecrementCurrentInlinedDepth();
471     if (step_without_resume) {
472       Log *log = GetLog(LLDBLog::Step);
473       LLDB_LOGF(log,
474                 "ThreadPlanStepInRange::DoWillResume: returning false, "
475                 "inline_depth: %d",
476                 thread.GetCurrentInlinedDepth());
477       SetStopInfo(StopInfo::CreateStopReasonToTrace(thread));
478 
479       // FIXME: Maybe it would be better to create a InlineStep stop reason, but
480       // then
481       // the whole rest of the world would have to handle that stop reason.
482       m_virtual_step = true;
483     }
484     return !step_without_resume;
485   }
486   return true;
487 }
488 
489 bool ThreadPlanStepInRange::IsVirtualStep() { return m_virtual_step; }
490