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