blob: 09e606f490a6d39cde94910c08bc4971f03f3583 [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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000014#include "lldb/Target/ThreadPlanStepRange.h"
Jim Ingham0c61dee2013-03-13 01:56:41 +000015#include "lldb/Breakpoint/BreakpointLocation.h"
16#include "lldb/Breakpoint/BreakpointSite.h"
Jim Ingham564d8bc22012-03-09 04:10:47 +000017#include "lldb/Core/Disassembler.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Symbol/Function.h"
19#include "lldb/Symbol/Symbol.h"
Jim Ingham564d8bc22012-03-09 04:10:47 +000020#include "lldb/Target/ExecutionContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000021#include "lldb/Target/Process.h"
22#include "lldb/Target/RegisterContext.h"
23#include "lldb/Target/StopInfo.h"
Jim Ingham564d8bc22012-03-09 04:10:47 +000024#include "lldb/Target/Target.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000025#include "lldb/Target/Thread.h"
Jim Ingham564d8bc22012-03-09 04:10:47 +000026#include "lldb/Target/ThreadPlanRunToAddress.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000027#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000028#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
30using namespace lldb;
31using namespace lldb_private;
32
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033//----------------------------------------------------------------------
34// ThreadPlanStepRange: Step through a stack range, either stepping over or into
35// based on the value of \a type.
36//----------------------------------------------------------------------
37
Kate Stoneb9c1b512016-09-06 20:57:50 +000038ThreadPlanStepRange::ThreadPlanStepRange(ThreadPlanKind kind, const char *name,
39 Thread &thread,
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();
50 AddRange(range);
51 m_stack_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
52 StackFrameSP parent_stack = m_thread.GetStackFrameAtIndex(1);
53 if (parent_stack)
54 m_parent_stack_id = parent_stack->GetStackID();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055}
56
Kate Stoneb9c1b512016-09-06 20:57:50 +000057ThreadPlanStepRange::~ThreadPlanStepRange() { ClearNextBranchBreakpoint(); }
58
59void ThreadPlanStepRange::DidPush() {
60 // See if we can find a "next range" breakpoint:
61 SetNextBranchBreakpoint();
Jim Ingham564d8bc22012-03-09 04:10:47 +000062}
63
Kate Stoneb9c1b512016-09-06 20:57:50 +000064bool ThreadPlanStepRange::ValidatePlan(Stream *error) { return true; }
65
66Vote ThreadPlanStepRange::ShouldReportStop(Event *event_ptr) {
67 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
68
69 const Vote vote = IsPlanComplete() ? eVoteYes : eVoteNo;
70 if (log)
71 log->Printf("ThreadPlanStepRange::ShouldReportStop() returning vote %i\n",
72 vote);
73 return vote;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074}
75
Kate Stoneb9c1b512016-09-06 20:57:50 +000076void 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);
81
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());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087}
88
Kate Stoneb9c1b512016-09-06 20:57:50 +000089void 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);
94 } else {
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);
Jim Inghamc4c9fed2011-10-15 00:24:48 +000099 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 }
101}
102
103bool ThreadPlanStepRange::InRange() {
104 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
105 bool ret_value = false;
106
107 lldb::addr_t pc_load_addr = m_thread.GetRegisterContext()->GetPC();
108
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());
113 if (ret_value)
114 break;
115 }
116
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();
120
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;
129 AddRange(
130 m_addr_context.line_entry.GetSameLineContiguousAddressRange());
131 ret_value = true;
132 if (log) {
133 StreamString s;
134 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
135 true, Address::DumpStyleLoadAddress,
136 Address::DumpStyleLoadAddress, true);
137
138 log->Printf(
139 "Step range plan stepped to another range of same line: %s",
140 s.GetData());
141 }
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;
145 AddRange(
146 m_addr_context.line_entry.GetSameLineContiguousAddressRange());
147 ret_value = true;
148 if (log) {
149 StreamString s;
150 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
151 true, Address::DumpStyleLoadAddress,
152 Address::DumpStyleLoadAddress, true);
153
154 log->Printf("Step range plan stepped to a range at linenumber 0 "
155 "stepping through that range: %s",
156 s.GetData());
157 }
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
163 // information.
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);
170 ret_value = true;
171 if (log) {
172 StreamString s;
173 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
174 true, Address::DumpStyleLoadAddress,
175 Address::DumpStyleLoadAddress, true);
176
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());
180 }
Jim Inghamc4c9fed2011-10-15 00:24:48 +0000181 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 }
Jim Inghamc4c9fed2011-10-15 00:24:48 +0000183 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 }
185
186 if (!ret_value && log)
187 log->Printf("Step range plan out of range to 0x%" PRIx64, pc_load_addr);
188
189 return ret_value;
Jim Inghamc4c9fed2011-10-15 00:24:48 +0000190}
191
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192bool 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());
201 }
202 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203}
204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205// FIXME: This should also handle inlining if we aren't going to do inlining in
206// the
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207// main stack.
208//
209// Ideally we should remember the whole stack frame list, and then compare that
210// to the current list.
211
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212lldb::FrameComparison ThreadPlanStepRange::CompareCurrentFrameToStartFrame() {
213 FrameComparison frame_order;
214
215 StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
216
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;
221 } else {
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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230 frame_order = eFrameCompareOlder;
231 }
232 return frame_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233}
234
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235bool ThreadPlanStepRange::StopOthers() {
236 return (m_stop_others == lldb::eOnlyThisThread ||
237 m_stop_others == lldb::eOnlyDuringStepping);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238}
239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240InstructionList *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)
247 return nullptr;
Jim Ingham564d8bc22012-03-09 04:10:47 +0000248
Kate Stoneb9c1b512016-09-06 20:57:50 +0000249 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);
258 }
259 if (!m_instruction_ranges[i])
260 return nullptr;
261 else {
262 // Find where we are in the instruction list as well. If we aren't at
263 // an instruction,
264 // return nullptr. In this case, we're probably lost, and shouldn't try
265 // to do anything fancy.
266
267 insn_offset =
268 m_instruction_ranges[i]
269 ->GetInstructionList()
270 .GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
271 if (insn_offset == UINT32_MAX)
272 return nullptr;
273 else {
274 range_index = i;
275 return &m_instruction_ranges[i]->GetInstructionList();
Jim Ingham564d8bc22012-03-09 04:10:47 +0000276 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 }
Jim Ingham564d8bc22012-03-09 04:10:47 +0000278 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 }
280 return nullptr;
Jim Ingham564d8bc22012-03-09 04:10:47 +0000281}
282
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283void ThreadPlanStepRange::ClearNextBranchBreakpoint() {
284 if (m_next_branch_bp_sp) {
285 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
286 if (log)
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();
291 }
Jim Ingham564d8bc22012-03-09 04:10:47 +0000292}
293
Kate Stoneb9c1b512016-09-06 20:57:50 +0000294bool ThreadPlanStepRange::SetNextBranchBreakpoint() {
295 if (m_next_branch_bp_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 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
301 // single stepping.
302 if (!m_use_fast_step)
Jim Ingham64e7ead2012-05-03 21:19:36 +0000303 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000304
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:
308 size_t pc_index;
309 size_t range_index;
310 InstructionList *instructions =
311 GetInstructionsForAddress(cur_addr, range_index, pc_index);
312 if (instructions == nullptr)
313 return false;
314 else {
315 Target &target = GetThread().GetProcess()->GetTarget();
316 uint32_t branch_index;
317 branch_index =
318 instructions->GetIndexOfNextBranchInstruction(pc_index, target);
319
320 Address run_to_address;
321
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);
331 }
332 } else if (branch_index - pc_index > 1) {
333 run_to_address =
334 instructions->GetInstructionAtIndex(branch_index)->GetAddress();
335 }
336
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) {
342 if (log) {
343 lldb::break_id_t bp_site_id = LLDB_INVALID_BREAK_ID;
344 BreakpointLocationSP bp_loc =
345 m_next_branch_bp_sp->GetLocationAtIndex(0);
346 if (bp_loc) {
347 BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite();
348 if (bp_site) {
349 bp_site_id = bp_site->GetID();
350 }
351 }
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()));
357 }
358 m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
359 m_next_branch_bp_sp->SetBreakpointKind("next-branch-location");
360 return true;
361 } else
362 return false;
363 }
364 }
365 return false;
366}
367
368bool ThreadPlanStepRange::NextRangeBreakpointExplainsStop(
369 lldb::StopInfoSP stop_info_sp) {
370 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
371 if (!m_next_branch_bp_sp)
372 return false;
373
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);
377 if (!bp_site_sp)
378 return false;
379 else if (!bp_site_sp->IsBreakpointAtThisSite(m_next_branch_bp_sp->GetID()))
380 return false;
381 else {
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;
393 break;
394 }
395 }
396 if (log)
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;
403 }
404}
405
406bool ThreadPlanStepRange::WillStop() { return true; }
407
408StateType ThreadPlanStepRange::GetPlanRunState() {
409 if (m_next_branch_bp_sp)
410 return eStateRunning;
411 else
412 return eStateStepping;
413}
414
415bool ThreadPlanStepRange::MischiefManaged() {
416 // If we have pushed some plans between ShouldStop & MischiefManaged, then
417 // we're not done...
418 // I do this check first because we might have stepped somewhere that will
419 // fool InRange into
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.
423
424 if (!m_no_more_plans)
425 return false;
426
427 bool done = true;
428 if (!IsPlanComplete()) {
429 if (InRange()) {
430 done = false;
431 } else {
432 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
433 done = (frame_order != eFrameCompareOlder) ? m_no_more_plans : true;
434 }
435 }
436
437 if (done) {
438 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
439 if (log)
440 log->Printf("Completed step through range plan.");
441 ClearNextBranchBreakpoint();
442 ThreadPlan::MischiefManaged();
443 return true;
444 } else {
445 return false;
446 }
447}
448
449bool ThreadPlanStepRange::IsPlanStale() {
450 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
451 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
452
453 if (frame_order == eFrameCompareOlder) {
454 if (log) {
455 log->Printf("ThreadPlanStepRange::IsPlanStale returning true, we've "
456 "stepped out.");
457 }
458 return true;
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.
463 if (!InRange()) {
Boris Ulasevich86aaa8a2017-02-15 11:42:47 +0000464 // 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());
470 if (in_range) {
471 SetPlanComplete();
472 }
473 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000474 return true;
475 }
476 }
477 return false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000478}