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