1cfca06d7SDimitry Andric //===-- ThreadPlanStepInstruction.cpp -------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9e81d9d49SDimitry Andric #include "lldb/Target/ThreadPlanStepInstruction.h"
10f034231aSEd Maste #include "lldb/Target/Process.h"
11f034231aSEd Maste #include "lldb/Target/RegisterContext.h"
12f034231aSEd Maste #include "lldb/Target/StopInfo.h"
13f034231aSEd Maste #include "lldb/Target/Target.h"
14145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
1574a628f7SDimitry Andric #include "lldb/Utility/Log.h"
1674a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
17f034231aSEd Maste
18f034231aSEd Maste using namespace lldb;
19f034231aSEd Maste using namespace lldb_private;
20f034231aSEd Maste
21f034231aSEd Maste // ThreadPlanStepInstruction: Step over the current instruction
22f034231aSEd Maste
ThreadPlanStepInstruction(Thread & thread,bool step_over,bool stop_other_threads,Vote report_stop_vote,Vote report_run_vote)2314f1b3e8SDimitry Andric ThreadPlanStepInstruction::ThreadPlanStepInstruction(Thread &thread,
24f034231aSEd Maste bool step_over,
25f034231aSEd Maste bool stop_other_threads,
26344a3780SDimitry Andric Vote report_stop_vote,
27344a3780SDimitry Andric Vote report_run_vote)
2814f1b3e8SDimitry Andric : ThreadPlan(ThreadPlan::eKindStepInstruction,
29344a3780SDimitry Andric "Step over single instruction", thread, report_stop_vote,
30344a3780SDimitry Andric report_run_vote),
3114f1b3e8SDimitry Andric m_instruction_addr(0), m_stop_other_threads(stop_other_threads),
3214f1b3e8SDimitry Andric m_step_over(step_over) {
330cac4ca3SEd Maste m_takes_iteration_count = true;
340cac4ca3SEd Maste SetUpState();
35f034231aSEd Maste }
36f034231aSEd Maste
37e81d9d49SDimitry Andric ThreadPlanStepInstruction::~ThreadPlanStepInstruction() = default;
38f034231aSEd Maste
SetUpState()3914f1b3e8SDimitry Andric void ThreadPlanStepInstruction::SetUpState() {
40cfca06d7SDimitry Andric Thread &thread = GetThread();
41cfca06d7SDimitry Andric m_instruction_addr = thread.GetRegisterContext()->GetPC(0);
42cfca06d7SDimitry Andric StackFrameSP start_frame_sp(thread.GetStackFrameAtIndex(0));
430cac4ca3SEd Maste m_stack_id = start_frame_sp->GetStackID();
440cac4ca3SEd Maste
4514f1b3e8SDimitry Andric m_start_has_symbol =
4614f1b3e8SDimitry Andric start_frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol != nullptr;
470cac4ca3SEd Maste
48cfca06d7SDimitry Andric StackFrameSP parent_frame_sp = thread.GetStackFrameAtIndex(1);
490cac4ca3SEd Maste if (parent_frame_sp)
500cac4ca3SEd Maste m_parent_frame_id = parent_frame_sp->GetStackID();
510cac4ca3SEd Maste }
520cac4ca3SEd Maste
GetDescription(Stream * s,lldb::DescriptionLevel level)5314f1b3e8SDimitry Andric void ThreadPlanStepInstruction::GetDescription(Stream *s,
5414f1b3e8SDimitry Andric lldb::DescriptionLevel level) {
5594994d37SDimitry Andric auto PrintFailureIfAny = [&]() {
5694994d37SDimitry Andric if (m_status.Success())
5794994d37SDimitry Andric return;
5894994d37SDimitry Andric s->Printf(" failed (%s)", m_status.AsCString());
5994994d37SDimitry Andric };
6094994d37SDimitry Andric
6114f1b3e8SDimitry Andric if (level == lldb::eDescriptionLevelBrief) {
62f034231aSEd Maste if (m_step_over)
63f034231aSEd Maste s->Printf("instruction step over");
64f034231aSEd Maste else
65f034231aSEd Maste s->Printf("instruction step into");
6694994d37SDimitry Andric
6794994d37SDimitry Andric PrintFailureIfAny();
6814f1b3e8SDimitry Andric } else {
69f034231aSEd Maste s->Printf("Stepping one instruction past ");
70706b4fc4SDimitry Andric DumpAddress(s->AsRawOstream(), m_instruction_addr, sizeof(addr_t));
71f034231aSEd Maste if (!m_start_has_symbol)
72f034231aSEd Maste s->Printf(" which has no symbol");
73f034231aSEd Maste
74f034231aSEd Maste if (m_step_over)
75f034231aSEd Maste s->Printf(" stepping over calls");
76f034231aSEd Maste else
77f034231aSEd Maste s->Printf(" stepping into calls");
7894994d37SDimitry Andric
7994994d37SDimitry Andric PrintFailureIfAny();
80f034231aSEd Maste }
81f034231aSEd Maste }
82f034231aSEd Maste
ValidatePlan(Stream * error)8314f1b3e8SDimitry Andric bool ThreadPlanStepInstruction::ValidatePlan(Stream *error) {
84f73363f1SDimitry Andric // Since we read the instruction we're stepping over from the thread, this
85f73363f1SDimitry Andric // plan will always work.
86f034231aSEd Maste return true;
87f034231aSEd Maste }
88f034231aSEd Maste
DoPlanExplainsStop(Event * event_ptr)8914f1b3e8SDimitry Andric bool ThreadPlanStepInstruction::DoPlanExplainsStop(Event *event_ptr) {
90f034231aSEd Maste StopInfoSP stop_info_sp = GetPrivateStopInfo();
9114f1b3e8SDimitry Andric if (stop_info_sp) {
92f034231aSEd Maste StopReason reason = stop_info_sp->GetStopReason();
93e81d9d49SDimitry Andric return (reason == eStopReasonTrace || reason == eStopReasonNone);
94f034231aSEd Maste }
95f034231aSEd Maste return false;
96f034231aSEd Maste }
97f034231aSEd Maste
IsPlanStale()9814f1b3e8SDimitry Andric bool ThreadPlanStepInstruction::IsPlanStale() {
99145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
100cfca06d7SDimitry Andric Thread &thread = GetThread();
101cfca06d7SDimitry Andric StackID cur_frame_id = thread.GetStackFrameAtIndex(0)->GetStackID();
10214f1b3e8SDimitry Andric if (cur_frame_id == m_stack_id) {
10374a628f7SDimitry Andric // Set plan Complete when we reach next instruction
104cfca06d7SDimitry Andric uint64_t pc = thread.GetRegisterContext()->GetPC(0);
105cfca06d7SDimitry Andric uint32_t max_opcode_size =
106cfca06d7SDimitry Andric GetTarget().GetArchitecture().GetMaximumOpcodeByteSize();
10774a628f7SDimitry Andric bool next_instruction_reached = (pc > m_instruction_addr) &&
10874a628f7SDimitry Andric (pc <= m_instruction_addr + max_opcode_size);
10974a628f7SDimitry Andric if (next_instruction_reached) {
11074a628f7SDimitry Andric SetPlanComplete();
11174a628f7SDimitry Andric }
112cfca06d7SDimitry Andric return (thread.GetRegisterContext()->GetPC(0) != m_instruction_addr);
11314f1b3e8SDimitry Andric } else if (cur_frame_id < m_stack_id) {
11414f1b3e8SDimitry Andric // If the current frame is younger than the start frame and we are stepping
115f73363f1SDimitry Andric // over, then we need to continue, but if we are doing just one step, we're
116f73363f1SDimitry Andric // done.
117e81d9d49SDimitry Andric return !m_step_over;
11814f1b3e8SDimitry Andric } else {
11914f1b3e8SDimitry Andric if (log) {
120ead24645SDimitry Andric LLDB_LOGF(log,
121ead24645SDimitry Andric "ThreadPlanStepInstruction::IsPlanStale - Current frame is "
12214f1b3e8SDimitry Andric "older than start frame, plan is stale.");
1230cac4ca3SEd Maste }
1240cac4ca3SEd Maste return true;
1250cac4ca3SEd Maste }
1260cac4ca3SEd Maste }
1270cac4ca3SEd Maste
ShouldStop(Event * event_ptr)12814f1b3e8SDimitry Andric bool ThreadPlanStepInstruction::ShouldStop(Event *event_ptr) {
129cfca06d7SDimitry Andric Thread &thread = GetThread();
13014f1b3e8SDimitry Andric if (m_step_over) {
131145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
132cfca06d7SDimitry Andric StackFrameSP cur_frame_sp = thread.GetStackFrameAtIndex(0);
13314f1b3e8SDimitry Andric if (!cur_frame_sp) {
134ead24645SDimitry Andric LLDB_LOGF(
135ead24645SDimitry Andric log,
13614f1b3e8SDimitry Andric "ThreadPlanStepInstruction couldn't get the 0th frame, stopping.");
137205afe67SEd Maste SetPlanComplete();
138205afe67SEd Maste return true;
139205afe67SEd Maste }
140205afe67SEd Maste
141205afe67SEd Maste StackID cur_frame_zero_id = cur_frame_sp->GetStackID();
142f034231aSEd Maste
14314f1b3e8SDimitry Andric if (cur_frame_zero_id == m_stack_id || m_stack_id < cur_frame_zero_id) {
144cfca06d7SDimitry Andric if (thread.GetRegisterContext()->GetPC(0) != m_instruction_addr) {
14514f1b3e8SDimitry Andric if (--m_iteration_count <= 0) {
146f034231aSEd Maste SetPlanComplete();
147f034231aSEd Maste return true;
14814f1b3e8SDimitry Andric } else {
14914f1b3e8SDimitry Andric // We are still stepping, reset the start pc, and in case we've
150f73363f1SDimitry Andric // stepped out, reset the current stack id.
1510cac4ca3SEd Maste SetUpState();
1520cac4ca3SEd Maste return false;
1530cac4ca3SEd Maste }
15414f1b3e8SDimitry Andric } else
155f034231aSEd Maste return false;
15614f1b3e8SDimitry Andric } else {
157f034231aSEd Maste // We've stepped in, step back out again:
158cfca06d7SDimitry Andric StackFrame *return_frame = thread.GetStackFrameAtIndex(1).get();
15914f1b3e8SDimitry Andric if (return_frame) {
16014f1b3e8SDimitry Andric if (return_frame->GetStackID() != m_parent_frame_id ||
16114f1b3e8SDimitry Andric m_start_has_symbol) {
16214f1b3e8SDimitry Andric // next-instruction shouldn't step out of inlined functions. But we
163f73363f1SDimitry Andric // may have stepped into a real function that starts with an inlined
164f73363f1SDimitry Andric // function, and we do want to step out of that...
165205afe67SEd Maste
16614f1b3e8SDimitry Andric if (cur_frame_sp->IsInlined()) {
16714f1b3e8SDimitry Andric StackFrameSP parent_frame_sp =
168cfca06d7SDimitry Andric thread.GetFrameWithStackID(m_stack_id);
169205afe67SEd Maste
17014f1b3e8SDimitry Andric if (parent_frame_sp &&
17114f1b3e8SDimitry Andric parent_frame_sp->GetConcreteFrameIndex() ==
17214f1b3e8SDimitry Andric cur_frame_sp->GetConcreteFrameIndex()) {
173205afe67SEd Maste SetPlanComplete();
17414f1b3e8SDimitry Andric if (log) {
175ead24645SDimitry Andric LLDB_LOGF(log,
176ead24645SDimitry Andric "Frame we stepped into is inlined into the frame "
17714f1b3e8SDimitry Andric "we were stepping from, stopping.");
178205afe67SEd Maste }
179205afe67SEd Maste return true;
180205afe67SEd Maste }
181205afe67SEd Maste }
182205afe67SEd Maste
18314f1b3e8SDimitry Andric if (log) {
184f034231aSEd Maste StreamString s;
185f034231aSEd Maste s.PutCString("Stepped in to: ");
18614f1b3e8SDimitry Andric addr_t stop_addr =
187cfca06d7SDimitry Andric thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
188706b4fc4SDimitry Andric DumpAddress(s.AsRawOstream(), stop_addr,
189cfca06d7SDimitry Andric GetTarget().GetArchitecture().GetAddressByteSize());
190f034231aSEd Maste s.PutCString(" stepping out to: ");
191f034231aSEd Maste addr_t return_addr = return_frame->GetRegisterContext()->GetPC();
192706b4fc4SDimitry Andric DumpAddress(s.AsRawOstream(), return_addr,
193cfca06d7SDimitry Andric GetTarget().GetArchitecture().GetAddressByteSize());
194ead24645SDimitry Andric LLDB_LOGF(log, "%s.", s.GetData());
195f034231aSEd Maste }
196f034231aSEd Maste
197f73363f1SDimitry Andric // StepInstruction should probably have the tri-state RunMode, but
198f73363f1SDimitry Andric // for now it is safer to run others.
199f034231aSEd Maste const bool stop_others = false;
200cfca06d7SDimitry Andric thread.QueueThreadPlanForStepOutNoShouldStop(
20194994d37SDimitry Andric false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0,
20294994d37SDimitry Andric m_status);
203f034231aSEd Maste return false;
20414f1b3e8SDimitry Andric } else {
20514f1b3e8SDimitry Andric if (log) {
20614f1b3e8SDimitry Andric log->PutCString(
20714f1b3e8SDimitry Andric "The stack id we are stepping in changed, but our parent frame "
20814f1b3e8SDimitry Andric "did not when stepping from code with no symbols. "
209f034231aSEd Maste "We are probably just confused about where we are, stopping.");
210f034231aSEd Maste }
211f034231aSEd Maste SetPlanComplete();
212f034231aSEd Maste return true;
213f034231aSEd Maste }
21414f1b3e8SDimitry Andric } else {
215ead24645SDimitry Andric LLDB_LOGF(log, "Could not find previous frame, stopping.");
216f034231aSEd Maste SetPlanComplete();
217f034231aSEd Maste return true;
218f034231aSEd Maste }
219f034231aSEd Maste }
22014f1b3e8SDimitry Andric } else {
221cfca06d7SDimitry Andric lldb::addr_t pc_addr = thread.GetRegisterContext()->GetPC(0);
22214f1b3e8SDimitry Andric if (pc_addr != m_instruction_addr) {
22314f1b3e8SDimitry Andric if (--m_iteration_count <= 0) {
224f034231aSEd Maste SetPlanComplete();
225f034231aSEd Maste return true;
22614f1b3e8SDimitry Andric } else {
22714f1b3e8SDimitry Andric // We are still stepping, reset the start pc, and in case we've stepped
228f73363f1SDimitry Andric // in or out, reset the current stack id.
2290cac4ca3SEd Maste SetUpState();
2300cac4ca3SEd Maste return false;
2310cac4ca3SEd Maste }
23214f1b3e8SDimitry Andric } else
233f034231aSEd Maste return false;
234f034231aSEd Maste }
235f034231aSEd Maste }
236f034231aSEd Maste
StopOthers()23714f1b3e8SDimitry Andric bool ThreadPlanStepInstruction::StopOthers() { return m_stop_other_threads; }
238f034231aSEd Maste
GetPlanRunState()23914f1b3e8SDimitry Andric StateType ThreadPlanStepInstruction::GetPlanRunState() {
240f034231aSEd Maste return eStateStepping;
241f034231aSEd Maste }
242f034231aSEd Maste
WillStop()24314f1b3e8SDimitry Andric bool ThreadPlanStepInstruction::WillStop() { return true; }
244f034231aSEd Maste
MischiefManaged()24514f1b3e8SDimitry Andric bool ThreadPlanStepInstruction::MischiefManaged() {
24614f1b3e8SDimitry Andric if (IsPlanComplete()) {
247145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Step);
248ead24645SDimitry Andric LLDB_LOGF(log, "Completed single instruction step plan.");
249f034231aSEd Maste ThreadPlan::MischiefManaged();
250f034231aSEd Maste return true;
25114f1b3e8SDimitry Andric } else {
252f034231aSEd Maste return false;
253f034231aSEd Maste }
254f034231aSEd Maste }
255