1 //===-- ThreadPlanStepInRange.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/ThreadPlanStepInRange.h" 11 12 // C Includes 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 17 #include "lldb/lldb-private-log.h" 18 #include "lldb/Core/Log.h" 19 #include "lldb/Core/Stream.h" 20 #include "lldb/Symbol/Symbol.h" 21 #include "lldb/Symbol/Function.h" 22 #include "lldb/Target/Process.h" 23 #include "lldb/Target/RegisterContext.h" 24 #include "lldb/Target/Target.h" 25 #include "lldb/Target/Thread.h" 26 #include "lldb/Target/ThreadPlanStepOut.h" 27 #include "lldb/Target/ThreadPlanStepThrough.h" 28 #include "lldb/Core/RegularExpression.h" 29 30 using namespace lldb; 31 using namespace lldb_private; 32 33 uint32_t ThreadPlanStepInRange::s_default_flag_values = ThreadPlanShouldStopHere::eAvoidNoDebug; 34 35 //---------------------------------------------------------------------- 36 // ThreadPlanStepInRange: Step through a stack range, either stepping over or into 37 // based on the value of \a type. 38 //---------------------------------------------------------------------- 39 40 ThreadPlanStepInRange::ThreadPlanStepInRange 41 ( 42 Thread &thread, 43 const AddressRange &range, 44 const SymbolContext &addr_context, 45 lldb::RunMode stop_others 46 ) : 47 ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others), 48 ThreadPlanShouldStopHere (this, ThreadPlanStepInRange::DefaultShouldStopHereCallback, NULL), 49 m_step_past_prologue (true), 50 m_virtual_step (false) 51 { 52 SetFlagsToDefault (); 53 } 54 55 ThreadPlanStepInRange::ThreadPlanStepInRange 56 ( 57 Thread &thread, 58 const AddressRange &range, 59 const SymbolContext &addr_context, 60 const char *step_into_target, 61 lldb::RunMode stop_others 62 ) : 63 ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others), 64 ThreadPlanShouldStopHere (this, ThreadPlanStepInRange::DefaultShouldStopHereCallback, NULL), 65 m_step_past_prologue (true), 66 m_virtual_step (false), 67 m_step_into_target (step_into_target) 68 { 69 SetFlagsToDefault (); 70 } 71 72 ThreadPlanStepInRange::~ThreadPlanStepInRange () 73 { 74 } 75 76 void 77 ThreadPlanStepInRange::GetDescription (Stream *s, lldb::DescriptionLevel level) 78 { 79 if (level == lldb::eDescriptionLevelBrief) 80 s->Printf("step in"); 81 else 82 { 83 s->Printf ("Stepping through range (stepping into functions): "); 84 DumpRanges(s); 85 s->Printf ("targeting %s.", m_step_into_target.AsCString()); 86 } 87 } 88 89 bool 90 ThreadPlanStepInRange::ShouldStop (Event *event_ptr) 91 { 92 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 93 m_no_more_plans = false; 94 95 if (log) 96 { 97 StreamString s; 98 s.Address (m_thread.GetRegisterContext()->GetPC(), 99 m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize()); 100 log->Printf("ThreadPlanStepInRange reached %s.", s.GetData()); 101 } 102 103 if (IsPlanComplete()) 104 return true; 105 106 ThreadPlan* new_plan = NULL; 107 108 if (m_virtual_step) 109 { 110 // If we've just completed a virtual step, all we need to do is check for a ShouldStopHere plan, and otherwise 111 // we're done. 112 new_plan = InvokeShouldStopHereCallback(); 113 } 114 else 115 { 116 // Stepping through should be done running other threads in general, since we're setting a breakpoint and 117 // continuing. So only stop others if we are explicitly told to do so. 118 119 bool stop_others; 120 if (m_stop_others == lldb::eOnlyThisThread) 121 stop_others = false; 122 else 123 stop_others = true; 124 125 FrameComparison frame_order = CompareCurrentFrameToStartFrame(); 126 127 if (frame_order == eFrameCompareOlder) 128 { 129 // If we're in an older frame then we should stop. 130 // 131 // A caveat to this is if we think the frame is older but we're actually in a trampoline. 132 // I'm going to make the assumption that you wouldn't RETURN to a trampoline. So if we are 133 // in a trampoline we think the frame is older because the trampoline confused the backtracer. 134 new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others); 135 if (new_plan == NULL) 136 return true; 137 else if (log) 138 { 139 log->Printf("Thought I stepped out, but in fact arrived at a trampoline."); 140 } 141 142 } 143 else if (frame_order == eFrameCompareEqual && InSymbol()) 144 { 145 // If we are not in a place we should step through, we're done. 146 // One tricky bit here is that some stubs don't push a frame, so we have to check 147 // both the case of a frame that is younger, or the same as this frame. 148 // However, if the frame is the same, and we are still in the symbol we started 149 // in, the we don't need to do this. This first check isn't strictly necessary, 150 // but it is more efficient. 151 152 // If we're still in the range, keep going, either by running to the next branch breakpoint, or by 153 // stepping. 154 if (InRange()) 155 { 156 SetNextBranchBreakpoint(); 157 return false; 158 } 159 160 SetPlanComplete(); 161 m_no_more_plans = true; 162 return true; 163 } 164 165 // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it: 166 ClearNextBranchBreakpoint(); 167 168 // We may have set the plan up above in the FrameIsOlder section: 169 170 if (new_plan == NULL) 171 new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others); 172 173 if (log) 174 { 175 if (new_plan != NULL) 176 log->Printf ("Found a step through plan: %s", new_plan->GetName()); 177 else 178 log->Printf ("No step through plan found."); 179 } 180 181 // If not, give the "should_stop" callback a chance to push a plan to get us out of here. 182 // But only do that if we actually have stepped in. 183 if (!new_plan && frame_order == eFrameCompareYounger) 184 new_plan = InvokeShouldStopHereCallback(); 185 186 // If we've stepped in and we are going to stop here, check to see if we were asked to 187 // run past the prologue, and if so do that. 188 189 if (new_plan == NULL && frame_order == eFrameCompareYounger && m_step_past_prologue) 190 { 191 lldb::StackFrameSP curr_frame = m_thread.GetStackFrameAtIndex(0); 192 if (curr_frame) 193 { 194 size_t bytes_to_skip = 0; 195 lldb::addr_t curr_addr = m_thread.GetRegisterContext()->GetPC(); 196 Address func_start_address; 197 198 SymbolContext sc = curr_frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextSymbol); 199 200 if (sc.function) 201 { 202 func_start_address = sc.function->GetAddressRange().GetBaseAddress(); 203 if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get())) 204 bytes_to_skip = sc.function->GetPrologueByteSize(); 205 } 206 else if (sc.symbol) 207 { 208 func_start_address = sc.symbol->GetAddress(); 209 if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get())) 210 bytes_to_skip = sc.symbol->GetPrologueByteSize(); 211 } 212 213 if (bytes_to_skip != 0) 214 { 215 func_start_address.Slide (bytes_to_skip); 216 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP); 217 if (log) 218 log->Printf ("Pushing past prologue "); 219 220 new_plan = m_thread.QueueThreadPlanForRunToAddress(false, func_start_address,true); 221 } 222 } 223 } 224 } 225 226 if (new_plan == NULL) 227 { 228 m_no_more_plans = true; 229 SetPlanComplete(); 230 return true; 231 } 232 else 233 { 234 m_no_more_plans = false; 235 return false; 236 } 237 } 238 239 void 240 ThreadPlanStepInRange::SetFlagsToDefault () 241 { 242 GetFlags().Set(ThreadPlanStepInRange::s_default_flag_values); 243 } 244 245 void 246 ThreadPlanStepInRange::SetAvoidRegexp(const char *name) 247 { 248 if (m_avoid_regexp_ap.get() == NULL) 249 m_avoid_regexp_ap.reset (new RegularExpression(name)); 250 251 m_avoid_regexp_ap->Compile (name); 252 } 253 254 void 255 ThreadPlanStepInRange::SetDefaultFlagValue (uint32_t new_value) 256 { 257 // TODO: Should we test this for sanity? 258 ThreadPlanStepInRange::s_default_flag_values = new_value; 259 } 260 261 bool 262 ThreadPlanStepInRange::FrameMatchesAvoidRegexp () 263 { 264 StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get(); 265 266 const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_ap.get(); 267 if (avoid_regexp_to_use == NULL) 268 avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp(); 269 270 if (avoid_regexp_to_use != NULL) 271 { 272 SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock|eSymbolContextSymbol); 273 if (sc.symbol != NULL) 274 { 275 const char *frame_function_name = sc.GetFunctionName().GetCString(); 276 if (frame_function_name) 277 { 278 size_t num_matches = 0; 279 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 280 if (log) 281 num_matches = 1; 282 bool return_value = avoid_regexp_to_use->Execute(frame_function_name, num_matches); 283 if (return_value) 284 { 285 if (log) 286 { 287 std::string match; 288 avoid_regexp_to_use->GetMatchAtIndex(frame_function_name,0, match); 289 log->Printf ("Stepping out of function \"%s\" because it matches the avoid regexp \"%s\" - match substring: \"%s\".", 290 frame_function_name, 291 avoid_regexp_to_use->GetText(), 292 match.c_str()); 293 } 294 295 } 296 return return_value; 297 } 298 } 299 } 300 return false; 301 } 302 303 ThreadPlan * 304 ThreadPlanStepInRange::DefaultShouldStopHereCallback (ThreadPlan *current_plan, Flags &flags, void *baton) 305 { 306 bool should_step_out = false; 307 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get(); 308 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 309 310 if (flags.Test(eAvoidNoDebug)) 311 { 312 if (!frame->HasDebugInformation()) 313 { 314 if (log) 315 log->Printf ("Stepping out of frame with no debug info"); 316 317 should_step_out = true; 318 } 319 } 320 321 if (current_plan->GetKind() == eKindStepInRange) 322 { 323 ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan); 324 if (step_in_range_plan->m_step_into_target) 325 { 326 SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock|eSymbolContextSymbol); 327 if (sc.symbol != NULL) 328 { 329 // First try an exact match, since that's cheap with ConstStrings. Then do a strstr compare. 330 if (step_in_range_plan->m_step_into_target == sc.GetFunctionName()) 331 { 332 should_step_out = false; 333 } 334 else 335 { 336 const char *target_name = step_in_range_plan->m_step_into_target.AsCString(); 337 const char *function_name = sc.GetFunctionName().AsCString(); 338 339 if (function_name == NULL) 340 should_step_out = true; 341 else if (strstr (function_name, target_name) == NULL) 342 should_step_out = true; 343 } 344 if (log && should_step_out) 345 log->Printf("Stepping out of frame %s which did not match step into target %s.", 346 sc.GetFunctionName().AsCString(), 347 step_in_range_plan->m_step_into_target.AsCString()); 348 } 349 } 350 351 if (!should_step_out) 352 { 353 ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan); 354 // Don't log the should_step_out here, it's easier to do it in FrameMatchesAvoidRegexp. 355 should_step_out = step_in_range_plan->FrameMatchesAvoidRegexp (); 356 } 357 } 358 359 360 if (should_step_out) 361 { 362 // FIXME: Make sure the ThreadPlanForStepOut does the right thing with inlined functions. 363 // We really should have all plans take the tri-state for "stop others" so we can do the right 364 // thing. For now let's be safe and always run others when we are likely to run arbitrary code. 365 const bool stop_others = false; 366 return current_plan->GetThread().QueueThreadPlanForStepOut (false, 367 NULL, 368 true, 369 stop_others, 370 eVoteNo, 371 eVoteNoOpinion, 372 0); // Frame index 373 } 374 375 return NULL; 376 } 377 378 bool 379 ThreadPlanStepInRange::PlanExplainsStop (Event *event_ptr) 380 { 381 // We always explain a stop. Either we've just done a single step, in which 382 // case we'll do our ordinary processing, or we stopped for some 383 // reason that isn't handled by our sub-plans, in which case we want to just stop right 384 // away. 385 // In general, we don't want to mark the plan as complete for unexplained stops. 386 // For instance, if you step in to some code with no debug info, so you step out 387 // and in the course of that hit a breakpoint, then you want to stop & show the user 388 // the breakpoint, but not unship the step in plan, since you still may want to complete that 389 // plan when you continue. This is particularly true when doing "step in to target function." 390 // stepping. 391 // 392 // The only variation is that if we are doing "step by running to next branch" in which case 393 // if we hit our branch breakpoint we don't set the plan to complete. 394 395 if (m_virtual_step) 396 return true; 397 398 StopInfoSP stop_info_sp = GetPrivateStopReason(); 399 if (stop_info_sp) 400 { 401 StopReason reason = stop_info_sp->GetStopReason(); 402 403 switch (reason) 404 { 405 case eStopReasonBreakpoint: 406 if (NextRangeBreakpointExplainsStop(stop_info_sp)) 407 return true; 408 case eStopReasonWatchpoint: 409 case eStopReasonSignal: 410 case eStopReasonException: 411 case eStopReasonExec: 412 case eStopReasonThreadExiting: 413 { 414 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 415 if (log) 416 log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step."); 417 } 418 return false; 419 break; 420 default: 421 break; 422 } 423 } 424 return true; 425 } 426 427 bool 428 ThreadPlanStepInRange::WillResume (lldb::StateType resume_state, bool current_plan) 429 { 430 if (resume_state == eStateStepping && current_plan) 431 { 432 // See if we are about to step over a virtual inlined call. 433 bool step_without_resume = m_thread.DecrementCurrentInlinedDepth(); 434 if (step_without_resume) 435 { 436 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 437 if (log) 438 log->Printf ("ThreadPlanStepInRange::WillResume: returning false, inline_depth: %d", 439 m_thread.GetCurrentInlinedDepth()); 440 SetStopInfo(StopInfo::CreateStopReasonToTrace(m_thread)); 441 442 // FIXME: Maybe it would be better to create a InlineStep stop reason, but then 443 // the whole rest of the world would have to handle that stop reason. 444 m_virtual_step = true; 445 } 446 return !step_without_resume; 447 } 448 else 449 return ThreadPlan::WillResume(resume_state, current_plan); 450 451 } 452