Skip to content

Commit 2976240

Browse files
committed
[lldb] Use ThreadPlanSP instead of raw ThreadPlan* (NFC)
I'm investigating a lifetime issue and I noticed raw ThreadPlan pointers being passed around. Although this may be safe, it's really hard to prove and more fundamentally, defeats the purpose of using smart pointers in the first place.
1 parent 3641448 commit 2976240

File tree

6 files changed

+49
-54
lines changed

6 files changed

+49
-54
lines changed

lldb/include/lldb/Target/Thread.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ class Thread : public std::enable_shared_from_this<Thread>,
10171017
///
10181018
/// \return
10191019
/// A pointer to the next executed plan.
1020-
ThreadPlan *GetCurrentPlan() const;
1020+
lldb::ThreadPlanSP GetCurrentPlan() const;
10211021

10221022
/// Unwinds the thread stack for the innermost expression plan currently
10231023
/// on the thread plan stack.
@@ -1311,7 +1311,7 @@ class Thread : public std::enable_shared_from_this<Thread>,
13111311

13121312
void DiscardPlan();
13131313

1314-
ThreadPlan *GetPreviousPlan(ThreadPlan *plan) const;
1314+
lldb::ThreadPlanSP GetPreviousPlan(ThreadPlan *plan) const;
13151315

13161316
virtual Unwind &GetUnwinder();
13171317

lldb/include/lldb/Target/ThreadPlan.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,9 @@ class ThreadPlan : public std::enable_shared_from_this<ThreadPlan>,
536536
// This is mostly a formal requirement, it allows us to make the Thread's
537537
// GetPreviousPlan protected, but only friend ThreadPlan to thread.
538538

539-
ThreadPlan *GetPreviousPlan() { return GetThread().GetPreviousPlan(this); }
539+
lldb::ThreadPlanSP GetPreviousPlan() {
540+
return GetThread().GetPreviousPlan(this);
541+
}
540542

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

