1cfca06d7SDimitry Andric //===-- ThreadPlanStepInRange.cpp -----------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9e81d9d49SDimitry Andric #include "lldb/Target/ThreadPlanStepInRange.h"
10f73363f1SDimitry Andric #include "lldb/Core/Architecture.h"
11866dcdacSEd Maste #include "lldb/Core/Module.h"
12f034231aSEd Maste #include "lldb/Symbol/Function.h"
1314f1b3e8SDimitry Andric #include "lldb/Symbol/Symbol.h"
14f034231aSEd Maste #include "lldb/Target/Process.h"
15f034231aSEd Maste #include "lldb/Target/RegisterContext.h"
16f73363f1SDimitry Andric #include "lldb/Target/SectionLoadList.h"
17f034231aSEd Maste #include "lldb/Target/Target.h"
18f034231aSEd Maste #include "lldb/Target/Thread.h"
19f034231aSEd Maste #include "lldb/Target/ThreadPlanStepOut.h"
20f034231aSEd Maste #include "lldb/Target/ThreadPlanStepThrough.h"
21145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
2274a628f7SDimitry Andric #include "lldb/Utility/Log.h"
2374a628f7SDimitry Andric #include "lldb/Utility/RegularExpression.h"
2474a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
25f034231aSEd Maste
26f034231aSEd Maste using namespace lldb;
27f034231aSEd Maste using namespace lldb_private;
28f034231aSEd Maste
2914f1b3e8SDimitry Andric uint32_t ThreadPlanStepInRange::s_default_flag_values =
3014f1b3e8SDimitry Andric ThreadPlanShouldStopHere::eStepInAvoidNoDebug;
31f034231aSEd Maste
3214f1b3e8SDimitry Andric // ThreadPlanStepInRange: Step through a stack range, either stepping over or
33f73363f1SDimitry Andric // into based on the value of \a type.
34f034231aSEd Maste
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)3514f1b3e8SDimitry Andric ThreadPlanStepInRange::ThreadPlanStepInRange(
3614f1b3e8SDimitry Andric Thread &thread, const AddressRange &range,
37344a3780SDimitry Andric const SymbolContext &addr_context, const char *step_into_target,
38344a3780SDimitry Andric lldb::RunMode stop_others, LazyBool step_in_avoids_code_without_debug_info,
3914f1b3e8SDimitry Andric LazyBool step_out_avoids_code_without_debug_info)
4014f1b3e8SDimitry Andric : ThreadPlanStepRange(ThreadPlan::eKindStepInRange,
4114f1b3e8SDimitry Andric "Step Range stepping in", thread, range, addr_context,
4214f1b3e8SDimitry Andric stop_others),
4314f1b3e8SDimitry Andric ThreadPlanShouldStopHere(this), m_step_past_prologue(true),
44344a3780SDimitry Andric m_virtual_step(false), m_step_into_target(step_into_target) {
450cac4ca3SEd Maste SetCallbacks();
46f034231aSEd Maste SetFlagsToDefault();
4714f1b3e8SDimitry Andric SetupAvoidNoDebug(step_in_avoids_code_without_debug_info,
4814f1b3e8SDimitry Andric step_out_avoids_code_without_debug_info);
49f034231aSEd Maste }
50f034231aSEd Maste
51e81d9d49SDimitry Andric ThreadPlanStepInRange::~ThreadPlanStepInRange() = default;
52f034231aSEd Maste
SetupAvoidNoDebug(LazyBool step_in_avoids_code_without_debug_info,LazyBool step_out_avoids_code_without_debug_info)5314f1b3e8SDimitry Andric void ThreadPlanStepInRange::SetupAvoidNoDebug(
5414f1b3e8SDimitry Andric LazyBool step_in_avoids_code_without_debug_info,
5514f1b3e8SDimitry Andric LazyBool step_out_avoids_code_without_debug_info) {
560cac4ca3SEd Maste bool avoid_nodebug = true;
57cfca06d7SDimitry Andric Thread &thread = GetThread();
5814f1b3e8SDimitry Andric switch (step_in_avoids_code_without_debug_info) {
590cac4ca3SEd Maste case eLazyBoolYes:
600cac4ca3SEd Maste avoid_nodebug = true;
610cac4ca3SEd Maste break;
620cac4ca3SEd Maste case eLazyBoolNo:
630cac4ca3SEd Maste avoid_nodebug = false;
640cac4ca3SEd Maste break;
650cac4ca3SEd Maste case eLazyBoolCalculate:
66cfca06d7SDimitry Andric avoid_nodebug = thread.GetStepInAvoidsNoDebug();
670cac4ca3SEd Maste break;
680cac4ca3SEd Maste }
690cac4ca3SEd Maste if (avoid_nodebug)
700cac4ca3SEd Maste GetFlags().Set(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
710cac4ca3SEd Maste else
720cac4ca3SEd Maste GetFlags().Clear(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
730cac4ca3SEd Maste
7414f1b3e8SDimitry Andric switch (step_out_avoids_code_without_debug_info) {
750cac4ca3SEd Maste case eLazyBoolYes:
760cac4ca3SEd Maste avoid_nodebug = true;
770cac4ca3SEd Maste break;
780cac4ca3SEd Maste case eLazyBoolNo:
790cac4ca3SEd Maste avoid_nodebug = false;
800cac4ca3SEd Maste break;
810cac4ca3SEd Maste case eLazyBoolCalculate:
82cfca06d7SDimitry Andric avoid_nodebug = thread.GetStepOutAvoidsNoDebug();
830cac4ca3SEd Maste break;
840cac4ca3SEd Maste }
850cac4ca3SEd Maste if (avoid_nodebug)
860cac4ca3SEd Maste GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
870cac4ca3SEd Maste else
880cac4ca3SEd Maste GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
890cac4ca3SEd Maste }
900cac4ca3SEd Maste
GetDescription(Stream * s,lldb::DescriptionLevel level)9114f1b3e8SDimitry Andric void ThreadPlanStepInRange::GetDescription(Stream *s,
9214f1b3e8SDimitry Andric lldb::DescriptionLevel level) {
9394994d37SDimitry Andric
9494994d37SDimitry Andric auto PrintFailureIfAny = [&]() {
9594994d37SDimitry Andric if (m_status.Success())
9694994d37SDimitry Andric return;
9794994d37SDimitry Andric s->Printf(" failed (%s)", m_status.AsCString());
9894994d37SDimitry Andric };
9994994d37SDimitry Andric
10014f1b3e8SDimitry Andric if (level == lldb::eDescriptionLevelBrief) {
101205afe67SEd Maste s->Printf("step in");
10294994d37SDimitry Andric PrintFailureIfAny();
103205afe67SEd Maste return;
104205afe67SEd Maste }
105205afe67SEd Maste
106205afe67SEd Maste s->Printf("Stepping in");
107205afe67SEd Maste bool printed_line_info = false;
10814f1b3e8SDimitry Andric if (m_addr_context.line_entry.IsValid()) {
109205afe67SEd Maste s->Printf(" through line ");
110205afe67SEd Maste m_addr_context.line_entry.DumpStopContext(s, false);
111205afe67SEd Maste printed_line_info = true;
112205afe67SEd Maste }
113205afe67SEd Maste
114f034231aSEd Maste const char *step_into_target = m_step_into_target.AsCString();
115f034231aSEd Maste if (step_into_target && step_into_target[0] != '\0')
116205afe67SEd Maste s->Printf(" targeting %s", m_step_into_target.AsCString());
117205afe67SEd Maste
11814f1b3e8SDimitry Andric if (!printed_line_info || level == eDescriptionLevelVerbose) {
119205afe67SEd Maste s->Printf(" using ranges:");
120205afe67SEd Maste DumpRanges(s);
121f034231aSEd Maste }
122205afe67SEd Maste
12394994d37SDimitry Andric PrintFailureIfAny();
12494994d37SDimitry Andric
125205afe67SEd Maste s->PutChar('.');
126f034231aSEd Maste }
127f034231aSEd Maste
ShouldStop(Event * event_ptr)12814f1b3e8SDimitry Andric bool ThreadPlanStepInRange::ShouldStop(Event *event_ptr) {
129145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
130f034231aSEd Maste
13114f1b3e8SDimitry Andric if (log) {
132f034231aSEd Maste StreamString s;
133cfca06d7SDimitry Andric DumpAddress(s.AsRawOstream(), GetThread().GetRegisterContext()->GetPC(),
134cfca06d7SDimitry Andric GetTarget().GetArchitecture().GetAddressByteSize());
135ead24645SDimitry Andric LLDB_LOGF(log, "ThreadPlanStepInRange reached %s.", s.GetData());
136f034231aSEd Maste }
137f034231aSEd Maste
138f034231aSEd Maste if (IsPlanComplete())
139f034231aSEd Maste return true;
140f034231aSEd Maste
141f034231aSEd Maste m_no_more_plans = false;
14214f1b3e8SDimitry Andric if (m_sub_plan_sp && m_sub_plan_sp->IsPlanComplete()) {
14314f1b3e8SDimitry Andric if (!m_sub_plan_sp->PlanSucceeded()) {
144f034231aSEd Maste SetPlanComplete();
145f034231aSEd Maste m_no_more_plans = true;
146f034231aSEd Maste return true;
14714f1b3e8SDimitry Andric } else
148f034231aSEd Maste m_sub_plan_sp.reset();
149f034231aSEd Maste }
150f034231aSEd Maste
15114f1b3e8SDimitry Andric if (m_virtual_step) {
15214f1b3e8SDimitry Andric // If we've just completed a virtual step, all we need to do is check for a
153f73363f1SDimitry Andric // ShouldStopHere plan, and otherwise we're done.
15414f1b3e8SDimitry Andric // FIXME - This can be both a step in and a step out. Probably should
15514f1b3e8SDimitry Andric // record which in the m_virtual_step.
15694994d37SDimitry Andric m_sub_plan_sp =
15794994d37SDimitry Andric CheckShouldStopHereAndQueueStepOut(eFrameCompareYounger, m_status);
15814f1b3e8SDimitry Andric } else {
15914f1b3e8SDimitry Andric // Stepping through should be done running other threads in general, since
160f73363f1SDimitry Andric // we're setting a breakpoint and continuing. So only stop others if we
161f73363f1SDimitry Andric // are explicitly told to do so.
162f034231aSEd Maste
163e81d9d49SDimitry Andric bool stop_others = (m_stop_others == lldb::eOnlyThisThread);
164f034231aSEd Maste
165f034231aSEd Maste FrameComparison frame_order = CompareCurrentFrameToStartFrame();
166f034231aSEd Maste
167cfca06d7SDimitry Andric Thread &thread = GetThread();
16814f1b3e8SDimitry Andric if (frame_order == eFrameCompareOlder ||
16914f1b3e8SDimitry Andric frame_order == eFrameCompareSameParent) {
170f034231aSEd Maste // If we're in an older frame then we should stop.
171f034231aSEd Maste //
17214f1b3e8SDimitry Andric // A caveat to this is if we think the frame is older but we're actually
17314f1b3e8SDimitry Andric // in a trampoline.
17414f1b3e8SDimitry Andric // I'm going to make the assumption that you wouldn't RETURN to a
175f73363f1SDimitry Andric // trampoline. So if we are in a trampoline we think the frame is older
176f73363f1SDimitry Andric // because the trampoline confused the backtracer.
177cfca06d7SDimitry Andric m_sub_plan_sp = thread.QueueThreadPlanForStepThrough(
17894994d37SDimitry Andric m_stack_id, false, stop_others, m_status);
17914f1b3e8SDimitry Andric if (!m_sub_plan_sp) {
1800cac4ca3SEd Maste // Otherwise check the ShouldStopHere for step out:
18194994d37SDimitry Andric m_sub_plan_sp =
18294994d37SDimitry Andric CheckShouldStopHereAndQueueStepOut(frame_order, m_status);
183ef5d0b5eSDimitry Andric if (log) {
184ef5d0b5eSDimitry Andric if (m_sub_plan_sp)
185ead24645SDimitry Andric LLDB_LOGF(log,
186ead24645SDimitry Andric "ShouldStopHere found plan to step out of this frame.");
187ef5d0b5eSDimitry Andric else
188ead24645SDimitry Andric LLDB_LOGF(log, "ShouldStopHere no plan to step out of this frame.");
189ef5d0b5eSDimitry Andric }
19014f1b3e8SDimitry Andric } else if (log) {
191ead24645SDimitry Andric LLDB_LOGF(
192ead24645SDimitry Andric log, "Thought I stepped out, but in fact arrived at a trampoline.");
1930cac4ca3SEd Maste }
19414f1b3e8SDimitry Andric } else if (frame_order == eFrameCompareEqual && InSymbol()) {
195f73363f1SDimitry Andric // If we are not in a place we should step through, we're done. One
196f73363f1SDimitry Andric // tricky bit here is that some stubs don't push a frame, so we have to
197f73363f1SDimitry Andric // check both the case of a frame that is younger, or the same as this
198f73363f1SDimitry Andric // frame. However, if the frame is the same, and we are still in the
199f73363f1SDimitry Andric // symbol we started in, the we don't need to do this. This first check
200f73363f1SDimitry Andric // isn't strictly necessary, but it is more efficient.
201f034231aSEd Maste
20214f1b3e8SDimitry Andric // If we're still in the range, keep going, either by running to the next
203f73363f1SDimitry Andric // branch breakpoint, or by stepping.
20414f1b3e8SDimitry Andric if (InRange()) {
205f034231aSEd Maste SetNextBranchBreakpoint();
206f034231aSEd Maste return false;
207f034231aSEd Maste }
208f034231aSEd Maste
209f034231aSEd Maste SetPlanComplete();
210f034231aSEd Maste m_no_more_plans = true;
211f034231aSEd Maste return true;
212f034231aSEd Maste }
213f034231aSEd Maste
21414f1b3e8SDimitry Andric // If we get to this point, we're not going to use a previously set "next
21514f1b3e8SDimitry Andric // branch" breakpoint, so delete it:
216f034231aSEd Maste ClearNextBranchBreakpoint();
217f034231aSEd Maste
218f034231aSEd Maste // We may have set the plan up above in the FrameIsOlder section:
219f034231aSEd Maste
220f034231aSEd Maste if (!m_sub_plan_sp)
221cfca06d7SDimitry Andric m_sub_plan_sp = thread.QueueThreadPlanForStepThrough(
22294994d37SDimitry Andric m_stack_id, false, stop_others, m_status);
223f034231aSEd Maste
22414f1b3e8SDimitry Andric if (log) {
225f034231aSEd Maste if (m_sub_plan_sp)
226ead24645SDimitry Andric LLDB_LOGF(log, "Found a step through plan: %s",
227ead24645SDimitry Andric m_sub_plan_sp->GetName());
228f034231aSEd Maste else
229ead24645SDimitry Andric LLDB_LOGF(log, "No step through plan found.");
230f034231aSEd Maste }
231f034231aSEd Maste
232f73363f1SDimitry Andric // If not, give the "should_stop" callback a chance to push a plan to get
233f73363f1SDimitry Andric // us out of here. But only do that if we actually have stepped in.
234f034231aSEd Maste if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
23594994d37SDimitry Andric m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order, m_status);
236f034231aSEd Maste
23714f1b3e8SDimitry Andric // If we've stepped in and we are going to stop here, check to see if we
238f73363f1SDimitry Andric // were asked to run past the prologue, and if so do that.
239f034231aSEd Maste
24014f1b3e8SDimitry Andric if (!m_sub_plan_sp && frame_order == eFrameCompareYounger &&
24114f1b3e8SDimitry Andric m_step_past_prologue) {
242cfca06d7SDimitry Andric lldb::StackFrameSP curr_frame = thread.GetStackFrameAtIndex(0);
24314f1b3e8SDimitry Andric if (curr_frame) {
244f034231aSEd Maste size_t bytes_to_skip = 0;
245cfca06d7SDimitry Andric lldb::addr_t curr_addr = thread.GetRegisterContext()->GetPC();
246f034231aSEd Maste Address func_start_address;
247f034231aSEd Maste
24814f1b3e8SDimitry Andric SymbolContext sc = curr_frame->GetSymbolContext(eSymbolContextFunction |
24914f1b3e8SDimitry Andric eSymbolContextSymbol);
250f034231aSEd Maste
25114f1b3e8SDimitry Andric if (sc.function) {
252f034231aSEd Maste func_start_address = sc.function->GetAddressRange().GetBaseAddress();
253cfca06d7SDimitry Andric if (curr_addr == func_start_address.GetLoadAddress(&GetTarget()))
254f034231aSEd Maste bytes_to_skip = sc.function->GetPrologueByteSize();
25514f1b3e8SDimitry Andric } else if (sc.symbol) {
256f034231aSEd Maste func_start_address = sc.symbol->GetAddress();
257cfca06d7SDimitry Andric if (curr_addr == func_start_address.GetLoadAddress(&GetTarget()))
258f034231aSEd Maste bytes_to_skip = sc.symbol->GetPrologueByteSize();
259f034231aSEd Maste }
260f034231aSEd Maste
261f73363f1SDimitry Andric if (bytes_to_skip == 0 && sc.symbol) {
262cfca06d7SDimitry Andric const Architecture *arch = GetTarget().GetArchitecturePlugin();
263f73363f1SDimitry Andric if (arch) {
264f73363f1SDimitry Andric Address curr_sec_addr;
265cfca06d7SDimitry Andric GetTarget().GetSectionLoadList().ResolveLoadAddress(curr_addr,
266f73363f1SDimitry Andric curr_sec_addr);
267f73363f1SDimitry Andric bytes_to_skip = arch->GetBytesToSkip(*sc.symbol, curr_sec_addr);
268f73363f1SDimitry Andric }
269f73363f1SDimitry Andric }
270f73363f1SDimitry Andric
27114f1b3e8SDimitry Andric if (bytes_to_skip != 0) {
272f034231aSEd Maste func_start_address.Slide(bytes_to_skip);
273145449b1SDimitry Andric log = GetLog(LLDBLog::Step);
274ead24645SDimitry Andric LLDB_LOGF(log, "Pushing past prologue ");
275f034231aSEd Maste
276cfca06d7SDimitry Andric m_sub_plan_sp = thread.QueueThreadPlanForRunToAddress(
27794994d37SDimitry Andric false, func_start_address, true, m_status);
278f034231aSEd Maste }
279f034231aSEd Maste }
280f034231aSEd Maste }
281f034231aSEd Maste }
282f034231aSEd Maste
28314f1b3e8SDimitry Andric if (!m_sub_plan_sp) {
284f034231aSEd Maste m_no_more_plans = true;
285f034231aSEd Maste SetPlanComplete();
286f034231aSEd Maste return true;
28714f1b3e8SDimitry Andric } else {
288f034231aSEd Maste m_no_more_plans = false;
289205afe67SEd Maste m_sub_plan_sp->SetPrivate(true);
290f034231aSEd Maste return false;
291f034231aSEd Maste }
292f034231aSEd Maste }
293f034231aSEd Maste
SetAvoidRegexp(const char * name)29414f1b3e8SDimitry Andric void ThreadPlanStepInRange::SetAvoidRegexp(const char *name) {
295ead24645SDimitry Andric if (m_avoid_regexp_up)
296344a3780SDimitry Andric *m_avoid_regexp_up = RegularExpression(name);
297ead24645SDimitry Andric else
298344a3780SDimitry Andric m_avoid_regexp_up = std::make_unique<RegularExpression>(name);
299f034231aSEd Maste }
300f034231aSEd Maste
SetDefaultFlagValue(uint32_t new_value)30114f1b3e8SDimitry Andric void ThreadPlanStepInRange::SetDefaultFlagValue(uint32_t new_value) {
302f034231aSEd Maste // TODO: Should we test this for sanity?
303f034231aSEd Maste ThreadPlanStepInRange::s_default_flag_values = new_value;
304f034231aSEd Maste }
305f034231aSEd Maste
FrameMatchesAvoidCriteria()30614f1b3e8SDimitry Andric bool ThreadPlanStepInRange::FrameMatchesAvoidCriteria() {
307f034231aSEd Maste StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
308f034231aSEd Maste
309866dcdacSEd Maste // Check the library list first, as that's cheapest:
310866dcdacSEd Maste bool libraries_say_avoid = false;
311866dcdacSEd Maste
312866dcdacSEd Maste FileSpecList libraries_to_avoid(GetThread().GetLibrariesToAvoid());
313866dcdacSEd Maste size_t num_libraries = libraries_to_avoid.GetSize();
31414f1b3e8SDimitry Andric if (num_libraries > 0) {
315866dcdacSEd Maste SymbolContext sc(frame->GetSymbolContext(eSymbolContextModule));
316866dcdacSEd Maste FileSpec frame_library(sc.module_sp->GetFileSpec());
317866dcdacSEd Maste
31814f1b3e8SDimitry Andric if (frame_library) {
31914f1b3e8SDimitry Andric for (size_t i = 0; i < num_libraries; i++) {
320866dcdacSEd Maste const FileSpec &file_spec(libraries_to_avoid.GetFileSpecAtIndex(i));
321706b4fc4SDimitry Andric if (FileSpec::Match(file_spec, frame_library)) {
322866dcdacSEd Maste libraries_say_avoid = true;
323866dcdacSEd Maste break;
324866dcdacSEd Maste }
325866dcdacSEd Maste }
326866dcdacSEd Maste }
327866dcdacSEd Maste }
328866dcdacSEd Maste if (libraries_say_avoid)
329866dcdacSEd Maste return true;
330866dcdacSEd Maste
3315f29bb8aSDimitry Andric const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_up.get();
332e81d9d49SDimitry Andric if (avoid_regexp_to_use == nullptr)
333f034231aSEd Maste avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
334f034231aSEd Maste
33514f1b3e8SDimitry Andric if (avoid_regexp_to_use != nullptr) {
33614f1b3e8SDimitry Andric SymbolContext sc = frame->GetSymbolContext(
33714f1b3e8SDimitry Andric eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
33814f1b3e8SDimitry Andric if (sc.symbol != nullptr) {
33914f1b3e8SDimitry Andric const char *frame_function_name =
34014f1b3e8SDimitry Andric sc.GetFunctionName(Mangled::ePreferDemangledWithoutArguments)
34114f1b3e8SDimitry Andric .GetCString();
34214f1b3e8SDimitry Andric if (frame_function_name) {
343145449b1SDimitry Andric bool return_value = avoid_regexp_to_use->Execute(frame_function_name);
344145449b1SDimitry Andric if (return_value) {
345145449b1SDimitry Andric LLDB_LOGF(GetLog(LLDBLog::Step),
346145449b1SDimitry Andric "Stepping out of function \"%s\" because it matches the "
347145449b1SDimitry Andric "avoid regexp \"%s\".",
348f034231aSEd Maste frame_function_name,
349145449b1SDimitry Andric avoid_regexp_to_use->GetText().str().c_str());
350f034231aSEd Maste }
351f034231aSEd Maste return return_value;
352f034231aSEd Maste }
353f034231aSEd Maste }
354f034231aSEd Maste }
355f034231aSEd Maste return false;
356f034231aSEd Maste }
357f034231aSEd Maste
DefaultShouldStopHereCallback(ThreadPlan * current_plan,Flags & flags,FrameComparison operation,Status & status,void * baton)35814f1b3e8SDimitry Andric bool ThreadPlanStepInRange::DefaultShouldStopHereCallback(
35914f1b3e8SDimitry Andric ThreadPlan *current_plan, Flags &flags, FrameComparison operation,
36094994d37SDimitry Andric Status &status, void *baton) {
3610cac4ca3SEd Maste bool should_stop_here = true;
362f034231aSEd Maste StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
363145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
364f034231aSEd Maste
36514f1b3e8SDimitry Andric // First see if the ThreadPlanShouldStopHere default implementation thinks we
36614f1b3e8SDimitry Andric // should get out of here:
36714f1b3e8SDimitry Andric should_stop_here = ThreadPlanShouldStopHere::DefaultShouldStopHereCallback(
36894994d37SDimitry Andric current_plan, flags, operation, status, baton);
3690cac4ca3SEd Maste if (!should_stop_here)
370706b4fc4SDimitry Andric return false;
371f034231aSEd Maste
37214f1b3e8SDimitry Andric if (should_stop_here && current_plan->GetKind() == eKindStepInRange &&
37314f1b3e8SDimitry Andric operation == eFrameCompareYounger) {
37414f1b3e8SDimitry Andric ThreadPlanStepInRange *step_in_range_plan =
37514f1b3e8SDimitry Andric static_cast<ThreadPlanStepInRange *>(current_plan);
37614f1b3e8SDimitry Andric if (step_in_range_plan->m_step_into_target) {
37714f1b3e8SDimitry Andric SymbolContext sc = frame->GetSymbolContext(
37814f1b3e8SDimitry Andric eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
37914f1b3e8SDimitry Andric if (sc.symbol != nullptr) {
380f73363f1SDimitry Andric // First try an exact match, since that's cheap with ConstStrings.
381f73363f1SDimitry Andric // Then do a strstr compare.
38214f1b3e8SDimitry Andric if (step_in_range_plan->m_step_into_target == sc.GetFunctionName()) {
3830cac4ca3SEd Maste should_stop_here = true;
38414f1b3e8SDimitry Andric } else {
38514f1b3e8SDimitry Andric const char *target_name =
38614f1b3e8SDimitry Andric step_in_range_plan->m_step_into_target.AsCString();
387f034231aSEd Maste const char *function_name = sc.GetFunctionName().AsCString();
388f034231aSEd Maste
389e81d9d49SDimitry Andric if (function_name == nullptr)
3900cac4ca3SEd Maste should_stop_here = false;
391e81d9d49SDimitry Andric else if (strstr(function_name, target_name) == nullptr)
3920cac4ca3SEd Maste should_stop_here = false;
393f034231aSEd Maste }
3940cac4ca3SEd Maste if (log && !should_stop_here)
395ead24645SDimitry Andric LLDB_LOGF(log,
396ead24645SDimitry Andric "Stepping out of frame %s which did not match step into "
39714f1b3e8SDimitry Andric "target %s.",
398f034231aSEd Maste sc.GetFunctionName().AsCString(),
399f034231aSEd Maste step_in_range_plan->m_step_into_target.AsCString());
400f034231aSEd Maste }
401f034231aSEd Maste }
402f034231aSEd Maste
40314f1b3e8SDimitry Andric if (should_stop_here) {
40414f1b3e8SDimitry Andric ThreadPlanStepInRange *step_in_range_plan =
40514f1b3e8SDimitry Andric static_cast<ThreadPlanStepInRange *>(current_plan);
40614f1b3e8SDimitry Andric // Don't log the should_step_out here, it's easier to do it in
40714f1b3e8SDimitry Andric // FrameMatchesAvoidCriteria.
4080cac4ca3SEd Maste should_stop_here = !step_in_range_plan->FrameMatchesAvoidCriteria();
409f034231aSEd Maste }
410f034231aSEd Maste }
411f034231aSEd Maste
4120cac4ca3SEd Maste return should_stop_here;
413f034231aSEd Maste }
414f034231aSEd Maste
DoPlanExplainsStop(Event * event_ptr)41514f1b3e8SDimitry Andric bool ThreadPlanStepInRange::DoPlanExplainsStop(Event *event_ptr) {
416f034231aSEd Maste // We always explain a stop. Either we've just done a single step, in which
417f73363f1SDimitry Andric // case we'll do our ordinary processing, or we stopped for some reason that
418f73363f1SDimitry Andric // isn't handled by our sub-plans, in which case we want to just stop right
419f73363f1SDimitry Andric // away. In general, we don't want to mark the plan as complete for
420f73363f1SDimitry Andric // unexplained stops. For instance, if you step in to some code with no debug
421f73363f1SDimitry Andric // info, so you step out and in the course of that hit a breakpoint, then you
422f73363f1SDimitry Andric // want to stop & show the user the breakpoint, but not unship the step in
423f73363f1SDimitry Andric // plan, since you still may want to complete that plan when you continue.
424f73363f1SDimitry Andric // This is particularly true when doing "step in to target function."
425f034231aSEd Maste // stepping.
426f034231aSEd Maste //
427f73363f1SDimitry Andric // The only variation is that if we are doing "step by running to next
428f73363f1SDimitry Andric // branch" in which case if we hit our branch breakpoint we don't set the
429f73363f1SDimitry Andric // plan to complete.
430f034231aSEd Maste
431e81d9d49SDimitry Andric bool return_value = false;
432f034231aSEd Maste
43314f1b3e8SDimitry Andric if (m_virtual_step) {
434f034231aSEd Maste return_value = true;
43514f1b3e8SDimitry Andric } else {
436f034231aSEd Maste StopInfoSP stop_info_sp = GetPrivateStopInfo();
43714f1b3e8SDimitry Andric if (stop_info_sp) {
438f034231aSEd Maste StopReason reason = stop_info_sp->GetStopReason();
439f034231aSEd Maste
44014f1b3e8SDimitry Andric if (reason == eStopReasonBreakpoint) {
44114f1b3e8SDimitry Andric if (NextRangeBreakpointExplainsStop(stop_info_sp)) {
442f034231aSEd Maste return_value = true;
443f034231aSEd Maste }
44414f1b3e8SDimitry Andric } else if (IsUsuallyUnexplainedStopReason(reason)) {
445145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
446f034231aSEd Maste if (log)
44714f1b3e8SDimitry Andric log->PutCString("ThreadPlanStepInRange got asked if it explains the "
44814f1b3e8SDimitry Andric "stop for some reason other than step.");
449f034231aSEd Maste return_value = false;
45014f1b3e8SDimitry Andric } else {
451f034231aSEd Maste return_value = true;
452f034231aSEd Maste }
45314f1b3e8SDimitry Andric } else
454f034231aSEd Maste return_value = true;
455f034231aSEd Maste }
456f034231aSEd Maste
457f034231aSEd Maste return return_value;
458f034231aSEd Maste }
459f034231aSEd Maste
DoWillResume(lldb::StateType resume_state,bool current_plan)46014f1b3e8SDimitry Andric bool ThreadPlanStepInRange::DoWillResume(lldb::StateType resume_state,
46114f1b3e8SDimitry Andric bool current_plan) {
4625e95aa85SEd Maste m_virtual_step = false;
46314f1b3e8SDimitry Andric if (resume_state == eStateStepping && current_plan) {
464cfca06d7SDimitry Andric Thread &thread = GetThread();
465f034231aSEd Maste // See if we are about to step over a virtual inlined call.
466cfca06d7SDimitry Andric bool step_without_resume = thread.DecrementCurrentInlinedDepth();
46714f1b3e8SDimitry Andric if (step_without_resume) {
468145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
469ead24645SDimitry Andric LLDB_LOGF(log,
470ead24645SDimitry Andric "ThreadPlanStepInRange::DoWillResume: returning false, "
47114f1b3e8SDimitry Andric "inline_depth: %d",
472cfca06d7SDimitry Andric thread.GetCurrentInlinedDepth());
473cfca06d7SDimitry Andric SetStopInfo(StopInfo::CreateStopReasonToTrace(thread));
474f034231aSEd Maste
47514f1b3e8SDimitry Andric // FIXME: Maybe it would be better to create a InlineStep stop reason, but
47614f1b3e8SDimitry Andric // then
477f034231aSEd Maste // the whole rest of the world would have to handle that stop reason.
478f034231aSEd Maste m_virtual_step = true;
479f034231aSEd Maste }
480f034231aSEd Maste return !step_without_resume;
481f034231aSEd Maste }
482f034231aSEd Maste return true;
483f034231aSEd Maste }
484f034231aSEd Maste
IsVirtualStep()48514f1b3e8SDimitry Andric bool ThreadPlanStepInRange::IsVirtualStep() { return m_virtual_step; }
486