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