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