1 //===-- ThreadPlanStepRange.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Target/ThreadPlanStepRange.h"
15 #include "lldb/Breakpoint/BreakpointLocation.h"
16 #include "lldb/Breakpoint/BreakpointSite.h"
17 #include "lldb/Core/Disassembler.h"
18 #include "lldb/Symbol/Function.h"
19 #include "lldb/Symbol/Symbol.h"
20 #include "lldb/Target/ExecutionContext.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Target/RegisterContext.h"
23 #include "lldb/Target/StopInfo.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Target/Thread.h"
26 #include "lldb/Target/ThreadPlanRunToAddress.h"
27 #include "lldb/Utility/Log.h"
28 #include "lldb/Utility/Stream.h"
31 using namespace lldb_private;
33 //----------------------------------------------------------------------
34 // ThreadPlanStepRange: Step through a stack range, either stepping over or
35 // into based on the value of \a type.
36 //----------------------------------------------------------------------
38 ThreadPlanStepRange::ThreadPlanStepRange(ThreadPlanKind kind, const char *name,
40 const AddressRange &range,
41 const SymbolContext &addr_context,
42 lldb::RunMode stop_others,
43 bool given_ranges_only)
44 : ThreadPlan(kind, name, thread, eVoteNoOpinion, eVoteNoOpinion),
45 m_addr_context(addr_context), m_address_ranges(),
46 m_stop_others(stop_others), m_stack_id(), m_parent_stack_id(),
47 m_no_more_plans(false), m_first_run_event(true), m_use_fast_step(false),
48 m_given_ranges_only(given_ranges_only) {
49 m_use_fast_step = GetTarget().GetUseFastStepping();
51 m_stack_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
52 StackFrameSP parent_stack = m_thread.GetStackFrameAtIndex(1);
54 m_parent_stack_id = parent_stack->GetStackID();
57 ThreadPlanStepRange::~ThreadPlanStepRange() { ClearNextBranchBreakpoint(); }
59 void ThreadPlanStepRange::DidPush() {
60 // See if we can find a "next range" breakpoint:
61 SetNextBranchBreakpoint();
64 bool ThreadPlanStepRange::ValidatePlan(Stream *error) { return true; }
66 Vote ThreadPlanStepRange::ShouldReportStop(Event *event_ptr) {
67 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
69 const Vote vote = IsPlanComplete() ? eVoteYes : eVoteNo;
71 log->Printf("ThreadPlanStepRange::ShouldReportStop() returning vote %i\n",
76 void ThreadPlanStepRange::AddRange(const AddressRange &new_range) {
77 // For now I'm just adding the ranges. At some point we may want to condense
78 // the ranges if they overlap, though I don't think it is likely to be very
80 m_address_ranges.push_back(new_range);
82 // Fill the slot for this address range with an empty DisassemblerSP in the
83 // instruction ranges. I want the indices to match, but I don't want to do
84 // the work to disassemble this range if I don't step into it.
85 m_instruction_ranges.push_back(DisassemblerSP());
88 void ThreadPlanStepRange::DumpRanges(Stream *s) {
89 size_t num_ranges = m_address_ranges.size();
90 if (num_ranges == 1) {
91 m_address_ranges[0].Dump(s, m_thread.CalculateTarget().get(),
92 Address::DumpStyleLoadAddress);
94 for (size_t i = 0; i < num_ranges; i++) {
95 s->Printf(" %" PRIu64 ": ", uint64_t(i));
96 m_address_ranges[i].Dump(s, m_thread.CalculateTarget().get(),
97 Address::DumpStyleLoadAddress);
102 bool ThreadPlanStepRange::InRange() {
103 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
104 bool ret_value = false;
106 lldb::addr_t pc_load_addr = m_thread.GetRegisterContext()->GetPC();
108 size_t num_ranges = m_address_ranges.size();
109 for (size_t i = 0; i < num_ranges; i++) {
110 ret_value = m_address_ranges[i].ContainsLoadAddress(
111 pc_load_addr, m_thread.CalculateTarget().get());
116 if (!ret_value && !m_given_ranges_only) {
117 // See if we've just stepped to another part of the same line number...
118 StackFrame *frame = m_thread.GetStackFrameAtIndex(0).get();
120 SymbolContext new_context(
121 frame->GetSymbolContext(eSymbolContextEverything));
122 if (m_addr_context.line_entry.IsValid() &&
123 new_context.line_entry.IsValid()) {
124 if (m_addr_context.line_entry.original_file ==
125 new_context.line_entry.original_file) {
126 if (m_addr_context.line_entry.line == new_context.line_entry.line) {
127 m_addr_context = new_context;
129 m_addr_context.line_entry.GetSameLineContiguousAddressRange());
133 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
134 true, Address::DumpStyleLoadAddress,
135 Address::DumpStyleLoadAddress, true);
138 "Step range plan stepped to another range of same line: %s",
141 } else if (new_context.line_entry.line == 0) {
142 new_context.line_entry.line = m_addr_context.line_entry.line;
143 m_addr_context = new_context;
145 m_addr_context.line_entry.GetSameLineContiguousAddressRange());
149 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
150 true, Address::DumpStyleLoadAddress,
151 Address::DumpStyleLoadAddress, true);
153 log->Printf("Step range plan stepped to a range at linenumber 0 "
154 "stepping through that range: %s",
157 } else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress(
158 m_thread.CalculateTarget().get()) != pc_load_addr) {
159 // Another thing that sometimes happens here is that we step out of
160 // one line into the MIDDLE of another line. So far I mostly see
161 // this due to bugs in the debug information. But we probably don't
162 // want to be in the middle of a line range, so in that case reset
163 // the stepping range to the line we've stepped into the middle of
165 m_addr_context = new_context;
166 m_address_ranges.clear();
167 AddRange(m_addr_context.line_entry.range);
171 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
172 true, Address::DumpStyleLoadAddress,
173 Address::DumpStyleLoadAddress, true);
175 log->Printf("Step range plan stepped to the middle of new "
176 "line(%d): %s, continuing to clear this line.",
177 new_context.line_entry.line, s.GetData());
184 if (!ret_value && log)
185 log->Printf("Step range plan out of range to 0x%" PRIx64, pc_load_addr);
190 bool ThreadPlanStepRange::InSymbol() {
191 lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
192 if (m_addr_context.function != nullptr) {
193 return m_addr_context.function->GetAddressRange().ContainsLoadAddress(
194 cur_pc, m_thread.CalculateTarget().get());
195 } else if (m_addr_context.symbol && m_addr_context.symbol->ValueIsAddress()) {
196 AddressRange range(m_addr_context.symbol->GetAddressRef(),
197 m_addr_context.symbol->GetByteSize());
198 return range.ContainsLoadAddress(cur_pc, m_thread.CalculateTarget().get());
203 // FIXME: This should also handle inlining if we aren't going to do inlining in
207 // Ideally we should remember the whole stack frame list, and then compare that
208 // to the current list.
210 lldb::FrameComparison ThreadPlanStepRange::CompareCurrentFrameToStartFrame() {
211 FrameComparison frame_order;
213 StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
215 if (cur_frame_id == m_stack_id) {
216 frame_order = eFrameCompareEqual;
217 } else if (cur_frame_id < m_stack_id) {
218 frame_order = eFrameCompareYounger;
220 StackFrameSP cur_parent_frame = m_thread.GetStackFrameAtIndex(1);
221 StackID cur_parent_id;
222 if (cur_parent_frame)
223 cur_parent_id = cur_parent_frame->GetStackID();
224 if (m_parent_stack_id.IsValid() && cur_parent_id.IsValid() &&
225 m_parent_stack_id == cur_parent_id)
226 frame_order = eFrameCompareSameParent;
228 frame_order = eFrameCompareOlder;
233 bool ThreadPlanStepRange::StopOthers() {
234 return (m_stop_others == lldb::eOnlyThisThread ||
235 m_stop_others == lldb::eOnlyDuringStepping);
238 InstructionList *ThreadPlanStepRange::GetInstructionsForAddress(
239 lldb::addr_t addr, size_t &range_index, size_t &insn_offset) {
240 size_t num_ranges = m_address_ranges.size();
241 for (size_t i = 0; i < num_ranges; i++) {
242 if (m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget())) {
243 // Some joker added a zero size range to the stepping range...
244 if (m_address_ranges[i].GetByteSize() == 0)
247 if (!m_instruction_ranges[i]) {
248 // Disassemble the address range given:
249 ExecutionContext exe_ctx(m_thread.GetProcess());
250 const char *plugin_name = nullptr;
251 const char *flavor = nullptr;
252 const bool prefer_file_cache = true;
253 m_instruction_ranges[i] = Disassembler::DisassembleRange(
254 GetTarget().GetArchitecture(), plugin_name, flavor, exe_ctx,
255 m_address_ranges[i], prefer_file_cache);
257 if (!m_instruction_ranges[i])
260 // Find where we are in the instruction list as well. If we aren't at
261 // an instruction, return nullptr. In this case, we're probably lost,
262 // and shouldn't try to do anything fancy.
265 m_instruction_ranges[i]
266 ->GetInstructionList()
267 .GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
268 if (insn_offset == UINT32_MAX)
272 return &m_instruction_ranges[i]->GetInstructionList();
280 void ThreadPlanStepRange::ClearNextBranchBreakpoint() {
281 if (m_next_branch_bp_sp) {
282 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
284 log->Printf("Removing next branch breakpoint: %d.",
285 m_next_branch_bp_sp->GetID());
286 GetTarget().RemoveBreakpointByID(m_next_branch_bp_sp->GetID());
287 m_next_branch_bp_sp.reset();
291 bool ThreadPlanStepRange::SetNextBranchBreakpoint() {
292 if (m_next_branch_bp_sp)
295 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
296 // Stepping through ranges using breakpoints doesn't work yet, but with this
297 // off we fall back to instruction single stepping.
298 if (!m_use_fast_step)
301 lldb::addr_t cur_addr = GetThread().GetRegisterContext()->GetPC();
302 // Find the current address in our address ranges, and fetch the disassembly
303 // if we haven't already:
306 InstructionList *instructions =
307 GetInstructionsForAddress(cur_addr, range_index, pc_index);
308 if (instructions == nullptr)
311 Target &target = GetThread().GetProcess()->GetTarget();
312 uint32_t branch_index;
314 instructions->GetIndexOfNextBranchInstruction(pc_index, target);
316 Address run_to_address;
318 // If we didn't find a branch, run to the end of the range.
319 if (branch_index == UINT32_MAX) {
320 uint32_t last_index = instructions->GetSize() - 1;
321 if (last_index - pc_index > 1) {
322 InstructionSP last_inst =
323 instructions->GetInstructionAtIndex(last_index);
324 size_t last_inst_size = last_inst->GetOpcode().GetByteSize();
325 run_to_address = last_inst->GetAddress();
326 run_to_address.Slide(last_inst_size);
328 } else if (branch_index - pc_index > 1) {
330 instructions->GetInstructionAtIndex(branch_index)->GetAddress();
333 if (run_to_address.IsValid()) {
334 const bool is_internal = true;
335 m_next_branch_bp_sp =
336 GetTarget().CreateBreakpoint(run_to_address, is_internal, false);
337 if (m_next_branch_bp_sp) {
339 lldb::break_id_t bp_site_id = LLDB_INVALID_BREAK_ID;
340 BreakpointLocationSP bp_loc =
341 m_next_branch_bp_sp->GetLocationAtIndex(0);
343 BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite();
345 bp_site_id = bp_site->GetID();
348 log->Printf("ThreadPlanStepRange::SetNextBranchBreakpoint - Setting "
349 "breakpoint %d (site %d) to run to address 0x%" PRIx64,
350 m_next_branch_bp_sp->GetID(), bp_site_id,
351 run_to_address.GetLoadAddress(
352 &m_thread.GetProcess()->GetTarget()));
354 m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
355 m_next_branch_bp_sp->SetBreakpointKind("next-branch-location");
364 bool ThreadPlanStepRange::NextRangeBreakpointExplainsStop(
365 lldb::StopInfoSP stop_info_sp) {
366 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
367 if (!m_next_branch_bp_sp)
370 break_id_t bp_site_id = stop_info_sp->GetValue();
371 BreakpointSiteSP bp_site_sp =
372 m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id);
375 else if (!bp_site_sp->IsBreakpointAtThisSite(m_next_branch_bp_sp->GetID()))
378 // If we've hit the next branch breakpoint, then clear it.
379 size_t num_owners = bp_site_sp->GetNumberOfOwners();
380 bool explains_stop = true;
381 // If all the owners are internal, then we are probably just stepping over
382 // this range from multiple threads, or multiple frames, so we want to
383 // continue. If one is not internal, then we should not explain the stop,
384 // and let the user breakpoint handle the stop.
385 for (size_t i = 0; i < num_owners; i++) {
386 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal()) {
387 explains_stop = false;
392 log->Printf("ThreadPlanStepRange::NextRangeBreakpointExplainsStop - Hit "
393 "next range breakpoint which has %" PRIu64
394 " owners - explains stop: %u.",
395 (uint64_t)num_owners, explains_stop);
396 ClearNextBranchBreakpoint();
397 return explains_stop;
401 bool ThreadPlanStepRange::WillStop() { return true; }
403 StateType ThreadPlanStepRange::GetPlanRunState() {
404 if (m_next_branch_bp_sp)
405 return eStateRunning;
407 return eStateStepping;
410 bool ThreadPlanStepRange::MischiefManaged() {
411 // If we have pushed some plans between ShouldStop & MischiefManaged, then
413 // I do this check first because we might have stepped somewhere that will
415 // thinking it needs to step past the end of that line. This happens, for
416 // instance, when stepping over inlined code that is in the middle of the
419 if (!m_no_more_plans)
423 if (!IsPlanComplete()) {
427 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
428 done = (frame_order != eFrameCompareOlder) ? m_no_more_plans : true;
433 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
435 log->Printf("Completed step through range plan.");
436 ClearNextBranchBreakpoint();
437 ThreadPlan::MischiefManaged();
444 bool ThreadPlanStepRange::IsPlanStale() {
445 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
446 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
448 if (frame_order == eFrameCompareOlder) {
450 log->Printf("ThreadPlanStepRange::IsPlanStale returning true, we've "
454 } else if (frame_order == eFrameCompareEqual && InSymbol()) {
455 // If we are not in a place we should step through, we've gotten stale. One
456 // tricky bit here is that some stubs don't push a frame, so we should.
457 // check that we are in the same symbol.
459 // Set plan Complete when we reach next instruction just after the range
460 lldb::addr_t addr = m_thread.GetRegisterContext()->GetPC() - 1;
461 size_t num_ranges = m_address_ranges.size();
462 for (size_t i = 0; i < num_ranges; i++) {
463 bool in_range = m_address_ranges[i].ContainsLoadAddress(
464 addr, m_thread.CalculateTarget().get());