15ffd83dbSDimitry Andric //===-- ThreadPlanStepThrough.cpp -----------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanStepThrough.h"
10*0b57cec5SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
11*0b57cec5SDimitry Andric #include "lldb/Target/DynamicLoader.h"
12*0b57cec5SDimitry Andric #include "lldb/Target/LanguageRuntime.h"
13*0b57cec5SDimitry Andric #include "lldb/Target/Process.h"
14*0b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
15*0b57cec5SDimitry Andric #include "lldb/Target/Target.h"
16*0b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
17*0b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
18*0b57cec5SDimitry Andric 
19*0b57cec5SDimitry Andric using namespace lldb;
20*0b57cec5SDimitry Andric using namespace lldb_private;
21*0b57cec5SDimitry Andric 
22*0b57cec5SDimitry Andric // ThreadPlanStepThrough: If the current instruction is a trampoline, step
23*0b57cec5SDimitry Andric // through it If it is the beginning of the prologue of a function, step
24*0b57cec5SDimitry Andric // through that as well.
25*0b57cec5SDimitry Andric 
ThreadPlanStepThrough(Thread & thread,StackID & m_stack_id,bool stop_others)26*0b57cec5SDimitry Andric ThreadPlanStepThrough::ThreadPlanStepThrough(Thread &thread,
27*0b57cec5SDimitry Andric                                              StackID &m_stack_id,
28*0b57cec5SDimitry Andric                                              bool stop_others)
29*0b57cec5SDimitry Andric     : ThreadPlan(ThreadPlan::eKindStepThrough,
30*0b57cec5SDimitry Andric                  "Step through trampolines and prologues", thread,
31*0b57cec5SDimitry Andric                  eVoteNoOpinion, eVoteNoOpinion),
32*0b57cec5SDimitry Andric       m_start_address(0), m_backstop_bkpt_id(LLDB_INVALID_BREAK_ID),
33*0b57cec5SDimitry Andric       m_backstop_addr(LLDB_INVALID_ADDRESS), m_return_stack_id(m_stack_id),
34*0b57cec5SDimitry Andric       m_stop_others(stop_others) {
35*0b57cec5SDimitry Andric   LookForPlanToStepThroughFromCurrentPC();
36*0b57cec5SDimitry Andric 
37*0b57cec5SDimitry Andric   // If we don't get a valid step through plan, don't bother to set up a
38*0b57cec5SDimitry Andric   // backstop.
39*0b57cec5SDimitry Andric   if (m_sub_plan_sp) {
40*0b57cec5SDimitry Andric     m_start_address = GetThread().GetRegisterContext()->GetPC(0);
41*0b57cec5SDimitry Andric 
42*0b57cec5SDimitry Andric     // We are going to return back to the concrete frame 1, we might pass by
43*0b57cec5SDimitry Andric     // some inlined code that we're in the middle of by doing this, but it's
44*0b57cec5SDimitry Andric     // easier than trying to figure out where the inlined code might return to.
45*0b57cec5SDimitry Andric 
465ffd83dbSDimitry Andric     StackFrameSP return_frame_sp = thread.GetFrameWithStackID(m_stack_id);
47*0b57cec5SDimitry Andric 
48*0b57cec5SDimitry Andric     if (return_frame_sp) {
49*0b57cec5SDimitry Andric       m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(
505ffd83dbSDimitry Andric           thread.CalculateTarget().get());
51*0b57cec5SDimitry Andric       Breakpoint *return_bp =
525ffd83dbSDimitry Andric           m_process.GetTarget()
53*0b57cec5SDimitry Andric               .CreateBreakpoint(m_backstop_addr, true, false)
54*0b57cec5SDimitry Andric               .get();
55*0b57cec5SDimitry Andric 
56*0b57cec5SDimitry Andric       if (return_bp != nullptr) {
57*0b57cec5SDimitry Andric         if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
58*0b57cec5SDimitry Andric           m_could_not_resolve_hw_bp = true;
595ffd83dbSDimitry Andric         return_bp->SetThreadID(m_tid);
60*0b57cec5SDimitry Andric         m_backstop_bkpt_id = return_bp->GetID();
61*0b57cec5SDimitry Andric         return_bp->SetBreakpointKind("step-through-backstop");
62*0b57cec5SDimitry Andric       }
63*0b57cec5SDimitry Andric       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
64*0b57cec5SDimitry Andric       if (log) {
659dba64beSDimitry Andric         LLDB_LOGF(log, "Setting backstop breakpoint %d at address: 0x%" PRIx64,
66*0b57cec5SDimitry Andric                   m_backstop_bkpt_id, m_backstop_addr);
67*0b57cec5SDimitry Andric       }
68*0b57cec5SDimitry Andric     }
69*0b57cec5SDimitry Andric   }
70*0b57cec5SDimitry Andric }
71*0b57cec5SDimitry Andric 
~ThreadPlanStepThrough()72*0b57cec5SDimitry Andric ThreadPlanStepThrough::~ThreadPlanStepThrough() { ClearBackstopBreakpoint(); }
73*0b57cec5SDimitry Andric 
DidPush()74*0b57cec5SDimitry Andric void ThreadPlanStepThrough::DidPush() {
75*0b57cec5SDimitry Andric   if (m_sub_plan_sp)
76*0b57cec5SDimitry Andric     PushPlan(m_sub_plan_sp);
77*0b57cec5SDimitry Andric }
78*0b57cec5SDimitry Andric 
LookForPlanToStepThroughFromCurrentPC()79*0b57cec5SDimitry Andric void ThreadPlanStepThrough::LookForPlanToStepThroughFromCurrentPC() {
805ffd83dbSDimitry Andric   Thread &thread = GetThread();
815ffd83dbSDimitry Andric   DynamicLoader *loader = thread.GetProcess()->GetDynamicLoader();
82*0b57cec5SDimitry Andric   if (loader)
835ffd83dbSDimitry Andric     m_sub_plan_sp = loader->GetStepThroughTrampolinePlan(thread, m_stop_others);
84*0b57cec5SDimitry Andric 
85*0b57cec5SDimitry Andric   // If the DynamicLoader was unable to provide us with a ThreadPlan, then we
86*0b57cec5SDimitry Andric   // try the LanguageRuntimes.
87*0b57cec5SDimitry Andric   if (!m_sub_plan_sp) {
885ffd83dbSDimitry Andric     for (LanguageRuntime *runtime : m_process.GetLanguageRuntimes()) {
89*0b57cec5SDimitry Andric       m_sub_plan_sp =
905ffd83dbSDimitry Andric           runtime->GetStepThroughTrampolinePlan(thread, m_stop_others);
91*0b57cec5SDimitry Andric 
92*0b57cec5SDimitry Andric       if (m_sub_plan_sp)
93*0b57cec5SDimitry Andric         break;
94*0b57cec5SDimitry Andric     }
95*0b57cec5SDimitry Andric   }
96*0b57cec5SDimitry Andric 
97*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
98*0b57cec5SDimitry Andric   if (log) {
99*0b57cec5SDimitry Andric     lldb::addr_t current_address = GetThread().GetRegisterContext()->GetPC(0);
100*0b57cec5SDimitry Andric     if (m_sub_plan_sp) {
101*0b57cec5SDimitry Andric       StreamString s;
102*0b57cec5SDimitry Andric       m_sub_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull);
1039dba64beSDimitry Andric       LLDB_LOGF(log, "Found step through plan from 0x%" PRIx64 ": %s",
104*0b57cec5SDimitry Andric                 current_address, s.GetData());
105*0b57cec5SDimitry Andric     } else {
1069dba64beSDimitry Andric       LLDB_LOGF(log,
1079dba64beSDimitry Andric                 "Couldn't find step through plan from address 0x%" PRIx64 ".",
108*0b57cec5SDimitry Andric                 current_address);
109*0b57cec5SDimitry Andric     }
110*0b57cec5SDimitry Andric   }
111*0b57cec5SDimitry Andric }
112*0b57cec5SDimitry Andric 
GetDescription(Stream * s,lldb::DescriptionLevel level)113*0b57cec5SDimitry Andric void ThreadPlanStepThrough::GetDescription(Stream *s,
114*0b57cec5SDimitry Andric                                            lldb::DescriptionLevel level) {
115*0b57cec5SDimitry Andric   if (level == lldb::eDescriptionLevelBrief)
116*0b57cec5SDimitry Andric     s->Printf("Step through");
117*0b57cec5SDimitry Andric   else {
118*0b57cec5SDimitry Andric     s->PutCString("Stepping through trampoline code from: ");
119480093f4SDimitry Andric     DumpAddress(s->AsRawOstream(), m_start_address, sizeof(addr_t));
120*0b57cec5SDimitry Andric     if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
121*0b57cec5SDimitry Andric       s->Printf(" with backstop breakpoint ID: %d at address: ",
122*0b57cec5SDimitry Andric                 m_backstop_bkpt_id);
123480093f4SDimitry Andric       DumpAddress(s->AsRawOstream(), m_backstop_addr, sizeof(addr_t));
124*0b57cec5SDimitry Andric     } else
125*0b57cec5SDimitry Andric       s->PutCString(" unable to set a backstop breakpoint.");
126*0b57cec5SDimitry Andric   }
127*0b57cec5SDimitry Andric }
128*0b57cec5SDimitry Andric 
ValidatePlan(Stream * error)129*0b57cec5SDimitry Andric bool ThreadPlanStepThrough::ValidatePlan(Stream *error) {
130*0b57cec5SDimitry Andric   if (m_could_not_resolve_hw_bp) {
131*0b57cec5SDimitry Andric     if (error)
132*0b57cec5SDimitry Andric       error->PutCString(
133*0b57cec5SDimitry Andric           "Could not create hardware breakpoint for thread plan.");
134*0b57cec5SDimitry Andric     return false;
135*0b57cec5SDimitry Andric   }
136*0b57cec5SDimitry Andric 
137*0b57cec5SDimitry Andric   if (m_backstop_bkpt_id == LLDB_INVALID_BREAK_ID) {
138*0b57cec5SDimitry Andric     if (error)
139*0b57cec5SDimitry Andric       error->PutCString("Could not create backstop breakpoint.");
140*0b57cec5SDimitry Andric     return false;
141*0b57cec5SDimitry Andric   }
142*0b57cec5SDimitry Andric 
143*0b57cec5SDimitry Andric   if (!m_sub_plan_sp.get()) {
144*0b57cec5SDimitry Andric     if (error)
145*0b57cec5SDimitry Andric       error->PutCString("Does not have a subplan.");
146*0b57cec5SDimitry Andric     return false;
147*0b57cec5SDimitry Andric   }
148*0b57cec5SDimitry Andric 
149*0b57cec5SDimitry Andric   return true;
150*0b57cec5SDimitry Andric }
151*0b57cec5SDimitry Andric 
DoPlanExplainsStop(Event * event_ptr)152*0b57cec5SDimitry Andric bool ThreadPlanStepThrough::DoPlanExplainsStop(Event *event_ptr) {
153*0b57cec5SDimitry Andric   // If we have a sub-plan, it will have been asked first if we explain the
154*0b57cec5SDimitry Andric   // stop, and we won't get asked.  The only time we would be the one directly
155*0b57cec5SDimitry Andric   // asked this question is if we hit our backstop breakpoint.
156*0b57cec5SDimitry Andric 
157*0b57cec5SDimitry Andric   return HitOurBackstopBreakpoint();
158*0b57cec5SDimitry Andric }
159*0b57cec5SDimitry Andric 
ShouldStop(Event * event_ptr)160*0b57cec5SDimitry Andric bool ThreadPlanStepThrough::ShouldStop(Event *event_ptr) {
161*0b57cec5SDimitry Andric   // If we've already marked ourselves done, then we're done...
162*0b57cec5SDimitry Andric   if (IsPlanComplete())
163*0b57cec5SDimitry Andric     return true;
164*0b57cec5SDimitry Andric 
165*0b57cec5SDimitry Andric   // First, did we hit the backstop breakpoint?
166*0b57cec5SDimitry Andric   if (HitOurBackstopBreakpoint()) {
167*0b57cec5SDimitry Andric     SetPlanComplete(true);
168*0b57cec5SDimitry Andric     return true;
169*0b57cec5SDimitry Andric   }
170*0b57cec5SDimitry Andric 
171*0b57cec5SDimitry Andric   // If we don't have a sub-plan, then we're also done (can't see how we would
172*0b57cec5SDimitry Andric   // ever get here without a plan, but just in case.
173*0b57cec5SDimitry Andric 
174*0b57cec5SDimitry Andric   if (!m_sub_plan_sp) {
175*0b57cec5SDimitry Andric     SetPlanComplete();
176*0b57cec5SDimitry Andric     return true;
177*0b57cec5SDimitry Andric   }
178*0b57cec5SDimitry Andric 
179*0b57cec5SDimitry Andric   // If the current sub plan is not done, we don't want to stop.  Actually, we
180*0b57cec5SDimitry Andric   // probably won't ever get here in this state, since we generally won't get
181*0b57cec5SDimitry Andric   // asked any questions if out current sub-plan is not done...
182*0b57cec5SDimitry Andric   if (!m_sub_plan_sp->IsPlanComplete())
183*0b57cec5SDimitry Andric     return false;
184*0b57cec5SDimitry Andric 
185*0b57cec5SDimitry Andric   // If our current sub plan failed, then let's just run to our backstop.  If
186*0b57cec5SDimitry Andric   // we can't do that then just stop.
187*0b57cec5SDimitry Andric   if (!m_sub_plan_sp->PlanSucceeded()) {
188*0b57cec5SDimitry Andric     if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
189*0b57cec5SDimitry Andric       m_sub_plan_sp.reset();
190*0b57cec5SDimitry Andric       return false;
191*0b57cec5SDimitry Andric     } else {
192*0b57cec5SDimitry Andric       SetPlanComplete(false);
193*0b57cec5SDimitry Andric       return true;
194*0b57cec5SDimitry Andric     }
195*0b57cec5SDimitry Andric   }
196*0b57cec5SDimitry Andric 
197*0b57cec5SDimitry Andric   // Next see if there is a specific step through plan at our current pc (these
198*0b57cec5SDimitry Andric   // might chain, for instance stepping through a dylib trampoline to the objc
199*0b57cec5SDimitry Andric   // dispatch function...)
200*0b57cec5SDimitry Andric   LookForPlanToStepThroughFromCurrentPC();
201*0b57cec5SDimitry Andric   if (m_sub_plan_sp) {
202*0b57cec5SDimitry Andric     PushPlan(m_sub_plan_sp);
203*0b57cec5SDimitry Andric     return false;
204*0b57cec5SDimitry Andric   } else {
205*0b57cec5SDimitry Andric     SetPlanComplete();
206*0b57cec5SDimitry Andric     return true;
207*0b57cec5SDimitry Andric   }
208*0b57cec5SDimitry Andric }
209*0b57cec5SDimitry Andric 
StopOthers()210*0b57cec5SDimitry Andric bool ThreadPlanStepThrough::StopOthers() { return m_stop_others; }
211*0b57cec5SDimitry Andric 
GetPlanRunState()212*0b57cec5SDimitry Andric StateType ThreadPlanStepThrough::GetPlanRunState() { return eStateRunning; }
213*0b57cec5SDimitry Andric 
DoWillResume(StateType resume_state,bool current_plan)214*0b57cec5SDimitry Andric bool ThreadPlanStepThrough::DoWillResume(StateType resume_state,
215*0b57cec5SDimitry Andric                                          bool current_plan) {
216*0b57cec5SDimitry Andric   return true;
217*0b57cec5SDimitry Andric }
218*0b57cec5SDimitry Andric 
WillStop()219*0b57cec5SDimitry Andric bool ThreadPlanStepThrough::WillStop() { return true; }
220*0b57cec5SDimitry Andric 
ClearBackstopBreakpoint()221*0b57cec5SDimitry Andric void ThreadPlanStepThrough::ClearBackstopBreakpoint() {
222*0b57cec5SDimitry Andric   if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
2235ffd83dbSDimitry Andric     m_process.GetTarget().RemoveBreakpointByID(m_backstop_bkpt_id);
224*0b57cec5SDimitry Andric     m_backstop_bkpt_id = LLDB_INVALID_BREAK_ID;
225*0b57cec5SDimitry Andric     m_could_not_resolve_hw_bp = false;
226*0b57cec5SDimitry Andric   }
227*0b57cec5SDimitry Andric }
228*0b57cec5SDimitry Andric 
MischiefManaged()229*0b57cec5SDimitry Andric bool ThreadPlanStepThrough::MischiefManaged() {
230*0b57cec5SDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
231*0b57cec5SDimitry Andric 
232*0b57cec5SDimitry Andric   if (!IsPlanComplete()) {
233*0b57cec5SDimitry Andric     return false;
234*0b57cec5SDimitry Andric   } else {
2359dba64beSDimitry Andric     LLDB_LOGF(log, "Completed step through step plan.");
236*0b57cec5SDimitry Andric 
237*0b57cec5SDimitry Andric     ClearBackstopBreakpoint();
238*0b57cec5SDimitry Andric     ThreadPlan::MischiefManaged();
239*0b57cec5SDimitry Andric     return true;
240*0b57cec5SDimitry Andric   }
241*0b57cec5SDimitry Andric }
242*0b57cec5SDimitry Andric 
HitOurBackstopBreakpoint()243*0b57cec5SDimitry Andric bool ThreadPlanStepThrough::HitOurBackstopBreakpoint() {
2445ffd83dbSDimitry Andric   Thread &thread = GetThread();
2455ffd83dbSDimitry Andric   StopInfoSP stop_info_sp(thread.GetStopInfo());
246*0b57cec5SDimitry Andric   if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
247*0b57cec5SDimitry Andric     break_id_t stop_value = (break_id_t)stop_info_sp->GetValue();
248*0b57cec5SDimitry Andric     BreakpointSiteSP cur_site_sp =
2495ffd83dbSDimitry Andric         m_process.GetBreakpointSiteList().FindByID(stop_value);
250*0b57cec5SDimitry Andric     if (cur_site_sp &&
251*0b57cec5SDimitry Andric         cur_site_sp->IsBreakpointAtThisSite(m_backstop_bkpt_id)) {
2525ffd83dbSDimitry Andric       StackID cur_frame_zero_id = thread.GetStackFrameAtIndex(0)->GetStackID();
253*0b57cec5SDimitry Andric 
254*0b57cec5SDimitry Andric       if (cur_frame_zero_id == m_return_stack_id) {
255*0b57cec5SDimitry Andric         Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
256*0b57cec5SDimitry Andric         if (log)
257*0b57cec5SDimitry Andric           log->PutCString("ThreadPlanStepThrough hit backstop breakpoint.");
258*0b57cec5SDimitry Andric         return true;
259*0b57cec5SDimitry Andric       }
260*0b57cec5SDimitry Andric     }
261*0b57cec5SDimitry Andric   }
262*0b57cec5SDimitry Andric   return false;
263*0b57cec5SDimitry Andric }
264