418 changes: 418 additions & 0 deletions lldb/packages/Python/lldbsuite/test/lldbreverse.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lldb/packages/Python/lldbsuite/test/lldbtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@

STOPPED_DUE_TO_WATCHPOINT = "Process should be stopped due to watchpoint"

STOPPED_DUE_TO_HISTORY_BOUNDARY = "Process should be stopped due to history boundary"

DATA_TYPES_DISPLAYED_CORRECTLY = "Data type(s) displayed correctly"

VALID_BREAKPOINT = "Got a valid breakpoint"
Expand Down
8 changes: 6 additions & 2 deletions lldb/source/API/SBProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ uint32_t SBProcess::GetAddressByteSize() const {
}

SBError SBProcess::Continue() {
return Continue(RunDirection::eRunForward);
}

SBError SBProcess::Continue(RunDirection direction) {
LLDB_INSTRUMENT_VA(this);

SBError sb_error;
Expand All @@ -574,9 +578,9 @@ SBError SBProcess::Continue() {
process_sp->GetTarget().GetAPIMutex());

if (process_sp->GetTarget().GetDebugger().GetAsyncExecution())
sb_error.ref() = process_sp->Resume();
sb_error.ref() = process_sp->Resume(direction);
else
sb_error.ref() = process_sp->ResumeSynchronous(nullptr);
sb_error.ref() = process_sp->ResumeSynchronous(nullptr, direction);
} else
sb_error = Status::FromErrorString("SBProcess is invalid");

