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/Core/Log.h"
17 #include "lldb/Core/Stream.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Target/RegisterContext.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 //----------------------------------------------------------------------
25 // ThreadPlanStepOverBreakpoint: Single steps over a breakpoint bp_site_sp at the pc.
26 //----------------------------------------------------------------------
27 
28 ThreadPlanStepOverBreakpoint::ThreadPlanStepOverBreakpoint (Thread &thread) :
29     ThreadPlan (ThreadPlan::eKindStepOverBreakpoint, "Step over breakpoint trap",
30                 thread,
31                 eVoteNo,
32                 eVoteNoOpinion),  // We need to report the run since this happens
33                             // first in the thread plan stack when stepping
34                             // over a breakpoint
35     m_breakpoint_addr (LLDB_INVALID_ADDRESS),
36     m_auto_continue(false),
37     m_reenabled_breakpoint_site (false)
38 
39 {
40     m_breakpoint_addr = m_thread.GetRegisterContext()->GetPC();
41     m_breakpoint_site_id =  m_thread.GetProcess()->GetBreakpointSiteList().FindIDByAddress (m_breakpoint_addr);
42 }
43 
44 ThreadPlanStepOverBreakpoint::~ThreadPlanStepOverBreakpoint ()
45 {
46 }
47 
48 void
49 ThreadPlanStepOverBreakpoint::GetDescription (Stream *s, lldb::DescriptionLevel level)
50 {
51     s->Printf("Single stepping past breakpoint site %" PRIu64 " at 0x%" PRIx64, m_breakpoint_site_id, (uint64_t)m_breakpoint_addr);
52 }
53 
54 bool
55 ThreadPlanStepOverBreakpoint::ValidatePlan (Stream *error)
56 {
57     return true;
58 }
59 
60 bool
61 ThreadPlanStepOverBreakpoint::DoPlanExplainsStop (Event *event_ptr)
62 {
63     StopInfoSP stop_info_sp = GetPrivateStopInfo ();
64     if (stop_info_sp)
65     {
66         // It's a little surprising that we stop here for a breakpoint hit.  However, when you single step ONTO a breakpoint
67         // we still want to call that a breakpoint hit, and trigger the actions, etc.  Otherwise you would see the
68         // PC at the breakpoint without having triggered the actions, then you'd continue, the PC wouldn't change,
69         // and you'd see the breakpoint hit, which would be odd.
70         // So the lower levels fake "step onto breakpoint address" and return that as a breakpoint.  So our trace
71         // step COULD appear as a breakpoint hit if the next instruction also contained a breakpoint.
72         StopReason reason = stop_info_sp->GetStopReason();
73 
74         switch (reason)
75         {
76         case eStopReasonTrace:
77         case eStopReasonNone:
78             return true;
79         case eStopReasonBreakpoint:
80             // It's a little surprising that we stop here for a breakpoint hit.  However, when you single step ONTO a
81             // breakpoint we still want to call that a breakpoint hit, and trigger the actions, etc.  Otherwise you
82             // would see the PC at the breakpoint without having triggered the actions, then you'd continue, the PC
83             // wouldn't change, and you'd see the breakpoint hit, which would be odd.
84             // So the lower levels fake "step onto breakpoint address" and return that as a breakpoint hit.  So our trace
85             // step COULD appear as a breakpoint hit if the next instruction also contained a breakpoint.  We don't want
86             // to handle that, since we really don't know what to do with breakpoint hits.  But make sure we don't set
87             // ourselves to auto-continue or we'll wrench control away from 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
98 ThreadPlanStepOverBreakpoint::ShouldStop (Event *event_ptr)
99 {
100     return !ShouldAutoContinue(event_ptr);
101 }
102 
103 bool
104 ThreadPlanStepOverBreakpoint::StopOthers ()
105 {
106     return true;
107 }
108 
109 StateType
110 ThreadPlanStepOverBreakpoint::GetPlanRunState ()
111 {
112     return eStateStepping;
113 }
114 
115 bool
116 ThreadPlanStepOverBreakpoint::DoWillResume (StateType resume_state, bool current_plan)
117 {
118     if (current_plan)
119     {
120         BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress (m_breakpoint_addr));
121         if (bp_site_sp  && bp_site_sp->IsEnabled())
122             m_thread.GetProcess()->DisableBreakpointSite (bp_site_sp.get());
123     }
124     return true;
125 }
126 
127 bool
128 ThreadPlanStepOverBreakpoint::WillStop ()
129 {
130     ReenableBreakpointSite ();
131     return true;
132 }
133 
134 bool
135 ThreadPlanStepOverBreakpoint::MischiefManaged ()
136 {
137     lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC();
138 
139     if (pc_addr == m_breakpoint_addr)
140     {
141         // If we are still at the PC of our breakpoint, then for some reason we didn't
142         // get a chance to run.
143         return false;
144     }
145     else
146     {
147         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
148         if (log)
149             log->Printf("Completed step over breakpoint plan.");
150         // Otherwise, re-enable the breakpoint we were stepping over, and we're done.
151         ReenableBreakpointSite ();
152         ThreadPlan::MischiefManaged ();
153         return true;
154     }
155 }
156 
157 void
158 ThreadPlanStepOverBreakpoint::ReenableBreakpointSite ()
159 {
160     if (!m_reenabled_breakpoint_site)
161     {
162         m_reenabled_breakpoint_site = true;
163         BreakpointSiteSP bp_site_sp (m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress (m_breakpoint_addr));
164         if (bp_site_sp)
165         {
166             m_thread.GetProcess()->EnableBreakpointSite (bp_site_sp.get());
167         }
168     }
169 }
170 void
171 ThreadPlanStepOverBreakpoint::ThreadDestroyed ()
172 {
173     ReenableBreakpointSite ();
174 }
175 
176 void
177 ThreadPlanStepOverBreakpoint::SetAutoContinue (bool do_it)
178 {
179     m_auto_continue = do_it;
180 }
181 
182 bool
183 ThreadPlanStepOverBreakpoint::ShouldAutoContinue (Event *event_ptr)
184 {
185     return m_auto_continue;
186 }
187 
188 bool
189 ThreadPlanStepOverBreakpoint::IsPlanStale()
190 {
191     return m_thread.GetRegisterContext()->GetPC() != m_breakpoint_addr;
192 }
193 
194