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