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
33
34// ThreadPlanStepInRange: Step through a stack range, either stepping over or
35// into based on the value of \a type.
36
38 Thread &thread, const AddressRange &range,
39 const SymbolContext &addr_context, std::string step_into_target,
40 lldb::RunMode stop_others, LazyBool step_in_avoids_code_without_debug_info,
41 LazyBool step_out_avoids_code_without_debug_info)
43 "Step Range stepping in", thread, range, addr_context,
44 stop_others),
47 m_step_into_target(std::move(step_into_target)) {
50 SetupAvoidNoDebug(step_in_avoids_code_without_debug_info,
51 step_out_avoids_code_without_debug_info);
52}
53
55
57 LazyBool step_in_avoids_code_without_debug_info,
58 LazyBool step_out_avoids_code_without_debug_info) {
59 bool avoid_nodebug = true;
60 Thread &thread = GetThread();
61 switch (step_in_avoids_code_without_debug_info) {
62 case eLazyBoolYes:
63 avoid_nodebug = true;
64 break;
65 case eLazyBoolNo:
66 avoid_nodebug = false;
67 break;
69 avoid_nodebug = thread.GetStepInAvoidsNoDebug();
70 break;
71 }
72 if (avoid_nodebug)
74 else
76
77 switch (step_out_avoids_code_without_debug_info) {
78 case eLazyBoolYes:
79 avoid_nodebug = true;
80 break;
81 case eLazyBoolNo:
82 avoid_nodebug = false;
83 break;
85 avoid_nodebug = thread.GetStepOutAvoidsNoDebug();
86 break;
87 }
88 if (avoid_nodebug)
90 else
92}
93
96
97 auto PrintFailureIfAny = [&]() {
98 if (m_status.Success())
99 return;
100 s->Printf(" failed (%s)", m_status.AsCString());
101 };
102
103 if (level == lldb::eDescriptionLevelBrief) {
104 s->PutCString("step in");
105 PrintFailureIfAny();
106 return;
107 }
108
109 s->PutCString("Stepping in");
110 bool printed_line_info = false;
111 if (m_addr_context.line_entry.IsValid()) {
112 s->PutCString(" through line ");
113 m_addr_context.line_entry.DumpStopContext(s, false);
114 printed_line_info = true;
115 }
116
117 if (!m_step_into_target.empty())
118 s->Format(" targeting {0}", m_step_into_target);
119
120 if (!printed_line_info || level == eDescriptionLevelVerbose) {
121 s->PutCString(" using ranges:");
122 DumpRanges(s);
123 }
124
125 PrintFailureIfAny();
126
127 s->PutChar('.');
128}
129
131 Log *log = GetLog(LLDBLog::Step);
132
133 if (log) {
134 StreamString s;
135 DumpAddress(s.AsRawOstream(), GetThread().GetRegisterContext()->GetPC(),
136 GetTarget().GetArchitecture().GetAddressByteSize());
137 LLDB_LOGF(log, "ThreadPlanStepInRange reached %s.", s.GetData());
138 }
140
141 if (IsPlanComplete())
142 return true;
143
144 m_no_more_plans = false;
145 if (m_sub_plan_sp && m_sub_plan_sp->IsPlanComplete()) {
146 if (!m_sub_plan_sp->PlanSucceeded()) {
148 m_no_more_plans = true;
149 return true;
150 } else
151 m_sub_plan_sp.reset();
152 }
153
155 // If we've just completed a virtual step, all we need to do is check for a
156 // ShouldStopHere plan, and otherwise we're done.
157 // FIXME - This can be both a step in and a step out. Probably should
158 // record which in the m_virtual_step.
161 } else {
162 // Stepping through should be done running other threads in general, since
163 // we're setting a breakpoint and continuing. So only stop others if we
164 // are explicitly told to do so.
165
166 bool stop_others = (m_stop_others == lldb::eOnlyThisThread);
167
169
170 Thread &thread = GetThread();
171 if (frame_order == eFrameCompareOlder ||
172 frame_order == eFrameCompareSameParent) {
173 // If we're in an older frame then we should stop.
174 //
175 // A caveat to this is if we think the frame is older but we're actually
176 // in a trampoline.
177 // I'm going to make the assumption that you wouldn't RETURN to a
178 // trampoline. So if we are in a trampoline we think the frame is older
179 // because the trampoline confused the backtracer.
180 m_sub_plan_sp = thread.QueueThreadPlanForStepThrough(
181 m_stack_id, false, stop_others, m_status);
182 if (!m_sub_plan_sp) {
183 // Otherwise check the ShouldStopHere for step out:
186 if (m_sub_plan_sp)
187 LLDB_LOGF(log,
188 "ShouldStopHere found plan to step out of this frame.");
189 else
190 LLDB_LOGF(log, "ShouldStopHere no plan to step out of this frame.");
191 } else {
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)
222 m_sub_plan_sp = thread.QueueThreadPlanForStepThrough(
223 m_stack_id, false, stop_others, m_status);
224
225 if (m_sub_plan_sp)
226 LLDB_LOGF(log, "Found a step through plan: %s", m_sub_plan_sp->GetName());
227 else
228 LLDB_LOGF(log, "No step through plan found.");
229
230 // If not, give the "should_stop" callback a chance to push a plan to get
231 // us out of here. But only do that if we actually have stepped in.
232 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
234
235 // If we've stepped in and we are going to stop here, check to see if we
236 // were asked to run past the prologue, and if so do that.
237
238 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger &&
240 lldb::StackFrameSP curr_frame = thread.GetStackFrameAtIndex(0);
241 if (curr_frame) {
242 size_t bytes_to_skip = 0;
243 lldb::addr_t curr_addr = thread.GetRegisterContext()->GetPC();
244 Address func_start_address;
245
246 SymbolContext sc = curr_frame->GetSymbolContext(eSymbolContextFunction |
247 eSymbolContextSymbol);
248
249 if (sc.function) {
250 func_start_address = sc.function->GetAddress();
251 bytes_to_skip = sc.function->GetPrologueByteSize();
252 } else if (sc.symbol) {
253 func_start_address = sc.symbol->GetAddress();
254 bytes_to_skip = sc.symbol->GetPrologueByteSize();
255 }
256
257 // A function's entry point need not be its first address, so a pc past
258 // the entry point can still be inside the prologue. What says the
259 // prologue has yet to run is the pc being below its end.
260 if (bytes_to_skip != 0) {
261 const lldb::addr_t prologue_end =
262 func_start_address.GetLoadAddress(&GetTarget()) + bytes_to_skip;
263 if (curr_addr >= prologue_end)
264 bytes_to_skip = 0;
265 }
266
267 if (bytes_to_skip == 0 && sc.symbol) {
269 if (arch) {
270 Address curr_sec_addr;
271 GetTarget().ResolveLoadAddress(curr_addr, curr_sec_addr);
272 bytes_to_skip = arch->GetBytesToSkip(*sc.symbol, curr_sec_addr);
273 }
274 }
275
276 if (bytes_to_skip != 0) {
277 func_start_address.Slide(bytes_to_skip);
278 log = GetLog(LLDBLog::Step);
279 LLDB_LOGF(log, "Pushing past prologue ");
280
281 m_sub_plan_sp = thread.QueueThreadPlanForRunToAddress(
282 false, func_start_address, true, m_status);
283 }
284 }
285 }
286 }
287
288 if (!m_sub_plan_sp) {
289 m_no_more_plans = true;
291 return true;
292 } else {
293 m_no_more_plans = false;
294 m_sub_plan_sp->SetPrivate(true);
295 return false;
296 }
297}
298
302 else
303 m_avoid_regexp_up = std::make_unique<RegularExpression>(name);
304}
305
307 // TODO: Should we test this for sanity?
309}
310
312 StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
313
314 // Check the library list first, as that's cheapest:
315 bool libraries_say_avoid = false;
316
317 FileSpecList libraries_to_avoid(GetThread().GetLibrariesToAvoid());
318 size_t num_libraries = libraries_to_avoid.GetSize();
319 if (num_libraries > 0) {
320 SymbolContext sc(frame->GetSymbolContext(eSymbolContextModule));
321 FileSpec frame_library(sc.module_sp->GetFileSpec());
322
323 if (frame_library) {
324 for (size_t i = 0; i < num_libraries; i++) {
325 const FileSpec &file_spec(libraries_to_avoid.GetFileSpecAtIndex(i));
326 if (FileSpec::Match(file_spec, frame_library)) {
327 libraries_say_avoid = true;
328 break;
329 }
330 }
331 }
332 }
333 if (libraries_say_avoid)
334 return true;
335
336 const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_up.get();
337 if (avoid_regexp_to_use == nullptr)
338 avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
339
340 if (avoid_regexp_to_use != nullptr) {
342 eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
343 if (sc.symbol != nullptr) {
344 const char *frame_function_name =
346 .GetCString();
347 if (frame_function_name) {
348 bool return_value = avoid_regexp_to_use->Execute(frame_function_name);
349 if (return_value) {
351 "Stepping out of function \"%s\" because it matches the "
352 "avoid regexp \"%s\".",
353 frame_function_name,
354 avoid_regexp_to_use->GetText().str().c_str());
355 }
356 return return_value;
357 }
358 }
359 }
360 return false;
361}
362
364 ThreadPlan *current_plan, Flags &flags, FrameComparison operation,
365 Status &status, void *baton) {
366 bool should_stop_here = true;
367 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
368 Log *log = GetLog(LLDBLog::Step);
369
370 // First see if the ThreadPlanShouldStopHere default implementation thinks we
371 // should get out of here:
373 current_plan, flags, operation, status, baton);
374 if (!should_stop_here)
375 return false;
376
377 if (current_plan->GetKind() == eKindStepInRange &&
378 operation == eFrameCompareYounger) {
379 ThreadPlanStepInRange *step_in_range_plan =
380 static_cast<ThreadPlanStepInRange *>(current_plan);
381 if (!step_in_range_plan->m_step_into_target.empty()) {
382 llvm::StringRef target_name = step_in_range_plan->m_step_into_target;
384 eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
385 if (sc.symbol != nullptr) {
386 if (target_name == sc.GetFunctionName()) {
387 should_stop_here = true;
388 } else {
389 llvm::StringRef function_name = sc.GetFunctionName().GetStringRef();
390 if (function_name.empty() || !function_name.contains(target_name))
391 should_stop_here = false;
392 }
393 if (log && !should_stop_here)
394 LLDB_LOG(log,
395 "Stepping out of frame {0} which did not match step into "
396 "target {1}.",
397 sc.GetFunctionName(), target_name);
398 }
399 }
400
401 if (should_stop_here) {
402 ThreadPlanStepInRange *step_in_range_plan =
403 static_cast<ThreadPlanStepInRange *>(current_plan);
404 // Don't log the should_step_out here, it's easier to do it in
405 // FrameMatchesAvoidCriteria.
406 should_stop_here = !step_in_range_plan->FrameMatchesAvoidCriteria();
407 }
408 }
409
410 return should_stop_here;
411}
412
414 // We always explain a stop. Either we've just done a single step, in which
415 // case we'll do our ordinary processing, or we stopped for some reason that
416 // isn't handled by our sub-plans, in which case we want to just stop right
417 // away. In general, we don't want to mark the plan as complete for
418 // unexplained stops. For instance, if you step in to some code with no debug
419 // info, so you step out and in the course of that hit a breakpoint, then you
420 // want to stop & show the user the breakpoint, but not unship the step in
421 // plan, since you still may want to complete that plan when you continue.
422 // This is particularly true when doing "step in to target function."
423 // stepping.
424 //
425 // The only variation is that if we are doing "step by running to next
426 // branch" in which case if we hit our branch breakpoint we don't set the
427 // plan to complete.
428
429 bool return_value = false;
430
432 return_value = true;
433 } else {
434 StopInfoSP stop_info_sp = GetPrivateStopInfo();
435 if (stop_info_sp) {
436 StopReason reason = stop_info_sp->GetStopReason();
437
438 if (reason == eStopReasonBreakpoint) {
439 if (NextRangeBreakpointExplainsStop(stop_info_sp)) {
440 return_value = true;
441 }
442 } else if (IsUsuallyUnexplainedStopReason(reason)) {
443 Log *log = GetLog(LLDBLog::Step);
444 if (log)
445 log->PutCString("ThreadPlanStepInRange got asked if it explains the "
446 "stop for some reason other than step.");
447 return_value = false;
448 } else {
449 return_value = true;
450 }
451 } else
452 return_value = true;
453 }
454
455 return return_value;
456}
457
459 bool current_plan) {
461 if (resume_state == eStateStepping && current_plan) {
462 Thread &thread = GetThread();
463 // See if we are about to step over a virtual inlined call.
464 // But if we already know we're virtual stepping, don't decrement the
465 // inlined depth again...
466
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.
480 }
481 return !step_without_resume;
482 }
483 return true;
484}
485
488 Thread &thread = GetThread();
489 uint32_t cur_inline_depth = thread.GetCurrentInlinedDepth();
490 if (cur_inline_depth == UINT32_MAX || cur_inline_depth == 0)
492 else
494 }
496}
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
#define LLDB_LOGF(log,...)
Definition Log.h:389
A section + offset based address range class.
A section + offset based address class.
Definition Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition Address.cpp:301
bool Slide(int64_t offset)
Definition Address.h:446
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,...
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
const char * GetCString() const
Get the string value as a C string.
A file collection class.
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:317
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 Address & GetAddress() const
Return the address of the function (its entry point).
Definition Function.h:430
uint32_t GetPrologueByteSize()
Get the size of the prologue instructions for this function.
Definition Function.cpp:594
void PutCString(const char *cstr)
Definition Log.cpp:162
@ ePreferDemangledWithoutArguments
Definition Mangled.h:39
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.
This base class provides an interface to stack frames.
Definition StackFrame.h:44
virtual const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
An error handling class.
Definition Status.h:118
static lldb::StopInfoSP CreateStopReasonToTrace(Thread &thread)
const char * GetData() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
void Format(const char *format, Args &&... args)
Forwards the arguments to llvm::formatv and writes to the stream.
Definition Stream.h:370
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:405
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
size_t PutChar(char ch)
Definition Stream.cpp:131
Defines a symbol context baton that can be handed other debug core functions.
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.
Address GetAddress() const
Definition Symbol.h:102
uint32_t GetPrologueByteSize()
Definition Symbol.cpp:348
Architecture * GetArchitecturePlugin() const
Definition Target.h:1335
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow, bool allow_section_end=false)
Definition Target.cpp:3495
static bool DefaultShouldStopHereCallback(ThreadPlan *current_plan, Flags &flags, lldb::FrameComparison operation, Status &status, void *baton)
lldb::ThreadPlanSP CheckShouldStopHereAndQueueStepOut(lldb::FrameComparison operation, Status &status)
ThreadPlanStepInRange(Thread &thread, const AddressRange &range, const SymbolContext &addr_context, std::string step_into_target, lldb::RunMode stop_others, LazyBool step_in_avoids_code_without_debug_info, LazyBool step_out_avoids_code_without_debug_info)
static void SetDefaultFlagValue(uint32_t new_value)
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()
ThreadPlanStepRange(ThreadPlanKind kind, const char *name, Thread &thread, const AddressRange &range, const SymbolContext &addr_context, lldb::RunMode stop_others, bool given_ranges_only=false)
void SetStopInfo(lldb::StopInfoSP stop_reason_sp)
Definition ThreadPlan.h:548
bool IsUsuallyUnexplainedStopReason(lldb::StopReason)
ThreadPlanKind GetKind() const
Definition ThreadPlan.h:446
void SetPlanComplete(bool success=true)
Thread & GetThread()
Returns the Thread that is using this thread plan.
lldb::StopInfoSP GetPrivateStopInfo()
Definition ThreadPlan.h:544
const RegularExpression * GetSymbolsToAvoidRegexp()
The regular expression returned determines symbols that this thread won't stop in during "step-in" op...
Definition Thread.cpp:119
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition Thread.h:435
#define UINT32_MAX
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:338
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
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eDescriptionLevelBrief
@ eDescriptionLevelVerbose
FrameComparison
This is the return value for frame comparisons.
@ eFrameCompareSameParent
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
uint64_t addr_t
Definition lldb-types.h:80
StopReason
Thread stop reasons.
@ eStopReasonBreakpoint
RunMode
Thread Run Modes.