130fdc8d8SChris Lattner //===-- ThreadPlanStepInstruction.cpp ---------------------------*- C++ -*-===// 230fdc8d8SChris Lattner // 330fdc8d8SChris Lattner // The LLVM Compiler Infrastructure 430fdc8d8SChris Lattner // 530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source 630fdc8d8SChris Lattner // License. See LICENSE.TXT for details. 730fdc8d8SChris Lattner // 830fdc8d8SChris Lattner //===----------------------------------------------------------------------===// 930fdc8d8SChris Lattner 1030fdc8d8SChris Lattner // C Includes 1130fdc8d8SChris Lattner // C++ Includes 1230fdc8d8SChris Lattner // Other libraries and framework includes 1330fdc8d8SChris Lattner // Project includes 14e65b2cf2SEugene Zelenko #include "lldb/Target/ThreadPlanStepInstruction.h" 1530fdc8d8SChris Lattner #include "lldb/Core/Log.h" 1630fdc8d8SChris Lattner #include "lldb/Target/Process.h" 17f4b47e15SGreg Clayton #include "lldb/Target/RegisterContext.h" 18f4b47e15SGreg Clayton #include "lldb/Target/RegisterContext.h" 19f4b47e15SGreg Clayton #include "lldb/Target/StopInfo.h" 20f4b47e15SGreg Clayton #include "lldb/Target/Target.h" 21*bf9a7730SZachary Turner #include "lldb/Utility/Stream.h" 2230fdc8d8SChris Lattner 2330fdc8d8SChris Lattner using namespace lldb; 2430fdc8d8SChris Lattner using namespace lldb_private; 2530fdc8d8SChris Lattner 2630fdc8d8SChris Lattner //---------------------------------------------------------------------- 2730fdc8d8SChris Lattner // ThreadPlanStepInstruction: Step over the current instruction 2830fdc8d8SChris Lattner //---------------------------------------------------------------------- 2930fdc8d8SChris Lattner 30b9c1b51eSKate Stone ThreadPlanStepInstruction::ThreadPlanStepInstruction(Thread &thread, 3130fdc8d8SChris Lattner bool step_over, 3230fdc8d8SChris Lattner bool stop_other_threads, 3330fdc8d8SChris Lattner Vote stop_vote, 34b9c1b51eSKate Stone Vote run_vote) 35b9c1b51eSKate Stone : ThreadPlan(ThreadPlan::eKindStepInstruction, 36b9c1b51eSKate Stone "Step over single instruction", thread, stop_vote, run_vote), 37b9c1b51eSKate Stone m_instruction_addr(0), m_stop_other_threads(stop_other_threads), 38b9c1b51eSKate Stone m_step_over(step_over) { 397a88ec9aSJim Ingham m_takes_iteration_count = true; 407a88ec9aSJim Ingham SetUpState(); 4130fdc8d8SChris Lattner } 4230fdc8d8SChris Lattner 43e65b2cf2SEugene Zelenko ThreadPlanStepInstruction::~ThreadPlanStepInstruction() = default; 4430fdc8d8SChris Lattner 45b9c1b51eSKate Stone void ThreadPlanStepInstruction::SetUpState() { 467a88ec9aSJim Ingham m_instruction_addr = m_thread.GetRegisterContext()->GetPC(0); 477a88ec9aSJim Ingham StackFrameSP start_frame_sp(m_thread.GetStackFrameAtIndex(0)); 487a88ec9aSJim Ingham m_stack_id = start_frame_sp->GetStackID(); 497a88ec9aSJim Ingham 50b9c1b51eSKate Stone m_start_has_symbol = 51b9c1b51eSKate Stone start_frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol != nullptr; 527a88ec9aSJim Ingham 537a88ec9aSJim Ingham StackFrameSP parent_frame_sp = m_thread.GetStackFrameAtIndex(1); 547a88ec9aSJim Ingham if (parent_frame_sp) 557a88ec9aSJim Ingham m_parent_frame_id = parent_frame_sp->GetStackID(); 567a88ec9aSJim Ingham } 577a88ec9aSJim Ingham 58b9c1b51eSKate Stone void ThreadPlanStepInstruction::GetDescription(Stream *s, 59b9c1b51eSKate Stone lldb::DescriptionLevel level) { 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"); 65b9c1b51eSKate Stone } else { 6630fdc8d8SChris Lattner s->Printf("Stepping one instruction past "); 6730fdc8d8SChris Lattner s->Address(m_instruction_addr, sizeof(addr_t)); 68a7d4822cSJim Ingham if (!m_start_has_symbol) 69a7d4822cSJim Ingham s->Printf(" which has no symbol"); 70a7d4822cSJim Ingham 7130fdc8d8SChris Lattner if (m_step_over) 7230fdc8d8SChris Lattner s->Printf(" stepping over calls"); 7330fdc8d8SChris Lattner else 7430fdc8d8SChris Lattner s->Printf(" stepping into calls"); 7530fdc8d8SChris Lattner } 7630fdc8d8SChris Lattner } 7730fdc8d8SChris Lattner 78b9c1b51eSKate Stone bool ThreadPlanStepInstruction::ValidatePlan(Stream *error) { 7930fdc8d8SChris Lattner // Since we read the instruction we're stepping over from the thread, 8030fdc8d8SChris Lattner // this plan will always work. 8130fdc8d8SChris Lattner return true; 8230fdc8d8SChris Lattner } 8330fdc8d8SChris Lattner 84b9c1b51eSKate Stone bool ThreadPlanStepInstruction::DoPlanExplainsStop(Event *event_ptr) { 8560c4118cSJim Ingham StopInfoSP stop_info_sp = GetPrivateStopInfo(); 86b9c1b51eSKate Stone if (stop_info_sp) { 87b15bfc75SJim Ingham StopReason reason = stop_info_sp->GetStopReason(); 88e65b2cf2SEugene Zelenko return (reason == eStopReasonTrace || reason == eStopReasonNone); 8930fdc8d8SChris Lattner } 9030fdc8d8SChris Lattner return false; 9130fdc8d8SChris Lattner } 9230fdc8d8SChris Lattner 93b9c1b51eSKate Stone bool ThreadPlanStepInstruction::IsPlanStale() { 947a88ec9aSJim Ingham Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 957a88ec9aSJim Ingham StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID(); 96b9c1b51eSKate Stone if (cur_frame_id == m_stack_id) { 97e65b2cf2SEugene Zelenko return (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr); 98b9c1b51eSKate Stone } else if (cur_frame_id < m_stack_id) { 99b9c1b51eSKate Stone // If the current frame is younger than the start frame and we are stepping 100b9c1b51eSKate Stone // over, then we need to continue, 1017a88ec9aSJim Ingham // but if we are doing just one step, we're done. 102e65b2cf2SEugene Zelenko return !m_step_over; 103b9c1b51eSKate Stone } else { 104b9c1b51eSKate Stone if (log) { 105b9c1b51eSKate Stone log->Printf("ThreadPlanStepInstruction::IsPlanStale - Current frame is " 106b9c1b51eSKate Stone "older than start frame, plan is stale."); 1077a88ec9aSJim Ingham } 1087a88ec9aSJim Ingham return true; 1097a88ec9aSJim Ingham } 1107a88ec9aSJim Ingham } 1117a88ec9aSJim Ingham 112b9c1b51eSKate Stone bool ThreadPlanStepInstruction::ShouldStop(Event *event_ptr) { 113b9c1b51eSKate Stone if (m_step_over) { 1145160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 1156b35c86fSJim Ingham 116c0b4d5a1SJim Ingham StackFrameSP cur_frame_sp = m_thread.GetStackFrameAtIndex(0); 117b9c1b51eSKate Stone if (!cur_frame_sp) { 118c0b4d5a1SJim Ingham if (log) 119b9c1b51eSKate Stone log->Printf( 120b9c1b51eSKate Stone "ThreadPlanStepInstruction couldn't get the 0th frame, stopping."); 121c0b4d5a1SJim Ingham SetPlanComplete(); 122c0b4d5a1SJim Ingham return true; 123c0b4d5a1SJim Ingham } 124c0b4d5a1SJim Ingham 125c0b4d5a1SJim Ingham StackID cur_frame_zero_id = cur_frame_sp->GetStackID(); 1266b35c86fSJim Ingham 127b9c1b51eSKate Stone if (cur_frame_zero_id == m_stack_id || m_stack_id < cur_frame_zero_id) { 128b9c1b51eSKate Stone if (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr) { 129b9c1b51eSKate Stone if (--m_iteration_count <= 0) { 13030fdc8d8SChris Lattner SetPlanComplete(); 13130fdc8d8SChris Lattner return true; 132b9c1b51eSKate Stone } else { 133b9c1b51eSKate Stone // We are still stepping, reset the start pc, and in case we've 134b9c1b51eSKate Stone // stepped out, 1357a88ec9aSJim Ingham // reset the current stack id. 1367a88ec9aSJim Ingham SetUpState(); 1377a88ec9aSJim Ingham return false; 1387a88ec9aSJim Ingham } 139b9c1b51eSKate Stone } else 14030fdc8d8SChris Lattner return false; 141b9c1b51eSKate Stone } else { 14230fdc8d8SChris Lattner // We've stepped in, step back out again: 143b57e4a1bSJason Molenda StackFrame *return_frame = m_thread.GetStackFrameAtIndex(1).get(); 144b9c1b51eSKate Stone if (return_frame) { 145b9c1b51eSKate Stone if (return_frame->GetStackID() != m_parent_frame_id || 146b9c1b51eSKate Stone m_start_has_symbol) { 147b9c1b51eSKate Stone // next-instruction shouldn't step out of inlined functions. But we 148b9c1b51eSKate Stone // may have stepped into a 149b9c1b51eSKate Stone // real function that starts with an inlined function, and we do want 150b9c1b51eSKate Stone // to step out of that... 151c0b4d5a1SJim Ingham 152b9c1b51eSKate Stone if (cur_frame_sp->IsInlined()) { 153b9c1b51eSKate Stone StackFrameSP parent_frame_sp = 154b9c1b51eSKate Stone m_thread.GetFrameWithStackID(m_stack_id); 155c0b4d5a1SJim Ingham 156b9c1b51eSKate Stone if (parent_frame_sp && 157b9c1b51eSKate Stone parent_frame_sp->GetConcreteFrameIndex() == 158b9c1b51eSKate Stone cur_frame_sp->GetConcreteFrameIndex()) { 159c0b4d5a1SJim Ingham SetPlanComplete(); 160b9c1b51eSKate Stone if (log) { 161b9c1b51eSKate Stone log->Printf("Frame we stepped into is inlined into the frame " 162b9c1b51eSKate Stone "we were stepping from, stopping."); 163c0b4d5a1SJim Ingham } 164c0b4d5a1SJim Ingham return true; 165c0b4d5a1SJim Ingham } 166c0b4d5a1SJim Ingham } 167c0b4d5a1SJim Ingham 168b9c1b51eSKate Stone if (log) { 16930fdc8d8SChris Lattner StreamString s; 17030fdc8d8SChris Lattner s.PutCString("Stepped in to: "); 171b9c1b51eSKate Stone addr_t stop_addr = 172b9c1b51eSKate Stone m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC(); 173b9c1b51eSKate Stone s.Address(stop_addr, m_thread.CalculateTarget() 174b9c1b51eSKate Stone ->GetArchitecture() 175b9c1b51eSKate Stone .GetAddressByteSize()); 17630fdc8d8SChris Lattner s.PutCString(" stepping out to: "); 1779da7bd07SGreg Clayton addr_t return_addr = return_frame->GetRegisterContext()->GetPC(); 178b9c1b51eSKate Stone s.Address(return_addr, m_thread.CalculateTarget() 179b9c1b51eSKate Stone ->GetArchitecture() 180b9c1b51eSKate Stone .GetAddressByteSize()); 18130fdc8d8SChris Lattner log->Printf("%s.", s.GetData()); 18230fdc8d8SChris Lattner } 1834a58e968SJim Ingham 184b9c1b51eSKate Stone // StepInstruction should probably have the tri-state RunMode, but for 185b9c1b51eSKate Stone // now it is safer to 1864a58e968SJim Ingham // run others. 1874a58e968SJim Ingham const bool stop_others = false; 188b9c1b51eSKate Stone m_thread.QueueThreadPlanForStepOutNoShouldStop( 189b9c1b51eSKate Stone false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0); 19030fdc8d8SChris Lattner return false; 191b9c1b51eSKate Stone } else { 192b9c1b51eSKate Stone if (log) { 193b9c1b51eSKate Stone log->PutCString( 194b9c1b51eSKate Stone "The stack id we are stepping in changed, but our parent frame " 195b9c1b51eSKate Stone "did not when stepping from code with no symbols. " 196886a3e2cSJim Ingham "We are probably just confused about where we are, stopping."); 197886a3e2cSJim Ingham } 198886a3e2cSJim Ingham SetPlanComplete(); 199886a3e2cSJim Ingham return true; 200886a3e2cSJim Ingham } 201b9c1b51eSKate Stone } else { 202886a3e2cSJim Ingham if (log) 20330fdc8d8SChris Lattner log->Printf("Could not find previous frame, stopping."); 20430fdc8d8SChris Lattner SetPlanComplete(); 20530fdc8d8SChris Lattner return true; 20630fdc8d8SChris Lattner } 20730fdc8d8SChris Lattner } 208b9c1b51eSKate Stone } else { 20922eeb722SJim Ingham lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC(0); 210b9c1b51eSKate Stone if (pc_addr != m_instruction_addr) { 211b9c1b51eSKate Stone if (--m_iteration_count <= 0) { 21230fdc8d8SChris Lattner SetPlanComplete(); 21330fdc8d8SChris Lattner return true; 214b9c1b51eSKate Stone } else { 215b9c1b51eSKate Stone // We are still stepping, reset the start pc, and in case we've stepped 216b9c1b51eSKate Stone // in or out, 2177a88ec9aSJim Ingham // reset the current stack id. 2187a88ec9aSJim Ingham SetUpState(); 2197a88ec9aSJim Ingham return false; 2207a88ec9aSJim Ingham } 221b9c1b51eSKate Stone } else 22230fdc8d8SChris Lattner return false; 22330fdc8d8SChris Lattner } 22430fdc8d8SChris Lattner } 22530fdc8d8SChris Lattner 226b9c1b51eSKate Stone bool ThreadPlanStepInstruction::StopOthers() { return m_stop_other_threads; } 22730fdc8d8SChris Lattner 228b9c1b51eSKate Stone StateType ThreadPlanStepInstruction::GetPlanRunState() { 22930fdc8d8SChris Lattner return eStateStepping; 23030fdc8d8SChris Lattner } 23130fdc8d8SChris Lattner 232b9c1b51eSKate Stone bool ThreadPlanStepInstruction::WillStop() { return true; } 23330fdc8d8SChris Lattner 234b9c1b51eSKate Stone bool ThreadPlanStepInstruction::MischiefManaged() { 235b9c1b51eSKate Stone if (IsPlanComplete()) { 2365160ce5cSGreg Clayton Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 23730fdc8d8SChris Lattner if (log) 23830fdc8d8SChris Lattner log->Printf("Completed single instruction step plan."); 23930fdc8d8SChris Lattner ThreadPlan::MischiefManaged(); 24030fdc8d8SChris Lattner return true; 241b9c1b51eSKate Stone } else { 24230fdc8d8SChris Lattner return false; 24330fdc8d8SChris Lattner } 24430fdc8d8SChris Lattner } 245