blob: fabf63b1e9d647200779d2b450c46d6e13a79654 [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
10
11#include "lldb/Target/ThreadPlanStepInstruction.h"
12
13// C Includes
14// C++ Includes
15// Other libraries and framework includes
16// Project includes
17#include "lldb/lldb-private-log.h"
18#include "lldb/Core/Log.h"
19#include "lldb/Core/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Target/Process.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000021#include "lldb/Target/RegisterContext.h"
22#include "lldb/Target/RegisterContext.h"
23#include "lldb/Target/StopInfo.h"
24#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
26using namespace lldb;
27using namespace lldb_private;
28
29//----------------------------------------------------------------------
30// ThreadPlanStepInstruction: Step over the current instruction
31//----------------------------------------------------------------------
32
33ThreadPlanStepInstruction::ThreadPlanStepInstruction
34(
35 Thread &thread,
36 bool step_over,
37 bool stop_other_threads,
38 Vote stop_vote,
39 Vote run_vote
40) :
Jim Inghamb01e7422010-06-19 04:45:32 +000041 ThreadPlan (ThreadPlan::eKindStepInstruction, "Step over single instruction", thread, stop_vote, run_vote),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042 m_instruction_addr (0),
Benjamin Kramer1ee0d4f2010-07-16 12:32:33 +000043 m_stop_other_threads (stop_other_threads),
Jim Ingham6b35c862012-03-01 20:01:22 +000044 m_step_over (step_over)
Benjamin Kramer1ee0d4f2010-07-16 12:32:33 +000045{
Jim Ingham7a88ec92014-07-08 19:28:57 +000046 m_takes_iteration_count = true;
47 SetUpState();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048}
49
50ThreadPlanStepInstruction::~ThreadPlanStepInstruction ()
51{
52}
53
54void
Jim Ingham7a88ec92014-07-08 19:28:57 +000055ThreadPlanStepInstruction::SetUpState()
56{
57 m_instruction_addr = m_thread.GetRegisterContext()->GetPC(0);
58 StackFrameSP start_frame_sp(m_thread.GetStackFrameAtIndex(0));
59 m_stack_id = start_frame_sp->GetStackID();
60
61 m_start_has_symbol = start_frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol != NULL;
62
63 StackFrameSP parent_frame_sp = m_thread.GetStackFrameAtIndex(1);
64 if (parent_frame_sp)
65 m_parent_frame_id = parent_frame_sp->GetStackID();
66}
67
68void
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069ThreadPlanStepInstruction::GetDescription (Stream *s, lldb::DescriptionLevel level)
70{
71 if (level == lldb::eDescriptionLevelBrief)
72 {
73 if (m_step_over)
74 s->Printf ("instruction step over");
75 else
76 s->Printf ("instruction step into");
77 }
78 else
79 {
80 s->Printf ("Stepping one instruction past ");
81 s->Address(m_instruction_addr, sizeof (addr_t));
Jim Inghama7d48222013-07-26 00:27:57 +000082 if (!m_start_has_symbol)
83 s->Printf(" which has no symbol");
84
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085 if (m_step_over)
86 s->Printf(" stepping over calls");
87 else
88 s->Printf(" stepping into calls");
89 }
90}
91
92bool
93ThreadPlanStepInstruction::ValidatePlan (Stream *error)
94{
95 // Since we read the instruction we're stepping over from the thread,
96 // this plan will always work.
97 return true;
98}
99
100bool
Jim Ingham221d51c2013-05-08 00:35:16 +0000101ThreadPlanStepInstruction::DoPlanExplainsStop (Event *event_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102{
Jim Ingham60c41182013-06-04 01:40:51 +0000103 StopInfoSP stop_info_sp = GetPrivateStopInfo ();
Jim Inghamb15bfc72010-10-20 00:39:53 +0000104 if (stop_info_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105 {
Jim Inghamb15bfc72010-10-20 00:39:53 +0000106 StopReason reason = stop_info_sp->GetStopReason();
Greg Claytonf4b47e12010-08-04 01:40:35 +0000107 if (reason == eStopReasonTrace || reason == eStopReasonNone)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 return true;
109 else
110 return false;
111 }
112 return false;
113}
114
115bool
Jim Ingham7a88ec92014-07-08 19:28:57 +0000116ThreadPlanStepInstruction::IsPlanStale ()
117{
118 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
119 StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
120 if (cur_frame_id == m_stack_id)
121 {
122 if (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr)
123 return true;
124 else
125 return false;
126 }
127 else if (cur_frame_id < m_stack_id)
128 {
129 // If the current frame is younger than the start frame and we are stepping over, then we need to continue,
130 // but if we are doing just one step, we're done.
131 if (m_step_over)
132 return false;
133 else
134 return true;
135 }
136 else
137 {
138 if (log)
139 {
140 log->Printf ("ThreadPlanStepInstruction::IsPlanStale - Current frame is older than start frame, plan is stale.");
141 }
142 return true;
143 }
144}
145
146bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147ThreadPlanStepInstruction::ShouldStop (Event *event_ptr)
148{
149 if (m_step_over)
150 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000151 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham6b35c862012-03-01 20:01:22 +0000152
153 StackID cur_frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
154
155 if (cur_frame_zero_id == m_stack_id || m_stack_id < cur_frame_zero_id)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156 {
157 if (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr)
158 {
Jim Ingham7a88ec92014-07-08 19:28:57 +0000159 if (--m_iteration_count <= 0)
160 {
161 SetPlanComplete();
162 return true;
163 }
164 else
165 {
166 // We are still stepping, reset the start pc, and in case we've stepped out,
167 // reset the current stack id.
168 SetUpState();
169 return false;
170 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171 }
172 else
173 return false;
174 }
175 else
176 {
177 // We've stepped in, step back out again:
Jason Molendab57e4a12013-11-04 09:33:30 +0000178 StackFrame *return_frame = m_thread.GetStackFrameAtIndex(1).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 if (return_frame)
180 {
Jim Inghama7d48222013-07-26 00:27:57 +0000181 if (return_frame->GetStackID() != m_parent_frame_id || m_start_has_symbol)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182 {
Jim Ingham886a3e22013-07-25 00:59:01 +0000183 if (log)
184 {
185 StreamString s;
186 s.PutCString ("Stepped in to: ");
187 addr_t stop_addr = m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
188 s.Address (stop_addr, m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
189 s.PutCString (" stepping out to: ");
190 addr_t return_addr = return_frame->GetRegisterContext()->GetPC();
191 s.Address (return_addr, m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
192 log->Printf("%s.", s.GetData());
193 }
194
195 // StepInstruction should probably have the tri-state RunMode, but for now it is safer to
196 // run others.
197 const bool stop_others = false;
Jim Ingham4b4b2472014-03-13 02:47:14 +0000198 m_thread.QueueThreadPlanForStepOutNoShouldStop(false,
199 NULL,
200 true,
201 stop_others,
202 eVoteNo,
203 eVoteNoOpinion,
204 0);
Jim Ingham886a3e22013-07-25 00:59:01 +0000205 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206 }
Jim Ingham886a3e22013-07-25 00:59:01 +0000207 else
208 {
209 if (log)
210 {
Jim Inghama7d48222013-07-26 00:27:57 +0000211 log->PutCString("The stack id we are stepping in changed, but our parent frame did not when stepping from code with no symbols. "
Jim Ingham886a3e22013-07-25 00:59:01 +0000212 "We are probably just confused about where we are, stopping.");
213 }
214 SetPlanComplete();
215 return true;
216 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217 }
218 else
219 {
220 if (log)
221 log->Printf("Could not find previous frame, stopping.");
222 SetPlanComplete();
223 return true;
224 }
225
226 }
227
228 }
229 else
230 {
231 if (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr)
232 {
Jim Ingham7a88ec92014-07-08 19:28:57 +0000233 if (--m_iteration_count <= 0)
234 {
235 SetPlanComplete();
236 return true;
237 }
238 else
239 {
240 // We are still stepping, reset the start pc, and in case we've stepped in or out,
241 // reset the current stack id.
242 SetUpState();
243 return false;
244 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245 }
246 else
247 return false;
248 }
249}
250
251bool
252ThreadPlanStepInstruction::StopOthers ()
253{
254 return m_stop_other_threads;
255}
256
257StateType
Jim Ingham06e827c2010-11-11 19:26:09 +0000258ThreadPlanStepInstruction::GetPlanRunState ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259{
260 return eStateStepping;
261}
262
263bool
264ThreadPlanStepInstruction::WillStop ()
265{
266 return true;
267}
268
269bool
270ThreadPlanStepInstruction::MischiefManaged ()
271{
272 if (IsPlanComplete())
273 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000274 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 if (log)
276 log->Printf("Completed single instruction step plan.");
277 ThreadPlan::MischiefManaged ();
278 return true;
279 }
280 else
281 {
282 return false;
283 }
284}
285