LLDB mainline
ThreadPlanStepThrough.cpp
Go to the documentation of this file.
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
13#include "lldb/Target/Process.h"
16#include "lldb/Target/Target.h"
18#include "lldb/Utility/Log.h"
19#include "lldb/Utility/Stream.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24// ThreadPlanStepThrough: If the current instruction is a trampoline, step
25// through it If it is the beginning of the prologue of a function, step
26// through that as well.
27
29 StackID &m_stack_id,
30 bool stop_others)
32 "Step through trampolines and prologues", thread,
36 m_stop_others(stop_others) {
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) {
43
44 // We are going to return back to the concrete frame 1, we might pass by
45 // 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.
47
48 StackFrameSP return_frame_sp = thread.GetFrameWithStackID(m_stack_id);
49
50 if (return_frame_sp) {
51 m_backstop_addr = return_frame_sp->GetFrameCodeAddress().GetLoadAddress(
52 thread.CalculateTarget().get());
53 Breakpoint *return_bp =
54 m_process.GetTarget()
55 .CreateBreakpoint(m_backstop_addr, true, false)
56 .get();
57
58 if (return_bp != nullptr) {
59 if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
61 return_bp->SetThreadID(m_tid);
62 m_backstop_bkpt_id = return_bp->GetID();
63 return_bp->SetBreakpointKind("step-through-backstop");
64 }
65 Log *log = GetLog(LLDBLog::Step);
66 LLDB_LOGF(log, "Setting backstop breakpoint %d at address: 0x%" PRIx64,
68 }
69 }
70}
71
73
78
80 Thread &thread = GetThread();
81
82 // Give the frame recognizers a chance to provide a step through:
83 StackFrameSP frame_zero_sp = thread.GetStackFrameAtIndex(0);
84 if (RecognizedStackFrameSP frame_recognizer_sp =
85 frame_zero_sp->GetRecognizedFrame()) {
86 m_sub_plan_sp = frame_recognizer_sp->GetStepThroughPlan();
87 if (m_sub_plan_sp)
88 return;
89 }
90
91 DynamicLoader *loader = thread.GetProcess()->GetDynamicLoader();
92 if (loader)
94
95 // If the DynamicLoader was unable to provide us with a ThreadPlan, then we
96 // try the LanguageRuntimes.
97 if (!m_sub_plan_sp) {
98 for (LanguageRuntime *runtime : m_process.GetLanguageRuntimes()) {
100 runtime->GetStepThroughTrampolinePlan(thread, m_stop_others);
101
102 if (m_sub_plan_sp)
103 break;
104 }
105 }
106
107 Log *log = GetLog(LLDBLog::Step);
108 if (log) {
109 lldb::addr_t current_address = GetThread().GetRegisterContext()->GetPC(0);
110 if (m_sub_plan_sp) {
111 StreamString s;
112 m_sub_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull);
113 LLDB_LOGF(log, "Found step through plan from 0x%" PRIx64 ": %s",
114 current_address, s.GetData());
115 } else {
116 LLDB_LOGF(log,
117 "Couldn't find step through plan from address 0x%" PRIx64 ".",
118 current_address);
119 }
120 }
121}
122
125 if (level == lldb::eDescriptionLevelBrief)
126 s->PutCString("Step through");
127 else {
128 s->PutCString("Stepping through trampoline code from: ");
131 s->Printf(" with backstop breakpoint ID: %d at address: ",
134 } else
135 s->PutCString(" unable to set a backstop breakpoint.");
136 }
137}
138
141 if (error)
142 error->PutCString(
143 "Could not create hardware breakpoint for thread plan.");
144 return false;
145 }
146
148 if (error)
149 error->PutCString("Could not create backstop breakpoint.");
150 return false;
151 }
152
153 if (!m_sub_plan_sp.get()) {
154 if (error)
155 error->PutCString("Does not have a subplan.");
156 return false;
157 }
158
159 return true;
160}
161
163 // If we have a sub-plan, it will have been asked first if we explain the
164 // stop, and we won't get asked. The only time we would be the one directly
165 // asked this question is if we hit our backstop breakpoint.
166
168}
169
171 // If we've already marked ourselves done, then we're done...
172 if (IsPlanComplete())
173 return true;
174
175 // First, did we hit the backstop breakpoint?
177 SetPlanComplete(true);
178 return true;
179 }
180
181 // If we don't have a sub-plan, then we're also done (can't see how we would
182 // ever get here without a plan, but just in case.
183
184 if (!m_sub_plan_sp) {
186 return true;
187 }
188
189 // If the current sub plan is not done, we don't want to stop. Actually, we
190 // probably won't ever get here in this state, since we generally won't get
191 // asked any questions if out current sub-plan is not done...
192 if (!m_sub_plan_sp->IsPlanComplete())
193 return false;
194
195 // If our current sub plan failed, then let's just run to our backstop. If
196 // we can't do that then just stop.
197 if (!m_sub_plan_sp->PlanSucceeded()) {
199 m_sub_plan_sp.reset();
200 return false;
201 } else {
202 SetPlanComplete(false);
203 return true;
204 }
205 }
206
207 // Next see if there is a specific step through plan at our current pc (these
208 // might chain, for instance stepping through a dylib trampoline to the objc
209 // dispatch function...)
211 if (m_sub_plan_sp) {
213 return false;
214 } else {
216 return true;
217 }
218}
219
221
223
225 bool current_plan) {
226 return true;
227}
228
229bool ThreadPlanStepThrough::WillStop() { return true; }
230
238
240 Log *log = GetLog(LLDBLog::Step);
241
242 if (!IsPlanComplete()) {
243 return false;
244 } else {
245 LLDB_LOGF(log, "Completed step through step plan.");
246
249 return true;
250 }
251}
252
254 Thread &thread = GetThread();
255 StopInfoSP stop_info_sp(thread.GetStopInfo());
256 if (stop_info_sp && stop_info_sp->GetStopReason() == eStopReasonBreakpoint) {
257 break_id_t stop_value = (break_id_t)stop_info_sp->GetValue();
258 BreakpointSiteSP cur_site_sp =
259 m_process.GetBreakpointSiteList().FindByID(stop_value);
260 if (cur_site_sp &&
261 cur_site_sp->IsBreakpointAtThisSite(m_backstop_bkpt_id)) {
262 StackID cur_frame_zero_id = thread.GetStackFrameAtIndex(0)->GetStackID();
263
264 if (cur_frame_zero_id == m_return_stack_id) {
265 Log *log = GetLog(LLDBLog::Step);
266 if (log)
267 log->PutCString("ThreadPlanStepThrough hit backstop breakpoint.");
268 return true;
269 }
270 }
271 }
272 return false;
273}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition Log.h:389
General Outline: A breakpoint has four main parts, a filter, a resolver, the list of breakpoint locat...
Definition Breakpoint.h:83
void SetBreakpointKind(const char *kind)
Set the "kind" description for a breakpoint.
Definition Breakpoint.h:484
bool HasResolvedLocations() const
Return whether this breakpoint has any resolved locations.
void SetThreadID(lldb::tid_t thread_id)
Set the valid thread to be checked when the breakpoint is hit.
A plug-in interface definition class for dynamic loaders.
virtual lldb::ThreadPlanSP GetStepThroughTrampolinePlan(Thread &thread, bool stop_others)=0
Provides a plan to step through the dynamic loader trampoline for the current state of thread.
void PutCString(const char *cstr)
Definition Log.cpp:162
lldb::break_id_t GetID() const
Definition Stoppoint.cpp:22
const char * GetData() const
A stream class that can stream formatted output to a file.
Definition Stream.h:28
llvm::raw_ostream & AsRawOstream()
Returns a raw_ostream that forwards the data to this Stream object.
Definition Stream.h:405
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
bool DoWillResume(lldb::StateType resume_state, bool current_plan) override
bool DoPlanExplainsStop(Event *event_ptr) override
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
ThreadPlanStepThrough(Thread &thread, StackID &return_stack_id, bool stop_others)
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
bool ShouldStop(Event *event_ptr) override
ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread, Vote report_stop_vote, Vote report_run_vote)
void PushPlan(lldb::ThreadPlanSP &thread_plan_sp)
Definition ThreadPlan.h:529
void SetPlanComplete(bool success=true)
Thread & GetThread()
Returns the Thread that is using this thread plan.
virtual bool MischiefManaged()
virtual lldb::RegisterContextSP GetRegisterContext()=0
#define LLDB_INVALID_BREAK_ID
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
void DumpAddress(llvm::raw_ostream &s, uint64_t addr, uint32_t addr_size, const char *prefix=nullptr, const char *suffix=nullptr)
Output an address value to this stream.
Definition Stream.cpp:108
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::RecognizedStackFrame > RecognizedStackFrameSP
std::shared_ptr< lldb_private::BreakpointSite > BreakpointSiteSP
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eDescriptionLevelBrief
@ eDescriptionLevelFull
StateType
Process and Thread States.
@ eStateRunning
Process or thread is running and can't be examined.
int32_t break_id_t
Definition lldb-types.h:88
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
uint64_t addr_t
Definition lldb-types.h:80
@ eStopReasonBreakpoint