ThreadPlanStepOverRange.cpp revision 0c8fa2d7dd18ae1816c82846234c45f79142e3df
1//===-- ThreadPlanStepOverRange.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/ThreadPlanStepOverRange.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/Block.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
29using namespace lldb_private;
30using namespace lldb;
31
32
33//----------------------------------------------------------------------
34// ThreadPlanStepOverRange: Step through a stack range, either stepping over or into
35// based on the value of \a type.
36//----------------------------------------------------------------------
37
38ThreadPlanStepOverRange::ThreadPlanStepOverRange
39(
40    Thread &thread,
41    const AddressRange &range,
42    const SymbolContext &addr_context,
43    lldb::RunMode stop_others
44) :
45    ThreadPlanStepRange (ThreadPlan::eKindStepOverRange, "Step range stepping over", thread, range, addr_context, stop_others),
46    m_first_resume(true)
47{
48}
49
50ThreadPlanStepOverRange::~ThreadPlanStepOverRange ()
51{
52}
53
54void
55ThreadPlanStepOverRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
56{
57    if (level == lldb::eDescriptionLevelBrief)
58        s->Printf("step over");
59    else
60    {
61        s->Printf ("stepping through range (stepping over functions): ");
62        DumpRanges(s);
63    }
64}
65
66bool
67ThreadPlanStepOverRange::ShouldStop (Event *event_ptr)
68{
69    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
70
71    if (log)
72    {
73        StreamString s;
74        s.Address (m_thread.GetRegisterContext()->GetPC(),
75                   m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
76        log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
77    }
78
79    // If we're out of the range but in the same frame or in our caller's frame
80    // then we should stop.
81    // When stepping out we only step if we are forcing running one thread.
82    bool stop_others;
83    if (m_stop_others == lldb::eOnlyThisThread)
84        stop_others = true;
85    else
86        stop_others = false;
87
88    ThreadPlan* new_plan = NULL;
89
90    FrameComparison frame_order = CompareCurrentFrameToStartFrame();
91
92    if (frame_order == eFrameCompareOlder)
93    {
94        // If we're in an older frame then we should stop.
95        //
96        // A caveat to this is if we think the frame is older but we're actually in a trampoline.
97        // I'm going to make the assumption that you wouldn't RETURN to a trampoline.  So if we are
98        // in a trampoline we think the frame is older because the trampoline confused the backtracer.
99        // As below, we step through first, and then try to figure out how to get back out again.
100
101        new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
102
103        if (new_plan != NULL && log)
104            log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
105    }
106    else if (frame_order == eFrameCompareYounger)
107    {
108        // Make sure we really are in a new frame.  Do that by unwinding and seeing if the
109        // start function really is our start function...
110        StackFrameSP older_frame_sp = m_thread.GetStackFrameAtIndex(1);
111
112        // But if we can't even unwind one frame we should just get out of here & stop...
113        if (older_frame_sp)
114        {
115            const SymbolContext &older_context = older_frame_sp->GetSymbolContext(eSymbolContextEverything);
116
117            // Match as much as is specified in the m_addr_context:
118            // This is a fairly loose sanity check.  Note, sometimes the target doesn't get filled
119            // in so I left out the target check.  And sometimes the module comes in as the .o file from the
120            // inlined range, so I left that out too...
121
122            bool older_ctx_is_equivalent = true;
123            if (m_addr_context.comp_unit)
124            {
125                if (m_addr_context.comp_unit == older_context.comp_unit)
126                {
127                    if (m_addr_context.function && m_addr_context.function == older_context.function)
128                    {
129                        if (m_addr_context.block && m_addr_context.block == older_context.block)
130                        {
131                            older_ctx_is_equivalent = true;
132                        }
133                    }
134                }
135            }
136            else if (m_addr_context.symbol && m_addr_context.symbol == older_context.symbol)
137            {
138                older_ctx_is_equivalent = true;
139            }
140
141            if (older_ctx_is_equivalent)
142            {
143                new_plan = m_thread.QueueThreadPlanForStepOut (false,
144                                                           NULL,
145                                                           true,
146                                                           stop_others,
147                                                           eVoteNo,
148                                                           eVoteNoOpinion,
149                                                           0);
150            }
151            else
152            {
153                new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
154
155            }
156        }
157    }
158    else
159    {
160        // If we're still in the range, keep going.
161        if (InRange())
162        {
163            SetNextBranchBreakpoint();
164            return false;
165        }
166
167
168        if (!InSymbol())
169        {
170            // This one is a little tricky.  Sometimes we may be in a stub or something similar,
171            // in which case we need to get out of there.  But if we are in a stub then it's
172            // likely going to be hard to get out from here.  It is probably easiest to step into the
173            // stub, and then it will be straight-forward to step out.
174            new_plan = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
175        }
176    }
177
178    // If we get to this point, we're not going to use a previously set "next branch" breakpoint, so delete it:
179    ClearNextBranchBreakpoint();
180
181    if (new_plan == NULL)
182        m_no_more_plans = true;
183    else
184        m_no_more_plans = false;
185
186    if (new_plan == NULL)
187    {
188        // For efficiencies sake, we know we're done here so we don't have to do this
189        // calculation again in MischiefManaged.
190        SetPlanComplete();
191        return true;
192    }
193    else
194        return false;
195}
196
197bool
198ThreadPlanStepOverRange::PlanExplainsStop ()
199{
200    // For crashes, breakpoint hits, signals, etc, let the base plan (or some plan above us)
201    // handle the stop.  That way the user can see the stop, step around, and then when they
202    // are done, continue and have their step complete.  The exception is if we've hit our
203    // "run to next branch" breakpoint.
204    // Note, unlike the step in range plan, we don't mark ourselves complete if we hit an
205    // unexplained breakpoint/crash.
206
207    LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
208    StopInfoSP stop_info_sp = GetPrivateStopReason();
209    if (stop_info_sp)
210    {
211        StopReason reason = stop_info_sp->GetStopReason();
212
213        switch (reason)
214        {
215        case eStopReasonTrace:
216            return true;
217            break;
218        case eStopReasonBreakpoint:
219            if (NextRangeBreakpointExplainsStop(stop_info_sp))
220                return true;
221            else
222                return false;
223            break;
224        case eStopReasonWatchpoint:
225        case eStopReasonSignal:
226        case eStopReasonException:
227        default:
228            if (log)
229                log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
230            return false;
231            break;
232        }
233    }
234    return true;
235}
236
237bool
238ThreadPlanStepOverRange::WillResume (lldb::StateType resume_state, bool current_plan)
239{
240    if (resume_state != eStateSuspended && m_first_resume)
241    {
242        m_first_resume = false;
243        if (resume_state == eStateStepping && current_plan)
244        {
245            // See if we are about to step over an inlined call in the middle of the inlined stack, if so figure
246            // out its extents and reset our range to step over that.
247            bool in_inlined_stack = m_thread.DecrementCurrentInlinedDepth();
248            if (in_inlined_stack)
249            {
250                LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
251                if (log)
252                    log->Printf ("ThreadPlanStepInRange::WillResume: adjusting range to the frame at inlined depth %d.",
253                                 m_thread.GetCurrentInlinedDepth());
254                StackFrameSP stack_sp = m_thread.GetStackFrameAtIndex(0);
255                if (stack_sp)
256                {
257                    Block *frame_block = stack_sp->GetFrameBlock();
258                    lldb::addr_t curr_pc = m_thread.GetRegisterContext()->GetPC();
259                    AddressRange my_range;
260                    if (frame_block->GetRangeContainingLoadAddress(curr_pc, m_thread.GetProcess()->GetTarget(), my_range))
261                    {
262                        m_address_ranges.clear();
263                        m_address_ranges.push_back(my_range);
264                        if (log)
265                        {
266                            StreamString s;
267                            const InlineFunctionInfo *inline_info = frame_block->GetInlinedFunctionInfo();
268                            const char *name;
269                            if (inline_info)
270                                name = inline_info->GetName().AsCString();
271                            else
272                                name = "<unknown-notinlined>";
273
274                            s.Printf ("Stepping over inlined function \"%s\" in inlined stack: ", name);
275                            DumpRanges(&s);
276                            log->PutCString(s.GetData());
277                        }
278                    }
279
280                }
281            }
282        }
283    }
284
285    return ThreadPlan::WillResume(resume_state, current_plan);
286}
287