LLDB mainline
ThreadPlanStepRange.cpp
Go to the documentation of this file.
1//===-- ThreadPlanStepRange.cpp -------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
14#include "lldb/Symbol/Symbol.h"
16#include "lldb/Target/Process.h"
19#include "lldb/Target/Target.h"
20#include "lldb/Target/Thread.h"
23#include "lldb/Utility/Log.h"
24#include "lldb/Utility/Stream.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
29// ThreadPlanStepRange: Step through a stack range, either stepping over or
30// into based on the value of \a type.
31
33 Thread &thread,
34 const AddressRange &range,
35 const SymbolContext &addr_context,
36 lldb::RunMode stop_others,
37 bool given_ranges_only)
38 : ThreadPlan(kind, name, thread, eVoteNoOpinion, eVoteNoOpinion),
39 m_addr_context(addr_context), m_address_ranges(),
40 m_stop_others(stop_others), m_stack_id(), m_parent_stack_id(),
41 m_no_more_plans(false), m_first_run_event(true), m_use_fast_step(false),
42 m_given_ranges_only(given_ranges_only) {
44 AddRange(range);
45 m_stack_id = thread.GetStackFrameAtIndex(0)->GetStackID();
46 StackFrameSP parent_stack = thread.GetStackFrameAtIndex(1);
47 if (parent_stack)
48 m_parent_stack_id = parent_stack->GetStackID();
49}
50
52
54 // See if we can find a "next range" breakpoint:
56}
57
60 if (error)
61 error->PutCString(
62 "Could not create hardware breakpoint for thread plan.");
63 return false;
64 }
65 return true;
66}
67
69 Log *log = GetLog(LLDBLog::Step);
70
71 const Vote vote = IsPlanComplete() ? eVoteYes : eVoteNo;
72 LLDB_LOGF(log, "ThreadPlanStepRange::ShouldReportStop() returning vote %i\n",
73 vote);
74 return vote;
75}
76
78 // For now I'm just adding the ranges. At some point we may want to condense
79 // the ranges if they overlap, though I don't think it is likely to be very
80 // important.
81 m_address_ranges.push_back(new_range);
82
83 // Fill the slot for this address range with an empty DisassemblerSP in the
84 // instruction ranges. I want the indices to match, but I don't want to do
85 // the work to disassemble this range if I don't step into it.
87}
88
90 size_t num_ranges = m_address_ranges.size();
91 if (num_ranges == 1) {
93 } else {
94 for (size_t i = 0; i < num_ranges; i++) {
95 s->Printf(" %" PRIu64 ": ", uint64_t(i));
97 }
98 }
99}
100
102 Log *log = GetLog(LLDBLog::Step);
103 bool ret_value = false;
104 Thread &thread = GetThread();
105 lldb::addr_t pc_load_addr = thread.GetRegisterContext()->GetPC();
106
107 size_t num_ranges = m_address_ranges.size();
108 for (size_t i = 0; i < num_ranges; i++) {
109 ret_value =
110 m_address_ranges[i].ContainsLoadAddress(pc_load_addr, &GetTarget());
111 if (ret_value)
112 break;
113 }
114
115 if (!ret_value && !m_given_ranges_only) {
116 // See if we've just stepped to another part of the same line number...
117 StackFrame *frame = thread.GetStackFrameAtIndex(0).get();
118
119 SymbolContext new_context(
120 frame->GetSymbolContext(eSymbolContextEverything));
122 new_context.line_entry.IsValid()) {
124 *new_context.line_entry.original_file_sp,
126 if (m_addr_context.line_entry.line == new_context.line_entry.line) {
127 m_addr_context = new_context;
128 const bool include_inlined_functions =
131 include_inlined_functions));
132 ret_value = true;
133 if (log) {
134 StreamString s;
138
139 LLDB_LOGF(
140 log,
141 "Step range plan stepped to another range of same line: %s",
142 s.GetData());
143 }
144 } else if (new_context.line_entry.line == 0) {
146 m_addr_context = new_context;
147 const bool include_inlined_functions =
150 include_inlined_functions));
151 ret_value = true;
152 if (log) {
153 StreamString s;
157
158 LLDB_LOGF(log,
159 "Step range plan stepped to a range at linenumber 0 "
160 "stepping through that range: %s",
161 s.GetData());
162 }
163 } else if (new_context.line_entry.range.GetBaseAddress().GetLoadAddress(
164 &GetTarget()) != pc_load_addr) {
165 // Another thing that sometimes happens here is that we step out of
166 // one line into the MIDDLE of another line. So far I mostly see
167 // this due to bugs in the debug information. But we probably don't
168 // want to be in the middle of a line range, so in that case reset
169 // the stepping range to the line we've stepped into the middle of
170 // and continue.
171 m_addr_context = new_context;
172 m_address_ranges.clear();
174 ret_value = true;
175 if (log) {
176 StreamString s;
180
181 LLDB_LOGF(log,
182 "Step range plan stepped to the middle of new "
183 "line(%d): %s, continuing to clear this line.",
184 new_context.line_entry.line, s.GetData());
185 }
186 }
187 }
188 }
189 }
190
191 if (!ret_value && log)
192 LLDB_LOGF(log, "Step range plan out of range to 0x%" PRIx64, pc_load_addr);
193
194 return ret_value;
195}
196
198 lldb::addr_t cur_pc = GetThread().GetRegisterContext()->GetPC();
199 if (m_addr_context.function != nullptr) {
201 cur_pc, &GetTarget());
205 return range.ContainsLoadAddress(cur_pc, &GetTarget());
206 }
207 return false;
208}
209
210// FIXME: This should also handle inlining if we aren't going to do inlining in
211// the
212// main stack.
213//
214// Ideally we should remember the whole stack frame list, and then compare that
215// to the current list.
216
218 FrameComparison frame_order;
219 Thread &thread = GetThread();
220 StackID cur_frame_id = thread.GetStackFrameAtIndex(0)->GetStackID();
221
222 if (cur_frame_id == m_stack_id) {
223 frame_order = eFrameCompareEqual;
224 } else if (cur_frame_id < m_stack_id) {
225 frame_order = eFrameCompareYounger;
226 } else {
227 StackFrameSP cur_parent_frame = thread.GetStackFrameAtIndex(1);
228 StackID cur_parent_id;
229 if (cur_parent_frame)
230 cur_parent_id = cur_parent_frame->GetStackID();
231 if (m_parent_stack_id.IsValid() && cur_parent_id.IsValid() &&
232 m_parent_stack_id == cur_parent_id)
233 frame_order = eFrameCompareSameParent;
234 else
235 frame_order = eFrameCompareOlder;
236 }
237 return frame_order;
238}
239
241 switch (m_stop_others) {
243 return true;
245 // If there is a call in the range of the next branch breakpoint,
246 // then we should always run all threads, since a call can execute
247 // arbitrary code which might for instance take a lock that's held
248 // by another thread.
249 return !m_found_calls;
251 return false;
252 }
253 llvm_unreachable("Unhandled run mode!");
254}
255
257 lldb::addr_t addr, size_t &range_index, size_t &insn_offset) {
258 size_t num_ranges = m_address_ranges.size();
259 for (size_t i = 0; i < num_ranges; i++) {
260 if (m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget())) {
261 // Some joker added a zero size range to the stepping range...
262 if (m_address_ranges[i].GetByteSize() == 0)
263 return nullptr;
264
265 if (!m_instruction_ranges[i]) {
266 // Disassemble the address range given:
267 const char *plugin_name = nullptr;
268 const char *flavor = nullptr;
270 GetTarget().GetArchitecture(), plugin_name, flavor, GetTarget(),
272 }
273 if (!m_instruction_ranges[i])
274 return nullptr;
275 else {
276 // Find where we are in the instruction list as well. If we aren't at
277 // an instruction, return nullptr. In this case, we're probably lost,
278 // and shouldn't try to do anything fancy.
279
280 insn_offset =
282 ->GetInstructionList()
283 .GetIndexOfInstructionAtLoadAddress(addr, GetTarget());
284 if (insn_offset == UINT32_MAX)
285 return nullptr;
286 else {
287 range_index = i;
288 return &m_instruction_ranges[i]->GetInstructionList();
289 }
290 }
291 }
292 }
293 return nullptr;
294}
295
298 return false;
299
300 break_id_t bp_site_id = stop_info_sp->GetValue();
301 BreakpointSiteSP bp_site_sp =
303 if (!bp_site_sp)
304 return false;
305 else if (!bp_site_sp->IsBreakpointAtThisSite(m_next_branch_bp_sp->GetID()))
306 return false;
307 return true;
308}
309
312 Log *log = GetLog(LLDBLog::Step);
313 LLDB_LOGF(log, "Removing next branch breakpoint: %d.",
314 m_next_branch_bp_sp->GetID());
316 m_next_branch_bp_sp.reset();
318 m_found_calls = false;
319 }
320}
321
325}
326
329 return true;
330
331 Log *log = GetLog(LLDBLog::Step);
332 // Stepping through ranges using breakpoints doesn't work yet, but with this
333 // off we fall back to instruction single stepping.
334 if (!m_use_fast_step)
335 return false;
336
337 // clear the m_found_calls, we'll rediscover it for this range.
338 m_found_calls = false;
339
340 lldb::addr_t cur_addr = GetThread().GetRegisterContext()->GetPC();
341 // Find the current address in our address ranges, and fetch the disassembly
342 // if we haven't already:
343 size_t pc_index;
344 size_t range_index;
345 InstructionList *instructions =
346 GetInstructionsForAddress(cur_addr, range_index, pc_index);
347 if (instructions == nullptr)
348 return false;
349 else {
350 const bool ignore_calls = GetKind() == eKindStepOverRange;
351 uint32_t branch_index = instructions->GetIndexOfNextBranchInstruction(
352 pc_index, ignore_calls, &m_found_calls);
353 Address run_to_address;
354
355 // If we didn't find a branch, run to the end of the range.
356 if (branch_index == UINT32_MAX) {
357 uint32_t last_index = instructions->GetSize() - 1;
358 if (last_index - pc_index > 1) {
359 InstructionSP last_inst =
360 instructions->GetInstructionAtIndex(last_index);
361 size_t last_inst_size = last_inst->GetOpcode().GetByteSize();
362 run_to_address = last_inst->GetAddress();
363 run_to_address.Slide(last_inst_size);
364 }
365 } else if (branch_index - pc_index > 1) {
366 run_to_address =
367 instructions->GetInstructionAtIndex(branch_index)->GetAddress();
368 }
369 if (branch_index == pc_index)
370 LLDB_LOGF(log, "ThreadPlanStepRange::SetNextBranchBreakpoint - skipping "
371 "because current is branch instruction");
372 if (run_to_address.IsValid()) {
373 const bool is_internal = true;
375 GetTarget().CreateBreakpoint(run_to_address, is_internal, false);
377
378 if (m_next_branch_bp_sp->IsHardware() &&
379 !m_next_branch_bp_sp->HasResolvedLocations())
381
382 if (log) {
384 BreakpointLocationSP bp_loc =
385 m_next_branch_bp_sp->GetLocationAtIndex(0);
386 if (bp_loc) {
387 BreakpointSiteSP bp_site = bp_loc->GetBreakpointSite();
388 if (bp_site) {
389 bp_site_id = bp_site->GetID();
390 }
391 }
392 LLDB_LOGF(log,
393 "ThreadPlanStepRange::SetNextBranchBreakpoint - Setting "
394 "breakpoint %d (site %d) to run to address 0x%" PRIx64,
395 m_next_branch_bp_sp->GetID(), bp_site_id,
396 run_to_address.GetLoadAddress(&m_process.GetTarget()));
397 }
398
399 m_next_branch_bp_sp->SetThreadID(m_tid);
400 m_next_branch_bp_sp->SetBreakpointKind("next-branch-location");
401
402 return true;
403 } else
404 return false;
405 } else
406 LLDB_LOGF(log, "ThreadPlanStepRange::SetNextBranchBreakpoint - skipping "
407 "invalid run_to_address");
408 }
409 return false;
410}
411
413 lldb::StopInfoSP stop_info_sp) {
414 if (!IsNextBranchBreakpointStop(stop_info_sp))
415 return false;
416
417 break_id_t bp_site_id = stop_info_sp->GetValue();
418 BreakpointSiteSP bp_site_sp =
420 if (!bp_site_sp)
421 return false;
422
423 // If we've hit the next branch breakpoint, then clear it.
424 size_t num_constituents = bp_site_sp->GetNumberOfConstituents();
425 bool explains_stop = true;
426 // If all the constituents are internal, then we are probably just stepping
427 // over this range from multiple threads, or multiple frames, so we want to
428 // continue. If one is not internal, then we should not explain the stop,
429 // and let the user breakpoint handle the stop.
430 for (size_t i = 0; i < num_constituents; i++) {
431 if (!bp_site_sp->GetConstituentAtIndex(i)->GetBreakpoint().IsInternal()) {
432 explains_stop = false;
433 break;
434 }
435 }
436 Log *log = GetLog(LLDBLog::Step);
437 LLDB_LOGF(log,
438 "ThreadPlanStepRange::NextRangeBreakpointExplainsStop - Hit "
439 "next range breakpoint which has %" PRIu64
440 " constituents - explains stop: %u.",
441 (uint64_t)num_constituents, explains_stop);
442 return explains_stop;
443}
444
445bool ThreadPlanStepRange::WillStop() { return true; }
446
449 return eStateRunning;
450 else
451 return eStateStepping;
452}
453
455 // If we have pushed some plans between ShouldStop & MischiefManaged, then
456 // we're not done...
457 // I do this check first because we might have stepped somewhere that will
458 // fool InRange into
459 // thinking it needs to step past the end of that line. This happens, for
460 // instance, when stepping over inlined code that is in the middle of the
461 // current line.
462
463 if (!m_no_more_plans)
464 return false;
465
466 bool done = true;
467 if (!IsPlanComplete()) {
468 if (InRange()) {
469 done = false;
470 } else {
472 done = (frame_order != eFrameCompareOlder) ? m_no_more_plans : true;
473 }
474 }
475
476 if (done) {
477 Log *log = GetLog(LLDBLog::Step);
478 LLDB_LOGF(log, "Completed step through range plan.");
481 return true;
482 } else {
483 return false;
484 }
485}
486
488 Log *log = GetLog(LLDBLog::Step);
490
491 if (frame_order == eFrameCompareOlder) {
492 if (log) {
493 LLDB_LOGF(log, "ThreadPlanStepRange::IsPlanStale returning true, we've "
494 "stepped out.");
495 }
496 return true;
497 } else if (frame_order == eFrameCompareEqual && InSymbol()) {
498 // If we are not in a place we should step through, we've gotten stale. One
499 // tricky bit here is that some stubs don't push a frame, so we should.
500 // check that we are in the same symbol.
501 if (!InRange()) {
502 // Set plan Complete when we reach next instruction just after the range
503 lldb::addr_t addr = GetThread().GetRegisterContext()->GetPC() - 1;
504 size_t num_ranges = m_address_ranges.size();
505 for (size_t i = 0; i < num_ranges; i++) {
506 bool in_range =
507 m_address_ranges[i].ContainsLoadAddress(addr, &GetTarget());
508 if (in_range) {
510 }
511 }
512 return true;
513 }
514 }
515 return false;
516}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:366
A section + offset based address range class.
Definition: AddressRange.h:25
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:211
bool ContainsLoadAddress(const Address &so_addr, Target *target) const
Check if a section offset so_addr when represented as a load address is contained within this object'...
A section + offset based address class.
Definition: Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:313
@ DumpStyleLoadAddress
Display as the load address (if resolved).
Definition: Address.h:99
bool Slide(int64_t offset)
Definition: Address.h:459
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:355
static lldb::DisassemblerSP DisassembleRange(const ArchSpec &arch, const char *plugin_name, const char *flavor, Target &target, const AddressRange &disasm_range, bool force_live_memory=false)
const AddressRange & GetAddressRange()
Definition: Function.h:447
lldb::InstructionSP GetInstructionAtIndex(size_t idx) const
uint32_t GetIndexOfNextBranchInstruction(uint32_t start, bool ignore_calls, bool *found_calls) const
Get the index of the next branch instruction.
StopPointSiteList< lldb_private::BreakpointSite > & GetBreakpointSiteList()
Definition: Process.cpp:1610
Target & GetTarget()
Get the target object pointer for this module.
Definition: Process.h:1285
This base class provides an interface to stack frames.
Definition: StackFrame.h:43
const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
Definition: StackFrame.cpp:300
bool IsValid() const
Definition: StackID.h:47
StopPointSiteSP FindByID(typename StopPointSite::SiteID site_id)
Returns a shared pointer to the site with id site_id.
const char * GetData() const
Definition: StreamString.h:45
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:34
Function * function
The Function for a given query.
Symbol * symbol
The Symbol for a given query.
LineEntry line_entry
The LineEntry for a given query.
bool ValueIsAddress() const
Definition: Symbol.cpp:165
Address & GetAddressRef()
Definition: Symbol.h:72
lldb::addr_t GetByteSize() const
Definition: Symbol.cpp:468
bool RemoveBreakpointByID(lldb::break_id_t break_id)
Definition: Target.cpp:1005
lldb::BreakpointSP CreateBreakpoint(const FileSpecList *containingModules, const FileSpec &file, uint32_t line_no, uint32_t column, lldb::addr_t offset, LazyBool check_inlines, LazyBool skip_prologue, bool internal, bool request_hardware, LazyBool move_to_nearest_code)
Definition: Target.cpp:396
bool NextRangeBreakpointExplainsStop(lldb::StopInfoSP stop_info_sp)
Vote ShouldReportStop(Event *event_ptr) override
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
lldb::FrameComparison CompareCurrentFrameToStartFrame()
bool IsNextBranchBreakpointStop(lldb::StopInfoSP stop_info_sp)
ThreadPlanStepRange(ThreadPlanKind kind, const char *name, Thread &thread, const AddressRange &range, const SymbolContext &addr_context, lldb::RunMode stop_others, bool given_ranges_only=false)
InstructionList * GetInstructionsForAddress(lldb::addr_t addr, size_t &range_index, size_t &insn_offset)
std::vector< lldb::DisassemblerSP > m_instruction_ranges
lldb::StateType GetPlanRunState() override
std::vector< AddressRange > m_address_ranges
void AddRange(const AddressRange &new_range)
ThreadPlanKind GetKind() const
Definition: ThreadPlan.h:431
void SetPlanComplete(bool success=true)
Definition: ThreadPlan.cpp:66
Thread & GetThread()
Returns the Thread that is using this thread plan.
Definition: ThreadPlan.cpp:42
virtual bool MischiefManaged()
Definition: ThreadPlan.cpp:72
lldb::StopInfoSP GetPrivateStopInfo()
Definition: ThreadPlan.h:525
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition: Thread.h:408
virtual lldb::RegisterContextSP GetRegisterContext()=0
#define LLDB_INVALID_BREAK_ID
Definition: lldb-defines.h:37
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:331
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:420
std::shared_ptr< lldb_private::BreakpointSite > BreakpointSiteSP
Definition: lldb-forward.h:321
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
Definition: lldb-forward.h:322
FrameComparison
This is the return value for frame comparisons.
@ eFrameCompareSameParent
@ eFrameCompareEqual
@ eFrameCompareOlder
@ eFrameCompareYounger
StateType
Process and Thread States.
@ eStateRunning
Process or thread is running and can't be examined.
@ eStateStepping
Process or thread is in the process of stepping and can not be examined.
std::shared_ptr< lldb_private::Instruction > InstructionSP
Definition: lldb-forward.h:356
int32_t break_id_t
Definition: lldb-types.h:86
std::shared_ptr< lldb_private::Disassembler > DisassemblerSP
Definition: lldb-forward.h:339
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
Definition: lldb-forward.h:427
uint64_t addr_t
Definition: lldb-types.h:80
RunMode
Thread Run Modes.
@ eOnlyDuringStepping
bool Dump(Stream *s, Target *target, bool show_file, Address::DumpStyle style, Address::DumpStyle fallback_style, bool show_range) const
Dump a description of this object to a Stream.
Definition: LineEntry.cpp:60
lldb::SupportFileSP original_file_sp
The original source file, from debug info.
Definition: LineEntry.h:143
AddressRange GetSameLineContiguousAddressRange(bool include_inlined_functions) const
Give the range for this LineEntry + any additional LineEntries for this same source line that are con...
Definition: LineEntry.cpp:182
bool IsValid() const
Check if a line entry object is valid.
Definition: LineEntry.cpp:35
AddressRange range
The section offset address range for this line entry.
Definition: LineEntry.h:137
uint32_t line
The source line number, or LLDB_INVALID_LINE_NUMBER if there is no line number information.
Definition: LineEntry.h:147