1 //===-- 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
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"
18
19 using namespace lldb;
20 using namespace lldb_private;
21
22 //----------------------------------------------------------------------
23 // ThreadPlanStepInstruction: Step over the current instruction
24 //----------------------------------------------------------------------
25
ThreadPlanStepInstruction(Thread & thread,bool step_over,bool stop_other_threads,Vote stop_vote,Vote run_vote)26 ThreadPlanStepInstruction::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();
37 }
38
39 ThreadPlanStepInstruction::~ThreadPlanStepInstruction() = default;
40
SetUpState()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();
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();
52 }
53
GetDescription(Stream * s,lldb::DescriptionLevel level)54 void ThreadPlanStepInstruction::GetDescription(Stream *s,
55 lldb::DescriptionLevel level) {
56 auto PrintFailureIfAny = [&]() {
57 if (m_status.Success())
58 return;
59 s->Printf(" failed (%s)", m_status.AsCString());
60 };
61
62 if (level == lldb::eDescriptionLevelBrief) {
63 if (m_step_over)
64 s->Printf("instruction step over");
65 else
66 s->Printf("instruction step into");
67
68 PrintFailureIfAny();
69 } 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");
79
80 PrintFailureIfAny();
81 }
82 }
83
ValidatePlan(Stream * error)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.
87 return true;
88 }
89
DoPlanExplainsStop(Event * event_ptr)90 bool 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
IsPlanStale()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) {
110 SetPlanComplete();
111 }
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
116 // done.
117 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.");
122 }
123 return true;
124 }
125 }
126
ShouldStop(Event * event_ptr)127 bool 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;
138 }
139
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
149 // stepped out, reset the current stack id.
150 SetUpState();
151 return false;
152 }
153 } else
154 return false;
155 } 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
162 // may have stepped into a real function that starts with an inlined
163 // function, and we do want to step out of that...
164
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
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,
202 m_status);
203 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 }
220 }
221 } 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
229 // in or out, reset the current stack id.
230 SetUpState();
231 return false;
232 }
233 } else
234 return false;
235 }
236 }
237
StopOthers()238 bool ThreadPlanStepInstruction::StopOthers() { return m_stop_other_threads; }
239
GetPlanRunState()240 StateType ThreadPlanStepInstruction::GetPlanRunState() {
241 return eStateStepping;
242 }
243
WillStop()244 bool ThreadPlanStepInstruction::WillStop() { return true; }
245
MischiefManaged()246 bool 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 }
256 }
257