1 //===-- ThreadPlanStepOverRange.cpp ---------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Target/ThreadPlanStepOverRange.h"
10 #include "lldb/Symbol/Block.h"
11 #include "lldb/Symbol/CompileUnit.h"
12 #include "lldb/Symbol/Function.h"
13 #include "lldb/Symbol/LineTable.h"
14 #include "lldb/Target/Language.h"
15 #include "lldb/Target/Process.h"
16 #include "lldb/Target/RegisterContext.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Target/Thread.h"
19 #include "lldb/Target/ThreadPlanSingleThreadTimeout.h"
20 #include "lldb/Target/ThreadPlanStepOut.h"
21 #include "lldb/Target/ThreadPlanStepThrough.h"
22 #include "lldb/Utility/LLDBLog.h"
23 #include "lldb/Utility/Log.h"
24 #include "lldb/Utility/Stream.h"
26 using namespace lldb_private
;
29 uint32_t ThreadPlanStepOverRange::s_default_flag_values
= 0;
31 // ThreadPlanStepOverRange: Step through a stack range, either stepping over or
32 // into based on the value of \a type.
34 ThreadPlanStepOverRange::ThreadPlanStepOverRange(
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
),
41 ThreadPlanShouldStopHere(this), TimeoutResumeAll(thread
),
42 m_first_resume(true), m_run_mode(stop_others
) {
44 SetupAvoidNoDebug(step_out_avoids_code_without_debug_info
);
47 ThreadPlanStepOverRange::~ThreadPlanStepOverRange() = default;
49 void ThreadPlanStepOverRange::GetDescription(Stream
*s
,
50 lldb::DescriptionLevel level
) {
51 auto PrintFailureIfAny
= [&]() {
52 if (m_status
.Success())
54 s
->Printf(" failed (%s)", m_status
.AsCString());
57 if (level
== lldb::eDescriptionLevelBrief
) {
58 s
->Printf("step over");
63 s
->Printf("Stepping over");
64 bool printed_line_info
= false;
65 if (m_addr_context
.line_entry
.IsValid()) {
67 m_addr_context
.line_entry
.DumpStopContext(s
, false);
68 printed_line_info
= true;
71 if (!printed_line_info
|| level
== eDescriptionLevelVerbose
) {
72 s
->Printf(" using ranges: ");
81 void ThreadPlanStepOverRange::SetupAvoidNoDebug(
82 LazyBool step_out_avoids_code_without_debug_info
) {
83 bool avoid_nodebug
= true;
84 switch (step_out_avoids_code_without_debug_info
) {
89 avoid_nodebug
= false;
91 case eLazyBoolCalculate
:
92 avoid_nodebug
= GetThread().GetStepOutAvoidsNoDebug();
96 GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug
);
98 GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug
);
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.
102 GetFlags().Set(ThreadPlanShouldStopHere::eStepInAvoidNoDebug
);
105 bool ThreadPlanStepOverRange::IsEquivalentContext(
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...
115 if (m_addr_context
.comp_unit
) {
116 if (m_addr_context
.comp_unit
!= context
.comp_unit
)
118 if (m_addr_context
.function
) {
119 if (m_addr_context
.function
!= context
.function
)
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
124 if (m_addr_context
.block
->GetInlinedFunctionInfo() == nullptr &&
125 context
.block
->GetInlinedFunctionInfo() == nullptr)
127 return m_addr_context
.block
== context
.block
;
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
;
134 void ThreadPlanStepOverRange::SetStopOthers(bool stop_others
) {
136 m_stop_others
= RunMode::eAllThreads
;
139 bool ThreadPlanStepOverRange::ShouldStop(Event
*event_ptr
) {
140 Log
*log
= GetLog(LLDBLog::Step
);
141 Thread
&thread
= GetThread();
145 DumpAddress(s
.AsRawOstream(), thread
.GetRegisterContext()->GetPC(),
146 GetTarget().GetArchitecture().GetAddressByteSize());
147 LLDB_LOGF(log
, "ThreadPlanStepOverRange reached %s.", s
.GetData());
149 ClearNextBranchBreakpointExplainedStop();
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
;
156 FrameComparison frame_order
= CompareCurrentFrameToStartFrame();
157 LLDB_LOGF(log
, "ThreadPlanStepOverRange compare frame result: %d.",
160 if (frame_order
== eFrameCompareOlder
) {
161 // If we're in an older frame then we should stop.
163 // A caveat to this is if we think the frame is older but we're actually in
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.
170 new_plan_sp
= thread
.QueueThreadPlanForStepThrough(m_stack_id
, false,
171 stop_others
, m_status
);
173 if (new_plan_sp
&& 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 &
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.
192 if (m_next_branch_bp_sp
)
194 new_plan_sp
= thread
.QueueThreadPlanForStepOutNoShouldStop(
195 false, nullptr, true, stop_others
, eVoteNo
, eVoteNoOpinion
, 0,
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.
207 // If we're still in the range, keep going.
209 SetNextBranchBreakpoint();
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
);
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.
233 if (m_addr_context
.line_entry
.IsValid()) {
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(
239 *m_addr_context
.line_entry
.original_file_sp
,
240 SupportFile::eEqualFileSpecAndChecksumIfSet
) &&
241 sc
.comp_unit
== m_addr_context
.comp_unit
&&
242 sc
.function
== m_addr_context
.function
) {
243 // Okay, find the next occurrence of this file in the line table:
244 LineTable
*line_table
= m_addr_context
.comp_unit
->GetLineTable();
246 Address cur_address
= frame_sp
->GetFrameCodeAddress();
248 LineEntry line_entry
;
249 if (line_table
->FindLineEntryByAddress(cur_address
, line_entry
,
251 LineEntry next_line_entry
;
252 bool step_past_remaining_inline
= false;
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,
263 prev_line_entry
.original_file_sp
->Equal(
264 *line_entry
.original_file_sp
,
265 SupportFile::eEqualFileSpecAndChecksumIfSet
)) {
266 SymbolContext prev_sc
;
267 Address prev_address
=
268 prev_line_entry
.range
.GetBaseAddress();
269 prev_address
.CalculateSymbolContext(&prev_sc
);
271 Block
*inlined_block
=
272 prev_sc
.block
->GetContainingInlinedBlock();
274 AddressRange inline_range
;
275 inlined_block
->GetRangeContainingAddress(prev_address
,
277 if (!inline_range
.ContainsFileAddress(cur_address
)) {
279 step_past_remaining_inline
= true;
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
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
)
299 if (next_line_entry
.original_file_sp
->Equal(
300 *m_addr_context
.line_entry
.original_file_sp
,
301 SupportFile::eEqualFileSpecAndChecksumIfSet
)) {
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()
307 AddressRange
step_range(
309 next_line_address
.GetLoadAddress(&GetTarget()) -
312 new_plan_sp
= thread
.QueueThreadPlanForStepOverRange(
313 abort_other_plans
, step_range
, sc
, stop_other_threads
,
328 // If we get to this point, we're not going to use a previously set "next
329 // branch" breakpoint, so delete it:
330 ClearNextBranchBreakpoint();
332 // If we haven't figured out something to do yet, then ask the ShouldStopHere
335 new_plan_sp
= CheckShouldStopHereAndQueueStepOut(frame_order
, m_status
);
339 m_no_more_plans
= true;
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;
347 // For efficiencies sake, we know we're done here so we don't have to do
348 // this calculation again in MischiefManaged.
349 SetPlanComplete(m_status
.Success());
355 void ThreadPlanStepOverRange::DidPush() {
356 ThreadPlanStepRange::DidPush();
357 if (m_run_mode
== lldb::eOnlyThisThread
&& IsControllingPlan())
361 bool ThreadPlanStepOverRange::DoPlanExplainsStop(Event
*event_ptr
) {
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.
369 Log
*log
= GetLog(LLDBLog::Step
);
370 StopInfoSP stop_info_sp
= GetPrivateStopInfo();
374 StopReason reason
= stop_info_sp
->GetStopReason();
376 if (reason
== eStopReasonTrace
) {
378 } else if (reason
== eStopReasonBreakpoint
) {
379 return_value
= NextRangeBreakpointExplainsStop(stop_info_sp
);
382 log
->PutCString("ThreadPlanStepOverRange got asked if it explains the "
383 "stop for some reason other than step.");
384 return_value
= false;
392 bool ThreadPlanStepOverRange::DoWillResume(lldb::StateType resume_state
,
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
401 bool in_inlined_stack
= thread
.DecrementCurrentInlinedDepth();
402 if (in_inlined_stack
) {
403 Log
*log
= GetLog(LLDBLog::Step
);
405 "ThreadPlanStepOverRange::DoWillResume: adjusting range to "
406 "the frame at inlined depth %d.",
407 thread
.GetCurrentInlinedDepth());
408 StackFrameSP stack_sp
= thread
.GetStackFrameAtIndex(0);
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
);
419 const InlineFunctionInfo
*inline_info
=
420 frame_block
->GetInlinedFunctionInfo();
423 name
= inline_info
->GetName().AsCString();
425 name
= "<unknown-notinlined>";
428 "Stepping over inlined function \"%s\" in inlined stack: ",
431 log
->PutString(s
.GetString());
438 if (m_run_mode
== lldb::eOnlyThisThread
&& IsControllingPlan())