130fdc8d8SChris Lattner //===-- ThreadPlanStepInRange.cpp -------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1030fdc8d8SChris Lattner // C Includes
1130fdc8d8SChris Lattner // C++ Includes
1230fdc8d8SChris Lattner // Other libraries and framework includes
1330fdc8d8SChris Lattner // Project includes
14e65b2cf2SEugene Zelenko #include "lldb/Target/ThreadPlanStepInRange.h"
15*08581263SJim Ingham #include "lldb/Core/Architecture.h"
164da6206dSJim Ingham #include "lldb/Core/Module.h"
177ce490c6SJim Ingham #include "lldb/Symbol/Function.h"
18b9c1b51eSKate Stone #include "lldb/Symbol/Symbol.h"
1930fdc8d8SChris Lattner #include "lldb/Target/Process.h"
2030fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
21*08581263SJim Ingham #include "lldb/Target/SectionLoadList.h"
22514487e8SGreg Clayton #include "lldb/Target/Target.h"
2330fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
2430fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanStepOut.h"
2530fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanStepThrough.h"
266f9e6901SZachary Turner #include "lldb/Utility/Log.h"
27bf9a7730SZachary Turner #include "lldb/Utility/RegularExpression.h"
28bf9a7730SZachary Turner #include "lldb/Utility/Stream.h"
2930fdc8d8SChris Lattner 
3030fdc8d8SChris Lattner using namespace lldb;
3130fdc8d8SChris Lattner using namespace lldb_private;
3230fdc8d8SChris Lattner 
33b9c1b51eSKate Stone uint32_t ThreadPlanStepInRange::s_default_flag_values =
34b9c1b51eSKate Stone     ThreadPlanShouldStopHere::eStepInAvoidNoDebug;
3530fdc8d8SChris Lattner 
3630fdc8d8SChris Lattner //----------------------------------------------------------------------
37b9c1b51eSKate Stone // ThreadPlanStepInRange: Step through a stack range, either stepping over or
38b9c1b51eSKate Stone // into
3930fdc8d8SChris Lattner // based on the value of \a type.
4030fdc8d8SChris Lattner //----------------------------------------------------------------------
4130fdc8d8SChris Lattner 
42b9c1b51eSKate Stone ThreadPlanStepInRange::ThreadPlanStepInRange(
43b9c1b51eSKate Stone     Thread &thread, const AddressRange &range,
44b9c1b51eSKate Stone     const SymbolContext &addr_context, lldb::RunMode stop_others,
454b4b2478SJim Ingham     LazyBool step_in_avoids_code_without_debug_info,
46b9c1b51eSKate Stone     LazyBool step_out_avoids_code_without_debug_info)
47b9c1b51eSKate Stone     : ThreadPlanStepRange(ThreadPlan::eKindStepInRange,
48b9c1b51eSKate Stone                           "Step Range stepping in", thread, range, addr_context,
49b9c1b51eSKate Stone                           stop_others),
50b9c1b51eSKate Stone       ThreadPlanShouldStopHere(this), m_step_past_prologue(true),
51b9c1b51eSKate Stone       m_virtual_step(false) {
524b4b2478SJim Ingham   SetCallbacks();
5330fdc8d8SChris Lattner   SetFlagsToDefault();
54b9c1b51eSKate Stone   SetupAvoidNoDebug(step_in_avoids_code_without_debug_info,
55b9c1b51eSKate Stone                     step_out_avoids_code_without_debug_info);
5630fdc8d8SChris Lattner }
5730fdc8d8SChris Lattner 
58b9c1b51eSKate Stone ThreadPlanStepInRange::ThreadPlanStepInRange(
59b9c1b51eSKate Stone     Thread &thread, const AddressRange &range,
60b9c1b51eSKate Stone     const SymbolContext &addr_context, const char *step_into_target,
61b9c1b51eSKate Stone     lldb::RunMode stop_others, LazyBool step_in_avoids_code_without_debug_info,
62b9c1b51eSKate Stone     LazyBool step_out_avoids_code_without_debug_info)
63b9c1b51eSKate Stone     : ThreadPlanStepRange(ThreadPlan::eKindStepInRange,
64b9c1b51eSKate Stone                           "Step Range stepping in", thread, range, addr_context,
65b9c1b51eSKate Stone                           stop_others),
66b9c1b51eSKate Stone       ThreadPlanShouldStopHere(this), m_step_past_prologue(true),
67b9c1b51eSKate Stone       m_virtual_step(false), m_step_into_target(step_into_target) {
684b4b2478SJim Ingham   SetCallbacks();
69c627682eSJim Ingham   SetFlagsToDefault();
70b9c1b51eSKate Stone   SetupAvoidNoDebug(step_in_avoids_code_without_debug_info,
71b9c1b51eSKate Stone                     step_out_avoids_code_without_debug_info);
72c627682eSJim Ingham }
73c627682eSJim Ingham 
74e65b2cf2SEugene Zelenko ThreadPlanStepInRange::~ThreadPlanStepInRange() = default;
7530fdc8d8SChris Lattner 
76b9c1b51eSKate Stone void ThreadPlanStepInRange::SetupAvoidNoDebug(
77b9c1b51eSKate Stone     LazyBool step_in_avoids_code_without_debug_info,
78b9c1b51eSKate Stone     LazyBool step_out_avoids_code_without_debug_info) {
794b4b2478SJim Ingham   bool avoid_nodebug = true;
804b4b2478SJim Ingham 
81b9c1b51eSKate Stone   switch (step_in_avoids_code_without_debug_info) {
824b4b2478SJim Ingham   case eLazyBoolYes:
834b4b2478SJim Ingham     avoid_nodebug = true;
844b4b2478SJim Ingham     break;
854b4b2478SJim Ingham   case eLazyBoolNo:
864b4b2478SJim Ingham     avoid_nodebug = false;
874b4b2478SJim Ingham     break;
884b4b2478SJim Ingham   case eLazyBoolCalculate:
894b4b2478SJim Ingham     avoid_nodebug = m_thread.GetStepInAvoidsNoDebug();
904b4b2478SJim Ingham     break;
914b4b2478SJim Ingham   }
924b4b2478SJim Ingham   if (avoid_nodebug)
934b4b2478SJim Ingham     GetFlags().Set(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
944b4b2478SJim Ingham   else
954b4b2478SJim Ingham     GetFlags().Clear(ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
964b4b2478SJim Ingham 
97b9c1b51eSKate Stone   switch (step_out_avoids_code_without_debug_info) {
984b4b2478SJim Ingham   case eLazyBoolYes:
994b4b2478SJim Ingham     avoid_nodebug = true;
1004b4b2478SJim Ingham     break;
1014b4b2478SJim Ingham   case eLazyBoolNo:
1024b4b2478SJim Ingham     avoid_nodebug = false;
1034b4b2478SJim Ingham     break;
1044b4b2478SJim Ingham   case eLazyBoolCalculate:
1054b4b2478SJim Ingham     avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
1064b4b2478SJim Ingham     break;
1074b4b2478SJim Ingham   }
1084b4b2478SJim Ingham   if (avoid_nodebug)
1094b4b2478SJim Ingham     GetFlags().Set(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1104b4b2478SJim Ingham   else
1114b4b2478SJim Ingham     GetFlags().Clear(ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
1124b4b2478SJim Ingham }
1134b4b2478SJim Ingham 
114b9c1b51eSKate Stone void ThreadPlanStepInRange::GetDescription(Stream *s,
115b9c1b51eSKate Stone                                            lldb::DescriptionLevel level) {
116b9c1b51eSKate Stone   if (level == lldb::eDescriptionLevelBrief) {
1172bdbfd50SJim Ingham     s->Printf("step in");
1182bdbfd50SJim Ingham     return;
1192bdbfd50SJim Ingham   }
1202bdbfd50SJim Ingham 
1212bdbfd50SJim Ingham   s->Printf("Stepping in");
1222bdbfd50SJim Ingham   bool printed_line_info = false;
123b9c1b51eSKate Stone   if (m_addr_context.line_entry.IsValid()) {
1242bdbfd50SJim Ingham     s->Printf(" through line ");
1252bdbfd50SJim Ingham     m_addr_context.line_entry.DumpStopContext(s, false);
1262bdbfd50SJim Ingham     printed_line_info = true;
1272bdbfd50SJim Ingham   }
1282bdbfd50SJim Ingham 
1294d56e9c1SJim Ingham   const char *step_into_target = m_step_into_target.AsCString();
1304d56e9c1SJim Ingham   if (step_into_target && step_into_target[0] != '\0')
1312bdbfd50SJim Ingham     s->Printf(" targeting %s", m_step_into_target.AsCString());
1322bdbfd50SJim Ingham 
133b9c1b51eSKate Stone   if (!printed_line_info || level == eDescriptionLevelVerbose) {
1342bdbfd50SJim Ingham     s->Printf(" using ranges:");
1352bdbfd50SJim Ingham     DumpRanges(s);
13630fdc8d8SChris Lattner   }
1372bdbfd50SJim Ingham 
1382bdbfd50SJim Ingham   s->PutChar('.');
13930fdc8d8SChris Lattner }
14030fdc8d8SChris Lattner 
141b9c1b51eSKate Stone bool ThreadPlanStepInRange::ShouldStop(Event *event_ptr) {
1425160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
14330fdc8d8SChris Lattner 
144b9c1b51eSKate Stone   if (log) {
14530fdc8d8SChris Lattner     StreamString s;
146b9c1b51eSKate Stone     s.Address(
147b9c1b51eSKate Stone         m_thread.GetRegisterContext()->GetPC(),
1481ac04c30SGreg Clayton         m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
14930fdc8d8SChris Lattner     log->Printf("ThreadPlanStepInRange reached %s.", s.GetData());
15030fdc8d8SChris Lattner   }
15130fdc8d8SChris Lattner 
15225f66700SJim Ingham   if (IsPlanComplete())
15325f66700SJim Ingham     return true;
15425f66700SJim Ingham 
1554d56e9c1SJim Ingham   m_no_more_plans = false;
156b9c1b51eSKate Stone   if (m_sub_plan_sp && m_sub_plan_sp->IsPlanComplete()) {
157b9c1b51eSKate Stone     if (!m_sub_plan_sp->PlanSucceeded()) {
1584d56e9c1SJim Ingham       SetPlanComplete();
1594d56e9c1SJim Ingham       m_no_more_plans = true;
1604d56e9c1SJim Ingham       return true;
161b9c1b51eSKate Stone     } else
1624d56e9c1SJim Ingham       m_sub_plan_sp.reset();
1634d56e9c1SJim Ingham   }
16430fdc8d8SChris Lattner 
165b9c1b51eSKate Stone   if (m_virtual_step) {
166b9c1b51eSKate Stone     // If we've just completed a virtual step, all we need to do is check for a
167b9c1b51eSKate Stone     // ShouldStopHere plan, and otherwise
168f02a2e96SJim Ingham     // we're done.
169b9c1b51eSKate Stone     // FIXME - This can be both a step in and a step out.  Probably should
170b9c1b51eSKate Stone     // record which in the m_virtual_step.
1714b4b2478SJim Ingham     m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(eFrameCompareYounger);
172b9c1b51eSKate Stone   } else {
173b9c1b51eSKate Stone     // Stepping through should be done running other threads in general, since
174b9c1b51eSKate Stone     // we're setting a breakpoint and
1754a58e968SJim Ingham     // continuing.  So only stop others if we are explicitly told to do so.
1769d790c5dSJim Ingham 
177e65b2cf2SEugene Zelenko     bool stop_others = (m_stop_others == lldb::eOnlyThisThread);
17830fdc8d8SChris Lattner 
179b5c0d1ccSJim Ingham     FrameComparison frame_order = CompareCurrentFrameToStartFrame();
180b5c0d1ccSJim Ingham 
181b9c1b51eSKate Stone     if (frame_order == eFrameCompareOlder ||
182b9c1b51eSKate Stone         frame_order == eFrameCompareSameParent) {
1835822173bSJim Ingham       // If we're in an older frame then we should stop.
1845822173bSJim Ingham       //
185b9c1b51eSKate Stone       // A caveat to this is if we think the frame is older but we're actually
186b9c1b51eSKate Stone       // in a trampoline.
187b9c1b51eSKate Stone       // I'm going to make the assumption that you wouldn't RETURN to a
188b9c1b51eSKate Stone       // trampoline.  So if we are
189b9c1b51eSKate Stone       // in a trampoline we think the frame is older because the trampoline
190b9c1b51eSKate Stone       // confused the backtracer.
191b9c1b51eSKate Stone       m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough(m_stack_id, false,
192b9c1b51eSKate Stone                                                              stop_others);
193b9c1b51eSKate Stone       if (!m_sub_plan_sp) {
1944b4b2478SJim Ingham         // Otherwise check the ShouldStopHere for step out:
195862d1bbdSJim Ingham         m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order);
196a4bb80b2SJim Ingham         if (log) {
197a4bb80b2SJim Ingham           if (m_sub_plan_sp)
198a4bb80b2SJim Ingham             log->Printf("ShouldStopHere found plan to step out of this frame.");
199a4bb80b2SJim Ingham           else
200a4bb80b2SJim Ingham             log->Printf("ShouldStopHere no plan to step out of this frame.");
201a4bb80b2SJim Ingham         }
202b9c1b51eSKate Stone       } else if (log) {
203b9c1b51eSKate Stone         log->Printf(
204b9c1b51eSKate Stone             "Thought I stepped out, but in fact arrived at a trampoline.");
2054b4b2478SJim Ingham       }
206b9c1b51eSKate Stone     } else if (frame_order == eFrameCompareEqual && InSymbol()) {
2075822173bSJim Ingham       // If we are not in a place we should step through, we're done.
208b9c1b51eSKate Stone       // One tricky bit here is that some stubs don't push a frame, so we have
209b9c1b51eSKate Stone       // to check
2105822173bSJim Ingham       // both the case of a frame that is younger, or the same as this frame.
211b9c1b51eSKate Stone       // However, if the frame is the same, and we are still in the symbol we
212b9c1b51eSKate Stone       // started
213b9c1b51eSKate Stone       // in, the we don't need to do this.  This first check isn't strictly
214b9c1b51eSKate Stone       // necessary,
2155822173bSJim Ingham       // but it is more efficient.
2165822173bSJim Ingham 
217b9c1b51eSKate Stone       // If we're still in the range, keep going, either by running to the next
218b9c1b51eSKate Stone       // branch breakpoint, or by
219564d8bc2SJim Ingham       // stepping.
220b9c1b51eSKate Stone       if (InRange()) {
221564d8bc2SJim Ingham         SetNextBranchBreakpoint();
222564d8bc2SJim Ingham         return false;
223564d8bc2SJim Ingham       }
224564d8bc2SJim Ingham 
2255822173bSJim Ingham       SetPlanComplete();
226c627682eSJim Ingham       m_no_more_plans = true;
2275822173bSJim Ingham       return true;
2285822173bSJim Ingham     }
2295822173bSJim Ingham 
230b9c1b51eSKate Stone     // If we get to this point, we're not going to use a previously set "next
231b9c1b51eSKate Stone     // branch" breakpoint, so delete it:
232564d8bc2SJim Ingham     ClearNextBranchBreakpoint();
233564d8bc2SJim Ingham 
2345822173bSJim Ingham     // We may have set the plan up above in the FrameIsOlder section:
2355822173bSJim Ingham 
2364d56e9c1SJim Ingham     if (!m_sub_plan_sp)
237b9c1b51eSKate Stone       m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough(m_stack_id, false,
238b9c1b51eSKate Stone                                                              stop_others);
23908b87e0dSJim Ingham 
240b9c1b51eSKate Stone     if (log) {
2414d56e9c1SJim Ingham       if (m_sub_plan_sp)
2424d56e9c1SJim Ingham         log->Printf("Found a step through plan: %s", m_sub_plan_sp->GetName());
24308b87e0dSJim Ingham       else
24408b87e0dSJim Ingham         log->Printf("No step through plan found.");
24508b87e0dSJim Ingham     }
24608b87e0dSJim Ingham 
247b9c1b51eSKate Stone     // If not, give the "should_stop" callback a chance to push a plan to get us
248b9c1b51eSKate Stone     // out of here.
24930fdc8d8SChris Lattner     // But only do that if we actually have stepped in.
2504d56e9c1SJim Ingham     if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
2514b4b2478SJim Ingham       m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order);
25230fdc8d8SChris Lattner 
253b9c1b51eSKate Stone     // If we've stepped in and we are going to stop here, check to see if we
254b9c1b51eSKate Stone     // were asked to
2557ce490c6SJim Ingham     // run past the prologue, and if so do that.
2567ce490c6SJim Ingham 
257b9c1b51eSKate Stone     if (!m_sub_plan_sp && frame_order == eFrameCompareYounger &&
258b9c1b51eSKate Stone         m_step_past_prologue) {
259b57e4a1bSJason Molenda       lldb::StackFrameSP curr_frame = m_thread.GetStackFrameAtIndex(0);
260b9c1b51eSKate Stone       if (curr_frame) {
2617ce490c6SJim Ingham         size_t bytes_to_skip = 0;
2627ce490c6SJim Ingham         lldb::addr_t curr_addr = m_thread.GetRegisterContext()->GetPC();
2637ce490c6SJim Ingham         Address func_start_address;
2647ce490c6SJim Ingham 
265b9c1b51eSKate Stone         SymbolContext sc = curr_frame->GetSymbolContext(eSymbolContextFunction |
266b9c1b51eSKate Stone                                                         eSymbolContextSymbol);
2677ce490c6SJim Ingham 
268b9c1b51eSKate Stone         if (sc.function) {
2697ce490c6SJim Ingham           func_start_address = sc.function->GetAddressRange().GetBaseAddress();
270b9c1b51eSKate Stone           if (curr_addr ==
271b9c1b51eSKate Stone               func_start_address.GetLoadAddress(
272b9c1b51eSKate Stone                   m_thread.CalculateTarget().get()))
2737ce490c6SJim Ingham             bytes_to_skip = sc.function->GetPrologueByteSize();
274b9c1b51eSKate Stone         } else if (sc.symbol) {
275e7612134SGreg Clayton           func_start_address = sc.symbol->GetAddress();
276b9c1b51eSKate Stone           if (curr_addr ==
277b9c1b51eSKate Stone               func_start_address.GetLoadAddress(
278b9c1b51eSKate Stone                   m_thread.CalculateTarget().get()))
2797ce490c6SJim Ingham             bytes_to_skip = sc.symbol->GetPrologueByteSize();
2807ce490c6SJim Ingham         }
2817ce490c6SJim Ingham 
282*08581263SJim Ingham         if (bytes_to_skip == 0 && sc.symbol) {
283*08581263SJim Ingham           TargetSP target = m_thread.CalculateTarget();
284*08581263SJim Ingham           Architecture *arch = target->GetArchitecturePlugin();
285*08581263SJim Ingham           if (arch) {
286*08581263SJim Ingham             Address curr_sec_addr;
287*08581263SJim Ingham             target->GetSectionLoadList().ResolveLoadAddress(curr_addr,
288*08581263SJim Ingham                                                             curr_sec_addr);
289*08581263SJim Ingham             bytes_to_skip = arch->GetBytesToSkip(*sc.symbol, curr_sec_addr);
290*08581263SJim Ingham           }
291*08581263SJim Ingham         }
292*08581263SJim Ingham 
293b9c1b51eSKate Stone         if (bytes_to_skip != 0) {
2947ce490c6SJim Ingham           func_start_address.Slide(bytes_to_skip);
29520ad3c40SCaroline Tice           log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP);
2967ce490c6SJim Ingham           if (log)
2977ce490c6SJim Ingham             log->Printf("Pushing past prologue ");
2987ce490c6SJim Ingham 
299b9c1b51eSKate Stone           m_sub_plan_sp = m_thread.QueueThreadPlanForRunToAddress(
300b9c1b51eSKate Stone               false, func_start_address, true);
3017ce490c6SJim Ingham         }
3027ce490c6SJim Ingham       }
3037ce490c6SJim Ingham     }
304f02a2e96SJim Ingham   }
3057ce490c6SJim Ingham 
306b9c1b51eSKate Stone   if (!m_sub_plan_sp) {
30730fdc8d8SChris Lattner     m_no_more_plans = true;
30830fdc8d8SChris Lattner     SetPlanComplete();
30930fdc8d8SChris Lattner     return true;
310b9c1b51eSKate Stone   } else {
31130fdc8d8SChris Lattner     m_no_more_plans = false;
3122bdbfd50SJim Ingham     m_sub_plan_sp->SetPrivate(true);
31330fdc8d8SChris Lattner     return false;
31430fdc8d8SChris Lattner   }
31530fdc8d8SChris Lattner }
31630fdc8d8SChris Lattner 
317b9c1b51eSKate Stone void ThreadPlanStepInRange::SetAvoidRegexp(const char *name) {
31895eae423SZachary Turner   auto name_ref = llvm::StringRef::withNullAsEmpty(name);
319e65b2cf2SEugene Zelenko   if (!m_avoid_regexp_ap)
32095eae423SZachary Turner     m_avoid_regexp_ap.reset(new RegularExpression(name_ref));
321a56c8006SJim Ingham 
32295eae423SZachary Turner   m_avoid_regexp_ap->Compile(name_ref);
323a56c8006SJim Ingham }
324a56c8006SJim Ingham 
325b9c1b51eSKate Stone void ThreadPlanStepInRange::SetDefaultFlagValue(uint32_t new_value) {
32630fdc8d8SChris Lattner   // TODO: Should we test this for sanity?
32730fdc8d8SChris Lattner   ThreadPlanStepInRange::s_default_flag_values = new_value;
32830fdc8d8SChris Lattner }
32930fdc8d8SChris Lattner 
330b9c1b51eSKate Stone bool ThreadPlanStepInRange::FrameMatchesAvoidCriteria() {
331b57e4a1bSJason Molenda   StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
332a56c8006SJim Ingham 
3334da6206dSJim Ingham   // Check the library list first, as that's cheapest:
334a786e539SJim Ingham   bool libraries_say_avoid = false;
335a786e539SJim Ingham 
3364da6206dSJim Ingham   FileSpecList libraries_to_avoid(GetThread().GetLibrariesToAvoid());
3374da6206dSJim Ingham   size_t num_libraries = libraries_to_avoid.GetSize();
338b9c1b51eSKate Stone   if (num_libraries > 0) {
3394da6206dSJim Ingham     SymbolContext sc(frame->GetSymbolContext(eSymbolContextModule));
3404da6206dSJim Ingham     FileSpec frame_library(sc.module_sp->GetFileSpec());
3414da6206dSJim Ingham 
342b9c1b51eSKate Stone     if (frame_library) {
343b9c1b51eSKate Stone       for (size_t i = 0; i < num_libraries; i++) {
3444da6206dSJim Ingham         const FileSpec &file_spec(libraries_to_avoid.GetFileSpecAtIndex(i));
345b9c1b51eSKate Stone         if (FileSpec::Equal(file_spec, frame_library, false)) {
3464da6206dSJim Ingham           libraries_say_avoid = true;
3474da6206dSJim Ingham           break;
3484da6206dSJim Ingham         }
3494da6206dSJim Ingham       }
3504da6206dSJim Ingham     }
351a786e539SJim Ingham   }
3524da6206dSJim Ingham   if (libraries_say_avoid)
3534da6206dSJim Ingham     return true;
3544da6206dSJim Ingham 
35567cc0636SGreg Clayton   const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_ap.get();
356e65b2cf2SEugene Zelenko   if (avoid_regexp_to_use == nullptr)
357ee8aea10SJim Ingham     avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
358ee8aea10SJim Ingham 
359b9c1b51eSKate Stone   if (avoid_regexp_to_use != nullptr) {
360b9c1b51eSKate Stone     SymbolContext sc = frame->GetSymbolContext(
361b9c1b51eSKate Stone         eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
362b9c1b51eSKate Stone     if (sc.symbol != nullptr) {
363b9c1b51eSKate Stone       const char *frame_function_name =
364b9c1b51eSKate Stone           sc.GetFunctionName(Mangled::ePreferDemangledWithoutArguments)
365b9c1b51eSKate Stone               .GetCString();
366b9c1b51eSKate Stone       if (frame_function_name) {
367cf2667c4SJim Ingham         size_t num_matches = 0;
3685160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
3693101ba33SJim Ingham         if (log)
370cf2667c4SJim Ingham           num_matches = 1;
371bc43cab5SGreg Clayton 
372bc43cab5SGreg Clayton         RegularExpression::Match regex_match(num_matches);
373bc43cab5SGreg Clayton 
374b9c1b51eSKate Stone         bool return_value =
375b9c1b51eSKate Stone             avoid_regexp_to_use->Execute(frame_function_name, &regex_match);
376b9c1b51eSKate Stone         if (return_value) {
377b9c1b51eSKate Stone           if (log) {
378cf2667c4SJim Ingham             std::string match;
379bc43cab5SGreg Clayton             regex_match.GetMatchAtIndex(frame_function_name, 0, match);
380b9c1b51eSKate Stone             log->Printf("Stepping out of function \"%s\" because it matches "
381b9c1b51eSKate Stone                         "the avoid regexp \"%s\" - match substring: \"%s\".",
38295eae423SZachary Turner                         frame_function_name,
38395eae423SZachary Turner                         avoid_regexp_to_use->GetText().str().c_str(),
384cf2667c4SJim Ingham                         match.c_str());
385cf2667c4SJim Ingham           }
3863101ba33SJim Ingham         }
3873101ba33SJim Ingham         return return_value;
3883101ba33SJim Ingham       }
389a56c8006SJim Ingham     }
390a56c8006SJim Ingham   }
391a56c8006SJim Ingham   return false;
392a56c8006SJim Ingham }
393a56c8006SJim Ingham 
394b9c1b51eSKate Stone bool ThreadPlanStepInRange::DefaultShouldStopHereCallback(
395b9c1b51eSKate Stone     ThreadPlan *current_plan, Flags &flags, FrameComparison operation,
396b9c1b51eSKate Stone     void *baton) {
3974b4b2478SJim Ingham   bool should_stop_here = true;
398b57e4a1bSJason Molenda   StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
3995160ce5cSGreg Clayton   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
40030fdc8d8SChris Lattner 
401b9c1b51eSKate Stone   // First see if the ThreadPlanShouldStopHere default implementation thinks we
402b9c1b51eSKate Stone   // should get out of here:
403b9c1b51eSKate Stone   should_stop_here = ThreadPlanShouldStopHere::DefaultShouldStopHereCallback(
404b9c1b51eSKate Stone       current_plan, flags, operation, baton);
4050c2b9d2aSJim Ingham   if (!should_stop_here)
4060c2b9d2aSJim Ingham     return should_stop_here;
407a56c8006SJim Ingham 
408b9c1b51eSKate Stone   if (should_stop_here && current_plan->GetKind() == eKindStepInRange &&
409b9c1b51eSKate Stone       operation == eFrameCompareYounger) {
410b9c1b51eSKate Stone     ThreadPlanStepInRange *step_in_range_plan =
411b9c1b51eSKate Stone         static_cast<ThreadPlanStepInRange *>(current_plan);
412b9c1b51eSKate Stone     if (step_in_range_plan->m_step_into_target) {
413b9c1b51eSKate Stone       SymbolContext sc = frame->GetSymbolContext(
414b9c1b51eSKate Stone           eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol);
415b9c1b51eSKate Stone       if (sc.symbol != nullptr) {
416b9c1b51eSKate Stone         // First try an exact match, since that's cheap with ConstStrings.  Then
417b9c1b51eSKate Stone         // do a strstr compare.
418b9c1b51eSKate Stone         if (step_in_range_plan->m_step_into_target == sc.GetFunctionName()) {
4194b4b2478SJim Ingham           should_stop_here = true;
420b9c1b51eSKate Stone         } else {
421b9c1b51eSKate Stone           const char *target_name =
422b9c1b51eSKate Stone               step_in_range_plan->m_step_into_target.AsCString();
423c627682eSJim Ingham           const char *function_name = sc.GetFunctionName().AsCString();
424c627682eSJim Ingham 
425e65b2cf2SEugene Zelenko           if (function_name == nullptr)
4264b4b2478SJim Ingham             should_stop_here = false;
427e65b2cf2SEugene Zelenko           else if (strstr(function_name, target_name) == nullptr)
4284b4b2478SJim Ingham             should_stop_here = false;
429c627682eSJim Ingham         }
4304b4b2478SJim Ingham         if (log && !should_stop_here)
431b9c1b51eSKate Stone           log->Printf("Stepping out of frame %s which did not match step into "
432b9c1b51eSKate Stone                       "target %s.",
4333101ba33SJim Ingham                       sc.GetFunctionName().AsCString(),
4343101ba33SJim Ingham                       step_in_range_plan->m_step_into_target.AsCString());
435c627682eSJim Ingham       }
436c627682eSJim Ingham     }
437c627682eSJim Ingham 
438b9c1b51eSKate Stone     if (should_stop_here) {
439b9c1b51eSKate Stone       ThreadPlanStepInRange *step_in_range_plan =
440b9c1b51eSKate Stone           static_cast<ThreadPlanStepInRange *>(current_plan);
441b9c1b51eSKate Stone       // Don't log the should_step_out here, it's easier to do it in
442b9c1b51eSKate Stone       // FrameMatchesAvoidCriteria.
4434b4b2478SJim Ingham       should_stop_here = !step_in_range_plan->FrameMatchesAvoidCriteria();
444a56c8006SJim Ingham     }
445a56c8006SJim Ingham   }
446a56c8006SJim Ingham 
4474b4b2478SJim Ingham   return should_stop_here;
44830fdc8d8SChris Lattner }
449fbbfe6ecSJim Ingham 
450b9c1b51eSKate Stone bool ThreadPlanStepInRange::DoPlanExplainsStop(Event *event_ptr) {
451fbbfe6ecSJim Ingham   // We always explain a stop.  Either we've just done a single step, in which
452fbbfe6ecSJim Ingham   // case we'll do our ordinary processing, or we stopped for some
453b9c1b51eSKate Stone   // reason that isn't handled by our sub-plans, in which case we want to just
454b9c1b51eSKate Stone   // stop right
455fbbfe6ecSJim Ingham   // away.
456b9c1b51eSKate Stone   // In general, we don't want to mark the plan as complete for unexplained
457b9c1b51eSKate Stone   // stops.
458b9c1b51eSKate Stone   // For instance, if you step in to some code with no debug info, so you step
459b9c1b51eSKate Stone   // out
460b9c1b51eSKate Stone   // and in the course of that hit a breakpoint, then you want to stop & show
461b9c1b51eSKate Stone   // the user
462b9c1b51eSKate Stone   // the breakpoint, but not unship the step in plan, since you still may want
463b9c1b51eSKate Stone   // to complete that
464b9c1b51eSKate Stone   // plan when you continue.  This is particularly true when doing "step in to
465b9c1b51eSKate Stone   // target function."
466c627682eSJim Ingham   // stepping.
467fbbfe6ecSJim Ingham   //
468b9c1b51eSKate Stone   // The only variation is that if we are doing "step by running to next branch"
469b9c1b51eSKate Stone   // in which case
470fbbfe6ecSJim Ingham   // if we hit our branch breakpoint we don't set the plan to complete.
471fbbfe6ecSJim Ingham 
4729b03fa0cSJim Ingham   bool return_value = false;
473f02a2e96SJim Ingham 
474b9c1b51eSKate Stone   if (m_virtual_step) {
475221d51cfSJim Ingham     return_value = true;
476b9c1b51eSKate Stone   } else {
47760c4118cSJim Ingham     StopInfoSP stop_info_sp = GetPrivateStopInfo();
478b9c1b51eSKate Stone     if (stop_info_sp) {
479fbbfe6ecSJim Ingham       StopReason reason = stop_info_sp->GetStopReason();
480fbbfe6ecSJim Ingham 
481b9c1b51eSKate Stone       if (reason == eStopReasonBreakpoint) {
482b9c1b51eSKate Stone         if (NextRangeBreakpointExplainsStop(stop_info_sp)) {
483221d51cfSJim Ingham           return_value = true;
484221d51cfSJim Ingham         }
485b9c1b51eSKate Stone       } else if (IsUsuallyUnexplainedStopReason(reason)) {
4865160ce5cSGreg Clayton         Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
487fbbfe6ecSJim Ingham         if (log)
488b9c1b51eSKate Stone           log->PutCString("ThreadPlanStepInRange got asked if it explains the "
489b9c1b51eSKate Stone                           "stop for some reason other than step.");
490221d51cfSJim Ingham         return_value = false;
491b9c1b51eSKate Stone       } else {
492221d51cfSJim Ingham         return_value = true;
493fbbfe6ecSJim Ingham       }
494b9c1b51eSKate Stone     } else
495221d51cfSJim Ingham       return_value = true;
496221d51cfSJim Ingham   }
497221d51cfSJim Ingham 
498221d51cfSJim Ingham   return return_value;
499fbbfe6ecSJim Ingham }
500513c6bb8SJim Ingham 
501b9c1b51eSKate Stone bool ThreadPlanStepInRange::DoWillResume(lldb::StateType resume_state,
502b9c1b51eSKate Stone                                          bool current_plan) {
50369fc298aSTamas Berghammer   m_virtual_step = false;
504b9c1b51eSKate Stone   if (resume_state == eStateStepping && current_plan) {
505513c6bb8SJim Ingham     // See if we are about to step over a virtual inlined call.
506513c6bb8SJim Ingham     bool step_without_resume = m_thread.DecrementCurrentInlinedDepth();
507b9c1b51eSKate Stone     if (step_without_resume) {
5085160ce5cSGreg Clayton       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
509513c6bb8SJim Ingham       if (log)
510b9c1b51eSKate Stone         log->Printf("ThreadPlanStepInRange::DoWillResume: returning false, "
511b9c1b51eSKate Stone                     "inline_depth: %d",
512513c6bb8SJim Ingham                     m_thread.GetCurrentInlinedDepth());
513513c6bb8SJim Ingham       SetStopInfo(StopInfo::CreateStopReasonToTrace(m_thread));
514f02a2e96SJim Ingham 
515b9c1b51eSKate Stone       // FIXME: Maybe it would be better to create a InlineStep stop reason, but
516b9c1b51eSKate Stone       // then
517f02a2e96SJim Ingham       // the whole rest of the world would have to handle that stop reason.
518f02a2e96SJim Ingham       m_virtual_step = true;
519513c6bb8SJim Ingham     }
520513c6bb8SJim Ingham     return !step_without_resume;
521513c6bb8SJim Ingham   }
522221d51cfSJim Ingham   return true;
523513c6bb8SJim Ingham }
524246cb611SDaniel Malea 
525b9c1b51eSKate Stone bool ThreadPlanStepInRange::IsVirtualStep() { return m_virtual_step; }
526