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 into
35 // 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
78 // condense the ranges if they overlap, though I don't think it is likely
79 // to be very important.
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
84 // indices to match, but I don't want to do the work to disassemble this range
85 // if I don't step into it.
86 m_instruction_ranges.push_back(DisassemblerSP());
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);
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);
103 bool ThreadPlanStepRange::InRange() {
104 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
105 bool ret_value = false;
107 lldb::addr_t pc_load_addr = m_thread.GetRegisterContext()->GetPC();
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());
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();
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;
130 m_addr_context.line_entry.GetSameLineContiguousAddressRange());
134 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
135 true, Address::DumpStyleLoadAddress,
136 Address::DumpStyleLoadAddress, true);
139 "Step range plan stepped to another range of same line: %s",
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;
146 m_addr_context.line_entry.GetSameLineContiguousAddressRange());
150 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
151 true, Address::DumpStyleLoadAddress,
152 Address::DumpStyleLoadAddress, true);
154 log->Printf("Step range plan stepped to a range at linenumber 0 "
155 "stepping through that range: %s",
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
162 // line. So far I mostly see this due to bugs in the debug
164 // But we probably don't want to be in the middle of a line range, so
165 // in that case reset the stepping
166 // range to the line we've stepped into the middle of and continue.
167 m_addr_context = new_context;
168 m_address_ranges.clear();
169 AddRange(m_addr_context.line_entry.range);
173 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
174 true, Address::DumpStyleLoadAddress,
175 Address::DumpStyleLoadAddress, true);
177 log->Printf("Step range plan stepped to the middle of new "
178 "line(%d): %s, continuing to clear this line.",
179 new_context.line_entry.line, s.GetData());
186 if (!ret_value && log)
187 log->Printf("Step range plan out of range to 0x%" PRIx64, pc_load_addr);
192 bool ThreadPlanStepRange::InSymbol() {
193 lldb::addr_t cur_pc = m_thread.GetRegisterContext()->GetPC();
194 if (m_addr_context.function != nullptr) {
195 return m_addr_context.function->GetAddressRange().ContainsLoadAddress(
196 cur_pc, m_thread.CalculateTarget().get());
197 } else if (m_addr_context.symbol && m_addr_context.symbol->ValueIsAddress()) {
198 AddressRange range(m_addr_context.symbol->GetAddressRef(),
199 m_addr_context.symbol->GetByteSize());
200 return range.ContainsLoadAddress(cur_pc, m_thread.CalculateTarget().get());
205 // FIXME: This should also handle inlining if we aren't going to do inlining in
209 // Ideally we should remember the whole stack frame list, and then compare that
210 // to the current list.
212 lldb::FrameComparison ThreadPlanStepRange::CompareCurrentFrameToStartFrame() {
213 FrameComparison frame_order;
215 StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
217 if (cur_frame_id == m_stack_id) {
218 frame_order = eFrameCompareEqual;
219 } else if (cur_frame_id < m_stack_id) {
220 frame_order = eFrameCompareYounger;
222 StackFrameSP cur_parent_frame = m_thread.GetStackFrameAtIndex(1);
223 StackID cur_parent_id;
224 if (cur_parent_frame)
225 cur_parent_id = cur_parent_frame->GetStackID();
226 if (m_parent_stack_id.IsValid() && cur_parent_id.IsValid() &&
227 m_parent_stack_id == cur_parent_id)
228 frame_order = eFrameCompareSameParent;
230 frame_order = eFrameCompareOlder;
235 bool ThreadPlanStepRange::StopOthers() {
236 return (m_stop_others == lldb::eOnlyThisThread ||
237 m_stop_others == lldb::eOnlyDuringStepping);
240 InstructionList *ThreadPlanStepRange::GetInstructionsForAddress(
241 lldb::addr_t addr, size_t &range_index, size_t &insn_offset) {
242 size_t num_ranges = m_address_ranges.size();
243 for (size_t i = 0; i < num_ranges; i++) {
244 if (m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget())) {
245 // Some joker added a zero size range to the stepping range...
246 if (m_address_ranges[i].GetByteSize() == 0)
249 if (!m_instruction_ranges[i]) {
250 // Disassemble the address range given:
251 ExecutionContext exe_ctx(m_thread.GetProcess());
252 const char *plugin_name = nullptr;
253 const char *flavor = nullptr;
254 const bool prefer_file_cache = true;
255 m_instruction_ranges[i] = Disassembler::DisassembleRange(
256 GetTarget().GetArchitecture(), plugin_name, flavor, exe_ctx,
257 m_address_ranges[i], prefer_file_cache);
259 if (!m_instruction_ranges[i])
262 // Find where we are in the instruction list as well. If we aren't at
264 // return nullptr. In this case, we're probably lost, and shouldn't try
265 // to do anything fancy.
268 m_instruction_ranges[i]
269 ->GetInstructionList()
270 .GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
271 if (insn_offset == UINT32_MAX)
275 return &m_instruction_ranges[i]->GetInstructionList();
283 void ThreadPlanStepRange::ClearNextBranchBreakpoint() {
284 if (m_next_branch_bp_sp) {
285 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
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();
294 bool ThreadPlanStepRange::SetNextBranchBreakpoint() {
295 if (m_next_branch_bp_sp)
298 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
299 // Stepping through ranges using breakpoints doesn't work yet, but with this
300 // off we fall back to instruction
302 if (!m_use_fast_step)
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:
310 InstructionList *instructions =
311 GetInstructionsForAddress(cur_addr, range_index, pc_index);
312 if (instructions == nullptr)
315 Target &target = GetThread().GetProcess()->GetTarget();
316 uint32_t branch_index;
318 instructions->GetIndexOfNextBranchInstruction(pc_index, target);
320 Address run_to_address;
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);
332 } else if (branch_index - pc_index > 1) {
334 instructions->GetInstructionAtIndex(branch_index)->GetAddress();
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) {
343 lldb::break_id_t bp_site_id = LLDB_INVALID_BREAK_ID;
344 BreakpointLocationSP bp_loc =
345 m_next_branch_bp_sp->GetLocationAtIndex(0);
347 BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite();
349 bp_site_id = bp_site->GetID();
352 log->Printf("ThreadPlanStepRange::SetNextBranchBreakpoint - Setting "
353 "breakpoint %d (site %d) to run to address 0x%" PRIx64,
354 m_next_branch_bp_sp->GetID(), bp_site_id,
355 run_to_address.GetLoadAddress(
356 &m_thread.GetProcess()->GetTarget()));
358 m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
359 m_next_branch_bp_sp->SetBreakpointKind("next-branch-location");
368 bool ThreadPlanStepRange::NextRangeBreakpointExplainsStop(
369 lldb::StopInfoSP stop_info_sp) {
370 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
371 if (!m_next_branch_bp_sp)
374 break_id_t bp_site_id = stop_info_sp->GetValue();
375 BreakpointSiteSP bp_site_sp =
376 m_thread.GetProcess()->GetBreakpointSiteList().FindByID(bp_site_id);
379 else if (!bp_site_sp->IsBreakpointAtThisSite(m_next_branch_bp_sp->GetID()))
382 // If we've hit the next branch breakpoint, then clear it.
383 size_t num_owners = bp_site_sp->GetNumberOfOwners();
384 bool explains_stop = true;
385 // If all the owners are internal, then we are probably just stepping over
386 // this range from multiple threads,
387 // or multiple frames, so we want to continue. If one is not internal, then
388 // we should not explain the stop,
389 // and let the user breakpoint handle the stop.
390 for (size_t i = 0; i < num_owners; i++) {
391 if (!bp_site_sp->GetOwnerAtIndex(i)->GetBreakpoint().IsInternal()) {
392 explains_stop = false;
397 log->Printf("ThreadPlanStepRange::NextRangeBreakpointExplainsStop - Hit "
398 "next range breakpoint which has %" PRIu64
399 " owners - explains stop: %u.",
400 (uint64_t)num_owners, explains_stop);
401 ClearNextBranchBreakpoint();
402 return explains_stop;
406 bool ThreadPlanStepRange::WillStop() { return true; }
408 StateType ThreadPlanStepRange::GetPlanRunState() {
409 if (m_next_branch_bp_sp)
410 return eStateRunning;
412 return eStateStepping;
415 bool ThreadPlanStepRange::MischiefManaged() {
416 // If we have pushed some plans between ShouldStop & MischiefManaged, then
418 // I do this check first because we might have stepped somewhere that will
420 // thinking it needs to step past the end of that line. This happens, for
421 // instance, when stepping
422 // over inlined code that is in the middle of the current line.
424 if (!m_no_more_plans)
428 if (!IsPlanComplete()) {
432 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
433 done = (frame_order != eFrameCompareOlder) ? m_no_more_plans : true;
438 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
440 log->Printf("Completed step through range plan.");
441 ClearNextBranchBreakpoint();
442 ThreadPlan::MischiefManaged();
449 bool ThreadPlanStepRange::IsPlanStale() {
450 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
451 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
453 if (frame_order == eFrameCompareOlder) {
455 log->Printf("ThreadPlanStepRange::IsPlanStale returning true, we've "
459 } else if (frame_order == eFrameCompareEqual && InSymbol()) {
460 // If we are not in a place we should step through, we've gotten stale.
461 // One tricky bit here is that some stubs don't push a frame, so we should.
462 // check that we are in the same symbol.
464 // Set plan Complete when we reach next instruction just after the range
465 lldb::addr_t addr = m_thread.GetRegisterContext()->GetPC() - 1;
466 size_t num_ranges = m_address_ranges.size();
467 for (size_t i = 0; i < num_ranges; i++) {
468 bool in_range = m_address_ranges[i].ContainsLoadAddress(
469 addr, m_thread.CalculateTarget().get());