
This PR fixes another race condition in https://github.com/llvm/llvm-project/pull/90930. The failure was found by @labath with this log: https://paste.debian.net/hidden/30235a5c/: ``` dotest_wrapper. < 15> send packet: $z0,224505,1#65 ... b-remote.async> < 22> send packet: $vCont;s:p1dcf.1dcf#4c intern-state GDBRemoteClientBase::Lock::Lock sent packet: \x03 b-remote.async> < 818> read packet: $T13thread:p1dcf.1dcf;name:a.out;threads:1dcf,1dd2;jstopinfo:5b7b226e616d65223a22612e6f7574222c22726561736f6e223a227369676e616c222c227369676e616c223a31392c22746964223a373633317d2c7b226e616d65223a22612e6f7574222c22746964223a373633347d5d;thread-pcs:0000000000224505,00007f4e4302119a;00:0000000000000000;01:0000000000000000;02:0100000000000000;03:0000000000000000;04:9084997dfc7f0000;05:a8742a0000000000;06:b084997dfc7f0000;07:6084997dfc7f0000;08:0000000000000000;09:00d7e5424e7f0000;0a:d0d9e5424e7f0000;0b:0202000000000000;0c:80cc290000000000;0d:d8cc1c434e7f0000;0e:2886997dfc7f0000;0f:0100000000000000;10:0545220000000000;11:0602000000000000;12:3300000000000000;13:0000000000000000;14:0000000000000000;15:2b00000000000000;16:80fbe5424e7f0000;17:0000000000000000;18:0000000000000000;19:0000000000000000;reason:signal;#b9 ``` It shows an async interrupt "\x03" was sent immediately after `vCont;s` single step over breakpoint at address `0x224505` (which was disabled before vCont). And the later stop was still at the original PC (0x224505) not moving forward. The investigation shows the failure happens when timeout is short and async interrupt is sent to lldb-server immediately after vCont so ptrace() resumes and then async interrupts debuggee immediately so debuggee does not get a chance to execute and move PC. So it enters stop mode immediately at original PC. `ThreadPlanStepOverBreakpoint` does not expect PC not moving and reports stop at the original place. To fix this, the PR prevents `ThreadPlanSingleThreadTimeout` from being created during `ThreadPlanStepOverBreakpoint` by introduces a new `SupportsResumeOthers()` method and `ThreadPlanStepOverBreakpoint` returns false for it. This makes sense because we should never resume threads during step over breakpoint anyway otherwise it might cause other threads to miss breakpoint. --------- Co-authored-by: jeffreytan81 <jeffreytan@fb.com>
180 lines
6.4 KiB
C++
180 lines
6.4 KiB
C++
//===-- ThreadPlanStepOverBreakpoint.cpp ----------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
|
|
|
|
#include "lldb/Target/Process.h"
|
|
#include "lldb/Target/RegisterContext.h"
|
|
#include "lldb/Utility/LLDBLog.h"
|
|
#include "lldb/Utility/Log.h"
|
|
#include "lldb/Utility/Stream.h"
|
|
|
|
using namespace lldb;
|
|
using namespace lldb_private;
|
|
|
|
// ThreadPlanStepOverBreakpoint: Single steps over a breakpoint bp_site_sp at
|
|
// the pc.
|
|
|
|
ThreadPlanStepOverBreakpoint::ThreadPlanStepOverBreakpoint(Thread &thread)
|
|
: ThreadPlan(
|
|
ThreadPlan::eKindStepOverBreakpoint, "Step over breakpoint trap",
|
|
thread, eVoteNo,
|
|
eVoteNoOpinion), // We need to report the run since this happens
|
|
// first in the thread plan stack when stepping over
|
|
// a breakpoint
|
|
m_breakpoint_addr(LLDB_INVALID_ADDRESS),
|
|
m_auto_continue(false), m_reenabled_breakpoint_site(false)
|
|
|
|
{
|
|
m_breakpoint_addr = thread.GetRegisterContext()->GetPC();
|
|
m_breakpoint_site_id =
|
|
thread.GetProcess()->GetBreakpointSiteList().FindIDByAddress(
|
|
m_breakpoint_addr);
|
|
}
|
|
|
|
ThreadPlanStepOverBreakpoint::~ThreadPlanStepOverBreakpoint() = default;
|
|
|
|
void ThreadPlanStepOverBreakpoint::GetDescription(
|
|
Stream *s, lldb::DescriptionLevel level) {
|
|
s->Printf("Single stepping past breakpoint site %" PRIu64 " at 0x%" PRIx64,
|
|
m_breakpoint_site_id, (uint64_t)m_breakpoint_addr);
|
|
}
|
|
|
|
bool ThreadPlanStepOverBreakpoint::ValidatePlan(Stream *error) { return true; }
|
|
|
|
bool ThreadPlanStepOverBreakpoint::DoPlanExplainsStop(Event *event_ptr) {
|
|
StopInfoSP stop_info_sp = GetPrivateStopInfo();
|
|
if (stop_info_sp) {
|
|
StopReason reason = stop_info_sp->GetStopReason();
|
|
|
|
Log *log = GetLog(LLDBLog::Step);
|
|
LLDB_LOG(log, "Step over breakpoint stopped for reason: {0}.",
|
|
Thread::StopReasonAsString(reason));
|
|
|
|
switch (reason) {
|
|
case eStopReasonTrace:
|
|
case eStopReasonNone:
|
|
return true;
|
|
case eStopReasonBreakpoint:
|
|
{
|
|
// It's a little surprising that we stop here for a breakpoint hit.
|
|
// However, when you single step ONTO a breakpoint we still want to call
|
|
// that a breakpoint hit, and trigger the actions, etc. Otherwise you
|
|
// would see the PC at the breakpoint without having triggered the
|
|
// actions, then you'd continue, the PC wouldn't change, and you'd see
|
|
// the breakpoint hit, which would be odd. So the lower levels fake
|
|
// "step onto breakpoint address" and return that as a breakpoint hit.
|
|
// So our trace step COULD appear as a breakpoint hit if the next
|
|
// instruction also contained a breakpoint. We don't want to handle
|
|
// that, since we really don't know what to do with breakpoint hits.
|
|
// But make sure we don't set ourselves to auto-continue or we'll wrench
|
|
// control away from the plans that can deal with this.
|
|
// Be careful, however, as we may have "seen a breakpoint under the PC
|
|
// because we stopped without changing the PC, in which case we do want
|
|
// to re-claim this stop so we'll try again.
|
|
lldb::addr_t pc_addr = GetThread().GetRegisterContext()->GetPC();
|
|
|
|
if (pc_addr == m_breakpoint_addr) {
|
|
LLDB_LOGF(log,
|
|
"Got breakpoint stop reason but pc: 0x%" PRIx64
|
|
"hasn't changed.",
|
|
pc_addr);
|
|
return true;
|
|
}
|
|
|
|
SetAutoContinue(false);
|
|
return false;
|
|
}
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool ThreadPlanStepOverBreakpoint::ShouldStop(Event *event_ptr) {
|
|
return !ShouldAutoContinue(event_ptr);
|
|
}
|
|
|
|
bool ThreadPlanStepOverBreakpoint::StopOthers() { return true; }
|
|
|
|
// This thread plan does a single instruction step over a breakpoint instruction
|
|
// and needs to not resume other threads, so return false to stop the
|
|
// ThreadPlanSingleThreadTimeout from timing out and trying to resume all
|
|
// threads. If all threads gets resumed before we disable, single step and
|
|
// re-enable the breakpoint, we can miss breakpoints on other threads.
|
|
bool ThreadPlanStepOverBreakpoint::SupportsResumeOthers() { return false; }
|
|
|
|
StateType ThreadPlanStepOverBreakpoint::GetPlanRunState() {
|
|
return eStateStepping;
|
|
}
|
|
|
|
bool ThreadPlanStepOverBreakpoint::DoWillResume(StateType resume_state,
|
|
bool current_plan) {
|
|
if (current_plan) {
|
|
BreakpointSiteSP bp_site_sp(
|
|
m_process.GetBreakpointSiteList().FindByAddress(m_breakpoint_addr));
|
|
if (bp_site_sp && bp_site_sp->IsEnabled()) {
|
|
m_process.DisableBreakpointSite(bp_site_sp.get());
|
|
m_reenabled_breakpoint_site = false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool ThreadPlanStepOverBreakpoint::WillStop() {
|
|
ReenableBreakpointSite();
|
|
return true;
|
|
}
|
|
|
|
void ThreadPlanStepOverBreakpoint::DidPop() { ReenableBreakpointSite(); }
|
|
|
|
bool ThreadPlanStepOverBreakpoint::MischiefManaged() {
|
|
lldb::addr_t pc_addr = GetThread().GetRegisterContext()->GetPC();
|
|
|
|
if (pc_addr == m_breakpoint_addr) {
|
|
// If we are still at the PC of our breakpoint, then for some reason we
|
|
// didn't get a chance to run.
|
|
return false;
|
|
} else {
|
|
Log *log = GetLog(LLDBLog::Step);
|
|
LLDB_LOGF(log, "Completed step over breakpoint plan.");
|
|
// Otherwise, re-enable the breakpoint we were stepping over, and we're
|
|
// done.
|
|
ReenableBreakpointSite();
|
|
ThreadPlan::MischiefManaged();
|
|
return true;
|
|
}
|
|
}
|
|
|
|
void ThreadPlanStepOverBreakpoint::ReenableBreakpointSite() {
|
|
if (!m_reenabled_breakpoint_site) {
|
|
m_reenabled_breakpoint_site = true;
|
|
BreakpointSiteSP bp_site_sp(
|
|
m_process.GetBreakpointSiteList().FindByAddress(m_breakpoint_addr));
|
|
if (bp_site_sp) {
|
|
m_process.EnableBreakpointSite(bp_site_sp.get());
|
|
}
|
|
}
|
|
}
|
|
void ThreadPlanStepOverBreakpoint::ThreadDestroyed() {
|
|
ReenableBreakpointSite();
|
|
}
|
|
|
|
void ThreadPlanStepOverBreakpoint::SetAutoContinue(bool do_it) {
|
|
m_auto_continue = do_it;
|
|
}
|
|
|
|
bool ThreadPlanStepOverBreakpoint::ShouldAutoContinue(Event *event_ptr) {
|
|
return m_auto_continue;
|
|
}
|
|
|
|
bool ThreadPlanStepOverBreakpoint::IsPlanStale() {
|
|
return GetThread().GetRegisterContext()->GetPC() != m_breakpoint_addr;
|
|
}
|