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