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/Target/Process.h"
21 #include "lldb/Target/RegisterContext.h"
22 #include "lldb/Target/Thread.h"
23 #include "lldb/Target/ThreadPlanStepOut.h"
24 #include "lldb/Target/ThreadPlanStepThrough.h"
25 
26 using namespace lldb_private;
27 using namespace lldb;
28 
29 
30 //----------------------------------------------------------------------
31 // ThreadPlanStepOverRange: Step through a stack range, either stepping over or into
32 // based on the value of \a type.
33 //----------------------------------------------------------------------
34 
35 ThreadPlanStepOverRange::ThreadPlanStepOverRange
36 (
37     Thread &thread,
38     const AddressRange &range,
39     const SymbolContext &addr_context,
40     lldb::RunMode stop_others,
41     bool okay_to_discard
42 ) :
43     ThreadPlanStepRange (ThreadPlan::eKindStepOverRange, "Step range stepping over", thread, range, addr_context, stop_others)
44 {
45     SetOkayToDiscard (okay_to_discard);
46 }
47 
48 ThreadPlanStepOverRange::~ThreadPlanStepOverRange ()
49 {
50 }
51 
52 void
53 ThreadPlanStepOverRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
54 {
55     if (level == lldb::eDescriptionLevelBrief)
56         s->Printf("step over");
57     else
58     {
59         s->Printf ("stepping through range (stepping over functions): ");
60         m_address_range.Dump (s, &m_thread.GetProcess().GetTarget(), Address::DumpStyleLoadAddress);
61     }
62 }
63 
64 bool
65 ThreadPlanStepOverRange::ShouldStop (Event *event_ptr)
66 {
67     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
68 
69     if (log)
70     {
71         StreamString s;
72         s.Address (m_thread.GetRegisterContext()->GetPC(), m_thread.GetProcess().GetAddressByteSize());
73         log->Printf("ThreadPlanStepOverRange reached %s.", s.GetData());
74     }
75 
76     // If we're still in the range, keep going.
77     if (InRange())
78         return false;
79 
80     // If we're out of the range but in the same frame or in our caller's frame
81     // then we should stop.
82     // When stepping out we only step if we are forcing running one thread.
83     bool stop_others;
84     if (m_stop_others == lldb::eOnlyThisThread)
85         stop_others = true;
86     else
87         stop_others = false;
88 
89     ThreadPlan* new_plan = NULL;
90 
91     if (FrameIsOlder())
92     {
93         // If we're in an older frame then we should stop.
94         //
95         // A caveat to this is if we think the frame is older but we're actually in a trampoline.
96         // I'm going to make the assumption that you wouldn't RETURN to a trampoline.  So if we are
97         // in a trampoline we think the frame is older because the trampoline confused the backtracer.
98         // As below, we step through first, and then try to figure out how to get back out again.
99 
100         new_plan = m_thread.QueueThreadPlanForStepThrough (false, stop_others);
101 
102         if (new_plan != NULL && log)
103             log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
104     }
105     else if (FrameIsYounger())
106     {
107         new_plan = m_thread.QueueThreadPlanForStepOut (false,
108                                                        NULL,
109                                                        true,
110                                                        stop_others,
111                                                        lldb::eVoteNo,
112                                                        lldb::eVoteNoOpinion,
113                                                        0);
114     }
115     else if (!InSymbol())
116     {
117         // This one is a little tricky.  Sometimes we may be in a stub or something similar,
118         // in which case we need to get out of there.  But if we are in a stub then it's
119         // likely going to be hard to get out from here.  It is probably easiest to step into the
120         // stub, and then it will be straight-forward to step out.
121         new_plan = m_thread.QueueThreadPlanForStepThrough (false, stop_others);
122     }
123 
124     if (new_plan == NULL)
125         m_no_more_plans = true;
126     else
127         m_no_more_plans = false;
128 
129     if (new_plan == NULL)
130     {
131         // For efficiencies sake, we know we're done here so we don't have to do this
132         // calculation again in MischiefManaged.
133         SetPlanComplete();
134         return true;
135     }
136     else
137         return false;
138 }
139