blob: 507f0e7f3a39772d4b0edd79d3f801ad47bd778b [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000010#include "lldb/Target/ThreadPlanStepRange.h"
Jim Ingham0c61dee2013-03-13 01:56:41 +000011#include "lldb/Breakpoint/BreakpointLocation.h"
12#include "lldb/Breakpoint/BreakpointSite.h"
Jim Ingham564d8bc22012-03-09 04:10:47 +000013#include "lldb/Core/Disassembler.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Symbol/Function.h"
15#include "lldb/Symbol/Symbol.h"
Jim Ingham564d8bc22012-03-09 04:10:47 +000016#include "lldb/Target/ExecutionContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000017#include "lldb/Target/Process.h"
18#include "lldb/Target/RegisterContext.h"
19#include "lldb/Target/StopInfo.h"
Jim Ingham564d8bc22012-03-09 04:10:47 +000020#include "lldb/Target/Target.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000021#include "lldb/Target/Thread.h"
Jim Ingham564d8bc22012-03-09 04:10:47 +000022#include "lldb/Target/ThreadPlanRunToAddress.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000023#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000024#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
26using namespace lldb;
27using namespace lldb_private;
28
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +000030// ThreadPlanStepRange: Step through a stack range, either stepping over or
31// into based on the value of \a type.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032//----------------------------------------------------------------------
33
Kate Stoneb9c1b512016-09-06 20:57:50 +000034ThreadPlanStepRange::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();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051}
52
Kate Stoneb9c1b512016-09-06 20:57:50 +000053ThreadPlanStepRange::~ThreadPlanStepRange() { ClearNextBranchBreakpoint(); }
54
55void ThreadPlanStepRange::DidPush() {
56 // See if we can find a "next range" breakpoint:
57 SetNextBranchBreakpoint();
Jim Ingham564d8bc22012-03-09 04:10:47 +000058}
59
Kate Stoneb9c1b512016-09-06 20:57:50 +000060bool ThreadPlanStepRange::ValidatePlan(Stream *error) { return true; }
61
62Vote 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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070}
71
Kate Stoneb9c1b512016-09-06 20:57:50 +000072void ThreadPlanStepRange::AddRange(const AddressRange &new_range) {
Adrian Prantl05097242018-04-30 16:49:04 +000073 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 m_address_ranges.push_back(new_range);
77
78 // Fill the slot for this address range with an empty DisassemblerSP in the
Adrian Prantl05097242018-04-30 16:49:04 +000079 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 m_instruction_ranges.push_back(DisassemblerSP());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082}
83
Kate Stoneb9c1b512016-09-06 20:57:50 +000084void 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);
Jim Inghamc4c9fed2011-10-15 00:24:48 +000094 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 }
96}
97
98bool 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
Adrian Prantl05097242018-04-30 16:49:04 +0000156 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 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 }
Jim Inghamc4c9fed2011-10-15 00:24:48 +0000175 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176 }
Jim Inghamc4c9fed2011-10-15 00:24:48 +0000177 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178 }
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;
Jim Inghamc4c9fed2011-10-15 00:24:48 +0000184}
185
Kate Stoneb9c1b512016-09-06 20:57:50 +0000186bool 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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197}
198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199// FIXME: This should also handle inlining if we aren't going to do inlining in
200// the
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201// main stack.
202//
203// Ideally we should remember the whole stack frame list, and then compare that
204// to the current list.
205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206lldb::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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 frame_order = eFrameCompareOlder;
225 }
226 return frame_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227}
228
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229bool ThreadPlanStepRange::StopOthers() {
230 return (m_stop_others == lldb::eOnlyThisThread ||
231 m_stop_others == lldb::eOnlyDuringStepping);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232}
233
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234InstructionList *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;
Jim Ingham564d8bc22012-03-09 04:10:47 +0000242
Kate Stoneb9c1b512016-09-06 20:57:50 +0000243 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
Adrian Prantl05097242018-04-30 16:49:04 +0000257 // an instruction, return nullptr. In this case, we're probably lost,
258 // and shouldn't try to do anything fancy.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259
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();
Jim Ingham564d8bc22012-03-09 04:10:47 +0000269 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 }
Jim Ingham564d8bc22012-03-09 04:10:47 +0000271 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 }
273 return nullptr;
Jim Ingham564d8bc22012-03-09 04:10:47 +0000274}
275
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276void 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 }
Jim Ingham564d8bc22012-03-09 04:10:47 +0000285}
286
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287bool ThreadPlanStepRange::SetNextBranchBreakpoint() {
288 if (m_next_branch_bp_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
292 // Stepping through ranges using breakpoints doesn't work yet, but with this
Adrian Prantl05097242018-04-30 16:49:04 +0000293 // off we fall back to instruction single stepping.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294 if (!m_use_fast_step)
Jim Ingham64e7ead2012-05-03 21:19:36 +0000295 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296
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
360bool 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
Adrian Prantl05097242018-04-30 16:49:04 +0000378 // 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,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380 // 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
397bool ThreadPlanStepRange::WillStop() { return true; }
398
399StateType ThreadPlanStepRange::GetPlanRunState() {
400 if (m_next_branch_bp_sp)
401 return eStateRunning;
402 else
403 return eStateStepping;
404}
405
406bool 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
Adrian Prantl05097242018-04-30 16:49:04 +0000412 // instance, when stepping over inlined code that is in the middle of the
413 // current line.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414
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
440bool 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()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000451 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000453 // check that we are in the same symbol.
454 if (!InRange()) {
Boris Ulasevich86aaa8a2017-02-15 11:42:47 +0000455 // 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 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000465 return true;
466 }
467 }
468 return false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000469}