1*0b57cec5SDimitry Andric //===-- ThreadPlanStepInstruction.cpp -------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanStepInstruction.h"
10*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
11*0b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
12*0b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h"
13*0b57cec5SDimitry Andric #include "lldb/Target/Target.h"
14*0b57cec5SDimitry Andric #include "lldb/Utility/LLDBLog.h"
15*0b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
16*0b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
17*0b57cec5SDimitry Andric 
18*0b57cec5SDimitry Andric using namespace lldb;
19*0b57cec5SDimitry Andric using namespace lldb_private;
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric // ThreadPlanStepInstruction: Step over the current instruction
22*0b57cec5SDimitry Andric 
ThreadPlanStepInstruction(Thread & thread,bool step_over,bool stop_other_threads,Vote report_stop_vote,Vote report_run_vote)23*0b57cec5SDimitry Andric ThreadPlanStepInstruction::ThreadPlanStepInstruction(Thread &thread,
24*0b57cec5SDimitry Andric                                                      bool step_over,
25*0b57cec5SDimitry Andric                                                      bool stop_other_threads,
26*0b57cec5SDimitry Andric                                                      Vote report_stop_vote,
27*0b57cec5SDimitry Andric                                                      Vote report_run_vote)
28*0b57cec5SDimitry Andric     : ThreadPlan(ThreadPlan::eKindStepInstruction,
29*0b57cec5SDimitry Andric                  "Step over single instruction", thread, report_stop_vote,
30*0b57cec5SDimitry Andric                  report_run_vote),
31*0b57cec5SDimitry Andric       m_instruction_addr(0), m_stop_other_threads(stop_other_threads),
32*0b57cec5SDimitry Andric       m_step_over(step_over) {
33*0b57cec5SDimitry Andric   m_takes_iteration_count = true;
34*0b57cec5SDimitry Andric   SetUpState();
35*0b57cec5SDimitry Andric }
36*0b57cec5SDimitry Andric 
37*0b57cec5SDimitry Andric ThreadPlanStepInstruction::~ThreadPlanStepInstruction() = default;
38*0b57cec5SDimitry Andric 
SetUpState()39*0b57cec5SDimitry Andric void ThreadPlanStepInstruction::SetUpState() {
40*0b57cec5SDimitry Andric   Thread &thread = GetThread();
41*0b57cec5SDimitry Andric   m_instruction_addr = thread.GetRegisterContext()->GetPC(0);
42*0b57cec5SDimitry Andric   StackFrameSP start_frame_sp(thread.GetStackFrameAtIndex(0));
43*0b57cec5SDimitry Andric   m_stack_id = start_frame_sp->GetStackID();
44*0b57cec5SDimitry Andric 
45*0b57cec5SDimitry Andric   m_start_has_symbol =
46*0b57cec5SDimitry Andric       start_frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol != nullptr;
47*0b57cec5SDimitry Andric 
48*0b57cec5SDimitry Andric   StackFrameSP parent_frame_sp = thread.GetStackFrameAtIndex(1);
49*0b57cec5SDimitry Andric   if (parent_frame_sp)
50*0b57cec5SDimitry Andric     m_parent_frame_id = parent_frame_sp->GetStackID();
51*0b57cec5SDimitry Andric }
52*0b57cec5SDimitry Andric 
GetDescription(Stream * s,lldb::DescriptionLevel level)53*0b57cec5SDimitry Andric void ThreadPlanStepInstruction::GetDescription(Stream *s,
54*0b57cec5SDimitry Andric                                                lldb::DescriptionLevel level) {
55*0b57cec5SDimitry Andric   auto PrintFailureIfAny = [&]() {
56*0b57cec5SDimitry Andric     if (m_status.Success())
57*0b57cec5SDimitry Andric       return;
58*0b57cec5SDimitry Andric     s->Printf(" failed (%s)", m_status.AsCString());
59*0b57cec5SDimitry Andric   };
60*0b57cec5SDimitry Andric 
61*0b57cec5SDimitry Andric   if (level == lldb::eDescriptionLevelBrief) {
62*0b57cec5SDimitry Andric     if (m_step_over)
63*0b57cec5SDimitry Andric       s->Printf("instruction step over");
64*0b57cec5SDimitry Andric     else
65*0b57cec5SDimitry Andric       s->Printf("instruction step into");
66*0b57cec5SDimitry Andric 
67*0b57cec5SDimitry Andric     PrintFailureIfAny();
68*0b57cec5SDimitry Andric   } else {
69*0b57cec5SDimitry Andric     s->Printf("Stepping one instruction past ");
70*0b57cec5SDimitry Andric     DumpAddress(s->AsRawOstream(), m_instruction_addr, sizeof(addr_t));
71*0b57cec5SDimitry Andric     if (!m_start_has_symbol)
72*0b57cec5SDimitry Andric       s->Printf(" which has no symbol");
73*0b57cec5SDimitry Andric 
74*0b57cec5SDimitry Andric     if (m_step_over)
75*0b57cec5SDimitry Andric       s->Printf(" stepping over calls");
76*0b57cec5SDimitry Andric     else
77*0b57cec5SDimitry Andric       s->Printf(" stepping into calls");
78*0b57cec5SDimitry Andric 
79*0b57cec5SDimitry Andric     PrintFailureIfAny();
80*0b57cec5SDimitry Andric   }
81*0b57cec5SDimitry Andric }
82*0b57cec5SDimitry Andric 
ValidatePlan(Stream * error)83*0b57cec5SDimitry Andric bool ThreadPlanStepInstruction::ValidatePlan(Stream *error) {
84*0b57cec5SDimitry Andric   // Since we read the instruction we're stepping over from the thread, this
85*0b57cec5SDimitry Andric   // plan will always work.
86*0b57cec5SDimitry Andric   return true;
87*0b57cec5SDimitry Andric }
88*0b57cec5SDimitry Andric 
DoPlanExplainsStop(Event * event_ptr)89*0b57cec5SDimitry Andric bool ThreadPlanStepInstruction::DoPlanExplainsStop(Event *event_ptr) {
90*0b57cec5SDimitry Andric   StopInfoSP stop_info_sp = GetPrivateStopInfo();
91*0b57cec5SDimitry Andric   if (stop_info_sp) {
92*0b57cec5SDimitry Andric     StopReason reason = stop_info_sp->GetStopReason();
93*0b57cec5SDimitry Andric     return (reason == eStopReasonTrace || reason == eStopReasonNone);
94*0b57cec5SDimitry Andric   }
95*0b57cec5SDimitry Andric   return false;
96*0b57cec5SDimitry Andric }
97*0b57cec5SDimitry Andric 
IsPlanStale()98*0b57cec5SDimitry Andric bool ThreadPlanStepInstruction::IsPlanStale() {
99*0b57cec5SDimitry Andric   Log *log = GetLog(LLDBLog::Step);
100*0b57cec5SDimitry Andric   Thread &thread = GetThread();
101*0b57cec5SDimitry Andric   StackID cur_frame_id = thread.GetStackFrameAtIndex(0)->GetStackID();
102*0b57cec5SDimitry Andric   if (cur_frame_id == m_stack_id) {
103*0b57cec5SDimitry Andric     // Set plan Complete when we reach next instruction
104*0b57cec5SDimitry Andric     uint64_t pc = thread.GetRegisterContext()->GetPC(0);
105*0b57cec5SDimitry Andric     uint32_t max_opcode_size =
106*0b57cec5SDimitry Andric         GetTarget().GetArchitecture().GetMaximumOpcodeByteSize();
107*0b57cec5SDimitry Andric     bool next_instruction_reached = (pc > m_instruction_addr) &&
108*0b57cec5SDimitry Andric         (pc <= m_instruction_addr + max_opcode_size);
109*0b57cec5SDimitry Andric     if (next_instruction_reached) {
110*0b57cec5SDimitry Andric       SetPlanComplete();
111*0b57cec5SDimitry Andric     }
112*0b57cec5SDimitry Andric     return (thread.GetRegisterContext()->GetPC(0) != m_instruction_addr);
113*0b57cec5SDimitry Andric   } else if (cur_frame_id < m_stack_id) {
114*0b57cec5SDimitry Andric     // If the current frame is younger than the start frame and we are stepping
115*0b57cec5SDimitry Andric     // over, then we need to continue, but if we are doing just one step, we're
116*0b57cec5SDimitry Andric     // done.
117*0b57cec5SDimitry Andric     return !m_step_over;
118*0b57cec5SDimitry Andric   } else {
119*0b57cec5SDimitry Andric     if (log) {
120*0b57cec5SDimitry Andric       LLDB_LOGF(log,
121*0b57cec5SDimitry Andric                 "ThreadPlanStepInstruction::IsPlanStale - Current frame is "
122*0b57cec5SDimitry Andric                 "older than start frame, plan is stale.");
123*0b57cec5SDimitry Andric     }
124*0b57cec5SDimitry Andric     return true;
125*0b57cec5SDimitry Andric   }
126*0b57cec5SDimitry Andric }
127*0b57cec5SDimitry Andric 
ShouldStop(Event * event_ptr)128*0b57cec5SDimitry Andric bool ThreadPlanStepInstruction::ShouldStop(Event *event_ptr) {
129*0b57cec5SDimitry Andric   Thread &thread = GetThread();
130*0b57cec5SDimitry Andric   if (m_step_over) {
131*0b57cec5SDimitry Andric     Log *log = GetLog(LLDBLog::Step);
132*0b57cec5SDimitry Andric     StackFrameSP cur_frame_sp = thread.GetStackFrameAtIndex(0);
133*0b57cec5SDimitry Andric     if (!cur_frame_sp) {
134*0b57cec5SDimitry Andric       LLDB_LOGF(
135*0b57cec5SDimitry Andric           log,
136*0b57cec5SDimitry Andric           "ThreadPlanStepInstruction couldn't get the 0th frame, stopping.");
137*0b57cec5SDimitry Andric       SetPlanComplete();
138*0b57cec5SDimitry Andric       return true;
139*0b57cec5SDimitry Andric     }
140*0b57cec5SDimitry Andric 
141*0b57cec5SDimitry Andric     StackID cur_frame_zero_id = cur_frame_sp->GetStackID();
142*0b57cec5SDimitry Andric 
143*0b57cec5SDimitry Andric     if (cur_frame_zero_id == m_stack_id || m_stack_id < cur_frame_zero_id) {
144*0b57cec5SDimitry Andric       if (thread.GetRegisterContext()->GetPC(0) != m_instruction_addr) {
145*0b57cec5SDimitry Andric         if (--m_iteration_count <= 0) {
146*0b57cec5SDimitry Andric           SetPlanComplete();
147*0b57cec5SDimitry Andric           return true;
148*0b57cec5SDimitry Andric         } else {
149*0b57cec5SDimitry Andric           // We are still stepping, reset the start pc, and in case we've
150*0b57cec5SDimitry Andric           // stepped out, reset the current stack id.
151*0b57cec5SDimitry Andric           SetUpState();
152*0b57cec5SDimitry Andric           return false;
153*0b57cec5SDimitry Andric         }
154*0b57cec5SDimitry Andric       } else
155*0b57cec5SDimitry Andric         return false;
156*0b57cec5SDimitry Andric     } else {
157*0b57cec5SDimitry Andric       // We've stepped in, step back out again:
158*0b57cec5SDimitry Andric       StackFrame *return_frame = thread.GetStackFrameAtIndex(1).get();
159*0b57cec5SDimitry Andric       if (return_frame) {
160*0b57cec5SDimitry Andric         if (return_frame->GetStackID() != m_parent_frame_id ||
161*0b57cec5SDimitry Andric             m_start_has_symbol) {
162*0b57cec5SDimitry Andric           // next-instruction shouldn't step out of inlined functions.  But we
163*0b57cec5SDimitry Andric           // may have stepped into a real function that starts with an inlined
164*0b57cec5SDimitry Andric           // function, and we do want to step out of that...
165*0b57cec5SDimitry Andric 
166*0b57cec5SDimitry Andric           if (cur_frame_sp->IsInlined()) {
167*0b57cec5SDimitry Andric             StackFrameSP parent_frame_sp =
168*0b57cec5SDimitry Andric                 thread.GetFrameWithStackID(m_stack_id);
169*0b57cec5SDimitry Andric 
170*0b57cec5SDimitry Andric             if (parent_frame_sp &&
171*0b57cec5SDimitry Andric                 parent_frame_sp->GetConcreteFrameIndex() ==
172*0b57cec5SDimitry Andric                     cur_frame_sp->GetConcreteFrameIndex()) {
173*0b57cec5SDimitry Andric               SetPlanComplete();
174*0b57cec5SDimitry Andric               if (log) {
175*0b57cec5SDimitry Andric                 LLDB_LOGF(log,
176*0b57cec5SDimitry Andric                           "Frame we stepped into is inlined into the frame "
177*0b57cec5SDimitry Andric                           "we were stepping from, stopping.");
178*0b57cec5SDimitry Andric               }
179*0b57cec5SDimitry Andric               return true;
180*0b57cec5SDimitry Andric             }
181*0b57cec5SDimitry Andric           }
182*0b57cec5SDimitry Andric 
183*0b57cec5SDimitry Andric           if (log) {
184*0b57cec5SDimitry Andric             StreamString s;
185*0b57cec5SDimitry Andric             s.PutCString("Stepped in to: ");
186*0b57cec5SDimitry Andric             addr_t stop_addr =
187*0b57cec5SDimitry Andric                 thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
188*0b57cec5SDimitry Andric             DumpAddress(s.AsRawOstream(), stop_addr,
189*0b57cec5SDimitry Andric                         GetTarget().GetArchitecture().GetAddressByteSize());
190*0b57cec5SDimitry Andric             s.PutCString(" stepping out to: ");
191*0b57cec5SDimitry Andric             addr_t return_addr = return_frame->GetRegisterContext()->GetPC();
192*0b57cec5SDimitry Andric             DumpAddress(s.AsRawOstream(), return_addr,
193*0b57cec5SDimitry Andric                         GetTarget().GetArchitecture().GetAddressByteSize());
194*0b57cec5SDimitry Andric             LLDB_LOGF(log, "%s.", s.GetData());
195*0b57cec5SDimitry Andric           }
196*0b57cec5SDimitry Andric 
197*0b57cec5SDimitry Andric           // StepInstruction should probably have the tri-state RunMode, but
198*0b57cec5SDimitry Andric           // for now it is safer to run others.
199*0b57cec5SDimitry Andric           const bool stop_others = false;
200*0b57cec5SDimitry Andric           thread.QueueThreadPlanForStepOutNoShouldStop(
201*0b57cec5SDimitry Andric               false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0,
202*0b57cec5SDimitry Andric               m_status);
203*0b57cec5SDimitry Andric           return false;
204*0b57cec5SDimitry Andric         } else {
205*0b57cec5SDimitry Andric           if (log) {
206*0b57cec5SDimitry Andric             log->PutCString(
207*0b57cec5SDimitry Andric                 "The stack id we are stepping in changed, but our parent frame "
208*0b57cec5SDimitry Andric                 "did not when stepping from code with no symbols.  "
209*0b57cec5SDimitry Andric                 "We are probably just confused about where we are, stopping.");
210*0b57cec5SDimitry Andric           }
211*0b57cec5SDimitry Andric           SetPlanComplete();
212*0b57cec5SDimitry Andric           return true;
213*0b57cec5SDimitry Andric         }
214*0b57cec5SDimitry Andric       } else {
215*0b57cec5SDimitry Andric         LLDB_LOGF(log, "Could not find previous frame, stopping.");
216*0b57cec5SDimitry Andric         SetPlanComplete();
217*0b57cec5SDimitry Andric         return true;
218*0b57cec5SDimitry Andric       }
219*0b57cec5SDimitry Andric     }
220*0b57cec5SDimitry Andric   } else {
221*0b57cec5SDimitry Andric     lldb::addr_t pc_addr = thread.GetRegisterContext()->GetPC(0);
222*0b57cec5SDimitry Andric     if (pc_addr != m_instruction_addr) {
223*0b57cec5SDimitry Andric       if (--m_iteration_count <= 0) {
224*0b57cec5SDimitry Andric         SetPlanComplete();
225*0b57cec5SDimitry Andric         return true;
226*0b57cec5SDimitry Andric       } else {
227*0b57cec5SDimitry Andric         // We are still stepping, reset the start pc, and in case we've stepped
228*0b57cec5SDimitry Andric         // in or out, reset the current stack id.
229*0b57cec5SDimitry Andric         SetUpState();
230*0b57cec5SDimitry Andric         return false;
231*0b57cec5SDimitry Andric       }
232*0b57cec5SDimitry Andric     } else
233*0b57cec5SDimitry Andric       return false;
234*0b57cec5SDimitry Andric   }
235*0b57cec5SDimitry Andric }
236*0b57cec5SDimitry Andric 
StopOthers()237*0b57cec5SDimitry Andric bool ThreadPlanStepInstruction::StopOthers() { return m_stop_other_threads; }
238*0b57cec5SDimitry Andric 
GetPlanRunState()239*0b57cec5SDimitry Andric StateType ThreadPlanStepInstruction::GetPlanRunState() {
240*0b57cec5SDimitry Andric   return eStateStepping;
241*0b57cec5SDimitry Andric }
242*0b57cec5SDimitry Andric 
WillStop()243*0b57cec5SDimitry Andric bool ThreadPlanStepInstruction::WillStop() { return true; }
244*0b57cec5SDimitry Andric 
MischiefManaged()245*0b57cec5SDimitry Andric bool ThreadPlanStepInstruction::MischiefManaged() {
246*0b57cec5SDimitry Andric   if (IsPlanComplete()) {
247*0b57cec5SDimitry Andric     Log *log = GetLog(LLDBLog::Step);
248*0b57cec5SDimitry Andric     LLDB_LOGF(log, "Completed single instruction step plan.");
249*0b57cec5SDimitry Andric     ThreadPlan::MischiefManaged();
250*0b57cec5SDimitry Andric     return true;
251*0b57cec5SDimitry Andric   } else {
252*0b57cec5SDimitry Andric     return false;
253*0b57cec5SDimitry Andric   }
254 }
255