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