ThreadPlanStepInRange.cpp [plain text]
#include "lldb/Target/ThreadPlanStepInRange.h"
#include "lldb/lldb-private-log.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/Stream.h"
#include "lldb/Symbol/Symbol.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Target/ThreadPlanStepOut.h"
#include "lldb/Target/ThreadPlanStepThrough.h"
#include "lldb/Core/RegularExpression.h"
using namespace lldb;
using namespace lldb_private;
uint32_t ThreadPlanStepInRange::s_default_flag_values = ThreadPlanShouldStopHere::eAvoidNoDebug;
ThreadPlanStepInRange::ThreadPlanStepInRange
(
Thread &thread,
const AddressRange &range,
const SymbolContext &addr_context,
lldb::RunMode stop_others
) :
ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others),
ThreadPlanShouldStopHere (this, ThreadPlanStepInRange::DefaultShouldStopHereCallback, NULL),
m_step_past_prologue (true),
m_virtual_step (false)
{
SetFlagsToDefault ();
}
ThreadPlanStepInRange::ThreadPlanStepInRange
(
Thread &thread,
const AddressRange &range,
const SymbolContext &addr_context,
const char *step_into_target,
lldb::RunMode stop_others
) :
ThreadPlanStepRange (ThreadPlan::eKindStepInRange, "Step Range stepping in", thread, range, addr_context, stop_others),
ThreadPlanShouldStopHere (this, ThreadPlanStepInRange::DefaultShouldStopHereCallback, NULL),
m_step_past_prologue (true),
m_virtual_step (false),
m_step_into_target (step_into_target)
{
SetFlagsToDefault ();
}
ThreadPlanStepInRange::~ThreadPlanStepInRange ()
{
}
void
ThreadPlanStepInRange::GetDescription (Stream *s, lldb::DescriptionLevel level)
{
if (level == lldb::eDescriptionLevelBrief)
s->Printf("step in");
else
{
s->Printf ("Stepping through range (stepping into functions): ");
DumpRanges(s);
const char *step_into_target = m_step_into_target.AsCString();
if (step_into_target && step_into_target[0] != '\0')
s->Printf (" targeting %s.", m_step_into_target.AsCString());
else
s->PutChar('.');
}
}
bool
ThreadPlanStepInRange::ShouldStop (Event *event_ptr)
{
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
if (log)
{
StreamString s;
s.Address (m_thread.GetRegisterContext()->GetPC(),
m_thread.CalculateTarget()->GetArchitecture().GetAddressByteSize());
log->Printf("ThreadPlanStepInRange reached %s.", s.GetData());
}
if (IsPlanComplete())
return true;
m_no_more_plans = false;
if (m_sub_plan_sp && m_sub_plan_sp->IsPlanComplete())
{
if (!m_sub_plan_sp->PlanSucceeded())
{
SetPlanComplete();
m_no_more_plans = true;
return true;
}
else
m_sub_plan_sp.reset();
}
if (m_virtual_step)
{
m_sub_plan_sp = InvokeShouldStopHereCallback();
}
else
{
bool stop_others;
if (m_stop_others == lldb::eOnlyThisThread)
stop_others = true;
else
stop_others = false;
FrameComparison frame_order = CompareCurrentFrameToStartFrame();
if (frame_order == eFrameCompareOlder)
{
m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
if (!m_sub_plan_sp)
return true;
else if (log)
{
log->Printf("Thought I stepped out, but in fact arrived at a trampoline.");
}
}
else if (frame_order == eFrameCompareEqual && InSymbol())
{
if (InRange())
{
SetNextBranchBreakpoint();
return false;
}
SetPlanComplete();
m_no_more_plans = true;
return true;
}
ClearNextBranchBreakpoint();
if (!m_sub_plan_sp)
m_sub_plan_sp = m_thread.QueueThreadPlanForStepThrough (m_stack_id, false, stop_others);
if (log)
{
if (m_sub_plan_sp)
log->Printf ("Found a step through plan: %s", m_sub_plan_sp->GetName());
else
log->Printf ("No step through plan found.");
}
if (!m_sub_plan_sp && frame_order == eFrameCompareYounger)
m_sub_plan_sp = InvokeShouldStopHereCallback();
if (!m_sub_plan_sp && frame_order == eFrameCompareYounger && m_step_past_prologue)
{
lldb::StackFrameSP curr_frame = m_thread.GetStackFrameAtIndex(0);
if (curr_frame)
{
size_t bytes_to_skip = 0;
lldb::addr_t curr_addr = m_thread.GetRegisterContext()->GetPC();
Address func_start_address;
SymbolContext sc = curr_frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextSymbol);
if (sc.function)
{
func_start_address = sc.function->GetAddressRange().GetBaseAddress();
if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get()))
bytes_to_skip = sc.function->GetPrologueByteSize();
}
else if (sc.symbol)
{
func_start_address = sc.symbol->GetAddress();
if (curr_addr == func_start_address.GetLoadAddress(m_thread.CalculateTarget().get()))
bytes_to_skip = sc.symbol->GetPrologueByteSize();
}
if (bytes_to_skip != 0)
{
func_start_address.Slide (bytes_to_skip);
log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP);
if (log)
log->Printf ("Pushing past prologue ");
m_sub_plan_sp = m_thread.QueueThreadPlanForRunToAddress(false, func_start_address,true);
}
}
}
}
if (!m_sub_plan_sp)
{
m_no_more_plans = true;
SetPlanComplete();
return true;
}
else
{
m_no_more_plans = false;
return false;
}
}
void
ThreadPlanStepInRange::SetFlagsToDefault ()
{
GetFlags().Set(ThreadPlanStepInRange::s_default_flag_values);
}
void
ThreadPlanStepInRange::SetAvoidRegexp(const char *name)
{
if (m_avoid_regexp_ap.get() == NULL)
m_avoid_regexp_ap.reset (new RegularExpression(name));
m_avoid_regexp_ap->Compile (name);
}
void
ThreadPlanStepInRange::SetDefaultFlagValue (uint32_t new_value)
{
ThreadPlanStepInRange::s_default_flag_values = new_value;
}
bool
ThreadPlanStepInRange::FrameMatchesAvoidRegexp ()
{
StackFrame *frame = GetThread().GetStackFrameAtIndex(0).get();
const RegularExpression *avoid_regexp_to_use = m_avoid_regexp_ap.get();
if (avoid_regexp_to_use == NULL)
avoid_regexp_to_use = GetThread().GetSymbolsToAvoidRegexp();
if (avoid_regexp_to_use != NULL)
{
SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock|eSymbolContextSymbol);
if (sc.symbol != NULL)
{
const char *frame_function_name = sc.GetFunctionName().GetCString();
if (frame_function_name)
{
size_t num_matches = 0;
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
if (log)
num_matches = 1;
RegularExpression::Match regex_match(num_matches);
bool return_value = avoid_regexp_to_use->Execute(frame_function_name, ®ex_match);
if (return_value)
{
if (log)
{
std::string match;
regex_match.GetMatchAtIndex(frame_function_name,0, match);
log->Printf ("Stepping out of function \"%s\" because it matches the avoid regexp \"%s\" - match substring: \"%s\".",
frame_function_name,
avoid_regexp_to_use->GetText(),
match.c_str());
}
}
return return_value;
}
}
}
return false;
}
ThreadPlanSP
ThreadPlanStepInRange::DefaultShouldStopHereCallback (ThreadPlan *current_plan, Flags &flags, void *baton)
{
bool should_step_out = false;
StackFrame *frame = current_plan->GetThread().GetStackFrameAtIndex(0).get();
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
if (flags.Test(eAvoidNoDebug))
{
if (!frame->HasDebugInformation())
{
if (log)
log->Printf ("Stepping out of frame with no debug info");
should_step_out = true;
}
}
if (current_plan->GetKind() == eKindStepInRange)
{
ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan);
if (step_in_range_plan->m_step_into_target)
{
SymbolContext sc = frame->GetSymbolContext(eSymbolContextFunction|eSymbolContextBlock|eSymbolContextSymbol);
if (sc.symbol != NULL)
{
if (step_in_range_plan->m_step_into_target == sc.GetFunctionName())
{
should_step_out = false;
}
else
{
const char *target_name = step_in_range_plan->m_step_into_target.AsCString();
const char *function_name = sc.GetFunctionName().AsCString();
if (function_name == NULL)
should_step_out = true;
else if (strstr (function_name, target_name) == NULL)
should_step_out = true;
}
if (log && should_step_out)
log->Printf("Stepping out of frame %s which did not match step into target %s.",
sc.GetFunctionName().AsCString(),
step_in_range_plan->m_step_into_target.AsCString());
}
}
if (!should_step_out)
{
ThreadPlanStepInRange *step_in_range_plan = static_cast<ThreadPlanStepInRange *> (current_plan);
should_step_out = step_in_range_plan->FrameMatchesAvoidRegexp ();
}
}
if (should_step_out)
{
const bool stop_others = false;
return current_plan->GetThread().QueueThreadPlanForStepOut (false,
NULL,
true,
stop_others,
eVoteNo,
eVoteNoOpinion,
0); }
return ThreadPlanSP();
}
bool
ThreadPlanStepInRange::DoPlanExplainsStop (Event *event_ptr)
{
bool return_value;
if (m_virtual_step)
{
return_value = true;
}
else
{
StopInfoSP stop_info_sp = GetPrivateStopInfo ();
if (stop_info_sp)
{
StopReason reason = stop_info_sp->GetStopReason();
switch (reason)
{
case eStopReasonBreakpoint:
if (NextRangeBreakpointExplainsStop(stop_info_sp))
{
return_value = true;
break;
}
case eStopReasonWatchpoint:
case eStopReasonSignal:
case eStopReasonException:
case eStopReasonExec:
case eStopReasonThreadExiting:
{
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
if (log)
log->PutCString ("ThreadPlanStepInRange got asked if it explains the stop for some reason other than step.");
}
return_value = false;
break;
default:
return_value = true;
break;
}
}
else
return_value = true;
}
return return_value;
}
bool
ThreadPlanStepInRange::DoWillResume (lldb::StateType resume_state, bool current_plan)
{
if (resume_state == eStateStepping && current_plan)
{
bool step_without_resume = m_thread.DecrementCurrentInlinedDepth();
if (step_without_resume)
{
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
if (log)
log->Printf ("ThreadPlanStepInRange::DoWillResume: returning false, inline_depth: %d",
m_thread.GetCurrentInlinedDepth());
SetStopInfo(StopInfo::CreateStopReasonToTrace(m_thread));
m_virtual_step = true;
}
return !step_without_resume;
}
return true;
}
bool
ThreadPlanStepInRange::IsVirtualStep()
{
return m_virtual_step;
}