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
9#include "lldb/Target/ThreadPlanStepInRange.h"
10#include "lldb/Core/Architecture.h"
11#include "lldb/Core/Module.h"
12#include "lldb/Symbol/Function.h"
13#include "lldb/Symbol/Symbol.h"
14#include "lldb/Target/Process.h"
15#include "lldb/Target/RegisterContext.h"
16#include "lldb/Target/SectionLoadList.h"
17#include "lldb/Target/Target.h"
18#include "lldb/Target/Thread.h"
19#include "lldb/Target/ThreadPlanStepOut.h"
20#include "lldb/Target/ThreadPlanStepThrough.h"
21#include "lldb/Utility/LLDBLog.h"
22#include "lldb/Utility/Log.h"
23#include "lldb/Utility/RegularExpression.h"
24#include "lldb/Utility/Stream.h"
25
26using namespace lldb;
27using namespace lldb_private;
28
29uint32_t ThreadPlanStepInRange::s_default_flag_values =
30 ThreadPlanShouldStopHere::eStepInAvoidNoDebug |
31 ThreadPlanShouldStopHere::eStepOutPastThunks;
32
33// ThreadPlanStepInRange: Step through a stack range, either stepping over or
34// into based on the value of \a type.
35
36ThreadPlanStepInRange::ThreadPlanStepInRange(
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)
41 : ThreadPlanStepRange(ThreadPlan::eKindStepInRange,
42 "Step Range stepping in", thread, range, addr_context,
43 stop_others),
44 ThreadPlanShouldStopHere(this), m_step_past_prologue(true),
45 m_virtual_step(eLazyBoolCalculate), m_step_into_target(step_into_target) {
46 SetCallbacks();
47 SetFlagsToDefault();
48 SetupAvoidNoDebug(step_in_avoids_code_without_debug_info,
49 step_out_avoids_code_without_debug_info);
50}
51
52ThreadPlanStepInRange::~ThreadPlanStepInRange() = default;
53
54void ThreadPlanStepInRange::SetupAvoidNoDebug(
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;
66 case eLazyBoolCalculate:
67 avoid_nodebug = thread.GetStepInAvoidsNoDebug();
68 break;
69 }
70 if (avoid_nodebug)
71 GetFlags().Set(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
72 else
73 GetFlags().Clear(mask: ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
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;
82 case eLazyBoolCalculate:
83 avoid_nodebug = thread.GetStepOutAvoidsNoDebug();
84 break;
85 }
86 if (avoid_nodebug)
87 GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
88 else
89 GetFlags().Clear(mask: ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
90}
91
92void ThreadPlanStepInRange::GetDescription(Stream *s,
93 lldb::DescriptionLevel level) {
94
95 auto PrintFailureIfAny = [&]() {
96 if (m_status.Success())
97 return;
98 s->Printf(format: " failed (%s)", m_status.AsCString());
99 };
100
101 if (level == lldb::eDescriptionLevelBrief) {
102 s->Printf(format: "step in");
103 PrintFailureIfAny();
104 return;
105 }
106
107 s->Printf(format: "Stepping in");
108 bool printed_line_info = false;
109 if (m_addr_context.line_entry.IsValid()) {
110 s->Printf(format: " through line ");
111 m_addr_context.line_entry.DumpStopContext(s, show_fullpaths: 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(format: " targeting %s", m_step_into_target.AsCString());
118
119 if (!printed_line_info || level == eDescriptionLevelVerbose) {
120 s->Printf(format: " using ranges:");
121 DumpRanges(s);
122 }
123
124 PrintFailureIfAny();
125
126 s->PutChar(ch: '.');
127}
128
129bool ThreadPlanStepInRange::ShouldStop(Event *event_ptr) {
130 Log *log = GetLog(mask: LLDBLog::Step);
131
132 if (log) {
133 StreamString s;
134 DumpAddress(s&: s.AsRawOstream(), addr: GetThread().GetRegisterContext()->GetPC(),
135 addr_size: GetTarget().GetArchitecture().GetAddressByteSize());
136 LLDB_LOGF(log, "ThreadPlanStepInRange reached %s.", s.GetData());
137 }
138 ClearNextBranchBreakpointExplainedStop();
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()) {
146 SetPlanComplete();
147 m_no_more_plans = true;
148 return true;
149 } else
150 m_sub_plan_sp.reset();
151 }
152
153 if (m_virtual_step == eLazyBoolYes) {
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.
158 m_sub_plan_sp =
159 CheckShouldStopHereAndQueueStepOut(operation: eFrameCompareYounger, status&: m_status);
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
167 FrameComparison frame_order = CompareCurrentFrameToStartFrame();
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 return_stack_id&: m_stack_id, abort_other_plans: false, stop_other_threads: stop_others, status&: m_status);
181 if (!m_sub_plan_sp) {
182 // Otherwise check the ShouldStopHere for step out:
183 m_sub_plan_sp =
184 CheckShouldStopHereAndQueueStepOut(operation: frame_order, status&: m_status);
185 if (log) {
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 }
192 } else if (log) {
193 LLDB_LOGF(
194 log, "Thought I stepped out, but in fact arrived at a trampoline.");
195 }
196 } else if (frame_order == eFrameCompareEqual && InSymbol()) {
197 // If we are not in a place we should step through, we're done. One
198 // tricky bit here is that some stubs don't push a frame, so we have to
199 // check both the case of a frame that is younger, or the same as this
200 // frame. However, if the frame is the same, and we are still in the
201 // symbol we started in, the we don't need to do this. This first check
202 // isn't strictly necessary, but it is more efficient.
203
204 // If we're still in the range, keep going, either by running to the next
205 // branch breakpoint, or by stepping.
206 if (InRange()) {
207 SetNextBranchBreakpoint();
208 return false;
209 }
210
211 SetPlanComplete();
212 m_no_more_plans = true;
213 return true;
214 }
215
216 // If we get to this point, we're not going to use a previously set "next
217 // branch" breakpoint, so delete it:
218 ClearNextBranchBreakpoint();
219
220 // We may have set the plan up above in the FrameIsOlder section:
221
222 if (!m_sub_plan_sp)
223 m_sub_plan_sp = thread.QueueThreadPlanForStepThrough(
224 return_stack_id&: m_stack_id, abort_other_plans: false, stop_other_threads: stop_others, status&: m_status);
225
226 if (log) {
227 if (m_sub_plan_sp)
228 LLDB_LOGF(log, "Found a step through plan: %s",
229 m_sub_plan_sp->GetName());
230 else
231 LLDB_LOGF(log, "No step through plan found.");
232 }
233
234 // If not, give the "should_stop" callback a chance to push a plan to get
235 // us out of here. But only do that if we actually have stepped in.
236 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
237 m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(operation: frame_order, status&: m_status);
238
239 // If we've stepped in and we are going to stop here, check to see if we
240 // were asked to run past the prologue, and if so do that.
241
242 if (!m_sub_plan_sp && frame_order == eFrameCompareYounger &&
243 m_step_past_prologue) {
244 lldb::StackFrameSP curr_frame = thread.GetStackFrameAtIndex(idx: 0);
245 if (curr_frame) {
246 size_t bytes_to_skip = 0;
247 lldb::addr_t curr_addr = thread.GetRegisterContext()->GetPC();
248 Address func_start_address;
249
250 SymbolContext sc = curr_frame->GetSymbolContext(resolve_scope: eSymbolContextFunction |
251 eSymbolContextSymbol);
252
253 if (sc.function) {
254 func_start_address = sc.function->GetAddress();
255 if (curr_addr == func_start_address.GetLoadAddress(target: &GetTarget()))
256 bytes_to_skip = sc.function->GetPrologueByteSize();
257 } else if (sc.symbol) {
258 func_start_address = sc.symbol->GetAddress();
259 if (curr_addr == func_start_address.GetLoadAddress(target: &GetTarget()))
260 bytes_to_skip = sc.symbol->GetPrologueByteSize();
261 }
262
263 if (bytes_to_skip == 0 && sc.symbol) {
264 const Architecture *arch = GetTarget().GetArchitecturePlugin();
265 if (arch) {
266 Address curr_sec_addr;
267 GetTarget().ResolveLoadAddress(load_addr: curr_addr, so_addr&: curr_sec_addr);
268 bytes_to_skip = arch->GetBytesToSkip(func&: *sc.symbol, curr_addr: curr_sec_addr);
269 }
270 }
271
272 if (bytes_to_skip != 0) {
273 func_start_address.Slide(offset: bytes_to_skip);
274 log = GetLog(mask: LLDBLog::Step);
275 LLDB_LOGF(log, "Pushing past prologue ");
276
277 m_sub_plan_sp = thread.QueueThreadPlanForRunToAddress(
278 abort_other_plans: false, target_addr&: func_start_address, stop_other_threads: true, status&: m_status);
279 }
280 }
281 }
282 }
283
284 if (!m_sub_plan_sp) {
285 m_no_more_plans = true;
286 SetPlanComplete();
287 return true;
288 } else {
289 m_no_more_plans = false;
290 m_sub_plan_sp->SetPrivate(true);
291 return false;
292 }
293}
294
295void ThreadPlanStepInRange::SetAvoidRegexp(const char *name) {
296 if (m_avoid_regexp_up)
297 *m_avoid_regexp_up = RegularExpression(name);
298 else
299 m_avoid_regexp_up = std::make_unique<RegularExpression>(args&: name);
300}
301
302void ThreadPlanStepInRange::SetDefaultFlagValue(uint32_t new_value) {
303 // TODO: Should we test this for sanity?
304 ThreadPlanStepInRange::s_default_flag_values = new_value;
305}
306
307bool ThreadPlanStepInRange::FrameMatchesAvoidCriteria() {
308 StackFrame *frame = GetThread().GetStackFrameAtIndex(idx: 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(resolve_scope: 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(idx: i));
322 if (FileSpec::Match(pattern: file_spec, file: 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) {
337 SymbolContext sc = frame->GetSymbolContext(
338 resolve_scope: eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
339 if (sc.symbol != nullptr) {
340 const char *frame_function_name =
341 sc.GetFunctionName(preference: Mangled::ePreferDemangledWithoutArguments)
342 .GetCString();
343 if (frame_function_name) {
344 bool return_value = avoid_regexp_to_use->Execute(string: frame_function_name);
345 if (return_value) {
346 LLDB_LOGF(GetLog(LLDBLog::Step),
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
359bool ThreadPlanStepInRange::DefaultShouldStopHereCallback(
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(idx: 0).get();
364 Log *log = GetLog(mask: LLDBLog::Step);
365
366 // First see if the ThreadPlanShouldStopHere default implementation thinks we
367 // should get out of here:
368 should_stop_here = ThreadPlanShouldStopHere::DefaultShouldStopHereCallback(
369 current_plan, flags, operation, status, baton);
370 if (!should_stop_here)
371 return false;
372
373 if (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) {
378 SymbolContext sc = frame->GetSymbolContext(
379 resolve_scope: 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(haystack: function_name, needle: 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.",
399 sc.GetFunctionName().AsCString(),
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
416bool ThreadPlanStepInRange::DoPlanExplainsStop(Event *event_ptr) {
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 == eLazyBoolYes) {
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(mask: LLDBLog::Step);
447 if (log)
448 log->PutCString(cstr: "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
461bool ThreadPlanStepInRange::DoWillResume(lldb::StateType resume_state,
462 bool current_plan) {
463 m_virtual_step = eLazyBoolCalculate;
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 // But if we already know we're virtual stepping, don't decrement the
468 // inlined depth again...
469
470 bool step_without_resume = thread.DecrementCurrentInlinedDepth();
471 if (step_without_resume) {
472 Log *log = GetLog(mask: LLDBLog::Step);
473 LLDB_LOGF(log,
474 "ThreadPlanStepInRange::DoWillResume: returning false, "
475 "inline_depth: %d",
476 thread.GetCurrentInlinedDepth());
477 SetStopInfo(StopInfo::CreateStopReasonToTrace(thread));
478
479 // FIXME: Maybe it would be better to create a InlineStep stop reason, but
480 // then
481 // the whole rest of the world would have to handle that stop reason.
482 m_virtual_step = eLazyBoolYes;
483 }
484 return !step_without_resume;
485 }
486 return true;
487}
488
489bool ThreadPlanStepInRange::IsVirtualStep() {
490 if (m_virtual_step == eLazyBoolCalculate) {
491 Thread &thread = GetThread();
492 uint32_t cur_inline_depth = thread.GetCurrentInlinedDepth();
493 if (cur_inline_depth == UINT32_MAX || cur_inline_depth == 0)
494 m_virtual_step = eLazyBoolNo;
495 else
496 m_virtual_step = eLazyBoolYes;
497 }
498 return m_virtual_step == eLazyBoolYes;
499}
500

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of lldb/source/Target/ThreadPlanStepInRange.cpp