1 //===-- ThreadPlanStepOverBreakpoint.cpp ----------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
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"
18 using namespace lldb_private
;
20 // ThreadPlanStepOverBreakpoint: Single steps over a breakpoint bp_site_sp at
23 ThreadPlanStepOverBreakpoint::ThreadPlanStepOverBreakpoint(Thread
&thread
)
25 ThreadPlan::eKindStepOverBreakpoint
, "Step over breakpoint trap",
27 eVoteNoOpinion
), // We need to report the run since this happens
28 // first in the thread plan stack when stepping over
30 m_breakpoint_addr(LLDB_INVALID_ADDRESS
),
31 m_auto_continue(false), m_reenabled_breakpoint_site(false)
34 m_breakpoint_addr
= thread
.GetRegisterContext()->GetPC();
35 m_breakpoint_site_id
=
36 thread
.GetProcess()->GetBreakpointSiteList().FindIDByAddress(
40 ThreadPlanStepOverBreakpoint::~ThreadPlanStepOverBreakpoint() = default;
42 void 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
);
48 bool ThreadPlanStepOverBreakpoint::ValidatePlan(Stream
*error
) { return true; }
50 bool ThreadPlanStepOverBreakpoint::DoPlanExplainsStop(Event
*event_ptr
) {
51 StopInfoSP stop_info_sp
= GetPrivateStopInfo();
53 StopReason reason
= stop_info_sp
->GetStopReason();
55 Log
*log
= GetLog(LLDBLog::Step
);
56 LLDB_LOG(log
, "Step over breakpoint stopped for reason: {0}.",
57 Thread::StopReasonAsString(reason
));
60 case eStopReasonTrace
:
63 case eStopReasonBreakpoint
:
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 call
67 // that a breakpoint hit, and trigger the actions, etc. Otherwise you
68 // would see the PC at the breakpoint without having triggered the
69 // actions, then you'd continue, the PC wouldn't change, and you'd see
70 // 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 wrench
76 // control away from the plans that can deal with this.
77 // Be careful, however, as we may have "seen a breakpoint under the PC
78 // because we stopped without changing the PC, in which case we do want
79 // to re-claim this stop so we'll try again.
80 lldb::addr_t pc_addr
= GetThread().GetRegisterContext()->GetPC();
82 if (pc_addr
== m_breakpoint_addr
) {
84 "Got breakpoint stop reason but pc: 0x%" PRIx64
90 SetAutoContinue(false);
100 bool ThreadPlanStepOverBreakpoint::ShouldStop(Event
*event_ptr
) {
101 return !ShouldAutoContinue(event_ptr
);
104 bool ThreadPlanStepOverBreakpoint::StopOthers() { return true; }
106 StateType
ThreadPlanStepOverBreakpoint::GetPlanRunState() {
107 return eStateStepping
;
110 bool ThreadPlanStepOverBreakpoint::DoWillResume(StateType resume_state
,
113 BreakpointSiteSP
bp_site_sp(
114 m_process
.GetBreakpointSiteList().FindByAddress(m_breakpoint_addr
));
115 if (bp_site_sp
&& bp_site_sp
->IsEnabled()) {
116 m_process
.DisableBreakpointSite(bp_site_sp
.get());
117 m_reenabled_breakpoint_site
= false;
123 bool ThreadPlanStepOverBreakpoint::WillStop() {
124 ReenableBreakpointSite();
128 void ThreadPlanStepOverBreakpoint::DidPop() { ReenableBreakpointSite(); }
130 bool ThreadPlanStepOverBreakpoint::MischiefManaged() {
131 lldb::addr_t pc_addr
= GetThread().GetRegisterContext()->GetPC();
133 if (pc_addr
== m_breakpoint_addr
) {
134 // If we are still at the PC of our breakpoint, then for some reason we
135 // didn't get a chance to run.
138 Log
*log
= GetLog(LLDBLog::Step
);
139 LLDB_LOGF(log
, "Completed step over breakpoint plan.");
140 // Otherwise, re-enable the breakpoint we were stepping over, and we're
142 ReenableBreakpointSite();
143 ThreadPlan::MischiefManaged();
148 void ThreadPlanStepOverBreakpoint::ReenableBreakpointSite() {
149 if (!m_reenabled_breakpoint_site
) {
150 m_reenabled_breakpoint_site
= true;
151 BreakpointSiteSP
bp_site_sp(
152 m_process
.GetBreakpointSiteList().FindByAddress(m_breakpoint_addr
));
154 m_process
.EnableBreakpointSite(bp_site_sp
.get());
158 void ThreadPlanStepOverBreakpoint::ThreadDestroyed() {
159 ReenableBreakpointSite();
162 void ThreadPlanStepOverBreakpoint::SetAutoContinue(bool do_it
) {
163 m_auto_continue
= do_it
;
166 bool ThreadPlanStepOverBreakpoint::ShouldAutoContinue(Event
*event_ptr
) {
167 return m_auto_continue
;
170 bool ThreadPlanStepOverBreakpoint::IsPlanStale() {
171 return GetThread().GetRegisterContext()->GetPC() != m_breakpoint_addr
;