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