lldb/include/lldb/Target/ThreadPlanStack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class ThreadPlanStack {
8585

8686
bool WasPlanDiscarded(ThreadPlan *plan) const;
8787

88-
ThreadPlan *GetPreviousPlan(ThreadPlan *current_plan) const;
88+
lldb::ThreadPlanSP GetPreviousPlan(ThreadPlan *current_plan) const;
8989

9090
ThreadPlan *GetInnermostExpression() const;
9191

lldb/source/Target/Thread.cpp

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -609,15 +609,10 @@ std::string Thread::GetStopDescriptionRaw() {
609609
}
610610

611611
void Thread::WillStop() {
612-
ThreadPlan *current_plan = GetCurrentPlan();
613-
614612
// FIXME: I may decide to disallow threads with no plans. In which
615613
// case this should go to an assert.
616-
617-
if (!current_plan)
618-
return;
619-
620-
current_plan->WillStop();
614+
if (ThreadPlanSP current_plan = GetCurrentPlan())
615+
current_plan->WillStop();
621616
}
622617

623618
bool Thread::SetupToStepOverBreakpointIfNeeded(RunDirection direction) {
@@ -652,12 +647,12 @@ bool Thread::SetupToStepOverBreakpointIfNeeded(RunDirection direction) {
652647
// Note, don't assume there's a ThreadPlanStepOverBreakpoint, the
653648
// target may not require anything special to step over a breakpoint.
654649

655-
ThreadPlan *cur_plan = GetCurrentPlan();
650+
ThreadPlanSP cur_plan_sp = GetCurrentPlan();
656651

657652
bool push_step_over_bp_plan = false;
658-
if (cur_plan->GetKind() == ThreadPlan::eKindStepOverBreakpoint) {
653+
if (cur_plan_sp->GetKind() == ThreadPlan::eKindStepOverBreakpoint) {
659654
ThreadPlanStepOverBreakpoint *bp_plan =
660-
(ThreadPlanStepOverBreakpoint *)cur_plan;
655+
(ThreadPlanStepOverBreakpoint *)cur_plan_sp.get();
661656
if (bp_plan->GetBreakpointLoadAddress() != thread_pc)
662657
push_step_over_bp_plan = true;
663658
} else
@@ -720,12 +715,12 @@ bool Thread::ShouldResume(StateType resume_state) {
720715
// runs.
721716

722717
bool need_to_resume = false;
723-
ThreadPlan *plan_ptr = GetCurrentPlan();
724-
if (plan_ptr) {
725-
need_to_resume = plan_ptr->WillResume(resume_state, true);
718+
ThreadPlanSP plan_sp = GetCurrentPlan();
719+
if (plan_sp) {
720+
need_to_resume = plan_sp->WillResume(resume_state, true);
726721

727-
while ((plan_ptr = GetPreviousPlan(plan_ptr)) != nullptr) {
728-
plan_ptr->WillResume(resume_state, false);
722+
while ((plan_sp = GetPreviousPlan(plan_sp.get()))) {
723+
plan_sp->WillResume(resume_state, false);
729724
}
730725

731726
// If the WillResume for the plan says we are faking a resume, then it will
@@ -755,7 +750,7 @@ void Thread::DidResume() {
755750
void Thread::DidStop() { SetState(eStateStopped); }
756751

757752
bool Thread::ShouldStop(Event *event_ptr) {
758-
ThreadPlan *current_plan = GetCurrentPlan();
753+
ThreadPlanSP current_plan = GetCurrentPlan();
759754

760755
bool should_stop = true;
761756

@@ -854,34 +849,34 @@ bool Thread::ShouldStop(Event *event_ptr) {
854849

855850
// If the current plan doesn't explain the stop, then find one that does
856851
// and let it handle the situation.
857-
ThreadPlan *plan_ptr = current_plan;
858-
while ((plan_ptr = GetPreviousPlan(plan_ptr)) != nullptr) {
859-
if (plan_ptr->PlanExplainsStop(event_ptr)) {
860-
LLDB_LOGF(log, "Plan %s explains stop.", plan_ptr->GetName());
852+
ThreadPlanSP plan_sp = current_plan;
853+
while ((plan_sp = GetPreviousPlan(plan_sp.get())) != nullptr) {
854+
if (plan_sp->PlanExplainsStop(event_ptr)) {
855+
LLDB_LOGF(log, "Plan %s explains stop.", plan_sp->GetName());
861856

862-
should_stop = plan_ptr->ShouldStop(event_ptr);
857+
should_stop = plan_sp->ShouldStop(event_ptr);
863858

864859
// plan_ptr explains the stop, next check whether plan_ptr is done,
865860
// if so, then we should take it and all the plans below it off the
866861
// stack.
867862

868-
if (plan_ptr->MischiefManaged()) {
863+
if (plan_sp->MischiefManaged()) {
869864
// We're going to pop the plans up to and including the plan that
870865
// explains the stop.
871-
ThreadPlan *prev_plan_ptr = GetPreviousPlan(plan_ptr);
866+
ThreadPlanSP prev_plan_sp = GetPreviousPlan(plan_sp.get());
872867

873868
do {
874869
if (should_stop)
875870
current_plan->WillStop();
876871
PopPlan();
877-
} while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
872+
} while ((current_plan = GetCurrentPlan()) != prev_plan_sp);
878873
// Now, if the responsible plan was not "Okay to discard" then
879874
// we're done, otherwise we forward this to the next plan in the
880875
// stack below.
881876
done_processing_current_plan =
882-
(plan_ptr->IsControllingPlan() && !plan_ptr->OkayToDiscard());
877+
(plan_sp->IsControllingPlan() && !plan_sp->OkayToDiscard());
883878
} else {
884-
bool should_force_run = plan_ptr->ShouldRunBeforePublicStop();
879+
bool should_force_run = plan_sp->ShouldRunBeforePublicStop();
885880
if (should_force_run) {
886881
SetShouldRunBeforePublicStop(true);
887882
should_stop = false;
@@ -952,14 +947,14 @@ bool Thread::ShouldStop(Event *event_ptr) {
952947
// original plan on the stack, This code clears stale plans off the stack.
953948

954949
if (should_stop) {
955-
ThreadPlan *plan_ptr = GetCurrentPlan();
950+
ThreadPlanSP plan_sp = GetCurrentPlan();
956951

957952
// Discard the stale plans and all plans below them in the stack, plus move
958953
// the completed plans to the completed plan stack
959-
while (!plan_ptr->IsBasePlan()) {
960-
bool stale = plan_ptr->IsPlanStale();
961-
ThreadPlan *examined_plan = plan_ptr;
962-
plan_ptr = GetPreviousPlan(examined_plan);
954+
while (!plan_sp->IsBasePlan()) {
955+
bool stale = plan_sp->IsPlanStale();
956+
ThreadPlanSP examined_plan = plan_sp;
957+
plan_sp = GetPreviousPlan(examined_plan.get());
963958

964959
if (stale) {
965960
LLDB_LOGF(
@@ -1034,16 +1029,16 @@ Vote Thread::ShouldReportStop(Event *event_ptr) {
10341029
return GetPlans().GetCompletedPlan(false)->ShouldReportStop(event_ptr);
10351030
} else {
10361031
Vote thread_vote = eVoteNoOpinion;
1037-
ThreadPlan *plan_ptr = GetCurrentPlan();
1032+
ThreadPlanSP plan_sp = GetCurrentPlan();
10381033
while (true) {
1039-
if (plan_ptr->PlanExplainsStop(event_ptr)) {
1040-
thread_vote = plan_ptr->ShouldReportStop(event_ptr);
1034+
if (plan_sp->PlanExplainsStop(event_ptr)) {
1035+
thread_vote = plan_sp->ShouldReportStop(event_ptr);
10411036
break;
10421037
}
1043-
if (plan_ptr->IsBasePlan())
1038+
if (plan_sp->IsBasePlan())
10441039
break;
10451040
else
1046-
plan_ptr = GetPreviousPlan(plan_ptr);
1041+
plan_sp = GetPreviousPlan(plan_sp.get());
10471042
}
10481043
LLDB_LOGF(log,
10491044
"Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
@@ -1154,8 +1149,8 @@ void Thread::AutoCompleteThreadPlans(CompletionRequest &request) const {
11541149
}
11551150
}
11561151

1157-
ThreadPlan *Thread::GetCurrentPlan() const {
1158-
return GetPlans().GetCurrentPlan().get();
1152+
ThreadPlanSP Thread::GetCurrentPlan() const {
1153+
return GetPlans().GetCurrentPlan();
11591154
}
11601155

11611156
ThreadPlanSP Thread::GetCompletedPlan() const {
@@ -1182,7 +1177,7 @@ bool Thread::CompletedPlanOverridesBreakpoint() const {
11821177
return GetPlans().AnyCompletedPlans();
11831178
}
11841179

1185-
ThreadPlan *Thread::GetPreviousPlan(ThreadPlan *current_plan) const{
1180+
lldb::ThreadPlanSP Thread::GetPreviousPlan(ThreadPlan *current_plan) const {
11861181
return GetPlans().GetPreviousPlan(current_plan);
11871182
}
11881183

lldb/source/Target/ThreadPlan.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ Vote ThreadPlan::ShouldReportStop(Event *event_ptr) {
8080
Log *log = GetLog(LLDBLog::Step);
8181

8282
if (m_report_stop_vote == eVoteNoOpinion) {
83-
ThreadPlan *prev_plan = GetPreviousPlan();
84-
if (prev_plan) {
83+
if (ThreadPlanSP prev_plan = GetPreviousPlan()) {
8584
Vote prev_vote = prev_plan->ShouldReportStop(event_ptr);
8685
LLDB_LOG(log, "returning previous thread plan vote: {0}", prev_vote);
8786
return prev_vote;
@@ -93,8 +92,7 @@ Vote ThreadPlan::ShouldReportStop(Event *event_ptr) {
9392

9493
Vote ThreadPlan::ShouldReportRun(Event *event_ptr) {
9594
if (m_report_run_vote == eVoteNoOpinion) {
96-
ThreadPlan *prev_plan = GetPreviousPlan();
97-
if (prev_plan)
95+
if (ThreadPlanSP prev_plan = GetPreviousPlan())
9896
return prev_plan->ShouldReportRun(event_ptr);
9997
}
10098
return m_report_run_vote;
@@ -103,9 +101,9 @@ Vote ThreadPlan::ShouldReportRun(Event *event_ptr) {
103101
void ThreadPlan::ClearThreadCache() { m_thread = nullptr; }
104102

105103
bool ThreadPlan::StopOthers() {
106-
ThreadPlan *prev_plan;
107-
prev_plan = GetPreviousPlan();
108-
return (prev_plan == nullptr) ? false : prev_plan->StopOthers();
104+
if (ThreadPlanSP prev_plan = GetPreviousPlan())
105+
return prev_plan->StopOthers();
106+
return false;
109107
}
110108

111109
void ThreadPlan::SetStopOthers(bool new_value) {

lldb/source/Target/ThreadPlanStack.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ bool ThreadPlanStack::WasPlanDiscarded(ThreadPlan *in_plan) const {
355355
return false;
356356
}
357357

358-
ThreadPlan *ThreadPlanStack::GetPreviousPlan(ThreadPlan *current_plan) const {
358+
ThreadPlanSP ThreadPlanStack::GetPreviousPlan(ThreadPlan *current_plan) const {
359359
llvm::sys::ScopedReader guard(m_stack_mutex);
360360
if (current_plan == nullptr)
361361
return nullptr;
@@ -365,20 +365,20 @@ ThreadPlan *ThreadPlanStack::GetPreviousPlan(ThreadPlan *current_plan) const {
365365
int stack_size = m_completed_plans.size();
366366
for (int i = stack_size - 1; i > 0; i--) {
367367
if (current_plan == m_completed_plans[i].get())
368-
return m_completed_plans[i - 1].get();
368+
return m_completed_plans[i - 1];
369369
}
370370

371371
// If this is the first completed plan, the previous one is the
372372
// bottom of the regular plan stack.
373373
if (stack_size > 0 && m_completed_plans[0].get() == current_plan) {
374-
return GetCurrentPlanNoLock().get();
374+
return GetCurrentPlanNoLock();
375375
}
376376

377377
// Otherwise look for it in the regular plans.
378378
stack_size = m_plans.size();
379379
for (int i = stack_size - 1; i > 0; i--) {
380380
if (current_plan == m_plans[i].get())
381-
return m_plans[i - 1].get();
381+
return m_plans[i - 1];
382382
}
383383
return nullptr;
384384
}

0 commit comments

Comments
 (0)