1 //===-- ThreadPlanStepOverRange.cpp -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/ThreadPlanStepOverRange.h"
14 // Other libraries and framework includes
16 #include "lldb/Core/Log.h"
17 #include "lldb/Core/Stream.h"
18 #include "lldb/Symbol/Block.h"
19 #include "lldb/Symbol/CompileUnit.h"
20 #include "lldb/Symbol/Function.h"
21 #include "lldb/Symbol/LineTable.h"
22 #include "lldb/Target/Process.h"
23 #include "lldb/Target/RegisterContext.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Target/Thread.h"
26 #include "lldb/Target/ThreadPlanStepOut.h"
27 #include "lldb/Target/ThreadPlanStepThrough.h"
29 using namespace lldb_private;
32 uint32_t ThreadPlanStepOverRange::s_default_flag_values = 0;
34 //----------------------------------------------------------------------
35 // ThreadPlanStepOverRange: Step through a stack range, either stepping over or into
36 // based on the value of \a type.
37 //----------------------------------------------------------------------
39 ThreadPlanStepOverRange::ThreadPlanStepOverRange
42 const AddressRange &range,
43 const SymbolContext &addr_context,
44 lldb::RunMode stop_others,
45 LazyBool step_out_avoids_code_without_debug_info
47 ThreadPlanStepRange (ThreadPlan::eKindStepOverRange, "Step range stepping over", thread, range, addr_context, stop_others),
48 ThreadPlanShouldStopHere (this),
52 SetupAvoidNoDebug(step_out_avoids_code_without_debug_info);
55 ThreadPlanStepOverRange::~ThreadPlanStepOverRange ()
60 ThreadPlanStepOverRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
62 if (level == lldb::eDescriptionLevelBrief)
64 s->Printf("step over");
67 s->Printf ("Stepping over");
68 bool printed_line_info = false;
69 if (m_addr_context.line_entry.IsValid())
72 m_addr_context.line_entry.DumpStopContext (s, false);
73 printed_line_info = true;
76 if (!printed_line_info || level == eDescriptionLevelVerbose)
78 s->Printf (" using ranges: ");
86 ThreadPlanStepOverRange::SetupAvoidNoDebug(LazyBool step_out_avoids_code_without_debug_info)
88 bool avoid_nodebug = true;
89 switch (step_out_avoids_code_without_debug_info)
95 avoid_nodebug = false;
97 case eLazyBoolCalculate:
98 avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
102 GetFlags().Set (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
104 GetFlags().Clear (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
105 // Step Over plans should always avoid no-debug on step in. Seems like you shouldn't
106 // have to say this, but a tail call looks more like a step in that a step out, so
107 // we want to catch this case.
108 GetFlags().Set (ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
112 ThreadPlanStepOverRange::IsEquivalentContext(const SymbolContext &context)
115 // Match as much as is specified in the m_addr_context:
116 // This is a fairly loose sanity check. Note, sometimes the target doesn't get filled
117 // in so I left out the target check. And sometimes the module comes in as the .o file from the
118 // inlined range, so I left that out too...
119 if (m_addr_context.comp_unit)
121 if (m_addr_context.comp_unit == context.comp_unit)
123 if (m_addr_context.function && m_addr_context.function == context.function)
125 if (m_addr_context.block && m_addr_context.block == context.block)
130 else if (m_addr_context.symbol && m_addr_context.symbol == context.symbol)
138 ThreadPlanStepOverRange::ShouldStop (Event *event_ptr)
140 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
145 s.Address (m_thread.GetRegisterContext()->GetPC(),
146 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
147 log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
150 // If we're out of the range but in the same frame or in our caller's frame
151 // then we should stop.
152 // When stepping out we only stop others if we are forcing running one thread.
154 if (m_stop_others == lldb::eOnlyThisThread)
159 ThreadPlanSP new_plan_sp;
161 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
163 if (frame_order == eFrameCompareOlder)
165 // If we're in an older frame then we should stop.
167 // A caveat to this is if we think the frame is older but we're actually in a trampoline.
168 // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are
169 // in a trampoline we think the frame is older because the trampoline confused the backtracer.
170 // As below, we step through first, and then try to figure out how to get back out again.
172 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
174 if (new_plan_sp && log)
175 log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
177 else if (frame_order == eFrameCompareYounger)
179 // Make sure we really are in a new frame. Do that by unwinding and seeing if the
180 // start function really is our start function...
181 for(uint32_t i = 1;; ++i)
183 StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(i);
184 if (!older_frame_sp) {
185 // We can't unwind the next frame we should just get out of here & stop...
189 const SymbolContext &older_context = older_frame_sp->GetSymbolContext(eSymbolContextEverything);
190 if (IsEquivalentContext(older_context))
192 new_plan_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop (false,
203 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
204 // If we found a way through, then we should stop recursing.
212 // If we're still in the range, keep going.
215 SetNextBranchBreakpoint();
222 // This one is a little tricky. Sometimes we may be in a stub or something similar,
223 // in which case we need to get out of there. But if we are in a stub then it's
224 // likely going to be hard to get out from here. It is probably easiest to step into the
225 // stub, and then it will be straight-forward to step out.
226 new_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
230 // The current clang (at least through 424) doesn't always get the address range for the
231 // DW_TAG_inlined_subroutines right, so that when you leave the inlined range the line table says
232 // you are still in the source file of the inlining function. This is bad, because now you are missing
233 // the stack frame for the function containing the inlining, and if you sensibly do "finish" to get
234 // out of this function you will instead exit the containing function.
235 // To work around this, we check whether we are still in the source file we started in, and if not assume
236 // it is an error, and push a plan to get us out of this line and back to the containing file.
238 if (m_addr_context.line_entry.IsValid())
241 StackFrameSP frame_sp = m_thread.GetStackFrameAtIndex(0);
242 sc = frame_sp->GetSymbolContext (eSymbolContextEverything);
243 if (sc.line_entry.IsValid())
245 if (sc.line_entry.file != m_addr_context.line_entry.file
246 && sc.comp_unit == m_addr_context.comp_unit
247 && sc.function == m_addr_context.function)
249 // Okay, find the next occurrence of this file in the line table:
250 LineTable *line_table = m_addr_context.comp_unit->GetLineTable();
253 Address cur_address = frame_sp->GetFrameCodeAddress();
255 LineEntry line_entry;
256 if (line_table->FindLineEntryByAddress (cur_address, line_entry, &entry_idx))
258 LineEntry next_line_entry;
259 bool step_past_remaining_inline = false;
262 // We require the previous line entry and the current line entry come
263 // from the same file.
264 // The other requirement is that the previous line table entry be part of an
265 // inlined block, we don't want to step past cases where people have inlined
266 // some code fragment by using #include <source-fragment.c> directly.
267 LineEntry prev_line_entry;
268 if (line_table->GetLineEntryAtIndex(entry_idx - 1, prev_line_entry)
269 && prev_line_entry.file == line_entry.file)
271 SymbolContext prev_sc;
272 Address prev_address = prev_line_entry.range.GetBaseAddress();
273 prev_address.CalculateSymbolContext(&prev_sc);
276 Block *inlined_block = prev_sc.block->GetContainingInlinedBlock();
279 AddressRange inline_range;
280 inlined_block->GetRangeContainingAddress(prev_address, inline_range);
281 if (!inline_range.ContainsFileAddress(cur_address))
284 step_past_remaining_inline = true;
292 if (step_past_remaining_inline)
294 uint32_t look_ahead_step = 1;
295 while (line_table->GetLineEntryAtIndex(entry_idx + look_ahead_step, next_line_entry))
297 // Make sure we haven't wandered out of the function we started from...
298 Address next_line_address = next_line_entry.range.GetBaseAddress();
299 Function *next_line_function = next_line_address.CalculateSymbolContextFunction();
300 if (next_line_function != m_addr_context.function)
303 if (next_line_entry.file == m_addr_context.line_entry.file)
305 const bool abort_other_plans = false;
306 const bool stop_other_threads = false;
307 new_plan_sp = m_thread.QueueThreadPlanForRunToAddress(abort_other_plans,
323 // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
324 ClearNextBranchBreakpoint();
327 // If we haven't figured out something to do yet, then ask the ShouldStopHere callback:
330 new_plan_sp = CheckShouldStopHereAndQueueStepOut (frame_order);
334 m_no_more_plans = true;
337 // Any new plan will be an implementation plan, so mark it private:
338 new_plan_sp->SetPrivate(true);
339 m_no_more_plans = false;
344 // For efficiencies sake, we know we're done here so we don't have to do this
345 // calculation again in MischiefManaged.
354 ThreadPlanStepOverRange::DoPlanExplainsStop (Event *event_ptr)
356 // For crashes, breakpoint hits, signals, etc, let the base plan (or some plan above us)
357 // handle the stop. That way the user can see the stop, step around, and then when they
358 // are done, continue and have their step complete. The exception is if we've hit our
359 // "run to next branch" breakpoint.
360 // Note, unlike the step in range plan, we don't mark ourselves complete if we hit an
361 // unexplained breakpoint/crash.
363 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
364 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
369 StopReason reason = stop_info_sp->GetStopReason();
373 case eStopReasonTrace:
376 case eStopReasonBreakpoint:
377 if (NextRangeBreakpointExplainsStop(stop_info_sp))
380 return_value = false;
382 case eStopReasonWatchpoint:
383 case eStopReasonSignal:
384 case eStopReasonException:
385 case eStopReasonExec:
386 case eStopReasonThreadExiting:
389 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
390 return_value = false;
401 ThreadPlanStepOverRange::DoWillResume (lldb::StateType resume_state, bool current_plan)
403 if (resume_state != eStateSuspended && m_first_resume)
405 m_first_resume = false;
406 if (resume_state == eStateStepping && current_plan)
408 // See if we are about to step over an inlined call in the middle of the inlined stack, if so figure
409 // out its extents and reset our range to step over that.
410 bool in_inlined_stack = m_thread.DecrementCurrentInlinedDepth();
411 if (in_inlined_stack)
413 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
415 log->Printf ("ThreadPlanStepInRange::DoWillResume: adjusting range to the frame at inlined depth %d.",
416 m_thread.GetCurrentInlinedDepth());
417 StackFrameSP stack_sp = m_thread.GetStackFrameAtIndex(0);
420 Block *frame_block = stack_sp->GetFrameBlock();
421 lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
422 AddressRange my_range;
423 if (frame_block->GetRangeContainingLoadAddress(curr_pc, m_thread.GetProcess()->GetTarget(), my_range))
425 m_address_ranges.clear();
426 m_address_ranges.push_back(my_range);
430 const InlineFunctionInfo *inline_info = frame_block->GetInlinedFunctionInfo();
433 name = inline_info->GetName(frame_block->CalculateSymbolContextFunction()->GetLanguage()).AsCString();
435 name = "<unknown-notinlined>";
437 s.Printf ("Stepping over inlined function \"%s\" in inlined stack: ", name);
439 log->PutCString(s.GetData());