1 //===-- ThreadPlanStepInstruction.cpp ---------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // C Includes 11 // C++ Includes 12 // Other libraries and framework includes 13 // Project includes 14 #include "lldb/Target/ThreadPlanStepInstruction.h" 15 #include "lldb/Core/Log.h" 16 #include "lldb/Core/Stream.h" 17 #include "lldb/Target/Process.h" 18 #include "lldb/Target/RegisterContext.h" 19 #include "lldb/Target/RegisterContext.h" 20 #include "lldb/Target/StopInfo.h" 21 #include "lldb/Target/Target.h" 22 23 using namespace lldb; 24 using namespace lldb_private; 25 26 //---------------------------------------------------------------------- 27 // ThreadPlanStepInstruction: Step over the current instruction 28 //---------------------------------------------------------------------- 29 30 ThreadPlanStepInstruction::ThreadPlanStepInstruction(Thread &thread, 31 bool step_over, 32 bool stop_other_threads, 33 Vote stop_vote, 34 Vote run_vote) 35 : ThreadPlan(ThreadPlan::eKindStepInstruction, 36 "Step over single instruction", thread, stop_vote, run_vote), 37 m_instruction_addr(0), m_stop_other_threads(stop_other_threads), 38 m_step_over(step_over) { 39 m_takes_iteration_count = true; 40 SetUpState(); 41 } 42 43 ThreadPlanStepInstruction::~ThreadPlanStepInstruction() = default; 44 45 void ThreadPlanStepInstruction::SetUpState() { 46 m_instruction_addr = m_thread.GetRegisterContext()->GetPC(0); 47 StackFrameSP start_frame_sp(m_thread.GetStackFrameAtIndex(0)); 48 m_stack_id = start_frame_sp->GetStackID(); 49 50 m_start_has_symbol = 51 start_frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol != nullptr; 52 53 StackFrameSP parent_frame_sp = m_thread.GetStackFrameAtIndex(1); 54 if (parent_frame_sp) 55 m_parent_frame_id = parent_frame_sp->GetStackID(); 56 } 57 58 void ThreadPlanStepInstruction::GetDescription(Stream *s, 59 lldb::DescriptionLevel level) { 60 if (level == lldb::eDescriptionLevelBrief) { 61 if (m_step_over) 62 s->Printf("instruction step over"); 63 else 64 s->Printf("instruction step into"); 65 } else { 66 s->Printf("Stepping one instruction past "); 67 s->Address(m_instruction_addr, sizeof(addr_t)); 68 if (!m_start_has_symbol) 69 s->Printf(" which has no symbol"); 70 71 if (m_step_over) 72 s->Printf(" stepping over calls"); 73 else 74 s->Printf(" stepping into calls"); 75 } 76 } 77 78 bool ThreadPlanStepInstruction::ValidatePlan(Stream *error) { 79 // Since we read the instruction we're stepping over from the thread, 80 // this plan will always work. 81 return true; 82 } 83 84 bool ThreadPlanStepInstruction::DoPlanExplainsStop(Event *event_ptr) { 85 StopInfoSP stop_info_sp = GetPrivateStopInfo(); 86 if (stop_info_sp) { 87 StopReason reason = stop_info_sp->GetStopReason(); 88 return (reason == eStopReasonTrace || reason == eStopReasonNone); 89 } 90 return false; 91 } 92 93 bool ThreadPlanStepInstruction::IsPlanStale() { 94 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 95 StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID(); 96 if (cur_frame_id == m_stack_id) { 97 return (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr); 98 } else if (cur_frame_id < m_stack_id) { 99 // If the current frame is younger than the start frame and we are stepping 100 // over, then we need to continue, 101 // but if we are doing just one step, we're done. 102 return !m_step_over; 103 } else { 104 if (log) { 105 log->Printf("ThreadPlanStepInstruction::IsPlanStale - Current frame is " 106 "older than start frame, plan is stale."); 107 } 108 return true; 109 } 110 } 111 112 bool ThreadPlanStepInstruction::ShouldStop(Event *event_ptr) { 113 if (m_step_over) { 114 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 115 116 StackFrameSP cur_frame_sp = m_thread.GetStackFrameAtIndex(0); 117 if (!cur_frame_sp) { 118 if (log) 119 log->Printf( 120 "ThreadPlanStepInstruction couldn't get the 0th frame, stopping."); 121 SetPlanComplete(); 122 return true; 123 } 124 125 StackID cur_frame_zero_id = cur_frame_sp->GetStackID(); 126 127 if (cur_frame_zero_id == m_stack_id || m_stack_id < cur_frame_zero_id) { 128 if (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr) { 129 if (--m_iteration_count <= 0) { 130 SetPlanComplete(); 131 return true; 132 } else { 133 // We are still stepping, reset the start pc, and in case we've 134 // stepped out, 135 // reset the current stack id. 136 SetUpState(); 137 return false; 138 } 139 } else 140 return false; 141 } else { 142 // We've stepped in, step back out again: 143 StackFrame *return_frame = m_thread.GetStackFrameAtIndex(1).get(); 144 if (return_frame) { 145 if (return_frame->GetStackID() != m_parent_frame_id || 146 m_start_has_symbol) { 147 // next-instruction shouldn't step out of inlined functions. But we 148 // may have stepped into a 149 // real function that starts with an inlined function, and we do want 150 // to step out of that... 151 152 if (cur_frame_sp->IsInlined()) { 153 StackFrameSP parent_frame_sp = 154 m_thread.GetFrameWithStackID(m_stack_id); 155 156 if (parent_frame_sp && 157 parent_frame_sp->GetConcreteFrameIndex() == 158 cur_frame_sp->GetConcreteFrameIndex()) { 159 SetPlanComplete(); 160 if (log) { 161 log->Printf("Frame we stepped into is inlined into the frame " 162 "we were stepping from, stopping."); 163 } 164 return true; 165 } 166 } 167 168 if (log) { 169 StreamString s; 170 s.PutCString("Stepped in to: "); 171 addr_t stop_addr = 172 m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC(); 173 s.Address(stop_addr, m_thread.CalculateTarget() 174 ->GetArchitecture() 175 .GetAddressByteSize()); 176 s.PutCString(" stepping out to: "); 177 addr_t return_addr = return_frame->GetRegisterContext()->GetPC(); 178 s.Address(return_addr, m_thread.CalculateTarget() 179 ->GetArchitecture() 180 .GetAddressByteSize()); 181 log->Printf("%s.", s.GetData()); 182 } 183 184 // StepInstruction should probably have the tri-state RunMode, but for 185 // now it is safer to 186 // run others. 187 const bool stop_others = false; 188 m_thread.QueueThreadPlanForStepOutNoShouldStop( 189 false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0); 190 return false; 191 } else { 192 if (log) { 193 log->PutCString( 194 "The stack id we are stepping in changed, but our parent frame " 195 "did not when stepping from code with no symbols. " 196 "We are probably just confused about where we are, stopping."); 197 } 198 SetPlanComplete(); 199 return true; 200 } 201 } else { 202 if (log) 203 log->Printf("Could not find previous frame, stopping."); 204 SetPlanComplete(); 205 return true; 206 } 207 } 208 } else { 209 lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC(0); 210 if (pc_addr != m_instruction_addr) { 211 if (--m_iteration_count <= 0) { 212 SetPlanComplete(); 213 return true; 214 } else { 215 // We are still stepping, reset the start pc, and in case we've stepped 216 // in or out, 217 // reset the current stack id. 218 SetUpState(); 219 return false; 220 } 221 } else 222 return false; 223 } 224 } 225 226 bool ThreadPlanStepInstruction::StopOthers() { return m_stop_other_threads; } 227 228 StateType ThreadPlanStepInstruction::GetPlanRunState() { 229 return eStateStepping; 230 } 231 232 bool ThreadPlanStepInstruction::WillStop() { return true; } 233 234 bool ThreadPlanStepInstruction::MischiefManaged() { 235 if (IsPlanComplete()) { 236 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 237 if (log) 238 log->Printf("Completed single instruction step plan."); 239 ThreadPlan::MischiefManaged(); 240 return true; 241 } else { 242 return false; 243 } 244 } 245