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