blob: 7707454c97988db8ec1ffb6ca568f5bc0ad430c9 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ThreadPlanStepInstruction.cpp ---------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000010#include "lldb/Target/ThreadPlanStepInstruction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "lldb/Target/Process.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000012#include "lldb/Target/RegisterContext.h"
13#include "lldb/Target/RegisterContext.h"
14#include "lldb/Target/StopInfo.h"
15#include "lldb/Target/Target.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000016#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000017#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
19using namespace lldb;
20using namespace lldb_private;
21
22//----------------------------------------------------------------------
23// ThreadPlanStepInstruction: Step over the current instruction
24//----------------------------------------------------------------------
25
Kate Stoneb9c1b512016-09-06 20:57:50 +000026ThreadPlanStepInstruction::ThreadPlanStepInstruction(Thread &thread,
27 bool step_over,
28 bool stop_other_threads,
29 Vote stop_vote,
30 Vote run_vote)
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;
36 SetUpState();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037}
38
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000039ThreadPlanStepInstruction::~ThreadPlanStepInstruction() = default;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040
Kate Stoneb9c1b512016-09-06 20:57:50 +000041void 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();
45
46 m_start_has_symbol =
47 start_frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol != nullptr;
48
49 StackFrameSP parent_frame_sp = m_thread.GetStackFrameAtIndex(1);
50 if (parent_frame_sp)
51 m_parent_frame_id = parent_frame_sp->GetStackID();
Jim Ingham7a88ec92014-07-08 19:28:57 +000052}
53
Kate Stoneb9c1b512016-09-06 20:57:50 +000054void ThreadPlanStepInstruction::GetDescription(Stream *s,
55 lldb::DescriptionLevel level) {
Jonas Devliegheree103ae92018-11-15 01:18:15 +000056 auto PrintFailureIfAny = [&]() {
57 if (m_status.Success())
58 return;
59 s->Printf(" failed (%s)", m_status.AsCString());
60 };
61
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 if (level == lldb::eDescriptionLevelBrief) {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063 if (m_step_over)
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 s->Printf("instruction step over");
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 s->Printf("instruction step into");
Jonas Devliegheree103ae92018-11-15 01:18:15 +000067
68 PrintFailureIfAny();
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 } else {
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");
74
75 if (m_step_over)
76 s->Printf(" stepping over calls");
77 else
78 s->Printf(" stepping into calls");
Jonas Devliegheree103ae92018-11-15 01:18:15 +000079
80 PrintFailureIfAny();
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 }
82}
83
84bool ThreadPlanStepInstruction::ValidatePlan(Stream *error) {
Adrian Prantl05097242018-04-30 16:49:04 +000085 // Since we read the instruction we're stepping over from the thread, this
86 // plan will always work.
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 return true;
88}
89
90bool ThreadPlanStepInstruction::DoPlanExplainsStop(Event *event_ptr) {
91 StopInfoSP stop_info_sp = GetPrivateStopInfo();
92 if (stop_info_sp) {
93 StopReason reason = stop_info_sp->GetStopReason();
94 return (reason == eStopReasonTrace || reason == eStopReasonNone);
95 }
96 return false;
97}
98
99bool 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) {
Boris Ulasevich86aaa8a2017-02-15 11:42:47 +0000103 // 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) {
110 SetPlanComplete();
111 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 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
Adrian Prantl05097242018-04-30 16:49:04 +0000115 // over, then we need to continue, but if we are doing just one step, we're
116 // done.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 return !m_step_over;
118 } else {
119 if (log) {
120 log->Printf("ThreadPlanStepInstruction::IsPlanStale - Current frame is "
121 "older than start frame, plan is stale.");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125}
126
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127bool ThreadPlanStepInstruction::ShouldStop(Event *event_ptr) {
128 if (m_step_over) {
129 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
130
131 StackFrameSP cur_frame_sp = m_thread.GetStackFrameAtIndex(0);
132 if (!cur_frame_sp) {
133 if (log)
134 log->Printf(
135 "ThreadPlanStepInstruction couldn't get the 0th frame, stopping.");
136 SetPlanComplete();
137 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139
140 StackID cur_frame_zero_id = cur_frame_sp->GetStackID();
141
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) {
145 SetPlanComplete();
146 return true;
147 } else {
148 // We are still stepping, reset the start pc, and in case we've
Adrian Prantl05097242018-04-30 16:49:04 +0000149 // stepped out, reset the current stack id.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 SetUpState();
151 return false;
152 }
153 } else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000155 } else {
156 // We've stepped in, step back out again:
157 StackFrame *return_frame = m_thread.GetStackFrameAtIndex(1).get();
158 if (return_frame) {
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
Adrian Prantl05097242018-04-30 16:49:04 +0000162 // may have stepped into a real function that starts with an inlined
163 // function, and we do want to step out of that...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164
165 if (cur_frame_sp->IsInlined()) {
166 StackFrameSP parent_frame_sp =
167 m_thread.GetFrameWithStackID(m_stack_id);
168
169 if (parent_frame_sp &&
170 parent_frame_sp->GetConcreteFrameIndex() ==
171 cur_frame_sp->GetConcreteFrameIndex()) {
172 SetPlanComplete();
173 if (log) {
174 log->Printf("Frame we stepped into is inlined into the frame "
175 "we were stepping from, stopping.");
176 }
177 return true;
178 }
179 }
180
181 if (log) {
182 StreamString s;
183 s.PutCString("Stepped in to: ");
184 addr_t stop_addr =
185 m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
186 s.Address(stop_addr, m_thread.CalculateTarget()
187 ->GetArchitecture()
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()
192 ->GetArchitecture()
193 .GetAddressByteSize());
194 log->Printf("%s.", s.GetData());
195 }
196
Adrian Prantl05097242018-04-30 16:49:04 +0000197 // StepInstruction should probably have the tri-state RunMode, but
198 // for now it is safer to run others.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 const bool stop_others = false;
200 m_thread.QueueThreadPlanForStepOutNoShouldStop(
Jonas Devliegheree103ae92018-11-15 01:18:15 +0000201 false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0,
202 m_status);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203 return false;
204 } else {
205 if (log) {
206 log->PutCString(
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.");
210 }
211 SetPlanComplete();
212 return true;
213 }
214 } else {
215 if (log)
216 log->Printf("Could not find previous frame, stopping.");
217 SetPlanComplete();
218 return true;
219 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 } else {
222 lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC(0);
223 if (pc_addr != m_instruction_addr) {
224 if (--m_iteration_count <= 0) {
225 SetPlanComplete();
226 return true;
227 } else {
228 // We are still stepping, reset the start pc, and in case we've stepped
Adrian Prantl05097242018-04-30 16:49:04 +0000229 // in or out, reset the current stack id.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 SetUpState();
231 return false;
232 }
233 } else
234 return false;
235 }
236}
237
238bool ThreadPlanStepInstruction::StopOthers() { return m_stop_other_threads; }
239
240StateType ThreadPlanStepInstruction::GetPlanRunState() {
241 return eStateStepping;
242}
243
244bool ThreadPlanStepInstruction::WillStop() { return true; }
245
246bool ThreadPlanStepInstruction::MischiefManaged() {
247 if (IsPlanComplete()) {
248 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
249 if (log)
250 log->Printf("Completed single instruction step plan.");
251 ThreadPlan::MischiefManaged();
252 return true;
253 } else {
254 return false;
255 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256}