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