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 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/ThreadPlanStepInstruction.h"
11 #include "lldb/Target/Process.h"
12 #include "lldb/Target/RegisterContext.h"
13 #include "lldb/Target/RegisterContext.h"
14 #include "lldb/Target/StopInfo.h"
15 #include "lldb/Target/Target.h"
16 #include "lldb/Utility/Log.h"
17 #include "lldb/Utility/Stream.h"
20 using namespace lldb_private;
22 //----------------------------------------------------------------------
23 // ThreadPlanStepInstruction: Step over the current instruction
24 //----------------------------------------------------------------------
26 ThreadPlanStepInstruction::ThreadPlanStepInstruction(Thread &thread,
28 bool stop_other_threads,
31 : ThreadPlan(ThreadPlan::eKindStepInstruction,
32 "Step over single instruction", thread, stop_vote, run_vote),
33 m_instruction_addr(0), m_stop_other_threads(stop_other_threads),
34 m_step_over(step_over) {
35 m_takes_iteration_count = true;
39 ThreadPlanStepInstruction::~ThreadPlanStepInstruction() = default;
41 void ThreadPlanStepInstruction::SetUpState() {
42 m_instruction_addr = m_thread.GetRegisterContext()->GetPC(0);
43 StackFrameSP start_frame_sp(m_thread.GetStackFrameAtIndex(0));
44 m_stack_id = start_frame_sp->GetStackID();
47 start_frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol != nullptr;
49 StackFrameSP parent_frame_sp = m_thread.GetStackFrameAtIndex(1);
51 m_parent_frame_id = parent_frame_sp->GetStackID();
54 void ThreadPlanStepInstruction::GetDescription(Stream *s,
55 lldb::DescriptionLevel level) {
56 auto PrintFailureIfAny = [&]() {
57 if (m_status.Success())
59 s->Printf(" failed (%s)", m_status.AsCString());
62 if (level == lldb::eDescriptionLevelBrief) {
64 s->Printf("instruction step over");
66 s->Printf("instruction step into");
70 s->Printf("Stepping one instruction past ");
71 s->Address(m_instruction_addr, sizeof(addr_t));
72 if (!m_start_has_symbol)
73 s->Printf(" which has no symbol");
76 s->Printf(" stepping over calls");
78 s->Printf(" stepping into calls");
84 bool ThreadPlanStepInstruction::ValidatePlan(Stream *error) {
85 // Since we read the instruction we're stepping over from the thread, this
86 // plan will always work.
90 bool ThreadPlanStepInstruction::DoPlanExplainsStop(Event *event_ptr) {
91 StopInfoSP stop_info_sp = GetPrivateStopInfo();
93 StopReason reason = stop_info_sp->GetStopReason();
94 return (reason == eStopReasonTrace || reason == eStopReasonNone);
99 bool ThreadPlanStepInstruction::IsPlanStale() {
100 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
101 StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
102 if (cur_frame_id == m_stack_id) {
103 // Set plan Complete when we reach next instruction
104 uint64_t pc = m_thread.GetRegisterContext()->GetPC(0);
105 uint32_t max_opcode_size = m_thread.CalculateTarget()
106 ->GetArchitecture().GetMaximumOpcodeByteSize();
107 bool next_instruction_reached = (pc > m_instruction_addr) &&
108 (pc <= m_instruction_addr + max_opcode_size);
109 if (next_instruction_reached) {
112 return (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr);
113 } else if (cur_frame_id < m_stack_id) {
114 // If the current frame is younger than the start frame and we are stepping
115 // over, then we need to continue, but if we are doing just one step, we're
120 log->Printf("ThreadPlanStepInstruction::IsPlanStale - Current frame is "
121 "older than start frame, plan is stale.");
127 bool ThreadPlanStepInstruction::ShouldStop(Event *event_ptr) {
129 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
131 StackFrameSP cur_frame_sp = m_thread.GetStackFrameAtIndex(0);
135 "ThreadPlanStepInstruction couldn't get the 0th frame, stopping.");
140 StackID cur_frame_zero_id = cur_frame_sp->GetStackID();
142 if (cur_frame_zero_id == m_stack_id || m_stack_id < cur_frame_zero_id) {
143 if (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr) {
144 if (--m_iteration_count <= 0) {
148 // We are still stepping, reset the start pc, and in case we've
149 // stepped out, reset the current stack id.
156 // We've stepped in, step back out again:
157 StackFrame *return_frame = m_thread.GetStackFrameAtIndex(1).get();
159 if (return_frame->GetStackID() != m_parent_frame_id ||
160 m_start_has_symbol) {
161 // next-instruction shouldn't step out of inlined functions. But we
162 // may have stepped into a real function that starts with an inlined
163 // function, and we do want to step out of that...
165 if (cur_frame_sp->IsInlined()) {
166 StackFrameSP parent_frame_sp =
167 m_thread.GetFrameWithStackID(m_stack_id);
169 if (parent_frame_sp &&
170 parent_frame_sp->GetConcreteFrameIndex() ==
171 cur_frame_sp->GetConcreteFrameIndex()) {
174 log->Printf("Frame we stepped into is inlined into the frame "
175 "we were stepping from, stopping.");
183 s.PutCString("Stepped in to: ");
185 m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
186 s.Address(stop_addr, m_thread.CalculateTarget()
188 .GetAddressByteSize());
189 s.PutCString(" stepping out to: ");
190 addr_t return_addr = return_frame->GetRegisterContext()->GetPC();
191 s.Address(return_addr, m_thread.CalculateTarget()
193 .GetAddressByteSize());
194 log->Printf("%s.", s.GetData());
197 // StepInstruction should probably have the tri-state RunMode, but
198 // for now it is safer to run others.
199 const bool stop_others = false;
200 m_thread.QueueThreadPlanForStepOutNoShouldStop(
201 false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0,
207 "The stack id we are stepping in changed, but our parent frame "
208 "did not when stepping from code with no symbols. "
209 "We are probably just confused about where we are, stopping.");
216 log->Printf("Could not find previous frame, stopping.");
222 lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC(0);
223 if (pc_addr != m_instruction_addr) {
224 if (--m_iteration_count <= 0) {
228 // We are still stepping, reset the start pc, and in case we've stepped
229 // in or out, reset the current stack id.
238 bool ThreadPlanStepInstruction::StopOthers() { return m_stop_other_threads; }
240 StateType ThreadPlanStepInstruction::GetPlanRunState() {
241 return eStateStepping;
244 bool ThreadPlanStepInstruction::WillStop() { return true; }
246 bool ThreadPlanStepInstruction::MischiefManaged() {
247 if (IsPlanComplete()) {
248 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
250 log->Printf("Completed single instruction step plan.");
251 ThreadPlan::MischiefManaged();