[Go to site: main page, start]

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
32
33// ThreadPlanStepInRange: Step through a stack range, either stepping over or
34// into based on the value of \a type.
35
37 Thread &thread, const AddressRange &range,
38 const SymbolContext &addr_context, std::string step_into_target,
39 lldb::RunMode stop_others, LazyBool step_in_avoids_code_without_debug_info,
40 LazyBool step_out_avoids_code_without_debug_info)
42 "Step Range stepping in", thread, range, addr_context,
43 stop_others),
46 m_step_into_target(std::move(step_into_target)) {
49 SetupAvoidNoDebug(step_in_avoids_code_without_debug_info,
50 step_out_avoids_code_without_debug_info);
51}
52
54
56 LazyBool step_in_avoids_code_without_debug_info,
57 LazyBool step_out_avoids_code_without_debug_info) {
58 bool avoid_nodebug = true;
59 Thread &thread = GetThread();
60 switch (step_in_avoids_code_without_debug_info) {
61 case eLazyBoolYes:
62 avoid_nodebug = true;
63 break;
64 case eLazyBoolNo:
65 avoid_nodebug = false;
66 break;
68 avoid_nodebug = thread.GetStepInAvoidsNoDebug();
69 break;
70 }
71 if (avoid_nodebug)
73 else
75
76 switch (step_out_avoids_code_without_debug_info) {
77 case eLazyBoolYes:
78 avoid_nodebug = true;
79 break;
80 case eLazyBoolNo:
81 avoid_nodebug = false;
82 break;
84 avoid_nodebug = thread.GetStepOutAvoidsNoDebug();
85 break;
86 }
87 if (avoid_nodebug)
89 else
91}
92
95
96 auto PrintFailureIfAny = [&]() {
97 if (m_status.Success())
98 return;
99 s->Printf(" failed (%s)", m_status.AsCString());
100 };
101
102 if (level == lldb::eDescriptionLevelBrief) {
103 s->PutCString("step in");
104 PrintFailureIfAny();
105 return;
106 }
107
108 s->PutCString("Stepping in");
109 bool printed_line_info = false;
110 if (m_addr_context.line_entry.IsValid()) {
111 s->PutCString(" through line ");
112 m_addr_context.line_entry.DumpStopContext(s, false);
113 printed_line_info = true;
114 }
115
116 if (!m_step_into_target.empty())
117 s->Format(" targeting {0}", m_step_into_target);
118
119 if (!printed_line_info || level == eDescriptionLevelVerbose) {
120 s->PutCString(" using ranges:");
121 DumpRanges(s);
122 }
123
124 PrintFailureIfAny();
125
126 s->PutChar('.');
127}
128
130 Log *log = GetLog(LLDBLog::Step);
131
132 if (log) {
133 StreamString s;
134 DumpAddress(s.AsRawOstream(), GetThread().GetRegisterContext()->GetPC(),
135 GetTarget().GetArchitecture().GetAddressByteSize());
136 LLDB_LOGF(log, "ThreadPlanStepInRange reached %s.", s.GetData());
137 }
139
140 if (IsPlanComplete())
141 return true;
142
143 m_no_more_plans = false;
144 if (m_sub_plan_sp && m_sub_plan_sp->IsPlanComplete()) {
145 if (!m_sub_plan_sp->PlanSucceeded()) {
147 m_no_more_plans = true;
148 return true;
149 } else
150 m_sub_plan_sp.reset();
151 }
152
154 // If we've just completed a virtual step, all we need to do is check for a
155 // ShouldStopHere plan, and otherwise we're done.
156 // FIXME - This can be both a step in and a step out. Probably should
157 // record which in the m_virtual_step.
160 } else {
161 // Stepping through should be done running other threads in general, since
162 // we're setting a breakpoint and continuing. So only stop others if we
163 // are explicitly told to do so.
164
165 bool stop_others = (m_stop_others == lldb::eOnlyThisThread);
166
168
169 Thread &thread = GetThread();
170 if (frame_order == eFrameCompareOlder ||
171 frame_order == eFrameCompareSameParent) {
172 // If we're in an older frame then we should stop.
173 //
174 // A caveat to this is if we think the frame is older but we're actually
175 // in a trampoline.
176 // I'm going to make the assumption that you wouldn't RETURN to a
177 // trampoline. So if we are in a trampoline we think the frame is older
178 // because the trampoline confused the backtracer.
179 m_sub_plan_sp = thread.QueueThreadPlanForStepThrough(
180 m_stack_id, false, stop_others, m_status);
181 if (!m_sub_plan_sp) {
182 // Otherwise check the ShouldStopHere for step out:
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 } else {
191 LLDB_LOGF(
192 log, "Thought I stepped out, but in fact arrived at a trampoline.");
193 }
194 } else if (frame_order == eFrameCompareEqual && InSymbol()) {
195 // If we are not in a place we should step through, we're done. One
196 // tricky bit here is that some stubs don't push a frame, so we have to
197 // check both the case of a frame that is younger, or the same as this
198 // frame. However, if the frame is the same, and we are still in the
199 // symbol we started in, the we don't need to do this. This first check
200 // isn't strictly necessary, but it is more efficient.
201
202 // If we're still in the range, keep going, either by running to the next
203 // branch breakpoint, or by stepping.
204 if (InRange()) {
206 return false;
207 }
208
210 m_no_more_plans = true;
211 return true;
212 }
213
214 // If we get to this point, we're not going to use a previously set "next
215 // branch" breakpoint, so delete it:
217
218 // We may have set the plan up above in the FrameIsOlder section:
219
220 if (!m_sub_plan_sp)
221 m_sub_plan_sp = thread.QueueThreadPlanForStepThrough(
222 m_stack_id, false, stop_others, m_status);
223
224 if (m_sub_plan_sp)
225 LLDB_LOGF(log, "Found a step through plan: %s", m_sub_plan_sp->GetName());
226 else
227 LLDB_LOGF(log, "No step through plan found.");
228
229 // If not, give the "should_stop" callback a chance to push a plan to get
230 // us out of here. But only do that if we actually have stepped in.
231 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
233
234 // If we've stepped in and we are going to stop here, check to see if we
235 // were asked to run past the prologue, and if so do that.
236
237 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger &&
239 lldb::StackFrameSP curr_frame = thread.GetStackFrameAtIndex(0);
240 if (curr_frame) {
241 size_t bytes_to_skip = 0;
242 lldb::addr_t curr_addr = thread.GetRegisterContext()->GetPC();
243 Address func_start_address;
244
245 SymbolContext sc = curr_frame->GetSymbolContext(eSymbolContextFunction |
246 eSymbolContextSymbol);
247
248 if (sc.function) {
249 func_start_address = sc.function->GetAddress();
250 bytes_to_skip = sc.function->GetPrologueByteSize();
251 } else if (sc.symbol) {
252 func_start_address = sc.symbol->GetAddress();
253 bytes_to_skip = sc.symbol->GetPrologueByteSize();
254 }
255
256 // A function's entry point need not be its first address, so a pc past
257 // the entry point can still be inside the prologue. What says the
258 // prologue has yet to run is the pc being below its end.
259 if (bytes_to_skip != 0) {
260 const lldb::addr_t prologue_end =
261 func_start_address.GetLoadAddress(&GetTarget()) + bytes_to_skip;
262 if (curr_addr >= prologue_end)
263 bytes_to_skip = 0;
264 }
265
266 if (bytes_to_skip == 0 && sc.symbol) {
268 if (arch) {
269 Address curr_sec_addr;
270 GetTarget().ResolveLoadAddress(curr_addr, curr_sec_addr);
271 bytes_to_skip = arch->GetBytesToSkip(*sc.symbol, curr_sec_addr);
272 }
273 }
274
275 if (bytes_to_skip != 0) {
276 func_start_address.Slide(bytes_to_skip);
277 log = GetLog(LLDBLog::Step);
278 LLDB_LOGF(log, "Pushing past prologue ");
279
280 m_sub_plan_sp = thread.QueueThreadPlanForRunToAddress(
281 false, func_start_address, true, m_status);
282 }
283 }
284 }
285 }
286
287 if (!m_sub_plan_sp) {
288 m_no_more_plans = true;
290 return true;
291 } else {
292 m_no_more_plans = false;
293 m_sub_plan_sp->SetPrivate(true);
294 return false;
295 }
296}
297
301 else
302 m_avoid_regexp_up = std::make_unique<RegularExpression>(name);
303}
304
306 // TODO: Should we test this for sanity?
308}
309
311 StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
312
313 // Check the library list first, as that's cheapest:
314 bool libraries_say_avoid = false;
315
316 FileSpecList libraries_to_avoid(GetThread().GetLibrariesToAvoid());
317 size_t num_libraries = libraries_to_avoid.GetSize();
318 if (num_libraries > 0) {
319 SymbolContext sc(frame->GetSymbolContext(eSymbolContextModule));
320 FileSpec frame_library(sc.module_sp->GetFileSpec());
321
322 if (frame_library) {
323 for (size_t i = 0; i < num_libraries; i++) {
324 const FileSpec &file_spec(libraries_to_avoid.GetFileSpecAtIndex(i));
325 if (FileSpec::Match(file_spec, frame_library)) {
326 libraries_say_avoid = true;
327 break;
328 }
329 }
330 }
331 }
332 if (libraries_say_avoid)
333 return true;
334
335 const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_up.get();
336 if (avoid_regexp_to_use == nullptr)
337 avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
338
339 if (avoid_regexp_to_use != nullptr) {
341 eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
342 if (sc.symbol != nullptr) {
343 const char *frame_function_name =
345 .GetCString();
346 if (frame_function_name) {
347 bool return_value = avoid_regexp_to_use->Execute(frame_function_name);
348 if (return_value) {
350 "Stepping out of function \"%s\" because it matches the "
351 "avoid regexp \"%s\".",
352 frame_function_name,
353 avoid_regexp_to_use->GetText().str().c_str());
354 }
355 return return_value;
356 }
357 }
358 }
359 return false;
360}
361
363 ThreadPlan *current_plan, Flags &flags, FrameComparison operation,
364 Status &status, void *baton) {
365 bool should_stop_here = true;
366 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
367 Log *log = GetLog(LLDBLog::Step);
368
369 // First see if the ThreadPlanShouldStopHere default implementation thinks we
370 // should get out of here:
372 current_plan, flags, operation, status, baton);
373 if (!should_stop_here)
374 return false;
375
376 if (current_plan->GetKind() == eKindStepInRange &&
377 operation == eFrameCompareYounger) {
378 ThreadPlanStepInRange *step_in_range_plan =
379 static_cast<ThreadPlanStepInRange *>(current_plan);
380 if (!step_in_range_plan->m_step_into_target.empty()) {
381 llvm::StringRef target_name = step_in_range_plan->m_step_into_target;
383 eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
384 if (sc.symbol != nullptr) {
385 if (target_name == sc.GetFunctionName()) {
386 should_stop_here = true;
387 } else {
388 llvm::StringRef function_name = sc.GetFunctionName().GetStringRef();
389 if (function_name.empty() || !function_name.contains(target_name))
390 should_stop_here = false;
391 }
392 if (log && !should_stop_here)
393 LLDB_LOG(log,
394 "Stepping out of frame {0} which did not match step into "
395 "target {1}.",
396 sc.GetFunctionName(), target_name);
397 }
398 }
399
400 if (should_stop_here) {
401 ThreadPlanStepInRange *step_in_range_plan =
402 static_cast<ThreadPlanStepInRange *>(current_plan);
403 // Don't log the should_step_out here, it's easier to do it in
404 // FrameMatchesAvoidCriteria.
405 should_stop_here = !step_in_range_plan->FrameMatchesAvoidCriteria();
406 }
407 }
408
409 return should_stop_here;
410}
411
413 // We always explain a stop. Either we've just done a single step, in which
414 // case we'll do our ordinary processing, or we stopped for some reason that
415 // isn't handled by our sub-plans, in which case we want to just stop right
416 // away. In general, we don't want to mark the plan as complete for
417 // unexplained stops. For instance, if you step in to some code with no debug
418 // info, so you step out and in the course of that hit a breakpoint, then you
419 // want to stop & show the user the breakpoint, but not unship the step in
420 // plan, since you still may want to complete that plan when you continue.
421 // This is particularly true when doing "step in to target function."
422 // stepping.
423 //
424 // The only variation is that if we are doing "step by running to next
425 // branch" in which case if we hit our branch breakpoint we don't set the
426 // plan to complete.
427
428 bool return_value = false;
429
431 return_value = true;
432 } else {
433 StopInfoSP stop_info_sp = GetPrivateStopInfo();
434 if (stop_info_sp) {
435 StopReason reason = stop_info_sp->GetStopReason();
436
437 if (reason == eStopReasonBreakpoint) {
438 if (NextRangeBreakpointExplainsStop(stop_info_sp)) {
439 return_value = true;
440 }
441 } else if (IsUsuallyUnexplainedStopReason(reason)) {
442 Log *log = GetLog(LLDBLog::Step);
443 if (log)
444 log->PutCString("ThreadPlanStepInRange got asked if it explains the "
445 "stop for some reason other than step.");
446 return_value = false;
447 } else {
448 return_value = true;
449 }
450 } else
451 return_value = true;
452 }
453
454 return return_value;
455}
456
458 bool current_plan) {
460 if (resume_state == eStateStepping && current_plan) {
461 Thread &thread = GetThread();
462 // See if we are about to step over a virtual inlined call.
463 // But if we already know we're virtual stepping, don't decrement the
464 // inlined depth again...
465
466 bool step_without_resume = thread.DecrementCurrentInlinedDepth();
467 if (step_without_resume) {
468 Log *log = GetLog(LLDBLog::Step);
469 LLDB_LOGF(log,
470 "ThreadPlanStepInRange::DoWillResume: returning false, "
471 "inline_depth: %d",
472 thread.GetCurrentInlinedDepth());
474
475 // FIXME: Maybe it would be better to create a InlineStep stop reason, but
476 // then
477 // the whole rest of the world would have to handle that stop reason.
479 }
480 return !step_without_resume;
481 }
482 return true;
483}
484
487 Thread &thread = GetThread();
488 uint32_t cur_inline_depth = thread.GetCurrentInlinedDepth();
489 if (cur_inline_depth == UINT32_MAX || cur_inline_depth == 0)
491 else
493 }
495}
#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:1328
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow, bool allow_section_end=false)
Definition Target.cpp:3491
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.