LLDB  mainline
ThreadPlanStepOverRange.cpp
Go to the documentation of this file.
1 //===-- ThreadPlanStepOverRange.cpp -----------------------------*- C++ -*-===//
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 
10 #include "lldb/Symbol/Block.h"
12 #include "lldb/Symbol/Function.h"
13 #include "lldb/Symbol/LineTable.h"
14 #include "lldb/Target/Process.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Target/Thread.h"
20 #include "lldb/Utility/Log.h"
21 #include "lldb/Utility/Stream.h"
22 
23 using namespace lldb_private;
24 using namespace lldb;
25 
26 uint32_t ThreadPlanStepOverRange::s_default_flag_values = 0;
27 
28 // ThreadPlanStepOverRange: Step through a stack range, either stepping over or
29 // into based on the value of \a type.
30 
32  Thread &thread, const AddressRange &range,
33  const SymbolContext &addr_context, lldb::RunMode stop_others,
34  LazyBool step_out_avoids_code_without_debug_info)
35  : ThreadPlanStepRange(ThreadPlan::eKindStepOverRange,
36  "Step range stepping over", thread, range,
37  addr_context, stop_others),
38  ThreadPlanShouldStopHere(this), m_first_resume(true) {
40  SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
41 }
42 
44 
46  lldb::DescriptionLevel level) {
47  auto PrintFailureIfAny = [&]() {
48  if (m_status.Success())
49  return;
50  s->Printf(" failed (%s)", m_status.AsCString());
51  };
52 
53  if (level == lldb::eDescriptionLevelBrief) {
54  s->Printf("step over");
55  PrintFailureIfAny();
56  return;
57  }
58 
59  s->Printf("Stepping over");
60  bool printed_line_info = false;
62  s->Printf(" line ");
64  printed_line_info = true;
65  }
66 
67  if (!printed_line_info || level == eDescriptionLevelVerbose) {
68  s->Printf(" using ranges: ");
69  DumpRanges(s);
70  }
71 
72  PrintFailureIfAny();
73 
74  s->PutChar('.');
75 }
76 
77 void ThreadPlanStepOverRange::SetupAvoidNoDebug(
78  LazyBool step_out_avoids_code_without_debug_info) {
79  bool avoid_nodebug = true;
80  switch (step_out_avoids_code_without_debug_info) {
81  case eLazyBoolYes:
82  avoid_nodebug = true;
83  break;
84  case eLazyBoolNo:
85  avoid_nodebug = false;
86  break;
87  case eLazyBoolCalculate:
88  avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
89  break;
90  }
91  if (avoid_nodebug)
93  else
95  // Step Over plans should always avoid no-debug on step in. Seems like you
96  // shouldn't have to say this, but a tail call looks more like a step in that
97  // a step out, so we want to catch this case.
99 }
100 
101 bool ThreadPlanStepOverRange::IsEquivalentContext(
102  const SymbolContext &context) {
103  // Match as much as is specified in the m_addr_context: This is a fairly
104  // loose sanity check. Note, sometimes the target doesn't get filled in so I
105  // left out the target check. And sometimes the module comes in as the .o
106  // file from the inlined range, so I left that out too...
108  if (m_addr_context.comp_unit != context.comp_unit)
109  return false;
110  if (m_addr_context.function) {
111  if (m_addr_context.function != context.function)
112  return false;
113  // It is okay to return to a different block of a straight function, we
114  // only have to be more careful if returning from one inlined block to
115  // another.
116  if (m_addr_context.block->GetInlinedFunctionInfo() == nullptr &&
117  context.block->GetInlinedFunctionInfo() == nullptr)
118  return true;
119  return m_addr_context.block == context.block;
120  }
121  }
122  // Fall back to symbol if we have no decision from comp_unit/function/block.
123  return m_addr_context.symbol && m_addr_context.symbol == context.symbol;
124 }
125 
128 
129  if (log) {
130  StreamString s;
131  s.Address(
132  m_thread.GetRegisterContext()->GetPC(),
133  m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
134  log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
135  }
136 
137  // If we're out of the range but in the same frame or in our caller's frame
138  // then we should stop. When stepping out we only stop others if we are
139  // forcing running one thread.
140  bool stop_others = (m_stop_others == lldb::eOnlyThisThread);
141  ThreadPlanSP new_plan_sp;
143 
144  if (frame_order == eFrameCompareOlder) {
145  // If we're in an older frame then we should stop.
146  //
147  // A caveat to this is if we think the frame is older but we're actually in
148  // a trampoline.
149  // I'm going to make the assumption that you wouldn't RETURN to a
150  // trampoline. So if we are in a trampoline we think the frame is older
151  // because the trampoline confused the backtracer. As below, we step
152  // through first, and then try to figure out how to get back out again.
153 
155  stop_others, m_status);
156 
157  if (new_plan_sp && log)
158  log->Printf(
159  "Thought I stepped out, but in fact arrived at a trampoline.");
160  } else if (frame_order == eFrameCompareYounger) {
161  // Make sure we really are in a new frame. Do that by unwinding and seeing
162  // if the start function really is our start function...
163  for (uint32_t i = 1;; ++i) {
164  StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(i);
165  if (!older_frame_sp) {
166  // We can't unwind the next frame we should just get out of here &
167  // stop...
168  break;
169  }
170 
171  const SymbolContext &older_context =
172  older_frame_sp->GetSymbolContext(eSymbolContextEverything);
173  if (IsEquivalentContext(older_context)) {
175  false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0,
176  m_status, true);
177  break;
178  } else {
180  m_stack_id, false, stop_others, m_status);
181  // If we found a way through, then we should stop recursing.
182  if (new_plan_sp)
183  break;
184  }
185  }
186  } else {
187  // If we're still in the range, keep going.
188  if (InRange()) {
190  return false;
191  }
192 
193  if (!InSymbol()) {
194  // This one is a little tricky. Sometimes we may be in a stub or
195  // something similar, in which case we need to get out of there. But if
196  // we are in a stub then it's likely going to be hard to get out from
197  // here. It is probably easiest to step into the stub, and then it will
198  // be straight-forward to step out.
200  m_stack_id, false, stop_others, m_status);
201  } else {
202  // The current clang (at least through 424) doesn't always get the
203  // address range for the DW_TAG_inlined_subroutines right, so that when
204  // you leave the inlined range the line table says you are still in the
205  // source file of the inlining function. This is bad, because now you
206  // are missing the stack frame for the function containing the inlining,
207  // and if you sensibly do "finish" to get out of this function you will
208  // instead exit the containing function. To work around this, we check
209  // whether we are still in the source file we started in, and if not
210  // assume it is an error, and push a plan to get us out of this line and
211  // back to the containing file.
212 
214  SymbolContext sc;
215  StackFrameSP frame_sp = m_thread.GetStackFrameAtIndex(0);
216  sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
217  if (sc.line_entry.IsValid()) {
218  if (sc.line_entry.original_file !=
222  // Okay, find the next occurrence of this file in the line table:
224  if (line_table) {
225  Address cur_address = frame_sp->GetFrameCodeAddress();
226  uint32_t entry_idx;
227  LineEntry line_entry;
228  if (line_table->FindLineEntryByAddress(cur_address, line_entry,
229  &entry_idx)) {
230  LineEntry next_line_entry;
231  bool step_past_remaining_inline = false;
232  if (entry_idx > 0) {
233  // We require the previous line entry and the current line
234  // entry come from the same file. The other requirement is
235  // that the previous line table entry be part of an inlined
236  // block, we don't want to step past cases where people have
237  // inlined some code fragment by using #include <source-
238  // fragment.c> directly.
239  LineEntry prev_line_entry;
240  if (line_table->GetLineEntryAtIndex(entry_idx - 1,
241  prev_line_entry) &&
242  prev_line_entry.original_file ==
243  line_entry.original_file) {
244  SymbolContext prev_sc;
245  Address prev_address =
246  prev_line_entry.range.GetBaseAddress();
247  prev_address.CalculateSymbolContext(&prev_sc);
248  if (prev_sc.block) {
249  Block *inlined_block =
250  prev_sc.block->GetContainingInlinedBlock();
251  if (inlined_block) {
252  AddressRange inline_range;
253  inlined_block->GetRangeContainingAddress(prev_address,
254  inline_range);
255  if (!inline_range.ContainsFileAddress(cur_address)) {
256 
257  step_past_remaining_inline = true;
258  }
259  }
260  }
261  }
262  }
263 
264  if (step_past_remaining_inline) {
265  uint32_t look_ahead_step = 1;
266  while (line_table->GetLineEntryAtIndex(
267  entry_idx + look_ahead_step, next_line_entry)) {
268  // Make sure we haven't wandered out of the function we
269  // started from...
270  Address next_line_address =
271  next_line_entry.range.GetBaseAddress();
272  Function *next_line_function =
273  next_line_address.CalculateSymbolContextFunction();
274  if (next_line_function != m_addr_context.function)
275  break;
276 
277  if (next_line_entry.original_file ==
279  const bool abort_other_plans = false;
280  const RunMode stop_other_threads = RunMode::eAllThreads;
282  ->GetRegisterContext()
283  ->GetPC();
284  AddressRange step_range(
285  cur_pc,
286  next_line_address.GetLoadAddress(&GetTarget()) -
287  cur_pc);
288 
290  abort_other_plans, step_range, sc, stop_other_threads,
291  m_status);
292  break;
293  }
294  look_ahead_step++;
295  }
296  }
297  }
298  }
299  }
300  }
301  }
302  }
303  }
304 
305  // If we get to this point, we're not going to use a previously set "next
306  // branch" breakpoint, so delete it:
308 
309  // If we haven't figured out something to do yet, then ask the ShouldStopHere
310  // callback:
311  if (!new_plan_sp) {
312  new_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order, m_status);
313  }
314 
315  if (!new_plan_sp)
316  m_no_more_plans = true;
317  else {
318  // Any new plan will be an implementation plan, so mark it private:
319  new_plan_sp->SetPrivate(true);
320  m_no_more_plans = false;
321  }
322 
323  if (!new_plan_sp) {
324  // For efficiencies sake, we know we're done here so we don't have to do
325  // this calculation again in MischiefManaged.
327  return true;
328  } else
329  return false;
330 }
331 
333  // For crashes, breakpoint hits, signals, etc, let the base plan (or some
334  // plan above us) handle the stop. That way the user can see the stop, step
335  // around, and then when they are done, continue and have their step
336  // complete. The exception is if we've hit our "run to next branch"
337  // breakpoint. Note, unlike the step in range plan, we don't mark ourselves
338  // complete if we hit an unexplained breakpoint/crash.
339 
341  StopInfoSP stop_info_sp = GetPrivateStopInfo();
342  bool return_value;
343 
344  if (stop_info_sp) {
345  StopReason reason = stop_info_sp->GetStopReason();
346 
347  if (reason == eStopReasonTrace) {
348  return_value = true;
349  } else if (reason == eStopReasonBreakpoint) {
350  return_value = NextRangeBreakpointExplainsStop(stop_info_sp);
351  } else {
352  if (log)
353  log->PutCString("ThreadPlanStepInRange got asked if it explains the "
354  "stop for some reason other than step.");
355  return_value = false;
356  }
357  } else
358  return_value = true;
359 
360  return return_value;
361 }
362 
364  bool current_plan) {
365  if (resume_state != eStateSuspended && m_first_resume) {
366  m_first_resume = false;
367  if (resume_state == eStateStepping && current_plan) {
368  // See if we are about to step over an inlined call in the middle of the
369  // inlined stack, if so figure out its extents and reset our range to
370  // step over that.
371  bool in_inlined_stack = m_thread.DecrementCurrentInlinedDepth();
372  if (in_inlined_stack) {
374  if (log)
375  log->Printf("ThreadPlanStepInRange::DoWillResume: adjusting range to "
376  "the frame at inlined depth %d.",
378  StackFrameSP stack_sp = m_thread.GetStackFrameAtIndex(0);
379  if (stack_sp) {
380  Block *frame_block = stack_sp->GetFrameBlock();
381  lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
382  AddressRange my_range;
383  if (frame_block->GetRangeContainingLoadAddress(
384  curr_pc, m_thread.GetProcess()->GetTarget(), my_range)) {
385  m_address_ranges.clear();
386  m_address_ranges.push_back(my_range);
387  if (log) {
388  StreamString s;
389  const InlineFunctionInfo *inline_info =
390  frame_block->GetInlinedFunctionInfo();
391  const char *name;
392  if (inline_info)
393  name =
394  inline_info
395  ->GetName(frame_block->CalculateSymbolContextFunction()
396  ->GetLanguage())
397  .AsCString();
398  else
399  name = "<unknown-notinlined>";
400 
401  s.Printf(
402  "Stepping over inlined function \"%s\" in inlined stack: ",
403  name);
404  DumpRanges(&s);
405  log->PutString(s.GetString());
406  }
407  }
408  }
409  }
410  }
411  }
412 
413  return true;
414 }
void Address(uint64_t addr, uint32_t addr_size, const char *prefix=nullptr, const char *suffix=nullptr)
Output an address value to this stream.
Definition: Stream.cpp:79
A line table entry class.
Definition: LineEntry.h:20
lldb::StopInfoSP GetPrivateStopInfo()
Definition: ThreadPlan.h:564
Function * CalculateSymbolContextFunction() const
Definition: Address.cpp:822
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
virtual lldb::ThreadPlanSP QueueThreadPlanForStepOutNoShouldStop(bool abort_other_plans, SymbolContext *addr_context, bool first_insn, bool stop_other_threads, Vote stop_vote, Vote run_vote, uint32_t frame_idx, Status &status, bool continue_to_next_branch=false)
Queue the plan used to step out of the function at the current PC of a thread.
Definition: Thread.cpp:1454
uint32_t CalculateSymbolContext(SymbolContext *sc, lldb::SymbolContextItem resolve_scope=lldb::eSymbolContextEverything) const
Reconstruct a symbol context from an address.
Definition: Address.cpp:782
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
FileSpec original_file
The original source file, from debug info.
Definition: LineEntry.h:153
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:292
bool IsValid() const
Check if a line entry object is valid.
Definition: LineEntry.cpp:47
bool ShouldStop(Event *event_ptr) override
std::vector< AddressRange > m_address_ranges
bool GetLineEntryAtIndex(uint32_t idx, LineEntry &line_entry)
Get the line entry from the line table at index idx.
Definition: LineTable.cpp:161
void PutString(llvm::StringRef str)
Definition: Log.cpp:110
bool ContainsFileAddress(const Address &so_addr) const
Check if a section offset address is contained in this range.
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
A class that describes a function.
Definition: Function.h:323
CompileUnit * comp_unit
The CompileUnit for a given query.
const char * GetData() const
Definition: StreamString.h:43
virtual lldb::ThreadPlanSP QueueThreadPlanForStepThrough(StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads, Status &status)
Gets the plan used to step through the code that steps from a function call site at the current PC in...
Definition: Thread.cpp:1472
A class that describes a single lexical block.
Definition: Block.h:41
bool GetRangeContainingAddress(const Address &addr, AddressRange &range)
Definition: Block.cpp:240
lldb::LanguageType GetLanguage() const
Definition: Function.cpp:591
Symbol * symbol
The Symbol for a given query.
lldb::FrameComparison CompareCurrentFrameToStartFrame()
bool DumpStopContext(Stream *s, bool show_fullpaths) const
Dumps information specific to a process that stops at this line entry to the supplied stream s...
Definition: LineEntry.cpp:51
Function * function
The Function for a given query.
ThreadPlanStepOverRange(Thread &thread, const AddressRange &range, const SymbolContext &addr_context, lldb::RunMode stop_others, LazyBool step_out_avoids_no_debug)
bool GetRangeContainingLoadAddress(lldb::addr_t load_addr, Target &target, AddressRange &range)
Definition: Block.cpp:268
ValueType Clear(ValueType mask=~(ValueType) 0)
Clear one or more flags.
Definition: Flags.h:72
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:57
ConstString GetName(lldb::LanguageType language) const
Definition: Function.cpp:104
lldb::TargetSP CalculateTarget() override
Definition: Thread.cpp:1584
Block * GetContainingInlinedBlock()
Get the inlined block that contains this block.
Definition: Block.cpp:198
A line table class.
Definition: LineTable.h:37
llvm::StringRef GetString() const
virtual lldb::RegisterContextSP GetRegisterContext()=0
bool DoPlanExplainsStop(Event *event_ptr) override
uint32_t GetCurrentInlinedDepth()
Definition: Thread.h:406
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
bool Success() const
Test for success condition.
Definition: Status.cpp:287
Process or thread is in the process of stepping and can not be examined.
A section + offset based address class.
Definition: Address.h:80
ValueType Set(ValueType mask)
Set one or more flags by logical OR&#39;ing mask with the current flags.
Definition: Flags.h:84
LineTable * GetLineTable()
Get the line table for the compile unit.
void SetPlanComplete(bool success=true)
Definition: ThreadPlan.cpp:51
lldb::ProcessSP GetProcess() const
Definition: Thread.h:154
const InlineFunctionInfo * GetInlinedFunctionInfo() const
Get const accessor for any inlined function information.
Definition: Block.h:296
Process or thread is in a suspended state as far as the debugger is concerned while other processes o...
bool FindLineEntryByAddress(const Address &so_addr, LineEntry &line_entry, uint32_t *index_ptr=nullptr)
Find a line entry that contains the section offset address so_addr.
Definition: LineTable.cpp:170
size_t PutChar(char ch)
Definition: Stream.cpp:103
A class that describes information for an inlined function.
Definition: Function.h:122
uint64_t addr_t
Definition: lldb-types.h:83
virtual lldb::ThreadPlanSP QueueThreadPlanForStepOverRange(bool abort_other_plans, const AddressRange &range, const SymbolContext &addr_context, lldb::RunMode stop_other_threads, Status &status, LazyBool step_out_avoids_code_without_debug_info=eLazyBoolCalculate)
Queues the plan used to step through an address range, stepping over function calls.
Definition: Thread.cpp:1379
bool NextRangeBreakpointExplainsStop(lldb::StopInfoSP stop_info_sp)
bool DoWillResume(lldb::StateType resume_state, bool current_plan) override
void PutCString(const char *cstr)
Definition: Log.cpp:109
LineEntry line_entry
The LineEntry for a given query.
Definition: SBAddress.h:15
lldb::ThreadPlanSP CheckShouldStopHereAndQueueStepOut(lldb::FrameComparison operation, Status &status)
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:220
Function * CalculateSymbolContextFunction() override
Definition: Block.cpp:144
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:130
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition: Thread.h:395
bool GetStepOutAvoidsNoDebug() const
Definition: Thread.cpp:162
#define LIBLLDB_LOG_STEP
Definition: Logging.h:21
Block * block
The Block for a given query.
void Printf(const char *format,...) __attribute__((format(printf
Definition: Log.cpp:113
A section + offset based address range class.
Definition: AddressRange.h:32
AddressRange range
The section offset address range for this line entry.
Definition: LineEntry.h:150
bool DecrementCurrentInlinedDepth()
Definition: Thread.h:402