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