1ac7ddfbfSEd Maste //===-- ThreadPlanStepOverBreakpoint.cpp ------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
11ac7ddfbfSEd Maste 
12ac7ddfbfSEd Maste #include "lldb/Target/Process.h"
13ac7ddfbfSEd Maste #include "lldb/Target/RegisterContext.h"
14f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
15f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
16ac7ddfbfSEd Maste 
17ac7ddfbfSEd Maste using namespace lldb;
18ac7ddfbfSEd Maste using namespace lldb_private;
19ac7ddfbfSEd Maste 
20ac7ddfbfSEd Maste //----------------------------------------------------------------------
21435933ddSDimitry Andric // ThreadPlanStepOverBreakpoint: Single steps over a breakpoint bp_site_sp at
22435933ddSDimitry Andric // the pc.
23ac7ddfbfSEd Maste //----------------------------------------------------------------------
24ac7ddfbfSEd Maste 
ThreadPlanStepOverBreakpoint(Thread & thread)25435933ddSDimitry Andric ThreadPlanStepOverBreakpoint::ThreadPlanStepOverBreakpoint(Thread &thread)
26435933ddSDimitry Andric     : ThreadPlan(
27435933ddSDimitry Andric           ThreadPlan::eKindStepOverBreakpoint, "Step over breakpoint trap",
28435933ddSDimitry Andric           thread, eVoteNo,
29ac7ddfbfSEd Maste           eVoteNoOpinion), // We need to report the run since this happens
30*4ba319b5SDimitry Andric                            // first in the thread plan stack when stepping over
31*4ba319b5SDimitry Andric                            // a breakpoint
32ac7ddfbfSEd Maste       m_breakpoint_addr(LLDB_INVALID_ADDRESS),
33435933ddSDimitry Andric       m_auto_continue(false), m_reenabled_breakpoint_site(false)
34ac7ddfbfSEd Maste 
35ac7ddfbfSEd Maste {
36ac7ddfbfSEd Maste   m_breakpoint_addr = m_thread.GetRegisterContext()->GetPC();
37435933ddSDimitry Andric   m_breakpoint_site_id =
38435933ddSDimitry Andric       m_thread.GetProcess()->GetBreakpointSiteList().FindIDByAddress(
39435933ddSDimitry Andric           m_breakpoint_addr);
40ac7ddfbfSEd Maste }
41ac7ddfbfSEd Maste 
~ThreadPlanStepOverBreakpoint()42435933ddSDimitry Andric ThreadPlanStepOverBreakpoint::~ThreadPlanStepOverBreakpoint() {}
43435933ddSDimitry Andric 
GetDescription(Stream * s,lldb::DescriptionLevel level)44435933ddSDimitry Andric void ThreadPlanStepOverBreakpoint::GetDescription(
45435933ddSDimitry Andric     Stream *s, lldb::DescriptionLevel level) {
46435933ddSDimitry Andric   s->Printf("Single stepping past breakpoint site %" PRIu64 " at 0x%" PRIx64,
47435933ddSDimitry Andric             m_breakpoint_site_id, (uint64_t)m_breakpoint_addr);
48ac7ddfbfSEd Maste }
49ac7ddfbfSEd Maste 
ValidatePlan(Stream * error)50435933ddSDimitry Andric bool ThreadPlanStepOverBreakpoint::ValidatePlan(Stream *error) { return true; }
51ac7ddfbfSEd Maste 
DoPlanExplainsStop(Event * event_ptr)52435933ddSDimitry Andric bool ThreadPlanStepOverBreakpoint::DoPlanExplainsStop(Event *event_ptr) {
53ac7ddfbfSEd Maste   StopInfoSP stop_info_sp = GetPrivateStopInfo();
54435933ddSDimitry Andric   if (stop_info_sp) {
55435933ddSDimitry Andric     // It's a little surprising that we stop here for a breakpoint hit.
56*4ba319b5SDimitry Andric     // However, when you single step ONTO a breakpoint we still want to call
57*4ba319b5SDimitry Andric     // that a breakpoint hit, and trigger the actions, etc.  Otherwise you
58*4ba319b5SDimitry Andric     // would see the
59435933ddSDimitry Andric     // PC at the breakpoint without having triggered the actions, then you'd
60435933ddSDimitry Andric     // continue, the PC wouldn't change,
61*4ba319b5SDimitry Andric     // and you'd see the breakpoint hit, which would be odd. So the lower
62*4ba319b5SDimitry Andric     // levels fake "step onto breakpoint address" and return that as a
63*4ba319b5SDimitry Andric     // breakpoint.  So our trace step COULD appear as a breakpoint hit if the
64*4ba319b5SDimitry Andric     // next instruction also contained a breakpoint.
65ac7ddfbfSEd Maste     StopReason reason = stop_info_sp->GetStopReason();
667aa51b79SEd Maste 
67*4ba319b5SDimitry Andric     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
68*4ba319b5SDimitry Andric 
69*4ba319b5SDimitry Andric     if (log)
70*4ba319b5SDimitry Andric       log->Printf("Step over breakpoint stopped for reason: %s.",
71*4ba319b5SDimitry Andric           Thread::StopReasonAsCString(reason));
72*4ba319b5SDimitry Andric 
73435933ddSDimitry Andric     switch (reason) {
747aa51b79SEd Maste       case eStopReasonTrace:
757aa51b79SEd Maste       case eStopReasonNone:
76ac7ddfbfSEd Maste         return true;
777aa51b79SEd Maste       case eStopReasonBreakpoint:
78*4ba319b5SDimitry Andric       {
79435933ddSDimitry Andric         // It's a little surprising that we stop here for a breakpoint hit.
80*4ba319b5SDimitry Andric         // However, when you single step ONTO a breakpoint we still want to call
81*4ba319b5SDimitry Andric         // that a breakpoint hit, and trigger the actions, etc.  Otherwise you
82435933ddSDimitry Andric         // would see the PC at the breakpoint without having triggered the
83*4ba319b5SDimitry Andric         // actions, then you'd continue, the PC wouldn't change, and you'd see
84*4ba319b5SDimitry Andric         // the breakpoint hit, which would be odd. So the lower levels fake
85*4ba319b5SDimitry Andric         // "step onto breakpoint address" and return that as a breakpoint hit.
86*4ba319b5SDimitry Andric         // So our trace step COULD appear as a breakpoint hit if the next
87*4ba319b5SDimitry Andric         // instruction also contained a breakpoint.  We don't want to handle
88*4ba319b5SDimitry Andric         // that, since we really don't know what to do with breakpoint hits.
89*4ba319b5SDimitry Andric         // But make sure we don't set ourselves to auto-continue or we'll wrench
90*4ba319b5SDimitry Andric         // control away from the plans that can deal with this.
91*4ba319b5SDimitry Andric         // Be careful, however, as we may have "seen a breakpoint under the PC
92*4ba319b5SDimitry Andric         // because we stopped without changing the PC, in which case we do want
93*4ba319b5SDimitry Andric         // to re-claim this stop so we'll try again.
94*4ba319b5SDimitry Andric         lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC();
95*4ba319b5SDimitry Andric 
96*4ba319b5SDimitry Andric         if (pc_addr == m_breakpoint_addr) {
97*4ba319b5SDimitry Andric           if (log)
98*4ba319b5SDimitry Andric             log->Printf("Got breakpoint stop reason but pc: 0x%" PRIx64
99*4ba319b5SDimitry Andric                         "hasn't changed.", pc_addr);
100*4ba319b5SDimitry Andric           return true;
101*4ba319b5SDimitry Andric         }
102*4ba319b5SDimitry Andric 
1037aa51b79SEd Maste         SetAutoContinue(false);
104ac7ddfbfSEd Maste         return false;
105*4ba319b5SDimitry Andric       }
1067aa51b79SEd Maste       default:
1077aa51b79SEd Maste         return false;
1087aa51b79SEd Maste     }
109ac7ddfbfSEd Maste   }
110ac7ddfbfSEd Maste   return false;
111ac7ddfbfSEd Maste }
112ac7ddfbfSEd Maste 
ShouldStop(Event * event_ptr)113435933ddSDimitry Andric bool ThreadPlanStepOverBreakpoint::ShouldStop(Event *event_ptr) {
1147aa51b79SEd Maste   return !ShouldAutoContinue(event_ptr);
115ac7ddfbfSEd Maste }
116ac7ddfbfSEd Maste 
StopOthers()117435933ddSDimitry Andric bool ThreadPlanStepOverBreakpoint::StopOthers() { return true; }
118ac7ddfbfSEd Maste 
GetPlanRunState()119435933ddSDimitry Andric StateType ThreadPlanStepOverBreakpoint::GetPlanRunState() {
120ac7ddfbfSEd Maste   return eStateStepping;
121ac7ddfbfSEd Maste }
122ac7ddfbfSEd Maste 
DoWillResume(StateType resume_state,bool current_plan)123435933ddSDimitry Andric bool ThreadPlanStepOverBreakpoint::DoWillResume(StateType resume_state,
124435933ddSDimitry Andric                                                 bool current_plan) {
125435933ddSDimitry Andric   if (current_plan) {
126435933ddSDimitry Andric     BreakpointSiteSP bp_site_sp(
127435933ddSDimitry Andric         m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress(
128435933ddSDimitry Andric             m_breakpoint_addr));
129*4ba319b5SDimitry Andric     if (bp_site_sp && bp_site_sp->IsEnabled()) {
130ac7ddfbfSEd Maste       m_thread.GetProcess()->DisableBreakpointSite(bp_site_sp.get());
131*4ba319b5SDimitry Andric       m_reenabled_breakpoint_site = false;
132*4ba319b5SDimitry Andric     }
133ac7ddfbfSEd Maste   }
134ac7ddfbfSEd Maste   return true;
135ac7ddfbfSEd Maste }
136ac7ddfbfSEd Maste 
WillStop()137435933ddSDimitry Andric bool ThreadPlanStepOverBreakpoint::WillStop() {
138ac7ddfbfSEd Maste   ReenableBreakpointSite();
139ac7ddfbfSEd Maste   return true;
140ac7ddfbfSEd Maste }
141ac7ddfbfSEd Maste 
WillPop()142*4ba319b5SDimitry Andric void ThreadPlanStepOverBreakpoint::WillPop() {
143*4ba319b5SDimitry Andric   ReenableBreakpointSite();
144*4ba319b5SDimitry Andric }
145*4ba319b5SDimitry Andric 
MischiefManaged()146435933ddSDimitry Andric bool ThreadPlanStepOverBreakpoint::MischiefManaged() {
147ac7ddfbfSEd Maste   lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC();
148ac7ddfbfSEd Maste 
149435933ddSDimitry Andric   if (pc_addr == m_breakpoint_addr) {
150435933ddSDimitry Andric     // If we are still at the PC of our breakpoint, then for some reason we
151*4ba319b5SDimitry Andric     // didn't get a chance to run.
152ac7ddfbfSEd Maste     return false;
153435933ddSDimitry Andric   } else {
154ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
155ac7ddfbfSEd Maste     if (log)
156ac7ddfbfSEd Maste       log->Printf("Completed step over breakpoint plan.");
157435933ddSDimitry Andric     // Otherwise, re-enable the breakpoint we were stepping over, and we're
158435933ddSDimitry Andric     // done.
159ac7ddfbfSEd Maste     ReenableBreakpointSite();
160ac7ddfbfSEd Maste     ThreadPlan::MischiefManaged();
161ac7ddfbfSEd Maste     return true;
162ac7ddfbfSEd Maste   }
163ac7ddfbfSEd Maste }
164ac7ddfbfSEd Maste 
ReenableBreakpointSite()165435933ddSDimitry Andric void ThreadPlanStepOverBreakpoint::ReenableBreakpointSite() {
166435933ddSDimitry Andric   if (!m_reenabled_breakpoint_site) {
167ac7ddfbfSEd Maste     m_reenabled_breakpoint_site = true;
168435933ddSDimitry Andric     BreakpointSiteSP bp_site_sp(
169435933ddSDimitry Andric         m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress(
170435933ddSDimitry Andric             m_breakpoint_addr));
171435933ddSDimitry Andric     if (bp_site_sp) {
172ac7ddfbfSEd Maste       m_thread.GetProcess()->EnableBreakpointSite(bp_site_sp.get());
173ac7ddfbfSEd Maste     }
174ac7ddfbfSEd Maste   }
175ac7ddfbfSEd Maste }
ThreadDestroyed()176435933ddSDimitry Andric void ThreadPlanStepOverBreakpoint::ThreadDestroyed() {
177ac7ddfbfSEd Maste   ReenableBreakpointSite();
178ac7ddfbfSEd Maste }
179ac7ddfbfSEd Maste 
SetAutoContinue(bool do_it)180435933ddSDimitry Andric void ThreadPlanStepOverBreakpoint::SetAutoContinue(bool do_it) {
181ac7ddfbfSEd Maste   m_auto_continue = do_it;
182ac7ddfbfSEd Maste }
183ac7ddfbfSEd Maste 
ShouldAutoContinue(Event * event_ptr)184435933ddSDimitry Andric bool ThreadPlanStepOverBreakpoint::ShouldAutoContinue(Event *event_ptr) {
185ac7ddfbfSEd Maste   return m_auto_continue;
186ac7ddfbfSEd Maste }
1877aa51b79SEd Maste 
IsPlanStale()188435933ddSDimitry Andric bool ThreadPlanStepOverBreakpoint::IsPlanStale() {
1897aa51b79SEd Maste   return m_thread.GetRegisterContext()->GetPC() != m_breakpoint_addr;
1907aa51b79SEd Maste }
191