Skip to content

Commit

Permalink
Make ThreadPlans use TID and Process, rather than Thread *.
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D75711
  • Loading branch information
jimingham committed Apr 3, 2020
1 parent 7ae03e9 commit 3b84a5f
Show file tree
Hide file tree
Showing 18 changed files with 281 additions and 320 deletions.
24 changes: 13 additions & 11 deletions lldb/include/lldb/Target/ThreadPlan.h
Expand Up @@ -369,13 +369,11 @@ class ThreadPlan : public std::enable_shared_from_this<ThreadPlan>,
///
/// \return
/// A pointer to the thread plan's owning thread.
Thread &GetThread() { return m_thread; }
Thread &GetThread();

const Thread &GetThread() const { return m_thread; }
Target &GetTarget() { return m_process.GetTarget(); }

Target &GetTarget() { return m_thread.GetProcess()->GetTarget(); }

const Target &GetTarget() const { return m_thread.GetProcess()->GetTarget(); }
const Target &GetTarget() const { return m_process.GetTarget(); }

/// Print a description of this thread to the stream \a s.
/// \a thread.
Expand Down Expand Up @@ -464,7 +462,7 @@ class ThreadPlan : public std::enable_shared_from_this<ThreadPlan>,
// Also sets the plans to private and not master plans. A plan pushed by
// another thread plan is never either of the above.
void PushPlan(lldb::ThreadPlanSP &thread_plan_sp) {
m_thread.PushPlan(thread_plan_sp);
GetThread().PushPlan(thread_plan_sp);
thread_plan_sp->SetPrivate(false);
thread_plan_sp->SetIsMasterPlan(false);
}
Expand Down Expand Up @@ -497,7 +495,9 @@ class ThreadPlan : public std::enable_shared_from_this<ThreadPlan>,
// original stop reason so that stopping and calling a few functions won't
// lose the history of the run. This call can be implemented to get you back
// to the real stop info.
virtual lldb::StopInfoSP GetRealStopInfo() { return m_thread.GetStopInfo(); }
virtual lldb::StopInfoSP GetRealStopInfo() {
return GetThread().GetStopInfo();
}

// If the completion of the thread plan stepped out of a function, the return
// value of the function might have been captured by the thread plan
Expand Down Expand Up @@ -560,17 +560,17 @@ class ThreadPlan : public std::enable_shared_from_this<ThreadPlan>,
// This is mostly a formal requirement, it allows us to make the Thread's
// GetPreviousPlan protected, but only friend ThreadPlan to thread.

ThreadPlan *GetPreviousPlan() { return m_thread.GetPreviousPlan(this); }
ThreadPlan *GetPreviousPlan() { return GetThread().GetPreviousPlan(this); }

// This forwards the private Thread::GetPrivateStopInfo which is generally
// what ThreadPlan's need to know.

lldb::StopInfoSP GetPrivateStopInfo() {
return m_thread.GetPrivateStopInfo();
return GetThread().GetPrivateStopInfo();
}

void SetStopInfo(lldb::StopInfoSP stop_reason_sp) {
m_thread.SetStopInfo(stop_reason_sp);
GetThread().SetStopInfo(stop_reason_sp);
}

