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