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 35 // over 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 61 // we still want to call that a breakpoint hit, and trigger the actions, 62 // etc. Otherwise you 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. 66 // So the lower levels fake "step onto breakpoint address" and return that 67 // as a breakpoint. So our trace 68 // step COULD appear as a breakpoint hit if the next instruction also 69 // contained a breakpoint. 70 StopReason reason = stop_info_sp->GetStopReason(); 71 72 switch (reason) { 73 case eStopReasonTrace: 74 case eStopReasonNone: 75 return true; 76 case eStopReasonBreakpoint: 77 // It's a little surprising that we stop here for a breakpoint hit. 78 // However, when you single step ONTO a 79 // breakpoint we still want to call that a breakpoint hit, and trigger the 80 // actions, etc. Otherwise you 81 // would see the PC at the breakpoint without having triggered the 82 // 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 85 // as a breakpoint hit. So our trace 86 // step COULD appear as a breakpoint hit if the next instruction also 87 // contained a breakpoint. We don't want 88 // to handle that, since we really don't know what to do with breakpoint 89 // hits. But make sure we don't set 90 // ourselves to auto-continue or we'll wrench control away from the plans 91 // that can deal with this. 92 SetAutoContinue(false); 93 return false; 94 default: 95 return false; 96 } 97 } 98 return false; 99 } 100 101 bool ThreadPlanStepOverBreakpoint::ShouldStop(Event *event_ptr) { 102 return !ShouldAutoContinue(event_ptr); 103 } 104 105 bool ThreadPlanStepOverBreakpoint::StopOthers() { return true; } 106 107 StateType ThreadPlanStepOverBreakpoint::GetPlanRunState() { 108 return eStateStepping; 109 } 110 111 bool ThreadPlanStepOverBreakpoint::DoWillResume(StateType resume_state, 112 bool current_plan) { 113 if (current_plan) { 114 BreakpointSiteSP bp_site_sp( 115 m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress( 116 m_breakpoint_addr)); 117 if (bp_site_sp && bp_site_sp->IsEnabled()) 118 m_thread.GetProcess()->DisableBreakpointSite(bp_site_sp.get()); 119 } 120 return true; 121 } 122 123 bool ThreadPlanStepOverBreakpoint::WillStop() { 124 ReenableBreakpointSite(); 125 return true; 126 } 127 128 bool ThreadPlanStepOverBreakpoint::MischiefManaged() { 129 lldb::addr_t pc_addr = m_thread.GetRegisterContext()->GetPC(); 130 131 if (pc_addr == m_breakpoint_addr) { 132 // If we are still at the PC of our breakpoint, then for some reason we 133 // didn't 134 // get a chance to run. 135 return false; 136 } else { 137 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 138 if (log) 139 log->Printf("Completed step over breakpoint plan."); 140 // Otherwise, re-enable the breakpoint we were stepping over, and we're 141 // done. 142 ReenableBreakpointSite(); 143 ThreadPlan::MischiefManaged(); 144 return true; 145 } 146 } 147 148 void ThreadPlanStepOverBreakpoint::ReenableBreakpointSite() { 149 if (!m_reenabled_breakpoint_site) { 150 m_reenabled_breakpoint_site = true; 151 BreakpointSiteSP bp_site_sp( 152 m_thread.GetProcess()->GetBreakpointSiteList().FindByAddress( 153 m_breakpoint_addr)); 154 if (bp_site_sp) { 155 m_thread.GetProcess()->EnableBreakpointSite(bp_site_sp.get()); 156 } 157 } 158 } 159 void ThreadPlanStepOverBreakpoint::ThreadDestroyed() { 160 ReenableBreakpointSite(); 161 } 162 163 void ThreadPlanStepOverBreakpoint::SetAutoContinue(bool do_it) { 164 m_auto_continue = do_it; 165 } 166 167 bool ThreadPlanStepOverBreakpoint::ShouldAutoContinue(Event *event_ptr) { 168 return m_auto_continue; 169 } 170 171 bool ThreadPlanStepOverBreakpoint::IsPlanStale() { 172 return m_thread.GetRegisterContext()->GetPC() != m_breakpoint_addr; 173 } 174