Skip to content

Commit 2669fde

Browse files
authored
NFC: SBThread should not be the one to compute StopReasonData. (#157577)
This is something the StopInfo class manages, so it should be allowed to compute this rather than having SBThread do so. This code just moves the computation to methods in StopInfo. It is mostly NFC. The one change that I actually had to adjust the tests for was a couple of tests that were asking for the UnixSignal stop info data by asking for the data at index 1. GetStopInfoDataCount returns 1 and we don't do 1 based indexing so the test code was clearly wrong. But I don't think it makes sense to perpetuate handing out the value regardless of what index you pass us.
1 parent 6e5d008 commit 2669fde

File tree

5 files changed

+105
-117
lines changed

5 files changed

+105
-117
lines changed

lldb/include/lldb/Target/StopInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ class StopInfo : public std::enable_shared_from_this<StopInfo> {
9797
/// and silently continue again one more time.
9898
virtual bool WasContinueInterrupted(Thread &thread) { return false; }
9999

100+
virtual uint32_t GetStopReasonDataCount() const { return 0; }
101+
virtual uint64_t GetStopReasonDataAtIndex(uint32_t idx) {
102+
// Handle all the common cases that have no data.
103+
return 0;
104+
}
105+
100106
// Sometimes the thread plan logic will know that it wants a given stop to
101107
// stop or not, regardless of what the ordinary logic for that StopInfo would
102108
// dictate. The main example of this is the ThreadPlanCallFunction, which

lldb/source/API/SBThread.cpp

Lines changed: 4 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -157,52 +157,8 @@ size_t SBThread::GetStopReasonDataCount() {
157157
if (exe_ctx) {
158158
if (exe_ctx->HasThreadScope()) {
159159
StopInfoSP stop_info_sp = exe_ctx->GetThreadPtr()->GetStopInfo();
160-
if (stop_info_sp) {
161-
StopReason reason = stop_info_sp->GetStopReason();
162-
switch (reason) {
163-
case eStopReasonInvalid:
164-
case eStopReasonNone:
165-
case eStopReasonTrace:
166-
case eStopReasonExec:
167-
case eStopReasonPlanComplete:
168-
case eStopReasonThreadExiting:
169-
case eStopReasonInstrumentation:
170-
case eStopReasonProcessorTrace:
171-
case eStopReasonVForkDone:
172-
case eStopReasonHistoryBoundary:
173-
// There is no data for these stop reasons.
174-
return 0;
175-
176-
case eStopReasonBreakpoint: {
177-
break_id_t site_id = stop_info_sp->GetValue();
178-
lldb::BreakpointSiteSP bp_site_sp(
179-
exe_ctx->GetProcessPtr()->GetBreakpointSiteList().FindByID(
180-
site_id));
181-
if (bp_site_sp)
182-
return bp_site_sp->GetNumberOfConstituents() * 2;
183-
else
184-
return 0; // Breakpoint must have cleared itself...
185-
} break;
186-
187-
case eStopReasonWatchpoint:
188-
return 1;
189-
190-
case eStopReasonSignal:
191-
return 1;
192-
193-
case eStopReasonInterrupt:
194-
return 1;
195-
196-
case eStopReasonException:
197-
return 1;
198-
199-
case eStopReasonFork:
200-
return 1;
201-
202-
case eStopReasonVFork:
203-
return 1;
204-
}
205-
}
160+
if (stop_info_sp)
161+
return stop_info_sp->GetStopReasonDataCount();
206162
}
207163
} else {
208164
LLDB_LOG_ERROR(GetLog(LLDBLog::API), exe_ctx.takeError(), "{0}");
@@ -220,63 +176,8 @@ uint64_t SBThread::GetStopReasonDataAtIndex(uint32_t idx) {
220176
if (exe_ctx->HasThreadScope()) {
221177
Thread *thread = exe_ctx->GetThreadPtr();
222178
StopInfoSP stop_info_sp = thread->GetStopInfo();
223-
if (stop_info_sp) {
224-
StopReason reason = stop_info_sp->GetStopReason();
225-
switch (reason) {
226-
case eStopReasonInvalid:
227-
case eStopReasonNone:
228-
case eStopReasonTrace:
229-
case eStopReasonExec:
230-
case eStopReasonPlanComplete:
231-
case eStopReasonThreadExiting:
232-
case eStopReasonInstrumentation:
233-
case eStopReasonProcessorTrace:
234-
case eStopReasonVForkDone:
235-
case eStopReasonHistoryBoundary:
236-
// There is no data for these stop reasons.
237-
return 0;
238-
239-
case eStopReasonBreakpoint: {
240-
break_id_t site_id = stop_info_sp->GetValue();
241-
lldb::BreakpointSiteSP bp_site_sp(
242-
exe_ctx->GetProcessPtr()->GetBreakpointSiteList().FindByID(
243-
site_id));
244-
if (bp_site_sp) {
245-
uint32_t bp_index = idx / 2;
246-
BreakpointLocationSP bp_loc_sp(
247-
bp_site_sp->GetConstituentAtIndex(bp_index));
248-
if (bp_loc_sp) {
249-
if (idx & 1) {
250-
// Odd idx, return the breakpoint location ID
251-
return bp_loc_sp->GetID();
252-
} else {
253-
// Even idx, return the breakpoint ID
254-
return bp_loc_sp->GetBreakpoint().GetID();
255-
}
256-
}
257-
}
258-
return LLDB_INVALID_BREAK_ID;
259-
} break;
260-
261-
case eStopReasonWatchpoint:
262-
return stop_info_sp->GetValue();
263-
264-
case eStopReasonSignal:
265-
return stop_info_sp->GetValue();
266-
267-
case eStopReasonInterrupt:
268-
return stop_info_sp->GetValue();
269-
270-
case eStopReasonException:
271-
return stop_info_sp->GetValue();
272-
273-
case eStopReasonFork:
274-
return stop_info_sp->GetValue();
275-
276-
case eStopReasonVFork:
277-
return stop_info_sp->GetValue();
278-
}
279-
}
179+
if (stop_info_sp)
180+
return stop_info_sp->GetStopReasonDataAtIndex(idx);
280181
}
281182
} else {
282183
LLDB_LOG_ERROR(GetLog(LLDBLog::API), exe_ctx.takeError(), "{0}");

lldb/source/Target/StopInfo.cpp

Lines changed: 93 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ class StopInfoBreakpoint : public StopInfo {
108108
void StoreBPInfo() {
109109
ThreadSP thread_sp(m_thread_wp.lock());
110110
if (thread_sp) {
111-
BreakpointSiteSP bp_site_sp(
112-
thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
111+
BreakpointSiteSP bp_site_sp = GetBreakpointSiteSP();
113112
if (bp_site_sp) {
114113
uint32_t num_constituents = bp_site_sp->GetNumberOfConstituents();
115114
if (num_constituents == 1) {
@@ -139,8 +138,7 @@ class StopInfoBreakpoint : public StopInfo {
139138
bool IsValidForOperatingSystemThread(Thread &thread) override {
140139
ProcessSP process_sp(thread.GetProcess());
141140
if (process_sp) {
142-
BreakpointSiteSP bp_site_sp(
143-
process_sp->GetBreakpointSiteList().FindByID(m_value));
141+
BreakpointSiteSP bp_site_sp = GetBreakpointSiteSP();
144142
if (bp_site_sp)
145143
return bp_site_sp->ValidForThisThread(thread);
146144
}
@@ -154,8 +152,7 @@ class StopInfoBreakpoint : public StopInfo {
154152
if (thread_sp) {
155153
if (!m_should_stop_is_valid) {
156154
// Only check once if we should stop at a breakpoint
157-
BreakpointSiteSP bp_site_sp(
158-
thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
155+
BreakpointSiteSP bp_site_sp = GetBreakpointSiteSP();
159156
if (bp_site_sp) {
160157
ExecutionContext exe_ctx(thread_sp->GetStackFrameAtIndex(0));
161158
StoppointCallbackContext context(event_ptr, exe_ctx, true);
@@ -186,8 +183,7 @@ class StopInfoBreakpoint : public StopInfo {
186183
if (m_description.empty()) {
187184
ThreadSP thread_sp(m_thread_wp.lock());
188185
if (thread_sp) {
189-
BreakpointSiteSP bp_site_sp(
190-
thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
186+
BreakpointSiteSP bp_site_sp = GetBreakpointSiteSP();
191187
if (bp_site_sp) {
192188
StreamString strm;
193189
// If we have just hit an internal breakpoint, and it has a kind
@@ -247,6 +243,35 @@ class StopInfoBreakpoint : public StopInfo {
247243
return m_description.c_str();
248244
}
249245

246+
uint32_t GetStopReasonDataCount() const override {
247+
lldb::BreakpointSiteSP bp_site_sp = GetBreakpointSiteSP();
248+
if (bp_site_sp)
249+
return bp_site_sp->GetNumberOfConstituents() * 2;
250+
return 0; // Breakpoint must have cleared itself...
251+
}
252+
253+
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
254+
lldb::BreakpointSiteSP bp_site_sp = GetBreakpointSiteSP();
255+
if (bp_site_sp) {
256+
uint32_t bp_index = idx / 2;
257+
BreakpointLocationSP bp_loc_sp(
258+
bp_site_sp->GetConstituentAtIndex(bp_index));
259+
if (bp_loc_sp) {
260+
if (idx & 1) {
261+
// FIXME: This might be a Facade breakpoint, so we need to fetch
262+
// the one that the thread actually hit, not the native loc ID.
263+
264+
// Odd idx, return the breakpoint location ID
265+
return bp_loc_sp->GetID();
266+
} else {
267+
// Even idx, return the breakpoint ID
268+
return bp_loc_sp->GetBreakpoint().GetID();
269+
}
270+
}
271+
}
272+
return LLDB_INVALID_BREAK_ID;
273+
}
274+
250275
std::optional<uint32_t>
251276
GetSuggestedStackFrameIndex(bool inlined_stack) override {
252277
if (!inlined_stack)
@@ -255,8 +280,7 @@ class StopInfoBreakpoint : public StopInfo {
255280
ThreadSP thread_sp(m_thread_wp.lock());
256281
if (!thread_sp)
257282
return {};
258-
BreakpointSiteSP bp_site_sp(
259-
thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
283+
BreakpointSiteSP bp_site_sp = GetBreakpointSiteSP();
260284
if (!bp_site_sp)
261285
return {};
262286

@@ -297,8 +321,7 @@ class StopInfoBreakpoint : public StopInfo {
297321
return;
298322
}
299323

300-
BreakpointSiteSP bp_site_sp(
301-
thread_sp->GetProcess()->GetBreakpointSiteList().FindByID(m_value));
324+
BreakpointSiteSP bp_site_sp = GetBreakpointSiteSP();
302325
std::unordered_set<break_id_t> precondition_breakpoints;
303326
// Breakpoints that fail their condition check are not considered to
304327
// have been hit. If the only locations at this site have failed their
@@ -629,6 +652,20 @@ class StopInfoBreakpoint : public StopInfo {
629652
}
630653

631654
private:
655+
BreakpointSiteSP GetBreakpointSiteSP() const {
656+
if (m_value == LLDB_INVALID_BREAK_ID)
657+
return {};
658+
659+
ThreadSP thread_sp = GetThread();
660+
if (!thread_sp)
661+
return {};
662+
ProcessSP process_sp = thread_sp->GetProcess();
663+
if (!process_sp)
664+
return {};
665+
666+
return process_sp->GetBreakpointSiteList().FindByID(m_value);
667+
}
668+
632669
bool m_should_stop;
633670
bool m_should_stop_is_valid;
634671
bool m_should_perform_action; // Since we are trying to preserve the "state"
@@ -699,6 +736,13 @@ class StopInfoWatchpoint : public StopInfo {
699736

700737
StopReason GetStopReason() const override { return eStopReasonWatchpoint; }
701738

739+
uint32_t GetStopReasonDataCount() const override { return 1; }
740+
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
741+
if (idx == 0)
742+
return GetValue();
743+
return 0;
744+
}
745+
702746
const char *GetDescription() override {
703747
if (m_description.empty()) {
704748
StreamString strm;
@@ -1139,6 +1183,13 @@ class StopInfoUnixSignal : public StopInfo {
11391183

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

1186+
uint32_t GetStopReasonDataCount() const override { return 1; }
1187+
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1188+
if (idx == 0)
1189+
return GetValue();
1190+
return 0;
1191+
}
1192+
11421193
private:
11431194
// In siginfo_t terms, if m_value is si_signo, m_code is si_code.
11441195
std::optional<int> m_code;
@@ -1171,6 +1222,14 @@ class StopInfoInterrupt : public StopInfo {
11711222
}
11721223
return m_description.c_str();
11731224
}
1225+
1226+
uint32_t GetStopReasonDataCount() const override { return 1; }
1227+
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1228+
if (idx == 0)
1229+
return GetValue();
1230+
else
1231+
return 0;
1232+
}
11741233
};
11751234

11761235
// StopInfoTrace
@@ -1249,6 +1308,13 @@ class StopInfoException : public StopInfo {
12491308
else
12501309
return m_description.c_str();
12511310
}
1311+
uint32_t GetStopReasonDataCount() const override { return 1; }
1312+
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1313+
if (idx == 0)
1314+
return GetValue();
1315+
else
1316+
return 0;
1317+
}
12521318
};
12531319

12541320
// StopInfoProcessorTrace
@@ -1390,6 +1456,14 @@ class StopInfoFork : public StopInfo {
13901456

13911457
const char *GetDescription() override { return "fork"; }
13921458

1459+
uint32_t GetStopReasonDataCount() const override { return 1; }
1460+
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1461+
if (idx == 0)
1462+
return GetValue();
1463+
else
1464+
return 0;
1465+
}
1466+
13931467
protected:
13941468
void PerformAction(Event *event_ptr) override {
13951469
// Only perform the action once
@@ -1424,6 +1498,13 @@ class StopInfoVFork : public StopInfo {
14241498

14251499
const char *GetDescription() override { return "vfork"; }
14261500

1501+
uint32_t GetStopReasonDataCount() const override { return 1; }
1502+
uint64_t GetStopReasonDataAtIndex(uint32_t idx) override {
1503+
if (idx == 0)
1504+
return GetValue();
1505+
return 0;
1506+
}
1507+
14271508
protected:
14281509
void PerformAction(Event *event_ptr) override {
14291510
// Only perform the action once

lldb/test/API/functionalities/postmortem/elf-core/gcore/TestGCore.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def do_test(self, filename, pid):
3737
for thread in process:
3838
reason = thread.GetStopReason()
3939
self.assertStopReason(reason, lldb.eStopReasonSignal)
40-
signal = thread.GetStopReasonDataAtIndex(1)
40+
signal = thread.GetStopReasonDataAtIndex(0)
4141
# Check we got signal 19 (SIGSTOP)
4242
self.assertEqual(signal, 19)
4343

lldb/test/API/functionalities/postmortem/elf-core/thread_crash/TestLinuxCoreThreads.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def do_test(self, filename, pid, tid):
9191
reason = thread.GetStopReason()
9292
if thread.GetThreadID() == tid:
9393
self.assertStopReason(reason, lldb.eStopReasonSignal)
94-
signal = thread.GetStopReasonDataAtIndex(1)
94+
signal = thread.GetStopReasonDataAtIndex(0)
9595
# Check we got signal 4 (SIGILL)
9696
self.assertEqual(signal, 4)
9797
else:

0 commit comments

Comments
 (0)