LLDB mainline
ThreadPlanStepInRange.cpp
Go to the documentation of this file.
1//===-- ThreadPlanStepInRange.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
11#include "lldb/Core/Module.h"
13#include "lldb/Symbol/Symbol.h"
14#include "lldb/Target/Process.h"
17#include "lldb/Target/Target.h"
18#include "lldb/Target/Thread.h"
22#include "lldb/Utility/Log.h"
24#include "lldb/Utility/Stream.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
31
32// ThreadPlanStepInRange: Step through a stack range, either stepping over or
33// into based on the value of \a type.
34
36 Thread &thread, const AddressRange &range,
37 const SymbolContext &addr_context, const char *step_into_target,
38 lldb::RunMode stop_others, LazyBool step_in_avoids_code_without_debug_info,
39 LazyBool step_out_avoids_code_without_debug_info)
40 : ThreadPlanStepRange(ThreadPlan::eKindStepInRange,
41 "Step Range stepping in", thread, range, addr_context,
42 stop_others),
43 ThreadPlanShouldStopHere(this), m_step_past_prologue(true),
44 m_virtual_step(false), m_step_into_target(step_into_target) {
47 SetupAvoidNoDebug(step_in_avoids_code_without_debug_info,
48 step_out_avoids_code_without_debug_info);
49}
50
52
54 LazyBool step_in_avoids_code_without_debug_info,
55 LazyBool step_out_avoids_code_without_debug_info) {
56 bool avoid_nodebug = true;
57 Thread &thread = GetThread();
58 switch (step_in_avoids_code_without_debug_info) {
59 case eLazyBoolYes:
60 avoid_nodebug = true;
61 break;
62 case eLazyBoolNo:
63 avoid_nodebug = false;
64 break;
66 avoid_nodebug = thread.GetStepInAvoidsNoDebug();
67 break;
68 }
69 if (avoid_nodebug)
71 else
73
74 switch (step_out_avoids_code_without_debug_info) {
75 case eLazyBoolYes:
76 avoid_nodebug = true;
77 break;
78 case eLazyBoolNo:
79 avoid_nodebug = false;
80 break;
82 avoid_nodebug = thread.GetStepOutAvoidsNoDebug();
83 break;
84 }
85 if (avoid_nodebug)
87 else
89}
90
93
94 auto PrintFailureIfAny = [&]() {
95 if (m_status.Success())
96 return;
97 s->Printf(" failed (%s)", m_status.AsCString());
98 };
99
100 if (level == lldb::eDescriptionLevelBrief) {
101 s->Printf("step in");
102 PrintFailureIfAny();
103 return;
104 }
105
106 s->Printf("Stepping in");
107 bool printed_line_info = false;
109 s->Printf(" through line ");
111 printed_line_info = true;
112 }
113
114 const char *step_into_target = m_step_into_target.AsCString();
115 if (step_into_target && step_into_target[0] != '\0')
116 s->Printf(" targeting %s", m_step_into_target.AsCString());
117
118 if (!printed_line_info || level == eDescriptionLevelVerbose) {
119 s->Printf(" using ranges:");
120 DumpRanges(s);
121 }
122
123 PrintFailureIfAny();
124
125 s->PutChar('.');
126}
127
129 Log *log = GetLog(LLDBLog::Step);
130
131 if (log) {
132 StreamString s;
133 DumpAddress(s.AsRawOstream(), GetThread().GetRegisterContext()->GetPC(),
134 GetTarget().GetArchitecture().GetAddressByteSize());
135 LLDB_LOGF(log, "ThreadPlanStepInRange reached %s.", s.GetData());
136 }
138
139 if (IsPlanComplete())
140 return true;
141
142 m_no_more_plans = false;
143 if (m_sub_plan_sp && m_sub_plan_sp->IsPlanComplete()) {
144 if (!m_sub_plan_sp->PlanSucceeded()) {
146 m_no_more_plans = true;
147 return true;
148 } else
149 m_sub_plan_sp.reset();
150 }
151
152 if (m_virtual_step) {
153 // If we've just completed a virtual step, all we need to do is check for a
154 // ShouldStopHere plan, and otherwise we're done.
155 // FIXME - This can be both a step in and a step out. Probably should
156 // record which in the m_virtual_step.
159 } else {
160 // Stepping through should be done running other threads in general, since
161 // we're setting a breakpoint and continuing. So only stop others if we
162 // are explicitly told to do so.
163
164 bool stop_others = (m_stop_others == lldb::eOnlyThisThread);
165
167
168 Thread &thread = GetThread();
169 if (frame_order == eFrameCompareOlder ||
170 frame_order == eFrameCompareSameParent) {
171 // If we're in an older frame then we should stop.
172 //
173 // A caveat to this is if we think the frame is older but we're actually
174 // in a trampoline.
175 // I'm going to make the assumption that you wouldn't RETURN to a
176 // trampoline. So if we are in a trampoline we think the frame is older
177 // because the trampoline confused the backtracer.
179 m_stack_id, false, stop_others, m_status);
180 if (!m_sub_plan_sp) {
181 // Otherwise check the ShouldStopHere for step out:
184 if (log) {
185 if (m_sub_plan_sp)
186 LLDB_LOGF(log,
187 "ShouldStopHere found plan to step out of this frame.");
188 else
189 LLDB_LOGF(log, "ShouldStopHere no plan to step out of this frame.");
190 }
191 } else if (log) {
192 LLDB_LOGF(
193 log, "Thought I stepped out, but in fact arrived at a trampoline.");
194 }
195 } else if (frame_order == eFrameCompareEqual && InSymbol()) {
196 // If we are not in a place we should step through, we're done. One
197 // tricky bit here is that some stubs don't push a frame, so we have to
198 // check both the case of a frame that is younger, or the same as this
199 // frame. However, if the frame is the same, and we are still in the
200 // symbol we started in, the we don't need to do this. This first check
201 // isn't strictly necessary, but it is more efficient.
202
203 // If we're still in the range, keep going, either by running to the next
204 // branch breakpoint, or by stepping.
205 if (InRange()) {
207 return false;
208 }
209
211 m_no_more_plans = true;
212 return true;
213 }
214
215 // If we get to this point, we're not going to use a previously set "next
216 // branch" breakpoint, so delete it:
218
219 // We may have set the plan up above in the FrameIsOlder section:
220
221 if (!m_sub_plan_sp)
223 m_stack_id, false, stop_others, m_status);
224
225 if (log) {
226 if (m_sub_plan_sp)
227 LLDB_LOGF(log, "Found a step through plan: %s",
228 m_sub_plan_sp->GetName());
229 else
230 LLDB_LOGF(log, "No step through plan found.");
231 }
232
233 // If not, give the "should_stop" callback a chance to push a plan to get
234 // us out of here. But only do that if we actually have stepped in.
235 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
237
238 // If we've stepped in and we are going to stop here, check to see if we
239 // were asked to run past the prologue, and if so do that.
240
241 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger &&
243 lldb::StackFrameSP curr_frame = thread.GetStackFrameAtIndex(0);
244 if (curr_frame) {
245 size_t bytes_to_skip = 0;
246 lldb::addr_t curr_addr = thread.GetRegisterContext()->GetPC();
247 Address func_start_address;
248
249 SymbolContext sc = curr_frame->GetSymbolContext(eSymbolContextFunction |
250 eSymbolContextSymbol);
251
252 if (sc.function) {
253 func_start_address = sc.function->GetAddressRange().GetBaseAddress();
254 if (curr_addr == func_start_address.GetLoadAddress(&GetTarget()))
255 bytes_to_skip = sc.function->GetPrologueByteSize();
256 } else if (sc.symbol) {
257 func_start_address = sc.symbol->GetAddress();
258 if (curr_addr == func_start_address.GetLoadAddress(&GetTarget()))
259 bytes_to_skip = sc.symbol->GetPrologueByteSize();
260 }
261
262 if (bytes_to_skip == 0 && sc.symbol) {
264 if (arch) {
265 Address curr_sec_addr;
267 curr_sec_addr);
268 bytes_to_skip = arch->GetBytesToSkip(*sc.symbol, curr_sec_addr);
269 }
270 }
271
272 if (bytes_to_skip != 0) {
273 func_start_address.Slide(bytes_to_skip);
274 log = GetLog(LLDBLog::Step);
275 LLDB_LOGF(log, "Pushing past prologue ");
276
278 false, func_start_address, true, m_status);
279 }
280 }
281 }
282 }
283
284 if (!m_sub_plan_sp) {
285 m_no_more_plans = true;
287 return true;
288 } else {
289 m_no_more_plans = false;
290 m_sub_plan_sp->SetPrivate(true);
291 return false;
292 }
293}
294
298 else
299 m_avoid_regexp_up = std::make_unique<RegularExpression>(name);
300}
301
303 // TODO: Should we test this for sanity?
305}
306
308 StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
309
310 // Check the library list first, as that's cheapest:
311 bool libraries_say_avoid = false;
312
313 FileSpecList libraries_to_avoid(GetThread().GetLibrariesToAvoid());
314 size_t num_libraries = libraries_to_avoid.GetSize();
315 if (num_libraries > 0) {
316 SymbolContext sc(frame->GetSymbolContext(eSymbolContextModule));
317 FileSpec frame_library(sc.module_sp->GetFileSpec());
318
319 if (frame_library) {
320 for (size_t i = 0; i < num_libraries; i++) {
321 const FileSpec &file_spec(libraries_to_avoid.GetFileSpecAtIndex(i));
322 if (FileSpec::Match(file_spec, frame_library)) {
323 libraries_say_avoid = true;
324 break;
325 }
326 }
327 }
328 }
329 if (libraries_say_avoid)
330 return true;
331
332 const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_up.get();
333 if (avoid_regexp_to_use == nullptr)
334 avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
335
336 if (avoid_regexp_to_use != nullptr) {
338 eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
339 if (sc.symbol != nullptr) {
340 const char *frame_function_name =
342 .GetCString();
343 if (frame_function_name) {
344 bool return_value = avoid_regexp_to_use->Execute(frame_function_name);
345 if (return_value) {
347 "Stepping out of function \"%s\" because it matches the "
348 "avoid regexp \"%s\".",
349 frame_function_name,
350 avoid_regexp_to_use->GetText().str().c_str());
351 }
352 return return_value;
353 }
354 }
355 }
356 return false;
357}
358
360 ThreadPlan *current_plan, Flags &flags, FrameComparison operation,
361 Status &status, void *baton) {
362 bool should_stop_here = true;
363 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
364 Log *log = GetLog(LLDBLog::Step);
365
366 // First see if the ThreadPlanShouldStopHere default implementation thinks we
367 // should get out of here:
369 current_plan, flags, operation, status, baton);
370 if (!should_stop_here)
371 return false;
372
373 if (should_stop_here && current_plan->GetKind() == eKindStepInRange &&
374 operation == eFrameCompareYounger) {
375 ThreadPlanStepInRange *step_in_range_plan =
376 static_cast<ThreadPlanStepInRange *>(current_plan);
377 if (step_in_range_plan->m_step_into_target) {
379 eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
380 if (sc.symbol != nullptr) {
381 // First try an exact match, since that's cheap with ConstStrings.
382 // Then do a strstr compare.
383 if (step_in_range_plan->m_step_into_target == sc.GetFunctionName()) {
384 should_stop_here = true;
385 } else {
386 const char *target_name =
387 step_in_range_plan->m_step_into_target.AsCString();
388 const char *function_name = sc.GetFunctionName().AsCString();
389
390 if (function_name == nullptr)
391 should_stop_here = false;
392 else if (strstr(function_name, target_name) == nullptr)
393 should_stop_here = false;
394 }
395 if (log && !should_stop_here)
396 LLDB_LOGF(log,
397 "Stepping out of frame %s which did not match step into "
398 "target %s.",
400 step_in_range_plan->m_step_into_target.AsCString());
401 }
402 }
403
404 if (should_stop_here) {
405 ThreadPlanStepInRange *step_in_range_plan =
406 static_cast<ThreadPlanStepInRange *>(current_plan);
407 // Don't log the should_step_out here, it's easier to do it in
408 // FrameMatchesAvoidCriteria.
409 should_stop_here = !step_in_range_plan->FrameMatchesAvoidCriteria();
410 }
411 }
412
413 return should_stop_here;
414}
415
417 // We always explain a stop. Either we've just done a single step, in which
418 // case we'll do our ordinary processing, or we stopped for some reason that
419 // isn't handled by our sub-plans, in which case we want to just stop right
420 // away. In general, we don't want to mark the plan as complete for
421 // unexplained stops. For instance, if you step in to some code with no debug
422 // info, so you step out and in the course of that hit a breakpoint, then you
423 // want to stop & show the user the breakpoint, but not unship the step in
424 // plan, since you still may want to complete that plan when you continue.
425 // This is particularly true when doing "step in to target function."
426 // stepping.
427 //
428 // The only variation is that if we are doing "step by running to next
429 // branch" in which case if we hit our branch breakpoint we don't set the
430 // plan to complete.
431
432 bool return_value = false;
433
434 if (m_virtual_step) {
435 return_value = true;
436 } else {
437 StopInfoSP stop_info_sp = GetPrivateStopInfo();
438 if (stop_info_sp) {
439 StopReason reason = stop_info_sp->GetStopReason();
440
441 if (reason == eStopReasonBreakpoint) {
442 if (NextRangeBreakpointExplainsStop(stop_info_sp)) {
443 return_value = true;
444 }
445 } else if (IsUsuallyUnexplainedStopReason(reason)) {
446 Log *log = GetLog(LLDBLog::Step);
447 if (log)
448 log->PutCString("ThreadPlanStepInRange got asked if it explains the "
449 "stop for some reason other than step.");
450 return_value = false;
451 } else {
452 return_value = true;
453 }
454 } else
455 return_value = true;
456 }
457
458 return return_value;
459}
460
462 bool current_plan) {
463 m_virtual_step = false;
464 if (resume_state == eStateStepping && current_plan) {
465 Thread &thread = GetThread();
466 // See if we are about to step over a virtual inlined call.
467 bool step_without_resume = thread.DecrementCurrentInlinedDepth();
468 if (step_without_resume) {
469 Log *log = GetLog(LLDBLog::Step);
470 LLDB_LOGF(log,
471 "ThreadPlanStepInRange::DoWillResume: returning false, "
472 "inline_depth: %d",
473 thread.GetCurrentInlinedDepth());
475
476 // FIXME: Maybe it would be better to create a InlineStep stop reason, but
477 // then
478 // the whole rest of the world would have to handle that stop reason.
479 m_virtual_step = true;
480 }
481 return !step_without_resume;
482 }
483 return true;
484}
485
#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
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
bool Slide(int64_t offset)
Definition: Address.h:459
virtual size_t GetBytesToSkip(Symbol &func, const Address &curr_addr) const
This method is used to get the number of bytes that should be skipped, from function start address,...
Definition: Architecture.h:47
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:188
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
A file collection class.
Definition: FileSpecList.h:91
const FileSpec & GetFileSpecAtIndex(size_t idx) const
Get file at index.
size_t GetSize() const
Get the number of files in the file list.
A file utility class.
Definition: FileSpec.h:56
static bool Match(const FileSpec &pattern, const FileSpec &file)
Match FileSpec pattern against FileSpec file.
Definition: FileSpec.cpp:301
A class to manage flags.
Definition: Flags.h:22
ValueType Clear(ValueType mask=~static_cast< ValueType >(0))
Clear one or more flags.
Definition: Flags.h:61
ValueType Set(ValueType mask)
Set one or more flags by logical OR'ing mask with the current flags.
Definition: Flags.h:73
const AddressRange & GetAddressRange()
Definition: Function.h:447
uint32_t GetPrologueByteSize()
Get the size of the prologue instructions for this function.
Definition: Function.cpp:567
void PutCString(const char *cstr)
Definition: Log.cpp:135
@ ePreferDemangledWithoutArguments
Definition: Mangled.h:38
bool Execute(llvm::StringRef string, llvm::SmallVectorImpl< llvm::StringRef > *matches=nullptr) const
Execute a regular expression match using the compiled regular expression that is already in this obje...
llvm::StringRef GetText() const
Access the regular expression text.
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, bool allow_section_end=false) const
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
An error handling class.
Definition: Status.h:44
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:129
bool Success() const
Test for success condition.
Definition: Status.cpp:278
static lldb::StopInfoSP CreateStopReasonToTrace(Thread &thread)
Definition: StopInfo.cpp:1421
const char * GetData() const
Definition: StreamString.h:45
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition: Stream.h:401
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutChar(char ch)
Definition: Stream.cpp:131
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.
ConstString GetFunctionName(Mangled::NamePreference preference=Mangled::ePreferDemangled) const
Find a name of the innermost function for the symbol context.
lldb::ModuleSP module_sp
The Module for a given query.
Symbol * symbol
The Symbol for a given query.
LineEntry line_entry
The LineEntry for a given query.
Address GetAddress() const
Definition: Symbol.h:88
uint32_t GetPrologueByteSize()
Definition: Symbol.cpp:313
SectionLoadList & GetSectionLoadList()
Definition: Target.h:1143
Architecture * GetArchitecturePlugin() const
Definition: Target.h:1067
static bool DefaultShouldStopHereCallback(ThreadPlan *current_plan, Flags &flags, lldb::FrameComparison operation, Status &status, void *baton)
lldb::ThreadPlanSP CheckShouldStopHereAndQueueStepOut(lldb::FrameComparison operation, Status &status)
static void SetDefaultFlagValue(uint32_t new_value)
ThreadPlanStepInRange(Thread &thread, const AddressRange &range, const SymbolContext &addr_context, const char *step_into_target, lldb::RunMode stop_others, LazyBool step_in_avoids_code_without_debug_info, LazyBool step_out_avoids_code_without_debug_info)
bool DoPlanExplainsStop(Event *event_ptr) override
bool DoWillResume(lldb::StateType resume_state, bool current_plan) override
void SetupAvoidNoDebug(LazyBool step_in_avoids_code_without_debug_info, LazyBool step_out_avoids_code_without_debug_info)
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
bool ShouldStop(Event *event_ptr) override
std::unique_ptr< RegularExpression > m_avoid_regexp_up
static bool DefaultShouldStopHereCallback(ThreadPlan *current_plan, Flags &flags, lldb::FrameComparison operation, Status &status, void *baton)
bool NextRangeBreakpointExplainsStop(lldb::StopInfoSP stop_info_sp)
lldb::FrameComparison CompareCurrentFrameToStartFrame()
void SetStopInfo(lldb::StopInfoSP stop_reason_sp)
Definition: ThreadPlan.h:529
bool IsUsuallyUnexplainedStopReason(lldb::StopReason)
Definition: ThreadPlan.cpp:166
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
lldb::StopInfoSP GetPrivateStopInfo()
Definition: ThreadPlan.h:525
bool GetStepInAvoidsNoDebug() const
Definition: Thread.cpp:128
const RegularExpression * GetSymbolsToAvoidRegexp()
The regular expression returned determines symbols that this thread won't stop in during "step-in" op...
Definition: Thread.cpp:112
bool GetStepOutAvoidsNoDebug() const
Definition: Thread.cpp:134
bool DecrementCurrentInlinedDepth()
Definition: Thread.h:415
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition: Thread.h:408
virtual lldb::RegisterContextSP GetRegisterContext()=0
virtual lldb::ThreadPlanSP QueueThreadPlanForRunToAddress(bool abort_other_plans, Address &target_addr, bool stop_other_threads, Status &status)
Gets the plan used to continue from the current PC.
Definition: Thread.cpp:1366
virtual lldb::ThreadPlanSP QueueThreadPlanForStepThrough(StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads, Status &status)
Gets the plan used to step through the code that steps from a function call site at the current PC in...
Definition: Thread.cpp:1353
uint32_t GetCurrentInlinedDepth()
Definition: Thread.h:419
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
void DumpAddress(llvm::raw_ostream &s, uint64_t addr, uint32_t addr_size, const char *prefix=nullptr, const char *suffix=nullptr)
Output an address value to this stream.
Definition: Stream.cpp:108
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:420
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eDescriptionLevelBrief
@ eDescriptionLevelVerbose
FrameComparison
This is the return value for frame comparisons.
@ eFrameCompareSameParent
@ eFrameCompareEqual
@ eFrameCompareOlder
@ eFrameCompareYounger
StateType
Process and Thread States.
@ eStateStepping
Process or thread is in the process of stepping and can not be examined.
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
Definition: lldb-forward.h:427
uint64_t addr_t
Definition: lldb-types.h:80
StopReason
Thread stop reasons.
@ eStopReasonBreakpoint
RunMode
Thread Run Modes.
bool IsValid() const
Check if a line entry object is valid.
Definition: LineEntry.cpp:35
bool DumpStopContext(Stream *s, bool show_fullpaths) const
Dumps information specific to a process that stops at this line entry to the supplied stream s.
Definition: LineEntry.cpp:39