1 //===-- ThreadPlanStepOverRange.cpp -----------------------------*- C++ -*-===//
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/Process.h"
15 #include "lldb/Target/RegisterContext.h"
16 #include "lldb/Target/Target.h"
17 #include "lldb/Target/Thread.h"
18 #include "lldb/Target/ThreadPlanStepOut.h"
19 #include "lldb/Target/ThreadPlanStepThrough.h"
20 #include "lldb/Utility/Log.h"
21 #include "lldb/Utility/Stream.h"
23 using namespace lldb_private;
26 uint32_t ThreadPlanStepOverRange::s_default_flag_values = 0;
28 // ThreadPlanStepOverRange: Step through a stack range, either stepping over or
29 // into based on the value of \a type.
31 ThreadPlanStepOverRange::ThreadPlanStepOverRange(
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);
43 ThreadPlanStepOverRange::~ThreadPlanStepOverRange() = default;
45 void ThreadPlanStepOverRange::GetDescription(Stream *s,
46 lldb::DescriptionLevel level) {
47 auto PrintFailureIfAny = [&]() {
48 if (m_status.Success())
50 s->Printf(" failed (%s)", m_status.AsCString());
53 if (level == lldb::eDescriptionLevelBrief) {
54 s->Printf("step over");
59 s->Printf("Stepping over");
60 bool printed_line_info = false;
61 if (m_addr_context.line_entry.IsValid()) {
63 m_addr_context.line_entry.DumpStopContext(s, false);
64 printed_line_info = true;
67 if (!printed_line_info || level == eDescriptionLevelVerbose) {
68 s->Printf(" using ranges: ");
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) {
85 avoid_nodebug = false;
87 case eLazyBoolCalculate:
88 avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
92 GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
94 GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
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.
98 GetFlags().Set(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
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...
107 if (m_addr_context.comp_unit) {
108 if (m_addr_context.comp_unit != context.comp_unit)
110 if (m_addr_context.function) {
111 if (m_addr_context.function != context.function)
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
116 if (m_addr_context.block->GetInlinedFunctionInfo() == nullptr &&
117 context.block->GetInlinedFunctionInfo() == nullptr)
119 return m_addr_context.block == context.block;
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;
126 bool ThreadPlanStepOverRange::ShouldStop(Event *event_ptr) {
127 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
132 m_thread.GetRegisterContext()->GetPC(),
133 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
134 log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
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;
142 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
144 if (frame_order == eFrameCompareOlder) {
145 // If we're in an older frame then we should stop.
147 // A caveat to this is if we think the frame is older but we're actually in
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.
154 new_plan_sp = m_thread.QueueThreadPlanForStepThrough(m_stack_id, false,
155 stop_others, m_status);
157 if (new_plan_sp && log)
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 &
171 const SymbolContext &older_context =
172 older_frame_sp->GetSymbolContext(eSymbolContextEverything);
173 if (IsEquivalentContext(older_context)) {
174 new_plan_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop(
175 false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0,
179 new_plan_sp = m_thread.QueueThreadPlanForStepThrough(
180 m_stack_id, false, stop_others, m_status);
181 // If we found a way through, then we should stop recursing.
187 // If we're still in the range, keep going.
189 SetNextBranchBreakpoint();
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.
199 new_plan_sp = m_thread.QueueThreadPlanForStepThrough(
200 m_stack_id, false, stop_others, m_status);
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.
213 if (m_addr_context.line_entry.IsValid()) {
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 !=
219 m_addr_context.line_entry.original_file &&
220 sc.comp_unit == m_addr_context.comp_unit &&
221 sc.function == m_addr_context.function) {
222 // Okay, find the next occurrence of this file in the line table:
223 LineTable *line_table = m_addr_context.comp_unit->GetLineTable();
225 Address cur_address = frame_sp->GetFrameCodeAddress();
227 LineEntry line_entry;
228 if (line_table->FindLineEntryByAddress(cur_address, line_entry,
230 LineEntry next_line_entry;
231 bool step_past_remaining_inline = false;
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,
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);
249 Block *inlined_block =
250 prev_sc.block->GetContainingInlinedBlock();
252 AddressRange inline_range;
253 inlined_block->GetRangeContainingAddress(prev_address,
255 if (!inline_range.ContainsFileAddress(cur_address)) {
257 step_past_remaining_inline = true;
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
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)
277 if (next_line_entry.original_file ==
278 m_addr_context.line_entry.original_file) {
279 const bool abort_other_plans = false;
280 const RunMode stop_other_threads = RunMode::eAllThreads;
281 lldb::addr_t cur_pc = m_thread.GetStackFrameAtIndex(0)
282 ->GetRegisterContext()
284 AddressRange step_range(
286 next_line_address.GetLoadAddress(&GetTarget()) -
289 new_plan_sp = m_thread.QueueThreadPlanForStepOverRange(
290 abort_other_plans, step_range, sc, stop_other_threads,
305 // If we get to this point, we're not going to use a previously set "next
306 // branch" breakpoint, so delete it:
307 ClearNextBranchBreakpoint();
309 // If we haven't figured out something to do yet, then ask the ShouldStopHere
312 new_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order, m_status);
316 m_no_more_plans = true;
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;
324 // For efficiencies sake, we know we're done here so we don't have to do
325 // this calculation again in MischiefManaged.
326 SetPlanComplete(m_status.Success());
332 bool ThreadPlanStepOverRange::DoPlanExplainsStop(Event *event_ptr) {
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.
340 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
341 StopInfoSP stop_info_sp = GetPrivateStopInfo();
345 StopReason reason = stop_info_sp->GetStopReason();
347 if (reason == eStopReasonTrace) {
349 } else if (reason == eStopReasonBreakpoint) {
350 return_value = NextRangeBreakpointExplainsStop(stop_info_sp);
353 log->PutCString("ThreadPlanStepInRange got asked if it explains the "
354 "stop for some reason other than step.");
355 return_value = false;
363 bool ThreadPlanStepOverRange::DoWillResume(lldb::StateType resume_state,
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
371 bool in_inlined_stack = m_thread.DecrementCurrentInlinedDepth();
372 if (in_inlined_stack) {
373 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
375 log->Printf("ThreadPlanStepInRange::DoWillResume: adjusting range to "
376 "the frame at inlined depth %d.",
377 m_thread.GetCurrentInlinedDepth());
378 StackFrameSP stack_sp = m_thread.GetStackFrameAtIndex(0);
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);
389 const InlineFunctionInfo *inline_info =
390 frame_block->GetInlinedFunctionInfo();
395 ->GetName(frame_block->CalculateSymbolContextFunction()
399 name = "<unknown-notinlined>";
402 "Stepping over inlined function \"%s\" in inlined stack: ",
405 log->PutString(s.GetString());