1 //===-- ThreadPlanStepThrough.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/ThreadPlanStepThrough.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 // Project includes
16 #include "lldb/lldb-private-log.h"
17 #include "lldb/Core/Log.h"
18 #include "lldb/Core/Stream.h"
19 #include "lldb/Target/DynamicLoader.h"
20 #include "lldb/Target/ObjCLanguageRuntime.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Target/RegisterContext.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Breakpoint/Breakpoint.h"
25 
26 using namespace lldb;
27 using namespace lldb_private;
28 
29 //----------------------------------------------------------------------
30 // ThreadPlanStepThrough: If the current instruction is a trampoline, step through it
31 // If it is the beginning of the prologue of a function, step through that as well.
32 // FIXME: At present only handles DYLD trampolines.
33 //----------------------------------------------------------------------
34 
35 ThreadPlanStepThrough::ThreadPlanStepThrough (Thread &thread, StackID &m_stack_id, bool stop_others) :
36     ThreadPlan (ThreadPlan::eKindStepThrough, "Step through trampolines and prologues", thread, eVoteNoOpinion, eVoteNoOpinion),
37     m_start_address (0),
38     m_backstop_bkpt_id (LLDB_INVALID_BREAK_ID),
39     m_backstop_addr(LLDB_INVALID_ADDRESS),
40     m_return_stack_id (m_stack_id),
41     m_stop_others (stop_others)
42 {
43 
44     LookForPlanToStepThroughFromCurrentPC();
45 
46     // If we don't get a valid step through plan, don't bother to set up a backstop.
47     if (m_sub_plan_sp)
48     {
49         m_start_address = GetThread().GetRegisterContext()->GetPC(0);
50 
51         // We are going to return back to the concrete frame 1, we might pass by some inlined code that we're in
52         // the middle of by doing this, but it's easier than trying to figure out where the inlined code might return to.
53 
54         StackFrameSP return_frame_sp = m_thread.GetFrameWithStackID (m_stack_id);
55 
56         if (return_frame_sp)
57         {
58             m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(m_thread.CalculateTarget().get());
59             Breakpoint *return_bp = m_thread.GetProcess()->GetTarget().CreateBreakpoint (m_backstop_addr, true, false).get();
60             if (return_bp != NULL)
61             {
62                 return_bp->SetThreadID(m_thread.GetID());
63                 m_backstop_bkpt_id = return_bp->GetID();
64                 return_bp->SetBreakpointKind("step-through-backstop");
65             }
66             Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
67             if (log)
68             {
69                 log->Printf ("Setting backstop breakpoint %d at address: 0x%" PRIx64, m_backstop_bkpt_id, m_backstop_addr);
70             }
71         }
72     }
73 }
74 
75 ThreadPlanStepThrough::~ThreadPlanStepThrough ()
76 {
77     ClearBackstopBreakpoint ();
78 }
79 
80 void
81 ThreadPlanStepThrough::DidPush ()
82 {
83     if (m_sub_plan_sp)
84         PushPlan(m_sub_plan_sp);
85 }
86 
87 void
88 ThreadPlanStepThrough::LookForPlanToStepThroughFromCurrentPC()
89 {
90     DynamicLoader *loader = m_thread.GetProcess()->GetDynamicLoader();
91     if (loader)
92         m_sub_plan_sp = loader->GetStepThroughTrampolinePlan (m_thread, m_stop_others);
93 
94     // If that didn't come up with anything, try the ObjC runtime plugin:
95     if (!m_sub_plan_sp.get())
96     {
97         ObjCLanguageRuntime *objc_runtime = m_thread.GetProcess()->GetObjCLanguageRuntime();
98         if (objc_runtime)
99             m_sub_plan_sp = objc_runtime->GetStepThroughTrampolinePlan (m_thread, m_stop_others);
100     }
101 
102     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
103     if (log)
104     {
105         lldb::addr_t current_address = GetThread().GetRegisterContext()->GetPC(0);
106         if (m_sub_plan_sp)
107         {
108             StreamString s;
109             m_sub_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull);
110             log->Printf ("Found step through plan from 0x%" PRIx64 ": %s", current_address, s.GetData());
111         }
112         else
113         {
114             log->Printf ("Couldn't find step through plan from address 0x%" PRIx64 ".", current_address);
115         }
116     }
117 }
118 
119 void
120 ThreadPlanStepThrough::GetDescription (Stream *s, lldb::DescriptionLevel level)
121 {
122     if (level == lldb::eDescriptionLevelBrief)
123         s->Printf ("Step through");
124     else
125     {
126         s->PutCString ("Stepping through trampoline code from: ");
127         s->Address(m_start_address, sizeof (addr_t));
128         if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID)
129         {
130             s->Printf (" with backstop breakpoint id: %d at address: ", m_backstop_bkpt_id);
131             s->Address (m_backstop_addr, sizeof (addr_t));
132         }
133         else
134             s->PutCString (" unable to set a backstop breakpoint.");
135     }
136 }
137 
138 bool
139 ThreadPlanStepThrough::ValidatePlan (Stream *error)
140 {
141     return m_sub_plan_sp.get() != NULL;
142 }
143 
144 bool
145 ThreadPlanStepThrough::DoPlanExplainsStop (Event *event_ptr)
146 {
147     // If we have a sub-plan, it will have been asked first if we explain the stop, and
148     // we won't get asked.  The only time we would be the one directly asked this question
149     // is if we hit our backstop breakpoint.
150 
151     if (HitOurBackstopBreakpoint())
152         return true;
153     else
154         return false;
155 }
156 
157 bool
158 ThreadPlanStepThrough::ShouldStop (Event *event_ptr)
159 {
160     // If we've already marked ourselves done, then we're done...
161     if (IsPlanComplete())
162         return true;
163 
164     // First, did we hit the backstop breakpoint?
165     if (HitOurBackstopBreakpoint())
166     {
167         SetPlanComplete(true);
168         return true;
169     }
170 
171     // If we don't have a sub-plan, then we're also done (can't see how we would ever get here
172     // without a plan, but just in case.
173 
174     if (!m_sub_plan_sp)
175     {
176         SetPlanComplete();
177         return true;
178     }
179 
180     // If the current sub plan is not done, we don't want to stop.  Actually, we probably won't
181     // ever get here in this state, since we generally won't get asked any questions if out
182     // current sub-plan is not done...
183     if (!m_sub_plan_sp->IsPlanComplete())
184         return false;
185 
186     // If our current sub plan failed, then let's just run to our backstop.  If we can't do that then just stop.
187     if (!m_sub_plan_sp->PlanSucceeded())
188     {
189         if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID)
190         {
191             m_sub_plan_sp.reset();
192             return false;
193         }
194         else
195         {
196             SetPlanComplete(false);
197             return true;
198         }
199     }
200 
201     // Next see if there is a specific step through plan at our current pc (these might
202     // chain, for instance stepping through a dylib trampoline to the objc dispatch function...)
203     LookForPlanToStepThroughFromCurrentPC();
204     if (m_sub_plan_sp)
205     {
206         PushPlan (m_sub_plan_sp);
207         return false;
208     }
209     else
210     {
211         SetPlanComplete();
212         return true;
213     }
214 }
215 
216 bool
217 ThreadPlanStepThrough::StopOthers ()
218 {
219     return m_stop_others;
220 }
221 
222 StateType
223 ThreadPlanStepThrough::GetPlanRunState ()
224 {
225     return eStateRunning;
226 }
227 
228 bool
229 ThreadPlanStepThrough::DoWillResume (StateType resume_state, bool current_plan)
230 {
231     return true;
232 }
233 
234 bool
235 ThreadPlanStepThrough::WillStop ()
236 {
237     return true;
238 }
239 
240 void
241 ThreadPlanStepThrough::ClearBackstopBreakpoint ()
242 {
243     if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID)
244     {
245         m_thread.GetProcess()->GetTarget().RemoveBreakpointByID (m_backstop_bkpt_id);
246         m_backstop_bkpt_id = LLDB_INVALID_BREAK_ID;
247     }
248 }
249 
250 bool
251 ThreadPlanStepThrough::MischiefManaged ()
252 {
253     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
254 
255     if (!IsPlanComplete())
256     {
257         return false;
258     }
259     else
260     {
261         if (log)
262             log->Printf("Completed step through step plan.");
263 
264         ClearBackstopBreakpoint ();
265         ThreadPlan::MischiefManaged ();
266         return true;
267     }
268 }
269 
270 bool
271 ThreadPlanStepThrough::HitOurBackstopBreakpoint()
272 {
273     StopInfoSP stop_info_sp(m_thread.GetStopInfo());
274     if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint)
275     {
276         break_id_t stop_value = (break_id_t) stop_info_sp->GetValue();
277         BreakpointSiteSP cur_site_sp = m_thread.GetProcess()->GetBreakpointSiteList().FindByID(stop_value);
278         if (cur_site_sp && cur_site_sp->IsBreakpointAtThisSite(m_backstop_bkpt_id))
279         {
280             StackID cur_frame_zero_id = m_thread.GetStackFrameAtIndex(0)->GetStackID();
281 
282             if (cur_frame_zero_id == m_return_stack_id)
283             {
284                 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
285                 if (log)
286                     log->PutCString ("ThreadPlanStepThrough hit backstop breakpoint.");
287                 return true;
288             }
289         }
290     }
291     return false;
292 }
293 
294