1*0b57cec5SDimitry Andric //===-- ThreadPlanStepInRange.cpp -----------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanStepInRange.h"
10*0b57cec5SDimitry Andric #include "lldb/Core/Architecture.h"
11*0b57cec5SDimitry Andric #include "lldb/Core/Module.h"
12*0b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
13*0b57cec5SDimitry Andric #include "lldb/Symbol/Symbol.h"
14*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
15*0b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
16*0b57cec5SDimitry Andric #include "lldb/Target/SectionLoadList.h"
17*0b57cec5SDimitry Andric #include "lldb/Target/Target.h"
18*0b57cec5SDimitry Andric #include "lldb/Target/Thread.h"
19*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanStepOut.h"
20*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanStepThrough.h"
21*0b57cec5SDimitry Andric #include "lldb/Utility/LLDBLog.h"
22*0b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
23*0b57cec5SDimitry Andric #include "lldb/Utility/RegularExpression.h"
24*0b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
25*0b57cec5SDimitry Andric 
26*0b57cec5SDimitry Andric using namespace lldb;
27*0b57cec5SDimitry Andric using namespace lldb_private;
28*0b57cec5SDimitry Andric 
29*0b57cec5SDimitry Andric uint32_t ThreadPlanStepInRange::s_default_flag_values =
30*0b57cec5SDimitry Andric     ThreadPlanShouldStopHere::eStepInAvoidNoDebug;
31*0b57cec5SDimitry Andric 
32*0b57cec5SDimitry Andric // ThreadPlanStepInRange: Step through a stack range, either stepping over or
33*0b57cec5SDimitry Andric // into based on the value of \a type.
34*0b57cec5SDimitry Andric 
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)35*0b57cec5SDimitry Andric ThreadPlanStepInRange::ThreadPlanStepInRange(
36*0b57cec5SDimitry Andric     Thread &thread, const AddressRange &range,
37*0b57cec5SDimitry Andric     const SymbolContext &addr_context, const char *step_into_target,
38*0b57cec5SDimitry Andric     lldb::RunMode stop_others, LazyBool step_in_avoids_code_without_debug_info,
39*0b57cec5SDimitry Andric     LazyBool step_out_avoids_code_without_debug_info)
40*0b57cec5SDimitry Andric     : ThreadPlanStepRange(ThreadPlan::eKindStepInRange,
41*0b57cec5SDimitry Andric                           "Step Range stepping in", thread, range, addr_context,
42*0b57cec5SDimitry Andric                           stop_others),
43*0b57cec5SDimitry Andric       ThreadPlanShouldStopHere(this), m_step_past_prologue(true),
44*0b57cec5SDimitry Andric       m_virtual_step(false), m_step_into_target(step_into_target) {
45*0b57cec5SDimitry Andric   SetCallbacks();
46*0b57cec5SDimitry Andric   SetFlagsToDefault();
47*0b57cec5SDimitry Andric   SetupAvoidNoDebug(step_in_avoids_code_without_debug_info,
48*0b57cec5SDimitry Andric                     step_out_avoids_code_without_debug_info);
49*0b57cec5SDimitry Andric }
50*0b57cec5SDimitry Andric 
51*0b57cec5SDimitry Andric ThreadPlanStepInRange::~ThreadPlanStepInRange() = default;
52*0b57cec5SDimitry Andric 
SetupAvoidNoDebug(LazyBool step_in_avoids_code_without_debug_info,LazyBool step_out_avoids_code_without_debug_info)53*0b57cec5SDimitry Andric void ThreadPlanStepInRange::SetupAvoidNoDebug(
54*0b57cec5SDimitry Andric     LazyBool step_in_avoids_code_without_debug_info,
55*0b57cec5SDimitry Andric     LazyBool step_out_avoids_code_without_debug_info) {
56*0b57cec5SDimitry Andric   bool avoid_nodebug = true;
57*0b57cec5SDimitry Andric   Thread &thread = GetThread();
58*0b57cec5SDimitry Andric   switch (step_in_avoids_code_without_debug_info) {
59*0b57cec5SDimitry Andric   case eLazyBoolYes:
60*0b57cec5SDimitry Andric     avoid_nodebug = true;
61*0b57cec5SDimitry Andric     break;
62*0b57cec5SDimitry Andric   case eLazyBoolNo:
63*0b57cec5SDimitry Andric     avoid_nodebug = false;
64*0b57cec5SDimitry Andric     break;
65*0b57cec5SDimitry Andric   case eLazyBoolCalculate:
66*0b57cec5SDimitry Andric     avoid_nodebug = thread.GetStepInAvoidsNoDebug();
67*0b57cec5SDimitry Andric     break;
68*0b57cec5SDimitry Andric   }
69*0b57cec5SDimitry Andric   if (avoid_nodebug)
70*0b57cec5SDimitry Andric     GetFlags().Set(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
71*0b57cec5SDimitry Andric   else
72*0b57cec5SDimitry Andric     GetFlags().Clear(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
73*0b57cec5SDimitry Andric 
74*0b57cec5SDimitry Andric   switch (step_out_avoids_code_without_debug_info) {
75*0b57cec5SDimitry Andric   case eLazyBoolYes:
76*0b57cec5SDimitry Andric     avoid_nodebug = true;
77*0b57cec5SDimitry Andric     break;
78*0b57cec5SDimitry Andric   case eLazyBoolNo:
79*0b57cec5SDimitry Andric     avoid_nodebug = false;
80*0b57cec5SDimitry Andric     break;
81*0b57cec5SDimitry Andric   case eLazyBoolCalculate:
82*0b57cec5SDimitry Andric     avoid_nodebug = thread.GetStepOutAvoidsNoDebug();
83*0b57cec5SDimitry Andric     break;
84*0b57cec5SDimitry Andric   }
85*0b57cec5SDimitry Andric   if (avoid_nodebug)
86*0b57cec5SDimitry Andric     GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
87*0b57cec5SDimitry Andric   else
88*0b57cec5SDimitry Andric     GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
89*0b57cec5SDimitry Andric }
90*0b57cec5SDimitry Andric 
GetDescription(Stream * s,lldb::DescriptionLevel level)91*0b57cec5SDimitry Andric void ThreadPlanStepInRange::GetDescription(Stream *s,
92*0b57cec5SDimitry Andric                                            lldb::DescriptionLevel level) {
93*0b57cec5SDimitry Andric 
94*0b57cec5SDimitry Andric   auto PrintFailureIfAny = [&]() {
95*0b57cec5SDimitry Andric     if (m_status.Success())
96*0b57cec5SDimitry Andric       return;
97*0b57cec5SDimitry Andric     s->Printf(" failed (%s)", m_status.AsCString());
98*0b57cec5SDimitry Andric   };
99*0b57cec5SDimitry Andric 
100*0b57cec5SDimitry Andric   if (level == lldb::eDescriptionLevelBrief) {
101*0b57cec5SDimitry Andric     s->Printf("step in");
102*0b57cec5SDimitry Andric     PrintFailureIfAny();
103*0b57cec5SDimitry Andric     return;
104*0b57cec5SDimitry Andric   }
105*0b57cec5SDimitry Andric 
106*0b57cec5SDimitry Andric   s->Printf("Stepping in");
107*0b57cec5SDimitry Andric   bool printed_line_info = false;
108*0b57cec5SDimitry Andric   if (m_addr_context.line_entry.IsValid()) {
109*0b57cec5SDimitry Andric     s->Printf(" through line ");
110*0b57cec5SDimitry Andric     m_addr_context.line_entry.DumpStopContext(s, false);
111*0b57cec5SDimitry Andric     printed_line_info = true;
112*0b57cec5SDimitry Andric   }
113*0b57cec5SDimitry Andric 
114*0b57cec5SDimitry Andric   const char *step_into_target = m_step_into_target.AsCString();
115*0b57cec5SDimitry Andric   if (step_into_target && step_into_target[0] != '\0')
116*0b57cec5SDimitry Andric     s->Printf(" targeting %s", m_step_into_target.AsCString());
117*0b57cec5SDimitry Andric 
118*0b57cec5SDimitry Andric   if (!printed_line_info || level == eDescriptionLevelVerbose) {
119*0b57cec5SDimitry Andric     s->Printf(" using ranges:");
120*0b57cec5SDimitry Andric     DumpRanges(s);
121*0b57cec5SDimitry Andric   }
122*0b57cec5SDimitry Andric 
123*0b57cec5SDimitry Andric   PrintFailureIfAny();
124*0b57cec5SDimitry Andric 
125*0b57cec5SDimitry Andric   s->PutChar('.');
126*0b57cec5SDimitry Andric }
127*0b57cec5SDimitry Andric 
ShouldStop(Event * event_ptr)128*0b57cec5SDimitry Andric bool ThreadPlanStepInRange::ShouldStop(Event *event_ptr) {
129*0b57cec5SDimitry Andric   Log *log = GetLog(LLDBLog::Step);
130*0b57cec5SDimitry Andric 
131*0b57cec5SDimitry Andric   if (log) {
132*0b57cec5SDimitry Andric     StreamString s;
133*0b57cec5SDimitry Andric     DumpAddress(s.AsRawOstream(), GetThread().GetRegisterContext()->GetPC(),
134*0b57cec5SDimitry Andric                 GetTarget().GetArchitecture().GetAddressByteSize());
135*0b57cec5SDimitry Andric     LLDB_LOGF(log, "ThreadPlanStepInRange reached %s.", s.GetData());
136*0b57cec5SDimitry Andric   }
137*0b57cec5SDimitry Andric 
138*0b57cec5SDimitry Andric   if (IsPlanComplete())
139*0b57cec5SDimitry Andric     return true;
140*0b57cec5SDimitry Andric 
141*0b57cec5SDimitry Andric   m_no_more_plans = false;
142*0b57cec5SDimitry Andric   if (m_sub_plan_sp && m_sub_plan_sp->IsPlanComplete()) {
143*0b57cec5SDimitry Andric     if (!m_sub_plan_sp->PlanSucceeded()) {
144*0b57cec5SDimitry Andric       SetPlanComplete();
145*0b57cec5SDimitry Andric       m_no_more_plans = true;
146*0b57cec5SDimitry Andric       return true;
147*0b57cec5SDimitry Andric     } else
148*0b57cec5SDimitry Andric       m_sub_plan_sp.reset();
149*0b57cec5SDimitry Andric   }
150*0b57cec5SDimitry Andric 
151*0b57cec5SDimitry Andric   if (m_virtual_step) {
152*0b57cec5SDimitry Andric     // If we've just completed a virtual step, all we need to do is check for a
153*0b57cec5SDimitry Andric     // ShouldStopHere plan, and otherwise we're done.
154*0b57cec5SDimitry Andric     // FIXME - This can be both a step in and a step out.  Probably should
155*0b57cec5SDimitry Andric     // record which in the m_virtual_step.
156*0b57cec5SDimitry Andric     m_sub_plan_sp =
157*0b57cec5SDimitry Andric         CheckShouldStopHereAndQueueStepOut(eFrameCompareYounger, m_status);
158*0b57cec5SDimitry Andric   } else {
159*0b57cec5SDimitry Andric     // Stepping through should be done running other threads in general, since
160*0b57cec5SDimitry Andric     // we're setting a breakpoint and continuing.  So only stop others if we
161*0b57cec5SDimitry Andric     // are explicitly told to do so.
162*0b57cec5SDimitry Andric 
163*0b57cec5SDimitry Andric     bool stop_others = (m_stop_others == lldb::eOnlyThisThread);
164*0b57cec5SDimitry Andric 
165*0b57cec5SDimitry Andric     FrameComparison frame_order = CompareCurrentFrameToStartFrame();
166*0b57cec5SDimitry Andric 
167*0b57cec5SDimitry Andric     Thread &thread = GetThread();
168*0b57cec5SDimitry Andric     if (frame_order == eFrameCompareOlder ||
169*0b57cec5SDimitry Andric         frame_order == eFrameCompareSameParent) {
170*0b57cec5SDimitry Andric       // If we're in an older frame then we should stop.
171*0b57cec5SDimitry Andric       //
172*0b57cec5SDimitry Andric       // A caveat to this is if we think the frame is older but we're actually
173*0b57cec5SDimitry Andric       // in a trampoline.
174*0b57cec5SDimitry Andric       // I'm going to make the assumption that you wouldn't RETURN to a
175*0b57cec5SDimitry Andric       // trampoline.  So if we are in a trampoline we think the frame is older
176*0b57cec5SDimitry Andric       // because the trampoline confused the backtracer.
177*0b57cec5SDimitry Andric       m_sub_plan_sp = thread.QueueThreadPlanForStepThrough(
178*0b57cec5SDimitry Andric           m_stack_id, false, stop_others, m_status);
179*0b57cec5SDimitry Andric       if (!m_sub_plan_sp) {
180*0b57cec5SDimitry Andric         // Otherwise check the ShouldStopHere for step out:
181*0b57cec5SDimitry Andric         m_sub_plan_sp =
182*0b57cec5SDimitry Andric             CheckShouldStopHereAndQueueStepOut(frame_order, m_status);
183*0b57cec5SDimitry Andric         if (log) {
184*0b57cec5SDimitry Andric           if (m_sub_plan_sp)
185*0b57cec5SDimitry Andric             LLDB_LOGF(log,
186*0b57cec5SDimitry Andric                       "ShouldStopHere found plan to step out of this frame.");
187*0b57cec5SDimitry Andric           else
188*0b57cec5SDimitry Andric             LLDB_LOGF(log, "ShouldStopHere no plan to step out of this frame.");
189*0b57cec5SDimitry Andric         }
190*0b57cec5SDimitry Andric       } else if (log) {
191*0b57cec5SDimitry Andric         LLDB_LOGF(
192*0b57cec5SDimitry Andric             log, "Thought I stepped out, but in fact arrived at a trampoline.");
193*0b57cec5SDimitry Andric       }
194*0b57cec5SDimitry Andric     } else if (frame_order == eFrameCompareEqual && InSymbol()) {
195*0b57cec5SDimitry Andric       // If we are not in a place we should step through, we're done. One
196*0b57cec5SDimitry Andric       // tricky bit here is that some stubs don't push a frame, so we have to
197*0b57cec5SDimitry Andric       // check both the case of a frame that is younger, or the same as this
198*0b57cec5SDimitry Andric       // frame. However, if the frame is the same, and we are still in the
199*0b57cec5SDimitry Andric       // symbol we started in, the we don't need to do this.  This first check
200*0b57cec5SDimitry Andric       // isn't strictly necessary, but it is more efficient.
201*0b57cec5SDimitry Andric 
202*0b57cec5SDimitry Andric       // If we're still in the range, keep going, either by running to the next
203*0b57cec5SDimitry Andric       // branch breakpoint, or by stepping.
204*0b57cec5SDimitry Andric       if (InRange()) {
205*0b57cec5SDimitry Andric         SetNextBranchBreakpoint();
206*0b57cec5SDimitry Andric         return false;
207*0b57cec5SDimitry Andric       }
208*0b57cec5SDimitry Andric 
209*0b57cec5SDimitry Andric       SetPlanComplete();
210*0b57cec5SDimitry Andric       m_no_more_plans = true;
211*0b57cec5SDimitry Andric       return true;
212*0b57cec5SDimitry Andric     }
213*0b57cec5SDimitry Andric 
214*0b57cec5SDimitry Andric     // If we get to this point, we're not going to use a previously set "next
215*0b57cec5SDimitry Andric     // branch" breakpoint, so delete it:
216*0b57cec5SDimitry Andric     ClearNextBranchBreakpoint();
217*0b57cec5SDimitry Andric 
218*0b57cec5SDimitry Andric     // We may have set the plan up above in the FrameIsOlder section:
219*0b57cec5SDimitry Andric 
220*0b57cec5SDimitry Andric     if (!m_sub_plan_sp)
221*0b57cec5SDimitry Andric       m_sub_plan_sp = thread.QueueThreadPlanForStepThrough(
222*0b57cec5SDimitry Andric           m_stack_id, false, stop_others, m_status);
223*0b57cec5SDimitry Andric 
224*0b57cec5SDimitry Andric     if (log) {
225*0b57cec5SDimitry Andric       if (m_sub_plan_sp)
226*0b57cec5SDimitry Andric         LLDB_LOGF(log, "Found a step through plan: %s",
227*0b57cec5SDimitry Andric                   m_sub_plan_sp->GetName());
228*0b57cec5SDimitry Andric       else
229*0b57cec5SDimitry Andric         LLDB_LOGF(log, "No step through plan found.");
230*0b57cec5SDimitry Andric     }
231*0b57cec5SDimitry Andric 
232*0b57cec5SDimitry Andric     // If not, give the "should_stop" callback a chance to push a plan to get
233*0b57cec5SDimitry Andric     // us out of here. But only do that if we actually have stepped in.
234*0b57cec5SDimitry Andric     if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
235*0b57cec5SDimitry Andric       m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order, m_status);
236*0b57cec5SDimitry Andric 
237*0b57cec5SDimitry Andric     // If we've stepped in and we are going to stop here, check to see if we
238*0b57cec5SDimitry Andric     // were asked to run past the prologue, and if so do that.
239*0b57cec5SDimitry Andric 
240*0b57cec5SDimitry Andric     if (!m_sub_plan_sp && frame_order == eFrameCompareYounger &&
241*0b57cec5SDimitry Andric         m_step_past_prologue) {
242*0b57cec5SDimitry Andric       lldb::StackFrameSP curr_frame = thread.GetStackFrameAtIndex(0);
243*0b57cec5SDimitry Andric       if (curr_frame) {
244*0b57cec5SDimitry Andric         size_t bytes_to_skip = 0;
245*0b57cec5SDimitry Andric         lldb::addr_t curr_addr = thread.GetRegisterContext()->GetPC();
246*0b57cec5SDimitry Andric         Address func_start_address;
247*0b57cec5SDimitry Andric 
248*0b57cec5SDimitry Andric         SymbolContext sc = curr_frame->GetSymbolContext(eSymbolContextFunction |
249*0b57cec5SDimitry Andric                                                         eSymbolContextSymbol);
250*0b57cec5SDimitry Andric 
251*0b57cec5SDimitry Andric         if (sc.function) {
252*0b57cec5SDimitry Andric           func_start_address = sc.function->GetAddressRange().GetBaseAddress();
253*0b57cec5SDimitry Andric           if (curr_addr == func_start_address.GetLoadAddress(&GetTarget()))
254*0b57cec5SDimitry Andric             bytes_to_skip = sc.function->GetPrologueByteSize();
255*0b57cec5SDimitry Andric         } else if (sc.symbol) {
256*0b57cec5SDimitry Andric           func_start_address = sc.symbol->GetAddress();
257*0b57cec5SDimitry Andric           if (curr_addr == func_start_address.GetLoadAddress(&GetTarget()))
258*0b57cec5SDimitry Andric             bytes_to_skip = sc.symbol->GetPrologueByteSize();
259*0b57cec5SDimitry Andric         }
260*0b57cec5SDimitry Andric 
261*0b57cec5SDimitry Andric         if (bytes_to_skip == 0 && sc.symbol) {
262*0b57cec5SDimitry Andric           const Architecture *arch = GetTarget().GetArchitecturePlugin();
263*0b57cec5SDimitry Andric           if (arch) {
264*0b57cec5SDimitry Andric             Address curr_sec_addr;
265*0b57cec5SDimitry Andric             GetTarget().GetSectionLoadList().ResolveLoadAddress(curr_addr,
266*0b57cec5SDimitry Andric                                                                 curr_sec_addr);
267*0b57cec5SDimitry Andric             bytes_to_skip = arch->GetBytesToSkip(*sc.symbol, curr_sec_addr);
268*0b57cec5SDimitry Andric           }
269*0b57cec5SDimitry Andric         }
270*0b57cec5SDimitry Andric 
271*0b57cec5SDimitry Andric         if (bytes_to_skip != 0) {
272*0b57cec5SDimitry Andric           func_start_address.Slide(bytes_to_skip);
273*0b57cec5SDimitry Andric           log = GetLog(LLDBLog::Step);
274*0b57cec5SDimitry Andric           LLDB_LOGF(log, "Pushing past prologue ");
275*0b57cec5SDimitry Andric 
276*0b57cec5SDimitry Andric           m_sub_plan_sp = thread.QueueThreadPlanForRunToAddress(
277*0b57cec5SDimitry Andric               false, func_start_address, true, m_status);
278*0b57cec5SDimitry Andric         }
279*0b57cec5SDimitry Andric       }
280*0b57cec5SDimitry Andric     }
281*0b57cec5SDimitry Andric   }
282*0b57cec5SDimitry Andric 
283*0b57cec5SDimitry Andric   if (!m_sub_plan_sp) {
284*0b57cec5SDimitry Andric     m_no_more_plans = true;
285*0b57cec5SDimitry Andric     SetPlanComplete();
286*0b57cec5SDimitry Andric     return true;
287*0b57cec5SDimitry Andric   } else {
288*0b57cec5SDimitry Andric     m_no_more_plans = false;
289*0b57cec5SDimitry Andric     m_sub_plan_sp->SetPrivate(true);
290*0b57cec5SDimitry Andric     return false;
291*0b57cec5SDimitry Andric   }
292*0b57cec5SDimitry Andric }
293*0b57cec5SDimitry Andric 
SetAvoidRegexp(const char * name)294*0b57cec5SDimitry Andric void ThreadPlanStepInRange::SetAvoidRegexp(const char *name) {
295*0b57cec5SDimitry Andric   if (m_avoid_regexp_up)
296*0b57cec5SDimitry Andric     *m_avoid_regexp_up = RegularExpression(name);
297*0b57cec5SDimitry Andric   else
298*0b57cec5SDimitry Andric     m_avoid_regexp_up = std::make_unique<RegularExpression>(name);
299*0b57cec5SDimitry Andric }
300*0b57cec5SDimitry Andric 
SetDefaultFlagValue(uint32_t new_value)301*0b57cec5SDimitry Andric void ThreadPlanStepInRange::SetDefaultFlagValue(uint32_t new_value) {
302*0b57cec5SDimitry Andric   // TODO: Should we test this for sanity?
303*0b57cec5SDimitry Andric   ThreadPlanStepInRange::s_default_flag_values = new_value;
304*0b57cec5SDimitry Andric }
305*0b57cec5SDimitry Andric 
FrameMatchesAvoidCriteria()306*0b57cec5SDimitry Andric bool ThreadPlanStepInRange::FrameMatchesAvoidCriteria() {
307*0b57cec5SDimitry Andric   StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
308*0b57cec5SDimitry Andric 
309*0b57cec5SDimitry Andric   // Check the library list first, as that's cheapest:
310*0b57cec5SDimitry Andric   bool libraries_say_avoid = false;
311*0b57cec5SDimitry Andric 
312*0b57cec5SDimitry Andric   FileSpecList libraries_to_avoid(GetThread().GetLibrariesToAvoid());
313*0b57cec5SDimitry Andric   size_t num_libraries = libraries_to_avoid.GetSize();
314*0b57cec5SDimitry Andric   if (num_libraries > 0) {
315*0b57cec5SDimitry Andric     SymbolContext sc(frame->GetSymbolContext(eSymbolContextModule));
316*0b57cec5SDimitry Andric     FileSpec frame_library(sc.module_sp->GetFileSpec());
317*0b57cec5SDimitry Andric 
318*0b57cec5SDimitry Andric     if (frame_library) {
319*0b57cec5SDimitry Andric       for (size_t i = 0; i < num_libraries; i++) {
320*0b57cec5SDimitry Andric         const FileSpec &file_spec(libraries_to_avoid.GetFileSpecAtIndex(i));
321*0b57cec5SDimitry Andric         if (FileSpec::Match(file_spec, frame_library)) {
322*0b57cec5SDimitry Andric           libraries_say_avoid = true;
323*0b57cec5SDimitry Andric           break;
324*0b57cec5SDimitry Andric         }
325*0b57cec5SDimitry Andric       }
326*0b57cec5SDimitry Andric     }
327*0b57cec5SDimitry Andric   }
328*0b57cec5SDimitry Andric   if (libraries_say_avoid)
329*0b57cec5SDimitry Andric     return true;
330*0b57cec5SDimitry Andric 
331*0b57cec5SDimitry Andric   const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_up.get();
332*0b57cec5SDimitry Andric   if (avoid_regexp_to_use == nullptr)
333*0b57cec5SDimitry Andric     avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
334*0b57cec5SDimitry Andric 
335*0b57cec5SDimitry Andric   if (avoid_regexp_to_use != nullptr) {
336*0b57cec5SDimitry Andric     SymbolContext sc = frame->GetSymbolContext(
337*0b57cec5SDimitry Andric         eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
338*0b57cec5SDimitry Andric     if (sc.symbol != nullptr) {
339*0b57cec5SDimitry Andric       const char *frame_function_name =
340*0b57cec5SDimitry Andric           sc.GetFunctionName(Mangled::ePreferDemangledWithoutArguments)
341*0b57cec5SDimitry Andric               .GetCString();
342*0b57cec5SDimitry Andric       if (frame_function_name) {
343*0b57cec5SDimitry Andric         bool return_value = avoid_regexp_to_use->Execute(frame_function_name);
344*0b57cec5SDimitry Andric         if (return_value) {
345*0b57cec5SDimitry Andric           LLDB_LOGF(GetLog(LLDBLog::Step),
346*0b57cec5SDimitry Andric                     "Stepping out of function \"%s\" because it matches the "
347*0b57cec5SDimitry Andric                     "avoid regexp \"%s\".",
348*0b57cec5SDimitry Andric                     frame_function_name,
349*0b57cec5SDimitry Andric                     avoid_regexp_to_use->GetText().str().c_str());
350*0b57cec5SDimitry Andric         }
351*0b57cec5SDimitry Andric         return return_value;
352*0b57cec5SDimitry Andric       }
353*0b57cec5SDimitry Andric     }
354*0b57cec5SDimitry Andric   }
355*0b57cec5SDimitry Andric   return false;
356*0b57cec5SDimitry Andric }
357*0b57cec5SDimitry Andric 
DefaultShouldStopHereCallback(ThreadPlan * current_plan,Flags & flags,FrameComparison operation,Status & status,void * baton)358*0b57cec5SDimitry Andric bool ThreadPlanStepInRange::DefaultShouldStopHereCallback(
359*0b57cec5SDimitry Andric     ThreadPlan *current_plan, Flags &flags, FrameComparison operation,
360*0b57cec5SDimitry Andric     Status &status, void *baton) {
361*0b57cec5SDimitry Andric   bool should_stop_here = true;
362*0b57cec5SDimitry Andric   StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
363*0b57cec5SDimitry Andric   Log *log = GetLog(LLDBLog::Step);
364*0b57cec5SDimitry Andric 
365*0b57cec5SDimitry Andric   // First see if the ThreadPlanShouldStopHere default implementation thinks we
366*0b57cec5SDimitry Andric   // should get out of here:
367*0b57cec5SDimitry Andric   should_stop_here = ThreadPlanShouldStopHere::DefaultShouldStopHereCallback(
368*0b57cec5SDimitry Andric       current_plan, flags, operation, status, baton);
369*0b57cec5SDimitry Andric   if (!should_stop_here)
370*0b57cec5SDimitry Andric     return false;
371*0b57cec5SDimitry Andric 
372*0b57cec5SDimitry Andric   if (should_stop_here && current_plan->GetKind() == eKindStepInRange &&
373*0b57cec5SDimitry Andric       operation == eFrameCompareYounger) {
374*0b57cec5SDimitry Andric     ThreadPlanStepInRange *step_in_range_plan =
375*0b57cec5SDimitry Andric         static_cast<ThreadPlanStepInRange *>(current_plan);
376*0b57cec5SDimitry Andric     if (step_in_range_plan->m_step_into_target) {
377*0b57cec5SDimitry Andric       SymbolContext sc = frame->GetSymbolContext(
378*0b57cec5SDimitry Andric           eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
379*0b57cec5SDimitry Andric       if (sc.symbol != nullptr) {
380*0b57cec5SDimitry Andric         // First try an exact match, since that's cheap with ConstStrings.
381*0b57cec5SDimitry Andric         // Then do a strstr compare.
382*0b57cec5SDimitry Andric         if (step_in_range_plan->m_step_into_target == sc.GetFunctionName()) {
383*0b57cec5SDimitry Andric           should_stop_here = true;
384*0b57cec5SDimitry Andric         } else {
385*0b57cec5SDimitry Andric           const char *target_name =
386*0b57cec5SDimitry Andric               step_in_range_plan->m_step_into_target.AsCString();
387*0b57cec5SDimitry Andric           const char *function_name = sc.GetFunctionName().AsCString();
388*0b57cec5SDimitry Andric 
389*0b57cec5SDimitry Andric           if (function_name == nullptr)
390*0b57cec5SDimitry Andric             should_stop_here = false;
391*0b57cec5SDimitry Andric           else if (strstr(function_name, target_name) == nullptr)
392*0b57cec5SDimitry Andric             should_stop_here = false;
393*0b57cec5SDimitry Andric         }
394*0b57cec5SDimitry Andric         if (log && !should_stop_here)
395*0b57cec5SDimitry Andric           LLDB_LOGF(log,
396*0b57cec5SDimitry Andric                     "Stepping out of frame %s which did not match step into "
397*0b57cec5SDimitry Andric                     "target %s.",
398*0b57cec5SDimitry Andric                     sc.GetFunctionName().AsCString(),
399*0b57cec5SDimitry Andric                     step_in_range_plan->m_step_into_target.AsCString());
400*0b57cec5SDimitry Andric       }
401*0b57cec5SDimitry Andric     }
402*0b57cec5SDimitry Andric 
403*0b57cec5SDimitry Andric     if (should_stop_here) {
404*0b57cec5SDimitry Andric       ThreadPlanStepInRange *step_in_range_plan =
405*0b57cec5SDimitry Andric           static_cast<ThreadPlanStepInRange *>(current_plan);
406*0b57cec5SDimitry Andric       // Don't log the should_step_out here, it's easier to do it in
407*0b57cec5SDimitry Andric       // FrameMatchesAvoidCriteria.
408*0b57cec5SDimitry Andric       should_stop_here = !step_in_range_plan->FrameMatchesAvoidCriteria();
409*0b57cec5SDimitry Andric     }
410*0b57cec5SDimitry Andric   }
411*0b57cec5SDimitry Andric 
412*0b57cec5SDimitry Andric   return should_stop_here;
413*0b57cec5SDimitry Andric }
414*0b57cec5SDimitry Andric 
DoPlanExplainsStop(Event * event_ptr)415*0b57cec5SDimitry Andric bool ThreadPlanStepInRange::DoPlanExplainsStop(Event *event_ptr) {
416*0b57cec5SDimitry Andric   // We always explain a stop.  Either we've just done a single step, in which
417*0b57cec5SDimitry Andric   // case we'll do our ordinary processing, or we stopped for some reason that
418*0b57cec5SDimitry Andric   // isn't handled by our sub-plans, in which case we want to just stop right
419*0b57cec5SDimitry Andric   // away. In general, we don't want to mark the plan as complete for
420*0b57cec5SDimitry Andric   // unexplained stops. For instance, if you step in to some code with no debug
421*0b57cec5SDimitry Andric   // info, so you step out and in the course of that hit a breakpoint, then you
422*0b57cec5SDimitry Andric   // want to stop & show the user the breakpoint, but not unship the step in
423*0b57cec5SDimitry Andric   // plan, since you still may want to complete that plan when you continue.
424*0b57cec5SDimitry Andric   // This is particularly true when doing "step in to target function."
425*0b57cec5SDimitry Andric   // stepping.
426*0b57cec5SDimitry Andric   //
427*0b57cec5SDimitry Andric   // The only variation is that if we are doing "step by running to next
428*0b57cec5SDimitry Andric   // branch" in which case if we hit our branch breakpoint we don't set the
429*0b57cec5SDimitry Andric   // plan to complete.
430*0b57cec5SDimitry Andric 
431*0b57cec5SDimitry Andric   bool return_value = false;
432*0b57cec5SDimitry Andric 
433*0b57cec5SDimitry Andric   if (m_virtual_step) {
434*0b57cec5SDimitry Andric     return_value = true;
435*0b57cec5SDimitry Andric   } else {
436*0b57cec5SDimitry Andric     StopInfoSP stop_info_sp = GetPrivateStopInfo();
437*0b57cec5SDimitry Andric     if (stop_info_sp) {
438*0b57cec5SDimitry Andric       StopReason reason = stop_info_sp->GetStopReason();
439*0b57cec5SDimitry Andric 
440*0b57cec5SDimitry Andric       if (reason == eStopReasonBreakpoint) {
441*0b57cec5SDimitry Andric         if (NextRangeBreakpointExplainsStop(stop_info_sp)) {
442*0b57cec5SDimitry Andric           return_value = true;
443*0b57cec5SDimitry Andric         }
444*0b57cec5SDimitry Andric       } else if (IsUsuallyUnexplainedStopReason(reason)) {
445*0b57cec5SDimitry Andric         Log *log = GetLog(LLDBLog::Step);
446*0b57cec5SDimitry Andric         if (log)
447*0b57cec5SDimitry Andric           log->PutCString("ThreadPlanStepInRange got asked if it explains the "
448*0b57cec5SDimitry Andric                           "stop for some reason other than step.");
449*0b57cec5SDimitry Andric         return_value = false;
450*0b57cec5SDimitry Andric       } else {
451*0b57cec5SDimitry Andric         return_value = true;
452*0b57cec5SDimitry Andric       }
453*0b57cec5SDimitry Andric     } else
454*0b57cec5SDimitry Andric       return_value = true;
455*0b57cec5SDimitry Andric   }
456*0b57cec5SDimitry Andric 
457*0b57cec5SDimitry Andric   return return_value;
458*0b57cec5SDimitry Andric }
459*0b57cec5SDimitry Andric 
DoWillResume(lldb::StateType resume_state,bool current_plan)460*0b57cec5SDimitry Andric bool ThreadPlanStepInRange::DoWillResume(lldb::StateType resume_state,
461*0b57cec5SDimitry Andric                                          bool current_plan) {
462*0b57cec5SDimitry Andric   m_virtual_step = false;
463*0b57cec5SDimitry Andric   if (resume_state == eStateStepping && current_plan) {
464*0b57cec5SDimitry Andric     Thread &thread = GetThread();
465*0b57cec5SDimitry Andric     // See if we are about to step over a virtual inlined call.
466*0b57cec5SDimitry Andric     bool step_without_resume = thread.DecrementCurrentInlinedDepth();
467*0b57cec5SDimitry Andric     if (step_without_resume) {
468*0b57cec5SDimitry Andric       Log *log = GetLog(LLDBLog::Step);
469*0b57cec5SDimitry Andric       LLDB_LOGF(log,
470*0b57cec5SDimitry Andric                 "ThreadPlanStepInRange::DoWillResume: returning false, "
471*0b57cec5SDimitry Andric                 "inline_depth: %d",
472*0b57cec5SDimitry Andric                 thread.GetCurrentInlinedDepth());
473*0b57cec5SDimitry Andric       SetStopInfo(StopInfo::CreateStopReasonToTrace(thread));
474*0b57cec5SDimitry Andric 
475*0b57cec5SDimitry Andric       // FIXME: Maybe it would be better to create a InlineStep stop reason, but
476*0b57cec5SDimitry Andric       // then
477*0b57cec5SDimitry Andric       // the whole rest of the world would have to handle that stop reason.
478*0b57cec5SDimitry Andric       m_virtual_step = true;
479*0b57cec5SDimitry Andric     }
480*0b57cec5SDimitry Andric     return !step_without_resume;
481*0b57cec5SDimitry Andric   }
482*0b57cec5SDimitry Andric   return true;
483*0b57cec5SDimitry Andric }
484*0b57cec5SDimitry Andric 
IsVirtualStep()485*0b57cec5SDimitry Andric bool ThreadPlanStepInRange::IsVirtualStep() { return m_virtual_step; }
486*0b57cec5SDimitry Andric