Skip to content

Commit df51a1b

Browse files
[lldb][NFC] Move ShouldShow/ShouldSelect logic into Stopinfo (llvm#134160)
This NFC patch simplifies the main loop in HandleProcessStateChanged event by moving duplicated code into the StopInfo class, also allowing StopInfo subclasses to override behavior. More specifically, two functions are created: * ShouldShow: should a Thread with such StopInfo should be printed when the debugger stops? Currently, no StopInfo subclasses override this, but a subsequent patch will fix a bug by making StopInfoBreakpoint check whether the breakpoint is internal. * ShouldSelect: should a Thread with such a StopInfo be selected? This is currently overridden by StopInfoUnixSignal but will, in the future, be overridden by StopInfoBreakpoint. (cherry-picked from commit c14b6e9)
1 parent 55e68a0 commit df51a1b

File tree

3 files changed

+38
-67
lines changed

3 files changed

+38
-67
lines changed

lldb/include/lldb/Target/StopInfo.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ class StopInfo : public std::enable_shared_from_this<StopInfo> {
117117

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

120+
/// Returns true if this is a stop reason that should be shown to a user when
121+
/// stopping.
122+
virtual bool ShouldShow() const { return IsValid(); }
123+
124+
/// Returns true if this is a stop reason that should cause a thread to be
125+
/// selected when stopping.
126+
virtual bool ShouldSelect() const {
127+
return GetStopReason() != lldb::eStopReasonNone &&
128+
GetStopReason() != lldb::eStopReasonInvalid;
129+
}
130+
120131
static lldb::StopInfoSP
121132
CreateStopReasonWithBreakpointSiteID(Thread &thread,
122133
lldb::break_id_t break_id);

lldb/source/Target/Process.cpp

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -940,77 +940,29 @@ bool Process::HandleProcessStateChangedEvent(
940940
std::lock_guard<std::recursive_mutex> guard(thread_list.GetMutex());
941941

942942
curr_thread = thread_list.GetSelectedThread();
943-
ThreadSP thread;
944-
StopReason curr_thread_stop_reason = eStopReasonInvalid;
945-
bool prefer_curr_thread = false;
946-
if (curr_thread && curr_thread->IsValid()) {
947-
curr_thread_stop_reason = curr_thread->GetStopReason();
948-
switch (curr_thread_stop_reason) {
949-
case eStopReasonNone:
950-
case eStopReasonInvalid:
951-
// Don't prefer the current thread if it didn't stop for a reason.
952-
break;
953-
case eStopReasonSignal: {
954-
// We need to do the same computation we do for other threads
955-
// below in case the current thread happens to be the one that
956-
// stopped for the no-stop signal.
957-
uint64_t signo = curr_thread->GetStopInfo()->GetValue();
958-
if (process_sp->GetUnixSignals()->GetShouldStop(signo))
959-
prefer_curr_thread = true;
960-
} break;
961-
default:
962-
prefer_curr_thread = true;
963-
break;
964-
}
943+
944+
if (curr_thread && curr_thread->IsValid())
965945
curr_thread_stop_info_sp = curr_thread->GetStopInfo();
966-
}
946+
bool prefer_curr_thread = curr_thread_stop_info_sp &&
947+
curr_thread_stop_info_sp->ShouldSelect();
967948

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

974-
const size_t num_threads = thread_list.GetSize();
975-
size_t i;
976-
for (i = 0; i < num_threads; ++i) {
977-
thread = thread_list.GetThreadAtIndex(i);
978-
StopReason thread_stop_reason = thread->GetStopReason();
979-
switch (thread_stop_reason) {
980-
case eStopReasonInvalid:
981-
case eStopReasonNone:
982-
break;
983-
984-
case eStopReasonSignal: {
985-
// Don't select a signal thread if we weren't going to stop at
986-
// that signal. We have to have had another reason for stopping
987-
// here, and the user doesn't want to see this thread.
988-
uint64_t signo = thread->GetStopInfo()->GetValue();
989-
if (process_sp->GetUnixSignals()->GetShouldStop(signo)) {
990-
if (!other_thread)
991-
other_thread = thread;
992-
}
993-
break;
994-
}
995-
case eStopReasonTrace:
996-
case eStopReasonBreakpoint:
997-
case eStopReasonWatchpoint:
998-
case eStopReasonException:
999-
case eStopReasonExec:
1000-
case eStopReasonFork:
1001-
case eStopReasonVFork:
1002-
case eStopReasonVForkDone:
1003-
case eStopReasonThreadExiting:
1004-
case eStopReasonInstrumentation:
1005-
case eStopReasonProcessorTrace:
1006-
if (!other_thread)
1007-
other_thread = thread;
1008-
break;
1009-
case eStopReasonPlanComplete:
955+
for (ThreadSP thread : thread_list.Threads()) {
956+
StopInfoSP stop_info = thread->GetStopInfo();
957+
if (!stop_info || !stop_info->ShouldSelect())
958+
continue;
959+
StopReason thread_stop_reason = stop_info->GetStopReason();
960+
if (thread_stop_reason == eStopReasonPlanComplete) {
1010961
if (!plan_thread)
1011962
plan_thread = thread;
1012-
break;
1013963
}
964+
else if (!other_thread)
965+
other_thread = thread;
1014966
}
1015967
if (plan_thread) {
1016968
thread_list.SetSelectedThreadByID(plan_thread->GetID());
@@ -1019,6 +971,7 @@ bool Process::HandleProcessStateChangedEvent(
1019971
thread_list.SetSelectedThreadByID(other_thread->GetID());
1020972
curr_thread = other_thread;
1021973
} else {
974+
ThreadSP thread;
1022975
if (curr_thread && curr_thread->IsValid())
1023976
thread = curr_thread;
1024977
else {
@@ -1031,6 +984,9 @@ bool Process::HandleProcessStateChangedEvent(
1031984
}
1032985
}
1033986

987+
StopReason curr_thread_stop_reason =
988+
curr_thread_stop_info_sp ? curr_thread_stop_info_sp->GetStopReason()
989+
: eStopReasonInvalid;
1034990
check_for_repl_breakpoint =
1035991
prefer_curr_thread ? IsDebuggerCausedStop(curr_thread_stop_reason)
1036992
: AnyDebuggerCausedStop(thread_list);
@@ -5940,7 +5896,7 @@ size_t Process::GetThreadStatus(Stream &strm,
59405896
if (thread_sp) {
59415897
if (only_threads_with_stop_reason) {
59425898
StopInfoSP stop_info_sp = thread_sp->GetStopInfo();
5943-
if (!stop_info_sp || !stop_info_sp->IsValid())
5899+
if (!stop_info_sp || !stop_info_sp->ShouldShow())
59445900
continue;
59455901
}
59465902
thread_sp->GetStatus(strm, start_frame, num_frames,

lldb/source/Target/StopInfo.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,12 +1080,7 @@ class StopInfoUnixSignal : public StopInfo {
10801080
return false;
10811081
}
10821082

1083-
bool ShouldStop(Event *event_ptr) override {
1084-
ThreadSP thread_sp(m_thread_wp.lock());
1085-
if (thread_sp)
1086-
return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
1087-
return false;
1088-
}
1083+
bool ShouldStop(Event *event_ptr) override { return IsShouldStopSignal(); }
10891084

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

1135+
bool ShouldSelect() const override { return IsShouldStopSignal(); }
1136+
11401137
private:
11411138
// In siginfo_t terms, if m_value is si_signo, m_code is si_code.
11421139
std::optional<int> m_code;
1140+
1141+
bool IsShouldStopSignal() const {
1142+
ThreadSP thread_sp(m_thread_wp.lock());
1143+
if (thread_sp)
1144+
return thread_sp->GetProcess()->GetUnixSignals()->GetShouldStop(m_value);
1145+
return false;
1146+
}
11431147
};
11441148

11451149
// StopInfoTrace

0 commit comments

Comments
 (0)