blob: d08d77965b73142ff33b172b1e2b1622a8c8c11c [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ThreadPlanStepThrough.cpp -------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00009#include "lldb/Target/ThreadPlanStepThrough.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000010#include "lldb/Breakpoint/Breakpoint.h"
Shafik Yaghmouraa302682018-10-12 17:20:39 +000011#include "lldb/Target/CPPLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Target/DynamicLoader.h"
Jim Ingham5a369122010-09-28 01:25:32 +000013#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Target/Process.h"
15#include "lldb/Target/RegisterContext.h"
Jim Ingham25f66702011-12-03 01:52:59 +000016#include "lldb/Target/Target.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000017#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000018#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
20using namespace lldb;
21using namespace lldb_private;
22
Kate Stoneb9c1b512016-09-06 20:57:50 +000023// ThreadPlanStepThrough: If the current instruction is a trampoline, step
Adrian Prantl05097242018-04-30 16:49:04 +000024// through it If it is the beginning of the prologue of a function, step
25// through that as well.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026// FIXME: At present only handles DYLD trampolines.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Kate Stoneb9c1b512016-09-06 20:57:50 +000028ThreadPlanStepThrough::ThreadPlanStepThrough(Thread &thread,
29 StackID &m_stack_id,
30 bool stop_others)
31 : ThreadPlan(ThreadPlan::eKindStepThrough,
32 "Step through trampolines and prologues", thread,
33 eVoteNoOpinion, eVoteNoOpinion),
34 m_start_address(0), m_backstop_bkpt_id(LLDB_INVALID_BREAK_ID),
35 m_backstop_addr(LLDB_INVALID_ADDRESS), m_return_stack_id(m_stack_id),
36 m_stop_others(stop_others) {
37 LookForPlanToStepThroughFromCurrentPC();
38
39 // If we don't get a valid step through plan, don't bother to set up a
40 // backstop.
41 if (m_sub_plan_sp) {
42 m_start_address = GetThread().GetRegisterContext()->GetPC(0);
43
44 // We are going to return back to the concrete frame 1, we might pass by
Adrian Prantl05097242018-04-30 16:49:04 +000045 // some inlined code that we're in the middle of by doing this, but it's
46 // easier than trying to figure out where the inlined code might return to.
Kate Stoneb9c1b512016-09-06 20:57:50 +000047
48 StackFrameSP return_frame_sp = m_thread.GetFrameWithStackID(m_stack_id);
49
50 if (return_frame_sp) {
51 m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(
52 m_thread.CalculateTarget().get());
53 Breakpoint *return_bp =
54 m_thread.GetProcess()
55 ->GetTarget()
56 .CreateBreakpoint(m_backstop_addr, true, false)
57 .get();
Jonas Devliegheree103ae92018-11-15 01:18:15 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 if (return_bp != nullptr) {
Jonas Devliegheree103ae92018-11-15 01:18:15 +000060 if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
61 m_could_not_resolve_hw_bp = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 return_bp->SetThreadID(m_thread.GetID());
63 m_backstop_bkpt_id = return_bp->GetID();
64 return_bp->SetBreakpointKind("step-through-backstop");
65 }
66 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
67 if (log) {
68 log->Printf("Setting backstop breakpoint %d at address: 0x%" PRIx64,
69 m_backstop_bkpt_id, m_backstop_addr);
70 }
Jim Ingham25f66702011-12-03 01:52:59 +000071 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075ThreadPlanStepThrough::~ThreadPlanStepThrough() { ClearBackstopBreakpoint(); }
76
77void ThreadPlanStepThrough::DidPush() {
78 if (m_sub_plan_sp)
79 PushPlan(m_sub_plan_sp);
Jim Ingham25f66702011-12-03 01:52:59 +000080}
81
Kate Stoneb9c1b512016-09-06 20:57:50 +000082void ThreadPlanStepThrough::LookForPlanToStepThroughFromCurrentPC() {
83 DynamicLoader *loader = m_thread.GetProcess()->GetDynamicLoader();
84 if (loader)
85 m_sub_plan_sp =
86 loader->GetStepThroughTrampolinePlan(m_thread, m_stop_others);
Jim Ingham25f66702011-12-03 01:52:59 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 // If that didn't come up with anything, try the ObjC runtime plugin:
89 if (!m_sub_plan_sp.get()) {
90 ObjCLanguageRuntime *objc_runtime =
91 m_thread.GetProcess()->GetObjCLanguageRuntime();
92 if (objc_runtime)
93 m_sub_plan_sp =
94 objc_runtime->GetStepThroughTrampolinePlan(m_thread, m_stop_others);
Shafik Yaghmouraa302682018-10-12 17:20:39 +000095
96 CPPLanguageRuntime *cpp_runtime =
97 m_thread.GetProcess()->GetCPPLanguageRuntime();
98
99 // If the ObjC runtime did not provide us with a step though plan then if we
100 // have it check the C++ runtime for a step though plan.
101 if (!m_sub_plan_sp.get() && cpp_runtime)
102 m_sub_plan_sp =
103 cpp_runtime->GetStepThroughTrampolinePlan(m_thread, m_stop_others);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 }
105
106 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
107 if (log) {
108 lldb::addr_t current_address = GetThread().GetRegisterContext()->GetPC(0);
109 if (m_sub_plan_sp) {
110 StreamString s;
111 m_sub_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull);
112 log->Printf("Found step through plan from 0x%" PRIx64 ": %s",
113 current_address, s.GetData());
114 } else {
115 log->Printf("Couldn't find step through plan from address 0x%" PRIx64 ".",
116 current_address);
Jim Ingham25f66702011-12-03 01:52:59 +0000117 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119}
120
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121void ThreadPlanStepThrough::GetDescription(Stream *s,
122 lldb::DescriptionLevel level) {
123 if (level == lldb::eDescriptionLevelBrief)
124 s->Printf("Step through");
125 else {
126 s->PutCString("Stepping through trampoline code from: ");
127 s->Address(m_start_address, sizeof(addr_t));
128 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
129 s->Printf(" with backstop breakpoint ID: %d at address: ",
130 m_backstop_bkpt_id);
131 s->Address(m_backstop_addr, sizeof(addr_t));
132 } else
133 s->PutCString(" unable to set a backstop breakpoint.");
134 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135}
136
Kate Stoneb9c1b512016-09-06 20:57:50 +0000137bool ThreadPlanStepThrough::ValidatePlan(Stream *error) {
Jonas Devliegheree103ae92018-11-15 01:18:15 +0000138 if (m_could_not_resolve_hw_bp) {
139 if (error)
140 error->PutCString(
141 "Could not create hardware breakpoint for thread plan.");
142 return false;
143 }
144
145 if (m_backstop_bkpt_id == LLDB_INVALID_BREAK_ID) {
146 if (error)
147 error->PutCString("Could not create backstop breakpoint.");
148 return false;
149 }
150
151 if (!m_sub_plan_sp.get()) {
152 if (error)
153 error->PutCString("Does not have a subplan.");
154 return false;
155 }
156
157 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160bool ThreadPlanStepThrough::DoPlanExplainsStop(Event *event_ptr) {
161 // If we have a sub-plan, it will have been asked first if we explain the
Adrian Prantl05097242018-04-30 16:49:04 +0000162 // stop, and we won't get asked. The only time we would be the one directly
163 // asked this question is if we hit our backstop breakpoint.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000164
165 return HitOurBackstopBreakpoint();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166}
167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168bool ThreadPlanStepThrough::ShouldStop(Event *event_ptr) {
169 // If we've already marked ourselves done, then we're done...
170 if (IsPlanComplete())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173 // First, did we hit the backstop breakpoint?
174 if (HitOurBackstopBreakpoint()) {
175 SetPlanComplete(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179 // If we don't have a sub-plan, then we're also done (can't see how we would
Adrian Prantl05097242018-04-30 16:49:04 +0000180 // ever get here without a plan, but just in case.
Jim Ingham18de2fd2012-05-10 01:35:39 +0000181
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 if (!m_sub_plan_sp) {
183 SetPlanComplete();
184 return true;
185 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 // If the current sub plan is not done, we don't want to stop. Actually, we
Adrian Prantl05097242018-04-30 16:49:04 +0000188 // probably won't ever get here in this state, since we generally won't get
189 // asked any questions if out current sub-plan is not done...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 if (!m_sub_plan_sp->IsPlanComplete())
Jim Ingham25f66702011-12-03 01:52:59 +0000191 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000192
Adrian Prantl05097242018-04-30 16:49:04 +0000193 // If our current sub plan failed, then let's just run to our backstop. If
194 // we can't do that then just stop.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 if (!m_sub_plan_sp->PlanSucceeded()) {
196 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
197 m_sub_plan_sp.reset();
198 return false;
199 } else {
200 SetPlanComplete(false);
201 return true;
202 }
203 }
204
205 // Next see if there is a specific step through plan at our current pc (these
Adrian Prantl05097242018-04-30 16:49:04 +0000206 // might chain, for instance stepping through a dylib trampoline to the objc
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 // dispatch function...)
208 LookForPlanToStepThroughFromCurrentPC();
209 if (m_sub_plan_sp) {
210 PushPlan(m_sub_plan_sp);
211 return false;
212 } else {
213 SetPlanComplete();
214 return true;
215 }
216}
217
218bool ThreadPlanStepThrough::StopOthers() { return m_stop_others; }
219
220StateType ThreadPlanStepThrough::GetPlanRunState() { return eStateRunning; }
221
222bool ThreadPlanStepThrough::DoWillResume(StateType resume_state,
223 bool current_plan) {
224 return true;
225}
226
227bool ThreadPlanStepThrough::WillStop() { return true; }
228
229void ThreadPlanStepThrough::ClearBackstopBreakpoint() {
230 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
231 m_thread.GetProcess()->GetTarget().RemoveBreakpointByID(m_backstop_bkpt_id);
232 m_backstop_bkpt_id = LLDB_INVALID_BREAK_ID;
Jonas Devliegheree103ae92018-11-15 01:18:15 +0000233 m_could_not_resolve_hw_bp = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 }
235}
236
237bool ThreadPlanStepThrough::MischiefManaged() {
238 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
239
240 if (!IsPlanComplete()) {
241 return false;
242 } else {
243 if (log)
244 log->Printf("Completed step through step plan.");
245
246 ClearBackstopBreakpoint();
247 ThreadPlan::MischiefManaged();
248 return true;
249 }
250}
251
252bool ThreadPlanStepThrough::HitOurBackstopBreakpoint() {
253 StopInfoSP stop_info_sp(m_thread.GetStopInfo());
254 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
255 break_id_t stop_value = (break_id_t)stop_info_sp->GetValue();
256 BreakpointSiteSP cur_site_sp =
257 m_thread.GetProcess()->GetBreakpointSiteList().FindByID(stop_value);
258 if (cur_site_sp &&
259 cur_site_sp->IsBreakpointAtThisSite(m_backstop_bkpt_id)) {
260 StackID cur_frame_zero_id =
261 m_thread.GetStackFrameAtIndex(0)->GetStackID();
262
263 if (cur_frame_zero_id == m_return_stack_id) {
264 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
265 if (log)
266 log->PutCString("ThreadPlanStepThrough hit backstop breakpoint.");
267 return true;
268 }
269 }
270 }
271 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272}