1 //===-- ThreadPlanStepRange.cpp ---------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Target/ThreadPlanStepRange.h" 10 #include "lldb/Breakpoint/BreakpointLocation.h" 11 #include "lldb/Breakpoint/BreakpointSite.h" 12 #include "lldb/Core/Disassembler.h" 13 #include "lldb/Symbol/Function.h" 14 #include "lldb/Symbol/Symbol.h" 15 #include "lldb/Target/ExecutionContext.h" 16 #include "lldb/Target/Process.h" 17 #include "lldb/Target/RegisterContext.h" 18 #include "lldb/Target/StopInfo.h" 19 #include "lldb/Target/Target.h" 20 #include "lldb/Target/Thread.h" 21 #include "lldb/Target/ThreadPlanRunToAddress.h" 22 #include "lldb/Utility/Log.h" 23 #include "lldb/Utility/Stream.h" 24 25 using namespace lldb; 26 using namespace lldb_private; 27 28 //---------------------------------------------------------------------- 29 // ThreadPlanStepRange: Step through a stack range, either stepping over or 30 // into based on the value of \a type. 31 //---------------------------------------------------------------------- 32 33 ThreadPlanStepRange::ThreadPlanStepRange(ThreadPlanKind kind, const char *name, 34 Thread &thread, 35 const AddressRange &range, 36 const SymbolContext &addr_context, 37 lldb::RunMode stop_others, 38 bool given_ranges_only) 39 : ThreadPlan(kind, name, thread, eVoteNoOpinion, eVoteNoOpinion), 40 m_addr_context(addr_context), m_address_ranges(), 41 m_stop_others(stop_others), m_stack_id(), m_parent_stack_id(), 42 m_no_more_plans(false), m_first_run_event(true), m_use_fast_step(false), 43 m_given_ranges_only(given_ranges_only) { 44 m_use_fast_step = GetTarget().GetUseFastStepping(); 45 AddRange(range); 46 m_stack_id = m_thread.GetStackFrameAtIndex(0)->GetStackID(); 47 StackFrameSP parent_stack = m_thread.GetStackFrameAtIndex(1); 48 if (parent_stack) 49 m_parent_stack_id = parent_stack->GetStackID(); 50 } 51 52 ThreadPlanStepRange::~ThreadPlanStepRange() { ClearNextBranchBreakpoint(); } 53 54 void ThreadPlanStepRange::DidPush() { 55 // See if we can find a "next range" breakpoint: 56 SetNextBranchBreakpoint(); 57 } 58 59 bool ThreadPlanStepRange::ValidatePlan(Stream *error) { 60 if (m_could_not_resolve_hw_bp) { 61 if (error) 62 error->PutCString( 63 "Could not create hardware breakpoint for thread plan."); 64 return false; 65 } 66 return true; 67 } 68 69 Vote ThreadPlanStepRange::ShouldReportStop(Event *event_ptr) { 70 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 71 72 const Vote vote = IsPlanComplete() ? eVoteYes : eVoteNo; 73 if (log) 74 log->Printf("ThreadPlanStepRange::ShouldReportStop() returning vote %i\n", 75 vote); 76 return vote; 77 } 78 79 void ThreadPlanStepRange::AddRange(const AddressRange &new_range) { 80 // For now I'm just adding the ranges. At some point we may want to condense 81 // the ranges if they overlap, though I don't think it is likely to be very 82 // important. 83 m_address_ranges.push_back(new_range); 84 85 // Fill the slot for this address range with an empty DisassemblerSP in the 86 // instruction ranges. I want the indices to match, but I don't want to do 87 // the work to disassemble this range if I don't step into it. 88 m_instruction_ranges.push_back(DisassemblerSP()); 89 } 90 91 void ThreadPlanStepRange::DumpRanges(Stream *s) { 92 size_t num_ranges = m_address_ranges.size(); 93 if (num_ranges == 1) { 94 m_address_ranges[0].Dump(s, m_thread.CalculateTarget().get(), 95 Address::DumpStyleLoadAddress); 96 } else { 97 for (size_t i = 0; i < num_ranges; i++) { 98 s->Printf(" %" PRIu64 ": ", uint64_t(i)); 99 m_address_ranges[i].Dump(s, m_thread.CalculateTarget().get(), 100 Address::DumpStyleLoadAddress); 101 } 102 } 103 } 104 105 bool ThreadPlanStepRange::InRange() { 106 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 107 bool ret_value = false; 108 109 lldb::addr_t pc_load_addr = m_thread.GetRegisterContext()->GetPC(); 110 111 size_t num_ranges = m_address_ranges.size(); 112 for (size_t i = 0; i < num_ranges; i++) { 113 ret_value = m_address_ranges[i].ContainsLoadAddress( 114 pc_load_addr, m_thread.CalculateTarget().get()); 115 if (ret_value) 116 break; 117 } 118 119 if (!ret_value && !m_given_ranges_only) { 120 // See if we've just stepped to another part of the same line number... 121 StackFrame *frame = m_thread.GetStackFrameAtIndex(0).get(); 122 123 SymbolContext new_context( 124 frame->GetSymbolContext(eSymbolContextEverything)); 125 if (m_addr_context.line_entry.IsValid() && 126 new_context.line_entry.IsValid()) { 127 if (m_addr_context.line_entry.original_file == 128 new_context.line_entry.original_file) { 129 if (m_addr_context.line_entry.line == new_context.line_entry.line) { 130 m_addr_context = new_context; 131 AddRange( 132 m_addr_context.line_entry.GetSameLineContiguousAddressRange()); 133 ret_value = true; 134 if (log) { 135 StreamString s; 136 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(), 137 true, Address::DumpStyleLoadAddress, 138 Address::DumpStyleLoadAddress, true); 139 140 log->Printf( 141 "Step range plan stepped to another range of same line: %s", 142 s.GetData()); 143 } 144 } else if (new_context.line_entry.line == 0) { 145 new_context.line_entry.line = m_addr_context.line_entry.line; 146 m_addr_context = new_context; 147 AddRange( 148 m_addr_context.line_entry.GetSameLineContiguousAddressRange()); 149 ret_value = true; 150 if (log) { 151 StreamString s; 152 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(), 153 true, Address::DumpStyleLoadAddress, 154 Address::DumpStyleLoadAddress, true); 155 156 log->Printf("Step range plan stepped to a range at linenumber 0 " 157 "stepping through that range: %s", 158 s.GetData()); 159 } 160 } else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress( 161 m_thread.CalculateTarget().get()) != pc_load_addr) { 162 // Another thing that sometimes happens here is that we step out of 163 // one line into the MIDDLE of another line. So far I mostly see 164 // this due to bugs in the debug information. But we probably don't 165 // want to be in the middle of a line range, so in that case reset 166 // the stepping range to the line we've stepped into the middle of 167 // and continue. 168 m_addr_context = new_context; 169 m_address_ranges.clear(); 170 AddRange(m_addr_context.line_entry.range); 171 ret_value = true; 172 if (log) { 173 StreamString s; 174 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(), 175 true, Address::DumpStyleLoadAddress, 176 Address::DumpStyleLoadAddress, true); 177 178 log->Printf("Step range plan stepped to the middle of new " 179 "line(%d): %s, continuing to clear this line.", 180 new_context.line_entry.line, s.GetData()); 181 } 182 } 183 } 184 } 185 } 186 187 if (!ret_value && log) 188 log->Printf("Step range plan out of range to 0x%" PRIx64, pc_load_addr); 189 190 return ret_value; 191 } 192 193 bool ThreadPlanStepRange::InSymbol() { 194 lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC(); 195 if (m_addr_context.function != nullptr) { 196 return m_addr_context.function->GetAddressRange().ContainsLoadAddress( 197 cur_pc, m_thread.CalculateTarget().get()); 198 } else if (m_addr_context.symbol && m_addr_context.symbol->ValueIsAddress()) { 199 AddressRange range(m_addr_context.symbol->GetAddressRef(), 200 m_addr_context.symbol->GetByteSize()); 201 return range.ContainsLoadAddress(cur_pc, m_thread.CalculateTarget().get()); 202 } 203 return false; 204 } 205 206 // FIXME: This should also handle inlining if we aren't going to do inlining in 207 // the 208 // main stack. 209 // 210 // Ideally we should remember the whole stack frame list, and then compare that 211 // to the current list. 212 213 lldb::FrameComparison ThreadPlanStepRange::CompareCurrentFrameToStartFrame() { 214 FrameComparison frame_order; 215 216 StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID(); 217 218 if (cur_frame_id == m_stack_id) { 219 frame_order = eFrameCompareEqual; 220 } else if (cur_frame_id < m_stack_id) { 221 frame_order = eFrameCompareYounger; 222 } else { 223 StackFrameSP cur_parent_frame = m_thread.GetStackFrameAtIndex(1); 224 StackID cur_parent_id; 225 if (cur_parent_frame) 226 cur_parent_id = cur_parent_frame->GetStackID(); 227 if (m_parent_stack_id.IsValid() && cur_parent_id.IsValid() && 228 m_parent_stack_id == cur_parent_id) 229 frame_order = eFrameCompareSameParent; 230 else 231 frame_order = eFrameCompareOlder; 232 } 233 return frame_order; 234 } 235 236 bool ThreadPlanStepRange::StopOthers() { 237 return (m_stop_others == lldb::eOnlyThisThread || 238 m_stop_others == lldb::eOnlyDuringStepping); 239 } 240 241 InstructionList *ThreadPlanStepRange::GetInstructionsForAddress( 242 lldb::addr_t addr, size_t &range_index, size_t &insn_offset) { 243 size_t num_ranges = m_address_ranges.size(); 244 for (size_t i = 0; i < num_ranges; i++) { 245 if (m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget())) { 246 // Some joker added a zero size range to the stepping range... 247 if (m_address_ranges[i].GetByteSize() == 0) 248 return nullptr; 249 250 if (!m_instruction_ranges[i]) { 251 // Disassemble the address range given: 252 ExecutionContext exe_ctx(m_thread.GetProcess()); 253 const char *plugin_name = nullptr; 254 const char *flavor = nullptr; 255 const bool prefer_file_cache = true; 256 m_instruction_ranges[i] = Disassembler::DisassembleRange( 257 GetTarget().GetArchitecture(), plugin_name, flavor, exe_ctx, 258 m_address_ranges[i], prefer_file_cache); 259 } 260 if (!m_instruction_ranges[i]) 261 return nullptr; 262 else { 263 // Find where we are in the instruction list as well. If we aren't at 264 // an instruction, return nullptr. In this case, we're probably lost, 265 // and shouldn't try to do anything fancy. 266 267 insn_offset = 268 m_instruction_ranges[i] 269 ->GetInstructionList() 270 .GetIndexOfInstructionAtLoadAddress(addr, GetTarget()); 271 if (insn_offset == UINT32_MAX) 272 return nullptr; 273 else { 274 range_index = i; 275 return &m_instruction_ranges[i]->GetInstructionList(); 276 } 277 } 278 } 279 } 280 return nullptr; 281 } 282 283 void ThreadPlanStepRange::ClearNextBranchBreakpoint() { 284 if (m_next_branch_bp_sp) { 285 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 286 if (log) 287 log->Printf("Removing next branch breakpoint: %d.", 288 m_next_branch_bp_sp->GetID()); 289 GetTarget().RemoveBreakpointByID(m_next_branch_bp_sp->GetID()); 290 m_next_branch_bp_sp.reset(); 291 m_could_not_resolve_hw_bp = false; 292 } 293 } 294 295 bool ThreadPlanStepRange::SetNextBranchBreakpoint() { 296 if (m_next_branch_bp_sp) 297 return true; 298 299 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 300 // Stepping through ranges using breakpoints doesn't work yet, but with this 301 // off we fall back to instruction single stepping. 302 if (!m_use_fast_step) 303 return false; 304 305 lldb::addr_t cur_addr = GetThread().GetRegisterContext()->GetPC(); 306 // Find the current address in our address ranges, and fetch the disassembly 307 // if we haven't already: 308 size_t pc_index; 309 size_t range_index; 310 InstructionList *instructions = 311 GetInstructionsForAddress(cur_addr, range_index, pc_index); 312 if (instructions == nullptr) 313 return false; 314 else { 315 Target &target = GetThread().GetProcess()->GetTarget(); 316 uint32_t branch_index; 317 branch_index = 318 instructions->GetIndexOfNextBranchInstruction(pc_index, target); 319 320 Address run_to_address; 321 322 // If we didn't find a branch, run to the end of the range. 323 if (branch_index == UINT32_MAX) { 324 uint32_t last_index = instructions->GetSize() - 1; 325 if (last_index - pc_index > 1) { 326 InstructionSP last_inst = 327 instructions->GetInstructionAtIndex(last_index); 328 size_t last_inst_size = last_inst->GetOpcode().GetByteSize(); 329 run_to_address = last_inst->GetAddress(); 330 run_to_address.Slide(last_inst_size); 331 } 332 } else if (branch_index - pc_index > 1) { 333 run_to_address = 334 instructions->GetInstructionAtIndex(branch_index)->GetAddress(); 335 } 336 337 if (run_to_address.IsValid()) { 338 const bool is_internal = true; 339 m_next_branch_bp_sp = 340 GetTarget().CreateBreakpoint(run_to_address, is_internal, false); 341 if (m_next_branch_bp_sp) { 342 343 if (m_next_branch_bp_sp->IsHardware() && 344 !m_next_branch_bp_sp->HasResolvedLocations()) 345 m_could_not_resolve_hw_bp = true; 346 347 if (log) { 348 lldb::break_id_t bp_site_id = LLDB_INVALID_BREAK_ID; 349 BreakpointLocationSP bp_loc = 350 m_next_branch_bp_sp->GetLocationAtIndex(0); 351 if (bp_loc) { 352 BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite(); 353 if (bp_site) { 354 bp_site_id = bp_site->GetID(); 355 } 356 } 357 log->Printf("ThreadPlanStepRange::SetNextBranchBreakpoint - Setting " 358 "breakpoint %d (site %d) to run to address 0x%" PRIx64, 359 m_next_branch_bp_sp->GetID(), bp_site_id, 360 run_to_address.GetLoadAddress( 361 &m_thread.GetProcess()->GetTarget())); 362 } 363 364 m_next_branch_bp_sp->SetThreadID(m_thread.GetID()); 365 m_next_branch_bp_sp->SetBreakpointKind("next-branch-location"); 366 367 return true; 368 } else 369 return false; 370 } 371 } 372 return false; 373 } 374 375 bool ThreadPlanStepRange::NextRangeBreakpointExplainsStop( 376 lldb::StopInfoSP stop_info_sp) { 377 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 378 if (!m_next_branch_bp_sp) 379 return false; 380 381 break_id_t bp_site_id = stop_info_sp->GetValue(); 382 BreakpointSiteSP bp_site_sp = 383 m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id); 384 if (!bp_site_sp) 385 return false; 386 else if (!bp_site_sp->IsBreakpointAtThisSite(m_next_branch_bp_sp->GetID())) 387 return false; 388 else { 389 // If we've hit the next branch breakpoint, then clear it. 390 size_t num_owners = bp_site_sp->GetNumberOfOwners(); 391 bool explains_stop = true; 392 // If all the owners are internal, then we are probably just stepping over 393 // this range from multiple threads, or multiple frames, so we want to 394 // continue. If one is not internal, then we should not explain the stop, 395 // and let the user breakpoint handle the stop. 396 for (size_t i = 0; i < num_owners; i++) { 397 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal()) { 398 explains_stop = false; 399 break; 400 } 401 } 402 if (log) 403 log->Printf("ThreadPlanStepRange::NextRangeBreakpointExplainsStop - Hit " 404 "next range breakpoint which has %" PRIu64 405 " owners - explains stop: %u.", 406 (uint64_t)num_owners, explains_stop); 407 ClearNextBranchBreakpoint(); 408 return explains_stop; 409 } 410 } 411 412 bool ThreadPlanStepRange::WillStop() { return true; } 413 414 StateType ThreadPlanStepRange::GetPlanRunState() { 415 if (m_next_branch_bp_sp) 416 return eStateRunning; 417 else 418 return eStateStepping; 419 } 420 421 bool ThreadPlanStepRange::MischiefManaged() { 422 // If we have pushed some plans between ShouldStop & MischiefManaged, then 423 // we're not done... 424 // I do this check first because we might have stepped somewhere that will 425 // fool InRange into 426 // thinking it needs to step past the end of that line. This happens, for 427 // instance, when stepping over inlined code that is in the middle of the 428 // current line. 429 430 if (!m_no_more_plans) 431 return false; 432 433 bool done = true; 434 if (!IsPlanComplete()) { 435 if (InRange()) { 436 done = false; 437 } else { 438 FrameComparison frame_order = CompareCurrentFrameToStartFrame(); 439 done = (frame_order != eFrameCompareOlder) ? m_no_more_plans : true; 440 } 441 } 442 443 if (done) { 444 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 445 if (log) 446 log->Printf("Completed step through range plan."); 447 ClearNextBranchBreakpoint(); 448 ThreadPlan::MischiefManaged(); 449 return true; 450 } else { 451 return false; 452 } 453 } 454 455 bool ThreadPlanStepRange::IsPlanStale() { 456 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP)); 457 FrameComparison frame_order = CompareCurrentFrameToStartFrame(); 458 459 if (frame_order == eFrameCompareOlder) { 460 if (log) { 461 log->Printf("ThreadPlanStepRange::IsPlanStale returning true, we've " 462 "stepped out."); 463 } 464 return true; 465 } else if (frame_order == eFrameCompareEqual && InSymbol()) { 466 // If we are not in a place we should step through, we've gotten stale. One 467 // tricky bit here is that some stubs don't push a frame, so we should. 468 // check that we are in the same symbol. 469 if (!InRange()) { 470 // Set plan Complete when we reach next instruction just after the range 471 lldb::addr_t addr = m_thread.GetRegisterContext()->GetPC() - 1; 472 size_t num_ranges = m_address_ranges.size(); 473 for (size_t i = 0; i < num_ranges; i++) { 474 bool in_range = m_address_ranges[i].ContainsLoadAddress( 475 addr, m_thread.CalculateTarget().get()); 476 if (in_range) { 477 SetPlanComplete(); 478 } 479 } 480 return true; 481 } 482 } 483 return false; 484 } 485