Skip to content

[lldb][NFC] Move ShouldShow/ShouldSelect logic into Stopinfo #134160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lldb/include/lldb/Target/StopInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ class StopInfo : public std::enable_shared_from_this<StopInfo> {

StructuredData::ObjectSP GetExtendedInfo() { return m_extended_info; }

/// Returns true if this is a stop reason that should be shown to a user when
/// viewing the thread with this stop info.
virtual bool ShouldShow() const { return IsValid(); }

/// Returns true if this is a stop reason that should cause a thread to be
/// selected when stopping.
virtual bool ShouldSelect() const {
lldb::StopReason reason = GetStopReason();
return reason != lldb::eStopReasonNone &&
reason != lldb::eStopReasonHistoryBoundary &&
reason != lldb::eStopReasonInvalid;
}

static lldb::StopInfoSP
CreateStopReasonWithBreakpointSiteID(Thread &thread,
lldb::break_id_t break_id);
Expand Down
78 changes: 14 additions & 64 deletions lldb/source/Target/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,85 +808,35 @@ bool Process::HandleProcessStateChangedEvent(
std::lock_guard<std::recursive_mutex> guard(thread_list.GetMutex());

ThreadSP curr_thread(thread_list.GetSelectedThread());
ThreadSP thread;
StopReason curr_thread_stop_reason = eStopReasonInvalid;
bool prefer_curr_thread = false;
if (curr_thread && curr_thread->IsValid()) {
curr_thread_stop_reason = curr_thread->GetStopReason();
switch (curr_thread_stop_reason) {
case eStopReasonNone:
case eStopReasonInvalid:
// Don't prefer the current thread if it didn't stop for a reason.
break;
case eStopReasonSignal: {
// We need to do the same computation we do for other threads
// below in case the current thread happens to be the one that
// stopped for the no-stop signal.
uint64_t signo = curr_thread->GetStopInfo()->GetValue();
if (process_sp->GetUnixSignals()->GetShouldStop(signo))
prefer_curr_thread = true;
} break;
default:
prefer_curr_thread = true;
break;
}

if (curr_thread && curr_thread->IsValid())
curr_thread_stop_info_sp = curr_thread->GetStopInfo();
}
bool prefer_curr_thread = curr_thread_stop_info_sp &&
curr_thread_stop_info_sp->ShouldSelect();

if (!prefer_curr_thread) {
// Prefer a thread that has just completed its plan over another
// thread as current thread.
ThreadSP plan_thread;
ThreadSP other_thread;

const size_t num_threads = thread_list.GetSize();
size_t i;
for (i = 0; i < num_threads; ++i) {
thread = thread_list.GetThreadAtIndex(i);
StopReason thread_stop_reason = thread->GetStopReason();
switch (thread_stop_reason) {
case eStopReasonInvalid:
case eStopReasonNone:
case eStopReasonHistoryBoundary:
break;

case eStopReasonSignal: {
// Don't select a signal thread if we weren't going to stop at
// that signal. We have to have had another reason for stopping
// here, and the user doesn't want to see this thread.
uint64_t signo = thread->GetStopInfo()->GetValue();
if (process_sp->GetUnixSignals()->GetShouldStop(signo)) {
if (!other_thread)
other_thread = thread;
}
break;
}
case eStopReasonTrace:
case eStopReasonBreakpoint:
case eStopReasonWatchpoint:
case eStopReasonException:
case eStopReasonExec:
case eStopReasonFork:
case eStopReasonVFork:
case eStopReasonVForkDone:
case eStopReasonThreadExiting:
case eStopReasonInstrumentation:
case eStopReasonProcessorTrace:
case eStopReasonInterrupt:
if (!other_thread)
other_thread = thread;
break;
case eStopReasonPlanComplete:
for (ThreadSP thread : thread_list.Threads()) {
StopInfoSP stop_info = thread->GetStopInfo();
if (!stop_info || !stop_info->ShouldSelect())
continue;
StopReason thread_stop_reason = stop_info->GetStopReason();
if (thread_stop_reason == eStopReasonPlanComplete) {
if (!plan_thread)
plan_thread = thread;
break;
}
} else if (!other_thread)
other_thread = thread;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The LLVM Style Guide says to "[u]se braces for the else if and else block to keep it uniform with the if block."

Suggested change
} else if (!other_thread)
other_thread = thread;
} else if (!other_thread) {
other_thread = thread;
}

}
if (plan_thread)
thread_list.SetSelectedThreadByID(plan_thread->GetID());
else if (other_thread)
thread_list.SetSelectedThreadByID(other_thread->GetID());
else {
ThreadSP thread;
if (curr_thread && curr_thread->IsValid())
thread = curr_thread;
else
Expand Down Expand Up @@ -5826,7 +5776,7 @@ size_t Process::GetThreadStatus(Stream &strm,
if (thread_sp) {
if (only_threads_with_stop_reason) {
StopInfoSP stop_info_sp = thread_sp->GetStopInfo();
if (!stop_info_sp || !stop_info_sp->IsValid())
if (!stop_info_sp || !stop_info_sp->ShouldShow())
continue;
}
thread_sp->GetStatus(strm, start_frame, num_frames,
Expand Down
15 changes: 9 additions & 6 deletions lldb/source/Target/StopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1080,12 +1080,7 @@ class StopInfoUnixSignal : public StopInfo {
return false;
}

bool ShouldStop(Event *event_ptr) override {
ThreadSP thread_sp(m_thread_wp.lock());
if (thread_sp)
return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
return false;
}
bool ShouldStop(Event *event_ptr) override { return IsShouldStopSignal(); }

// If should stop returns false, check if we should notify of this event
bool DoShouldNotify(Event *event_ptr) override {
Expand Down Expand Up @@ -1137,9 +1132,17 @@ class StopInfoUnixSignal : public StopInfo {
return m_description.c_str();
}

bool ShouldSelect() const override { return IsShouldStopSignal(); }

private:
// In siginfo_t terms, if m_value is si_signo, m_code is si_code.
std::optional<int> m_code;

bool IsShouldStopSignal() const {
if (ThreadSP thread_sp = m_thread_wp.lock())
return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
return false;
}
};

// StopInfoInterrupt
Expand Down