Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
| 13 | // Project includes |
Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 +0000 | [diff] [blame] | 14 | #include "lldb/Target/ThreadPlanStepInstruction.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include "lldb/Target/Process.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 16 | #include "lldb/Target/RegisterContext.h" |
| 17 | #include "lldb/Target/RegisterContext.h" |
| 18 | #include "lldb/Target/StopInfo.h" |
| 19 | #include "lldb/Target/Target.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame^] | 20 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 21 | #include "lldb/Utility/Stream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | |
| 26 | //---------------------------------------------------------------------- |
| 27 | // ThreadPlanStepInstruction: Step over the current instruction |
| 28 | //---------------------------------------------------------------------- |
| 29 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 +0000 | [diff] [blame] | 43 | ThreadPlanStepInstruction::~ThreadPlanStepInstruction() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Jim Ingham | 7a88ec9 | 2014-07-08 19:28:57 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | void ThreadPlanStepInstruction::GetDescription(Stream *s, |
| 59 | lldb::DescriptionLevel level) { |
| 60 | if (level == lldb::eDescriptionLevelBrief) { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | if (m_step_over) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 62 | s->Printf("instruction step over"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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) { |
Boris Ulasevich | 86aaa8a | 2017-02-15 11:42:47 +0000 | [diff] [blame] | 97 | // Set plan Complete when we reach next instruction |
| 98 | uint64_t pc = m_thread.GetRegisterContext()->GetPC(0); |
| 99 | uint32_t max_opcode_size = m_thread.CalculateTarget() |
| 100 | ->GetArchitecture().GetMaximumOpcodeByteSize(); |
| 101 | bool next_instruction_reached = (pc > m_instruction_addr) && |
| 102 | (pc <= m_instruction_addr + max_opcode_size); |
| 103 | if (next_instruction_reached) { |
| 104 | SetPlanComplete(); |
| 105 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | return (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr); |
| 107 | } else if (cur_frame_id < m_stack_id) { |
| 108 | // If the current frame is younger than the start frame and we are stepping |
| 109 | // over, then we need to continue, |
| 110 | // but if we are doing just one step, we're done. |
| 111 | return !m_step_over; |
| 112 | } else { |
| 113 | if (log) { |
| 114 | log->Printf("ThreadPlanStepInstruction::IsPlanStale - Current frame is " |
| 115 | "older than start frame, plan is stale."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 117 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | bool ThreadPlanStepInstruction::ShouldStop(Event *event_ptr) { |
| 122 | if (m_step_over) { |
| 123 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 124 | |
| 125 | StackFrameSP cur_frame_sp = m_thread.GetStackFrameAtIndex(0); |
| 126 | if (!cur_frame_sp) { |
| 127 | if (log) |
| 128 | log->Printf( |
| 129 | "ThreadPlanStepInstruction couldn't get the 0th frame, stopping."); |
| 130 | SetPlanComplete(); |
| 131 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 133 | |
| 134 | StackID cur_frame_zero_id = cur_frame_sp->GetStackID(); |
| 135 | |
| 136 | if (cur_frame_zero_id == m_stack_id || m_stack_id < cur_frame_zero_id) { |
| 137 | if (m_thread.GetRegisterContext()->GetPC(0) != m_instruction_addr) { |
| 138 | if (--m_iteration_count <= 0) { |
| 139 | SetPlanComplete(); |
| 140 | return true; |
| 141 | } else { |
| 142 | // We are still stepping, reset the start pc, and in case we've |
| 143 | // stepped out, |
| 144 | // reset the current stack id. |
| 145 | SetUpState(); |
| 146 | return false; |
| 147 | } |
| 148 | } else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | } else { |
| 151 | // We've stepped in, step back out again: |
| 152 | StackFrame *return_frame = m_thread.GetStackFrameAtIndex(1).get(); |
| 153 | if (return_frame) { |
| 154 | if (return_frame->GetStackID() != m_parent_frame_id || |
| 155 | m_start_has_symbol) { |
| 156 | // next-instruction shouldn't step out of inlined functions. But we |
| 157 | // may have stepped into a |
| 158 | // real function that starts with an inlined function, and we do want |
| 159 | // to step out of that... |
| 160 | |
| 161 | if (cur_frame_sp->IsInlined()) { |
| 162 | StackFrameSP parent_frame_sp = |
| 163 | m_thread.GetFrameWithStackID(m_stack_id); |
| 164 | |
| 165 | if (parent_frame_sp && |
| 166 | parent_frame_sp->GetConcreteFrameIndex() == |
| 167 | cur_frame_sp->GetConcreteFrameIndex()) { |
| 168 | SetPlanComplete(); |
| 169 | if (log) { |
| 170 | log->Printf("Frame we stepped into is inlined into the frame " |
| 171 | "we were stepping from, stopping."); |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (log) { |
| 178 | StreamString s; |
| 179 | s.PutCString("Stepped in to: "); |
| 180 | addr_t stop_addr = |
| 181 | m_thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC(); |
| 182 | s.Address(stop_addr, m_thread.CalculateTarget() |
| 183 | ->GetArchitecture() |
| 184 | .GetAddressByteSize()); |
| 185 | s.PutCString(" stepping out to: "); |
| 186 | addr_t return_addr = return_frame->GetRegisterContext()->GetPC(); |
| 187 | s.Address(return_addr, m_thread.CalculateTarget() |
| 188 | ->GetArchitecture() |
| 189 | .GetAddressByteSize()); |
| 190 | log->Printf("%s.", s.GetData()); |
| 191 | } |
| 192 | |
| 193 | // StepInstruction should probably have the tri-state RunMode, but for |
| 194 | // now it is safer to |
| 195 | // run others. |
| 196 | const bool stop_others = false; |
| 197 | m_thread.QueueThreadPlanForStepOutNoShouldStop( |
| 198 | false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0); |
| 199 | return false; |
| 200 | } else { |
| 201 | if (log) { |
| 202 | log->PutCString( |
| 203 | "The stack id we are stepping in changed, but our parent frame " |
| 204 | "did not when stepping from code with no symbols. " |
| 205 | "We are probably just confused about where we are, stopping."); |
| 206 | } |
| 207 | SetPlanComplete(); |
| 208 | return true; |
| 209 | } |
| 210 | } else { |
| 211 | if (log) |
| 212 | log->Printf("Could not find previous frame, stopping."); |
| 213 | SetPlanComplete(); |
| 214 | return true; |
| 215 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 216 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 217 | } else { |
| 218 | lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC(0); |
| 219 | if (pc_addr != m_instruction_addr) { |
| 220 | if (--m_iteration_count <= 0) { |
| 221 | SetPlanComplete(); |
| 222 | return true; |
| 223 | } else { |
| 224 | // We are still stepping, reset the start pc, and in case we've stepped |
| 225 | // in or out, |
| 226 | // reset the current stack id. |
| 227 | SetUpState(); |
| 228 | return false; |
| 229 | } |
| 230 | } else |
| 231 | return false; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | bool ThreadPlanStepInstruction::StopOthers() { return m_stop_other_threads; } |
| 236 | |
| 237 | StateType ThreadPlanStepInstruction::GetPlanRunState() { |
| 238 | return eStateStepping; |
| 239 | } |
| 240 | |
| 241 | bool ThreadPlanStepInstruction::WillStop() { return true; } |
| 242 | |
| 243 | bool ThreadPlanStepInstruction::MischiefManaged() { |
| 244 | if (IsPlanComplete()) { |
| 245 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 246 | if (log) |
| 247 | log->Printf("Completed single instruction step plan."); |
| 248 | ThreadPlan::MischiefManaged(); |
| 249 | return true; |
| 250 | } else { |
| 251 | return false; |
| 252 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 253 | } |