1ac7ddfbfSEd Maste //===-- ThreadPlanStepThrough.cpp -------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
109f2f44ceSEd Maste #include "lldb/Target/ThreadPlanStepThrough.h"
11435933ddSDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
12*b5893f02SDimitry Andric #include "lldb/Target/CPPLanguageRuntime.h"
13ac7ddfbfSEd Maste #include "lldb/Target/DynamicLoader.h"
14ac7ddfbfSEd Maste #include "lldb/Target/ObjCLanguageRuntime.h"
15ac7ddfbfSEd Maste #include "lldb/Target/Process.h"
16ac7ddfbfSEd Maste #include "lldb/Target/RegisterContext.h"
17ac7ddfbfSEd Maste #include "lldb/Target/Target.h"
18f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
19f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
20ac7ddfbfSEd Maste 
21ac7ddfbfSEd Maste using namespace lldb;
22ac7ddfbfSEd Maste using namespace lldb_private;
23ac7ddfbfSEd Maste 
24ac7ddfbfSEd Maste //----------------------------------------------------------------------
25435933ddSDimitry Andric // ThreadPlanStepThrough: If the current instruction is a trampoline, step
264ba319b5SDimitry Andric // through it If it is the beginning of the prologue of a function, step
274ba319b5SDimitry Andric // through that as well.
28ac7ddfbfSEd Maste // FIXME: At present only handles DYLD trampolines.
29ac7ddfbfSEd Maste //----------------------------------------------------------------------
30ac7ddfbfSEd Maste 
ThreadPlanStepThrough(Thread & thread,StackID & m_stack_id,bool stop_others)31435933ddSDimitry Andric ThreadPlanStepThrough::ThreadPlanStepThrough(Thread &thread,
32435933ddSDimitry Andric                                              StackID &m_stack_id,
33435933ddSDimitry Andric                                              bool stop_others)
34435933ddSDimitry Andric     : ThreadPlan(ThreadPlan::eKindStepThrough,
35435933ddSDimitry Andric                  "Step through trampolines and prologues", thread,
36435933ddSDimitry Andric                  eVoteNoOpinion, eVoteNoOpinion),
37435933ddSDimitry Andric       m_start_address(0), m_backstop_bkpt_id(LLDB_INVALID_BREAK_ID),
38435933ddSDimitry Andric       m_backstop_addr(LLDB_INVALID_ADDRESS), m_return_stack_id(m_stack_id),
39435933ddSDimitry Andric       m_stop_others(stop_others) {
40ac7ddfbfSEd Maste   LookForPlanToStepThroughFromCurrentPC();
41ac7ddfbfSEd Maste 
42435933ddSDimitry Andric   // If we don't get a valid step through plan, don't bother to set up a
43435933ddSDimitry Andric   // backstop.
44435933ddSDimitry Andric   if (m_sub_plan_sp) {
45ac7ddfbfSEd Maste     m_start_address = GetThread().GetRegisterContext()->GetPC(0);
46ac7ddfbfSEd Maste 
47435933ddSDimitry Andric     // We are going to return back to the concrete frame 1, we might pass by
484ba319b5SDimitry Andric     // some inlined code that we're in the middle of by doing this, but it's
494ba319b5SDimitry Andric     // easier than trying to figure out where the inlined code might return to.
50ac7ddfbfSEd Maste 
51ac7ddfbfSEd Maste     StackFrameSP return_frame_sp = m_thread.GetFrameWithStackID(m_stack_id);
52ac7ddfbfSEd Maste 
53435933ddSDimitry Andric     if (return_frame_sp) {
54435933ddSDimitry Andric       m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(
55435933ddSDimitry Andric           m_thread.CalculateTarget().get());
56435933ddSDimitry Andric       Breakpoint *return_bp =
57435933ddSDimitry Andric           m_thread.GetProcess()
58435933ddSDimitry Andric               ->GetTarget()
59435933ddSDimitry Andric               .CreateBreakpoint(m_backstop_addr, true, false)
60435933ddSDimitry Andric               .get();
61*b5893f02SDimitry Andric 
62435933ddSDimitry Andric       if (return_bp != nullptr) {
63*b5893f02SDimitry Andric         if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
64*b5893f02SDimitry Andric           m_could_not_resolve_hw_bp = true;
65ac7ddfbfSEd Maste         return_bp->SetThreadID(m_thread.GetID());
66ac7ddfbfSEd Maste         m_backstop_bkpt_id = return_bp->GetID();
67ac7ddfbfSEd Maste         return_bp->SetBreakpointKind("step-through-backstop");
68ac7ddfbfSEd Maste       }
69ac7ddfbfSEd Maste       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
70435933ddSDimitry Andric       if (log) {
71435933ddSDimitry Andric         log->Printf("Setting backstop breakpoint %d at address: 0x%" PRIx64,
72435933ddSDimitry Andric                     m_backstop_bkpt_id, m_backstop_addr);
73ac7ddfbfSEd Maste       }
74ac7ddfbfSEd Maste     }
75ac7ddfbfSEd Maste   }
76ac7ddfbfSEd Maste }
77ac7ddfbfSEd Maste 
~ThreadPlanStepThrough()78435933ddSDimitry Andric ThreadPlanStepThrough::~ThreadPlanStepThrough() { ClearBackstopBreakpoint(); }
79ac7ddfbfSEd Maste 
DidPush()80435933ddSDimitry Andric void ThreadPlanStepThrough::DidPush() {
81ac7ddfbfSEd Maste   if (m_sub_plan_sp)
82ac7ddfbfSEd Maste     PushPlan(m_sub_plan_sp);
83ac7ddfbfSEd Maste }
84ac7ddfbfSEd Maste 
LookForPlanToStepThroughFromCurrentPC()85435933ddSDimitry Andric void ThreadPlanStepThrough::LookForPlanToStepThroughFromCurrentPC() {
867aa51b79SEd Maste   DynamicLoader *loader = m_thread.GetProcess()->GetDynamicLoader();
877aa51b79SEd Maste   if (loader)
88435933ddSDimitry Andric     m_sub_plan_sp =
89435933ddSDimitry Andric         loader->GetStepThroughTrampolinePlan(m_thread, m_stop_others);
907aa51b79SEd Maste 
91ac7ddfbfSEd Maste   // If that didn't come up with anything, try the ObjC runtime plugin:
92435933ddSDimitry Andric   if (!m_sub_plan_sp.get()) {
93435933ddSDimitry Andric     ObjCLanguageRuntime *objc_runtime =
94435933ddSDimitry Andric         m_thread.GetProcess()->GetObjCLanguageRuntime();
95ac7ddfbfSEd Maste     if (objc_runtime)
96435933ddSDimitry Andric       m_sub_plan_sp =
97435933ddSDimitry Andric           objc_runtime->GetStepThroughTrampolinePlan(m_thread, m_stop_others);
98*b5893f02SDimitry Andric 
99*b5893f02SDimitry Andric     CPPLanguageRuntime *cpp_runtime =
100*b5893f02SDimitry Andric         m_thread.GetProcess()->GetCPPLanguageRuntime();
101*b5893f02SDimitry Andric 
102*b5893f02SDimitry Andric     // If the ObjC runtime did not provide us with a step though plan then if we
103*b5893f02SDimitry Andric     // have it check the C++ runtime for a step though plan.
104*b5893f02SDimitry Andric     if (!m_sub_plan_sp.get() && cpp_runtime)
105*b5893f02SDimitry Andric       m_sub_plan_sp =
106*b5893f02SDimitry Andric           cpp_runtime->GetStepThroughTrampolinePlan(m_thread, m_stop_others);
107ac7ddfbfSEd Maste   }
108ac7ddfbfSEd Maste 
109ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
110435933ddSDimitry Andric   if (log) {
111ac7ddfbfSEd Maste     lldb::addr_t current_address = GetThread().GetRegisterContext()->GetPC(0);
112435933ddSDimitry Andric     if (m_sub_plan_sp) {
113ac7ddfbfSEd Maste       StreamString s;
114ac7ddfbfSEd Maste       m_sub_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull);
115435933ddSDimitry Andric       log->Printf("Found step through plan from 0x%" PRIx64 ": %s",
116435933ddSDimitry Andric                   current_address, s.GetData());
117435933ddSDimitry Andric     } else {
118435933ddSDimitry Andric       log->Printf("Couldn't find step through plan from address 0x%" PRIx64 ".",
119435933ddSDimitry Andric                   current_address);
120ac7ddfbfSEd Maste     }
121ac7ddfbfSEd Maste   }
122ac7ddfbfSEd Maste }
123ac7ddfbfSEd Maste 
GetDescription(Stream * s,lldb::DescriptionLevel level)124435933ddSDimitry Andric void ThreadPlanStepThrough::GetDescription(Stream *s,
125435933ddSDimitry Andric                                            lldb::DescriptionLevel level) {
126ac7ddfbfSEd Maste   if (level == lldb::eDescriptionLevelBrief)
127ac7ddfbfSEd Maste     s->Printf("Step through");
128435933ddSDimitry Andric   else {
129ac7ddfbfSEd Maste     s->PutCString("Stepping through trampoline code from: ");
130ac7ddfbfSEd Maste     s->Address(m_start_address, sizeof(addr_t));
131435933ddSDimitry Andric     if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
132435933ddSDimitry Andric       s->Printf(" with backstop breakpoint ID: %d at address: ",
133435933ddSDimitry Andric                 m_backstop_bkpt_id);
134ac7ddfbfSEd Maste       s->Address(m_backstop_addr, sizeof(addr_t));
135435933ddSDimitry Andric     } else
136ac7ddfbfSEd Maste       s->PutCString(" unable to set a backstop breakpoint.");
137ac7ddfbfSEd Maste   }
138ac7ddfbfSEd Maste }
139ac7ddfbfSEd Maste 
ValidatePlan(Stream * error)140435933ddSDimitry Andric bool ThreadPlanStepThrough::ValidatePlan(Stream *error) {
141*b5893f02SDimitry Andric   if (m_could_not_resolve_hw_bp) {
142*b5893f02SDimitry Andric     if (error)
143*b5893f02SDimitry Andric       error->PutCString(
144*b5893f02SDimitry Andric           "Could not create hardware breakpoint for thread plan.");
145*b5893f02SDimitry Andric     return false;
146*b5893f02SDimitry Andric   }
147*b5893f02SDimitry Andric 
148*b5893f02SDimitry Andric   if (m_backstop_bkpt_id == LLDB_INVALID_BREAK_ID) {
149*b5893f02SDimitry Andric     if (error)
150*b5893f02SDimitry Andric       error->PutCString("Could not create backstop breakpoint.");
151*b5893f02SDimitry Andric     return false;
152*b5893f02SDimitry Andric   }
153*b5893f02SDimitry Andric 
154*b5893f02SDimitry Andric   if (!m_sub_plan_sp.get()) {
155*b5893f02SDimitry Andric     if (error)
156*b5893f02SDimitry Andric       error->PutCString("Does not have a subplan.");
157*b5893f02SDimitry Andric     return false;
158*b5893f02SDimitry Andric   }
159*b5893f02SDimitry Andric 
160*b5893f02SDimitry Andric   return true;
161ac7ddfbfSEd Maste }
162ac7ddfbfSEd Maste 
DoPlanExplainsStop(Event * event_ptr)163435933ddSDimitry Andric bool ThreadPlanStepThrough::DoPlanExplainsStop(Event *event_ptr) {
164435933ddSDimitry Andric   // If we have a sub-plan, it will have been asked first if we explain the
1654ba319b5SDimitry Andric   // stop, and we won't get asked.  The only time we would be the one directly
1664ba319b5SDimitry Andric   // asked this question is if we hit our backstop breakpoint.
167ac7ddfbfSEd Maste 
1689f2f44ceSEd Maste   return HitOurBackstopBreakpoint();
169ac7ddfbfSEd Maste }
170ac7ddfbfSEd Maste 
ShouldStop(Event * event_ptr)171435933ddSDimitry Andric bool ThreadPlanStepThrough::ShouldStop(Event *event_ptr) {
172ac7ddfbfSEd Maste   // If we've already marked ourselves done, then we're done...
173ac7ddfbfSEd Maste   if (IsPlanComplete())
174ac7ddfbfSEd Maste     return true;
175ac7ddfbfSEd Maste 
176ac7ddfbfSEd Maste   // First, did we hit the backstop breakpoint?
177435933ddSDimitry Andric   if (HitOurBackstopBreakpoint()) {
17812b93ac6SEd Maste     SetPlanComplete(true);
179ac7ddfbfSEd Maste     return true;
180ac7ddfbfSEd Maste   }
181ac7ddfbfSEd Maste 
182435933ddSDimitry Andric   // If we don't have a sub-plan, then we're also done (can't see how we would
1834ba319b5SDimitry Andric   // ever get here without a plan, but just in case.
184ac7ddfbfSEd Maste 
185435933ddSDimitry Andric   if (!m_sub_plan_sp) {
186ac7ddfbfSEd Maste     SetPlanComplete();
187ac7ddfbfSEd Maste     return true;
188ac7ddfbfSEd Maste   }
189ac7ddfbfSEd Maste 
190435933ddSDimitry Andric   // If the current sub plan is not done, we don't want to stop.  Actually, we
1914ba319b5SDimitry Andric   // probably won't ever get here in this state, since we generally won't get
1924ba319b5SDimitry Andric   // asked any questions if out current sub-plan is not done...
193ac7ddfbfSEd Maste   if (!m_sub_plan_sp->IsPlanComplete())
194ac7ddfbfSEd Maste     return false;
195ac7ddfbfSEd Maste 
1964ba319b5SDimitry Andric   // If our current sub plan failed, then let's just run to our backstop.  If
1974ba319b5SDimitry Andric   // we can't do that then just stop.
198435933ddSDimitry Andric   if (!m_sub_plan_sp->PlanSucceeded()) {
199435933ddSDimitry Andric     if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
200ac7ddfbfSEd Maste       m_sub_plan_sp.reset();
201ac7ddfbfSEd Maste       return false;
202435933ddSDimitry Andric     } else {
203ac7ddfbfSEd Maste       SetPlanComplete(false);
204ac7ddfbfSEd Maste       return true;
205ac7ddfbfSEd Maste     }
206ac7ddfbfSEd Maste   }
207ac7ddfbfSEd Maste 
208435933ddSDimitry Andric   // Next see if there is a specific step through plan at our current pc (these
2094ba319b5SDimitry Andric   // might chain, for instance stepping through a dylib trampoline to the objc
210435933ddSDimitry Andric   // dispatch function...)
211ac7ddfbfSEd Maste   LookForPlanToStepThroughFromCurrentPC();
212435933ddSDimitry Andric   if (m_sub_plan_sp) {
213ac7ddfbfSEd Maste     PushPlan(m_sub_plan_sp);
214ac7ddfbfSEd Maste     return false;
215435933ddSDimitry Andric   } else {
216ac7ddfbfSEd Maste     SetPlanComplete();
217ac7ddfbfSEd Maste     return true;
218ac7ddfbfSEd Maste   }
219ac7ddfbfSEd Maste }
220ac7ddfbfSEd Maste 
StopOthers()221435933ddSDimitry Andric bool ThreadPlanStepThrough::StopOthers() { return m_stop_others; }
222ac7ddfbfSEd Maste 
GetPlanRunState()223435933ddSDimitry Andric StateType ThreadPlanStepThrough::GetPlanRunState() { return eStateRunning; }
224ac7ddfbfSEd Maste 
DoWillResume(StateType resume_state,bool current_plan)225435933ddSDimitry Andric bool ThreadPlanStepThrough::DoWillResume(StateType resume_state,
226435933ddSDimitry Andric                                          bool current_plan) {
227ac7ddfbfSEd Maste   return true;
228ac7ddfbfSEd Maste }
229ac7ddfbfSEd Maste 
WillStop()230435933ddSDimitry Andric bool ThreadPlanStepThrough::WillStop() { return true; }
231ac7ddfbfSEd Maste 
ClearBackstopBreakpoint()232435933ddSDimitry Andric void ThreadPlanStepThrough::ClearBackstopBreakpoint() {
233435933ddSDimitry Andric   if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
234ac7ddfbfSEd Maste     m_thread.GetProcess()->GetTarget().RemoveBreakpointByID(m_backstop_bkpt_id);
235ac7ddfbfSEd Maste     m_backstop_bkpt_id = LLDB_INVALID_BREAK_ID;
236*b5893f02SDimitry Andric     m_could_not_resolve_hw_bp = false;
237ac7ddfbfSEd Maste   }
238ac7ddfbfSEd Maste }
239ac7ddfbfSEd Maste 
MischiefManaged()240435933ddSDimitry Andric bool ThreadPlanStepThrough::MischiefManaged() {
241ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
242ac7ddfbfSEd Maste 
243435933ddSDimitry Andric   if (!IsPlanComplete()) {
244ac7ddfbfSEd Maste     return false;
245435933ddSDimitry Andric   } else {
246ac7ddfbfSEd Maste     if (log)
247ac7ddfbfSEd Maste       log->Printf("Completed step through step plan.");
248ac7ddfbfSEd Maste 
249ac7ddfbfSEd Maste     ClearBackstopBreakpoint();
250ac7ddfbfSEd Maste     ThreadPlan::MischiefManaged();
251ac7ddfbfSEd Maste     return true;
252ac7ddfbfSEd Maste   }
253ac7ddfbfSEd Maste }
254ac7ddfbfSEd Maste 
HitOurBackstopBreakpoint()255435933ddSDimitry Andric bool ThreadPlanStepThrough::HitOurBackstopBreakpoint() {
256ac7ddfbfSEd Maste   StopInfoSP stop_info_sp(m_thread.GetStopInfo());
257435933ddSDimitry Andric   if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
258ac7ddfbfSEd Maste     break_id_t stop_value = (break_id_t)stop_info_sp->GetValue();
259435933ddSDimitry Andric     BreakpointSiteSP cur_site_sp =
260435933ddSDimitry Andric         m_thread.GetProcess()->GetBreakpointSiteList().FindByID(stop_value);
261435933ddSDimitry Andric     if (cur_site_sp &&
262435933ddSDimitry Andric         cur_site_sp->IsBreakpointAtThisSite(m_backstop_bkpt_id)) {
263435933ddSDimitry Andric       StackID cur_frame_zero_id =
264435933ddSDimitry Andric           m_thread.GetStackFrameAtIndex(0)->GetStackID();
265ac7ddfbfSEd Maste 
266435933ddSDimitry Andric       if (cur_frame_zero_id == m_return_stack_id) {
267ac7ddfbfSEd Maste         Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
268ac7ddfbfSEd Maste         if (log)
269ac7ddfbfSEd Maste           log->PutCString("ThreadPlanStepThrough hit backstop breakpoint.");
270ac7ddfbfSEd Maste         return true;
271ac7ddfbfSEd Maste       }
272ac7ddfbfSEd Maste     }
273ac7ddfbfSEd Maste   }
274ac7ddfbfSEd Maste   return false;
275ac7ddfbfSEd Maste }
276