blob: 6fc0312222fb95b2d7f4d91346ebe007f4e5851b [file] [log] [blame]
Raphael Isemann80814282020-01-24 07:23:271//===-- ThreadPlanStepThrough.cpp -----------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:242//
Chandler Carruth2946cd72019-01-19 08:50:563// 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:246//
7//===----------------------------------------------------------------------===//
8
Eugene Zelenkoe65b2cf2015-12-15 01:33:199#include "lldb/Target/ThreadPlanStepThrough.h"
Kate Stoneb9c1b512016-09-06 20:57:5010#include "lldb/Breakpoint/Breakpoint.h"
Chris Lattner30fdc8d2010-06-08 16:52:2411#include "lldb/Target/DynamicLoader.h"
Alex Langforde5a7a852019-05-30 22:00:1812#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:2413#include "lldb/Target/Process.h"
14#include "lldb/Target/RegisterContext.h"
Jim Ingham25f66702011-12-03 01:52:5915#include "lldb/Target/Target.h"
Zachary Turner6f9e6902017-03-03 20:56:2816#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:5017#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:2418
19using namespace lldb;
20using namespace lldb_private;
21
Kate Stoneb9c1b512016-09-06 20:57:5022// ThreadPlanStepThrough: If the current instruction is a trampoline, step
Adrian Prantl05097242018-04-30 16:49:0423// through it If it is the beginning of the prologue of a function, step
24// through that as well.
Chris Lattner30fdc8d2010-06-08 16:52:2425
Kate Stoneb9c1b512016-09-06 20:57:5026ThreadPlanStepThrough::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
Adrian Prantl05097242018-04-30 16:49:0443 // 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.
Kate Stoneb9c1b512016-09-06 20:57:5045
Jim Inghame4598dc2020-03-10 21:03:5346 StackFrameSP return_frame_sp = thread.GetFrameWithStackID(m_stack_id);
Kate Stoneb9c1b512016-09-06 20:57:5047
48 if (return_frame_sp) {
49 m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(
Jim Inghame4598dc2020-03-10 21:03:5350 thread.CalculateTarget().get());
Kate Stoneb9c1b512016-09-06 20:57:5051 Breakpoint *return_bp =
Jim Inghame4598dc2020-03-10 21:03:5352 m_process.GetTarget()
Kate Stoneb9c1b512016-09-06 20:57:5053 .CreateBreakpoint(m_backstop_addr, true, false)
54 .get();
Jonas Devliegheree103ae92018-11-15 01:18:1555
Kate Stoneb9c1b512016-09-06 20:57:5056 if (return_bp != nullptr) {
Jonas Devliegheree103ae92018-11-15 01:18:1557 if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
58 m_could_not_resolve_hw_bp = true;
Jim Inghame4598dc2020-03-10 21:03:5359 return_bp->SetThreadID(m_tid);
Kate Stoneb9c1b512016-09-06 20:57:5060 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) {
Jonas Devlieghere63e5fb72019-07-24 17:56:1065 LLDB_LOGF(log, "Setting backstop breakpoint %d at address: 0x%" PRIx64,
66 m_backstop_bkpt_id, m_backstop_addr);
Kate Stoneb9c1b512016-09-06 20:57:5067 }
Jim Ingham25f66702011-12-03 01:52:5968 }
Kate Stoneb9c1b512016-09-06 20:57:5069 }
Chris Lattner30fdc8d2010-06-08 16:52:2470}
71
Kate Stoneb9c1b512016-09-06 20:57:5072ThreadPlanStepThrough::~ThreadPlanStepThrough() { ClearBackstopBreakpoint(); }
73
74void ThreadPlanStepThrough::DidPush() {
75 if (m_sub_plan_sp)
76 PushPlan(m_sub_plan_sp);
Jim Ingham25f66702011-12-03 01:52:5977}
78
Kate Stoneb9c1b512016-09-06 20:57:5079void ThreadPlanStepThrough::LookForPlanToStepThroughFromCurrentPC() {
Jim Inghame4598dc2020-03-10 21:03:5380 Thread &thread = GetThread();
81 DynamicLoader *loader = thread.GetProcess()->GetDynamicLoader();
Kate Stoneb9c1b512016-09-06 20:57:5082 if (loader)
Jim Inghame4598dc2020-03-10 21:03:5383 m_sub_plan_sp = loader->GetStepThroughTrampolinePlan(thread, m_stop_others);
Jim Ingham25f66702011-12-03 01:52:5984
Alex Langforde5a7a852019-05-30 22:00:1885 // If the DynamicLoader was unable to provide us with a ThreadPlan, then we
86 // try the LanguageRuntimes.
87 if (!m_sub_plan_sp) {
Jim Inghame4598dc2020-03-10 21:03:5388 for (LanguageRuntime *runtime : m_process.GetLanguageRuntimes()) {
Kate Stoneb9c1b512016-09-06 20:57:5089 m_sub_plan_sp =
Jim Inghame4598dc2020-03-10 21:03:5390 runtime->GetStepThroughTrampolinePlan(thread, m_stop_others);
Shafik Yaghmouraa302682018-10-12 17:20:3991
Alex Langforde5a7a852019-05-30 22:00:1892 if (m_sub_plan_sp)
93 break;
94 }
Kate Stoneb9c1b512016-09-06 20:57:5095 }
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);
Jonas Devlieghere63e5fb72019-07-24 17:56:10103 LLDB_LOGF(log, "Found step through plan from 0x%" PRIx64 ": %s",
104 current_address, s.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50105 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10106 LLDB_LOGF(log,
107 "Couldn't find step through plan from address 0x%" PRIx64 ".",
108 current_address);
Jim Ingham25f66702011-12-03 01:52:59109 }
Kate Stoneb9c1b512016-09-06 20:57:50110 }
Chris Lattner30fdc8d2010-06-08 16:52:24111}
112
Kate Stoneb9c1b512016-09-06 20:57:50113void 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: ");
Raphael Isemann1462f5a2019-12-05 13:41:09119 DumpAddress(s->AsRawOstream(), m_start_address, sizeof(addr_t));
Kate Stoneb9c1b512016-09-06 20:57:50120 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
121 s->Printf(" with backstop breakpoint ID: %d at address: ",
122 m_backstop_bkpt_id);
Raphael Isemann1462f5a2019-12-05 13:41:09123 DumpAddress(s->AsRawOstream(), m_backstop_addr, sizeof(addr_t));
Kate Stoneb9c1b512016-09-06 20:57:50124 } else
125 s->PutCString(" unable to set a backstop breakpoint.");
126 }
Chris Lattner30fdc8d2010-06-08 16:52:24127}
128
Kate Stoneb9c1b512016-09-06 20:57:50129bool ThreadPlanStepThrough::ValidatePlan(Stream *error) {
Jonas Devliegheree103ae92018-11-15 01:18:15130 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;
Chris Lattner30fdc8d2010-06-08 16:52:24150}
151
Kate Stoneb9c1b512016-09-06 20:57:50152bool ThreadPlanStepThrough::DoPlanExplainsStop(Event *event_ptr) {
153 // If we have a sub-plan, it will have been asked first if we explain the
Adrian Prantl05097242018-04-30 16:49:04154 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50156
157 return HitOurBackstopBreakpoint();
Chris Lattner30fdc8d2010-06-08 16:52:24158}
159
Kate Stoneb9c1b512016-09-06 20:57:50160bool ThreadPlanStepThrough::ShouldStop(Event *event_ptr) {
161 // If we've already marked ourselves done, then we're done...
162 if (IsPlanComplete())
Chris Lattner30fdc8d2010-06-08 16:52:24163 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24164
Kate Stoneb9c1b512016-09-06 20:57:50165 // First, did we hit the backstop breakpoint?
166 if (HitOurBackstopBreakpoint()) {
167 SetPlanComplete(true);
Chris Lattner30fdc8d2010-06-08 16:52:24168 return true;
Kate Stoneb9c1b512016-09-06 20:57:50169 }
Chris Lattner30fdc8d2010-06-08 16:52:24170
Kate Stoneb9c1b512016-09-06 20:57:50171 // 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:04172 // ever get here without a plan, but just in case.
Jim Ingham18de2fd2012-05-10 01:35:39173
Kate Stoneb9c1b512016-09-06 20:57:50174 if (!m_sub_plan_sp) {
175 SetPlanComplete();
176 return true;
177 }
Chris Lattner30fdc8d2010-06-08 16:52:24178
Kate Stoneb9c1b512016-09-06 20:57:50179 // If the current sub plan is not done, we don't want to stop. Actually, we
Adrian Prantl05097242018-04-30 16:49:04180 // 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...
Kate Stoneb9c1b512016-09-06 20:57:50182 if (!m_sub_plan_sp->IsPlanComplete())
Jim Ingham25f66702011-12-03 01:52:59183 return false;
Kate Stoneb9c1b512016-09-06 20:57:50184
Adrian Prantl05097242018-04-30 16:49:04185 // If our current sub plan failed, then let's just run to our backstop. If
186 // we can't do that then just stop.
Kate Stoneb9c1b512016-09-06 20:57:50187 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
Adrian Prantl05097242018-04-30 16:49:04198 // might chain, for instance stepping through a dylib trampoline to the objc
Kate Stoneb9c1b512016-09-06 20:57:50199 // 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
210bool ThreadPlanStepThrough::StopOthers() { return m_stop_others; }
211
212StateType ThreadPlanStepThrough::GetPlanRunState() { return eStateRunning; }
213
214bool ThreadPlanStepThrough::DoWillResume(StateType resume_state,
215 bool current_plan) {
216 return true;
217}
218
219bool ThreadPlanStepThrough::WillStop() { return true; }
220
221void ThreadPlanStepThrough::ClearBackstopBreakpoint() {
222 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
Jim Inghame4598dc2020-03-10 21:03:53223 m_process.GetTarget().RemoveBreakpointByID(m_backstop_bkpt_id);
Kate Stoneb9c1b512016-09-06 20:57:50224 m_backstop_bkpt_id = LLDB_INVALID_BREAK_ID;
Jonas Devliegheree103ae92018-11-15 01:18:15225 m_could_not_resolve_hw_bp = false;
Kate Stoneb9c1b512016-09-06 20:57:50226 }
227}
228
229bool ThreadPlanStepThrough::MischiefManaged() {
230 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
231
232 if (!IsPlanComplete()) {
233 return false;
234 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10235 LLDB_LOGF(log, "Completed step through step plan.");
Kate Stoneb9c1b512016-09-06 20:57:50236
237 ClearBackstopBreakpoint();
238 ThreadPlan::MischiefManaged();
239 return true;
240 }
241}
242
243bool ThreadPlanStepThrough::HitOurBackstopBreakpoint() {
Jim Inghame4598dc2020-03-10 21:03:53244 Thread &thread = GetThread();
245 StopInfoSP stop_info_sp(thread.GetStopInfo());
Kate Stoneb9c1b512016-09-06 20:57:50246 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 =
Jim Inghame4598dc2020-03-10 21:03:53249 m_process.GetBreakpointSiteList().FindByID(stop_value);
Kate Stoneb9c1b512016-09-06 20:57:50250 if (cur_site_sp &&
251 cur_site_sp->IsBreakpointAtThisSite(m_backstop_bkpt_id)) {
Jim Inghame4598dc2020-03-10 21:03:53252 StackID cur_frame_zero_id = thread.GetStackFrameAtIndex(0)->GetStackID();
Kate Stoneb9c1b512016-09-06 20:57:50253
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;
Chris Lattner30fdc8d2010-06-08 16:52:24263}