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 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/Target/Process.h"
17 #include "lldb/Target/RegisterContext.h"
18 #include "lldb/Utility/Log.h"
19 #include "lldb/Utility/Stream.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 //----------------------------------------------------------------------
25 // ThreadPlanStepOverBreakpoint: Single steps over a breakpoint bp_site_sp at
26 // the pc.
27 //----------------------------------------------------------------------
28 
29 ThreadPlanStepOverBreakpoint::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
34                            // first in the thread plan stack when stepping over
35                            // a breakpoint
36       m_breakpoint_addr(LLDB_INVALID_ADDRESS),
37       m_auto_continue(false), m_reenabled_breakpoint_site(false)
38 
39 {
40   m_breakpoint_addr = m_thread.GetRegisterContext()->GetPC();
41   m_breakpoint_site_id =
42       m_thread.GetProcess()->GetBreakpointSiteList().FindIDByAddress(
43           m_breakpoint_addr);
44 }
45 
46 ThreadPlanStepOverBreakpoint::~ThreadPlanStepOverBreakpoint() {}
47 
48 void 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);
52 }
53 
54 bool ThreadPlanStepOverBreakpoint::ValidatePlan(Stream *error) { return true; }
55 
56 bool 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.
60     // 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
63     // PC at the breakpoint without having triggered the actions, then you'd
64     // continue, the PC wouldn't change,
65     // 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.
69     StopReason reason = stop_info_sp->GetStopReason();
70 
71     switch (reason) {
72     case eStopReasonTrace:
73     case eStopReasonNone:
74       return true;
75     case eStopReasonBreakpoint:
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 "step
82       // onto breakpoint address" and return that as a breakpoint hit.  So our
83       // trace step COULD appear as a breakpoint hit if the next instruction
84       // also contained a breakpoint.  We don't want to handle that, since we
85       // really don't know what to do with breakpoint hits.  But make sure we
86       // don't set ourselves to auto-continue or we'll wrench control away from
87       // the plans that can deal with this.
88       SetAutoContinue(false);
89       return false;
90     default:
91       return false;
92     }
93   }
94   return false;
95 }
96 
97 bool ThreadPlanStepOverBreakpoint::ShouldStop(Event *event_ptr) {
98   return !ShouldAutoContinue(event_ptr);
99 }
100 
101 bool ThreadPlanStepOverBreakpoint::StopOthers() { return true; }
102 
103 StateType ThreadPlanStepOverBreakpoint::GetPlanRunState() {
104   return eStateStepping;
105 }
106 
107 bool ThreadPlanStepOverBreakpoint::DoWillResume(StateType resume_state,
108                                                 bool current_plan) {
109   if (current_plan) {
110     BreakpointSiteSP bp_site_sp(
111         m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress(
112             m_breakpoint_addr));
113     if (bp_site_sp && bp_site_sp->IsEnabled())
114       m_thread.GetProcess()->DisableBreakpointSite(bp_site_sp.get());
115   }
116   return true;
117 }
118 
119 bool ThreadPlanStepOverBreakpoint::WillStop() {
120   ReenableBreakpointSite();
121   return true;
122 }
123 
124 bool ThreadPlanStepOverBreakpoint::MischiefManaged() {
125   lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC();
126 
127   if (pc_addr == m_breakpoint_addr) {
128     // If we are still at the PC of our breakpoint, then for some reason we
129     // didn't get a chance to run.
130     return false;
131   } else {
132     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
133     if (log)
134       log->Printf("Completed step over breakpoint plan.");
135     // Otherwise, re-enable the breakpoint we were stepping over, and we're
136     // done.
137     ReenableBreakpointSite();
138     ThreadPlan::MischiefManaged();
139     return true;
140   }
141 }
142 
143 void ThreadPlanStepOverBreakpoint::ReenableBreakpointSite() {
144   if (!m_reenabled_breakpoint_site) {
145     m_reenabled_breakpoint_site = true;
146     BreakpointSiteSP bp_site_sp(
147         m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress(
148             m_breakpoint_addr));
149     if (bp_site_sp) {
150       m_thread.GetProcess()->EnableBreakpointSite(bp_site_sp.get());
151     }
152   }
153 }
154 void ThreadPlanStepOverBreakpoint::ThreadDestroyed() {
155   ReenableBreakpointSite();
156 }
157 
158 void ThreadPlanStepOverBreakpoint::SetAutoContinue(bool do_it) {
159   m_auto_continue = do_it;
160 }
161 
162 bool ThreadPlanStepOverBreakpoint::ShouldAutoContinue(Event *event_ptr) {
163   return m_auto_continue;
164 }
165 
166 bool ThreadPlanStepOverBreakpoint::IsPlanStale() {
167   return m_thread.GetRegisterContext()->GetPC() != m_breakpoint_addr;
168 }
169