1 //===-- ThreadPlanStepOverBreakpoint.cpp ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
12 #include "lldb/Target/Process.h"
13 #include "lldb/Target/RegisterContext.h"
14 #include "lldb/Utility/Log.h"
15 #include "lldb/Utility/Stream.h"
18 using namespace lldb_private;
20 //----------------------------------------------------------------------
21 // ThreadPlanStepOverBreakpoint: Single steps over a breakpoint bp_site_sp at
23 //----------------------------------------------------------------------
25 ThreadPlanStepOverBreakpoint::ThreadPlanStepOverBreakpoint(Thread &thread)
27 ThreadPlan::eKindStepOverBreakpoint, "Step over breakpoint trap",
29 eVoteNoOpinion), // We need to report the run since this happens
30 // first in the thread plan stack when stepping over
32 m_breakpoint_addr(LLDB_INVALID_ADDRESS),
33 m_auto_continue(false), m_reenabled_breakpoint_site(false)
36 m_breakpoint_addr = m_thread.GetRegisterContext()->GetPC();
37 m_breakpoint_site_id =
38 m_thread.GetProcess()->GetBreakpointSiteList().FindIDByAddress(
42 ThreadPlanStepOverBreakpoint::~ThreadPlanStepOverBreakpoint() {}
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);
50 bool ThreadPlanStepOverBreakpoint::ValidatePlan(Stream *error) { return true; }
52 bool ThreadPlanStepOverBreakpoint::DoPlanExplainsStop(Event *event_ptr) {
53 StopInfoSP stop_info_sp = GetPrivateStopInfo();
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
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();
67 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
70 log->Printf("Step over breakpoint stopped for reason: %s.",
71 Thread::StopReasonAsCString(reason));
74 case eStopReasonTrace:
77 case eStopReasonBreakpoint:
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();
96 if (pc_addr == m_breakpoint_addr) {
98 log->Printf("Got breakpoint stop reason but pc: 0x%" PRIx64
99 "hasn't changed.", pc_addr);
103 SetAutoContinue(false);
113 bool ThreadPlanStepOverBreakpoint::ShouldStop(Event *event_ptr) {
114 return !ShouldAutoContinue(event_ptr);
117 bool ThreadPlanStepOverBreakpoint::StopOthers() { return true; }
119 StateType ThreadPlanStepOverBreakpoint::GetPlanRunState() {
120 return eStateStepping;
123 bool ThreadPlanStepOverBreakpoint::DoWillResume(StateType resume_state,
126 BreakpointSiteSP bp_site_sp(
127 m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress(
129 if (bp_site_sp && bp_site_sp->IsEnabled()) {
130 m_thread.GetProcess()->DisableBreakpointSite(bp_site_sp.get());
131 m_reenabled_breakpoint_site = false;
137 bool ThreadPlanStepOverBreakpoint::WillStop() {
138 ReenableBreakpointSite();
142 void ThreadPlanStepOverBreakpoint::WillPop() {
143 ReenableBreakpointSite();
146 bool ThreadPlanStepOverBreakpoint::MischiefManaged() {
147 lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC();
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.
154 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
156 log->Printf("Completed step over breakpoint plan.");
157 // Otherwise, re-enable the breakpoint we were stepping over, and we're
159 ReenableBreakpointSite();
160 ThreadPlan::MischiefManaged();
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(
172 m_thread.GetProcess()->EnableBreakpointSite(bp_site_sp.get());
176 void ThreadPlanStepOverBreakpoint::ThreadDestroyed() {
177 ReenableBreakpointSite();
180 void ThreadPlanStepOverBreakpoint::SetAutoContinue(bool do_it) {
181 m_auto_continue = do_it;
184 bool ThreadPlanStepOverBreakpoint::ShouldAutoContinue(Event *event_ptr) {
185 return m_auto_continue;
188 bool ThreadPlanStepOverBreakpoint::IsPlanStale() {
189 return m_thread.GetRegisterContext()->GetPC() != m_breakpoint_addr;