LLDB mainline
ThreadPlanStepInstruction.cpp
Go to the documentation of this file.
1//===-- ThreadPlanStepInstruction.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
10#include "lldb/Target/Process.h"
13#include "lldb/Target/Target.h"
15#include "lldb/Utility/Log.h"
16#include "lldb/Utility/Stream.h"
17
18using namespace lldb;
19using namespace lldb_private;
20
21// ThreadPlanStepInstruction: Step over the current instruction
22
24 bool step_over,
25 bool stop_other_threads,
26 Vote report_stop_vote,
27 Vote report_run_vote)
29 "Step over single instruction", thread, report_stop_vote,
30 report_run_vote),
31 m_instruction_addr(0), m_stop_other_threads(stop_other_threads),
32 m_step_over(step_over) {
34 SetUpState();
35}
36
38
40 Thread &thread = GetThread();
41 m_instruction_addr = thread.GetRegisterContext()->GetPC(0);
42 StackFrameSP start_frame_sp(thread.GetStackFrameAtIndex(0));
43 m_stack_id = start_frame_sp->GetStackID();
44
46 start_frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol != nullptr;
47
48 StackFrameSP parent_frame_sp = thread.GetStackFrameAtIndex(1);
49 if (parent_frame_sp)
50 m_parent_frame_id = parent_frame_sp->GetStackID();
51}
52
55 auto PrintFailureIfAny = [&]() {
56 if (m_status.Success())
57 return;
58 s->Printf(" failed (%s)", m_status.AsCString());
59 };
60
61 if (level == lldb::eDescriptionLevelBrief) {
62 if (m_step_over)
63 s->PutCString("instruction step over");
64 else
65 s->PutCString("instruction step into");
66
67 PrintFailureIfAny();
68 } else {
69 s->PutCString("Stepping one instruction past ");
72 s->PutCString(" which has no symbol");
73
74 if (m_step_over)
75 s->PutCString(" stepping over calls");
76 else
77 s->PutCString(" stepping into calls");
78
79 PrintFailureIfAny();
80 }
81}
82
84 // Since we read the instruction we're stepping over from the thread, this
85 // plan will always work.
86 return true;
87}
88
90 StopInfoSP stop_info_sp = GetPrivateStopInfo();
91 if (stop_info_sp) {
92 StopReason reason = stop_info_sp->GetStopReason();
93 return (reason == eStopReasonTrace || reason == eStopReasonNone);
94 }
95 return false;
96}
97
99 Log *log = GetLog(LLDBLog::Step);
100 Thread &thread = GetThread();
101 StackID cur_frame_id = thread.GetStackFrameAtIndex(0)->GetStackID();
102 if (cur_frame_id == m_stack_id) {
103 // Set plan Complete when we reach next instruction
104 uint64_t pc = thread.GetRegisterContext()->GetPC(0);
105 uint32_t max_opcode_size =
107 bool next_instruction_reached = (pc > m_instruction_addr) &&
108 (pc <= m_instruction_addr + max_opcode_size);
109 if (next_instruction_reached) {
111 }
112 return (thread.GetRegisterContext()->GetPC(0) != m_instruction_addr);
113 } else if (cur_frame_id.IsYoungerThan(m_stack_id)) {
114 // If the current frame is younger than the start frame and we are stepping
115 // over, then we need to continue, but if we are doing just one step, we're
116 // done.
117 return !m_step_over;
118 } else {
119 LLDB_LOGF(log, "ThreadPlanStepInstruction::IsPlanStale - Current frame is "
120 "older than start frame, plan is stale.");
121 return true;
122 }
123}
124
126 Thread &thread = GetThread();
127 if (m_step_over) {
128 Log *log = GetLog(LLDBLog::Step);
129 StackFrameSP cur_frame_sp = thread.GetStackFrameAtIndex(0);
130 if (!cur_frame_sp) {
131 LLDB_LOGF(
132 log,
133 "ThreadPlanStepInstruction couldn't get the 0th frame, stopping.");
135 return true;
136 }
137
138 StackID cur_frame_zero_id = cur_frame_sp->GetStackID();
139
140 if (cur_frame_zero_id == m_stack_id ||
141 m_stack_id.IsYoungerThan(cur_frame_zero_id)) {
142 if (thread.GetRegisterContext()->GetPC(0) != m_instruction_addr) {
143 if (--m_iteration_count <= 0) {
145 return true;
146 } else {
147 // We are still stepping, reset the start pc, and in case we've
148 // stepped out, reset the current stack id.
149 SetUpState();
150 return false;
151 }
152 } else
153 return false;
154 } else {
155 // We've stepped in, step back out again:
156 StackFrame *return_frame = thread.GetStackFrameAtIndex(1).get();
157 if (return_frame) {
158 if (return_frame->GetStackID() != m_parent_frame_id ||
160 // next-instruction shouldn't step out of inlined functions. But we
161 // may have stepped into a real function that starts with an inlined
162 // function, and we do want to step out of that...
163
164 if (cur_frame_sp->IsInlined()) {
165 StackFrameSP parent_frame_sp =
166 thread.GetFrameWithStackID(m_stack_id);
167
168 if (parent_frame_sp &&
169 parent_frame_sp->GetConcreteFrameIndex() ==
170 cur_frame_sp->GetConcreteFrameIndex()) {
172 LLDB_LOGF(log, "Frame we stepped into is inlined into the frame "
173 "we were stepping from, stopping.");
174 return true;
175 }
176 }
177
178 if (log) {
179 StreamString s;
180 s.PutCString("Stepped in to: ");
181 addr_t stop_addr =
182 thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
183 DumpAddress(s.AsRawOstream(), stop_addr,
184 GetTarget().GetArchitecture().GetAddressByteSize());
185 s.PutCString(" stepping out to: ");
186 addr_t return_addr = return_frame->GetRegisterContext()->GetPC();
187 DumpAddress(s.AsRawOstream(), return_addr,
188 GetTarget().GetArchitecture().GetAddressByteSize());
189 LLDB_LOGF(log, "%s.", s.GetData());
190 }
191
192 // StepInstruction should probably have the tri-state RunMode, but
193 // for now it is safer to run others.
194 const bool stop_others = false;
195 thread.QueueThreadPlanForStepOutNoShouldStop(
196 false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0,
197 m_status);
198 return false;
199 } else {
200 if (log) {
201 log->PutCString(
202 "The stack id we are stepping in changed, but our parent frame "
203 "did not when stepping from code with no symbols. "
204 "We are probably just confused about where we are, stopping.");
205 }
207 return true;
208 }
209 } else {
210 LLDB_LOGF(log, "Could not find previous frame, stopping.");
212 return true;
213 }
214 }
215 } else {
216 lldb::addr_t pc_addr = thread.GetRegisterContext()->GetPC(0);
217 if (pc_addr != m_instruction_addr) {
218 if (--m_iteration_count <= 0) {
220 return true;
221 } else {
222 // We are still stepping, reset the start pc, and in case we've stepped
223 // in or out, reset the current stack id.
224 SetUpState();
225 return false;
226 }
227 } else
228 return false;
229 }
230}
231
233
237
239
241 if (IsPlanComplete()) {
242 Log *log = GetLog(LLDBLog::Step);
243 LLDB_LOGF(log, "Completed single instruction step plan.");
245 return true;
246 } else {
247 return false;
248 }
249}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition Log.h:389
uint32_t GetMaximumOpcodeByteSize() const
void PutCString(const char *cstr)
Definition Log.cpp:162
This base class provides an interface to stack frames.
Definition StackFrame.h:44
virtual lldb::RegisterContextSP GetRegisterContext()
Get the RegisterContext for this frame, if possible.
virtual StackID & GetStackID()
bool IsYoungerThan(const StackID &other) const
Returns true if this StackID corresponds to a frame younger (i.e.
Definition StackID.cpp:72
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
const ArchSpec & GetArchitecture() const
Definition Target.h:1289
bool DoPlanExplainsStop(Event *event_ptr) override
ThreadPlanStepInstruction(Thread &thread, bool step_over, bool stop_others, Vote report_stop_vote, Vote report_run_vote)
void GetDescription(Stream *s, lldb::DescriptionLevel level) override
Print a description of this thread to the stream s.
bool ValidatePlan(Stream *error) override
Returns whether this plan could be successfully created.
ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread, Vote report_stop_vote, Vote report_run_vote)
void SetPlanComplete(bool success=true)
Thread & GetThread()
Returns the Thread that is using this thread plan.
virtual bool MischiefManaged()
lldb::StopInfoSP GetPrivateStopInfo()
Definition ThreadPlan.h:544
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
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eDescriptionLevelBrief
StateType
Process and Thread States.
@ eStateStepping
Process or thread is in the process of stepping and can not be examined.
std::shared_ptr< lldb_private::StopInfo > StopInfoSP
uint64_t addr_t
Definition lldb-types.h:80
StopReason
Thread stop reasons.