Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ThreadPlanStepOverBreakpoint.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 | #include "lldb/Target/ThreadPlanStepOverBreakpoint.h" |
| 11 | |
| 12 | // C Includes |
| 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | // Project includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include "lldb/Target/Process.h" |
| 17 | #include "lldb/Target/RegisterContext.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/Stream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
| 23 | |
| 24 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | // ThreadPlanStepOverBreakpoint: Single steps over a breakpoint bp_site_sp at |
| 26 | // the pc. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | //---------------------------------------------------------------------- |
| 28 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 29 | ThreadPlanStepOverBreakpoint::ThreadPlanStepOverBreakpoint(Thread &thread) |
| 30 | : ThreadPlan( |
| 31 | ThreadPlan::eKindStepOverBreakpoint, "Step over breakpoint trap", |
| 32 | thread, eVoteNo, |
| 33 | eVoteNoOpinion), // We need to report the run since this happens |
| 34 | // first in the thread plan stack when stepping |
| 35 | // over a breakpoint |
| 36 | m_breakpoint_addr(LLDB_INVALID_ADDRESS), |
| 37 | m_auto_continue(false), m_reenabled_breakpoint_site(false) |
Jim Ingham | b01e742 | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | m_breakpoint_addr = m_thread.GetRegisterContext()->GetPC(); |
| 41 | m_breakpoint_site_id = |
| 42 | m_thread.GetProcess()->GetBreakpointSiteList().FindIDByAddress( |
| 43 | m_breakpoint_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 46 | ThreadPlanStepOverBreakpoint::~ThreadPlanStepOverBreakpoint() {} |
| 47 | |
| 48 | void ThreadPlanStepOverBreakpoint::GetDescription( |
| 49 | Stream *s, lldb::DescriptionLevel level) { |
| 50 | s->Printf("Single stepping past breakpoint site %" PRIu64 " at 0x%" PRIx64, |
| 51 | m_breakpoint_site_id, (uint64_t)m_breakpoint_addr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | bool ThreadPlanStepOverBreakpoint::ValidatePlan(Stream *error) { return true; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | bool ThreadPlanStepOverBreakpoint::DoPlanExplainsStop(Event *event_ptr) { |
| 57 | StopInfoSP stop_info_sp = GetPrivateStopInfo(); |
| 58 | if (stop_info_sp) { |
| 59 | // It's a little surprising that we stop here for a breakpoint hit. |
| 60 | // However, when you single step ONTO a breakpoint |
| 61 | // we still want to call that a breakpoint hit, and trigger the actions, |
| 62 | // etc. Otherwise you would see the |
| 63 | // PC at the breakpoint without having triggered the actions, then you'd |
| 64 | // continue, the PC wouldn't change, |
| 65 | // and you'd see the breakpoint hit, which would be odd. |
| 66 | // So the lower levels fake "step onto breakpoint address" and return that |
| 67 | // as a breakpoint. So our trace |
| 68 | // step COULD appear as a breakpoint hit if the next instruction also |
| 69 | // contained a breakpoint. |
| 70 | StopReason reason = stop_info_sp->GetStopReason(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | switch (reason) { |
| 73 | case eStopReasonTrace: |
| 74 | case eStopReasonNone: |
| 75 | return true; |
| 76 | case eStopReasonBreakpoint: |
| 77 | // It's a little surprising that we stop here for a breakpoint hit. |
| 78 | // However, when you single step ONTO a |
| 79 | // breakpoint we still want to call that a breakpoint hit, and trigger the |
| 80 | // actions, etc. Otherwise you |
| 81 | // would see the PC at the breakpoint without having triggered the |
| 82 | // actions, then you'd continue, the PC |
| 83 | // wouldn't change, and you'd see the breakpoint hit, which would be odd. |
| 84 | // So the lower levels fake "step onto breakpoint address" and return that |
| 85 | // as a breakpoint hit. So our trace |
| 86 | // step COULD appear as a breakpoint hit if the next instruction also |
| 87 | // contained a breakpoint. We don't want |
| 88 | // to handle that, since we really don't know what to do with breakpoint |
| 89 | // hits. But make sure we don't set |
| 90 | // ourselves to auto-continue or we'll wrench control away from the plans |
| 91 | // that can deal with this. |
| 92 | SetAutoContinue(false); |
| 93 | return false; |
| 94 | default: |
| 95 | return false; |
Jim Ingham | bc1d7f7 | 2012-06-22 20:42:22 +0000 | [diff] [blame] | 96 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | bool ThreadPlanStepOverBreakpoint::ShouldStop(Event *event_ptr) { |
| 102 | return !ShouldAutoContinue(event_ptr); |
| 103 | } |
| 104 | |
| 105 | bool ThreadPlanStepOverBreakpoint::StopOthers() { return true; } |
| 106 | |
| 107 | StateType ThreadPlanStepOverBreakpoint::GetPlanRunState() { |
| 108 | return eStateStepping; |
| 109 | } |
| 110 | |
| 111 | bool ThreadPlanStepOverBreakpoint::DoWillResume(StateType resume_state, |
| 112 | bool current_plan) { |
| 113 | if (current_plan) { |
| 114 | BreakpointSiteSP bp_site_sp( |
| 115 | m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress( |
| 116 | m_breakpoint_addr)); |
| 117 | if (bp_site_sp && bp_site_sp->IsEnabled()) |
| 118 | m_thread.GetProcess()->DisableBreakpointSite(bp_site_sp.get()); |
| 119 | } |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | bool ThreadPlanStepOverBreakpoint::WillStop() { |
| 124 | ReenableBreakpointSite(); |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | bool ThreadPlanStepOverBreakpoint::MischiefManaged() { |
| 129 | lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC(); |
| 130 | |
| 131 | if (pc_addr == m_breakpoint_addr) { |
| 132 | // If we are still at the PC of our breakpoint, then for some reason we |
| 133 | // didn't |
| 134 | // get a chance to run. |
Jim Ingham | bc1d7f7 | 2012-06-22 20:42:22 +0000 | [diff] [blame] | 135 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | } else { |
| 137 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); |
| 138 | if (log) |
| 139 | log->Printf("Completed step over breakpoint plan."); |
| 140 | // Otherwise, re-enable the breakpoint we were stepping over, and we're |
| 141 | // done. |
| 142 | ReenableBreakpointSite(); |
| 143 | ThreadPlan::MischiefManaged(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | void ThreadPlanStepOverBreakpoint::ReenableBreakpointSite() { |
| 149 | if (!m_reenabled_breakpoint_site) { |
| 150 | m_reenabled_breakpoint_site = true; |
| 151 | BreakpointSiteSP bp_site_sp( |
| 152 | m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress( |
| 153 | m_breakpoint_addr)); |
| 154 | if (bp_site_sp) { |
| 155 | m_thread.GetProcess()->EnableBreakpointSite(bp_site_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 156 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | void ThreadPlanStepOverBreakpoint::ThreadDestroyed() { |
| 160 | ReenableBreakpointSite(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 163 | void ThreadPlanStepOverBreakpoint::SetAutoContinue(bool do_it) { |
| 164 | m_auto_continue = do_it; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 167 | bool ThreadPlanStepOverBreakpoint::ShouldAutoContinue(Event *event_ptr) { |
| 168 | return m_auto_continue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 171 | bool ThreadPlanStepOverBreakpoint::IsPlanStale() { |
| 172 | return m_thread.GetRegisterContext()->GetPC() != m_breakpoint_addr; |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 173 | } |