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