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