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