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 //===----------------------------------------------------------------------===//
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"
27 using namespace lldb_private;
29 //----------------------------------------------------------------------
30 // ThreadPlanStepRange: Step through a stack range, either stepping over or
31 // into based on the value of \a type.
32 //----------------------------------------------------------------------
34 ThreadPlanStepRange::ThreadPlanStepRange(ThreadPlanKind kind, const char *name,
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();
47 m_stack_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
48 StackFrameSP parent_stack = m_thread.GetStackFrameAtIndex(1);
50 m_parent_stack_id = parent_stack->GetStackID();
53 ThreadPlanStepRange::~ThreadPlanStepRange() { ClearNextBranchBreakpoint(); }
55 void ThreadPlanStepRange::DidPush() {
56 // See if we can find a "next range" breakpoint:
57 SetNextBranchBreakpoint();
60 bool ThreadPlanStepRange::ValidatePlan(Stream *error) {
61 if (m_could_not_resolve_hw_bp) {
64 "Could not create hardware breakpoint for thread plan.");
70 Vote ThreadPlanStepRange::ShouldReportStop(Event *event_ptr) {
71 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
73 const Vote vote = IsPlanComplete() ? eVoteYes : eVoteNo;
75 log->Printf("ThreadPlanStepRange::ShouldReportStop() returning vote %i\n",
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
84 m_address_ranges.push_back(new_range);
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());
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);
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);
106 bool ThreadPlanStepRange::InRange() {
107 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
108 bool ret_value = false;
110 lldb::addr_t pc_load_addr = m_thread.GetRegisterContext()->GetPC();
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());
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();
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;
133 m_addr_context.line_entry.GetSameLineContiguousAddressRange());
137 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
138 true, Address::DumpStyleLoadAddress,
139 Address::DumpStyleLoadAddress, true);
142 "Step range plan stepped to another range of same line: %s",
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;
149 m_addr_context.line_entry.GetSameLineContiguousAddressRange());
153 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
154 true, Address::DumpStyleLoadAddress,
155 Address::DumpStyleLoadAddress, true);
157 log->Printf("Step range plan stepped to a range at linenumber 0 "
158 "stepping through that range: %s",
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
169 m_addr_context = new_context;
170 m_address_ranges.clear();
171 AddRange(m_addr_context.line_entry.range);
175 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
176 true, Address::DumpStyleLoadAddress,
177 Address::DumpStyleLoadAddress, true);
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());
188 if (!ret_value && log)
189 log->Printf("Step range plan out of range to 0x%" PRIx64, pc_load_addr);
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());
207 // FIXME: This should also handle inlining if we aren't going to do inlining in
211 // Ideally we should remember the whole stack frame list, and then compare that
212 // to the current list.
214 lldb::FrameComparison ThreadPlanStepRange::CompareCurrentFrameToStartFrame() {
215 FrameComparison frame_order;
217 StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
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;
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;
232 frame_order = eFrameCompareOlder;
237 bool ThreadPlanStepRange::StopOthers() {
238 return (m_stop_others == lldb::eOnlyThisThread ||
239 m_stop_others == lldb::eOnlyDuringStepping);
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)
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);
261 if (!m_instruction_ranges[i])
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.
269 m_instruction_ranges[i]
270 ->GetInstructionList()
271 .GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
272 if (insn_offset == UINT32_MAX)
276 return &m_instruction_ranges[i]->GetInstructionList();
284 void ThreadPlanStepRange::ClearNextBranchBreakpoint() {
285 if (m_next_branch_bp_sp) {
286 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
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;
296 bool ThreadPlanStepRange::SetNextBranchBreakpoint() {
297 if (m_next_branch_bp_sp)
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)
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:
311 InstructionList *instructions =
312 GetInstructionsForAddress(cur_addr, range_index, pc_index);
313 if (instructions == nullptr)
316 Target &target = GetThread().GetProcess()->GetTarget();
317 uint32_t branch_index;
319 instructions->GetIndexOfNextBranchInstruction(pc_index, target);
321 Address run_to_address;
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);
333 } else if (branch_index - pc_index > 1) {
335 instructions->GetInstructionAtIndex(branch_index)->GetAddress();
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) {
344 if (m_next_branch_bp_sp->IsHardware() &&
345 !m_next_branch_bp_sp->HasResolvedLocations())
346 m_could_not_resolve_hw_bp = true;
349 lldb::break_id_t bp_site_id = LLDB_INVALID_BREAK_ID;
350 BreakpointLocationSP bp_loc =
351 m_next_branch_bp_sp->GetLocationAtIndex(0);
353 BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite();
355 bp_site_id = bp_site->GetID();
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()));
365 m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
366 m_next_branch_bp_sp->SetBreakpointKind("next-branch-location");
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)
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);
387 else if (!bp_site_sp->IsBreakpointAtThisSite(m_next_branch_bp_sp->GetID()))
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;
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;
413 bool ThreadPlanStepRange::WillStop() { return true; }
415 StateType ThreadPlanStepRange::GetPlanRunState() {
416 if (m_next_branch_bp_sp)
417 return eStateRunning;
419 return eStateStepping;
422 bool ThreadPlanStepRange::MischiefManaged() {
423 // If we have pushed some plans between ShouldStop & MischiefManaged, then
425 // I do this check first because we might have stepped somewhere that will
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
431 if (!m_no_more_plans)
435 if (!IsPlanComplete()) {
439 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
440 done = (frame_order != eFrameCompareOlder) ? m_no_more_plans : true;
445 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
447 log->Printf("Completed step through range plan.");
448 ClearNextBranchBreakpoint();
449 ThreadPlan::MischiefManaged();
456 bool ThreadPlanStepRange::IsPlanStale() {
457 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
458 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
460 if (frame_order == eFrameCompareOlder) {
462 log->Printf("ThreadPlanStepRange::IsPlanStale returning true, we've "
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.
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());