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