void CachePlanExplainsStop(bool does_explain) {
Expand All @@ -586,7 +586,8 @@ class ThreadPlan : public std::enable_shared_from_this<ThreadPlan>,
bool IsUsuallyUnexplainedStopReason(lldb::StopReason);

Status m_status;
Thread &m_thread;
Process &m_process;
lldb::tid_t m_tid;
Vote m_stop_vote;
Vote m_run_vote;
bool m_takes_iteration_count;
Expand All @@ -597,6 +598,7 @@ class ThreadPlan : public std::enable_shared_from_this<ThreadPlan>,
// For ThreadPlan only
static lldb::user_id_t GetNextID();

Thread *m_thread;
ThreadPlanKind m_kind;
std::string m_name;
std::recursive_mutex m_plan_complete_mutex;
Expand Down
2 changes: 2 additions & 0 deletions lldb/include/lldb/Target/ThreadPlanPython.h
Expand Up @@ -55,6 +55,8 @@ class ThreadPlanPython : public ThreadPlan {
bool DoPlanExplainsStop(Event *event_ptr) override;

lldb::StateType GetPlanRunState() override;

ScriptInterpreter *GetScriptInterpreter();

private:
std::string m_class_name;
Expand Down
Expand Up @@ -48,15 +48,14 @@ void AppleThreadPlanStepThroughObjCTrampoline::DidPush() {
// Setting up the memory space for the called function text might require
// allocations, i.e. a nested function call. This needs to be done as a
// PreResumeAction.
m_thread.GetProcess()->AddPreResumeAction(PreResumeInitializeFunctionCaller,
(void *)this);
m_process.AddPreResumeAction(PreResumeInitializeFunctionCaller, (void *)this);
}

bool AppleThreadPlanStepThroughObjCTrampoline::InitializeFunctionCaller() {
if (!m_func_sp) {
DiagnosticManager diagnostics;
m_args_addr =
m_trampoline_handler.SetupDispatchFunction(m_thread, m_input_values);
m_trampoline_handler.SetupDispatchFunction(GetThread(), m_input_values);

if (m_args_addr == LLDB_INVALID_ADDRESS) {
return false;
Expand All @@ -68,7 +67,7 @@ bool AppleThreadPlanStepThroughObjCTrampoline::InitializeFunctionCaller() {
options.SetUnwindOnError(true);
options.SetIgnoreBreakpoints(true);
options.SetStopOthers(m_stop_others);
m_thread.CalculateExecutionContext(exc_ctx);
GetThread().CalculateExecutionContext(exc_ctx);
m_func_sp = m_impl_function->GetThreadPlanToCallFunction(
exc_ctx, m_args_addr, options, diagnostics);
m_func_sp->SetOkayToDiscard(true);
Expand Down Expand Up @@ -132,7 +131,7 @@ bool AppleThreadPlanStepThroughObjCTrampoline::ShouldStop(Event *event_ptr) {
if (!m_run_to_sp) {
Value target_addr_value;
ExecutionContext exc_ctx;
m_thread.CalculateExecutionContext(exc_ctx);
GetThread().CalculateExecutionContext(exc_ctx);
m_impl_function->FetchFunctionResults(exc_ctx, m_args_addr,
target_addr_value);
m_impl_function->DeallocateFunctionResults(exc_ctx, m_args_addr);
Expand All @@ -151,13 +150,13 @@ bool AppleThreadPlanStepThroughObjCTrampoline::ShouldStop(Event *event_ptr) {
", stopping.",
target_addr);

SymbolContext sc = m_thread.GetStackFrameAtIndex(0)->GetSymbolContext(
SymbolContext sc = GetThread().GetStackFrameAtIndex(0)->GetSymbolContext(
eSymbolContextEverything);
Status status;
const bool abort_other_plans = false;
const bool first_insn = true;
const uint32_t frame_idx = 0;
m_run_to_sp = m_thread.QueueThreadPlanForStepOutNoShouldStop(
m_run_to_sp = GetThread().QueueThreadPlanForStepOutNoShouldStop(
abort_other_plans, &sc, first_insn, m_stop_others, eVoteNoOpinion,
eVoteNoOpinion, frame_idx, status);
if (m_run_to_sp && status.Success())
Expand All @@ -180,10 +179,10 @@ bool AppleThreadPlanStepThroughObjCTrampoline::ShouldStop(Event *event_ptr) {
// Extract the target address from the value:

m_run_to_sp = std::make_shared<ThreadPlanRunToAddress>(
m_thread, target_so_addr, m_stop_others);
GetThread(), target_so_addr, m_stop_others);
PushPlan(m_run_to_sp);
return false;
} else if (m_thread.IsThreadPlanDone(m_run_to_sp.get())) {
} else if (GetThread().IsThreadPlanDone(m_run_to_sp.get())) {
// Third stage, work the run to target plan.
SetPlanComplete();
return true;
Expand Down
48 changes: 26 additions & 22 deletions lldb/source/Target/ThreadPlan.cpp
Expand Up @@ -21,9 +21,10 @@ using namespace lldb_private;
// ThreadPlan constructor
ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread,
Vote stop_vote, Vote run_vote)
: m_thread(thread), m_stop_vote(stop_vote), m_run_vote(run_vote),
: m_process(*thread.GetProcess().get()), m_tid(thread.GetID()),
m_stop_vote(stop_vote), m_run_vote(run_vote),
m_takes_iteration_count(false), m_could_not_resolve_hw_bp(false),
m_kind(kind), m_name(name), m_plan_complete_mutex(),
m_kind(kind), m_thread(&thread), m_name(name), m_plan_complete_mutex(),
m_cached_plan_explains_stop(eLazyBoolCalculate), m_plan_complete(false),
m_plan_private(false), m_okay_to_discard(true), m_is_master_plan(false),
m_plan_succeeded(true) {
Expand All @@ -33,6 +34,15 @@ ThreadPlan::ThreadPlan(ThreadPlanKind kind, const char *name, Thread &thread,
// Destructor
ThreadPlan::~ThreadPlan() = default;

Thread &ThreadPlan::GetThread() {
if (m_thread)
return *m_thread;

ThreadSP thread_sp = m_process.GetThreadList().FindThreadByID(m_tid);
m_thread = thread_sp.get();
return *m_thread;
}

bool ThreadPlan::PlanExplainsStop(Event *event_ptr) {
if (m_cached_plan_explains_stop == eLazyBoolCalculate) {
bool actual_value = DoPlanExplainsStop(event_ptr);
Expand Down Expand Up @@ -103,7 +113,7 @@ bool ThreadPlan::WillResume(StateType resume_state, bool current_plan) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));

if (log) {
RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
RegisterContext *reg_ctx = GetThread().GetRegisterContext().get();
assert(reg_ctx);
addr_t pc = reg_ctx->GetPC();
addr_t sp = reg_ctx->GetSP();
Expand All @@ -113,8 +123,8 @@ bool ThreadPlan::WillResume(StateType resume_state, bool current_plan) {
"%s Thread #%u (0x%p): tid = 0x%4.4" PRIx64 ", pc = 0x%8.8" PRIx64
", sp = 0x%8.8" PRIx64 ", fp = 0x%8.8" PRIx64 ", "
"plan = '%s', state = %s, stop others = %d",
__FUNCTION__, m_thread.GetIndexID(), static_cast<void *>(&m_thread),
m_thread.GetID(), static_cast<uint64_t>(pc),
__FUNCTION__, GetThread().GetIndexID(),
static_cast<void *>(&GetThread()), m_tid, static_cast<uint64_t>(pc),
static_cast<uint64_t>(sp), static_cast<uint64_t>(fp), m_name.c_str(),
StateAsCString(resume_state), StopOthers());
}
Expand Down Expand Up @@ -174,14 +184,13 @@ bool ThreadPlanNull::ValidatePlan(Stream *error) {
fprintf(stderr,
"error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
", ptid = 0x%" PRIx64 ")",
LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
#else
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
if (log)
log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
", ptid = 0x%" PRIx64 ")",
LLVM_PRETTY_FUNCTION, m_thread.GetID(),
m_thread.GetProtocolID());
LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
#endif
return true;
}
Expand All @@ -191,14 +200,13 @@ bool ThreadPlanNull::ShouldStop(Event *event_ptr) {
fprintf(stderr,
"error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
", ptid = 0x%" PRIx64 ")",
LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
#else
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
if (log)
log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
", ptid = 0x%" PRIx64 ")",
LLVM_PRETTY_FUNCTION, m_thread.GetID(),
m_thread.GetProtocolID());
LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
#endif
return true;
}
Expand All @@ -208,14 +216,13 @@ bool ThreadPlanNull::WillStop() {
fprintf(stderr,
"error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
", ptid = 0x%" PRIx64 ")",
LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
#else
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
if (log)
log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
", ptid = 0x%" PRIx64 ")",
LLVM_PRETTY_FUNCTION, m_thread.GetID(),
m_thread.GetProtocolID());
LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
#endif
return true;
}
Expand All @@ -231,8 +238,7 @@ bool ThreadPlanNull::DoPlanExplainsStop(Event *event_ptr) {
if (log)
log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
", ptid = 0x%" PRIx64 ")",
LLVM_PRETTY_FUNCTION, m_thread.GetID(),
m_thread.GetProtocolID());
LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
#endif
return true;
}
Expand All @@ -244,14 +250,13 @@ bool ThreadPlanNull::MischiefManaged() {
fprintf(stderr,
"error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
", ptid = 0x%" PRIx64 ")",
LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
#else
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
if (log)
log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
", ptid = 0x%" PRIx64 ")",
LLVM_PRETTY_FUNCTION, m_thread.GetID(),
m_thread.GetProtocolID());
LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
#endif
return false;
}
Expand All @@ -262,14 +267,13 @@ lldb::StateType ThreadPlanNull::GetPlanRunState() {
fprintf(stderr,
"error: %s called on thread that has been destroyed (tid = 0x%" PRIx64
", ptid = 0x%" PRIx64 ")",
LLVM_PRETTY_FUNCTION, m_thread.GetID(), m_thread.GetProtocolID());
LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
#else
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
if (log)
log->Error("%s called on thread that has been destroyed (tid = 0x%" PRIx64
", ptid = 0x%" PRIx64 ")",
LLVM_PRETTY_FUNCTION, m_thread.GetID(),
m_thread.GetProtocolID());
LLVM_PRETTY_FUNCTION, m_tid, GetThread().GetProtocolID());
#endif
return eStateRunning;
}
26 changes: 13 additions & 13 deletions lldb/source/Target/ThreadPlanBase.cpp
Expand Up @@ -34,11 +34,11 @@ ThreadPlanBase::ThreadPlanBase(Thread &thread)
#define THREAD_PLAN_USE_ASSEMBLY_TRACER 1

#ifdef THREAD_PLAN_USE_ASSEMBLY_TRACER
ThreadPlanTracerSP new_tracer_sp(new ThreadPlanAssemblyTracer(m_thread));
ThreadPlanTracerSP new_tracer_sp(new ThreadPlanAssemblyTracer(thread));
#else
ThreadPlanTracerSP new_tracer_sp(new ThreadPlanTracer(m_thread));
#endif
new_tracer_sp->EnableTracing(m_thread.GetTraceEnabledState());
new_tracer_sp->EnableTracing(thread.GetTraceEnabledState());
SetThreadPlanTracer(new_tracer_sp);
SetIsMasterPlan(true);
}
Expand All @@ -58,7 +58,7 @@ bool ThreadPlanBase::DoPlanExplainsStop(Event *event_ptr) {
}

Vote ThreadPlanBase::ShouldReportStop(Event *event_ptr) {
StopInfoSP stop_info_sp = m_thread.GetStopInfo();
StopInfoSP stop_info_sp = GetThread().GetStopInfo();
if (stop_info_sp) {
bool should_notify = stop_info_sp->ShouldNotify(event_ptr);
if (should_notify)
Expand Down Expand Up @@ -96,8 +96,8 @@ bool ThreadPlanBase::ShouldStop(Event *event_ptr) {
log,
"Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
" (breakpoint hit.)",
m_thread.GetID());
m_thread.DiscardThreadPlans(false);
m_tid);
GetThread().DiscardThreadPlans(false);
return true;
}
// If we aren't going to stop at this breakpoint, and it is internal,
Expand Down Expand Up @@ -125,9 +125,9 @@ bool ThreadPlanBase::ShouldStop(Event *event_ptr) {
LLDB_LOGF(
log,
"Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
" (exception: %s)",
m_thread.GetID(), stop_info_sp->GetDescription());
m_thread.DiscardThreadPlans(false);
" (exception: %s)",
m_tid, stop_info_sp->GetDescription());
GetThread().DiscardThreadPlans(false);
return true;

case eStopReasonExec:
Expand All @@ -138,8 +138,8 @@ bool ThreadPlanBase::ShouldStop(Event *event_ptr) {
log,
"Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
" (exec.)",
m_thread.GetID());
m_thread.DiscardThreadPlans(false);
m_tid);
GetThread().DiscardThreadPlans(false);
return true;

case eStopReasonThreadExiting:
Expand All @@ -148,9 +148,9 @@ bool ThreadPlanBase::ShouldStop(Event *event_ptr) {
LLDB_LOGF(
log,
"Base plan discarding thread plans for thread tid = 0x%4.4" PRIx64
" (signal: %s)",
m_thread.GetID(), stop_info_sp->GetDescription());
m_thread.DiscardThreadPlans(false);
" (signal: %s)",
m_tid, stop_info_sp->GetDescription());
GetThread().DiscardThreadPlans(false);
return true;
} else {
// We're not going to stop, but while we are here, let's figure out
Expand Down

0 comments on commit 3b84a5f

Please sign in to comment.