blob: 4770b57ab7f2ec88fe0242e6519357381dbfc16a [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ThreadPlanStepOverBreakpoint.cpp ------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
10
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "lldb/Target/Process.h"
12#include "lldb/Target/RegisterContext.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000013#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000014#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015
16using namespace lldb;
17using namespace lldb_private;
18
Kate Stoneb9c1b512016-09-06 20:57:50 +000019// ThreadPlanStepOverBreakpoint: Single steps over a breakpoint bp_site_sp at
20// the pc.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021
Kate Stoneb9c1b512016-09-06 20:57:50 +000022ThreadPlanStepOverBreakpoint::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
Adrian Prantl05097242018-04-30 16:49:04 +000027 // first in the thread plan stack when stepping over
28 // a breakpoint
Kate Stoneb9c1b512016-09-06 20:57:50 +000029 m_breakpoint_addr(LLDB_INVALID_ADDRESS),
30 m_auto_continue(false), m_reenabled_breakpoint_site(false)
Jim Inghamb01e7422010-06-19 04:45:32 +000031
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032{
Kate Stoneb9c1b512016-09-06 20:57:50 +000033 m_breakpoint_addr = m_thread.GetRegisterContext()->GetPC();
34 m_breakpoint_site_id =
35 m_thread.GetProcess()->GetBreakpointSiteList().FindIDByAddress(
36 m_breakpoint_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037}
38
Kate Stoneb9c1b512016-09-06 20:57:50 +000039ThreadPlanStepOverBreakpoint::~ThreadPlanStepOverBreakpoint() {}
40
41void 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);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045}
46
Kate Stoneb9c1b512016-09-06 20:57:50 +000047bool ThreadPlanStepOverBreakpoint::ValidatePlan(Stream *error) { return true; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049bool 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.
Adrian Prantl05097242018-04-30 16:49:04 +000053 // 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
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 // PC at the breakpoint without having triggered the actions, then you'd
57 // continue, the PC wouldn't change,
Adrian Prantl05097242018-04-30 16:49:04 +000058 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 StopReason reason = stop_info_sp->GetStopReason();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
Jim Inghama435d732018-05-22 00:06:55 +000064 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
65
66 if (log)
67 log->Printf("Step over breakpoint stopped for reason: %s.",
68 Thread::StopReasonAsCString(reason));
69
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 switch (reason) {
Jim Inghama435d732018-05-22 00:06:55 +000071 case eStopReasonTrace:
72 case eStopReasonNone:
73 return true;
74 case eStopReasonBreakpoint:
75 {
76 // It's a little surprising that we stop here for a breakpoint hit.
77 // However, when you single step ONTO a breakpoint we still want to call
78 // that a breakpoint hit, and trigger the actions, etc. Otherwise you
79 // would see the PC at the breakpoint without having triggered the
80 // actions, then you'd continue, the PC wouldn't change, and you'd see
81 // the breakpoint hit, which would be odd. So the lower levels fake
82 // "step onto breakpoint address" and return that as a breakpoint hit.
83 // So our trace step COULD appear as a breakpoint hit if the next
84 // instruction also contained a breakpoint. We don't want to handle
85 // that, since we really don't know what to do with breakpoint hits.
86 // But make sure we don't set ourselves to auto-continue or we'll wrench
87 // control away from the plans that can deal with this.
88 // Be careful, however, as we may have "seen a breakpoint under the PC
89 // because we stopped without changing the PC, in which case we do want
90 // to re-claim this stop so we'll try again.
91 lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC();
92
93 if (pc_addr == m_breakpoint_addr) {
94 if (log)
Benjamin Kramer192fe372018-05-25 12:59:59 +000095 log->Printf("Got breakpoint stop reason but pc: 0x%" PRIx64
Jim Inghama435d732018-05-22 00:06:55 +000096 "hasn't changed.", pc_addr);
97 return true;
98 }
99
100 SetAutoContinue(false);
101 return false;
102 }
103 default:
104 return false;
Jim Inghambc1d7f72012-06-22 20:42:22 +0000105 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 }
107 return false;
108}
109
110bool ThreadPlanStepOverBreakpoint::ShouldStop(Event *event_ptr) {
111 return !ShouldAutoContinue(event_ptr);
112}
113
114bool ThreadPlanStepOverBreakpoint::StopOthers() { return true; }
115
116StateType ThreadPlanStepOverBreakpoint::GetPlanRunState() {
117 return eStateStepping;
118}
119
120bool ThreadPlanStepOverBreakpoint::DoWillResume(StateType resume_state,
121 bool current_plan) {
122 if (current_plan) {
123 BreakpointSiteSP bp_site_sp(
124 m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress(
125 m_breakpoint_addr));
Jim Inghama435d732018-05-22 00:06:55 +0000126 if (bp_site_sp && bp_site_sp->IsEnabled()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000127 m_thread.GetProcess()->DisableBreakpointSite(bp_site_sp.get());
Jim Inghama435d732018-05-22 00:06:55 +0000128 m_reenabled_breakpoint_site = false;
129 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 }
131 return true;
132}
133
134bool ThreadPlanStepOverBreakpoint::WillStop() {
135 ReenableBreakpointSite();
136 return true;
137}
138
Jim Inghama435d732018-05-22 00:06:55 +0000139void ThreadPlanStepOverBreakpoint::WillPop() {
140 ReenableBreakpointSite();
141}
142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143bool ThreadPlanStepOverBreakpoint::MischiefManaged() {
144 lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC();
145
146 if (pc_addr == m_breakpoint_addr) {
147 // If we are still at the PC of our breakpoint, then for some reason we
Adrian Prantl05097242018-04-30 16:49:04 +0000148 // didn't get a chance to run.
Jim Inghambc1d7f72012-06-22 20:42:22 +0000149 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 } else {
151 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
152 if (log)
153 log->Printf("Completed step over breakpoint plan.");
154 // Otherwise, re-enable the breakpoint we were stepping over, and we're
155 // done.
156 ReenableBreakpointSite();
157 ThreadPlan::MischiefManaged();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160}
161
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162void ThreadPlanStepOverBreakpoint::ReenableBreakpointSite() {
163 if (!m_reenabled_breakpoint_site) {
164 m_reenabled_breakpoint_site = true;
165 BreakpointSiteSP bp_site_sp(
166 m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress(
167 m_breakpoint_addr));
168 if (bp_site_sp) {
169 m_thread.GetProcess()->EnableBreakpointSite(bp_site_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 }
172}
173void ThreadPlanStepOverBreakpoint::ThreadDestroyed() {
174 ReenableBreakpointSite();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175}
176
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177void ThreadPlanStepOverBreakpoint::SetAutoContinue(bool do_it) {
178 m_auto_continue = do_it;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179}
180
Kate Stoneb9c1b512016-09-06 20:57:50 +0000181bool ThreadPlanStepOverBreakpoint::ShouldAutoContinue(Event *event_ptr) {
182 return m_auto_continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183}
184
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185bool ThreadPlanStepOverBreakpoint::IsPlanStale() {
186 return m_thread.GetRegisterContext()->GetPC() != m_breakpoint_addr;
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000187}