180814287SRaphael Isemann //===-- ThreadPlanStepInstruction.cpp -------------------------------------===//
230fdc8d8SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630fdc8d8SChris Lattner //
730fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
830fdc8d8SChris Lattner 
9e65b2cf2SEugene Zelenko #include "lldb/Target/ThreadPlanStepInstruction.h"
1030fdc8d8SChris Lattner #include "lldb/Target/Process.h"
11f4b47e15SGreg Clayton #include "lldb/Target/RegisterContext.h"
12f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h"
13f4b47e15SGreg Clayton #include "lldb/Target/Target.h"
14*c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
156f9e6901SZachary Turner #include "lldb/Utility/Log.h"
16bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
1730fdc8d8SChris Lattner 
1830fdc8d8SChris Lattner using namespace lldb;
1930fdc8d8SChris Lattner using namespace lldb_private;
2030fdc8d8SChris Lattner 
2130fdc8d8SChris Lattner // ThreadPlanStepInstruction: Step over the current instruction
2230fdc8d8SChris Lattner 
ThreadPlanStepInstruction(Thread & thread,bool step_over,bool stop_other_threads,Vote report_stop_vote,Vote report_run_vote)23b9c1b51eSKate Stone ThreadPlanStepInstruction::ThreadPlanStepInstruction(Thread &thread,
2430fdc8d8SChris Lattner                                                      bool step_over,
2530fdc8d8SChris Lattner                                                      bool stop_other_threads,
269d3b9e57SDave Lee                                                      Vote report_stop_vote,
279d3b9e57SDave Lee                                                      Vote report_run_vote)
28b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindStepInstruction,
299d3b9e57SDave Lee                  "Step over single instruction", thread, report_stop_vote,
309d3b9e57SDave Lee                  report_run_vote),
31b9c1b51eSKate Stone       m_instruction_addr(0), m_stop_other_threads(stop_other_threads),
32b9c1b51eSKate Stone       m_step_over(step_over) {
337a88ec9aSJim Ingham   m_takes_iteration_count = true;
347a88ec9aSJim Ingham   SetUpState();
3530fdc8d8SChris Lattner }
3630fdc8d8SChris Lattner 
37e65b2cf2SEugene Zelenko ThreadPlanStepInstruction::~ThreadPlanStepInstruction() = default;
3830fdc8d8SChris Lattner 
SetUpState()39b9c1b51eSKate Stone void ThreadPlanStepInstruction::SetUpState() {
40e4598dc0SJim Ingham   Thread &thread = GetThread();
41e4598dc0SJim Ingham   m_instruction_addr = thread.GetRegisterContext()->GetPC(0);
42e4598dc0SJim Ingham   StackFrameSP start_frame_sp(thread.GetStackFrameAtIndex(0));
437a88ec9aSJim Ingham   m_stack_id = start_frame_sp->GetStackID();
447a88ec9aSJim Ingham 
45b9c1b51eSKate Stone   m_start_has_symbol =
46b9c1b51eSKate Stone       start_frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol != nullptr;
477a88ec9aSJim Ingham 
48e4598dc0SJim Ingham   StackFrameSP parent_frame_sp = thread.GetStackFrameAtIndex(1);
497a88ec9aSJim Ingham   if (parent_frame_sp)
507a88ec9aSJim Ingham     m_parent_frame_id = parent_frame_sp->GetStackID();
517a88ec9aSJim Ingham }
527a88ec9aSJim Ingham 
GetDescription(Stream * s,lldb::DescriptionLevel level)53b9c1b51eSKate Stone void ThreadPlanStepInstruction::GetDescription(Stream *s,
54b9c1b51eSKate Stone                                                lldb::DescriptionLevel level) {
55e103ae92SJonas Devlieghere   auto PrintFailureIfAny = [&]() {
56e103ae92SJonas Devlieghere     if (m_status.Success())
57e103ae92SJonas Devlieghere       return;
58e103ae92SJonas Devlieghere     s->Printf(" failed (%s)", m_status.AsCString());
59e103ae92SJonas Devlieghere   };
60e103ae92SJonas Devlieghere 
61b9c1b51eSKate Stone   if (level == lldb::eDescriptionLevelBrief) {
6230fdc8d8SChris Lattner     if (m_step_over)
6330fdc8d8SChris Lattner       s->Printf("instruction step over");
6430fdc8d8SChris Lattner     else
6530fdc8d8SChris Lattner       s->Printf("instruction step into");
66e103ae92SJonas Devlieghere 
67e103ae92SJonas Devlieghere     PrintFailureIfAny();
68b9c1b51eSKate Stone   } else {
6930fdc8d8SChris Lattner     s->Printf("Stepping one instruction past ");
701462f5a4SRaphael Isemann     DumpAddress(s->AsRawOstream(), m_instruction_addr, sizeof(addr_t));
71a7d4822cSJim Ingham     if (!m_start_has_symbol)
72a7d4822cSJim Ingham       s->Printf(" which has no symbol");
73a7d4822cSJim Ingham 
7430fdc8d8SChris Lattner     if (m_step_over)
7530fdc8d8SChris Lattner       s->Printf(" stepping over calls");
7630fdc8d8SChris Lattner     else
7730fdc8d8SChris Lattner       s->Printf(" stepping into calls");
78e103ae92SJonas Devlieghere 
79e103ae92SJonas Devlieghere     PrintFailureIfAny();
8030fdc8d8SChris Lattner   }
8130fdc8d8SChris Lattner }
8230fdc8d8SChris Lattner 
ValidatePlan(Stream * error)83b9c1b51eSKate Stone bool ThreadPlanStepInstruction::ValidatePlan(Stream *error) {
8405097246SAdrian Prantl   // Since we read the instruction we're stepping over from the thread, this
8505097246SAdrian Prantl   // plan will always work.
8630fdc8d8SChris Lattner   return true;
8730fdc8d8SChris Lattner }
8830fdc8d8SChris Lattner 
DoPlanExplainsStop(Event * event_ptr)89b9c1b51eSKate Stone bool ThreadPlanStepInstruction::DoPlanExplainsStop(Event *event_ptr) {
9060c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
91b9c1b51eSKate Stone   if (stop_info_sp) {
92b15bfc75SJim Ingham     StopReason reason = stop_info_sp->GetStopReason();
93e65b2cf2SEugene Zelenko     return (reason == eStopReasonTrace || reason == eStopReasonNone);
9430fdc8d8SChris Lattner   }
9530fdc8d8SChris Lattner   return false;
9630fdc8d8SChris Lattner }
9730fdc8d8SChris Lattner 
IsPlanStale()98b9c1b51eSKate Stone bool ThreadPlanStepInstruction::IsPlanStale() {
99a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Step);
100e4598dc0SJim Ingham   Thread &thread = GetThread();
101e4598dc0SJim Ingham   StackID cur_frame_id = thread.GetStackFrameAtIndex(0)->GetStackID();
102b9c1b51eSKate Stone   if (cur_frame_id == m_stack_id) {
10386aaa8a2SBoris Ulasevich     // Set plan Complete when we reach next instruction
104e4598dc0SJim Ingham     uint64_t pc = thread.GetRegisterContext()->GetPC(0);
105e4598dc0SJim Ingham     uint32_t max_opcode_size =
106e4598dc0SJim Ingham         GetTarget().GetArchitecture().GetMaximumOpcodeByteSize();
10786aaa8a2SBoris Ulasevich     bool next_instruction_reached = (pc > m_instruction_addr) &&
10886aaa8a2SBoris Ulasevich         (pc <= m_instruction_addr + max_opcode_size);
10986aaa8a2SBoris Ulasevich     if (next_instruction_reached) {
11086aaa8a2SBoris Ulasevich       SetPlanComplete();
11186aaa8a2SBoris Ulasevich     }
112e4598dc0SJim Ingham     return (thread.GetRegisterContext()->GetPC(0) != m_instruction_addr);
113b9c1b51eSKate Stone   } else if (cur_frame_id < m_stack_id) {
114b9c1b51eSKate Stone     // If the current frame is younger than the start frame and we are stepping
11505097246SAdrian Prantl     // over, then we need to continue, but if we are doing just one step, we're
11605097246SAdrian Prantl     // done.
117e65b2cf2SEugene Zelenko     return !m_step_over;
118b9c1b51eSKate Stone   } else {
119b9c1b51eSKate Stone     if (log) {
12063e5fb76SJonas Devlieghere       LLDB_LOGF(log,
12163e5fb76SJonas Devlieghere                 "ThreadPlanStepInstruction::IsPlanStale - Current frame is "
122b9c1b51eSKate Stone                 "older than start frame, plan is stale.");
1237a88ec9aSJim Ingham     }
1247a88ec9aSJim Ingham     return true;
1257a88ec9aSJim Ingham   }
1267a88ec9aSJim Ingham }
1277a88ec9aSJim Ingham 
ShouldStop(Event * event_ptr)128b9c1b51eSKate Stone bool ThreadPlanStepInstruction::ShouldStop(Event *event_ptr) {
129e4598dc0SJim Ingham   Thread &thread = GetThread();
130b9c1b51eSKate Stone   if (m_step_over) {
131a007a6d8SPavel Labath     Log *log = GetLog(LLDBLog::Step);
132e4598dc0SJim Ingham     StackFrameSP cur_frame_sp = thread.GetStackFrameAtIndex(0);
133b9c1b51eSKate Stone     if (!cur_frame_sp) {
13463e5fb76SJonas Devlieghere       LLDB_LOGF(
13563e5fb76SJonas Devlieghere           log,
136b9c1b51eSKate Stone           "ThreadPlanStepInstruction couldn't get the 0th frame, stopping.");
137c0b4d5a1SJim Ingham       SetPlanComplete();
138c0b4d5a1SJim Ingham       return true;
139c0b4d5a1SJim Ingham     }
140c0b4d5a1SJim Ingham 
141c0b4d5a1SJim Ingham     StackID cur_frame_zero_id = cur_frame_sp->GetStackID();
1426b35c86fSJim Ingham 
143b9c1b51eSKate Stone     if (cur_frame_zero_id == m_stack_id || m_stack_id < cur_frame_zero_id) {
144e4598dc0SJim Ingham       if (thread.GetRegisterContext()->GetPC(0) != m_instruction_addr) {
145b9c1b51eSKate Stone         if (--m_iteration_count <= 0) {
14630fdc8d8SChris Lattner           SetPlanComplete();
14730fdc8d8SChris Lattner           return true;
148b9c1b51eSKate Stone         } else {
149b9c1b51eSKate Stone           // We are still stepping, reset the start pc, and in case we've
15005097246SAdrian Prantl           // stepped out, reset the current stack id.
1517a88ec9aSJim Ingham           SetUpState();
1527a88ec9aSJim Ingham           return false;
1537a88ec9aSJim Ingham         }
154b9c1b51eSKate Stone       } else
15530fdc8d8SChris Lattner         return false;
156b9c1b51eSKate Stone     } else {
15730fdc8d8SChris Lattner       // We've stepped in, step back out again:
158e4598dc0SJim Ingham       StackFrame *return_frame = thread.GetStackFrameAtIndex(1).get();
159b9c1b51eSKate Stone       if (return_frame) {
160b9c1b51eSKate Stone         if (return_frame->GetStackID() != m_parent_frame_id ||
161b9c1b51eSKate Stone             m_start_has_symbol) {
162b9c1b51eSKate Stone           // next-instruction shouldn't step out of inlined functions.  But we
16305097246SAdrian Prantl           // may have stepped into a real function that starts with an inlined
16405097246SAdrian Prantl           // function, and we do want to step out of that...
165c0b4d5a1SJim Ingham 
166b9c1b51eSKate Stone           if (cur_frame_sp->IsInlined()) {
167b9c1b51eSKate Stone             StackFrameSP parent_frame_sp =
168e4598dc0SJim Ingham                 thread.GetFrameWithStackID(m_stack_id);
169c0b4d5a1SJim Ingham 
170b9c1b51eSKate Stone             if (parent_frame_sp &&
171b9c1b51eSKate Stone                 parent_frame_sp->GetConcreteFrameIndex() ==
172b9c1b51eSKate Stone                     cur_frame_sp->GetConcreteFrameIndex()) {
173c0b4d5a1SJim Ingham               SetPlanComplete();
174b9c1b51eSKate Stone               if (log) {
17563e5fb76SJonas Devlieghere                 LLDB_LOGF(log,
17663e5fb76SJonas Devlieghere                           "Frame we stepped into is inlined into the frame "
177b9c1b51eSKate Stone                           "we were stepping from, stopping.");
178c0b4d5a1SJim Ingham               }
179c0b4d5a1SJim Ingham               return true;
180c0b4d5a1SJim Ingham             }
181c0b4d5a1SJim Ingham           }
182c0b4d5a1SJim Ingham 
183b9c1b51eSKate Stone           if (log) {
18430fdc8d8SChris Lattner             StreamString s;
18530fdc8d8SChris Lattner             s.PutCString("Stepped in to: ");
186b9c1b51eSKate Stone             addr_t stop_addr =
187e4598dc0SJim Ingham                 thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
1881462f5a4SRaphael Isemann             DumpAddress(s.AsRawOstream(), stop_addr,
189e4598dc0SJim Ingham                         GetTarget().GetArchitecture().GetAddressByteSize());
19030fdc8d8SChris Lattner             s.PutCString(" stepping out to: ");
1919da7bd07SGreg Clayton             addr_t return_addr = return_frame->GetRegisterContext()->GetPC();
1921462f5a4SRaphael Isemann             DumpAddress(s.AsRawOstream(), return_addr,
193e4598dc0SJim Ingham                         GetTarget().GetArchitecture().GetAddressByteSize());
19463e5fb76SJonas Devlieghere             LLDB_LOGF(log, "%s.", s.GetData());
19530fdc8d8SChris Lattner           }
1964a58e968SJim Ingham 
19705097246SAdrian Prantl           // StepInstruction should probably have the tri-state RunMode, but
19805097246SAdrian Prantl           // for now it is safer to run others.
1994a58e968SJim Ingham           const bool stop_others = false;
200e4598dc0SJim Ingham           thread.QueueThreadPlanForStepOutNoShouldStop(
201e103ae92SJonas Devlieghere               false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0,
202e103ae92SJonas Devlieghere               m_status);
20330fdc8d8SChris Lattner           return false;
204b9c1b51eSKate Stone         } else {
205b9c1b51eSKate Stone           if (log) {
206b9c1b51eSKate Stone             log->PutCString(
207b9c1b51eSKate Stone                 "The stack id we are stepping in changed, but our parent frame "
208b9c1b51eSKate Stone                 "did not when stepping from code with no symbols.  "
209886a3e2cSJim Ingham                 "We are probably just confused about where we are, stopping.");
210886a3e2cSJim Ingham           }
211886a3e2cSJim Ingham           SetPlanComplete();
212886a3e2cSJim Ingham           return true;
213886a3e2cSJim Ingham         }
214b9c1b51eSKate Stone       } else {
21563e5fb76SJonas Devlieghere         LLDB_LOGF(log, "Could not find previous frame, stopping.");
21630fdc8d8SChris Lattner         SetPlanComplete();
21730fdc8d8SChris Lattner         return true;
21830fdc8d8SChris Lattner       }
21930fdc8d8SChris Lattner     }
220b9c1b51eSKate Stone   } else {
221e4598dc0SJim Ingham     lldb::addr_t pc_addr = thread.GetRegisterContext()->GetPC(0);
222b9c1b51eSKate Stone     if (pc_addr != m_instruction_addr) {
223b9c1b51eSKate Stone       if (--m_iteration_count <= 0) {
22430fdc8d8SChris Lattner         SetPlanComplete();
22530fdc8d8SChris Lattner         return true;
226b9c1b51eSKate Stone       } else {
227b9c1b51eSKate Stone         // We are still stepping, reset the start pc, and in case we've stepped
22805097246SAdrian Prantl         // in or out, reset the current stack id.
2297a88ec9aSJim Ingham         SetUpState();
2307a88ec9aSJim Ingham         return false;
2317a88ec9aSJim Ingham       }
232b9c1b51eSKate Stone     } else
23330fdc8d8SChris Lattner       return false;
23430fdc8d8SChris Lattner   }
23530fdc8d8SChris Lattner }
23630fdc8d8SChris Lattner 
StopOthers()237b9c1b51eSKate Stone bool ThreadPlanStepInstruction::StopOthers() { return m_stop_other_threads; }
23830fdc8d8SChris Lattner 
GetPlanRunState()239b9c1b51eSKate Stone StateType ThreadPlanStepInstruction::GetPlanRunState() {
24030fdc8d8SChris Lattner   return eStateStepping;
24130fdc8d8SChris Lattner }
24230fdc8d8SChris Lattner 
WillStop()243b9c1b51eSKate Stone bool ThreadPlanStepInstruction::WillStop() { return true; }
24430fdc8d8SChris Lattner 
MischiefManaged()245b9c1b51eSKate Stone bool ThreadPlanStepInstruction::MischiefManaged() {
246b9c1b51eSKate Stone   if (IsPlanComplete()) {
247a007a6d8SPavel Labath     Log *log = GetLog(LLDBLog::Step);
24863e5fb76SJonas Devlieghere     LLDB_LOGF(log, "Completed single instruction step plan.");
24930fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
25030fdc8d8SChris Lattner     return true;
251b9c1b51eSKate Stone   } else {
25230fdc8d8SChris Lattner     return false;
25330fdc8d8SChris Lattner   }
25430fdc8d8SChris Lattner }
255