15ffd83dbSDimitry Andric //===-- ThreadPlanStepUntil.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/ThreadPlanStepUntil.h"
100b57cec5SDimitry Andric
110b57cec5SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
120b57cec5SDimitry Andric #include "lldb/Symbol/SymbolContextScope.h"
130b57cec5SDimitry Andric #include "lldb/Target/Process.h"
140b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
150b57cec5SDimitry Andric #include "lldb/Target/StopInfo.h"
160b57cec5SDimitry Andric #include "lldb/Target/Target.h"
1781ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
180b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric using namespace lldb;
210b57cec5SDimitry Andric using namespace lldb_private;
220b57cec5SDimitry Andric
230b57cec5SDimitry Andric // ThreadPlanStepUntil: Run until we reach a given line number or step out of
240b57cec5SDimitry Andric // the current frame
250b57cec5SDimitry Andric
ThreadPlanStepUntil(Thread & thread,lldb::addr_t * address_list,size_t num_addresses,bool stop_others,uint32_t frame_idx)260b57cec5SDimitry Andric ThreadPlanStepUntil::ThreadPlanStepUntil(Thread &thread,
270b57cec5SDimitry Andric lldb::addr_t *address_list,
280b57cec5SDimitry Andric size_t num_addresses, bool stop_others,
290b57cec5SDimitry Andric uint32_t frame_idx)
300b57cec5SDimitry Andric : ThreadPlan(ThreadPlan::eKindStepUntil, "Step until", thread,
310b57cec5SDimitry Andric eVoteNoOpinion, eVoteNoOpinion),
320b57cec5SDimitry Andric m_step_from_insn(LLDB_INVALID_ADDRESS),
330b57cec5SDimitry Andric m_return_bp_id(LLDB_INVALID_BREAK_ID),
340b57cec5SDimitry Andric m_return_addr(LLDB_INVALID_ADDRESS), m_stepped_out(false),
350b57cec5SDimitry Andric m_should_stop(false), m_ran_analyze(false), m_explains_stop(false),
360b57cec5SDimitry Andric m_until_points(), m_stop_others(stop_others) {
370b57cec5SDimitry Andric // Stash away our "until" addresses:
385ffd83dbSDimitry Andric TargetSP target_sp(thread.CalculateTarget());
390b57cec5SDimitry Andric
405ffd83dbSDimitry Andric StackFrameSP frame_sp(thread.GetStackFrameAtIndex(frame_idx));
410b57cec5SDimitry Andric if (frame_sp) {
420b57cec5SDimitry Andric m_step_from_insn = frame_sp->GetStackID().GetPC();
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric // Find the return address and set a breakpoint there:
450b57cec5SDimitry Andric // FIXME - can we do this more securely if we know first_insn?
460b57cec5SDimitry Andric
475ffd83dbSDimitry Andric StackFrameSP return_frame_sp(thread.GetStackFrameAtIndex(frame_idx + 1));
480b57cec5SDimitry Andric if (return_frame_sp) {
490b57cec5SDimitry Andric // TODO: add inline functionality
500b57cec5SDimitry Andric m_return_addr = return_frame_sp->GetStackID().GetPC();
510b57cec5SDimitry Andric Breakpoint *return_bp =
520b57cec5SDimitry Andric target_sp->CreateBreakpoint(m_return_addr, true, false).get();
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric if (return_bp != nullptr) {
550b57cec5SDimitry Andric if (return_bp->IsHardware() && !return_bp->HasResolvedLocations())
560b57cec5SDimitry Andric m_could_not_resolve_hw_bp = true;
575ffd83dbSDimitry Andric return_bp->SetThreadID(m_tid);
580b57cec5SDimitry Andric m_return_bp_id = return_bp->GetID();
590b57cec5SDimitry Andric return_bp->SetBreakpointKind("until-return-backstop");
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric m_stack_id = frame_sp->GetStackID();
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric // Now set breakpoints on all our return addresses:
660b57cec5SDimitry Andric for (size_t i = 0; i < num_addresses; i++) {
670b57cec5SDimitry Andric Breakpoint *until_bp =
680b57cec5SDimitry Andric target_sp->CreateBreakpoint(address_list[i], true, false).get();
690b57cec5SDimitry Andric if (until_bp != nullptr) {
705ffd83dbSDimitry Andric until_bp->SetThreadID(m_tid);
710b57cec5SDimitry Andric m_until_points[address_list[i]] = until_bp->GetID();
720b57cec5SDimitry Andric until_bp->SetBreakpointKind("until-target");
730b57cec5SDimitry Andric } else {
740b57cec5SDimitry Andric m_until_points[address_list[i]] = LLDB_INVALID_BREAK_ID;
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric
~ThreadPlanStepUntil()800b57cec5SDimitry Andric ThreadPlanStepUntil::~ThreadPlanStepUntil() { Clear(); }
810b57cec5SDimitry Andric
Clear()820b57cec5SDimitry Andric void ThreadPlanStepUntil::Clear() {
835ffd83dbSDimitry Andric Target &target = GetTarget();
840b57cec5SDimitry Andric if (m_return_bp_id != LLDB_INVALID_BREAK_ID) {
855ffd83dbSDimitry Andric target.RemoveBreakpointByID(m_return_bp_id);
860b57cec5SDimitry Andric m_return_bp_id = LLDB_INVALID_BREAK_ID;
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric until_collection::iterator pos, end = m_until_points.end();
900b57cec5SDimitry Andric for (pos = m_until_points.begin(); pos != end; pos++) {
915ffd83dbSDimitry Andric target.RemoveBreakpointByID((*pos).second);
920b57cec5SDimitry Andric }
930b57cec5SDimitry Andric m_until_points.clear();
940b57cec5SDimitry Andric m_could_not_resolve_hw_bp = false;
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric
GetDescription(Stream * s,lldb::DescriptionLevel level)970b57cec5SDimitry Andric void ThreadPlanStepUntil::GetDescription(Stream *s,
980b57cec5SDimitry Andric lldb::DescriptionLevel level) {
990b57cec5SDimitry Andric if (level == lldb::eDescriptionLevelBrief) {
1000b57cec5SDimitry Andric s->Printf("step until");
1010b57cec5SDimitry Andric if (m_stepped_out)
1020b57cec5SDimitry Andric s->Printf(" - stepped out");
1030b57cec5SDimitry Andric } else {
1040b57cec5SDimitry Andric if (m_until_points.size() == 1)
1050b57cec5SDimitry Andric s->Printf("Stepping from address 0x%" PRIx64 " until we reach 0x%" PRIx64
1060b57cec5SDimitry Andric " using breakpoint %d",
1070b57cec5SDimitry Andric (uint64_t)m_step_from_insn,
1080b57cec5SDimitry Andric (uint64_t)(*m_until_points.begin()).first,
1090b57cec5SDimitry Andric (*m_until_points.begin()).second);
1100b57cec5SDimitry Andric else {
1110b57cec5SDimitry Andric until_collection::iterator pos, end = m_until_points.end();
1120b57cec5SDimitry Andric s->Printf("Stepping from address 0x%" PRIx64 " until we reach one of:",
1130b57cec5SDimitry Andric (uint64_t)m_step_from_insn);
1140b57cec5SDimitry Andric for (pos = m_until_points.begin(); pos != end; pos++) {
1150b57cec5SDimitry Andric s->Printf("\n\t0x%" PRIx64 " (bp: %d)", (uint64_t)(*pos).first,
1160b57cec5SDimitry Andric (*pos).second);
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric }
1190b57cec5SDimitry Andric s->Printf(" stepped out address is 0x%" PRIx64 ".",
1200b57cec5SDimitry Andric (uint64_t)m_return_addr);
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric }
1230b57cec5SDimitry Andric
ValidatePlan(Stream * error)1240b57cec5SDimitry Andric bool ThreadPlanStepUntil::ValidatePlan(Stream *error) {
1250b57cec5SDimitry Andric if (m_could_not_resolve_hw_bp) {
1260b57cec5SDimitry Andric if (error)
1270b57cec5SDimitry Andric error->PutCString(
1280b57cec5SDimitry Andric "Could not create hardware breakpoint for thread plan.");
1290b57cec5SDimitry Andric return false;
1300b57cec5SDimitry Andric } else if (m_return_bp_id == LLDB_INVALID_BREAK_ID) {
1310b57cec5SDimitry Andric if (error)
1320b57cec5SDimitry Andric error->PutCString("Could not create return breakpoint.");
1330b57cec5SDimitry Andric return false;
1340b57cec5SDimitry Andric } else {
1350b57cec5SDimitry Andric until_collection::iterator pos, end = m_until_points.end();
1360b57cec5SDimitry Andric for (pos = m_until_points.begin(); pos != end; pos++) {
1370b57cec5SDimitry Andric if (!LLDB_BREAK_ID_IS_VALID((*pos).second))
1380b57cec5SDimitry Andric return false;
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric return true;
1410b57cec5SDimitry Andric }
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric
AnalyzeStop()1440b57cec5SDimitry Andric void ThreadPlanStepUntil::AnalyzeStop() {
1450b57cec5SDimitry Andric if (m_ran_analyze)
1460b57cec5SDimitry Andric return;
1470b57cec5SDimitry Andric
1480b57cec5SDimitry Andric StopInfoSP stop_info_sp = GetPrivateStopInfo();
1490b57cec5SDimitry Andric m_should_stop = true;
1500b57cec5SDimitry Andric m_explains_stop = false;
1510b57cec5SDimitry Andric
1520b57cec5SDimitry Andric if (stop_info_sp) {
1530b57cec5SDimitry Andric StopReason reason = stop_info_sp->GetStopReason();
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric if (reason == eStopReasonBreakpoint) {
1560b57cec5SDimitry Andric // If this is OUR breakpoint, we're fine, otherwise we don't know why
1570b57cec5SDimitry Andric // this happened...
1580b57cec5SDimitry Andric BreakpointSiteSP this_site =
1595ffd83dbSDimitry Andric m_process.GetBreakpointSiteList().FindByID(stop_info_sp->GetValue());
1600b57cec5SDimitry Andric if (!this_site) {
1610b57cec5SDimitry Andric m_explains_stop = false;
1620b57cec5SDimitry Andric return;
1630b57cec5SDimitry Andric }
1640b57cec5SDimitry Andric
1650b57cec5SDimitry Andric if (this_site->IsBreakpointAtThisSite(m_return_bp_id)) {
1660b57cec5SDimitry Andric // If we are at our "step out" breakpoint, and the stack depth has
1670b57cec5SDimitry Andric // shrunk, then this is indeed our stop. If the stack depth has grown,
1680b57cec5SDimitry Andric // then we've hit our step out breakpoint recursively. If we are the
1690b57cec5SDimitry Andric // only breakpoint at that location, then we do explain the stop, and
1700b57cec5SDimitry Andric // we'll just continue. If there was another breakpoint here, then we
1710b57cec5SDimitry Andric // don't explain the stop, but we won't mark ourselves Completed,
1720b57cec5SDimitry Andric // because maybe that breakpoint will continue, and then we'll finish
1730b57cec5SDimitry Andric // the "until".
1740b57cec5SDimitry Andric bool done;
1750b57cec5SDimitry Andric StackID cur_frame_zero_id;
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andric done = (m_stack_id < cur_frame_zero_id);
1780b57cec5SDimitry Andric
1790b57cec5SDimitry Andric if (done) {
1800b57cec5SDimitry Andric m_stepped_out = true;
1810b57cec5SDimitry Andric SetPlanComplete();
1820b57cec5SDimitry Andric } else
1830b57cec5SDimitry Andric m_should_stop = false;
1840b57cec5SDimitry Andric
185*c9157d92SDimitry Andric if (this_site->GetNumberOfConstituents() == 1)
1860b57cec5SDimitry Andric m_explains_stop = true;
1870b57cec5SDimitry Andric else
1880b57cec5SDimitry Andric m_explains_stop = false;
1890b57cec5SDimitry Andric return;
1900b57cec5SDimitry Andric } else {
1910b57cec5SDimitry Andric // Check if we've hit one of our "until" breakpoints.
1920b57cec5SDimitry Andric until_collection::iterator pos, end = m_until_points.end();
1930b57cec5SDimitry Andric for (pos = m_until_points.begin(); pos != end; pos++) {
1940b57cec5SDimitry Andric if (this_site->IsBreakpointAtThisSite((*pos).second)) {
1950b57cec5SDimitry Andric // If we're at the right stack depth, then we're done.
1965ffd83dbSDimitry Andric Thread &thread = GetThread();
1970b57cec5SDimitry Andric bool done;
1980b57cec5SDimitry Andric StackID frame_zero_id =
1995ffd83dbSDimitry Andric thread.GetStackFrameAtIndex(0)->GetStackID();
2000b57cec5SDimitry Andric
2010b57cec5SDimitry Andric if (frame_zero_id == m_stack_id)
2020b57cec5SDimitry Andric done = true;
2030b57cec5SDimitry Andric else if (frame_zero_id < m_stack_id)
2040b57cec5SDimitry Andric done = false;
2050b57cec5SDimitry Andric else {
2065ffd83dbSDimitry Andric StackFrameSP older_frame_sp = thread.GetStackFrameAtIndex(1);
2070b57cec5SDimitry Andric
2080b57cec5SDimitry Andric // But if we can't even unwind one frame we should just get out
2090b57cec5SDimitry Andric // of here & stop...
2100b57cec5SDimitry Andric if (older_frame_sp) {
2110b57cec5SDimitry Andric const SymbolContext &older_context =
2120b57cec5SDimitry Andric older_frame_sp->GetSymbolContext(eSymbolContextEverything);
2130b57cec5SDimitry Andric SymbolContext stack_context;
2140b57cec5SDimitry Andric m_stack_id.GetSymbolContextScope()->CalculateSymbolContext(
2150b57cec5SDimitry Andric &stack_context);
2160b57cec5SDimitry Andric
2170b57cec5SDimitry Andric done = (older_context == stack_context);
2180b57cec5SDimitry Andric } else
2190b57cec5SDimitry Andric done = false;
2200b57cec5SDimitry Andric }
2210b57cec5SDimitry Andric
2220b57cec5SDimitry Andric if (done)
2230b57cec5SDimitry Andric SetPlanComplete();
2240b57cec5SDimitry Andric else
2250b57cec5SDimitry Andric m_should_stop = false;
2260b57cec5SDimitry Andric
2270b57cec5SDimitry Andric // Otherwise we've hit this breakpoint recursively. If we're the
2280b57cec5SDimitry Andric // only breakpoint here, then we do explain the stop, and we'll
2290b57cec5SDimitry Andric // continue. If not then we should let higher plans handle this
2300b57cec5SDimitry Andric // stop.
231*c9157d92SDimitry Andric if (this_site->GetNumberOfConstituents() == 1)
2320b57cec5SDimitry Andric m_explains_stop = true;
2330b57cec5SDimitry Andric else {
2340b57cec5SDimitry Andric m_should_stop = true;
2350b57cec5SDimitry Andric m_explains_stop = false;
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric return;
2380b57cec5SDimitry Andric }
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric }
2410b57cec5SDimitry Andric // If we get here we haven't hit any of our breakpoints, so let the
2420b57cec5SDimitry Andric // higher plans take care of the stop.
2430b57cec5SDimitry Andric m_explains_stop = false;
2440b57cec5SDimitry Andric return;
2450b57cec5SDimitry Andric } else if (IsUsuallyUnexplainedStopReason(reason)) {
2460b57cec5SDimitry Andric m_explains_stop = false;
2470b57cec5SDimitry Andric } else {
2480b57cec5SDimitry Andric m_explains_stop = true;
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric }
2520b57cec5SDimitry Andric
DoPlanExplainsStop(Event * event_ptr)2530b57cec5SDimitry Andric bool ThreadPlanStepUntil::DoPlanExplainsStop(Event *event_ptr) {
2540b57cec5SDimitry Andric // We don't explain signals or breakpoints (breakpoints that handle stepping
2550b57cec5SDimitry Andric // in or out will be handled by a child plan.
2560b57cec5SDimitry Andric AnalyzeStop();
2570b57cec5SDimitry Andric return m_explains_stop;
2580b57cec5SDimitry Andric }
2590b57cec5SDimitry Andric
ShouldStop(Event * event_ptr)2600b57cec5SDimitry Andric bool ThreadPlanStepUntil::ShouldStop(Event *event_ptr) {
2610b57cec5SDimitry Andric // If we've told our self in ExplainsStop that we plan to continue, then do
2620b57cec5SDimitry Andric // so here. Otherwise, as long as this thread has stopped for a reason, we
2630b57cec5SDimitry Andric // will stop.
2640b57cec5SDimitry Andric
2650b57cec5SDimitry Andric StopInfoSP stop_info_sp = GetPrivateStopInfo();
2660b57cec5SDimitry Andric if (!stop_info_sp || stop_info_sp->GetStopReason() == eStopReasonNone)
2670b57cec5SDimitry Andric return false;
2680b57cec5SDimitry Andric
2690b57cec5SDimitry Andric AnalyzeStop();
2700b57cec5SDimitry Andric return m_should_stop;
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric
StopOthers()2730b57cec5SDimitry Andric bool ThreadPlanStepUntil::StopOthers() { return m_stop_others; }
2740b57cec5SDimitry Andric
GetPlanRunState()2750b57cec5SDimitry Andric StateType ThreadPlanStepUntil::GetPlanRunState() { return eStateRunning; }
2760b57cec5SDimitry Andric
DoWillResume(StateType resume_state,bool current_plan)2770b57cec5SDimitry Andric bool ThreadPlanStepUntil::DoWillResume(StateType resume_state,
2780b57cec5SDimitry Andric bool current_plan) {
2790b57cec5SDimitry Andric if (current_plan) {
2805ffd83dbSDimitry Andric Target &target = GetTarget();
2815ffd83dbSDimitry Andric Breakpoint *return_bp = target.GetBreakpointByID(m_return_bp_id).get();
2820b57cec5SDimitry Andric if (return_bp != nullptr)
2830b57cec5SDimitry Andric return_bp->SetEnabled(true);
2840b57cec5SDimitry Andric
2850b57cec5SDimitry Andric until_collection::iterator pos, end = m_until_points.end();
2860b57cec5SDimitry Andric for (pos = m_until_points.begin(); pos != end; pos++) {
2875ffd83dbSDimitry Andric Breakpoint *until_bp = target.GetBreakpointByID((*pos).second).get();
2880b57cec5SDimitry Andric if (until_bp != nullptr)
2890b57cec5SDimitry Andric until_bp->SetEnabled(true);
2900b57cec5SDimitry Andric }
2910b57cec5SDimitry Andric }
2920b57cec5SDimitry Andric
2930b57cec5SDimitry Andric m_should_stop = true;
2940b57cec5SDimitry Andric m_ran_analyze = false;
2950b57cec5SDimitry Andric m_explains_stop = false;
2960b57cec5SDimitry Andric return true;
2970b57cec5SDimitry Andric }
2980b57cec5SDimitry Andric
WillStop()2990b57cec5SDimitry Andric bool ThreadPlanStepUntil::WillStop() {
3005ffd83dbSDimitry Andric Target &target = GetTarget();
3015ffd83dbSDimitry Andric Breakpoint *return_bp = target.GetBreakpointByID(m_return_bp_id).get();
3020b57cec5SDimitry Andric if (return_bp != nullptr)
3030b57cec5SDimitry Andric return_bp->SetEnabled(false);
3040b57cec5SDimitry Andric
3050b57cec5SDimitry Andric until_collection::iterator pos, end = m_until_points.end();
3060b57cec5SDimitry Andric for (pos = m_until_points.begin(); pos != end; pos++) {
3075ffd83dbSDimitry Andric Breakpoint *until_bp = target.GetBreakpointByID((*pos).second).get();
3080b57cec5SDimitry Andric if (until_bp != nullptr)
3090b57cec5SDimitry Andric until_bp->SetEnabled(false);
3100b57cec5SDimitry Andric }
3110b57cec5SDimitry Andric return true;
3120b57cec5SDimitry Andric }
3130b57cec5SDimitry Andric
MischiefManaged()3140b57cec5SDimitry Andric bool ThreadPlanStepUntil::MischiefManaged() {
3150b57cec5SDimitry Andric // I'm letting "PlanExplainsStop" do all the work, and just reporting that
3160b57cec5SDimitry Andric // here.
3170b57cec5SDimitry Andric bool done = false;
3180b57cec5SDimitry Andric if (IsPlanComplete()) {
31981ad6265SDimitry Andric Log *log = GetLog(LLDBLog::Step);
3209dba64beSDimitry Andric LLDB_LOGF(log, "Completed step until plan.");
3210b57cec5SDimitry Andric
3220b57cec5SDimitry Andric Clear();
3230b57cec5SDimitry Andric done = true;
3240b57cec5SDimitry Andric }
3250b57cec5SDimitry Andric if (done)
3260b57cec5SDimitry Andric ThreadPlan::MischiefManaged();
3270b57cec5SDimitry Andric
3280b57cec5SDimitry Andric return done;
3290b57cec5SDimitry Andric }
330