Skip to content

[lldb-dap] Add an RAII helper for synchronous mode (NFC) #137900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lldb/tools/lldb-dap/DAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,12 +820,12 @@ llvm::Error DAP::Disconnect(bool terminateDebuggee) {
case lldb::eStateCrashed:
case lldb::eStateSuspended:
case lldb::eStateStopped:
case lldb::eStateRunning:
debugger.SetAsync(false);
case lldb::eStateRunning: {
ScopeSyncMode scope_sync_mode(debugger);
error = terminateDebuggee ? process.Kill() : process.Detach();
debugger.SetAsync(true);
break;
}
}

SendTerminatedEvent();

Expand Down
10 changes: 6 additions & 4 deletions lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "DAP.h"
#include "EventHelper.h"
#include "JSONUtils.h"
#include "LLDBUtils.h"
#include "RequestHandler.h"
#include "lldb/API/SBListener.h"
#include "llvm/Support/FileSystem.h"
Expand Down Expand Up @@ -138,9 +139,11 @@ void AttachRequestHandler::operator()(const llvm::json::Object &request) const {
}
if (attachCommands.empty()) {
// No "attachCommands", just attach normally.

// Disable async events so the attach will be successful when we return from
// the launch call and the launch will happen synchronously
dap.debugger.SetAsync(false);
ScopeSyncMode scope_sync_mode(dap.debugger);

if (core_file.empty()) {
if ((pid != LLDB_INVALID_PROCESS_ID) &&
(gdb_remote_port != invalid_port)) {
Expand All @@ -161,10 +164,9 @@ void AttachRequestHandler::operator()(const llvm::json::Object &request) const {
// Attach by process name or id.
dap.target.Attach(attach_info, error);
}
} else
} else {
dap.target.LoadCore(core_file.data(), error);
// Reenable async events
dap.debugger.SetAsync(true);
}
} else {
// We have "attachCommands" that are a set of commands that are expected
// to execute the commands after which a process should be created. If there
Expand Down
8 changes: 4 additions & 4 deletions lldb/tools/lldb-dap/Handler/RequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "DAP.h"
#include "Handler/ResponseHandler.h"
#include "JSONUtils.h"
#include "LLDBUtils.h"
#include "Protocol/ProtocolBase.h"
#include "Protocol/ProtocolRequests.h"
#include "RunInTerminal.h"
Expand Down Expand Up @@ -138,7 +139,7 @@ RunInTerminal(DAP &dap, const protocol::LaunchRequestArguments &arguments) {
else
return pid.takeError();

dap.debugger.SetAsync(false);
std::optional<ScopeSyncMode> scope_sync_mode(dap.debugger);
lldb::SBError error;
dap.target.Attach(attach_info, error);

Expand All @@ -162,7 +163,7 @@ RunInTerminal(DAP &dap, const protocol::LaunchRequestArguments &arguments) {

// Now that the actual target is just starting (i.e. exec was just invoked),
// we return the debugger to its async state.
dap.debugger.SetAsync(true);
scope_sync_mode.reset();

// If sending the notification failed, the launcher should be dead by now and
// the async didAttach notification should have an error message, so we
Expand Down Expand Up @@ -244,9 +245,8 @@ llvm::Error BaseRequestHandler::LaunchProcess(
lldb::SBError error;
// Disable async events so the launch will be successful when we return from
// the launch call and the launch will happen synchronously
dap.debugger.SetAsync(false);
ScopeSyncMode scope_sync_mode(dap.debugger);
dap.target.Launch(launch_info, error);
dap.debugger.SetAsync(true);
if (error.Fail())
return llvm::make_error<DAPError>(error.GetCString());
} else {
Expand Down
4 changes: 2 additions & 2 deletions lldb/tools/lldb-dap/Handler/RestartRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "DAP.h"
#include "EventHelper.h"
#include "JSONUtils.h"
#include "LLDBUtils.h"
#include "Protocol/ProtocolRequests.h"
#include "RequestHandler.h"
#include "llvm/Support/JSON.h"
Expand Down Expand Up @@ -121,8 +122,8 @@ void RestartRequestHandler::operator()(
// Stop the current process if necessary. The logic here is similar to
// CommandObjectProcessLaunchOrAttach::StopProcessIfNecessary, except that
// we don't ask the user for confirmation.
dap.debugger.SetAsync(false);
if (process.IsValid()) {
ScopeSyncMode scope_sync_mode(dap.debugger);
lldb::StateType state = process.GetState();
if (state != lldb::eStateConnected) {
process.Kill();
Expand All @@ -131,7 +132,6 @@ void RestartRequestHandler::operator()(
// for threads of the process we are terminating.
dap.thread_ids.clear();
}
dap.debugger.SetAsync(true);

// FIXME: Should we run 'preRunCommands'?
// FIXME: Should we add a 'preRestartCommands'?
Expand Down
7 changes: 7 additions & 0 deletions lldb/tools/lldb-dap/LLDBUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,11 @@ std::string GetStringValue(const lldb::SBStructuredData &data) {
return str;
}

ScopeSyncMode::ScopeSyncMode(lldb::SBDebugger &debugger)
: m_debugger(debugger), m_async(m_debugger.GetAsync()) {
m_debugger.SetAsync(false);
}

ScopeSyncMode::~ScopeSyncMode() { m_debugger.SetAsync(m_async); }

} // namespace lldb_dap
12 changes: 11 additions & 1 deletion lldb/tools/lldb-dap/LLDBUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ class TelemetryDispatcher {
lldb::SBDebugger *debugger;
};

/// RAII utility to put the debugger temporarily into synchronous mode.
class ScopeSyncMode {
public:
ScopeSyncMode(lldb::SBDebugger &debugger);
~ScopeSyncMode();

private:
lldb::SBDebugger &m_debugger;
bool m_async;
};

/// Get the stop-disassembly-display settings
///
/// \param[in] debugger
Expand All @@ -207,7 +218,6 @@ class TelemetryDispatcher {
/// The value of the stop-disassembly-display setting
lldb::StopDisassemblyType GetStopDisassemblyDisplay(lldb::SBDebugger &debugger);


/// Take ownership of the stored error.
llvm::Error ToError(const lldb::SBError &error);

Expand Down
Loading