blob: 85bddd7380ad9e6b6a575eb6eab422f1a076e11b [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
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Target/Process.h"
17#include "lldb/Target/RegisterContext.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000018#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000019#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
21using namespace lldb;
22using namespace lldb_private;
23
24//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000025// ThreadPlanStepOverBreakpoint: Single steps over a breakpoint bp_site_sp at
26// the pc.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027//----------------------------------------------------------------------
28
Kate Stoneb9c1b512016-09-06 20:57:50 +000029ThreadPlanStepOverBreakpoint::ThreadPlanStepOverBreakpoint(Thread &thread)
30 : ThreadPlan(
31 ThreadPlan::eKindStepOverBreakpoint, "Step over breakpoint trap",
32 thread, eVoteNo,
33 eVoteNoOpinion), // We need to report the run since this happens
Adrian Prantl05097242018-04-30 16:49:04 +000034 // first in the thread plan stack when stepping over
35 // a breakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 m_breakpoint_addr(LLDB_INVALID_ADDRESS),
37 m_auto_continue(false), m_reenabled_breakpoint_site(false)
Jim Inghamb01e7422010-06-19 04:45:32 +000038
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039{
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 m_breakpoint_addr = m_thread.GetRegisterContext()->GetPC();
41 m_breakpoint_site_id =
42 m_thread.GetProcess()->GetBreakpointSiteList().FindIDByAddress(
43 m_breakpoint_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044}
45
Kate Stoneb9c1b512016-09-06 20:57:50 +000046ThreadPlanStepOverBreakpoint::~ThreadPlanStepOverBreakpoint() {}
47
48void ThreadPlanStepOverBreakpoint::GetDescription(
49 Stream *s, lldb::DescriptionLevel level) {
50 s->Printf("Single stepping past breakpoint site %" PRIu64 " at 0x%" PRIx64,
51 m_breakpoint_site_id, (uint64_t)m_breakpoint_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052}
53
Kate Stoneb9c1b512016-09-06 20:57:50 +000054bool ThreadPlanStepOverBreakpoint::ValidatePlan(Stream *error) { return true; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056bool ThreadPlanStepOverBreakpoint::DoPlanExplainsStop(Event *event_ptr) {
57 StopInfoSP stop_info_sp = GetPrivateStopInfo();
58 if (stop_info_sp) {
59 // It's a little surprising that we stop here for a breakpoint hit.
Adrian Prantl05097242018-04-30 16:49:04 +000060 // However, when you single step ONTO a breakpoint we still want to call
61 // that a breakpoint hit, and trigger the actions, etc. Otherwise you
62 // would see the
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 // PC at the breakpoint without having triggered the actions, then you'd
64 // continue, the PC wouldn't change,
Adrian Prantl05097242018-04-30 16:49:04 +000065 // and you'd see the breakpoint hit, which would be odd. So the lower
66 // levels fake "step onto breakpoint address" and return that as a
67 // breakpoint. So our trace step COULD appear as a breakpoint hit if the
68 // next instruction also contained a breakpoint.
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 StopReason reason = stop_info_sp->GetStopReason();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070
Jim Inghama435d732018-05-22 00:06:55 +000071 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
72
73 if (log)
74 log->Printf("Step over breakpoint stopped for reason: %s.",
75 Thread::StopReasonAsCString(reason));
76
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 switch (reason) {
Jim Inghama435d732018-05-22 00:06:55 +000078 case eStopReasonTrace:
79 case eStopReasonNone:
80 return true;
81 case eStopReasonBreakpoint:
82 {
83 // It's a little surprising that we stop here for a breakpoint hit.
84 // However, when you single step ONTO a breakpoint we still want to call
85 // that a breakpoint hit, and trigger the actions, etc. Otherwise you
86 // would see the PC at the breakpoint without having triggered the
87 // actions, then you'd continue, the PC wouldn't change, and you'd see
88 // the breakpoint hit, which would be odd. So the lower levels fake
89 // "step onto breakpoint address" and return that as a breakpoint hit.
90 // So our trace step COULD appear as a breakpoint hit if the next
91 // instruction also contained a breakpoint. We don't want to handle
92 // that, since we really don't know what to do with breakpoint hits.
93 // But make sure we don't set ourselves to auto-continue or we'll wrench
94 // control away from the plans that can deal with this.
95 // Be careful, however, as we may have "seen a breakpoint under the PC
96 // because we stopped without changing the PC, in which case we do want
97 // to re-claim this stop so we'll try again.
98 lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC();
99
100 if (pc_addr == m_breakpoint_addr) {
101 if (log)
Jim Ingham9465ced2018-05-24 17:06:48 +0000102 log->Printf("Got breakpoint stop reason but pc: %x" PRIx64
Jim Inghama435d732018-05-22 00:06:55 +0000103 "hasn't changed.", pc_addr);
104 return true;
105 }
106
107 SetAutoContinue(false);
108 return false;
109 }
110 default:
111 return false;
Jim Inghambc1d7f72012-06-22 20:42:22 +0000112 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 }
114 return false;
115}
116
117bool ThreadPlanStepOverBreakpoint::ShouldStop(Event *event_ptr) {
118 return !ShouldAutoContinue(event_ptr);
119}
120
121bool ThreadPlanStepOverBreakpoint::StopOthers() { return true; }
122
123StateType ThreadPlanStepOverBreakpoint::GetPlanRunState() {
124 return eStateStepping;
125}
126
127bool ThreadPlanStepOverBreakpoint::DoWillResume(StateType resume_state,
128 bool current_plan) {
129 if (current_plan) {
130 BreakpointSiteSP bp_site_sp(
131 m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress(
132 m_breakpoint_addr));
Jim Inghama435d732018-05-22 00:06:55 +0000133 if (bp_site_sp && bp_site_sp->IsEnabled()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 m_thread.GetProcess()->DisableBreakpointSite(bp_site_sp.get());
Jim Inghama435d732018-05-22 00:06:55 +0000135 m_reenabled_breakpoint_site = false;
136 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137 }
138 return true;
139}
140
141bool ThreadPlanStepOverBreakpoint::WillStop() {
142 ReenableBreakpointSite();
143 return true;
144}
145
Jim Inghama435d732018-05-22 00:06:55 +0000146void ThreadPlanStepOverBreakpoint::WillPop() {
147 ReenableBreakpointSite();
148}
149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150bool ThreadPlanStepOverBreakpoint::MischiefManaged() {
151 lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC();
152
153 if (pc_addr == m_breakpoint_addr) {
154 // If we are still at the PC of our breakpoint, then for some reason we
Adrian Prantl05097242018-04-30 16:49:04 +0000155 // didn't get a chance to run.
Jim Inghambc1d7f72012-06-22 20:42:22 +0000156 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 } else {
158 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
159 if (log)
160 log->Printf("Completed step over breakpoint plan.");
161 // Otherwise, re-enable the breakpoint we were stepping over, and we're
162 // done.
163 ReenableBreakpointSite();
164 ThreadPlan::MischiefManaged();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167}
168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169void ThreadPlanStepOverBreakpoint::ReenableBreakpointSite() {
170 if (!m_reenabled_breakpoint_site) {
171 m_reenabled_breakpoint_site = true;
172 BreakpointSiteSP bp_site_sp(
173 m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress(
174 m_breakpoint_addr));
175 if (bp_site_sp) {
176 m_thread.GetProcess()->EnableBreakpointSite(bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 }
179}
180void ThreadPlanStepOverBreakpoint::ThreadDestroyed() {
181 ReenableBreakpointSite();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182}
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184void ThreadPlanStepOverBreakpoint::SetAutoContinue(bool do_it) {
185 m_auto_continue = do_it;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186}
187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188bool ThreadPlanStepOverBreakpoint::ShouldAutoContinue(Event *event_ptr) {
189 return m_auto_continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190}
191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192bool ThreadPlanStepOverBreakpoint::IsPlanStale() {
193 return m_thread.GetRegisterContext()->GetPC() != m_breakpoint_addr;
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000194}