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