1 //===-- ThreadPlanStepInstruction.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 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Target/ThreadPlanStepInstruction.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Target/Process.h"
18 #include "lldb/Target/RegisterContext.h"
19 #include "lldb/Target/RegisterContext.h"
20 #include "lldb/Target/StopInfo.h"
21 #include "lldb/Target/Target.h"
24 using namespace lldb_private;
26 //----------------------------------------------------------------------
27 // ThreadPlanStepInstruction: Step over the current instruction
28 //----------------------------------------------------------------------
30 ThreadPlanStepInstruction::ThreadPlanStepInstruction
34 bool stop_other_threads,
38 ThreadPlan (ThreadPlan::eKindStepInstruction, "Step over single instruction", thread, stop_vote, run_vote),
39 m_instruction_addr (0),
40 m_stop_other_threads (stop_other_threads),
41 m_step_over (step_over)
43 m_takes_iteration_count = true;
47 ThreadPlanStepInstruction::~ThreadPlanStepInstruction() = default;
50 ThreadPlanStepInstruction::SetUpState()
52 m_instruction_addr = m_thread.GetRegisterContext()->GetPC(0);
53 StackFrameSP start_frame_sp(m_thread.GetStackFrameAtIndex(0));
54 m_stack_id = start_frame_sp->GetStackID();
56 m_start_has_symbol = start_frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol != nullptr;
58 StackFrameSP parent_frame_sp = m_thread.GetStackFrameAtIndex(1);
60 m_parent_frame_id = parent_frame_sp->GetStackID();
64 ThreadPlanStepInstruction::GetDescription (Stream *s, lldb::DescriptionLevel level)
66 if (level == lldb::eDescriptionLevelBrief)
69 s->Printf ("instruction step over");
71 s->Printf ("instruction step into");
75 s->Printf ("Stepping one instruction past ");
76 s->Address(m_instruction_addr, sizeof (addr_t));
77 if (!m_start_has_symbol)
78 s->Printf(" which has no symbol");
81 s->Printf(" stepping over calls");
83 s->Printf(" stepping into calls");
88 ThreadPlanStepInstruction::ValidatePlan (Stream *error)
90 // Since we read the instruction we're stepping over from the thread,
91 // this plan will always work.
96 ThreadPlanStepInstruction::DoPlanExplainsStop (Event *event_ptr)
98 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
101 StopReason reason = stop_info_sp->GetStopReason();
102 return (reason == eStopReasonTrace || reason == eStopReasonNone);
108 ThreadPlanStepInstruction::IsPlanStale ()
110 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
111 StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
112 if (cur_frame_id == m_stack_id)
114 return (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr);
116 else if (cur_frame_id < m_stack_id)
118 // If the current frame is younger than the start frame and we are stepping over, then we need to continue,
119 // but if we are doing just one step, we're done.
126 log->Printf ("ThreadPlanStepInstruction::IsPlanStale - Current frame is older than start frame, plan is stale.");
133 ThreadPlanStepInstruction::ShouldStop (Event *event_ptr)
137 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
139 StackFrameSP cur_frame_sp = m_thread.GetStackFrameAtIndex(0);
143 log->Printf ("ThreadPlanStepInstruction couldn't get the 0th frame, stopping.");
148 StackID cur_frame_zero_id = cur_frame_sp->GetStackID();
150 if (cur_frame_zero_id == m_stack_id || m_stack_id < cur_frame_zero_id)
152 if (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr)
154 if (--m_iteration_count <= 0)
161 // We are still stepping, reset the start pc, and in case we've stepped out,
162 // reset the current stack id.
172 // We've stepped in, step back out again:
173 StackFrame *return_frame = m_thread.GetStackFrameAtIndex(1).get();
176 if (return_frame->GetStackID() != m_parent_frame_id || m_start_has_symbol)
178 // next-instruction shouldn't step out of inlined functions. But we may have stepped into a
179 // real function that starts with an inlined function, and we do want to step out of that...
181 if (cur_frame_sp->IsInlined())
183 StackFrameSP parent_frame_sp = m_thread.GetFrameWithStackID(m_stack_id);
185 if(parent_frame_sp && parent_frame_sp->GetConcreteFrameIndex() == cur_frame_sp->GetConcreteFrameIndex())
190 log->Printf("Frame we stepped into is inlined into the frame we were stepping from, stopping.");
199 s.PutCString ("Stepped in to: ");
200 addr_t stop_addr = m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
201 s.Address (stop_addr, m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
202 s.PutCString (" stepping out to: ");
203 addr_t return_addr = return_frame->GetRegisterContext()->GetPC();
204 s.Address (return_addr, m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
205 log->Printf("%s.", s.GetData());
208 // StepInstruction should probably have the tri-state RunMode, but for now it is safer to
210 const bool stop_others = false;
211 m_thread.QueueThreadPlanForStepOutNoShouldStop(false,
224 log->PutCString("The stack id we are stepping in changed, but our parent frame did not when stepping from code with no symbols. "
225 "We are probably just confused about where we are, stopping.");
234 log->Printf("Could not find previous frame, stopping.");
242 lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC(0);
243 if (pc_addr != m_instruction_addr)
245 if (--m_iteration_count <= 0)
252 // We are still stepping, reset the start pc, and in case we've stepped in or out,
253 // reset the current stack id.
264 ThreadPlanStepInstruction::StopOthers ()
266 return m_stop_other_threads;
270 ThreadPlanStepInstruction::GetPlanRunState ()
272 return eStateStepping;
276 ThreadPlanStepInstruction::WillStop ()
282 ThreadPlanStepInstruction::MischiefManaged ()
284 if (IsPlanComplete())
286 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
288 log->Printf("Completed single instruction step plan.");
289 ThreadPlan::MischiefManaged ();