blob: 06b626935abaa21868aebdf8f4198cb5b678a65b [file] [log] [blame]
Raphael Isemann80814282020-01-24 08:23:27 +01001//===-- ThreadPlanStepThrough.cpp -----------------------------------------===//
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002//
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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "lldb/Target/DynamicLoader.h"
Alex Langforde5a7a852019-05-30 22:00:18 +000012#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Target/Process.h"
14#include "lldb/Target/RegisterContext.h"
Jim Ingham25f66702011-12-03 01:52:59 +000015#include "lldb/Target/Target.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000016#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000017#include "lldb/Utility/Stream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
19using namespace lldb;
20using namespace lldb_private;
21
Kate Stoneb9c1b512016-09-06 20:57:50 +000022// ThreadPlanStepThrough: If the current instruction is a trampoline, step
Adrian Prantl05097242018-04-30 16:49:04 +000023// 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:24 +000025// FIXME: At present only handles DYLD trampolines.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Kate Stoneb9c1b512016-09-06 20:57:50 +000027ThreadPlanStepThrough::ThreadPlanStepThrough(Thread &thread,
28 StackID &m_stack_id,
29 bool stop_others)
30 : ThreadPlan(ThreadPlan::eKindStepThrough,
31 "Step through trampolines and prologues", thread,
32 eVoteNoOpinion, eVoteNoOpinion),
33 m_start_address(0), m_backstop_bkpt_id(LLDB_INVALID_BREAK_ID),
34 m_backstop_addr(LLDB_INVALID_ADDRESS), m_return_stack_id(m_stack_id),
35 m_stop_others(stop_others) {
36 LookForPlanToStepThroughFromCurrentPC();
37
38 // If we don't get a valid step through plan, don't bother to set up a
39 // backstop.
40 if (m_sub_plan_sp) {
41 m_start_address = GetThread().GetRegisterContext()->GetPC(0);
42
43 // We are going to return back to the concrete frame 1, we might pass by
Adrian Prantl05097242018-04-30 16:49:04 +000044 // some inlined code that we're in the middle of by doing this, but it's
45 // easier than trying to figure out where the inlined code might return to.
Kate Stoneb9c1b512016-09-06 20:57:50 +000046
Jim Inghame4598dc2020-03-10 14:03:53 -070047 StackFrameSP return_frame_sp = thread.GetFrameWithStackID(m_stack_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +000048
49 if (return_frame_sp) {
50 m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(
Jim Inghame4598dc2020-03-10 14:03:53 -070051 thread.CalculateTarget().get());
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 Breakpoint *return_bp =
Jim Inghame4598dc2020-03-10 14:03:53 -070053 m_process.GetTarget()
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 .CreateBreakpoint(m_backstop_addr, true, false)
55 .get();
Jonas Devliegheree103ae92018-11-15 01:18:15 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 if (return_bp != nullptr) {
Jonas Devliegheree103ae92018-11-15 01:18:15 +000058 if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
59 m_could_not_resolve_hw_bp = true;
Jim Inghame4598dc2020-03-10 14:03:53 -070060 return_bp->SetThreadID(m_tid);
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 m_backstop_bkpt_id = return_bp->GetID();
62 return_bp->SetBreakpointKind("step-through-backstop");
63 }
64 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
65 if (log) {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +000066 LLDB_LOGF(log, "Setting backstop breakpoint %d at address: 0x%" PRIx64,
67 m_backstop_bkpt_id, m_backstop_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 }
Jim Ingham25f66702011-12-03 01:52:59 +000069 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071}
72
Kate Stoneb9c1b512016-09-06 20:57:50 +000073ThreadPlanStepThrough::~ThreadPlanStepThrough() { ClearBackstopBreakpoint(); }
74
75void ThreadPlanStepThrough::DidPush() {
76 if (m_sub_plan_sp)
77 PushPlan(m_sub_plan_sp);
Jim Ingham25f66702011-12-03 01:52:59 +000078}
79
Kate Stoneb9c1b512016-09-06 20:57:50 +000080void ThreadPlanStepThrough::LookForPlanToStepThroughFromCurrentPC() {
Jim Inghame4598dc2020-03-10 14:03:53 -070081 Thread &thread = GetThread();
82 DynamicLoader *loader = thread.GetProcess()->GetDynamicLoader();
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 if (loader)
Jim Inghame4598dc2020-03-10 14:03:53 -070084 m_sub_plan_sp = loader->GetStepThroughTrampolinePlan(thread, m_stop_others);
Jim Ingham25f66702011-12-03 01:52:59 +000085
Alex Langforde5a7a852019-05-30 22:00:18 +000086 // If the DynamicLoader was unable to provide us with a ThreadPlan, then we
87 // try the LanguageRuntimes.
88 if (!m_sub_plan_sp) {
Jim Inghame4598dc2020-03-10 14:03:53 -070089 for (LanguageRuntime *runtime : m_process.GetLanguageRuntimes()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 m_sub_plan_sp =
Jim Inghame4598dc2020-03-10 14:03:53 -070091 runtime->GetStepThroughTrampolinePlan(thread, m_stop_others);
Shafik Yaghmouraa302682018-10-12 17:20:39 +000092
Alex Langforde5a7a852019-05-30 22:00:18 +000093 if (m_sub_plan_sp)
94 break;
95 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 }
97
98 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
99 if (log) {
100 lldb::addr_t current_address = GetThread().GetRegisterContext()->GetPC(0);
101 if (m_sub_plan_sp) {
102 StreamString s;
103 m_sub_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull);
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000104 LLDB_LOGF(log, "Found step through plan from 0x%" PRIx64 ": %s",
105 current_address, s.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000107 LLDB_LOGF(log,
108 "Couldn't find step through plan from address 0x%" PRIx64 ".",
109 current_address);
Jim Ingham25f66702011-12-03 01:52:59 +0000110 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112}
113
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114void ThreadPlanStepThrough::GetDescription(Stream *s,
115 lldb::DescriptionLevel level) {
116 if (level == lldb::eDescriptionLevelBrief)
117 s->Printf("Step through");
118 else {
119 s->PutCString("Stepping through trampoline code from: ");
Raphael Isemann1462f5a2019-12-05 14:41:09 +0100120 DumpAddress(s->AsRawOstream(), m_start_address, sizeof(addr_t));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
122 s->Printf(" with backstop breakpoint ID: %d at address: ",
123 m_backstop_bkpt_id);
Raphael Isemann1462f5a2019-12-05 14:41:09 +0100124 DumpAddress(s->AsRawOstream(), m_backstop_addr, sizeof(addr_t));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 } else
126 s->PutCString(" unable to set a backstop breakpoint.");
127 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128}
129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130bool ThreadPlanStepThrough::ValidatePlan(Stream *error) {
Jonas Devliegheree103ae92018-11-15 01:18:15 +0000131 if (m_could_not_resolve_hw_bp) {
132 if (error)
133 error->PutCString(
134 "Could not create hardware breakpoint for thread plan.");
135 return false;
136 }
137
138 if (m_backstop_bkpt_id == LLDB_INVALID_BREAK_ID) {
139 if (error)
140 error->PutCString("Could not create backstop breakpoint.");
141 return false;
142 }
143
144 if (!m_sub_plan_sp.get()) {
145 if (error)
146 error->PutCString("Does not have a subplan.");
147 return false;
148 }
149
150 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151}
152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153bool ThreadPlanStepThrough::DoPlanExplainsStop(Event *event_ptr) {
154 // If we have a sub-plan, it will have been asked first if we explain the
Adrian Prantl05097242018-04-30 16:49:04 +0000155 // stop, and we won't get asked. The only time we would be the one directly
156 // asked this question is if we hit our backstop breakpoint.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157
158 return HitOurBackstopBreakpoint();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159}
160
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161bool ThreadPlanStepThrough::ShouldStop(Event *event_ptr) {
162 // If we've already marked ourselves done, then we're done...
163 if (IsPlanComplete())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 // First, did we hit the backstop breakpoint?
167 if (HitOurBackstopBreakpoint()) {
168 SetPlanComplete(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172 // 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 +0000173 // ever get here without a plan, but just in case.
Jim Ingham18de2fd2012-05-10 01:35:39 +0000174
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175 if (!m_sub_plan_sp) {
176 SetPlanComplete();
177 return true;
178 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 // If the current sub plan is not done, we don't want to stop. Actually, we
Adrian Prantl05097242018-04-30 16:49:04 +0000181 // probably won't ever get here in this state, since we generally won't get
182 // asked any questions if out current sub-plan is not done...
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 if (!m_sub_plan_sp->IsPlanComplete())
Jim Ingham25f66702011-12-03 01:52:59 +0000184 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000185
Adrian Prantl05097242018-04-30 16:49:04 +0000186 // If our current sub plan failed, then let's just run to our backstop. If
187 // we can't do that then just stop.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188 if (!m_sub_plan_sp->PlanSucceeded()) {
189 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
190 m_sub_plan_sp.reset();
191 return false;
192 } else {
193 SetPlanComplete(false);
194 return true;
195 }
196 }
197
198 // Next see if there is a specific step through plan at our current pc (these
Adrian Prantl05097242018-04-30 16:49:04 +0000199 // might chain, for instance stepping through a dylib trampoline to the objc
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 // dispatch function...)
201 LookForPlanToStepThroughFromCurrentPC();
202 if (m_sub_plan_sp) {
203 PushPlan(m_sub_plan_sp);
204 return false;
205 } else {
206 SetPlanComplete();
207 return true;
208 }
209}
210
211bool ThreadPlanStepThrough::StopOthers() { return m_stop_others; }
212
213StateType ThreadPlanStepThrough::GetPlanRunState() { return eStateRunning; }
214
215bool ThreadPlanStepThrough::DoWillResume(StateType resume_state,
216 bool current_plan) {
217 return true;
218}
219
220bool ThreadPlanStepThrough::WillStop() { return true; }
221
222void ThreadPlanStepThrough::ClearBackstopBreakpoint() {
223 if (m_backstop_bkpt_id != LLDB_INVALID_BREAK_ID) {
Jim Inghame4598dc2020-03-10 14:03:53 -0700224 m_process.GetTarget().RemoveBreakpointByID(m_backstop_bkpt_id);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225 m_backstop_bkpt_id = LLDB_INVALID_BREAK_ID;
Jonas Devliegheree103ae92018-11-15 01:18:15 +0000226 m_could_not_resolve_hw_bp = false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 }
228}
229
230bool ThreadPlanStepThrough::MischiefManaged() {
231 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
232
233 if (!IsPlanComplete()) {
234 return false;
235 } else {
Jonas Devlieghere63e5fb72019-07-24 17:56:10 +0000236 LLDB_LOGF(log, "Completed step through step plan.");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237
238 ClearBackstopBreakpoint();
239 ThreadPlan::MischiefManaged();
240 return true;
241 }
242}
243
244bool ThreadPlanStepThrough::HitOurBackstopBreakpoint() {
Jim Inghame4598dc2020-03-10 14:03:53 -0700245 Thread &thread = GetThread();
246 StopInfoSP stop_info_sp(thread.GetStopInfo());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
248 break_id_t stop_value = (break_id_t)stop_info_sp->GetValue();
249 BreakpointSiteSP cur_site_sp =
Jim Inghame4598dc2020-03-10 14:03:53 -0700250 m_process.GetBreakpointSiteList().FindByID(stop_value);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251 if (cur_site_sp &&
252 cur_site_sp->IsBreakpointAtThisSite(m_backstop_bkpt_id)) {
Jim Inghame4598dc2020-03-10 14:03:53 -0700253 StackID cur_frame_zero_id = thread.GetStackFrameAtIndex(0)->GetStackID();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000254
255 if (cur_frame_zero_id == m_return_stack_id) {
256 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
257 if (log)
258 log->PutCString("ThreadPlanStepThrough hit backstop breakpoint.");
259 return true;
260 }
261 }
262 }
263 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264}