blob: 8b24bf94973b5112c43df99b6c7f8f712f3db4cf [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Target/Process.h"
13#include "lldb/Target/RegisterContext.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000014#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000015#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016
17using namespace lldb;
18using namespace lldb_private;
19
20//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000021// ThreadPlanStepOverBreakpoint: Single steps over a breakpoint bp_site_sp at
22// the pc.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023//----------------------------------------------------------------------
24
Kate Stoneb9c1b512016-09-06 20:57:50 +000025ThreadPlanStepOverBreakpoint::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
Adrian Prantl05097242018-04-30 16:49:04 +000030 // first in the thread plan stack when stepping over
31 // a breakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +000032 m_breakpoint_addr(LLDB_INVALID_ADDRESS),
33 m_auto_continue(false), m_reenabled_breakpoint_site(false)
Jim Inghamb01e7422010-06-19 04:45:32 +000034
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035{
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 m_breakpoint_addr = m_thread.GetRegisterContext()->GetPC();
37 m_breakpoint_site_id =
38 m_thread.GetProcess()->GetBreakpointSiteList().FindIDByAddress(
39 m_breakpoint_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040}
41
Kate Stoneb9c1b512016-09-06 20:57:50 +000042ThreadPlanStepOverBreakpoint::~ThreadPlanStepOverBreakpoint() {}
43
44void 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);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048}
49
Kate Stoneb9c1b512016-09-06 20:57:50 +000050bool ThreadPlanStepOverBreakpoint::ValidatePlan(Stream *error) { return true; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051
Kate Stoneb9c1b512016-09-06 20:57:50 +000052bool 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.
Adrian Prantl05097242018-04-30 16:49:04 +000056 // 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
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 // PC at the breakpoint without having triggered the actions, then you'd
60 // continue, the PC wouldn't change,
Adrian Prantl05097242018-04-30 16:49:04 +000061 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 StopReason reason = stop_info_sp->GetStopReason();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
Jim Inghama435d732018-05-22 00:06:55 +000067 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
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 switch (reason) {
Jim Inghama435d732018-05-22 00:06:55 +000074 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)
Benjamin Kramer192fe372018-05-25 12:59:59 +000098 log->Printf("Got breakpoint stop reason but pc: 0x%" PRIx64
Jim Inghama435d732018-05-22 00:06:55 +000099 "hasn't changed.", pc_addr);
100 return true;
101 }
102
103 SetAutoContinue(false);
104 return false;
105 }
106 default:
107 return false;
Jim Inghambc1d7f72012-06-22 20:42:22 +0000108 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 }
110 return false;
111}
112
113bool ThreadPlanStepOverBreakpoint::ShouldStop(Event *event_ptr) {
114 return !ShouldAutoContinue(event_ptr);
115}
116
117bool ThreadPlanStepOverBreakpoint::StopOthers() { return true; }
118
119StateType ThreadPlanStepOverBreakpoint::GetPlanRunState() {
120 return eStateStepping;
121}
122
123bool 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));
Jim Inghama435d732018-05-22 00:06:55 +0000129 if (bp_site_sp && bp_site_sp->IsEnabled()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 m_thread.GetProcess()->DisableBreakpointSite(bp_site_sp.get());
Jim Inghama435d732018-05-22 00:06:55 +0000131 m_reenabled_breakpoint_site = false;
132 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 }
134 return true;
135}
136
137bool ThreadPlanStepOverBreakpoint::WillStop() {
138 ReenableBreakpointSite();
139 return true;
140}
141
Jim Inghama435d732018-05-22 00:06:55 +0000142void ThreadPlanStepOverBreakpoint::WillPop() {
143 ReenableBreakpointSite();
144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146bool 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
Adrian Prantl05097242018-04-30 16:49:04 +0000151 // didn't get a chance to run.
Jim Inghambc1d7f72012-06-22 20:42:22 +0000152 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 } 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();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163}
164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165void 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());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 }
175}
176void ThreadPlanStepOverBreakpoint::ThreadDestroyed() {
177 ReenableBreakpointSite();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178}
179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180void ThreadPlanStepOverBreakpoint::SetAutoContinue(bool do_it) {
181 m_auto_continue = do_it;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182}
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184bool ThreadPlanStepOverBreakpoint::ShouldAutoContinue(Event *event_ptr) {
185 return m_auto_continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186}
187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188bool ThreadPlanStepOverBreakpoint::IsPlanStale() {
189 return m_thread.GetRegisterContext()->GetPC() != m_breakpoint_addr;
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000190}