Expand Down
2 changes: 2 additions & 0 deletions lldb/source/API/SBThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ size_t SBThread::GetStopReasonDataCount() {
case eStopReasonInstrumentation:
case eStopReasonProcessorTrace:
case eStopReasonVForkDone:
case eStopReasonHistoryBoundary:
// There is no data for these stop reasons.
return 0;

Expand Down Expand Up @@ -233,6 +234,7 @@ uint64_t SBThread::GetStopReasonDataAtIndex(uint32_t idx) {
case eStopReasonInstrumentation:
case eStopReasonProcessorTrace:
case eStopReasonVForkDone:
case eStopReasonHistoryBoundary:
// There is no data for these stop reasons.
return 0;

Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Interpreter/CommandInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2553,7 +2553,8 @@ bool CommandInterpreter::DidProcessStopAbnormally() const {
const StopReason reason = stop_info->GetStopReason();
if (reason == eStopReasonException ||
reason == eStopReasonInstrumentation ||
reason == eStopReasonProcessorTrace || reason == eStopReasonInterrupt)
reason == eStopReasonProcessorTrace || reason == eStopReasonInterrupt ||
reason == eStopReasonHistoryBoundary)
return true;

if (reason == eStopReasonSignal) {
Expand Down
3 changes: 3 additions & 0 deletions lldb/source/Plugins/Process/Linux/NativeThreadLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ void LogThreadStopInfo(Log &log, const ThreadStopInfo &stop_info,
case eStopReasonProcessorTrace:
log.Printf("%s: %s processor trace", __FUNCTION__, header);
return;
case eStopReasonHistoryBoundary:
log.Printf("%s: %s history boundary", __FUNCTION__, header);
return;
default:
log.Printf("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header,
static_cast<uint32_t>(stop_info.reason));
Expand Down
10 changes: 9 additions & 1 deletion lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,17 @@ lldb_private::DynamicLoader *ProcessKDP::GetDynamicLoader() {

Status ProcessKDP::WillResume() { return Status(); }

Status ProcessKDP::DoResume() {
Status ProcessKDP::DoResume(RunDirection direction) {
Status error;
Log *log = GetLog(KDPLog::Process);

if (direction == RunDirection::eRunReverse) {
error.FromErrorStringWithFormatv(
"error: {0} does not support reverse execution of processes",
GetPluginName());
return error;
}

// Only start the async thread if we try to do any process control
if (!m_async_thread.IsJoinable())
StartAsyncThread();
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class ProcessKDP : public lldb_private::Process {
// Process Control
lldb_private::Status WillResume() override;

lldb_private::Status DoResume() override;
lldb_private::Status DoResume(lldb::RunDirection direction) override;

lldb_private::Status DoHalt(bool &caused_stop) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,17 @@ ProcessWindows::DoAttachToProcessWithID(lldb::pid_t pid,
return error;
}

Status ProcessWindows::DoResume() {
Status ProcessWindows::DoResume(RunDirection direction) {
Log *log = GetLog(WindowsLog::Process);
llvm::sys::ScopedLock lock(m_mutex);
Status error;

if (direction == RunDirection::eRunReverse) {
error.SetErrorStringWithFormatv(
"error: {0} does not support reverse execution of processes", GetPluginName());
return error;
}

StateType private_state = GetPrivateState();
if (private_state == eStateStopped || private_state == eStateCrashed) {
LLDB_LOG(log, "process {0} is in state {1}. Resuming...",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ProcessWindows : public Process, public ProcessDebugger {
Status DoAttachToProcessWithID(
lldb::pid_t pid,
const lldb_private::ProcessAttachInfo &attach_info) override;
Status DoResume() override;
Status DoResume(lldb::RunDirection direction) override;
Status DoDestroy() override;
Status DoHalt(bool &caused_stop) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ uint64_t GDBRemoteCommunicationClient::GetRemoteMaxPacketSize() {
return m_max_packet_size;
}

bool GDBRemoteCommunicationClient::GetReverseContinueSupported() {
if (m_supports_reverse_continue == eLazyBoolCalculate) {
GetRemoteQSupported();
}
return m_supports_reverse_continue == eLazyBoolYes;
}

bool GDBRemoteCommunicationClient::GetReverseStepSupported() {
if (m_supports_reverse_step == eLazyBoolCalculate) {
GetRemoteQSupported();
}
return m_supports_reverse_step == eLazyBoolYes;
}

bool GDBRemoteCommunicationClient::QueryNoAckModeSupported() {
if (m_supports_not_sending_acks == eLazyBoolCalculate) {
m_send_acks = true;
Expand Down Expand Up @@ -295,6 +309,8 @@ void GDBRemoteCommunicationClient::ResetDiscoverableSettings(bool did_exec) {
m_supports_qXfer_siginfo_read = eLazyBoolCalculate;
m_supports_augmented_libraries_svr4_read = eLazyBoolCalculate;
m_uses_native_signals = eLazyBoolCalculate;
m_supports_reverse_continue = eLazyBoolCalculate;
m_supports_reverse_step = eLazyBoolCalculate;
m_supports_qProcessInfoPID = true;
m_supports_qfProcessInfo = true;
m_supports_qUserName = true;
Expand Down Expand Up @@ -348,6 +364,8 @@ void GDBRemoteCommunicationClient::GetRemoteQSupported() {
m_supports_memory_tagging = eLazyBoolNo;
m_supports_qSaveCore = eLazyBoolNo;
m_uses_native_signals = eLazyBoolNo;
m_supports_reverse_continue = eLazyBoolNo;
m_supports_reverse_step = eLazyBoolNo;

m_max_packet_size = UINT64_MAX; // It's supposed to always be there, but if
// not, we assume no limit
Expand Down Expand Up @@ -401,6 +419,10 @@ void GDBRemoteCommunicationClient::GetRemoteQSupported() {
m_supports_qSaveCore = eLazyBoolYes;
else if (x == "native-signals+")
m_uses_native_signals = eLazyBoolYes;
else if (x == "ReverseContinue+")
m_supports_reverse_continue = eLazyBoolYes;
else if (x == "ReverseStep+")
m_supports_reverse_step = eLazyBoolYes;
// Look for a list of compressions in the features list e.g.
// qXfer:features:read+;PacketSize=20000;qEcho+;SupportedCompressions=zlib-
// deflate,lzma
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ class GDBRemoteCommunicationClient : public GDBRemoteClientBase {

bool GetMultiprocessSupported();

bool GetReverseContinueSupported();

bool GetReverseStepSupported();

LazyBool SupportsAllocDeallocMemory() // const
{
// Uncomment this to have lldb pretend the debug server doesn't respond to
Expand Down Expand Up @@ -561,6 +565,8 @@ class GDBRemoteCommunicationClient : public GDBRemoteClientBase {
LazyBool m_supports_memory_tagging = eLazyBoolCalculate;
LazyBool m_supports_qSaveCore = eLazyBoolCalculate;
LazyBool m_uses_native_signals = eLazyBoolCalculate;
LazyBool m_supports_reverse_continue = eLazyBoolCalculate;
LazyBool m_supports_reverse_step = eLazyBoolCalculate;

bool m_supports_qProcessInfoPID : 1, m_supports_qfProcessInfo : 1,
m_supports_qUserName : 1, m_supports_qGroupName : 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ static const char *GetStopReasonString(StopReason stop_reason) {
return "vforkdone";
case eStopReasonInterrupt:
return "async interrupt";
case eStopReasonHistoryBoundary:
case eStopReasonInstrumentation:
case eStopReasonInvalid:
case eStopReasonPlanComplete:
Expand Down
77 changes: 63 additions & 14 deletions lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ class PluginProperties : public Properties {
}
};

std::chrono::seconds ResumeTimeout() {
return std::chrono::seconds(5);
}

} // namespace

static PluginProperties &GetGlobalPluginProperties() {
Expand Down Expand Up @@ -1180,10 +1184,11 @@ Status ProcessGDBRemote::WillResume() {
return Status();
}

Status ProcessGDBRemote::DoResume() {
Status ProcessGDBRemote::DoResume(RunDirection direction) {
Status error;
Log *log = GetLog(GDBRLog::Process);
LLDB_LOGF(log, "ProcessGDBRemote::Resume()");
LLDB_LOGF(log, "ProcessGDBRemote::Resume(%s)",
direction == RunDirection::eRunForward ? "" : "reverse");

ListenerSP listener_sp(
Listener::MakeListener("gdb-remote.resume-packet-sent"));
Expand All @@ -1197,12 +1202,21 @@ Status ProcessGDBRemote::DoResume() {

StreamString continue_packet;
bool continue_packet_error = false;
if (m_gdb_comm.HasAnyVContSupport()) {
// Number of threads continuing with "c", i.e. continuing without a signal to deliver.
const size_t num_continue_c_tids = m_continue_c_tids.size();
// Number of threads continuing with "C", i.e. continuing with a signal to deliver.
const size_t num_continue_C_tids = m_continue_C_tids.size();
// Number of threads continuing with "s", i.e. single-stepping.
const size_t num_continue_s_tids = m_continue_s_tids.size();
// Number of threads continuing with "S", i.e. single-stepping with a signal to deliver.
const size_t num_continue_S_tids = m_continue_S_tids.size();
if (direction == RunDirection::eRunForward &&
m_gdb_comm.HasAnyVContSupport()) {
std::string pid_prefix;
if (m_gdb_comm.GetMultiprocessSupported())
pid_prefix = llvm::formatv("p{0:x-}.", GetID());

if (m_continue_c_tids.size() == num_threads ||
if (num_continue_c_tids == num_threads ||
(m_continue_c_tids.empty() && m_continue_C_tids.empty() &&
m_continue_s_tids.empty() && m_continue_S_tids.empty())) {
// All threads are continuing
Expand Down Expand Up @@ -1265,14 +1279,11 @@ Status ProcessGDBRemote::DoResume() {
} else
continue_packet_error = true;

if (continue_packet_error) {
if (direction == RunDirection::eRunForward && continue_packet_error) {
// Either no vCont support, or we tried to use part of the vCont packet
// that wasn't supported by the remote GDB server. We need to try and
// make a simple packet that can do our continue
const size_t num_continue_c_tids = m_continue_c_tids.size();
const size_t num_continue_C_tids = m_continue_C_tids.size();
const size_t num_continue_s_tids = m_continue_s_tids.size();
const size_t num_continue_S_tids = m_continue_S_tids.size();
// that wasn't supported by the remote GDB server, or it's the reverse
// direction. We need to try and make a simple packet that can do our
// continue.
if (num_continue_c_tids > 0) {
if (num_continue_c_tids == num_threads) {
// All threads are resuming...
Expand Down Expand Up @@ -1363,9 +1374,41 @@ Status ProcessGDBRemote::DoResume() {
}
}

if (direction == RunDirection::eRunReverse && continue_packet_error) {
if (num_continue_C_tids > 0 || num_continue_S_tids > 0) {
LLDB_LOGF(log, "ProcessGDBRemote::DoResumeReverse: Signals not supported");
return Status::FromErrorString("can't deliver signals while running in reverse");
}

if (num_continue_s_tids > 0) {
if (num_continue_s_tids > 1) {
LLDB_LOGF(log, "ProcessGDBRemote::DoResumeReverse: can't step multiple threads");
return Status::FromErrorString("can't step multiple threads while reverse-stepping");
}

if (!m_gdb_comm.GetReverseStepSupported()) {
LLDB_LOGF(log, "ProcessGDBRemote::DoResumeReverse: target does not support reverse-stepping");
return Status::FromErrorString("target does not support reverse-stepping");
}

m_gdb_comm.SetCurrentThreadForRun(m_continue_s_tids.front());
continue_packet.PutCString("bs");
} else {
if (!m_gdb_comm.GetReverseContinueSupported()) {
LLDB_LOGF(log, "ProcessGDBRemote::DoResumeReverse: target does not support reverse-continue");
return Status::FromErrorString("target does not support reverse-continue");
}

// All threads continue whether requested or not ---
// we can't change how threads ran in the past.
continue_packet.PutCString("bc");
}

continue_packet_error = false;
}

if (continue_packet_error) {
error =
Status::FromErrorString("can't make continue packet for this resume");
return Status::FromErrorString("can't make continue packet for this resume");
} else {
EventSP event_sp;
if (!m_async_thread.IsJoinable()) {
Expand All @@ -1380,7 +1423,7 @@ Status ProcessGDBRemote::DoResume() {
std::make_shared<EventDataBytes>(continue_packet.GetString());
m_async_broadcaster.BroadcastEvent(eBroadcastBitAsyncContinue, data_sp);

if (!listener_sp->GetEvent(event_sp, std::chrono::seconds(5))) {
if (!listener_sp->GetEvent(event_sp, ResumeTimeout())) {
error = Status::FromErrorString("Resume timed out.");
LLDB_LOGF(log, "ProcessGDBRemote::DoResume: Resume timed out.");
} else if (event_sp->BroadcasterIs(&m_async_broadcaster)) {
Expand Down Expand Up @@ -1863,6 +1906,10 @@ ThreadSP ProcessGDBRemote::SetThreadStopInfo(
thread_sp->SetStopInfo(StopInfo::CreateStopReasonWithException(
*thread_sp, description.c_str()));
handled = true;
} else if (reason == "replaylog") {
thread_sp->SetStopInfo(StopInfo::CreateStopReasonHistoryBoundary(
*thread_sp, description.c_str()));
handled = true;
} else if (reason == "exec") {
did_exec = true;
thread_sp->SetStopInfo(
Expand Down Expand Up @@ -2318,6 +2365,8 @@ StateType ProcessGDBRemote::SetThreadStopInfo(StringExtractor &stop_packet) {
description = std::string(ostr.GetString());
} else if (key.compare("swbreak") == 0 || key.compare("hwbreak") == 0) {
reason = "breakpoint";
} else if (key.compare("replaylog") == 0) {
reason = "replaylog";
} else if (key.compare("library") == 0) {
auto error = LoadModules();
if (error) {
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class ProcessGDBRemote : public Process,
// Process Control
Status WillResume() override;

Status DoResume() override;
Status DoResume(lldb::RunDirection direction) override;

Status DoHalt(bool &caused_stop) override;

Expand Down
9 changes: 7 additions & 2 deletions lldb/source/Plugins/Process/scripted/ScriptedProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,15 @@ void ScriptedProcess::DidResume() {
m_pid = GetInterface().GetProcessID();
}

Status ScriptedProcess::DoResume() {
Status ScriptedProcess::DoResume(RunDirection direction) {
LLDB_LOGF(GetLog(LLDBLog::Process), "ScriptedProcess::%s resuming process", __FUNCTION__);

return GetInterface().Resume();
if (direction == RunDirection::eRunForward) {
return GetInterface().Resume();
} else {
return Status::FromErrorStringWithFormatv(
"error: {0} does not support reverse execution of processes", GetPluginName());
}
}

Status ScriptedProcess::DoAttach(const ProcessAttachInfo &attach_info) {
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Plugins/Process/scripted/ScriptedProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ScriptedProcess : public Process {

void DidResume() override;

Status DoResume() override;
Status DoResume(lldb::RunDirection direction) override;

Status DoAttachToProcessWithID(lldb::pid_t pid,
const ProcessAttachInfo &attach_info) override;
Expand Down
29 changes: 20 additions & 9 deletions lldb/source/Target/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ Process::Process(lldb::TargetSP target_sp, ListenerSP listener_sp,
m_memory_cache(*this), m_allocated_memory_cache(*this),
m_should_detach(false), m_next_event_action_up(), m_public_run_lock(),
m_private_run_lock(), m_currently_handling_do_on_removals(false),
m_resume_requested(false), m_interrupt_tid(LLDB_INVALID_THREAD_ID),
m_resume_requested(false), m_last_run_direction(eRunForward),
m_interrupt_tid(LLDB_INVALID_THREAD_ID),
m_finalizing(false), m_destructing(false),
m_clear_thread_plans_on_stop(false), m_force_next_event_delivery(false),
m_last_broadcast_state(eStateInvalid), m_destroy_in_process(false),
Expand Down Expand Up @@ -845,6 +846,7 @@ bool Process::HandleProcessStateChangedEvent(
switch (thread_stop_reason) {
case eStopReasonInvalid:
case eStopReasonNone:
case eStopReasonHistoryBoundary:
break;

case eStopReasonSignal: {
Expand Down Expand Up @@ -1352,7 +1354,7 @@ void Process::SetPublicState(StateType new_state, bool restarted) {
}
}

Status Process::Resume() {
Status Process::Resume(RunDirection direction) {
Log *log(GetLog(LLDBLog::State | LLDBLog::Process));
LLDB_LOGF(log, "(plugin = %s) -- locking run lock", GetPluginName().data());
if (!m_public_run_lock.TrySetRunning()) {
Expand All @@ -1361,15 +1363,15 @@ Status Process::Resume() {
return Status::FromErrorString(
"Resume request failed - process still running.");
}
Status error = PrivateResume();
Status error = PrivateResume(direction);
if (!error.Success()) {
// Undo running state change
m_public_run_lock.SetStopped();
}
return error;
}

Status Process::ResumeSynchronous(Stream *stream) {
Status Process::ResumeSynchronous(Stream *stream, RunDirection direction) {
Log *log(GetLog(LLDBLog::State | LLDBLog::Process));
LLDB_LOGF(log, "Process::ResumeSynchronous -- locking run lock");
if (!m_public_run_lock.TrySetRunning()) {
Expand All @@ -1382,7 +1384,7 @@ Status Process::ResumeSynchronous(Stream *stream) {
Listener::MakeListener(ResumeSynchronousHijackListenerName.data()));
HijackProcessEvents(listener_sp);

Status error = PrivateResume();
Status error = PrivateResume(direction);
if (error.Success()) {
StateType state =
WaitForProcessToStop(std::nullopt, nullptr, true, listener_sp, stream,
Expand Down Expand Up @@ -3239,7 +3241,7 @@ Status Process::ConnectRemote(llvm::StringRef remote_url) {
return error;
}

Status Process::PrivateResume() {
Status Process::PrivateResume(RunDirection direction) {
Log *log(GetLog(LLDBLog::Process | LLDBLog::Step));
LLDB_LOGF(log,
"Process::PrivateResume() m_stop_id = %u, public state: %s "
Expand All @@ -3255,6 +3257,15 @@ Status Process::PrivateResume() {
if (!GetModID().IsLastResumeForUserExpression())
ResetExtendedCrashInfoDict();

if (m_last_run_direction != direction) {
// In the future we might want to support mixed-direction plans,
// e.g. a forward step-over stops at a breakpoint, the user does
// a reverse-step, then disables the breakpoint and continues forward.
// This code will need to be changed to support that.
m_thread_list.DiscardThreadPlans();
m_last_run_direction = direction;
}

Status error(WillResume());
// Tell the process it is about to resume before the thread list
if (error.Success()) {
Expand All @@ -3272,7 +3283,7 @@ Status Process::PrivateResume() {
"Process::PrivateResume PreResumeActions failed, not resuming.");
} else {
m_mod_id.BumpResumeID();
error = DoResume();
error = DoResume(direction);
if (error.Success()) {
DidResume();
m_thread_list.DidResume();
Expand Down Expand Up @@ -3735,7 +3746,7 @@ bool Process::ShouldBroadcastEvent(Event *event_ptr) {
"from state: %s",
static_cast<void *>(event_ptr), StateAsCString(state));
ProcessEventData::SetRestartedInEvent(event_ptr, true);
PrivateResume();
PrivateResume(m_last_run_direction);
}
} else {
return_value = true;
Expand Down Expand Up @@ -4346,7 +4357,7 @@ void Process::ProcessEventData::DoOnRemoval(Event *event_ptr) {
SetRestarted(true);
// Use the private resume method here, since we aren't changing the run
// lock state.
process_sp->PrivateResume();
process_sp->PrivateResume(process_sp->m_last_run_direction);
} else {
bool hijacked = process_sp->IsHijackedForEvent(eBroadcastBitStateChanged) &&
!process_sp->StateChangedIsHijackedForSynchronousResume();
Expand Down
29 changes: 29 additions & 0 deletions lldb/source/Target/StopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,30 @@ class StopInfoProcessorTrace : public StopInfo {
}
};

// StopInfoHistoryBoundary

class StopInfoHistoryBoundary : public StopInfo {
public:
StopInfoHistoryBoundary(Thread &thread, const char *description)
: StopInfo(thread, LLDB_INVALID_UID) {
if (description)
SetDescription(description);
}

~StopInfoHistoryBoundary() override = default;

StopReason GetStopReason() const override {
return eStopReasonHistoryBoundary;
}

const char *GetDescription() override {
if (m_description.empty())
return "history boundary";
else
return m_description.c_str();
}
};

// StopInfoThreadPlan

class StopInfoThreadPlan : public StopInfo {
Expand Down Expand Up @@ -1439,6 +1463,11 @@ StopInfoSP StopInfo::CreateStopReasonProcessorTrace(Thread &thread,
return StopInfoSP(new StopInfoProcessorTrace(thread, description));
}

StopInfoSP StopInfo::CreateStopReasonHistoryBoundary(Thread &thread,
const char *description) {
return StopInfoSP(new StopInfoHistoryBoundary(thread, description));
}

StopInfoSP StopInfo::CreateStopReasonWithExec(Thread &thread) {
return StopInfoSP(new StopInfoExec(thread));
}
Expand Down
8 changes: 6 additions & 2 deletions lldb/source/Target/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -624,10 +624,12 @@ void Thread::SetupForResume() {
// what the current plan is.

lldb::RegisterContextSP reg_ctx_sp(GetRegisterContext());
if (reg_ctx_sp) {
ProcessSP process_sp(GetProcess());
if (reg_ctx_sp && process_sp &&
process_sp->GetLastRunDirection() == eRunForward) {
const addr_t thread_pc = reg_ctx_sp->GetPC();
BreakpointSiteSP bp_site_sp =
GetProcess()->GetBreakpointSiteList().FindByAddress(thread_pc);
process_sp->GetBreakpointSiteList().FindByAddress(thread_pc);
if (bp_site_sp) {
// Note, don't assume there's a ThreadPlanStepOverBreakpoint, the
// target may not require anything special to step over a breakpoint.
Expand Down Expand Up @@ -1732,6 +1734,8 @@ std::string Thread::StopReasonAsString(lldb::StopReason reason) {
return "processor trace";
case eStopReasonInterrupt:
return "async interrupt";
case eStopReasonHistoryBoundary:
return "history boundary";
}

return "StopReason = " + std::to_string(reason);
Expand Down
3 changes: 3 additions & 0 deletions lldb/test/API/functionalities/reverse-execution/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
C_SOURCES := main.c

include Makefile.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import lldb
import time
import unittest
from lldbsuite.test.lldbtest import *
from lldbsuite.test.decorators import *
from lldbsuite.test.gdbclientutils import *
from lldbsuite.test.lldbreverse import ReverseTestBase
from lldbsuite.test import lldbutil


class TestReverseContinueBreakpoints(ReverseTestBase):
NO_DEBUG_INFO_TESTCASE = True

@skipIfDarwin # No Darwin ProcessNative impl for lldb-server
def test_reverse_continue(self):
self.reverse_continue_internal(async_mode=False)

@skipIfDarwin # No Darwin ProcessNative impl for lldb-server
def test_reverse_continue_async(self):
self.reverse_continue_internal(async_mode=True)

def reverse_continue_internal(self, async_mode):
target, process, initial_threads = self.setup_recording(async_mode)

# Reverse-continue. We'll stop at the point where we started recording.
status = process.Continue(lldb.eRunReverse)
self.assertSuccess(status)
self.expect_async_state_changes(async_mode, process, [lldb.eStateRunning, lldb.eStateStopped])
self.expect(
"thread list",
STOPPED_DUE_TO_HISTORY_BOUNDARY,
substrs=["stopped", "stop reason = history boundary"],
)

# Continue forward normally until the target exits.
status = process.Continue()
self.expect_async_state_changes(async_mode, process, [lldb.eStateRunning, lldb.eStateExited])
self.assertSuccess(status)
self.assertState(process.GetState(), lldb.eStateExited)
self.assertEqual(process.GetExitStatus(), 0)

@skipIfDarwin # No Darwin ProcessNative impl for lldb-server
def test_reverse_continue_breakpoint(self):
self.reverse_continue_breakpoint_internal(async_mode=False)

@skipIfDarwin # No Darwin ProcessNative impl for lldb-server
def test_reverse_continue_breakpoint_async(self):
self.reverse_continue_breakpoint_internal(async_mode=True)

def reverse_continue_breakpoint_internal(self, async_mode):
target, process, initial_threads = self.setup_recording(async_mode)

# Reverse-continue to the function "trigger_breakpoint".
trigger_bkpt = target.BreakpointCreateByName("trigger_breakpoint", None)
status = process.Continue(lldb.eRunReverse)
self.assertSuccess(status)
self.expect_async_state_changes(async_mode, process, [lldb.eStateRunning, lldb.eStateStopped])
threads_now = lldbutil.get_threads_stopped_at_breakpoint(process, trigger_bkpt)
self.assertEqual(threads_now, initial_threads)

@skipIfDarwin # No Darwin ProcessNative impl for lldb-server
def test_reverse_continue_skip_breakpoint(self):
self.reverse_continue_skip_breakpoint_internal(async_mode=False)

@skipIfDarwin # No Darwin ProcessNative impl for lldb-server
def test_reverse_continue_skip_breakpoint_async(self):
self.reverse_continue_skip_breakpoint_internal(async_mode=True)

def reverse_continue_skip_breakpoint_internal(self, async_mode):
target, process, initial_threads = self.setup_recording(async_mode)

# Reverse-continue over a breakpoint at "trigger_breakpoint" whose
# condition is false.
# This tests that we continue in the correct direction after hitting
# the breakpoint.
trigger_bkpt = target.BreakpointCreateByName("trigger_breakpoint", None)
trigger_bkpt.SetCondition("false_condition")
status = process.Continue(lldb.eRunReverse)
self.expect_async_state_changes(async_mode, process, [lldb.eStateRunning, lldb.eStateStopped])
self.assertSuccess(status)
self.expect(
"thread list",
STOPPED_DUE_TO_HISTORY_BOUNDARY,
substrs=["stopped", "stop reason = history boundary"],
)

def setup_recording(self, async_mode):
"""
Record execution of code between "start_recording" and "stop_recording" breakpoints.

Returns with the target stopped at "stop_recording", with recording disabled,
ready to reverse-execute.
"""
self.build()
target = self.dbg.CreateTarget("")
process = self.connect(target)

# Record execution from the start of the function "start_recording"
# to the start of the function "stop_recording". We want to keep the
# interval that we record as small as possible to minimize the run-time
# of our single-stepping recorder.
start_recording_bkpt = target.BreakpointCreateByName("start_recording", None)
initial_threads = lldbutil.continue_to_breakpoint(process, start_recording_bkpt)
self.assertEqual(len(initial_threads), 1)
target.BreakpointDelete(start_recording_bkpt.GetID())
self.start_recording()
stop_recording_bkpt = target.BreakpointCreateByName("stop_recording", None)
lldbutil.continue_to_breakpoint(process, stop_recording_bkpt)
target.BreakpointDelete(stop_recording_bkpt.GetID())
self.stop_recording()

self.dbg.SetAsync(async_mode)
self.expect_async_state_changes(async_mode, process, [lldb.eStateStopped])

return target, process, initial_threads

def expect_async_state_changes(self, async_mode, process, states):
if not async_mode:
return
listener = self.dbg.GetListener()
lldbutil.expect_state_changes(self, listener, process, states)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import lldb
import unittest
from lldbsuite.test.lldbtest import *
from lldbsuite.test.decorators import *
from lldbsuite.test import lldbutil


class TestReverseContinueNotSupported(TestBase):
NO_DEBUG_INFO_TESTCASE = True

@skipIfDarwin # No Darwin ProcessNative impl for lldb-server
def test_reverse_continue_not_supported(self):
self.build()
exe = self.getBuildArtifact("a.out")
target = self.dbg.CreateTarget(exe)
self.assertTrue(target, VALID_TARGET)

main_bkpt = target.BreakpointCreateByName("main", None)
self.assertTrue(main_bkpt, VALID_BREAKPOINT)

process = target.LaunchSimple(None, None, self.get_process_working_directory())
self.assertTrue(process, PROCESS_IS_VALID)

# This will fail gracefully.
status = process.Continue(lldb.eRunReverse)
self.assertFailure(status, "target does not support reverse-continue")

status = process.Continue()
self.assertSuccess(status)
self.assertState(process.GetState(), lldb.eStateExited)
self.assertEqual(process.GetExitStatus(), 0)
14 changes: 14 additions & 0 deletions lldb/test/API/functionalities/reverse-execution/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
volatile int false_condition = 0;

static void start_recording() {}

static void trigger_breakpoint() {}

static void stop_recording() {}

int main() {
start_recording();
trigger_breakpoint();
stop_recording();
return 0;
}
3 changes: 3 additions & 0 deletions lldb/tools/lldb-dap/JSONUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,9 @@ llvm::json::Value CreateThreadStopped(lldb::SBThread &thread,
case lldb::eStopReasonProcessorTrace:
body.try_emplace("reason", "processor trace");
break;
case lldb::eStopReasonHistoryBoundary:
body.try_emplace("reason", "history boundary");
break;
case lldb::eStopReasonSignal:
case lldb::eStopReasonException:
body.try_emplace("reason", "exception");
Expand Down
1 change: 1 addition & 0 deletions lldb/tools/lldb-dap/LLDBUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ bool ThreadHasStopReason(lldb::SBThread &thread) {
case lldb::eStopReasonVFork:
case lldb::eStopReasonVForkDone:
case lldb::eStopReasonInterrupt:
case lldb::eStopReasonHistoryBoundary:
return true;
case lldb::eStopReasonThreadExiting:
case lldb::eStopReasonInvalid:
Expand Down