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