1 //===-- ThreadPlanStepThrough.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 // C Includes 11 // C++ Includes 12 // Other libraries and framework includes 13 // Project includes 14 #include "lldb/Target/ThreadPlanStepThrough.h" 15 #include "lldb/Breakpoint/Breakpoint.h" 16 #include "lldb/Target/DynamicLoader.h" 17 #include "lldb/Target/ObjCLanguageRuntime.h" 18 #include "lldb/Target/Process.h" 19 #include "lldb/Target/RegisterContext.h" 20 #include "lldb/Target/Target.h" 21 #include "lldb/Utility/Log.h" 22 #include "lldb/Utility/Stream.h" 23 24 using namespace lldb; 25 using namespace lldb_private; 26 27 //---------------------------------------------------------------------- 28 // ThreadPlanStepThrough: If the current instruction is a trampoline, step 29 // through it If it is the beginning of the prologue of a function, step 30 // through that as well. 31 // FIXME: At present only handles DYLD trampolines. 32 //---------------------------------------------------------------------- 33 34 ThreadPlanStepThrough::ThreadPlanStepThrough(Thread &thread, 35 StackID &m_stack_id, 36 bool stop_others) 37 : ThreadPlan(ThreadPlan::eKindStepThrough, 38 "Step through trampolines and prologues", thread, 39 eVoteNoOpinion, eVoteNoOpinion), 40 m_start_address(0), m_backstop_bkpt_id(LLDB_INVALID_BREAK_ID), 41 m_backstop_addr(LLDB_INVALID_ADDRESS), m_return_stack_id(m_stack_id), 42 m_stop_others(stop_others) { 43 LookForPlanToStepThroughFromCurrentPC(); 44 45 // If we don't get a valid step through plan, don't bother to set up a 46 // backstop. 47 if (m_sub_plan_sp) { 48 m_start_address = GetThread().GetRegisterContext()->GetPC(0); 49 50 // We are going to return back to the concrete frame 1, we might pass by 51 // some inlined code that we're in the middle of by doing this, but it's 52 // easier than trying to figure out where the inlined code might return to. 53 54 StackFrameSP return_frame_sp = m_thread.GetFrameWithStackID(m_stack_id); 55 56 if (return_frame_sp) { 57 m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress( 58 m_thread.CalculateTarget().get()); 59 Breakpoint *return_bp = 60 m_thread.GetProcess() 61 ->GetTarget() 62 .CreateBreakpoint(m_backstop_addr, true, false) 63 .get(); 64 if (return_bp != nullptr) { 65 return_bp->SetThreadID(m_thread.GetID()); 66 m_backstop_bkpt_id = return_bp->GetID(); 67 return_bp->SetBreakpointKind("step-through-backstop"); 68 } 69 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 70 if (log) { 71 log->Printf("Setting backstop breakpoint %d at address: 0x%" PRIx64, 72 m_backstop_bkpt_id, m_backstop_addr); 73 } 74 } 75 } 76 } 77 78 ThreadPlanStepThrough::~ThreadPlanStepThrough() { ClearBackstopBreakpoint(); } 79 80 void ThreadPlanStepThrough::DidPush() { 81 if (m_sub_plan_sp) 82 PushPlan(m_sub_plan_sp); 83 } 84 85 void ThreadPlanStepThrough::LookForPlanToStepThroughFromCurrentPC() { 86 DynamicLoader *loader = m_thread.GetProcess()->GetDynamicLoader(); 87 if (loader) 88 m_sub_plan_sp = 89 loader->GetStepThroughTrampolinePlan(m_thread, m_stop_others); 90 91 // If that didn't come up with anything, try the ObjC runtime plugin: 92 if (!m_sub_plan_sp.get()) { 93 ObjCLanguageRuntime *objc_runtime = 94 m_thread.GetProcess()->GetObjCLanguageRuntime(); 95 if (objc_runtime) 96 m_sub_plan_sp = 97 objc_runtime->GetStepThroughTrampolinePlan(m_thread, m_stop_others); 98 } 99 100 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 101 if (log) { 102 lldb::addr_t current_address = GetThread().GetRegisterContext()->GetPC(0); 103 if (m_sub_plan_sp) { 104 StreamString s; 105 m_sub_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull); 106 log->Printf("Found step through plan from 0x%" PRIx64 ": %s", 107 current_address, s.GetData()); 108 } else { 109 log->Printf("Couldn't find step through plan from address 0x%" PRIx64 ".", 110 current_address); 111 } 112 } 113 } 114 115 void ThreadPlanStepThrough::GetDescription(Stream *s, 116 lldb::DescriptionLevel level) { 117 if (level == lldb::eDescriptionLevelBrief) 118 s->Printf("Step through"); 119 else { 120 s->PutCString("Stepping through trampoline code from: "); 121 s->Address(m_start_address, sizeof(addr_t)); 122 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) { 123 s->Printf(" with backstop breakpoint ID: %d at address: ", 124 m_backstop_bkpt_id); 125 s->Address(m_backstop_addr, sizeof(addr_t)); 126 } else 127 s->PutCString(" unable to set a backstop breakpoint."); 128 } 129 } 130 131 bool ThreadPlanStepThrough::ValidatePlan(Stream *error) { 132 return m_sub_plan_sp.get() != nullptr; 133 } 134 135 bool ThreadPlanStepThrough::DoPlanExplainsStop(Event *event_ptr) { 136 // If we have a sub-plan, it will have been asked first if we explain the 137 // stop, and we won't get asked. The only time we would be the one directly 138 // asked this question is if we hit our backstop breakpoint. 139 140 return HitOurBackstopBreakpoint(); 141 } 142 143 bool ThreadPlanStepThrough::ShouldStop(Event *event_ptr) { 144 // If we've already marked ourselves done, then we're done... 145 if (IsPlanComplete()) 146 return true; 147 148 // First, did we hit the backstop breakpoint? 149 if (HitOurBackstopBreakpoint()) { 150 SetPlanComplete(true); 151 return true; 152 } 153 154 // If we don't have a sub-plan, then we're also done (can't see how we would 155 // ever get here without a plan, but just in case. 156 157 if (!m_sub_plan_sp) { 158 SetPlanComplete(); 159 return true; 160 } 161 162 // If the current sub plan is not done, we don't want to stop. Actually, we 163 // probably won't ever get here in this state, since we generally won't get 164 // asked any questions if out current sub-plan is not done... 165 if (!m_sub_plan_sp->IsPlanComplete()) 166 return false; 167 168 // If our current sub plan failed, then let's just run to our backstop. If 169 // we can't do that then just stop. 170 if (!m_sub_plan_sp->PlanSucceeded()) { 171 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) { 172 m_sub_plan_sp.reset(); 173 return false; 174 } else { 175 SetPlanComplete(false); 176 return true; 177 } 178 } 179 180 // Next see if there is a specific step through plan at our current pc (these 181 // might chain, for instance stepping through a dylib trampoline to the objc 182 // dispatch function...) 183 LookForPlanToStepThroughFromCurrentPC(); 184 if (m_sub_plan_sp) { 185 PushPlan(m_sub_plan_sp); 186 return false; 187 } else { 188 SetPlanComplete(); 189 return true; 190 } 191 } 192 193 bool ThreadPlanStepThrough::StopOthers() { return m_stop_others; } 194 195 StateType ThreadPlanStepThrough::GetPlanRunState() { return eStateRunning; } 196 197 bool ThreadPlanStepThrough::DoWillResume(StateType resume_state, 198 bool current_plan) { 199 return true; 200 } 201 202 bool ThreadPlanStepThrough::WillStop() { return true; } 203 204 void ThreadPlanStepThrough::ClearBackstopBreakpoint() { 205 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) { 206 m_thread.GetProcess()->GetTarget().RemoveBreakpointByID(m_backstop_bkpt_id); 207 m_backstop_bkpt_id = LLDB_INVALID_BREAK_ID; 208 } 209 } 210 211 bool ThreadPlanStepThrough::MischiefManaged() { 212 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 213 214 if (!IsPlanComplete()) { 215 return false; 216 } else { 217 if (log) 218 log->Printf("Completed step through step plan."); 219 220 ClearBackstopBreakpoint(); 221 ThreadPlan::MischiefManaged(); 222 return true; 223 } 224 } 225 226 bool ThreadPlanStepThrough::HitOurBackstopBreakpoint() { 227 StopInfoSP stop_info_sp(m_thread.GetStopInfo()); 228 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint) { 229 break_id_t stop_value = (break_id_t)stop_info_sp->GetValue(); 230 BreakpointSiteSP cur_site_sp = 231 m_thread.GetProcess()->GetBreakpointSiteList().FindByID(stop_value); 232 if (cur_site_sp && 233 cur_site_sp->IsBreakpointAtThisSite(m_backstop_bkpt_id)) { 234 StackID cur_frame_zero_id = 235 m_thread.GetStackFrameAtIndex(0)->GetStackID(); 236 237 if (cur_frame_zero_id == m_return_stack_id) { 238 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 239 if (log) 240 log->PutCString("ThreadPlanStepThrough hit backstop breakpoint."); 241 return true; 242 } 243 } 244 } 245 return false; 246 } 247