ThreadPlanStepUntil.cpp [plain text]
#include "lldb/Target/ThreadPlanStepUntil.h"
#include "lldb/Breakpoint/Breakpoint.h"
#include "lldb/lldb-private-log.h"
#include "lldb/Core/Log.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/StopInfo.h"
#include "lldb/Target/Target.h"
using namespace lldb;
using namespace lldb_private;
ThreadPlanStepUntil::ThreadPlanStepUntil
(
Thread &thread,
lldb::addr_t *address_list,
size_t num_addresses,
bool stop_others,
uint32_t frame_idx
) :
ThreadPlan (ThreadPlan::eKindStepUntil, "Step until", thread, eVoteNoOpinion, eVoteNoOpinion),
m_stack_depth (0),
m_step_from_insn (LLDB_INVALID_ADDRESS),
m_return_bp_id(LLDB_INVALID_BREAK_ID),
m_return_addr (LLDB_INVALID_ADDRESS),
m_stepped_out(false),
m_should_stop(false),
m_ran_analyze (false),
m_explains_stop(false),
m_until_points(),
m_stop_others (stop_others)
{
SetOkayToDiscard(true);
Target &target = m_thread.GetProcess().GetTarget();
StackFrameSP frame_sp (m_thread.GetStackFrameAtIndex (frame_idx));
if (frame_sp)
{
m_step_from_insn = frame_sp->GetStackID().GetPC();
lldb::user_id_t thread_id = m_thread.GetID();
StackFrameSP return_frame_sp (m_thread.GetStackFrameAtIndex(frame_idx + 1));
if (return_frame_sp)
{
m_return_addr = return_frame_sp->GetStackID().GetPC();
Breakpoint *return_bp = target.CreateBreakpoint (m_return_addr, true).get();
if (return_bp != NULL)
{
return_bp->SetThreadID(thread_id);
m_return_bp_id = return_bp->GetID();
}
}
m_stack_depth = m_thread.GetStackFrameCount() - frame_idx;
for (int i = 0; i < num_addresses; i++)
{
Breakpoint *until_bp = target.CreateBreakpoint (address_list[i], true).get();
if (until_bp != NULL)
{
until_bp->SetThreadID(thread_id);
m_until_points[address_list[i]] = until_bp->GetID();
}
else
{
m_until_points[address_list[i]] = LLDB_INVALID_BREAK_ID;
}
}
}
}
ThreadPlanStepUntil::~ThreadPlanStepUntil ()
{
Clear();
}
void
ThreadPlanStepUntil::Clear()
{
Target &target = m_thread.GetProcess().GetTarget();
if (m_return_bp_id != LLDB_INVALID_BREAK_ID)
{
target.RemoveBreakpointByID(m_return_bp_id);
m_return_bp_id = LLDB_INVALID_BREAK_ID;
}
until_collection::iterator pos, end = m_until_points.end();
for (pos = m_until_points.begin(); pos != end; pos++)
{
target.RemoveBreakpointByID((*pos).second);
}
m_until_points.clear();
}
void
ThreadPlanStepUntil::GetDescription (Stream *s, lldb::DescriptionLevel level)
{
if (level == lldb::eDescriptionLevelBrief)
{
s->Printf ("step until");
if (m_stepped_out)
s->Printf (" - stepped out");
}
else
{
if (m_until_points.size() == 1)
s->Printf ("Stepping from address 0x%llx until we reach 0x%llx using breakpoint %d",
(uint64_t)m_step_from_insn,
(uint64_t) (*m_until_points.begin()).first,
(*m_until_points.begin()).second);
else
{
until_collection::iterator pos, end = m_until_points.end();
s->Printf ("Stepping from address 0x%llx until we reach one of:",
(uint64_t)m_step_from_insn);
for (pos = m_until_points.begin(); pos != end; pos++)
{
s->Printf ("\n\t0x%llx (bp: %d)", (uint64_t) (*pos).first, (*pos).second);
}
}
s->Printf(" stepped out address is 0x%lx.", (uint64_t) m_return_addr);
}
}
bool
ThreadPlanStepUntil::ValidatePlan (Stream *error)
{
if (m_return_bp_id == LLDB_INVALID_BREAK_ID)
return false;
else
{
until_collection::iterator pos, end = m_until_points.end();
for (pos = m_until_points.begin(); pos != end; pos++)
{
if (!LLDB_BREAK_ID_IS_VALID ((*pos).second))
return false;
}
return true;
}
}
void
ThreadPlanStepUntil::AnalyzeStop()
{
if (m_ran_analyze)
return;
StopInfoSP stop_info_sp = GetPrivateStopReason();
m_should_stop = true;
m_explains_stop = false;
if (stop_info_sp)
{
StopReason reason = stop_info_sp->GetStopReason();
switch (reason)
{
case eStopReasonBreakpoint:
{
BreakpointSiteSP this_site = m_thread.GetProcess().GetBreakpointSiteList().FindByID (stop_info_sp->GetValue());
if (!this_site)
{
m_explains_stop = false;
return;
}
if (this_site->IsBreakpointAtThisSite (m_return_bp_id))
{
if (m_stack_depth > m_thread.GetStackFrameCount())
{
m_stepped_out = true;
SetPlanComplete();
}
else
m_should_stop = false;
if (this_site->GetNumberOfOwners() == 1)
m_explains_stop = true;
else
m_explains_stop = false;
return;
}
else
{
until_collection::iterator pos, end = m_until_points.end();
for (pos = m_until_points.begin(); pos != end; pos++)
{
if (this_site->IsBreakpointAtThisSite ((*pos).second))
{
if (m_stack_depth == m_thread.GetStackFrameCount())
SetPlanComplete();
else
m_should_stop = false;
if (this_site->GetNumberOfOwners() == 1)
m_explains_stop = true;
else
{
m_should_stop = true;
m_explains_stop = false;
}
return;
}
}
}
m_explains_stop = false;
return;
}
case eStopReasonWatchpoint:
case eStopReasonSignal:
case eStopReasonException:
m_explains_stop = false;
break;
default:
m_explains_stop = true;
break;
}
}
}
bool
ThreadPlanStepUntil::PlanExplainsStop ()
{
AnalyzeStop();
return m_explains_stop;
}
bool
ThreadPlanStepUntil::ShouldStop (Event *event_ptr)
{
StopInfoSP stop_info_sp = GetPrivateStopReason();
if (stop_info_sp == NULL || stop_info_sp->GetStopReason() == eStopReasonNone)
return false;
AnalyzeStop();
return m_should_stop;
}
bool
ThreadPlanStepUntil::StopOthers ()
{
return m_stop_others;
}
StateType
ThreadPlanStepUntil::GetPlanRunState ()
{
return eStateRunning;
}
bool
ThreadPlanStepUntil::WillResume (StateType resume_state, bool current_plan)
{
ThreadPlan::WillResume (resume_state, current_plan);
if (current_plan)
{
Target &target = m_thread.GetProcess().GetTarget();
Breakpoint *return_bp = target.GetBreakpointByID(m_return_bp_id).get();
if (return_bp != NULL)
return_bp->SetEnabled (true);
until_collection::iterator pos, end = m_until_points.end();
for (pos = m_until_points.begin(); pos != end; pos++)
{
Breakpoint *until_bp = target.GetBreakpointByID((*pos).second).get();
if (until_bp != NULL)
until_bp->SetEnabled (true);
}
}
m_should_stop = true;
m_ran_analyze = false;
m_explains_stop = false;
return true;
}
bool
ThreadPlanStepUntil::WillStop ()
{
Target &target = m_thread.GetProcess().GetTarget();
Breakpoint *return_bp = target.GetBreakpointByID(m_return_bp_id).get();
if (return_bp != NULL)
return_bp->SetEnabled (false);
until_collection::iterator pos, end = m_until_points.end();
for (pos = m_until_points.begin(); pos != end; pos++)
{
Breakpoint *until_bp = target.GetBreakpointByID((*pos).second).get();
if (until_bp != NULL)
until_bp->SetEnabled (false);
}
return true;
}
bool
ThreadPlanStepUntil::MischiefManaged ()
{
bool done = false;
if (IsPlanComplete())
{
LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
if (log)
log->Printf("Completed step until plan.");
Clear();
done = true;
}
if (done)
ThreadPlan::MischiefManaged ();
return done;
}