Skip to content

Commit 87fadb3

Browse files
authored
[lldb] Correctly annotate threads at a bp site as hitting it (llvm#82709)
This is next in my series of "fix the racey tests that fail on greendragon" addressing the failure of TestConcurrentManyBreakpoints.py where we set a breakpoint in a function that 100 threads execute, and we check that we hit the breakpoint 100 times. But sometimes it is only hit 99 times, and the test fails. When we hit a software breakpoint, the pc value for the thread is the address of the breakpoint instruction - as if it had not been hit yet. And because a user might ADD a breakpoint for the current pc from the commandline, when we go to resume execution, any thread that is sitting at a breakpoint site will be silently advanced past the breakpoint instruction (disable bp, instruction step that thread, re-enable bp) before resuming -- whether that thread has hit its breakpoint or not. What this test is exposing is that there is another corner case, a thread that is sitting at a breakpoint site but has not yet executed the breakpoint instruction. The thread will have no stop reason, no mach exception, so it will not be recorded as having hit the breakpoint (because it hasn't yet). But when we resume execution, because it is sitting at a breakpoint site, we advance past it and miss the breakpoint hit. In 2016 Abhishek Aggarwal handled a similar issue with a patch in `ProcessGDBRemote::SetThreadStopInfo()`, adding a breakpoint StopInfo for a thread sitting at a breakpoint site that has no stop reason. debugserver's `jThreadsInfo` would not correctly execute Abhishek's code though because it would respond with `"reason":"none"` for a thread with no stop reason, and `SetThreadStopInfo()` expected an empty reason here. The first part of my patch is to clear the `reason` if it is `"none"` so we flow through the code correctly. On Darwin, though, our stop reply packet (Txx...) includes the `threads`, `thread-pcs`, and `jstopinfo` keys, which give us the tids for all current threads, the pc values for those threads, and `jstopinfo` has a JSON dictionary with the mach exceptions for all threads that have a mach exception. In `ProcessGDBRemote::CalculateThreadStopInfo()` we set the StopInfo for each thread for a private stop and if we have `jstopinfo` it is the source of all the StopInfos. I have to add the same logic here, to give the thread a breakpoint StopInfo even though it hasn't executed the breakpoint yet. In this case we are very early in thread construction and I only have the information in the Txx stop reply packet -- tids, pcs, and jstopinfo, so I can't use the normal general mechanisms of going through the RegisterContext to get the pc, it's a bit different. If I hack debugserver to not issue `jstopinfo`, `CalculateThreadStopInfo` will fall back to sending `qThreadStopInfo` for each thread and going through `ProcessGDBRemote::SetThreadStopInfo()` to set the stop infos (and with the `reason:none` fix, use Abhishek's code). rdar://110549165
1 parent 69c0b2f commit 87fadb3

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

lldb/include/lldb/Target/Thread.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,13 +1163,20 @@ class Thread : public std::enable_shared_from_this<Thread>,
11631163

11641164
void CalculatePublicStopInfo();
11651165

1166-
// Ask the thread subclass to set its stop info.
1167-
//
1168-
// Thread subclasses should call Thread::SetStopInfo(...) with the reason the
1169-
// thread stopped.
1170-
//
1171-
// \return
1172-
// True if Thread::SetStopInfo(...) was called, false otherwise.
1166+
/// Ask the thread subclass to set its stop info.
1167+
///
1168+
/// Thread subclasses should call Thread::SetStopInfo(...) with the reason the
1169+
/// thread stopped.
1170+
///
1171+
/// A thread that is sitting at a breakpoint site, but has not yet executed
1172+
/// the breakpoint instruction, should have a breakpoint-hit StopInfo set.
1173+
/// When execution is resumed, any thread sitting at a breakpoint site will
1174+
/// instruction-step over the breakpoint instruction silently, and we will
1175+
/// never record this breakpoint as being hit, updating the hit count,
1176+
/// possibly executing breakpoint commands or conditions.
1177+
///
1178+
/// \return
1179+
/// True if Thread::SetStopInfo(...) was called, false otherwise.
11731180
virtual bool CalculateStopInfo() = 0;
11741181

11751182
// Gets the temporary resume state for a thread.

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,26 @@ bool ProcessGDBRemote::CalculateThreadStopInfo(ThreadGDBRemote *thread) {
16001600
// has no stop reason.
16011601
thread->GetRegisterContext()->InvalidateIfNeeded(true);
16021602
if (!GetThreadStopInfoFromJSON(thread, m_jstopinfo_sp)) {
1603+
// If a thread is stopped at a breakpoint site, set that as the stop
1604+
// reason even if it hasn't executed the breakpoint instruction yet.
1605+
// We will silently step over the breakpoint when we resume execution
1606+
// and miss the fact that this thread hit the breakpoint.
1607+
const size_t num_thread_ids = m_thread_ids.size();
1608+
for (size_t i = 0; i < num_thread_ids; i++) {
1609+
if (m_thread_ids[i] == thread->GetID() && m_thread_pcs.size() > i) {
1610+
addr_t pc = m_thread_pcs[i];
1611+
lldb::BreakpointSiteSP bp_site_sp =
1612+
thread->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
1613+
if (bp_site_sp) {
1614+
if (bp_site_sp->ValidForThisThread(*thread)) {
1615+
thread->SetStopInfo(
1616+
StopInfo::CreateStopReasonWithBreakpointSiteID(
1617+
*thread, bp_site_sp->GetID()));
1618+
return true;
1619+
}
1620+
}
1621+
}
1622+
}
16031623
thread->SetStopInfo(StopInfoSP());
16041624
}
16051625
return true;
@@ -1722,7 +1742,9 @@ ThreadSP ProcessGDBRemote::SetThreadStopInfo(
17221742
} else {
17231743
bool handled = false;
17241744
bool did_exec = false;
1725-
if (!reason.empty()) {
1745+
// debugserver can send reason = "none" which is equivalent
1746+
// to no reason.
1747+
if (!reason.empty() && reason != "none") {
17261748
if (reason == "trace") {
17271749
addr_t pc = thread_sp->GetRegisterContext()->GetPC();
17281750
lldb::BreakpointSiteSP bp_site_sp =
@@ -1864,11 +1886,10 @@ ThreadSP ProcessGDBRemote::SetThreadStopInfo(
18641886
lldb::BreakpointSiteSP bp_site_sp =
18651887
thread_sp->GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
18661888

1867-
// If the current pc is a breakpoint site then the StopInfo should be
1868-
// set to Breakpoint even though the remote stub did not set it as such.
1869-
// This can happen when the thread is involuntarily interrupted (e.g.
1870-
// due to stops on other threads) just as it is about to execute the
1871-
// breakpoint instruction.
1889+
// If a thread is stopped at a breakpoint site, set that as the stop
1890+
// reason even if it hasn't executed the breakpoint instruction yet.
1891+
// We will silently step over the breakpoint when we resume execution
1892+
// and miss the fact that this thread hit the breakpoint.
18721893
if (bp_site_sp && bp_site_sp->ValidForThisThread(*thread_sp)) {
18731894
thread_sp->SetStopInfo(StopInfo::CreateStopReasonWithBreakpointSiteID(
18741895
*thread_sp, bp_site_sp->GetID()));

0 commit comments

Comments
 (0)