Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ThreadPlanStepUntil.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 | //===----------------------------------------------------------------------===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 9 | |
| 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/ThreadPlanStepUntil.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 15 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Breakpoint/Breakpoint.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/SymbolContextScope.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Target/Process.h" |
| 19 | #include "lldb/Target/RegisterContext.h" |
Greg Clayton | f4b47e1 | 2010-08-04 01:40:35 +0000 | [diff] [blame] | 20 | #include "lldb/Target/StopInfo.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Target/Target.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 22 | #include "lldb/Utility/Log.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace lldb; |
| 25 | using namespace lldb_private; |
| 26 | |
| 27 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 28 | // ThreadPlanStepUntil: Run until we reach a given line number or step out of |
| 29 | // the current frame |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | //---------------------------------------------------------------------- |
| 31 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 32 | ThreadPlanStepUntil::ThreadPlanStepUntil(Thread &thread, |
| 33 | lldb::addr_t *address_list, |
| 34 | size_t num_addresses, bool stop_others, |
| 35 | uint32_t frame_idx) |
| 36 | : ThreadPlan(ThreadPlan::eKindStepUntil, "Step until", thread, |
| 37 | eVoteNoOpinion, eVoteNoOpinion), |
| 38 | m_step_from_insn(LLDB_INVALID_ADDRESS), |
| 39 | m_return_bp_id(LLDB_INVALID_BREAK_ID), |
| 40 | m_return_addr(LLDB_INVALID_ADDRESS), m_stepped_out(false), |
| 41 | m_should_stop(false), m_ran_analyze(false), m_explains_stop(false), |
| 42 | m_until_points(), m_stop_others(stop_others) { |
| 43 | // Stash away our "until" addresses: |
| 44 | TargetSP target_sp(m_thread.CalculateTarget()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 45 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | StackFrameSP frame_sp(m_thread.GetStackFrameAtIndex(frame_idx)); |
| 47 | if (frame_sp) { |
| 48 | m_step_from_insn = frame_sp->GetStackID().GetPC(); |
| 49 | lldb::user_id_t thread_id = m_thread.GetID(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 50 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | // Find the return address and set a breakpoint there: |
| 52 | // FIXME - can we do this more securely if we know first_insn? |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | StackFrameSP return_frame_sp(m_thread.GetStackFrameAtIndex(frame_idx + 1)); |
| 55 | if (return_frame_sp) { |
| 56 | // TODO: add inline functionality |
| 57 | m_return_addr = return_frame_sp->GetStackID().GetPC(); |
| 58 | Breakpoint *return_bp = |
| 59 | target_sp->CreateBreakpoint(m_return_addr, true, false).get(); |
| 60 | if (return_bp != nullptr) { |
| 61 | return_bp->SetThreadID(thread_id); |
| 62 | m_return_bp_id = return_bp->GetID(); |
| 63 | return_bp->SetBreakpointKind("until-return-backstop"); |
| 64 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 66 | |
| 67 | m_stack_id = frame_sp->GetStackID(); |
| 68 | |
| 69 | // Now set breakpoints on all our return addresses: |
| 70 | for (size_t i = 0; i < num_addresses; i++) { |
| 71 | Breakpoint *until_bp = |
| 72 | target_sp->CreateBreakpoint(address_list[i], true, false).get(); |
| 73 | if (until_bp != nullptr) { |
| 74 | until_bp->SetThreadID(thread_id); |
| 75 | m_until_points[address_list[i]] = until_bp->GetID(); |
| 76 | until_bp->SetBreakpointKind("until-target"); |
| 77 | } else { |
| 78 | m_until_points[address_list[i]] = LLDB_INVALID_BREAK_ID; |
| 79 | } |
| 80 | } |
| 81 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | ThreadPlanStepUntil::~ThreadPlanStepUntil() { Clear(); } |
| 85 | |
| 86 | void ThreadPlanStepUntil::Clear() { |
| 87 | TargetSP target_sp(m_thread.CalculateTarget()); |
| 88 | if (target_sp) { |
| 89 | if (m_return_bp_id != LLDB_INVALID_BREAK_ID) { |
| 90 | target_sp->RemoveBreakpointByID(m_return_bp_id); |
| 91 | m_return_bp_id = LLDB_INVALID_BREAK_ID; |
| 92 | } |
| 93 | |
| 94 | until_collection::iterator pos, end = m_until_points.end(); |
| 95 | for (pos = m_until_points.begin(); pos != end; pos++) { |
| 96 | target_sp->RemoveBreakpointByID((*pos).second); |
| 97 | } |
| 98 | } |
| 99 | m_until_points.clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | void ThreadPlanStepUntil::GetDescription(Stream *s, |
| 103 | lldb::DescriptionLevel level) { |
| 104 | if (level == lldb::eDescriptionLevelBrief) { |
| 105 | s->Printf("step until"); |
| 106 | if (m_stepped_out) |
| 107 | s->Printf(" - stepped out"); |
| 108 | } else { |
| 109 | if (m_until_points.size() == 1) |
| 110 | s->Printf("Stepping from address 0x%" PRIx64 " until we reach 0x%" PRIx64 |
| 111 | " using breakpoint %d", |
| 112 | (uint64_t)m_step_from_insn, |
| 113 | (uint64_t)(*m_until_points.begin()).first, |
| 114 | (*m_until_points.begin()).second); |
| 115 | else { |
| 116 | until_collection::iterator pos, end = m_until_points.end(); |
| 117 | s->Printf("Stepping from address 0x%" PRIx64 " until we reach one of:", |
| 118 | (uint64_t)m_step_from_insn); |
| 119 | for (pos = m_until_points.begin(); pos != end; pos++) { |
| 120 | s->Printf("\n\t0x%" PRIx64 " (bp: %d)", (uint64_t)(*pos).first, |
| 121 | (*pos).second); |
| 122 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 123 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | s->Printf(" stepped out address is 0x%" PRIx64 ".", |
| 125 | (uint64_t)m_return_addr); |
| 126 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | bool ThreadPlanStepUntil::ValidatePlan(Stream *error) { |
| 130 | if (m_return_bp_id == LLDB_INVALID_BREAK_ID) |
| 131 | return false; |
| 132 | else { |
| 133 | until_collection::iterator pos, end = m_until_points.end(); |
| 134 | for (pos = m_until_points.begin(); pos != end; pos++) { |
| 135 | if (!LLDB_BREAK_ID_IS_VALID((*pos).second)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 136 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 137 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 138 | return true; |
| 139 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | void ThreadPlanStepUntil::AnalyzeStop() { |
| 143 | if (m_ran_analyze) |
| 144 | return; |
| 145 | |
| 146 | StopInfoSP stop_info_sp = GetPrivateStopInfo(); |
| 147 | m_should_stop = true; |
| 148 | m_explains_stop = false; |
| 149 | |
| 150 | if (stop_info_sp) { |
| 151 | StopReason reason = stop_info_sp->GetStopReason(); |
| 152 | |
| 153 | if (reason == eStopReasonBreakpoint) { |
| 154 | // If this is OUR breakpoint, we're fine, otherwise we don't know why this |
| 155 | // happened... |
| 156 | BreakpointSiteSP this_site = |
| 157 | m_thread.GetProcess()->GetBreakpointSiteList().FindByID( |
| 158 | stop_info_sp->GetValue()); |
| 159 | if (!this_site) { |
| 160 | m_explains_stop = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 161 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 162 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 163 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 164 | if (this_site->IsBreakpointAtThisSite(m_return_bp_id)) { |
| 165 | // If we are at our "step out" breakpoint, and the stack depth has |
| 166 | // shrunk, then |
| 167 | // this is indeed our stop. |
| 168 | // If the stack depth has grown, then we've hit our step out breakpoint |
| 169 | // recursively. |
| 170 | // If we are the only breakpoint at that location, then we do explain |
| 171 | // the stop, and |
| 172 | // we'll just continue. |
| 173 | // If there was another breakpoint here, then we don't explain the stop, |
| 174 | // but we won't |
| 175 | // mark ourselves Completed, because maybe that breakpoint will |
| 176 | // continue, and then |
| 177 | // we'll finish the "until". |
| 178 | bool done; |
| 179 | StackID cur_frame_zero_id; |
Jim Ingham | 9b03fa0 | 2015-07-23 19:55:02 +0000 | [diff] [blame] | 180 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 181 | done = (m_stack_id < cur_frame_zero_id); |
Jim Ingham | 9b03fa0 | 2015-07-23 19:55:02 +0000 | [diff] [blame] | 182 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 183 | if (done) { |
| 184 | m_stepped_out = true; |
| 185 | SetPlanComplete(); |
| 186 | } else |
| 187 | m_should_stop = false; |
Jim Ingham | 9b03fa0 | 2015-07-23 19:55:02 +0000 | [diff] [blame] | 188 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | if (this_site->GetNumberOfOwners() == 1) |
| 190 | m_explains_stop = true; |
Jim Ingham | 9b03fa0 | 2015-07-23 19:55:02 +0000 | [diff] [blame] | 191 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 192 | m_explains_stop = false; |
| 193 | return; |
| 194 | } else { |
| 195 | // Check if we've hit one of our "until" breakpoints. |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 196 | until_collection::iterator pos, end = m_until_points.end(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 197 | for (pos = m_until_points.begin(); pos != end; pos++) { |
| 198 | if (this_site->IsBreakpointAtThisSite((*pos).second)) { |
| 199 | // If we're at the right stack depth, then we're done. |
| 200 | |
| 201 | bool done; |
| 202 | StackID frame_zero_id = |
| 203 | m_thread.GetStackFrameAtIndex(0)->GetStackID(); |
| 204 | |
| 205 | if (frame_zero_id == m_stack_id) |
| 206 | done = true; |
| 207 | else if (frame_zero_id < m_stack_id) |
| 208 | done = false; |
| 209 | else { |
| 210 | StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(1); |
| 211 | |
| 212 | // But if we can't even unwind one frame we should just get out of |
| 213 | // here & stop... |
| 214 | if (older_frame_sp) { |
| 215 | const SymbolContext &older_context = |
| 216 | older_frame_sp->GetSymbolContext(eSymbolContextEverything); |
| 217 | SymbolContext stack_context; |
| 218 | m_stack_id.GetSymbolContextScope()->CalculateSymbolContext( |
| 219 | &stack_context); |
| 220 | |
| 221 | done = (older_context == stack_context); |
| 222 | } else |
| 223 | done = false; |
| 224 | } |
| 225 | |
| 226 | if (done) |
| 227 | SetPlanComplete(); |
| 228 | else |
| 229 | m_should_stop = false; |
| 230 | |
| 231 | // Otherwise we've hit this breakpoint recursively. If we're the |
| 232 | // only breakpoint here, then we do explain the stop, and we'll |
| 233 | // continue. |
| 234 | // If not then we should let higher plans handle this stop. |
| 235 | if (this_site->GetNumberOfOwners() == 1) |
| 236 | m_explains_stop = true; |
| 237 | else { |
| 238 | m_should_stop = true; |
| 239 | m_explains_stop = false; |
| 240 | } |
| 241 | return; |
| 242 | } |
Greg Clayton | 1ac04c3 | 2012-02-21 00:09:25 +0000 | [diff] [blame] | 243 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 244 | } |
| 245 | // If we get here we haven't hit any of our breakpoints, so let the higher |
| 246 | // plans take care of the stop. |
| 247 | m_explains_stop = false; |
| 248 | return; |
| 249 | } else if (IsUsuallyUnexplainedStopReason(reason)) { |
| 250 | m_explains_stop = false; |
| 251 | } else { |
| 252 | m_explains_stop = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 253 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 254 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 257 | bool ThreadPlanStepUntil::DoPlanExplainsStop(Event *event_ptr) { |
| 258 | // We don't explain signals or breakpoints (breakpoints that handle stepping |
| 259 | // in or |
| 260 | // out will be handled by a child plan. |
| 261 | AnalyzeStop(); |
| 262 | return m_explains_stop; |
| 263 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 264 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 265 | bool ThreadPlanStepUntil::ShouldStop(Event *event_ptr) { |
| 266 | // If we've told our self in ExplainsStop that we plan to continue, then |
| 267 | // do so here. Otherwise, as long as this thread has stopped for a reason, |
| 268 | // we will stop. |
| 269 | |
| 270 | StopInfoSP stop_info_sp = GetPrivateStopInfo(); |
| 271 | if (!stop_info_sp || stop_info_sp->GetStopReason() == eStopReasonNone) |
| 272 | return false; |
| 273 | |
| 274 | AnalyzeStop(); |
| 275 | return m_should_stop; |
| 276 | } |
| 277 | |
| 278 | bool ThreadPlanStepUntil::StopOthers() { return m_stop_others; } |
| 279 | |
| 280 | StateType ThreadPlanStepUntil::GetPlanRunState() { return eStateRunning; } |
| 281 | |
| 282 | bool ThreadPlanStepUntil::DoWillResume(StateType resume_state, |
| 283 | bool current_plan) { |
| 284 | if (current_plan) { |
| 285 | TargetSP target_sp(m_thread.CalculateTarget()); |
| 286 | if (target_sp) { |
| 287 | Breakpoint *return_bp = |
| 288 | target_sp->GetBreakpointByID(m_return_bp_id).get(); |
| 289 | if (return_bp != nullptr) |
| 290 | return_bp->SetEnabled(true); |
| 291 | |
| 292 | until_collection::iterator pos, end = m_until_points.end(); |
| 293 | for (pos = m_until_points.begin(); pos != end; pos++) { |
| 294 | Breakpoint *until_bp = |
| 295 | target_sp->GetBreakpointByID((*pos).second).get(); |
| 296 | if (until_bp != nullptr) |
| 297 | until_bp->SetEnabled(true); |
| 298 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 299 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 300 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 301 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 302 | m_should_stop = true; |
| 303 | m_ran_analyze = false; |
| 304 | m_explains_stop = false; |
| 305 | return true; |
| 306 | } |
| 307 | |
| 308 | bool ThreadPlanStepUntil::WillStop() { |
| 309 | TargetSP target_sp(m_thread.CalculateTarget()); |
| 310 | if (target_sp) { |
| 311 | Breakpoint *return_bp = target_sp->GetBreakpointByID(m_return_bp_id).get(); |
| 312 | if (return_bp != nullptr) |
| 313 | return_bp->SetEnabled(false); |
| 314 | |
| 315 | until_collection::iterator pos, end = m_until_points.end(); |
| 316 | for (pos = m_until_points.begin(); pos != end; pos++) { |
| 317 | Breakpoint *until_bp = target_sp->GetBreakpointByID((*pos).second).get(); |
| 318 | if (until_bp != nullptr) |
| 319 | until_bp->SetEnabled(false); |
| 320 | } |
| 321 | } |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | bool ThreadPlanStepUntil::MischiefManaged() { |
| 326 | // I'm letting "PlanExplainsStop" do all the work, and just reporting that |
| 327 | // here. |
| 328 | bool done = false; |
| 329 | if (IsPlanComplete()) { |
| 330 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 331 | if (log) |
| 332 | log->Printf("Completed step until plan."); |
| 333 | |
| 334 | Clear(); |
| 335 | done = true; |
| 336 | } |
| 337 | if (done) |
| 338 | ThreadPlan::MischiefManaged(); |
| 339 | |
| 340 | return done; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 341 | } |