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