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