Skip to content

Commit e53d5e6

Browse files
[lldb][nfc] Improve duplicated code in unexecuted breakpoint detection
This code is replicated in multiple places, and a subsequent commit would introduce another copy of it in ThreadMemory.
1 parent 1824bb4 commit e53d5e6

File tree

5 files changed

+22
-24
lines changed

5 files changed

+22
-24
lines changed

lldb/include/lldb/Target/Thread.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,21 +385,22 @@ class Thread : public std::enable_shared_from_this<Thread>,
385385
virtual void SetQueueLibdispatchQueueAddress(lldb::addr_t dispatch_queue_t) {}
386386

387387
/// When a thread stops at an enabled BreakpointSite that has not executed,
388-
/// the Process plugin should call SetThreadStoppedAtUnexecutedBP(pc).
388+
/// the Process plugin should call DetectThreadStoppedAtUnexecutedBP().
389389
/// If that BreakpointSite was actually triggered (the instruction was
390390
/// executed, for a software breakpoint), regardless of whether the
391391
/// breakpoint is valid for this thread, SetThreadHitBreakpointSite()
392392
/// should be called to record that fact.
393393
///
394394
/// Depending on the structure of the Process plugin, it may be easiest
395-
/// to call SetThreadStoppedAtUnexecutedBP(pc) unconditionally when at
395+
/// to call DetectThreadStoppedAtUnexecutedBP() unconditionally when at
396396
/// a BreakpointSite, and later when it is known that it was triggered,
397397
/// SetThreadHitBreakpointSite() can be called. These two methods
398398
/// overwrite the same piece of state in the Thread, the last one
399399
/// called on a Thread wins.
400-
void SetThreadStoppedAtUnexecutedBP(lldb::addr_t pc) {
401-
m_stopped_at_unexecuted_bp = pc;
402-
}
400+
///
401+
/// Returns the BreakpointSite this thread is stopped at, if any.
402+
lldb::BreakpointSiteSP DetectThreadStoppedAtUnexecutedBP();
403+
403404
void SetThreadHitBreakpointSite() {
404405
m_stopped_at_unexecuted_bp = LLDB_INVALID_ADDRESS;
405406
}

lldb/source/Plugins/Process/Utility/StopInfoMachException.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,11 +591,7 @@ StopInfoSP StopInfoMachException::CreateStopReasonWithMachException(
591591
// breakpoint at 0x100.
592592
// The fact that the pc may be off by one at this point
593593
// (for an x86 KDP breakpoint hit) is not a problem.
594-
addr_t pc = reg_ctx_sp->GetPC();
595-
BreakpointSiteSP bp_site_sp =
596-
process_sp->GetBreakpointSiteList().FindByAddress(pc);
597-
if (bp_site_sp && bp_site_sp->IsEnabled())
598-
thread.SetThreadStoppedAtUnexecutedBP(pc);
594+
BreakpointSiteSP bp_site_sp = thread.DetectThreadStoppedAtUnexecutedBP();
599595

600596
switch (exc_type) {
601597
case 1: // EXC_BAD_ACCESS

lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,11 +1706,8 @@ ThreadSP ProcessGDBRemote::SetThreadStopInfo(
17061706
if (!thread_sp->StopInfoIsUpToDate()) {
17071707
thread_sp->SetStopInfo(StopInfoSP());
17081708

1709-
addr_t pc = thread_sp->GetRegisterContext()->GetPC();
17101709
BreakpointSiteSP bp_site_sp =
1711-
thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
1712-
if (bp_site_sp && bp_site_sp->IsEnabled())
1713-
thread_sp->SetThreadStoppedAtUnexecutedBP(pc);
1710+
thread_sp->DetectThreadStoppedAtUnexecutedBP();
17141711

17151712
if (exc_type != 0) {
17161713
// For thread plan async interrupt, creating stop info on the

lldb/source/Plugins/Process/scripted/ScriptedThread.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,7 @@ bool ScriptedThread::CalculateStopInfo() {
229229
LLVM_PRETTY_FUNCTION, "Failed to get scripted thread stop info.", error,
230230
LLDBLog::Thread);
231231

232-
// If we're at a BreakpointSite, mark that we stopped there and
233-
// need to hit the breakpoint when we resume. This will be cleared
234-
// if we CreateStopReasonWithBreakpointSiteID.
235-
if (RegisterContextSP reg_ctx_sp = GetRegisterContext()) {
236-
addr_t pc = reg_ctx_sp->GetPC();
237-
if (BreakpointSiteSP bp_site_sp =
238-
GetProcess()->GetBreakpointSiteList().FindByAddress(pc))
239-
if (bp_site_sp->IsEnabled())
240-
SetThreadStoppedAtUnexecutedBP(pc);
241-
}
232+
DetectThreadStoppedAtUnexecutedBP();
242233

243234
lldb::StopInfoSP stop_info_sp;
244235
lldb::StopReason stop_reason_type;

lldb/source/Target/Thread.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,3 +2108,16 @@ lldb::ValueObjectSP Thread::GetSiginfoValue() {
21082108
process_sp->GetByteOrder(), arch.GetAddressByteSize()};
21092109
return ValueObjectConstResult::Create(&target, type, ConstString("__lldb_siginfo"), data_extractor);
21102110
}
2111+
2112+
BreakpointSiteSP Thread::DetectThreadStoppedAtUnexecutedBP() {
2113+
if (RegisterContextSP reg_ctx_sp = GetRegisterContext()) {
2114+
addr_t pc = reg_ctx_sp->GetPC();
2115+
BreakpointSiteSP bp_site_sp =
2116+
GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
2117+
if (bp_site_sp && bp_site_sp->IsEnabled()) {
2118+
m_stopped_at_unexecuted_bp = pc;
2119+
return bp_site_sp;
2120+
}
2121+
}
2122+
return nullptr;
2123+
}

0 commit comments

Comments
 (0)