130fdc8d8SChris Lattner //===-- ThreadPlanStepInstruction.cpp ---------------------------*- C++ -*-===// 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() { 397a88ec9aSJim Ingham m_instruction_addr = m_thread.GetRegisterContext()->GetPC(0); 407a88ec9aSJim Ingham StackFrameSP start_frame_sp(m_thread.GetStackFrameAtIndex(0)); 417a88ec9aSJim Ingham m_stack_id = start_frame_sp->GetStackID(); 427a88ec9aSJim Ingham 43b9c1b51eSKate Stone m_start_has_symbol = 44b9c1b51eSKate Stone start_frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol != nullptr; 457a88ec9aSJim Ingham 467a88ec9aSJim Ingham StackFrameSP parent_frame_sp = m_thread.GetStackFrameAtIndex(1); 477a88ec9aSJim Ingham if (parent_frame_sp) 487a88ec9aSJim Ingham m_parent_frame_id = parent_frame_sp->GetStackID(); 497a88ec9aSJim Ingham } 507a88ec9aSJim Ingham 51b9c1b51eSKate Stone void ThreadPlanStepInstruction::GetDescription(Stream *s, 52b9c1b51eSKate Stone lldb::DescriptionLevel level) { 53e103ae92SJonas Devlieghere auto PrintFailureIfAny = [&]() { 54e103ae92SJonas Devlieghere if (m_status.Success()) 55e103ae92SJonas Devlieghere return; 56e103ae92SJonas Devlieghere s->Printf(" failed (%s)", m_status.AsCString()); 57e103ae92SJonas Devlieghere }; 58e103ae92SJonas Devlieghere 59b9c1b51eSKate Stone if (level == lldb::eDescriptionLevelBrief) { 6030fdc8d8SChris Lattner if (m_step_over) 6130fdc8d8SChris Lattner s->Printf("instruction step over"); 6230fdc8d8SChris Lattner else 6330fdc8d8SChris Lattner s->Printf("instruction step into"); 64e103ae92SJonas Devlieghere 65e103ae92SJonas Devlieghere PrintFailureIfAny(); 66b9c1b51eSKate Stone } else { 6730fdc8d8SChris Lattner s->Printf("Stepping one instruction past "); 6830fdc8d8SChris Lattner s->Address(m_instruction_addr, sizeof(addr_t)); 69a7d4822cSJim Ingham if (!m_start_has_symbol) 70a7d4822cSJim Ingham s->Printf(" which has no symbol"); 71a7d4822cSJim Ingham 7230fdc8d8SChris Lattner if (m_step_over) 7330fdc8d8SChris Lattner s->Printf(" stepping over calls"); 7430fdc8d8SChris Lattner else 7530fdc8d8SChris Lattner s->Printf(" stepping into calls"); 76e103ae92SJonas Devlieghere 77e103ae92SJonas Devlieghere PrintFailureIfAny(); 7830fdc8d8SChris Lattner } 7930fdc8d8SChris Lattner } 8030fdc8d8SChris Lattner 81b9c1b51eSKate Stone bool ThreadPlanStepInstruction::ValidatePlan(Stream *error) { 8205097246SAdrian Prantl // Since we read the instruction we're stepping over from the thread, this 8305097246SAdrian Prantl // plan will always work. 8430fdc8d8SChris Lattner return true; 8530fdc8d8SChris Lattner } 8630fdc8d8SChris Lattner 87b9c1b51eSKate Stone bool ThreadPlanStepInstruction::DoPlanExplainsStop(Event *event_ptr) { 8860c4118cSJim Ingham StopInfoSP stop_info_sp = GetPrivateStopInfo(); 89b9c1b51eSKate Stone if (stop_info_sp) { 90b15bfc75SJim Ingham StopReason reason = stop_info_sp->GetStopReason(); 91e65b2cf2SEugene Zelenko return (reason == eStopReasonTrace || reason == eStopReasonNone); 9230fdc8d8SChris Lattner } 9330fdc8d8SChris Lattner return false; 9430fdc8d8SChris Lattner } 9530fdc8d8SChris Lattner 96b9c1b51eSKate Stone bool ThreadPlanStepInstruction::IsPlanStale() { 977a88ec9aSJim Ingham Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 987a88ec9aSJim Ingham StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID(); 99b9c1b51eSKate Stone if (cur_frame_id == m_stack_id) { 10086aaa8a2SBoris Ulasevich // Set plan Complete when we reach next instruction 10186aaa8a2SBoris Ulasevich uint64_t pc = m_thread.GetRegisterContext()->GetPC(0); 10286aaa8a2SBoris Ulasevich uint32_t max_opcode_size = m_thread.CalculateTarget() 10386aaa8a2SBoris Ulasevich ->GetArchitecture().GetMaximumOpcodeByteSize(); 10486aaa8a2SBoris Ulasevich bool next_instruction_reached = (pc > m_instruction_addr) && 10586aaa8a2SBoris Ulasevich (pc <= m_instruction_addr + max_opcode_size); 10686aaa8a2SBoris Ulasevich if (next_instruction_reached) { 10786aaa8a2SBoris Ulasevich SetPlanComplete(); 10886aaa8a2SBoris Ulasevich } 109e65b2cf2SEugene Zelenko return (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr); 110b9c1b51eSKate Stone } else if (cur_frame_id < m_stack_id) { 111b9c1b51eSKate Stone // If the current frame is younger than the start frame and we are stepping 11205097246SAdrian Prantl // over, then we need to continue, but if we are doing just one step, we're 11305097246SAdrian Prantl // done. 114e65b2cf2SEugene Zelenko return !m_step_over; 115b9c1b51eSKate Stone } else { 116b9c1b51eSKate Stone if (log) { 117*63e5fb76SJonas Devlieghere LLDB_LOGF(log, 118*63e5fb76SJonas Devlieghere "ThreadPlanStepInstruction::IsPlanStale - Current frame is " 119b9c1b51eSKate Stone "older than start frame, plan is stale."); 1207a88ec9aSJim Ingham } 1217a88ec9aSJim Ingham return true; 1227a88ec9aSJim Ingham } 1237a88ec9aSJim Ingham } 1247a88ec9aSJim Ingham 125b9c1b51eSKate Stone bool ThreadPlanStepInstruction::ShouldStop(Event *event_ptr) { 126b9c1b51eSKate Stone if (m_step_over) { 1275160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 1286b35c86fSJim Ingham 129c0b4d5a1SJim Ingham StackFrameSP cur_frame_sp = m_thread.GetStackFrameAtIndex(0); 130b9c1b51eSKate Stone if (!cur_frame_sp) { 131*63e5fb76SJonas Devlieghere LLDB_LOGF( 132*63e5fb76SJonas Devlieghere log, 133b9c1b51eSKate Stone "ThreadPlanStepInstruction couldn't get the 0th frame, stopping."); 134c0b4d5a1SJim Ingham SetPlanComplete(); 135c0b4d5a1SJim Ingham return true; 136c0b4d5a1SJim Ingham } 137c0b4d5a1SJim Ingham 138c0b4d5a1SJim Ingham StackID cur_frame_zero_id = cur_frame_sp->GetStackID(); 1396b35c86fSJim Ingham 140b9c1b51eSKate Stone if (cur_frame_zero_id == m_stack_id || m_stack_id < cur_frame_zero_id) { 141b9c1b51eSKate Stone if (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr) { 142b9c1b51eSKate Stone if (--m_iteration_count <= 0) { 14330fdc8d8SChris Lattner SetPlanComplete(); 14430fdc8d8SChris Lattner return true; 145b9c1b51eSKate Stone } else { 146b9c1b51eSKate Stone // We are still stepping, reset the start pc, and in case we've 14705097246SAdrian Prantl // stepped out, reset the current stack id. 1487a88ec9aSJim Ingham SetUpState(); 1497a88ec9aSJim Ingham return false; 1507a88ec9aSJim Ingham } 151b9c1b51eSKate Stone } else 15230fdc8d8SChris Lattner return false; 153b9c1b51eSKate Stone } else { 15430fdc8d8SChris Lattner // We've stepped in, step back out again: 155b57e4a1bSJason Molenda StackFrame *return_frame = m_thread.GetStackFrameAtIndex(1).get(); 156b9c1b51eSKate Stone if (return_frame) { 157b9c1b51eSKate Stone if (return_frame->GetStackID() != m_parent_frame_id || 158b9c1b51eSKate Stone m_start_has_symbol) { 159b9c1b51eSKate Stone // next-instruction shouldn't step out of inlined functions. But we 16005097246SAdrian Prantl // may have stepped into a real function that starts with an inlined 16105097246SAdrian Prantl // function, and we do want to step out of that... 162c0b4d5a1SJim Ingham 163b9c1b51eSKate Stone if (cur_frame_sp->IsInlined()) { 164b9c1b51eSKate Stone StackFrameSP parent_frame_sp = 165b9c1b51eSKate Stone m_thread.GetFrameWithStackID(m_stack_id); 166c0b4d5a1SJim Ingham 167b9c1b51eSKate Stone if (parent_frame_sp && 168b9c1b51eSKate Stone parent_frame_sp->GetConcreteFrameIndex() == 169b9c1b51eSKate Stone cur_frame_sp->GetConcreteFrameIndex()) { 170c0b4d5a1SJim Ingham SetPlanComplete(); 171b9c1b51eSKate Stone if (log) { 172*63e5fb76SJonas Devlieghere LLDB_LOGF(log, 173*63e5fb76SJonas Devlieghere "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()); 193*63e5fb76SJonas 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; 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 { 214*63e5fb76SJonas Devlieghere LLDB_LOGF(log, "Could not find previous frame, stopping."); 21530fdc8d8SChris Lattner SetPlanComplete(); 21630fdc8d8SChris Lattner return true; 21730fdc8d8SChris Lattner } 21830fdc8d8SChris Lattner } 219b9c1b51eSKate Stone } else { 22022eeb722SJim Ingham lldb::addr_t pc_addr = m_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)); 247*63e5fb76SJonas 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