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 
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 
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 
54 void ThreadPlanStepInstruction::GetDescription(Stream *s,
55                                                lldb::DescriptionLevel level) {
56   if (level == lldb::eDescriptionLevelBrief) {
57     if (m_step_over)
58       s->Printf("instruction step over");
59     else
60       s->Printf("instruction step into");
61   } else {
62     s->Printf("Stepping one instruction past ");
63     s->Address(m_instruction_addr, sizeof(addr_t));
64     if (!m_start_has_symbol)
65       s->Printf(" which has no symbol");
66 
67     if (m_step_over)
68       s->Printf(" stepping over calls");
69     else
70       s->Printf(" stepping into calls");
71   }
72 }
73 
74 bool ThreadPlanStepInstruction::ValidatePlan(Stream *error) {
75   // Since we read the instruction we're stepping over from the thread, this
76   // plan will always work.
77   return true;
78 }
79 
80 bool ThreadPlanStepInstruction::DoPlanExplainsStop(Event *event_ptr) {
81   StopInfoSP stop_info_sp = GetPrivateStopInfo();
82   if (stop_info_sp) {
83     StopReason reason = stop_info_sp->GetStopReason();
84     return (reason == eStopReasonTrace || reason == eStopReasonNone);
85   }
86   return false;
87 }
88 
89 bool ThreadPlanStepInstruction::IsPlanStale() {
90   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
91   StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
92   if (cur_frame_id == m_stack_id) {
93     // Set plan Complete when we reach next instruction
94     uint64_t pc = m_thread.GetRegisterContext()->GetPC(0);
95     uint32_t max_opcode_size = m_thread.CalculateTarget()
96         ->GetArchitecture().GetMaximumOpcodeByteSize();
97     bool next_instruction_reached = (pc > m_instruction_addr) &&
98         (pc <= m_instruction_addr + max_opcode_size);
99     if (next_instruction_reached) {
100       SetPlanComplete();
101     }
102     return (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr);
103   } else if (cur_frame_id < m_stack_id) {
104     // If the current frame is younger than the start frame and we are stepping
105     // over, then we need to continue, but if we are doing just one step, we're
106     // done.
107     return !m_step_over;
108   } else {
109     if (log) {
110       log->Printf("ThreadPlanStepInstruction::IsPlanStale - Current frame is "
111                   "older than start frame, plan is stale.");
112     }
113     return true;
114   }
115 }
116 
117 bool ThreadPlanStepInstruction::ShouldStop(Event *event_ptr) {
118   if (m_step_over) {
119     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
120 
121     StackFrameSP cur_frame_sp = m_thread.GetStackFrameAtIndex(0);
122     if (!cur_frame_sp) {
123       if (log)
124         log->Printf(
125             "ThreadPlanStepInstruction couldn't get the 0th frame, stopping.");
126       SetPlanComplete();
127       return true;
128     }
129 
130     StackID cur_frame_zero_id = cur_frame_sp->GetStackID();
131 
132     if (cur_frame_zero_id == m_stack_id || m_stack_id < cur_frame_zero_id) {
133       if (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr) {
134         if (--m_iteration_count <= 0) {
135           SetPlanComplete();
136           return true;
137         } else {
138           // We are still stepping, reset the start pc, and in case we've
139           // stepped out, reset the current stack id.
140           SetUpState();
141           return false;
142         }
143       } else
144         return false;
145     } else {
146       // We've stepped in, step back out again:
147       StackFrame *return_frame = m_thread.GetStackFrameAtIndex(1).get();
148       if (return_frame) {
149         if (return_frame->GetStackID() != m_parent_frame_id ||
150             m_start_has_symbol) {
151           // next-instruction shouldn't step out of inlined functions.  But we
152           // may have stepped into a real function that starts with an inlined
153           // function, and we do want to step out of that...
154 
155           if (cur_frame_sp->IsInlined()) {
156             StackFrameSP parent_frame_sp =
157                 m_thread.GetFrameWithStackID(m_stack_id);
158 
159             if (parent_frame_sp &&
160                 parent_frame_sp->GetConcreteFrameIndex() ==
161                     cur_frame_sp->GetConcreteFrameIndex()) {
162               SetPlanComplete();
163               if (log) {
164                 log->Printf("Frame we stepped into is inlined into the frame "
165                             "we were stepping from, stopping.");
166               }
167               return true;
168             }
169           }
170 
171           if (log) {
172             StreamString s;
173             s.PutCString("Stepped in to: ");
174             addr_t stop_addr =
175                 m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
176             s.Address(stop_addr, m_thread.CalculateTarget()
177                                      ->GetArchitecture()
178                                      .GetAddressByteSize());
179             s.PutCString(" stepping out to: ");
180             addr_t return_addr = return_frame->GetRegisterContext()->GetPC();
181             s.Address(return_addr, m_thread.CalculateTarget()
182                                        ->GetArchitecture()
183                                        .GetAddressByteSize());
184             log->Printf("%s.", s.GetData());
185           }
186 
187           // StepInstruction should probably have the tri-state RunMode, but
188           // for now it is safer to run others.
189           const bool stop_others = false;
190           m_thread.QueueThreadPlanForStepOutNoShouldStop(
191               false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0);
192           return false;
193         } else {
194           if (log) {
195             log->PutCString(
196                 "The stack id we are stepping in changed, but our parent frame "
197                 "did not when stepping from code with no symbols.  "
198                 "We are probably just confused about where we are, stopping.");
199           }
200           SetPlanComplete();
201           return true;
202         }
203       } else {
204         if (log)
205           log->Printf("Could not find previous frame, stopping.");
206         SetPlanComplete();
207         return true;
208       }
209     }
210   } else {
211     lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC(0);
212     if (pc_addr != m_instruction_addr) {
213       if (--m_iteration_count <= 0) {
214         SetPlanComplete();
215         return true;
216       } else {
217         // We are still stepping, reset the start pc, and in case we've stepped
218         // in or out, reset the current stack id.
219         SetUpState();
220         return false;
221       }
222     } else
223       return false;
224   }
225 }
226 
227 bool ThreadPlanStepInstruction::StopOthers() { return m_stop_other_threads; }
228 
229 StateType ThreadPlanStepInstruction::GetPlanRunState() {
230   return eStateStepping;
231 }
232 
233 bool ThreadPlanStepInstruction::WillStop() { return true; }
234 
235 bool ThreadPlanStepInstruction::MischiefManaged() {
236   if (IsPlanComplete()) {
237     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
238     if (log)
239       log->Printf("Completed single instruction step plan.");
240     ThreadPlan::MischiefManaged();
241     return true;
242   } else {
243     return false;
244   }
245 }
246