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 #include "lldb/Target/ThreadPlanStepInRange.h"
1130fdc8d8SChris Lattner 
1230fdc8d8SChris Lattner // C Includes
1330fdc8d8SChris Lattner // C++ Includes
1430fdc8d8SChris Lattner // Other libraries and framework includes
1530fdc8d8SChris Lattner // Project includes
1630fdc8d8SChris Lattner 
1730fdc8d8SChris Lattner #include "lldb/lldb-private-log.h"
1830fdc8d8SChris Lattner #include "lldb/Core/Log.h"
194da6206dSJim Ingham #include "lldb/Core/Module.h"
2030fdc8d8SChris Lattner #include "lldb/Core/Stream.h"
21a56c8006SJim Ingham #include "lldb/Symbol/Symbol.h"
227ce490c6SJim Ingham #include "lldb/Symbol/Function.h"
2330fdc8d8SChris Lattner #include "lldb/Target/Process.h"
2430fdc8d8SChris Lattner #include "lldb/Target/RegisterContext.h"
25514487e8SGreg Clayton #include "lldb/Target/Target.h"
2630fdc8d8SChris Lattner #include "lldb/Target/Thread.h"
2730fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanStepOut.h"
2830fdc8d8SChris Lattner #include "lldb/Target/ThreadPlanStepThrough.h"
29a56c8006SJim Ingham #include "lldb/Core/RegularExpression.h"
3030fdc8d8SChris Lattner 
3130fdc8d8SChris Lattner using namespace lldb;
3230fdc8d8SChris Lattner using namespace lldb_private;
3330fdc8d8SChris Lattner 
34*4b4b2478SJim Ingham uint32_t ThreadPlanStepInRange::s_default_flag_values = ThreadPlanShouldStopHere::eStepInAvoidNoDebug;
3530fdc8d8SChris Lattner 
3630fdc8d8SChris Lattner //----------------------------------------------------------------------
3730fdc8d8SChris Lattner // ThreadPlanStepInRange: Step through a stack range, either stepping over or into
3830fdc8d8SChris Lattner // based on the value of \a type.
3930fdc8d8SChris Lattner //----------------------------------------------------------------------
4030fdc8d8SChris Lattner 
4130fdc8d8SChris Lattner ThreadPlanStepInRange::ThreadPlanStepInRange
4230fdc8d8SChris Lattner (
4330fdc8d8SChris Lattner     Thread &thread,
4430fdc8d8SChris Lattner     const AddressRange &range,
4530fdc8d8SChris Lattner     const SymbolContext &addr_context,
46*4b4b2478SJim Ingham     lldb::RunMode stop_others,
47*4b4b2478SJim Ingham     LazyBool step_in_avoids_code_without_debug_info,
48*4b4b2478SJim Ingham     LazyBool step_out_avoids_code_without_debug_info
4930fdc8d8SChris Lattner ) :
50b01e742aSJim Ingham     ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others),
51*4b4b2478SJim Ingham     ThreadPlanShouldStopHere (this),
52f02a2e96SJim Ingham     m_step_past_prologue (true),
53f02a2e96SJim Ingham     m_virtual_step (false)
5430fdc8d8SChris Lattner {
55*4b4b2478SJim Ingham     SetCallbacks();
5630fdc8d8SChris Lattner     SetFlagsToDefault ();
57*4b4b2478SJim Ingham     SetupAvoidNoDebug(step_in_avoids_code_without_debug_info, step_out_avoids_code_without_debug_info);
5830fdc8d8SChris Lattner }
5930fdc8d8SChris Lattner 
60c627682eSJim Ingham ThreadPlanStepInRange::ThreadPlanStepInRange
61c627682eSJim Ingham (
62c627682eSJim Ingham     Thread &thread,
63c627682eSJim Ingham     const AddressRange &range,
64c627682eSJim Ingham     const SymbolContext &addr_context,
65c627682eSJim Ingham     const char *step_into_target,
66*4b4b2478SJim Ingham     lldb::RunMode stop_others,
67*4b4b2478SJim Ingham     LazyBool step_in_avoids_code_without_debug_info,
68*4b4b2478SJim Ingham     LazyBool step_out_avoids_code_without_debug_info
69c627682eSJim Ingham ) :
70c627682eSJim Ingham     ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others),
71*4b4b2478SJim Ingham     ThreadPlanShouldStopHere (this),
72c627682eSJim Ingham     m_step_past_prologue (true),
73c627682eSJim Ingham     m_virtual_step (false),
74c627682eSJim Ingham     m_step_into_target (step_into_target)
75c627682eSJim Ingham {
76*4b4b2478SJim Ingham     SetCallbacks();
77c627682eSJim Ingham     SetFlagsToDefault ();
78*4b4b2478SJim Ingham     SetupAvoidNoDebug(step_in_avoids_code_without_debug_info, step_out_avoids_code_without_debug_info);
79c627682eSJim Ingham }
80c627682eSJim Ingham 
8130fdc8d8SChris Lattner ThreadPlanStepInRange::~ThreadPlanStepInRange ()
8230fdc8d8SChris Lattner {
8330fdc8d8SChris Lattner }
8430fdc8d8SChris Lattner 
8530fdc8d8SChris Lattner void
86*4b4b2478SJim Ingham ThreadPlanStepInRange::SetupAvoidNoDebug(LazyBool step_in_avoids_code_without_debug_info,
87*4b4b2478SJim Ingham                                          LazyBool step_out_avoids_code_without_debug_info)
88*4b4b2478SJim Ingham {
89*4b4b2478SJim Ingham     bool avoid_nodebug = true;
90*4b4b2478SJim Ingham 
91*4b4b2478SJim Ingham     switch (step_in_avoids_code_without_debug_info)
92*4b4b2478SJim Ingham     {
93*4b4b2478SJim Ingham         case eLazyBoolYes:
94*4b4b2478SJim Ingham             avoid_nodebug = true;
95*4b4b2478SJim Ingham             break;
96*4b4b2478SJim Ingham         case eLazyBoolNo:
97*4b4b2478SJim Ingham             avoid_nodebug = false;
98*4b4b2478SJim Ingham             break;
99*4b4b2478SJim Ingham         case eLazyBoolCalculate:
100*4b4b2478SJim Ingham             avoid_nodebug = m_thread.GetStepInAvoidsNoDebug();
101*4b4b2478SJim Ingham             break;
102*4b4b2478SJim Ingham     }
103*4b4b2478SJim Ingham     if (avoid_nodebug)
104*4b4b2478SJim Ingham         GetFlags().Set (ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
105*4b4b2478SJim Ingham     else
106*4b4b2478SJim Ingham         GetFlags().Clear (ThreadPlanShouldStopHere::eStepInAvoidNoDebug);
107*4b4b2478SJim Ingham 
108*4b4b2478SJim Ingham     avoid_nodebug = true;
109*4b4b2478SJim Ingham     switch (step_out_avoids_code_without_debug_info)
110*4b4b2478SJim Ingham     {
111*4b4b2478SJim Ingham         case eLazyBoolYes:
112*4b4b2478SJim Ingham             avoid_nodebug = true;
113*4b4b2478SJim Ingham             break;
114*4b4b2478SJim Ingham         case eLazyBoolNo:
115*4b4b2478SJim Ingham             avoid_nodebug = false;
116*4b4b2478SJim Ingham             break;
117*4b4b2478SJim Ingham         case eLazyBoolCalculate:
118*4b4b2478SJim Ingham             avoid_nodebug = m_thread.GetStepOutAvoidsNoDebug();
119*4b4b2478SJim Ingham             break;
120*4b4b2478SJim Ingham     }
121*4b4b2478SJim Ingham     if (avoid_nodebug)
122*4b4b2478SJim Ingham         GetFlags().Set (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
123*4b4b2478SJim Ingham     else
124*4b4b2478SJim Ingham         GetFlags().Clear (ThreadPlanShouldStopHere::eStepOutAvoidNoDebug);
125*4b4b2478SJim Ingham }
126*4b4b2478SJim Ingham 
127*4b4b2478SJim Ingham void
12830fdc8d8SChris Lattner ThreadPlanStepInRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
12930fdc8d8SChris Lattner {
13030fdc8d8SChris Lattner     if (level == lldb::eDescriptionLevelBrief)
13130fdc8d8SChris Lattner         s->Printf("step in");
13230fdc8d8SChris Lattner     else
13330fdc8d8SChris Lattner     {
13430fdc8d8SChris Lattner         s->Printf ("Stepping through range (stepping into functions): ");
135c4c9fedcSJim Ingham         DumpRanges(s);
1364d56e9c1SJim Ingham         const char *step_into_target = m_step_into_target.AsCString();
1374d56e9c1SJim Ingham         if (step_into_target && step_into_target[0] != '\0')
138c627682eSJim Ingham             s->Printf (" targeting %s.", m_step_into_target.AsCString());
1394d56e9c1SJim Ingham         else
1404d56e9c1SJim Ingham             s->PutChar('.');
14130fdc8d8SChris Lattner     }
14230fdc8d8SChris Lattner }
14330fdc8d8SChris Lattner 
14430fdc8d8SChris Lattner bool
14530fdc8d8SChris Lattner ThreadPlanStepInRange::ShouldStop (Event *event_ptr)
14630fdc8d8SChris Lattner {
1475160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
14830fdc8d8SChris Lattner 
14930fdc8d8SChris Lattner     if (log)
15030fdc8d8SChris Lattner     {
15130fdc8d8SChris Lattner         StreamString s;
152514487e8SGreg Clayton         s.Address (m_thread.GetRegisterContext()->GetPC(),
1531ac04c30SGreg Clayton                    m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
15430fdc8d8SChris Lattner         log->Printf("ThreadPlanStepInRange reached %s.", s.GetData());
15530fdc8d8SChris Lattner     }
15630fdc8d8SChris Lattner 
15725f66700SJim Ingham     if (IsPlanComplete())
15825f66700SJim Ingham         return true;
15925f66700SJim Ingham 
1604d56e9c1SJim Ingham     m_no_more_plans = false;
1614d56e9c1SJim Ingham     if (m_sub_plan_sp && m_sub_plan_sp->IsPlanComplete())
1624d56e9c1SJim Ingham     {
1634d56e9c1SJim Ingham         if (!m_sub_plan_sp->PlanSucceeded())
1644d56e9c1SJim Ingham         {
1654d56e9c1SJim Ingham             SetPlanComplete();
1664d56e9c1SJim Ingham             m_no_more_plans = true;
1674d56e9c1SJim Ingham             return true;
1684d56e9c1SJim Ingham         }
1694d56e9c1SJim Ingham         else
1704d56e9c1SJim Ingham             m_sub_plan_sp.reset();
1714d56e9c1SJim Ingham     }
17230fdc8d8SChris Lattner 
173f02a2e96SJim Ingham     if (m_virtual_step)
174f02a2e96SJim Ingham     {
175f02a2e96SJim Ingham         // If we've just completed a virtual step, all we need to do is check for a ShouldStopHere plan, and otherwise
176f02a2e96SJim Ingham         // we're done.
177*4b4b2478SJim Ingham         // FIXME - This can be both a step in and a step out.  Probably should record which in the m_virtual_step.
178*4b4b2478SJim Ingham         m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(eFrameCompareYounger);
179f02a2e96SJim Ingham     }
180f02a2e96SJim Ingham     else
181f02a2e96SJim Ingham     {
1824a58e968SJim Ingham         // Stepping through should be done running other threads in general, since we're setting a breakpoint and
1834a58e968SJim Ingham         // continuing.  So only stop others if we are explicitly told to do so.
1849d790c5dSJim Ingham 
18530fdc8d8SChris Lattner         bool stop_others;
1864a58e968SJim Ingham         if (m_stop_others == lldb::eOnlyThisThread)
1874a58e968SJim Ingham             stop_others = true;
1886cf5b8f1SEd Maste         else
1896cf5b8f1SEd Maste             stop_others = false;
19030fdc8d8SChris Lattner 
191b5c0d1ccSJim Ingham         FrameComparison frame_order = CompareCurrentFrameToStartFrame();
192b5c0d1ccSJim Ingham 
193b5c0d1ccSJim Ingham         if (frame_order == eFrameCompareOlder)
1945822173bSJim Ingham         {
1955822173bSJim Ingham             // If we're in an older frame then we should stop.
1965822173bSJim Ingham             //
1975822173bSJim Ingham             // A caveat to this is if we think the frame is older but we're actually in a trampoline.
1985822173bSJim Ingham             // I'm going to make the assumption that you wouldn't RETURN to a trampoline.  So if we are
1995822173bSJim Ingham             // in a trampoline we think the frame is older because the trampoline confused the backtracer.
2004d56e9c1SJim Ingham             m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
2014d56e9c1SJim Ingham             if (!m_sub_plan_sp)
202*4b4b2478SJim Ingham             {
203*4b4b2478SJim Ingham                 // Otherwise check the ShouldStopHere for step out:
204*4b4b2478SJim Ingham                 m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(eFrameCompareOlder);
205*4b4b2478SJim Ingham                 if (log)
206*4b4b2478SJim Ingham                     log->Printf ("ShouldStopHere says we should step out of this frame.");
207*4b4b2478SJim Ingham             }
2085822173bSJim Ingham             else if (log)
2095822173bSJim Ingham             {
2105822173bSJim Ingham                 log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
2115822173bSJim Ingham             }
2125822173bSJim Ingham 
2135822173bSJim Ingham         }
214564d8bc2SJim Ingham         else if (frame_order == eFrameCompareEqual && InSymbol())
2155822173bSJim Ingham         {
2165822173bSJim Ingham             // If we are not in a place we should step through, we're done.
2175822173bSJim Ingham             // One tricky bit here is that some stubs don't push a frame, so we have to check
2185822173bSJim Ingham             // both the case of a frame that is younger, or the same as this frame.
2195822173bSJim Ingham             // However, if the frame is the same, and we are still in the symbol we started
2205822173bSJim Ingham             // in, the we don't need to do this.  This first check isn't strictly necessary,
2215822173bSJim Ingham             // but it is more efficient.
2225822173bSJim Ingham 
223564d8bc2SJim Ingham             // If we're still in the range, keep going, either by running to the next branch breakpoint, or by
224564d8bc2SJim Ingham             // stepping.
225564d8bc2SJim Ingham             if (InRange())
226564d8bc2SJim Ingham             {
227564d8bc2SJim Ingham                 SetNextBranchBreakpoint();
228564d8bc2SJim Ingham                 return false;
229564d8bc2SJim Ingham             }
230564d8bc2SJim Ingham 
2315822173bSJim Ingham             SetPlanComplete();
232c627682eSJim Ingham             m_no_more_plans = true;
2335822173bSJim Ingham             return true;
2345822173bSJim Ingham         }
2355822173bSJim Ingham 
236564d8bc2SJim Ingham         // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
237564d8bc2SJim Ingham         ClearNextBranchBreakpoint();
238564d8bc2SJim Ingham 
2395822173bSJim Ingham         // We may have set the plan up above in the FrameIsOlder section:
2405822173bSJim Ingham 
2414d56e9c1SJim Ingham         if (!m_sub_plan_sp)
2424d56e9c1SJim Ingham             m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
24308b87e0dSJim Ingham 
24408b87e0dSJim Ingham         if (log)
24508b87e0dSJim Ingham         {
2464d56e9c1SJim Ingham             if (m_sub_plan_sp)
2474d56e9c1SJim Ingham                 log->Printf ("Found a step through plan: %s", m_sub_plan_sp->GetName());
24808b87e0dSJim Ingham             else
24908b87e0dSJim Ingham                 log->Printf ("No step through plan found.");
25008b87e0dSJim Ingham         }
25108b87e0dSJim Ingham 
25230fdc8d8SChris Lattner         // If not, give the "should_stop" callback a chance to push a plan to get us out of here.
25330fdc8d8SChris Lattner         // But only do that if we actually have stepped in.
2544d56e9c1SJim Ingham         if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
255*4b4b2478SJim Ingham             m_sub_plan_sp = CheckShouldStopHereAndQueueStepOut(frame_order);
25630fdc8d8SChris Lattner 
2577ce490c6SJim Ingham         // If we've stepped in and we are going to stop here, check to see if we were asked to
2587ce490c6SJim Ingham         // run past the prologue, and if so do that.
2597ce490c6SJim Ingham 
2604d56e9c1SJim Ingham         if (!m_sub_plan_sp && frame_order == eFrameCompareYounger && m_step_past_prologue)
2617ce490c6SJim Ingham         {
262b57e4a1bSJason Molenda             lldb::StackFrameSP curr_frame = m_thread.GetStackFrameAtIndex(0);
2637ce490c6SJim Ingham             if (curr_frame)
2647ce490c6SJim Ingham             {
2657ce490c6SJim Ingham                 size_t bytes_to_skip = 0;
2667ce490c6SJim Ingham                 lldb::addr_t curr_addr = m_thread.GetRegisterContext()->GetPC();
2677ce490c6SJim Ingham                 Address func_start_address;
2687ce490c6SJim Ingham 
2697ce490c6SJim Ingham                 SymbolContext sc = curr_frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextSymbol);
2707ce490c6SJim Ingham 
2717ce490c6SJim Ingham                 if (sc.function)
2727ce490c6SJim Ingham                 {
2737ce490c6SJim Ingham                     func_start_address = sc.function->GetAddressRange().GetBaseAddress();
274d9e416c0SGreg Clayton                     if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get()))
2757ce490c6SJim Ingham                         bytes_to_skip = sc.function->GetPrologueByteSize();
2767ce490c6SJim Ingham                 }
2777ce490c6SJim Ingham                 else if (sc.symbol)
2787ce490c6SJim Ingham                 {
279e7612134SGreg Clayton                     func_start_address = sc.symbol->GetAddress();
280d9e416c0SGreg Clayton                     if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get()))
2817ce490c6SJim Ingham                         bytes_to_skip = sc.symbol->GetPrologueByteSize();
2827ce490c6SJim Ingham                 }
2837ce490c6SJim Ingham 
2847ce490c6SJim Ingham                 if (bytes_to_skip != 0)
2857ce490c6SJim Ingham                 {
2867ce490c6SJim Ingham                     func_start_address.Slide (bytes_to_skip);
28720ad3c40SCaroline Tice                     log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
2887ce490c6SJim Ingham                     if (log)
2897ce490c6SJim Ingham                         log->Printf ("Pushing past prologue ");
2907ce490c6SJim Ingham 
2914d56e9c1SJim Ingham                     m_sub_plan_sp = m_thread.QueueThreadPlanForRunToAddress(false, func_start_address,true);
2927ce490c6SJim Ingham                 }
2937ce490c6SJim Ingham             }
2947ce490c6SJim Ingham         }
295f02a2e96SJim Ingham      }
2967ce490c6SJim Ingham 
2974d56e9c1SJim Ingham      if (!m_sub_plan_sp)
29830fdc8d8SChris Lattner      {
29930fdc8d8SChris Lattner         m_no_more_plans = true;
30030fdc8d8SChris Lattner         SetPlanComplete();
30130fdc8d8SChris Lattner         return true;
30230fdc8d8SChris Lattner     }
30330fdc8d8SChris Lattner     else
30430fdc8d8SChris Lattner     {
30530fdc8d8SChris Lattner         m_no_more_plans = false;
30630fdc8d8SChris Lattner         return false;
30730fdc8d8SChris Lattner     }
30830fdc8d8SChris Lattner }
30930fdc8d8SChris Lattner 
31030fdc8d8SChris Lattner void
311a56c8006SJim Ingham ThreadPlanStepInRange::SetAvoidRegexp(const char *name)
312a56c8006SJim Ingham {
313a56c8006SJim Ingham     if (m_avoid_regexp_ap.get() == NULL)
314a56c8006SJim Ingham         m_avoid_regexp_ap.reset (new RegularExpression(name));
315a56c8006SJim Ingham 
316a56c8006SJim Ingham     m_avoid_regexp_ap->Compile (name);
317a56c8006SJim Ingham }
318a56c8006SJim Ingham 
319a56c8006SJim Ingham void
32030fdc8d8SChris Lattner ThreadPlanStepInRange::SetDefaultFlagValue (uint32_t new_value)
32130fdc8d8SChris Lattner {
32230fdc8d8SChris Lattner     // TODO: Should we test this for sanity?
32330fdc8d8SChris Lattner     ThreadPlanStepInRange::s_default_flag_values = new_value;
32430fdc8d8SChris Lattner }
32530fdc8d8SChris Lattner 
326a56c8006SJim Ingham bool
3274da6206dSJim Ingham ThreadPlanStepInRange::FrameMatchesAvoidCriteria ()
328a56c8006SJim Ingham {
329b57e4a1bSJason Molenda     StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
330a56c8006SJim Ingham 
3314da6206dSJim Ingham     // Check the library list first, as that's cheapest:
332a786e539SJim Ingham     bool libraries_say_avoid = false;
333a786e539SJim Ingham 
3344da6206dSJim Ingham     FileSpecList libraries_to_avoid (GetThread().GetLibrariesToAvoid());
3354da6206dSJim Ingham     size_t num_libraries = libraries_to_avoid.GetSize();
336a786e539SJim Ingham     if (num_libraries > 0)
337a786e539SJim Ingham     {
3384da6206dSJim Ingham         SymbolContext sc(frame->GetSymbolContext(eSymbolContextModule));
3394da6206dSJim Ingham         FileSpec frame_library(sc.module_sp->GetFileSpec());
3404da6206dSJim Ingham 
3414da6206dSJim Ingham         if (frame_library)
3424da6206dSJim Ingham         {
3434da6206dSJim Ingham             for (size_t i = 0; i < num_libraries; i++)
3444da6206dSJim Ingham             {
3454da6206dSJim Ingham                 const FileSpec &file_spec(libraries_to_avoid.GetFileSpecAtIndex(i));
3464da6206dSJim Ingham                 if (FileSpec::Equal (file_spec, frame_library, false))
3474da6206dSJim Ingham                 {
3484da6206dSJim Ingham                     libraries_say_avoid = true;
3494da6206dSJim Ingham                     break;
3504da6206dSJim Ingham                 }
3514da6206dSJim Ingham             }
3524da6206dSJim Ingham         }
353a786e539SJim Ingham     }
3544da6206dSJim Ingham     if (libraries_say_avoid)
3554da6206dSJim Ingham         return true;
3564da6206dSJim Ingham 
35767cc0636SGreg Clayton     const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_ap.get();
358ee8aea10SJim Ingham     if (avoid_regexp_to_use == NULL)
359ee8aea10SJim Ingham         avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
360ee8aea10SJim Ingham 
361ee8aea10SJim Ingham     if (avoid_regexp_to_use != NULL)
362a56c8006SJim Ingham     {
3634592cbc4SGreg Clayton         SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock|eSymbolContextSymbol);
364a56c8006SJim Ingham         if (sc.symbol != NULL)
365a56c8006SJim Ingham         {
3664592cbc4SGreg Clayton             const char *frame_function_name = sc.GetFunctionName().GetCString();
3674592cbc4SGreg Clayton             if (frame_function_name)
3683101ba33SJim Ingham             {
369cf2667c4SJim Ingham                 size_t num_matches = 0;
3705160ce5cSGreg Clayton                 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
3713101ba33SJim Ingham                 if (log)
372cf2667c4SJim Ingham                     num_matches = 1;
373bc43cab5SGreg Clayton 
374bc43cab5SGreg Clayton                 RegularExpression::Match regex_match(num_matches);
375bc43cab5SGreg Clayton 
376bc43cab5SGreg Clayton                 bool return_value = avoid_regexp_to_use->Execute(frame_function_name, &regex_match);
377cf2667c4SJim Ingham                 if (return_value)
378cf2667c4SJim Ingham                 {
379cf2667c4SJim Ingham                     if (log)
380cf2667c4SJim Ingham                     {
381cf2667c4SJim Ingham                         std::string match;
382bc43cab5SGreg Clayton                         regex_match.GetMatchAtIndex(frame_function_name,0, match);
383cf2667c4SJim Ingham                         log->Printf ("Stepping out of function \"%s\" because it matches the avoid regexp \"%s\" - match substring: \"%s\".",
3843101ba33SJim Ingham                                      frame_function_name,
385cf2667c4SJim Ingham                                      avoid_regexp_to_use->GetText(),
386cf2667c4SJim Ingham                                      match.c_str());
387cf2667c4SJim Ingham                     }
3883101ba33SJim Ingham 
3893101ba33SJim Ingham                 }
3903101ba33SJim Ingham                 return return_value;
3913101ba33SJim Ingham             }
392a56c8006SJim Ingham         }
393a56c8006SJim Ingham     }
394a56c8006SJim Ingham     return false;
395a56c8006SJim Ingham }
396a56c8006SJim Ingham 
397*4b4b2478SJim Ingham bool
398*4b4b2478SJim Ingham ThreadPlanStepInRange::DefaultShouldStopHereCallback (ThreadPlan *current_plan, Flags &flags, FrameComparison operation, void *baton)
39930fdc8d8SChris Lattner {
400*4b4b2478SJim Ingham     bool should_stop_here = true;
401b57e4a1bSJason Molenda     StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
4025160ce5cSGreg Clayton     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
40330fdc8d8SChris Lattner 
404*4b4b2478SJim Ingham     if ((operation == eFrameCompareYounger && flags.Test(eStepInAvoidNoDebug))
405*4b4b2478SJim Ingham         || (operation == eFrameCompareOlder && flags.Test(eStepOutAvoidNoDebug)))
406a56c8006SJim Ingham     {
40730fdc8d8SChris Lattner         if (!frame->HasDebugInformation())
408af0f1759SJim Ingham         {
409af0f1759SJim Ingham             if (log)
410af0f1759SJim Ingham                 log->Printf ("Stepping out of frame with no debug info");
411af0f1759SJim Ingham 
412*4b4b2478SJim Ingham             should_stop_here = false;
413a56c8006SJim Ingham         }
414af0f1759SJim Ingham     }
415a56c8006SJim Ingham 
416*4b4b2478SJim Ingham     if (should_stop_here && current_plan->GetKind() == eKindStepInRange && operation == eFrameCompareYounger)
417a56c8006SJim Ingham     {
418a56c8006SJim Ingham         ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan);
419c627682eSJim Ingham         if (step_in_range_plan->m_step_into_target)
420c627682eSJim Ingham         {
421c627682eSJim Ingham             SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock|eSymbolContextSymbol);
422c627682eSJim Ingham             if (sc.symbol != NULL)
423c627682eSJim Ingham             {
424c627682eSJim Ingham                 // First try an exact match, since that's cheap with ConstStrings.  Then do a strstr compare.
425c627682eSJim Ingham                 if (step_in_range_plan->m_step_into_target == sc.GetFunctionName())
426c627682eSJim Ingham                 {
427*4b4b2478SJim Ingham                     should_stop_here = true;
428c627682eSJim Ingham                 }
429c627682eSJim Ingham                 else
430c627682eSJim Ingham                 {
431c627682eSJim Ingham                     const char *target_name = step_in_range_plan->m_step_into_target.AsCString();
432c627682eSJim Ingham                     const char *function_name = sc.GetFunctionName().AsCString();
433c627682eSJim Ingham 
434c627682eSJim Ingham                     if (function_name == NULL)
435*4b4b2478SJim Ingham                         should_stop_here = false;
436c627682eSJim Ingham                     else if (strstr (function_name, target_name) == NULL)
437*4b4b2478SJim Ingham                         should_stop_here = false;
438c627682eSJim Ingham                 }
439*4b4b2478SJim Ingham                 if (log && !should_stop_here)
4403101ba33SJim Ingham                     log->Printf("Stepping out of frame %s which did not match step into target %s.",
4413101ba33SJim Ingham                                 sc.GetFunctionName().AsCString(),
4423101ba33SJim Ingham                                 step_in_range_plan->m_step_into_target.AsCString());
443c627682eSJim Ingham             }
444c627682eSJim Ingham         }
445c627682eSJim Ingham 
446*4b4b2478SJim Ingham         if (should_stop_here)
447c627682eSJim Ingham         {
448c627682eSJim Ingham             ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan);
4494da6206dSJim Ingham             // Don't log the should_step_out here, it's easier to do it in FrameMatchesAvoidCriteria.
450*4b4b2478SJim Ingham             should_stop_here = !step_in_range_plan->FrameMatchesAvoidCriteria ();
451a56c8006SJim Ingham         }
452a56c8006SJim Ingham     }
453a56c8006SJim Ingham 
454*4b4b2478SJim Ingham     return should_stop_here;
45530fdc8d8SChris Lattner }
456fbbfe6ecSJim Ingham 
457fbbfe6ecSJim Ingham bool
458221d51cfSJim Ingham ThreadPlanStepInRange::DoPlanExplainsStop (Event *event_ptr)
459fbbfe6ecSJim Ingham {
460fbbfe6ecSJim Ingham     // We always explain a stop.  Either we've just done a single step, in which
461fbbfe6ecSJim Ingham     // case we'll do our ordinary processing, or we stopped for some
462fbbfe6ecSJim Ingham     // reason that isn't handled by our sub-plans, in which case we want to just stop right
463fbbfe6ecSJim Ingham     // away.
464c627682eSJim Ingham     // In general, we don't want to mark the plan as complete for unexplained stops.
465c627682eSJim Ingham     // For instance, if you step in to some code with no debug info, so you step out
466c627682eSJim Ingham     // and in the course of that hit a breakpoint, then you want to stop & show the user
467c627682eSJim Ingham     // the breakpoint, but not unship the step in plan, since you still may want to complete that
468c627682eSJim Ingham     // plan when you continue.  This is particularly true when doing "step in to target function."
469c627682eSJim Ingham     // stepping.
470fbbfe6ecSJim Ingham     //
471fbbfe6ecSJim Ingham     // The only variation is that if we are doing "step by running to next branch" in which case
472fbbfe6ecSJim Ingham     // if we hit our branch breakpoint we don't set the plan to complete.
473fbbfe6ecSJim Ingham 
474221d51cfSJim Ingham     bool return_value;
475f02a2e96SJim Ingham 
476221d51cfSJim Ingham     if (m_virtual_step)
477221d51cfSJim Ingham     {
478221d51cfSJim Ingham         return_value = true;
479221d51cfSJim Ingham     }
480221d51cfSJim Ingham     else
481221d51cfSJim Ingham     {
48260c4118cSJim Ingham         StopInfoSP stop_info_sp = GetPrivateStopInfo ();
483fbbfe6ecSJim Ingham         if (stop_info_sp)
484fbbfe6ecSJim Ingham         {
485fbbfe6ecSJim Ingham             StopReason reason = stop_info_sp->GetStopReason();
486fbbfe6ecSJim Ingham 
487fbbfe6ecSJim Ingham             switch (reason)
488fbbfe6ecSJim Ingham             {
489fbbfe6ecSJim Ingham             case eStopReasonBreakpoint:
490fbbfe6ecSJim Ingham                 if (NextRangeBreakpointExplainsStop(stop_info_sp))
491221d51cfSJim Ingham                 {
492221d51cfSJim Ingham                     return_value = true;
493221d51cfSJim Ingham                     break;
494221d51cfSJim Ingham                 }
495fbbfe6ecSJim Ingham             case eStopReasonWatchpoint:
496fbbfe6ecSJim Ingham             case eStopReasonSignal:
497fbbfe6ecSJim Ingham             case eStopReasonException:
49890ba8115SGreg Clayton             case eStopReasonExec:
499f85defaeSAndrew Kaylor             case eStopReasonThreadExiting:
500513c6bb8SJim Ingham                 {
5015160ce5cSGreg Clayton                     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
502fbbfe6ecSJim Ingham                     if (log)
503fbbfe6ecSJim Ingham                         log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
504513c6bb8SJim Ingham                 }
505221d51cfSJim Ingham                 return_value = false;
506fbbfe6ecSJim Ingham                 break;
507fbbfe6ecSJim Ingham             default:
508221d51cfSJim Ingham                 return_value = true;
509fbbfe6ecSJim Ingham                 break;
510fbbfe6ecSJim Ingham             }
511fbbfe6ecSJim Ingham         }
512221d51cfSJim Ingham         else
513221d51cfSJim Ingham             return_value = true;
514221d51cfSJim Ingham     }
515221d51cfSJim Ingham 
516221d51cfSJim Ingham     return return_value;
517fbbfe6ecSJim Ingham }
518513c6bb8SJim Ingham 
519513c6bb8SJim Ingham bool
520221d51cfSJim Ingham ThreadPlanStepInRange::DoWillResume (lldb::StateType resume_state, bool current_plan)
521513c6bb8SJim Ingham {
522513c6bb8SJim Ingham     if (resume_state == eStateStepping && current_plan)
523513c6bb8SJim Ingham     {
524513c6bb8SJim Ingham         // See if we are about to step over a virtual inlined call.
525513c6bb8SJim Ingham         bool step_without_resume = m_thread.DecrementCurrentInlinedDepth();
526513c6bb8SJim Ingham         if (step_without_resume)
527513c6bb8SJim Ingham         {
5285160ce5cSGreg Clayton             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
529513c6bb8SJim Ingham             if (log)
530221d51cfSJim Ingham                 log->Printf ("ThreadPlanStepInRange::DoWillResume: returning false, inline_depth: %d",
531513c6bb8SJim Ingham                              m_thread.GetCurrentInlinedDepth());
532513c6bb8SJim Ingham             SetStopInfo(StopInfo::CreateStopReasonToTrace(m_thread));
533f02a2e96SJim Ingham 
534f02a2e96SJim Ingham             // FIXME: Maybe it would be better to create a InlineStep stop reason, but then
535f02a2e96SJim Ingham             // the whole rest of the world would have to handle that stop reason.
536f02a2e96SJim Ingham             m_virtual_step = true;
537513c6bb8SJim Ingham         }
538513c6bb8SJim Ingham         return !step_without_resume;
539513c6bb8SJim Ingham     }
540221d51cfSJim Ingham     return true;
541513c6bb8SJim Ingham }
542246cb611SDaniel Malea 
543246cb611SDaniel Malea bool
544246cb611SDaniel Malea ThreadPlanStepInRange::IsVirtualStep()
545246cb611SDaniel Malea {
546246cb611SDaniel Malea   return m_virtual_step;
547246cb611SDaniel Malea }
548