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/RegisterContext.h"
13f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h"
14f4b47e15SGreg Clayton #include "lldb/Target/Target.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 
23b9c1b51eSKate Stone ThreadPlanStepInstruction::ThreadPlanStepInstruction(Thread &thread,
2430fdc8d8SChris Lattner                                                      bool step_over,
2530fdc8d8SChris Lattner                                                      bool stop_other_threads,
2630fdc8d8SChris Lattner                                                      Vote stop_vote,
27b9c1b51eSKate Stone                                                      Vote run_vote)
28b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindStepInstruction,
29b9c1b51eSKate Stone                  "Step over single instruction", thread, stop_vote, run_vote),
30b9c1b51eSKate Stone       m_instruction_addr(0), m_stop_other_threads(stop_other_threads),
31b9c1b51eSKate Stone       m_step_over(step_over) {
327a88ec9aSJim Ingham   m_takes_iteration_count = true;
337a88ec9aSJim Ingham   SetUpState();
3430fdc8d8SChris Lattner }
3530fdc8d8SChris Lattner 
36e65b2cf2SEugene Zelenko ThreadPlanStepInstruction::~ThreadPlanStepInstruction() = default;
3730fdc8d8SChris Lattner 
38b9c1b51eSKate Stone void ThreadPlanStepInstruction::SetUpState() {
39*e4598dc0SJim Ingham   Thread &thread = GetThread();
40*e4598dc0SJim Ingham   m_instruction_addr = thread.GetRegisterContext()->GetPC(0);
41*e4598dc0SJim Ingham   StackFrameSP start_frame_sp(thread.GetStackFrameAtIndex(0));
427a88ec9aSJim Ingham   m_stack_id = start_frame_sp->GetStackID();
437a88ec9aSJim Ingham 
44b9c1b51eSKate Stone   m_start_has_symbol =
45b9c1b51eSKate Stone       start_frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol != nullptr;
467a88ec9aSJim Ingham 
47*e4598dc0SJim Ingham   StackFrameSP parent_frame_sp = thread.GetStackFrameAtIndex(1);
487a88ec9aSJim Ingham   if (parent_frame_sp)
497a88ec9aSJim Ingham     m_parent_frame_id = parent_frame_sp->GetStackID();
507a88ec9aSJim Ingham }
517a88ec9aSJim Ingham 
52b9c1b51eSKate Stone void ThreadPlanStepInstruction::GetDescription(Stream *s,
53b9c1b51eSKate Stone                                                lldb::DescriptionLevel level) {
54e103ae92SJonas Devlieghere   auto PrintFailureIfAny = [&]() {
55e103ae92SJonas Devlieghere     if (m_status.Success())
56e103ae92SJonas Devlieghere       return;
57e103ae92SJonas Devlieghere     s->Printf(" failed (%s)", m_status.AsCString());
58e103ae92SJonas Devlieghere   };
59e103ae92SJonas Devlieghere 
60b9c1b51eSKate Stone   if (level == lldb::eDescriptionLevelBrief) {
6130fdc8d8SChris Lattner     if (m_step_over)
6230fdc8d8SChris Lattner       s->Printf("instruction step over");
6330fdc8d8SChris Lattner     else
6430fdc8d8SChris Lattner       s->Printf("instruction step into");
65e103ae92SJonas Devlieghere 
66e103ae92SJonas Devlieghere     PrintFailureIfAny();
67b9c1b51eSKate Stone   } else {
6830fdc8d8SChris Lattner     s->Printf("Stepping one instruction past ");
691462f5a4SRaphael Isemann     DumpAddress(s->AsRawOstream(), m_instruction_addr, sizeof(addr_t));
70a7d4822cSJim Ingham     if (!m_start_has_symbol)
71a7d4822cSJim Ingham       s->Printf(" which has no symbol");
72a7d4822cSJim Ingham 
7330fdc8d8SChris Lattner     if (m_step_over)
7430fdc8d8SChris Lattner       s->Printf(" stepping over calls");
7530fdc8d8SChris Lattner     else
7630fdc8d8SChris Lattner       s->Printf(" stepping into calls");
77e103ae92SJonas Devlieghere 
78e103ae92SJonas Devlieghere     PrintFailureIfAny();
7930fdc8d8SChris Lattner   }
8030fdc8d8SChris Lattner }
8130fdc8d8SChris Lattner 
82b9c1b51eSKate Stone bool ThreadPlanStepInstruction::ValidatePlan(Stream *error) {
8305097246SAdrian Prantl   // Since we read the instruction we're stepping over from the thread, this
8405097246SAdrian Prantl   // plan will always work.
8530fdc8d8SChris Lattner   return true;
8630fdc8d8SChris Lattner }
8730fdc8d8SChris Lattner 
88b9c1b51eSKate Stone bool ThreadPlanStepInstruction::DoPlanExplainsStop(Event *event_ptr) {
8960c4118cSJim Ingham   StopInfoSP stop_info_sp = GetPrivateStopInfo();
90b9c1b51eSKate Stone   if (stop_info_sp) {
91b15bfc75SJim Ingham     StopReason reason = stop_info_sp->GetStopReason();
92e65b2cf2SEugene Zelenko     return (reason == eStopReasonTrace || reason == eStopReasonNone);
9330fdc8d8SChris Lattner   }
9430fdc8d8SChris Lattner   return false;
9530fdc8d8SChris Lattner }
9630fdc8d8SChris Lattner 
97b9c1b51eSKate Stone bool ThreadPlanStepInstruction::IsPlanStale() {
987a88ec9aSJim Ingham   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
99*e4598dc0SJim Ingham   Thread &thread = GetThread();
100*e4598dc0SJim Ingham   StackID cur_frame_id = thread.GetStackFrameAtIndex(0)->GetStackID();
101b9c1b51eSKate Stone   if (cur_frame_id == m_stack_id) {
10286aaa8a2SBoris Ulasevich     // Set plan Complete when we reach next instruction
103*e4598dc0SJim Ingham     uint64_t pc = thread.GetRegisterContext()->GetPC(0);
104*e4598dc0SJim Ingham     uint32_t max_opcode_size =
105*e4598dc0SJim Ingham         GetTarget().GetArchitecture().GetMaximumOpcodeByteSize();
10686aaa8a2SBoris Ulasevich     bool next_instruction_reached = (pc > m_instruction_addr) &&
10786aaa8a2SBoris Ulasevich         (pc <= m_instruction_addr + max_opcode_size);
10886aaa8a2SBoris Ulasevich     if (next_instruction_reached) {
10986aaa8a2SBoris Ulasevich       SetPlanComplete();
11086aaa8a2SBoris Ulasevich     }
111*e4598dc0SJim Ingham     return (thread.GetRegisterContext()->GetPC(0) != m_instruction_addr);
112b9c1b51eSKate Stone   } else if (cur_frame_id < m_stack_id) {
113b9c1b51eSKate Stone     // If the current frame is younger than the start frame and we are stepping
11405097246SAdrian Prantl     // over, then we need to continue, but if we are doing just one step, we're
11505097246SAdrian Prantl     // done.
116e65b2cf2SEugene Zelenko     return !m_step_over;
117b9c1b51eSKate Stone   } else {
118b9c1b51eSKate Stone     if (log) {
11963e5fb76SJonas Devlieghere       LLDB_LOGF(log,
12063e5fb76SJonas Devlieghere                 "ThreadPlanStepInstruction::IsPlanStale - Current frame is "
121b9c1b51eSKate Stone                 "older than start frame, plan is stale.");
1227a88ec9aSJim Ingham     }
1237a88ec9aSJim Ingham     return true;
1247a88ec9aSJim Ingham   }
1257a88ec9aSJim Ingham }
1267a88ec9aSJim Ingham 
127b9c1b51eSKate Stone bool ThreadPlanStepInstruction::ShouldStop(Event *event_ptr) {
128*e4598dc0SJim Ingham   Thread &thread = GetThread();
129b9c1b51eSKate Stone   if (m_step_over) {
1305160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
131*e4598dc0SJim Ingham     StackFrameSP cur_frame_sp = thread.GetStackFrameAtIndex(0);
132b9c1b51eSKate Stone     if (!cur_frame_sp) {
13363e5fb76SJonas Devlieghere       LLDB_LOGF(
13463e5fb76SJonas Devlieghere           log,
135b9c1b51eSKate Stone           "ThreadPlanStepInstruction couldn't get the 0th frame, stopping.");
136c0b4d5a1SJim Ingham       SetPlanComplete();
137c0b4d5a1SJim Ingham       return true;
138c0b4d5a1SJim Ingham     }
139c0b4d5a1SJim Ingham 
140c0b4d5a1SJim Ingham     StackID cur_frame_zero_id = cur_frame_sp->GetStackID();
1416b35c86fSJim Ingham 
142b9c1b51eSKate Stone     if (cur_frame_zero_id == m_stack_id || m_stack_id < cur_frame_zero_id) {
143*e4598dc0SJim Ingham       if (thread.GetRegisterContext()->GetPC(0) != m_instruction_addr) {
144b9c1b51eSKate Stone         if (--m_iteration_count <= 0) {
14530fdc8d8SChris Lattner           SetPlanComplete();
14630fdc8d8SChris Lattner           return true;
147b9c1b51eSKate Stone         } else {
148b9c1b51eSKate Stone           // We are still stepping, reset the start pc, and in case we've
14905097246SAdrian Prantl           // stepped out, reset the current stack id.
1507a88ec9aSJim Ingham           SetUpState();
1517a88ec9aSJim Ingham           return false;
1527a88ec9aSJim Ingham         }
153b9c1b51eSKate Stone       } else
15430fdc8d8SChris Lattner         return false;
155b9c1b51eSKate Stone     } else {
15630fdc8d8SChris Lattner       // We've stepped in, step back out again:
157*e4598dc0SJim Ingham       StackFrame *return_frame = thread.GetStackFrameAtIndex(1).get();
158b9c1b51eSKate Stone       if (return_frame) {
159b9c1b51eSKate Stone         if (return_frame->GetStackID() != m_parent_frame_id ||
160b9c1b51eSKate Stone             m_start_has_symbol) {
161b9c1b51eSKate Stone           // next-instruction shouldn't step out of inlined functions.  But we
16205097246SAdrian Prantl           // may have stepped into a real function that starts with an inlined
16305097246SAdrian Prantl           // function, and we do want to step out of that...
164c0b4d5a1SJim Ingham 
165b9c1b51eSKate Stone           if (cur_frame_sp->IsInlined()) {
166b9c1b51eSKate Stone             StackFrameSP parent_frame_sp =
167*e4598dc0SJim Ingham                 thread.GetFrameWithStackID(m_stack_id);
168c0b4d5a1SJim Ingham 
169b9c1b51eSKate Stone             if (parent_frame_sp &&
170b9c1b51eSKate Stone                 parent_frame_sp->GetConcreteFrameIndex() ==
171b9c1b51eSKate Stone                     cur_frame_sp->GetConcreteFrameIndex()) {
172c0b4d5a1SJim Ingham               SetPlanComplete();
173b9c1b51eSKate Stone               if (log) {
17463e5fb76SJonas Devlieghere                 LLDB_LOGF(log,
17563e5fb76SJonas Devlieghere                           "Frame we stepped into is inlined into the frame "
176b9c1b51eSKate Stone                           "we were stepping from, stopping.");
177c0b4d5a1SJim Ingham               }
178c0b4d5a1SJim Ingham               return true;
179c0b4d5a1SJim Ingham             }
180c0b4d5a1SJim Ingham           }
181c0b4d5a1SJim Ingham 
182b9c1b51eSKate Stone           if (log) {
18330fdc8d8SChris Lattner             StreamString s;
18430fdc8d8SChris Lattner             s.PutCString("Stepped in to: ");
185b9c1b51eSKate Stone             addr_t stop_addr =
186*e4598dc0SJim Ingham                 thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
1871462f5a4SRaphael Isemann             DumpAddress(s.AsRawOstream(), stop_addr,
188*e4598dc0SJim Ingham                         GetTarget().GetArchitecture().GetAddressByteSize());
18930fdc8d8SChris Lattner             s.PutCString(" stepping out to: ");
1909da7bd07SGreg Clayton             addr_t return_addr = return_frame->GetRegisterContext()->GetPC();
1911462f5a4SRaphael Isemann             DumpAddress(s.AsRawOstream(), return_addr,
192*e4598dc0SJim Ingham                         GetTarget().GetArchitecture().GetAddressByteSize());
19363e5fb76SJonas Devlieghere             LLDB_LOGF(log, "%s.", s.GetData());
19430fdc8d8SChris Lattner           }
1954a58e968SJim Ingham 
19605097246SAdrian Prantl           // StepInstruction should probably have the tri-state RunMode, but
19705097246SAdrian Prantl           // for now it is safer to run others.
1984a58e968SJim Ingham           const bool stop_others = false;
199*e4598dc0SJim Ingham           thread.QueueThreadPlanForStepOutNoShouldStop(
200e103ae92SJonas Devlieghere               false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0,
201e103ae92SJonas Devlieghere               m_status);
20230fdc8d8SChris Lattner           return false;
203b9c1b51eSKate Stone         } else {
204b9c1b51eSKate Stone           if (log) {
205b9c1b51eSKate Stone             log->PutCString(
206b9c1b51eSKate Stone                 "The stack id we are stepping in changed, but our parent frame "
207b9c1b51eSKate Stone                 "did not when stepping from code with no symbols.  "
208886a3e2cSJim Ingham                 "We are probably just confused about where we are, stopping.");
209886a3e2cSJim Ingham           }
210886a3e2cSJim Ingham           SetPlanComplete();
211886a3e2cSJim Ingham           return true;
212886a3e2cSJim Ingham         }
213b9c1b51eSKate Stone       } else {
21463e5fb76SJonas Devlieghere         LLDB_LOGF(log, "Could not find previous frame, stopping.");
21530fdc8d8SChris Lattner         SetPlanComplete();
21630fdc8d8SChris Lattner         return true;
21730fdc8d8SChris Lattner       }
21830fdc8d8SChris Lattner     }
219b9c1b51eSKate Stone   } else {
220*e4598dc0SJim Ingham     lldb::addr_t pc_addr = thread.GetRegisterContext()->GetPC(0);
221b9c1b51eSKate Stone     if (pc_addr != m_instruction_addr) {
222b9c1b51eSKate Stone       if (--m_iteration_count <= 0) {
22330fdc8d8SChris Lattner         SetPlanComplete();
22430fdc8d8SChris Lattner         return true;
225b9c1b51eSKate Stone       } else {
226b9c1b51eSKate Stone         // We are still stepping, reset the start pc, and in case we've stepped
22705097246SAdrian Prantl         // in or out, reset the current stack id.
2287a88ec9aSJim Ingham         SetUpState();
2297a88ec9aSJim Ingham         return false;
2307a88ec9aSJim Ingham       }
231b9c1b51eSKate Stone     } else
23230fdc8d8SChris Lattner       return false;
23330fdc8d8SChris Lattner   }
23430fdc8d8SChris Lattner }
23530fdc8d8SChris Lattner 
236b9c1b51eSKate Stone bool ThreadPlanStepInstruction::StopOthers() { return m_stop_other_threads; }
23730fdc8d8SChris Lattner 
238b9c1b51eSKate Stone StateType ThreadPlanStepInstruction::GetPlanRunState() {
23930fdc8d8SChris Lattner   return eStateStepping;
24030fdc8d8SChris Lattner }
24130fdc8d8SChris Lattner 
242b9c1b51eSKate Stone bool ThreadPlanStepInstruction::WillStop() { return true; }
24330fdc8d8SChris Lattner 
244b9c1b51eSKate Stone bool ThreadPlanStepInstruction::MischiefManaged() {
245b9c1b51eSKate Stone   if (IsPlanComplete()) {
2465160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
24763e5fb76SJonas Devlieghere     LLDB_LOGF(log, "Completed single instruction step plan.");
24830fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
24930fdc8d8SChris Lattner     return true;
250b9c1b51eSKate Stone   } else {
25130fdc8d8SChris Lattner     return false;
25230fdc8d8SChris Lattner   }
25330fdc8d8SChris Lattner }
254