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"
22#include "lldb/Utility/Log.h"
23#include "lldb/Utility/Stream.h"
24
25using namespace lldb_private;
26using namespace lldb;
27
29
30// ThreadPlanStepOverRange: Step through a stack range, either stepping over or
31// into based on the value of \a type.
32
34 Thread &thread, const AddressRange &range,
35 const SymbolContext &addr_context, lldb::RunMode stop_others,
36 LazyBool step_out_avoids_code_without_debug_info)
37 : ThreadPlanStepRange(ThreadPlan::eKindStepOverRange,
38 "Step range stepping over", thread, range,
39 addr_context, stop_others),
41 m_first_resume(true), m_run_mode(stop_others) {
43 SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
44}
45
47
50 auto PrintFailureIfAny = [&]() {
51 if (m_status.Success())
52 return;
53 s->Printf(" failed (%s)", m_status.AsCString());
54 };
55
56 if (level == lldb::eDescriptionLevelBrief) {
57 s->Printf("step over");
58 PrintFailureIfAny();
59 return;
60 }
61
62 s->Printf("Stepping over");
63 bool printed_line_info = false;
65 s->Printf(" line ");
67 printed_line_info = true;
68 }
69
70 if (!printed_line_info || level == eDescriptionLevelVerbose) {
71 s->Printf(" using ranges: ");
72 DumpRanges(s);
73 }
74
75 PrintFailureIfAny();
76
77 s->PutChar('.');
78}
79
81 LazyBool step_out_avoids_code_without_debug_info) {
82 bool avoid_nodebug = true;
83 switch (step_out_avoids_code_without_debug_info) {
84 case eLazyBoolYes:
85 avoid_nodebug = true;
86 break;
87 case eLazyBoolNo:
88 avoid_nodebug = false;
89 break;
91 avoid_nodebug = GetThread().GetStepOutAvoidsNoDebug();
92 break;
93 }
94 if (avoid_nodebug)
96 else
98 // Step Over plans should always avoid no-debug on step in. Seems like you
99 // shouldn't have to say this, but a tail call looks more like a step in that
100 // a step out, so we want to catch this case.
102}
103
105 const SymbolContext &context) {
106 // Match as much as is specified in the m_addr_context: This is a fairly
107 // loose sanity check. Note, sometimes the target doesn't get filled in so I
108 // left out the target check. And sometimes the module comes in as the .o
109 // file from the inlined range, so I left that out too...
111 if (m_addr_context.comp_unit != context.comp_unit)
112 return false;
114 if (m_addr_context.function != context.function)
115 return false;
116 // It is okay to return to a different block of a straight function, we
117 // only have to be more careful if returning from one inlined block to
118 // another.
119 if (m_addr_context.block->GetInlinedFunctionInfo() == nullptr &&
120 context.block->GetInlinedFunctionInfo() == nullptr)
121 return true;
122 return m_addr_context.block == context.block;
123 }
124 }
125 // Fall back to symbol if we have no decision from comp_unit/function/block.
126 return m_addr_context.symbol && m_addr_context.symbol == context.symbol;
127}
128
130 if (!stop_others)
131 m_stop_others = RunMode::eAllThreads;
132}
133
135 Log *log = GetLog(LLDBLog::Step);
136 Thread &thread = GetThread();
137
138 if (log) {
139 StreamString s;
140 DumpAddress(s.AsRawOstream(), thread.GetRegisterContext()->GetPC(),
141 GetTarget().GetArchitecture().GetAddressByteSize());
142 LLDB_LOGF(log, "ThreadPlanStepOverRange reached %s.", s.GetData());
143 }
145
146 // If we're out of the range but in the same frame or in our caller's frame
147 // then we should stop. When stepping out we only stop others if we are
148 // forcing running one thread.
149 bool stop_others = (m_stop_others == lldb::eOnlyThisThread);
150 ThreadPlanSP new_plan_sp;
152 LLDB_LOGF(log, "ThreadPlanStepOverRange compare frame result: %d.",
153 frame_order);
154
155 if (frame_order == eFrameCompareOlder) {
156 // If we're in an older frame then we should stop.
157 //
158 // A caveat to this is if we think the frame is older but we're actually in
159 // a trampoline.
160 // I'm going to make the assumption that you wouldn't RETURN to a
161 // trampoline. So if we are in a trampoline we think the frame is older
162 // because the trampoline confused the backtracer. As below, we step
163 // through first, and then try to figure out how to get back out again.
164
165 new_plan_sp = thread.QueueThreadPlanForStepThrough(m_stack_id, false,
166 stop_others, m_status);
167
168 if (new_plan_sp && log)
169 LLDB_LOGF(log,
170 "Thought I stepped out, but in fact arrived at a trampoline.");
171 } else if (frame_order == eFrameCompareYounger) {
172 // Make sure we really are in a new frame. Do that by unwinding and seeing
173 // if the start function really is our start function...
174 for (uint32_t i = 1;; ++i) {
175 StackFrameSP older_frame_sp = thread.GetStackFrameAtIndex(i);
176 if (!older_frame_sp) {
177 // We can't unwind the next frame we should just get out of here &
178 // stop...
179 break;
180 }
181
182 const SymbolContext &older_context =
183 older_frame_sp->GetSymbolContext(eSymbolContextEverything);
184 if (IsEquivalentContext(older_context)) {
185 // If we have the next-branch-breakpoint in the range, we can just
186 // rely on that breakpoint to trigger once we return to the range.
188 return false;
189 new_plan_sp = thread.QueueThreadPlanForStepOutNoShouldStop(
190 false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0,
191 m_status, true);
192 break;
193 } else {
194 new_plan_sp = thread.QueueThreadPlanForStepThrough(
195 m_stack_id, false, stop_others, m_status);
196 // If we found a way through, then we should stop recursing.
197 if (new_plan_sp)
198 break;
199 }
200 }
201 } else {
202 // If we're still in the range, keep going.
203 if (InRange()) {
205 return false;
206 }
207
208 if (!InSymbol()) {
209 // This one is a little tricky. Sometimes we may be in a stub or
210 // something similar, in which case we need to get out of there. But if
211 // we are in a stub then it's likely going to be hard to get out from
212 // here. It is probably easiest to step into the stub, and then it will
213 // be straight-forward to step out.
214 new_plan_sp = thread.QueueThreadPlanForStepThrough(m_stack_id, false,
215 stop_others, m_status);
216 } else {
217 // The current clang (at least through 424) doesn't always get the
218 // address range for the DW_TAG_inlined_subroutines right, so that when
219 // you leave the inlined range the line table says you are still in the
220 // source file of the inlining function. This is bad, because now you
221 // are missing the stack frame for the function containing the inlining,
222 // and if you sensibly do "finish" to get out of this function you will
223 // instead exit the containing function. To work around this, we check
224 // whether we are still in the source file we started in, and if not
225 // assume it is an error, and push a plan to get us out of this line and
226 // back to the containing file.
227
229 SymbolContext sc;
230 StackFrameSP frame_sp = thread.GetStackFrameAtIndex(0);
231 sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
232 if (sc.line_entry.IsValid()) {
233 if (!sc.line_entry.original_file_sp->Equal(
238 // Okay, find the next occurrence of this file in the line table:
240 if (line_table) {
241 Address cur_address = frame_sp->GetFrameCodeAddress();
242 uint32_t entry_idx;
243 LineEntry line_entry;
244 if (line_table->FindLineEntryByAddress(cur_address, line_entry,
245 &entry_idx)) {
246 LineEntry next_line_entry;
247 bool step_past_remaining_inline = false;
248 if (entry_idx > 0) {
249 // We require the previous line entry and the current line
250 // entry come from the same file. The other requirement is
251 // that the previous line table entry be part of an inlined
252 // block, we don't want to step past cases where people have
253 // inlined some code fragment by using #include <source-
254 // fragment.c> directly.
255 LineEntry prev_line_entry;
256 if (line_table->GetLineEntryAtIndex(entry_idx - 1,
257 prev_line_entry) &&
258 prev_line_entry.original_file_sp->Equal(
259 *line_entry.original_file_sp,
261 SymbolContext prev_sc;
262 Address prev_address =
263 prev_line_entry.range.GetBaseAddress();
264 prev_address.CalculateSymbolContext(&prev_sc);
265 if (prev_sc.block) {
266 Block *inlined_block =
268 if (inlined_block) {
269 AddressRange inline_range;
270 inlined_block->GetRangeContainingAddress(prev_address,
271 inline_range);
272 if (!inline_range.ContainsFileAddress(cur_address)) {
273
274 step_past_remaining_inline = true;
275 }
276 }
277 }
278 }
279 }
280
281 if (step_past_remaining_inline) {
282 uint32_t look_ahead_step = 1;
283 while (line_table->GetLineEntryAtIndex(
284 entry_idx + look_ahead_step, next_line_entry)) {
285 // Make sure we haven't wandered out of the function we
286 // started from...
287 Address next_line_address =
288 next_line_entry.range.GetBaseAddress();
289 Function *next_line_function =
290 next_line_address.CalculateSymbolContextFunction();
291 if (next_line_function != m_addr_context.function)
292 break;
293
294 if (next_line_entry.original_file_sp->Equal(
297 const bool abort_other_plans = false;
298 const RunMode stop_other_threads = RunMode::eAllThreads;
299 lldb::addr_t cur_pc = thread.GetStackFrameAtIndex(0)
300 ->GetRegisterContext()
301 ->GetPC();
302 AddressRange step_range(
303 cur_pc,
304 next_line_address.GetLoadAddress(&GetTarget()) -
305 cur_pc);
306
307 new_plan_sp = thread.QueueThreadPlanForStepOverRange(
308 abort_other_plans, step_range, sc, stop_other_threads,
309 m_status);
310 break;
311 }
312 look_ahead_step++;
313 }
314 }
315 }
316 }
317 }
318 }
319 }
320 }
321 }
322
323 // If we get to this point, we're not going to use a previously set "next
324 // branch" breakpoint, so delete it:
326
327 // If we haven't figured out something to do yet, then ask the ShouldStopHere
328 // callback:
329 if (!new_plan_sp) {
330 new_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order, m_status);
331 }
332
333 if (!new_plan_sp)
334 m_no_more_plans = true;
335 else {
336 // Any new plan will be an implementation plan, so mark it private:
337 new_plan_sp->SetPrivate(true);
338 m_no_more_plans = false;
339 }
340
341 if (!new_plan_sp) {
342 // For efficiencies sake, we know we're done here so we don't have to do
343 // this calculation again in MischiefManaged.
345 return true;
346 } else
347 return false;
348}
349
354}
355
357 // For crashes, breakpoint hits, signals, etc, let the base plan (or some
358 // plan above us) handle the stop. That way the user can see the stop, step
359 // around, and then when they are done, continue and have their step
360 // complete. The exception is if we've hit our "run to next branch"
361 // breakpoint. Note, unlike the step in range plan, we don't mark ourselves
362 // complete if we hit an unexplained breakpoint/crash.
363
364 Log *log = GetLog(LLDBLog::Step);
365 StopInfoSP stop_info_sp = GetPrivateStopInfo();
366 bool return_value;
367
368 if (stop_info_sp) {
369 StopReason reason = stop_info_sp->GetStopReason();
370
371 if (reason == eStopReasonTrace) {
372 return_value = true;
373 } else if (reason == eStopReasonBreakpoint) {
374 return_value = NextRangeBreakpointExplainsStop(stop_info_sp);
375 } else {
376 if (log)
377 log->PutCString("ThreadPlanStepOverRange got asked if it explains the "
378 "stop for some reason other than step.");
379 return_value = false;
380 }
381 } else
382 return_value = true;
383
384 return return_value;
385}
386
388 bool current_plan) {
389 if (resume_state != eStateSuspended && m_first_resume) {
390 m_first_resume = false;
391 if (resume_state == eStateStepping && current_plan) {
392 Thread &thread = GetThread();
393 // See if we are about to step over an inlined call in the middle of the
394 // inlined stack, if so figure out its extents and reset our range to
395 // step over that.
396 bool in_inlined_stack = thread.DecrementCurrentInlinedDepth();
397 if (in_inlined_stack) {
398 Log *log = GetLog(LLDBLog::Step);
399 LLDB_LOGF(log,
400 "ThreadPlanStepInRange::DoWillResume: adjusting range to "
401 "the frame at inlined depth %d.",
402 thread.GetCurrentInlinedDepth());
403 StackFrameSP stack_sp = thread.GetStackFrameAtIndex(0);
404 if (stack_sp) {
405 Block *frame_block = stack_sp->GetFrameBlock();
406 lldb::addr_t curr_pc = thread.GetRegisterContext()->GetPC();
407 AddressRange my_range;
408 if (frame_block->GetRangeContainingLoadAddress(
409 curr_pc, m_process.GetTarget(), my_range)) {
410 m_address_ranges.clear();
411 m_address_ranges.push_back(my_range);
412 if (log) {
413 StreamString s;
414 const InlineFunctionInfo *inline_info =
415 frame_block->GetInlinedFunctionInfo();
416 const char *name;
417 if (inline_info)
418 name = inline_info->GetName().AsCString();
419 else
420 name = "<unknown-notinlined>";
421
422 s.Printf(
423 "Stepping over inlined function \"%s\" in inlined stack: ",
424 name);
425 DumpRanges(&s);
426 log->PutString(s.GetString());
427 }
428 }
429 }
430 }
431 }
432 }
435 return true;
436}
#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:1285
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:525
bool GetStepOutAvoidsNoDebug() const
Definition: Thread.cpp:134
bool DecrementCurrentInlinedDepth()
Definition: Thread.h:415
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition: Thread.h:408
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:1353
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:1334
uint32_t GetCurrentInlinedDepth()
Definition: Thread.h:419
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:1264
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:449
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:420
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:427
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