blob: 5a71119015eb0e780ece997e9d210763e6ed91bb [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//----------------------------------------------------------------------
Adrian Prantl05097242018-04-30 16:49:04 +000034// ThreadPlanStepRange: Step through a stack range, either stepping over or
35// into based on the value of \a type.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036//----------------------------------------------------------------------
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) {
Adrian Prantl05097242018-04-30 16:49:04 +000077 // 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
79 // important.
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 m_address_ranges.push_back(new_range);
81
82 // Fill the slot for this address range with an empty DisassemblerSP in the
Adrian Prantl05097242018-04-30 16:49:04 +000083 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 m_instruction_ranges.push_back(DisassemblerSP());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086}
87
Kate Stoneb9c1b512016-09-06 20:57:50 +000088void 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);
93 } else {
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);
Jim Inghamc4c9fed2011-10-15 00:24:48 +000098 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 }
100}
101
102bool ThreadPlanStepRange::InRange() {
103 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
104 bool ret_value = false;
105
106 lldb::addr_t pc_load_addr = m_thread.GetRegisterContext()->GetPC();
107
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());
112 if (ret_value)
113 break;
114 }
115
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();
119
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;
128 AddRange(
129 m_addr_context.line_entry.GetSameLineContiguousAddressRange());
130 ret_value = true;
131 if (log) {
132 StreamString s;
133 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
134 true, Address::DumpStyleLoadAddress,
135 Address::DumpStyleLoadAddress, true);
136
137 log->Printf(
138 "Step range plan stepped to another range of same line: %s",
139 s.GetData());
140 }
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;
144 AddRange(
145 m_addr_context.line_entry.GetSameLineContiguousAddressRange());
146 ret_value = true;
147 if (log) {
148 StreamString s;
149 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
150 true, Address::DumpStyleLoadAddress,
151 Address::DumpStyleLoadAddress, true);
152
153 log->Printf("Step range plan stepped to a range at linenumber 0 "
154 "stepping through that range: %s",
155 s.GetData());
156 }
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
Adrian Prantl05097242018-04-30 16:49:04 +0000160 // 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
164 // and continue.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 m_addr_context = new_context;
166 m_address_ranges.clear();
167 AddRange(m_addr_context.line_entry.range);
168 ret_value = true;
169 if (log) {
170 StreamString s;
171 m_addr_context.line_entry.Dump(&s, m_thread.CalculateTarget().get(),
172 true, Address::DumpStyleLoadAddress,
173 Address::DumpStyleLoadAddress, true);
174
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());
178 }
Jim Inghamc4c9fed2011-10-15 00:24:48 +0000179 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 }
Jim Inghamc4c9fed2011-10-15 00:24:48 +0000181 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 }
183
184 if (!ret_value && log)
185 log->Printf("Step range plan out of range to 0x%" PRIx64, pc_load_addr);
186
187 return ret_value;
Jim Inghamc4c9fed2011-10-15 00:24:48 +0000188}
189
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190bool 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());
199 }
200 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201}
202
Kate Stoneb9c1b512016-09-06 20:57:50 +0000203// FIXME: This should also handle inlining if we aren't going to do inlining in
204// the
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205// main stack.
206//
207// Ideally we should remember the whole stack frame list, and then compare that
208// to the current list.
209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210lldb::FrameComparison ThreadPlanStepRange::CompareCurrentFrameToStartFrame() {
211 FrameComparison frame_order;
212
213 StackID cur_frame_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
214
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;
219 } else {
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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 frame_order = eFrameCompareOlder;
229 }
230 return frame_order;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231}
232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233bool ThreadPlanStepRange::StopOthers() {
234 return (m_stop_others == lldb::eOnlyThisThread ||
235 m_stop_others == lldb::eOnlyDuringStepping);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236}
237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238InstructionList *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)
245 return nullptr;
Jim Ingham564d8bc22012-03-09 04:10:47 +0000246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 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);
256 }
257 if (!m_instruction_ranges[i])
258 return nullptr;
259 else {
260 // Find where we are in the instruction list as well. If we aren't at
Adrian Prantl05097242018-04-30 16:49:04 +0000261 // an instruction, return nullptr. In this case, we're probably lost,
262 // and shouldn't try to do anything fancy.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263
264 insn_offset =
265 m_instruction_ranges[i]
266 ->GetInstructionList()
267 .GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
268 if (insn_offset == UINT32_MAX)
269 return nullptr;
270 else {
271 range_index = i;
272 return &m_instruction_ranges[i]->GetInstructionList();
Jim Ingham564d8bc22012-03-09 04:10:47 +0000273 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274 }
Jim Ingham564d8bc22012-03-09 04:10:47 +0000275 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000276 }
277 return nullptr;
Jim Ingham564d8bc22012-03-09 04:10:47 +0000278}
279
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280void ThreadPlanStepRange::ClearNextBranchBreakpoint() {
281 if (m_next_branch_bp_sp) {
282 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
283 if (log)
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();
288 }
Jim Ingham564d8bc22012-03-09 04:10:47 +0000289}
290
Kate Stoneb9c1b512016-09-06 20:57:50 +0000291bool ThreadPlanStepRange::SetNextBranchBreakpoint() {
292 if (m_next_branch_bp_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
296 // Stepping through ranges using breakpoints doesn't work yet, but with this
Adrian Prantl05097242018-04-30 16:49:04 +0000297 // off we fall back to instruction single stepping.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000298 if (!m_use_fast_step)
Jim Ingham64e7ead2012-05-03 21:19:36 +0000299 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000300
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:
304 size_t pc_index;
305 size_t range_index;
306 InstructionList *instructions =
307 GetInstructionsForAddress(cur_addr, range_index, pc_index);
308 if (instructions == nullptr)
309 return false;
310 else {
311 Target &target = GetThread().GetProcess()->GetTarget();
312 uint32_t branch_index;
313 branch_index =
314 instructions->GetIndexOfNextBranchInstruction(pc_index, target);
315
316 Address run_to_address;
317
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);
327 }
328 } else if (branch_index - pc_index > 1) {
329 run_to_address =
330 instructions->GetInstructionAtIndex(branch_index)->GetAddress();
331 }
332
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) {
338 if (log) {
339 lldb::break_id_t bp_site_id = LLDB_INVALID_BREAK_ID;
340 BreakpointLocationSP bp_loc =
341 m_next_branch_bp_sp->GetLocationAtIndex(0);
342 if (bp_loc) {
343 BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite();
344 if (bp_site) {
345 bp_site_id = bp_site->GetID();
346 }
347 }
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()));
353 }
354 m_next_branch_bp_sp->SetThreadID(m_thread.GetID());
355 m_next_branch_bp_sp->SetBreakpointKind("next-branch-location");
356 return true;
357 } else
358 return false;
359 }
360 }
361 return false;
362}
363
364bool ThreadPlanStepRange::NextRangeBreakpointExplainsStop(
365 lldb::StopInfoSP stop_info_sp) {
366 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
367 if (!m_next_branch_bp_sp)
368 return false;
369
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);
373 if (!bp_site_sp)
374 return false;
375 else if (!bp_site_sp->IsBreakpointAtThisSite(m_next_branch_bp_sp->GetID()))
376 return false;
377 else {
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
Adrian Prantl05097242018-04-30 16:49:04 +0000382 // 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,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 // 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;
388 break;
389 }
390 }
391 if (log)
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;
398 }
399}
400
401bool ThreadPlanStepRange::WillStop() { return true; }
402
403StateType ThreadPlanStepRange::GetPlanRunState() {
404 if (m_next_branch_bp_sp)
405 return eStateRunning;
406 else
407 return eStateStepping;
408}
409
410bool ThreadPlanStepRange::MischiefManaged() {
411 // If we have pushed some plans between ShouldStop & MischiefManaged, then
412 // we're not done...
413 // I do this check first because we might have stepped somewhere that will
414 // fool InRange into
415 // thinking it needs to step past the end of that line. This happens, for
Adrian Prantl05097242018-04-30 16:49:04 +0000416 // instance, when stepping over inlined code that is in the middle of the
417 // current line.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000418
419 if (!m_no_more_plans)
420 return false;
421
422 bool done = true;
423 if (!IsPlanComplete()) {
424 if (InRange()) {
425 done = false;
426 } else {
427 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
428 done = (frame_order != eFrameCompareOlder) ? m_no_more_plans : true;
429 }
430 }
431
432 if (done) {
433 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
434 if (log)
435 log->Printf("Completed step through range plan.");
436 ClearNextBranchBreakpoint();
437 ThreadPlan::MischiefManaged();
438 return true;
439 } else {
440 return false;
441 }
442}
443
444bool ThreadPlanStepRange::IsPlanStale() {
445 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
446 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
447
448 if (frame_order == eFrameCompareOlder) {
449 if (log) {
450 log->Printf("ThreadPlanStepRange::IsPlanStale returning true, we've "
451 "stepped out.");
452 }
453 return true;
454 } else if (frame_order == eFrameCompareEqual && InSymbol()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000455 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000457 // check that we are in the same symbol.
458 if (!InRange()) {
Boris Ulasevich86aaa8a2017-02-15 11:42:47 +0000459 // 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());
465 if (in_range) {
466 SetPlanComplete();
467 }
468 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 return true;
470 }
471 }
472 return false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000473}