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