130fdc8d8SChris Lattner //===-- ThreadPlanStepInstruction.cpp ---------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler 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 //----------------------------------------------------------------------
2230fdc8d8SChris Lattner // ThreadPlanStepInstruction: Step over the current instruction
2330fdc8d8SChris Lattner //----------------------------------------------------------------------
2430fdc8d8SChris Lattner 
25b9c1b51eSKate Stone ThreadPlanStepInstruction::ThreadPlanStepInstruction(Thread &thread,
2630fdc8d8SChris Lattner                                                      bool step_over,
2730fdc8d8SChris Lattner                                                      bool stop_other_threads,
2830fdc8d8SChris Lattner                                                      Vote stop_vote,
29b9c1b51eSKate Stone                                                      Vote run_vote)
30b9c1b51eSKate Stone     : ThreadPlan(ThreadPlan::eKindStepInstruction,
31b9c1b51eSKate Stone                  "Step over single instruction", thread, stop_vote, run_vote),
32b9c1b51eSKate Stone       m_instruction_addr(0), m_stop_other_threads(stop_other_threads),
33b9c1b51eSKate Stone       m_step_over(step_over) {
347a88ec9aSJim Ingham   m_takes_iteration_count = true;
357a88ec9aSJim Ingham   SetUpState();
3630fdc8d8SChris Lattner }
3730fdc8d8SChris Lattner 
38e65b2cf2SEugene Zelenko ThreadPlanStepInstruction::~ThreadPlanStepInstruction() = default;
3930fdc8d8SChris Lattner 
40b9c1b51eSKate Stone void ThreadPlanStepInstruction::SetUpState() {
417a88ec9aSJim Ingham   m_instruction_addr = m_thread.GetRegisterContext()->GetPC(0);
427a88ec9aSJim Ingham   StackFrameSP start_frame_sp(m_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 
487a88ec9aSJim Ingham   StackFrameSP parent_frame_sp = m_thread.GetStackFrameAtIndex(1);
497a88ec9aSJim Ingham   if (parent_frame_sp)
507a88ec9aSJim Ingham     m_parent_frame_id = parent_frame_sp->GetStackID();
517a88ec9aSJim Ingham }
527a88ec9aSJim Ingham 
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 ");
7030fdc8d8SChris Lattner     s->Address(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 
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 
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 
98b9c1b51eSKate Stone bool ThreadPlanStepInstruction::IsPlanStale() {
997a88ec9aSJim Ingham   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1007a88ec9aSJim Ingham   StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
101b9c1b51eSKate Stone   if (cur_frame_id == m_stack_id) {
10286aaa8a2SBoris Ulasevich     // Set plan Complete when we reach next instruction
10386aaa8a2SBoris Ulasevich     uint64_t pc = m_thread.GetRegisterContext()->GetPC(0);
10486aaa8a2SBoris Ulasevich     uint32_t max_opcode_size = m_thread.CalculateTarget()
10586aaa8a2SBoris Ulasevich         ->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     }
111e65b2cf2SEugene Zelenko     return (m_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) {
119b9c1b51eSKate Stone       log->Printf("ThreadPlanStepInstruction::IsPlanStale - Current frame is "
120b9c1b51eSKate Stone                   "older than start frame, plan is stale.");
1217a88ec9aSJim Ingham     }
1227a88ec9aSJim Ingham     return true;
1237a88ec9aSJim Ingham   }
1247a88ec9aSJim Ingham }
1257a88ec9aSJim Ingham 
126b9c1b51eSKate Stone bool ThreadPlanStepInstruction::ShouldStop(Event *event_ptr) {
127b9c1b51eSKate Stone   if (m_step_over) {
1285160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1296b35c86fSJim Ingham 
130c0b4d5a1SJim Ingham     StackFrameSP cur_frame_sp = m_thread.GetStackFrameAtIndex(0);
131b9c1b51eSKate Stone     if (!cur_frame_sp) {
132c0b4d5a1SJim Ingham       if (log)
133b9c1b51eSKate Stone         log->Printf(
134b9c1b51eSKate Stone             "ThreadPlanStepInstruction couldn't get the 0th frame, stopping.");
135c0b4d5a1SJim Ingham       SetPlanComplete();
136c0b4d5a1SJim Ingham       return true;
137c0b4d5a1SJim Ingham     }
138c0b4d5a1SJim Ingham 
139c0b4d5a1SJim Ingham     StackID cur_frame_zero_id = cur_frame_sp->GetStackID();
1406b35c86fSJim Ingham 
141b9c1b51eSKate Stone     if (cur_frame_zero_id == m_stack_id || m_stack_id < cur_frame_zero_id) {
142b9c1b51eSKate Stone       if (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr) {
143b9c1b51eSKate Stone         if (--m_iteration_count <= 0) {
14430fdc8d8SChris Lattner           SetPlanComplete();
14530fdc8d8SChris Lattner           return true;
146b9c1b51eSKate Stone         } else {
147b9c1b51eSKate Stone           // We are still stepping, reset the start pc, and in case we've
14805097246SAdrian Prantl           // stepped out, reset the current stack id.
1497a88ec9aSJim Ingham           SetUpState();
1507a88ec9aSJim Ingham           return false;
1517a88ec9aSJim Ingham         }
152b9c1b51eSKate Stone       } else
15330fdc8d8SChris Lattner         return false;
154b9c1b51eSKate Stone     } else {
15530fdc8d8SChris Lattner       // We've stepped in, step back out again:
156b57e4a1bSJason Molenda       StackFrame *return_frame = m_thread.GetStackFrameAtIndex(1).get();
157b9c1b51eSKate Stone       if (return_frame) {
158b9c1b51eSKate Stone         if (return_frame->GetStackID() != m_parent_frame_id ||
159b9c1b51eSKate Stone             m_start_has_symbol) {
160b9c1b51eSKate Stone           // next-instruction shouldn't step out of inlined functions.  But we
16105097246SAdrian Prantl           // may have stepped into a real function that starts with an inlined
16205097246SAdrian Prantl           // function, and we do want to step out of that...
163c0b4d5a1SJim Ingham 
164b9c1b51eSKate Stone           if (cur_frame_sp->IsInlined()) {
165b9c1b51eSKate Stone             StackFrameSP parent_frame_sp =
166b9c1b51eSKate Stone                 m_thread.GetFrameWithStackID(m_stack_id);
167c0b4d5a1SJim Ingham 
168b9c1b51eSKate Stone             if (parent_frame_sp &&
169b9c1b51eSKate Stone                 parent_frame_sp->GetConcreteFrameIndex() ==
170b9c1b51eSKate Stone                     cur_frame_sp->GetConcreteFrameIndex()) {
171c0b4d5a1SJim Ingham               SetPlanComplete();
172b9c1b51eSKate Stone               if (log) {
173b9c1b51eSKate Stone                 log->Printf("Frame we stepped into is inlined into the frame "
174b9c1b51eSKate Stone                             "we were stepping from, stopping.");
175c0b4d5a1SJim Ingham               }
176c0b4d5a1SJim Ingham               return true;
177c0b4d5a1SJim Ingham             }
178c0b4d5a1SJim Ingham           }
179c0b4d5a1SJim Ingham 
180b9c1b51eSKate Stone           if (log) {
18130fdc8d8SChris Lattner             StreamString s;
18230fdc8d8SChris Lattner             s.PutCString("Stepped in to: ");
183b9c1b51eSKate Stone             addr_t stop_addr =
184b9c1b51eSKate Stone                 m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
185b9c1b51eSKate Stone             s.Address(stop_addr, m_thread.CalculateTarget()
186b9c1b51eSKate Stone                                      ->GetArchitecture()
187b9c1b51eSKate Stone                                      .GetAddressByteSize());
18830fdc8d8SChris Lattner             s.PutCString(" stepping out to: ");
1899da7bd07SGreg Clayton             addr_t return_addr = return_frame->GetRegisterContext()->GetPC();
190b9c1b51eSKate Stone             s.Address(return_addr, m_thread.CalculateTarget()
191b9c1b51eSKate Stone                                        ->GetArchitecture()
192b9c1b51eSKate Stone                                        .GetAddressByteSize());
19330fdc8d8SChris Lattner             log->Printf("%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;
199b9c1b51eSKate Stone           m_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 {
214886a3e2cSJim Ingham         if (log)
21530fdc8d8SChris Lattner           log->Printf("Could not find previous frame, stopping.");
21630fdc8d8SChris Lattner         SetPlanComplete();
21730fdc8d8SChris Lattner         return true;
21830fdc8d8SChris Lattner       }
21930fdc8d8SChris Lattner     }
220b9c1b51eSKate Stone   } else {
22122eeb722SJim Ingham     lldb::addr_t pc_addr = m_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 
237b9c1b51eSKate Stone bool ThreadPlanStepInstruction::StopOthers() { return m_stop_other_threads; }
23830fdc8d8SChris Lattner 
239b9c1b51eSKate Stone StateType ThreadPlanStepInstruction::GetPlanRunState() {
24030fdc8d8SChris Lattner   return eStateStepping;
24130fdc8d8SChris Lattner }
24230fdc8d8SChris Lattner 
243b9c1b51eSKate Stone bool ThreadPlanStepInstruction::WillStop() { return true; }
24430fdc8d8SChris Lattner 
245b9c1b51eSKate Stone bool ThreadPlanStepInstruction::MischiefManaged() {
246b9c1b51eSKate Stone   if (IsPlanComplete()) {
2475160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
24830fdc8d8SChris Lattner     if (log)
24930fdc8d8SChris Lattner       log->Printf("Completed single instruction step plan.");
25030fdc8d8SChris Lattner     ThreadPlan::MischiefManaged();
25130fdc8d8SChris Lattner     return true;
252b9c1b51eSKate Stone   } else {
25330fdc8d8SChris Lattner     return false;
25430fdc8d8SChris Lattner   }
25530fdc8d8SChris Lattner }
256