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, const char *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),
48 SetupAvoidNoDebug(step_in_avoids_code_without_debug_info,
49 step_out_avoids_code_without_debug_info);
50}
51
53
55 LazyBool step_in_avoids_code_without_debug_info,
56 LazyBool step_out_avoids_code_without_debug_info) {
57 bool avoid_nodebug = true;
58 Thread &thread = GetThread();
59 switch (step_in_avoids_code_without_debug_info) {
60 case eLazyBoolYes:
61 avoid_nodebug = true;
62 break;
63 case eLazyBoolNo:
64 avoid_nodebug = false;
65 break;
67 avoid_nodebug = thread.GetStepInAvoidsNoDebug();
68 break;
69 }
70 if (avoid_nodebug)
72 else
74
75 switch (step_out_avoids_code_without_debug_info) {
76 case eLazyBoolYes:
77 avoid_nodebug = true;
78 break;
79 case eLazyBoolNo:
80 avoid_nodebug = false;
81 break;
83 avoid_nodebug = thread.GetStepOutAvoidsNoDebug();
84 break;
85 }
86 if (avoid_nodebug)
88 else
90}
91
94
95 auto PrintFailureIfAny = [&]() {
96 if (m_status.Success())
97 return;
98 s->Printf(" failed (%s)", m_status.AsCString());
99 };
100
101 if (level == lldb::eDescriptionLevelBrief) {
102 s->Printf("step in");
103 PrintFailureIfAny();
104 return;
105 }
106
107 s->Printf("Stepping in");
108 bool printed_line_info = false;
109 if (m_addr_context.line_entry.IsValid()) {
110 s->Printf(" through line ");
111 m_addr_context.line_entry.DumpStopContext(s, false);
112 printed_line_info = true;
113 }
114
115 const char *step_into_target = m_step_into_target.AsCString();
116 if (step_into_target && step_into_target[0] != '\0')
117 s->Printf(" targeting %s", m_step_into_target.AsCString());
118
119 if (!printed_line_info || level == eDescriptionLevelVerbose) {
120 s->Printf(" 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 if (curr_addr == func_start_address.GetLoadAddress(&GetTarget()))
251 bytes_to_skip = sc.function->GetPrologueByteSize();
252 } else if (sc.symbol) {
253 func_start_address = sc.symbol->GetAddress();
254 if (curr_addr == func_start_address.GetLoadAddress(&GetTarget()))
255 bytes_to_skip = sc.symbol->GetPrologueByteSize();
256 }
257
258 if (bytes_to_skip == 0 && sc.symbol) {
260 if (arch) {
261 Address curr_sec_addr;
262 GetTarget().ResolveLoadAddress(curr_addr, curr_sec_addr);
263 bytes_to_skip = arch->GetBytesToSkip(*sc.symbol, curr_sec_addr);
264 }
265 }
266
267 if (bytes_to_skip != 0) {
268 func_start_address.Slide(bytes_to_skip);
269 log = GetLog(LLDBLog::Step);
270 LLDB_LOGF(log, "Pushing past prologue ");
271
272 m_sub_plan_sp = thread.QueueThreadPlanForRunToAddress(
273 false, func_start_address, true, m_status);
274 }
275 }
276 }
277 }
278
279 if (!m_sub_plan_sp) {
280 m_no_more_plans = true;
282 return true;
283 } else {
284 m_no_more_plans = false;
285 m_sub_plan_sp->SetPrivate(true);
286 return false;
287 }
288}
289
293 else
294 m_avoid_regexp_up = std::make_unique<RegularExpression>(name);
295}
296
298 // TODO: Should we test this for sanity?
300}
301
303 StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
304
305 // Check the library list first, as that's cheapest:
306 bool libraries_say_avoid = false;
307
308 FileSpecList libraries_to_avoid(GetThread().GetLibrariesToAvoid());
309 size_t num_libraries = libraries_to_avoid.GetSize();
310 if (num_libraries > 0) {
311 SymbolContext sc(frame->GetSymbolContext(eSymbolContextModule));
312 FileSpec frame_library(sc.module_sp->GetFileSpec());
313
314 if (frame_library) {
315 for (size_t i = 0; i < num_libraries; i++) {
316 const FileSpec &file_spec(libraries_to_avoid.GetFileSpecAtIndex(i));
317 if (FileSpec::Match(file_spec, frame_library)) {
318 libraries_say_avoid = true;
319 break;
320 }
321 }
322 }
323 }
324 if (libraries_say_avoid)
325 return true;
326
327 const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_up.get();
328 if (avoid_regexp_to_use == nullptr)
329 avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
330
331 if (avoid_regexp_to_use != nullptr) {
333 eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
334 if (sc.symbol != nullptr) {
335 const char *frame_function_name =
337 .GetCString();
338 if (frame_function_name) {
339 bool return_value = avoid_regexp_to_use->Execute(frame_function_name);
340 if (return_value) {
342 "Stepping out of function \"%s\" because it matches the "
343 "avoid regexp \"%s\".",
344 frame_function_name,
345 avoid_regexp_to_use->GetText().str().c_str());
346 }
347 return return_value;
348 }
349 }
350 }
351 return false;
352}
353
355 ThreadPlan *current_plan, Flags &flags, FrameComparison operation,
356 Status &status, void *baton) {
357 bool should_stop_here = true;
358 StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
359 Log *log = GetLog(LLDBLog::Step);
360
361 // First see if the ThreadPlanShouldStopHere default implementation thinks we
362 // should get out of here:
364 current_plan, flags, operation, status, baton);
365 if (!should_stop_here)
366 return false;
367
368 if (current_plan->GetKind() == eKindStepInRange &&
369 operation == eFrameCompareYounger) {
370 ThreadPlanStepInRange *step_in_range_plan =
371 static_cast<ThreadPlanStepInRange *>(current_plan);
372 if (step_in_range_plan->m_step_into_target) {
374 eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
375 if (sc.symbol != nullptr) {
376 // First try an exact match, since that's cheap with ConstStrings.
377 // Then do a strstr compare.
378 if (step_in_range_plan->m_step_into_target == sc.GetFunctionName()) {
379 should_stop_here = true;
380 } else {
381 const char *target_name =
382 step_in_range_plan->m_step_into_target.AsCString();
383 const char *function_name = sc.GetFunctionName().AsCString();
384
385 if (function_name == nullptr)
386 should_stop_here = false;
387 else if (strstr(function_name, target_name) == nullptr)
388 should_stop_here = false;
389 }
390 if (log && !should_stop_here)
391 LLDB_LOGF(log,
392 "Stepping out of frame %s which did not match step into "
393 "target %s.",
395 step_in_range_plan->m_step_into_target.AsCString());
396 }
397 }
398
399 if (should_stop_here) {
400 ThreadPlanStepInRange *step_in_range_plan =
401 static_cast<ThreadPlanStepInRange *>(current_plan);
402 // Don't log the should_step_out here, it's easier to do it in
403 // FrameMatchesAvoidCriteria.
404 should_stop_here = !step_in_range_plan->FrameMatchesAvoidCriteria();
405 }
406 }
407
408 return should_stop_here;
409}
410
412 // We always explain a stop. Either we've just done a single step, in which
413 // case we'll do our ordinary processing, or we stopped for some reason that
414 // isn't handled by our sub-plans, in which case we want to just stop right
415 // away. In general, we don't want to mark the plan as complete for
416 // unexplained stops. For instance, if you step in to some code with no debug
417 // info, so you step out and in the course of that hit a breakpoint, then you
418 // want to stop & show the user the breakpoint, but not unship the step in
419 // plan, since you still may want to complete that plan when you continue.
420 // This is particularly true when doing "step in to target function."
421 // stepping.
422 //
423 // The only variation is that if we are doing "step by running to next
424 // branch" in which case if we hit our branch breakpoint we don't set the
425 // plan to complete.
426
427 bool return_value = false;
428
430 return_value = true;
431 } else {
432 StopInfoSP stop_info_sp = GetPrivateStopInfo();
433 if (stop_info_sp) {
434 StopReason reason = stop_info_sp->GetStopReason();
435
436 if (reason == eStopReasonBreakpoint) {
437 if (NextRangeBreakpointExplainsStop(stop_info_sp)) {
438 return_value = true;
439 }
440 } else if (IsUsuallyUnexplainedStopReason(reason)) {
441 Log *log = GetLog(LLDBLog::Step);
442 if (log)
443 log->PutCString("ThreadPlanStepInRange got asked if it explains the "
444 "stop for some reason other than step.");
445 return_value = false;
446 } else {
447 return_value = true;
448 }
449 } else
450 return_value = true;
451 }
452
453 return return_value;
454}
455
457 bool current_plan) {
459 if (resume_state == eStateStepping && current_plan) {
460 Thread &thread = GetThread();
461 // See if we are about to step over a virtual inlined call.
462 // But if we already know we're virtual stepping, don't decrement the
463 // inlined depth again...
464
465 bool step_without_resume = thread.DecrementCurrentInlinedDepth();
466 if (step_without_resume) {
467 Log *log = GetLog(LLDBLog::Step);
468 LLDB_LOGF(log,
469 "ThreadPlanStepInRange::DoWillResume: returning false, "
470 "inline_depth: %d",
471 thread.GetCurrentInlinedDepth());
473
474 // FIXME: Maybe it would be better to create a InlineStep stop reason, but
475 // then
476 // the whole rest of the world would have to handle that stop reason.
478 }
479 return !step_without_resume;
480 }
481 return true;
482}
483
486 Thread &thread = GetThread();
487 uint32_t cur_inline_depth = thread.GetCurrentInlinedDepth();
488 if (cur_inline_depth == UINT32_MAX || cur_inline_depth == 0)
490 else
492 }
494}
#define LLDB_LOGF(log,...)
Definition Log.h:383
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:452
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,...
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
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:57
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 Address & GetAddress() const
Return the address of the function (its entry point).
Definition Function.h:453
uint32_t GetPrologueByteSize()
Get the size of the prologue instructions for this function.
Definition Function.cpp:579
void PutCString(const char *cstr)
Definition Log.cpp:145
@ 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
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:418
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.
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:89
uint32_t GetPrologueByteSize()
Definition Symbol.cpp:313
Architecture * GetArchitecturePlugin() const
Definition Target.h:1221
bool ResolveLoadAddress(lldb::addr_t load_addr, Address &so_addr, uint32_t stop_id=SectionLoadHistory::eStopIDNow, bool allow_section_end=false)
Definition Target.cpp:3328
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()
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:118
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition Thread.h:434
#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:332
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
@ 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
uint64_t addr_t
Definition lldb-types.h:80
StopReason
Thread stop reasons.
@ eStopReasonBreakpoint
RunMode
Thread Run Modes.