15ffd83dbSDimitry Andric //===-- ThreadPlanStepInstruction.cpp -------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "lldb/Target/ThreadPlanStepInstruction.h"
100b57cec5SDimitry Andric #include "lldb/Target/Process.h"
110b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
120b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h"
130b57cec5SDimitry Andric #include "lldb/Target/Target.h"
1481ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
150b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
160b57cec5SDimitry Andric #include "lldb/Utility/Stream.h"
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric using namespace lldb;
190b57cec5SDimitry Andric using namespace lldb_private;
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric // ThreadPlanStepInstruction: Step over the current instruction
220b57cec5SDimitry Andric
ThreadPlanStepInstruction(Thread & thread,bool step_over,bool stop_other_threads,Vote report_stop_vote,Vote report_run_vote)230b57cec5SDimitry Andric ThreadPlanStepInstruction::ThreadPlanStepInstruction(Thread &thread,
240b57cec5SDimitry Andric bool step_over,
250b57cec5SDimitry Andric bool stop_other_threads,
26fe6060f1SDimitry Andric Vote report_stop_vote,
27fe6060f1SDimitry Andric Vote report_run_vote)
280b57cec5SDimitry Andric : ThreadPlan(ThreadPlan::eKindStepInstruction,
29fe6060f1SDimitry Andric "Step over single instruction", thread, report_stop_vote,
30fe6060f1SDimitry Andric report_run_vote),
310b57cec5SDimitry Andric m_instruction_addr(0), m_stop_other_threads(stop_other_threads),
320b57cec5SDimitry Andric m_step_over(step_over) {
330b57cec5SDimitry Andric m_takes_iteration_count = true;
340b57cec5SDimitry Andric SetUpState();
350b57cec5SDimitry Andric }
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric ThreadPlanStepInstruction::~ThreadPlanStepInstruction() = default;
380b57cec5SDimitry Andric
SetUpState()390b57cec5SDimitry Andric void ThreadPlanStepInstruction::SetUpState() {
405ffd83dbSDimitry Andric Thread &thread = GetThread();
415ffd83dbSDimitry Andric m_instruction_addr = thread.GetRegisterContext()->GetPC(0);
425ffd83dbSDimitry Andric StackFrameSP start_frame_sp(thread.GetStackFrameAtIndex(0));
430b57cec5SDimitry Andric m_stack_id = start_frame_sp->GetStackID();
440b57cec5SDimitry Andric
450b57cec5SDimitry Andric m_start_has_symbol =
460b57cec5SDimitry Andric start_frame_sp->GetSymbolContext(eSymbolContextSymbol).symbol != nullptr;
470b57cec5SDimitry Andric
485ffd83dbSDimitry Andric StackFrameSP parent_frame_sp = thread.GetStackFrameAtIndex(1);
490b57cec5SDimitry Andric if (parent_frame_sp)
500b57cec5SDimitry Andric m_parent_frame_id = parent_frame_sp->GetStackID();
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric
GetDescription(Stream * s,lldb::DescriptionLevel level)530b57cec5SDimitry Andric void ThreadPlanStepInstruction::GetDescription(Stream *s,
540b57cec5SDimitry Andric lldb::DescriptionLevel level) {
550b57cec5SDimitry Andric auto PrintFailureIfAny = [&]() {
560b57cec5SDimitry Andric if (m_status.Success())
570b57cec5SDimitry Andric return;
580b57cec5SDimitry Andric s->Printf(" failed (%s)", m_status.AsCString());
590b57cec5SDimitry Andric };
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric if (level == lldb::eDescriptionLevelBrief) {
620b57cec5SDimitry Andric if (m_step_over)
630b57cec5SDimitry Andric s->Printf("instruction step over");
640b57cec5SDimitry Andric else
650b57cec5SDimitry Andric s->Printf("instruction step into");
660b57cec5SDimitry Andric
670b57cec5SDimitry Andric PrintFailureIfAny();
680b57cec5SDimitry Andric } else {
690b57cec5SDimitry Andric s->Printf("Stepping one instruction past ");
70480093f4SDimitry Andric DumpAddress(s->AsRawOstream(), m_instruction_addr, sizeof(addr_t));
710b57cec5SDimitry Andric if (!m_start_has_symbol)
720b57cec5SDimitry Andric s->Printf(" which has no symbol");
730b57cec5SDimitry Andric
740b57cec5SDimitry Andric if (m_step_over)
750b57cec5SDimitry Andric s->Printf(" stepping over calls");
760b57cec5SDimitry Andric else
770b57cec5SDimitry Andric s->Printf(" stepping into calls");
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric PrintFailureIfAny();
800b57cec5SDimitry Andric }
810b57cec5SDimitry Andric }
820b57cec5SDimitry Andric
ValidatePlan(Stream * error)830b57cec5SDimitry Andric bool ThreadPlanStepInstruction::ValidatePlan(Stream *error) {
840b57cec5SDimitry Andric // Since we read the instruction we're stepping over from the thread, this
850b57cec5SDimitry Andric // plan will always work.
860b57cec5SDimitry Andric return true;
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric
DoPlanExplainsStop(Event * event_ptr)890b57cec5SDimitry Andric bool ThreadPlanStepInstruction::DoPlanExplainsStop(Event *event_ptr) {
900b57cec5SDimitry Andric StopInfoSP stop_info_sp = GetPrivateStopInfo();
910b57cec5SDimitry Andric if (stop_info_sp) {
920b57cec5SDimitry Andric StopReason reason = stop_info_sp->GetStopReason();
930b57cec5SDimitry Andric return (reason == eStopReasonTrace || reason == eStopReasonNone);
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric return false;
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric
IsPlanStale()980b57cec5SDimitry Andric bool ThreadPlanStepInstruction::IsPlanStale() {
9981ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Step);
1005ffd83dbSDimitry Andric Thread &thread = GetThread();
1015ffd83dbSDimitry Andric StackID cur_frame_id = thread.GetStackFrameAtIndex(0)->GetStackID();
1020b57cec5SDimitry Andric if (cur_frame_id == m_stack_id) {
1030b57cec5SDimitry Andric // Set plan Complete when we reach next instruction
1045ffd83dbSDimitry Andric uint64_t pc = thread.GetRegisterContext()->GetPC(0);
1055ffd83dbSDimitry Andric uint32_t max_opcode_size =
1065ffd83dbSDimitry Andric GetTarget().GetArchitecture().GetMaximumOpcodeByteSize();
1070b57cec5SDimitry Andric bool next_instruction_reached = (pc > m_instruction_addr) &&
1080b57cec5SDimitry Andric (pc <= m_instruction_addr + max_opcode_size);
1090b57cec5SDimitry Andric if (next_instruction_reached) {
1100b57cec5SDimitry Andric SetPlanComplete();
1110b57cec5SDimitry Andric }
1125ffd83dbSDimitry Andric return (thread.GetRegisterContext()->GetPC(0) != m_instruction_addr);
1130b57cec5SDimitry Andric } else if (cur_frame_id < m_stack_id) {
1140b57cec5SDimitry Andric // If the current frame is younger than the start frame and we are stepping
1150b57cec5SDimitry Andric // over, then we need to continue, but if we are doing just one step, we're
1160b57cec5SDimitry Andric // done.
1170b57cec5SDimitry Andric return !m_step_over;
1180b57cec5SDimitry Andric } else {
1190b57cec5SDimitry Andric if (log) {
1209dba64beSDimitry Andric LLDB_LOGF(log,
1219dba64beSDimitry Andric "ThreadPlanStepInstruction::IsPlanStale - Current frame is "
1220b57cec5SDimitry Andric "older than start frame, plan is stale.");
1230b57cec5SDimitry Andric }
1240b57cec5SDimitry Andric return true;
1250b57cec5SDimitry Andric }
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric
ShouldStop(Event * event_ptr)1280b57cec5SDimitry Andric bool ThreadPlanStepInstruction::ShouldStop(Event *event_ptr) {
1295ffd83dbSDimitry Andric Thread &thread = GetThread();
1300b57cec5SDimitry Andric if (m_step_over) {
13181ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Step);
1325ffd83dbSDimitry Andric StackFrameSP cur_frame_sp = thread.GetStackFrameAtIndex(0);
1330b57cec5SDimitry Andric if (!cur_frame_sp) {
1349dba64beSDimitry Andric LLDB_LOGF(
1359dba64beSDimitry Andric log,
1360b57cec5SDimitry Andric "ThreadPlanStepInstruction couldn't get the 0th frame, stopping.");
1370b57cec5SDimitry Andric SetPlanComplete();
1380b57cec5SDimitry Andric return true;
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric
1410b57cec5SDimitry Andric StackID cur_frame_zero_id = cur_frame_sp->GetStackID();
1420b57cec5SDimitry Andric
1430b57cec5SDimitry Andric if (cur_frame_zero_id == m_stack_id || m_stack_id < cur_frame_zero_id) {
1445ffd83dbSDimitry Andric if (thread.GetRegisterContext()->GetPC(0) != m_instruction_addr) {
1450b57cec5SDimitry Andric if (--m_iteration_count <= 0) {
1460b57cec5SDimitry Andric SetPlanComplete();
1470b57cec5SDimitry Andric return true;
1480b57cec5SDimitry Andric } else {
1490b57cec5SDimitry Andric // We are still stepping, reset the start pc, and in case we've
1500b57cec5SDimitry Andric // stepped out, reset the current stack id.
1510b57cec5SDimitry Andric SetUpState();
1520b57cec5SDimitry Andric return false;
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric } else
1550b57cec5SDimitry Andric return false;
1560b57cec5SDimitry Andric } else {
1570b57cec5SDimitry Andric // We've stepped in, step back out again:
1585ffd83dbSDimitry Andric StackFrame *return_frame = thread.GetStackFrameAtIndex(1).get();
1590b57cec5SDimitry Andric if (return_frame) {
1600b57cec5SDimitry Andric if (return_frame->GetStackID() != m_parent_frame_id ||
1610b57cec5SDimitry Andric m_start_has_symbol) {
1620b57cec5SDimitry Andric // next-instruction shouldn't step out of inlined functions. But we
1630b57cec5SDimitry Andric // may have stepped into a real function that starts with an inlined
1640b57cec5SDimitry Andric // function, and we do want to step out of that...
1650b57cec5SDimitry Andric
1660b57cec5SDimitry Andric if (cur_frame_sp->IsInlined()) {
1670b57cec5SDimitry Andric StackFrameSP parent_frame_sp =
1685ffd83dbSDimitry Andric thread.GetFrameWithStackID(m_stack_id);
1690b57cec5SDimitry Andric
1700b57cec5SDimitry Andric if (parent_frame_sp &&
1710b57cec5SDimitry Andric parent_frame_sp->GetConcreteFrameIndex() ==
1720b57cec5SDimitry Andric cur_frame_sp->GetConcreteFrameIndex()) {
1730b57cec5SDimitry Andric SetPlanComplete();
1740b57cec5SDimitry Andric if (log) {
1759dba64beSDimitry Andric LLDB_LOGF(log,
1769dba64beSDimitry Andric "Frame we stepped into is inlined into the frame "
1770b57cec5SDimitry Andric "we were stepping from, stopping.");
1780b57cec5SDimitry Andric }
1790b57cec5SDimitry Andric return true;
1800b57cec5SDimitry Andric }
1810b57cec5SDimitry Andric }
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andric if (log) {
1840b57cec5SDimitry Andric StreamString s;
1850b57cec5SDimitry Andric s.PutCString("Stepped in to: ");
1860b57cec5SDimitry Andric addr_t stop_addr =
1875ffd83dbSDimitry Andric thread.GetStackFrameAtIndex(0)->GetRegisterContext()->GetPC();
188480093f4SDimitry Andric DumpAddress(s.AsRawOstream(), stop_addr,
1895ffd83dbSDimitry Andric GetTarget().GetArchitecture().GetAddressByteSize());
1900b57cec5SDimitry Andric s.PutCString(" stepping out to: ");
1910b57cec5SDimitry Andric addr_t return_addr = return_frame->GetRegisterContext()->GetPC();
192480093f4SDimitry Andric DumpAddress(s.AsRawOstream(), return_addr,
1935ffd83dbSDimitry Andric GetTarget().GetArchitecture().GetAddressByteSize());
1949dba64beSDimitry Andric LLDB_LOGF(log, "%s.", s.GetData());
1950b57cec5SDimitry Andric }
1960b57cec5SDimitry Andric
1970b57cec5SDimitry Andric // StepInstruction should probably have the tri-state RunMode, but
1980b57cec5SDimitry Andric // for now it is safer to run others.
1990b57cec5SDimitry Andric const bool stop_others = false;
2005ffd83dbSDimitry Andric thread.QueueThreadPlanForStepOutNoShouldStop(
2010b57cec5SDimitry Andric false, nullptr, true, stop_others, eVoteNo, eVoteNoOpinion, 0,
2020b57cec5SDimitry Andric m_status);
2030b57cec5SDimitry Andric return false;
2040b57cec5SDimitry Andric } else {
2050b57cec5SDimitry Andric if (log) {
2060b57cec5SDimitry Andric log->PutCString(
2070b57cec5SDimitry Andric "The stack id we are stepping in changed, but our parent frame "
2080b57cec5SDimitry Andric "did not when stepping from code with no symbols. "
2090b57cec5SDimitry Andric "We are probably just confused about where we are, stopping.");
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric SetPlanComplete();
2120b57cec5SDimitry Andric return true;
2130b57cec5SDimitry Andric }
2140b57cec5SDimitry Andric } else {
2159dba64beSDimitry Andric LLDB_LOGF(log, "Could not find previous frame, stopping.");
2160b57cec5SDimitry Andric SetPlanComplete();
2170b57cec5SDimitry Andric return true;
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric }
2200b57cec5SDimitry Andric } else {
2215ffd83dbSDimitry Andric lldb::addr_t pc_addr = thread.GetRegisterContext()->GetPC(0);
2220b57cec5SDimitry Andric if (pc_addr != m_instruction_addr) {
2230b57cec5SDimitry Andric if (--m_iteration_count <= 0) {
2240b57cec5SDimitry Andric SetPlanComplete();
2250b57cec5SDimitry Andric return true;
2260b57cec5SDimitry Andric } else {
2270b57cec5SDimitry Andric // We are still stepping, reset the start pc, and in case we've stepped
2280b57cec5SDimitry Andric // in or out, reset the current stack id.
2290b57cec5SDimitry Andric SetUpState();
2300b57cec5SDimitry Andric return false;
2310b57cec5SDimitry Andric }
2320b57cec5SDimitry Andric } else
2330b57cec5SDimitry Andric return false;
2340b57cec5SDimitry Andric }
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric
StopOthers()2370b57cec5SDimitry Andric bool ThreadPlanStepInstruction::StopOthers() { return m_stop_other_threads; }
2380b57cec5SDimitry Andric
GetPlanRunState()2390b57cec5SDimitry Andric StateType ThreadPlanStepInstruction::GetPlanRunState() {
2400b57cec5SDimitry Andric return eStateStepping;
2410b57cec5SDimitry Andric }
2420b57cec5SDimitry Andric
WillStop()2430b57cec5SDimitry Andric bool ThreadPlanStepInstruction::WillStop() { return true; }
2440b57cec5SDimitry Andric
MischiefManaged()2450b57cec5SDimitry Andric bool ThreadPlanStepInstruction::MischiefManaged() {
2460b57cec5SDimitry Andric if (IsPlanComplete()) {
24781ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Step);
2489dba64beSDimitry Andric LLDB_LOGF(log, "Completed single instruction step plan.");
2490b57cec5SDimitry Andric ThreadPlan::MischiefManaged();
2500b57cec5SDimitry Andric return true;
2510b57cec5SDimitry Andric } else {
2520b57cec5SDimitry Andric return false;
2530b57cec5SDimitry Andric }
2540b57cec5SDimitry Andric }
255