Skip to content

[lldb-dap] Listen for broadcast classes. #140142

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
53 changes: 26 additions & 27 deletions lldb/tools/lldb-dap/DAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "lldb/API/SBListener.h"
#include "lldb/API/SBProcess.h"
#include "lldb/API/SBStream.h"
#include "lldb/API/SBTarget.h"
#include "lldb/Utility/IOObject.h"
#include "lldb/Utility/Status.h"
#include "lldb/lldb-defines.h"
Expand Down Expand Up @@ -719,23 +720,7 @@ lldb::SBTarget DAP::CreateTarget(lldb::SBError &error) {
return target;
}

void DAP::SetTarget(const lldb::SBTarget target) {
this->target = target;

if (target.IsValid()) {
// Configure breakpoint event listeners for the target.
lldb::SBListener listener = this->debugger.GetListener();
listener.StartListeningForEvents(
this->target.GetBroadcaster(),
lldb::SBTarget::eBroadcastBitBreakpointChanged |
lldb::SBTarget::eBroadcastBitModulesLoaded |
lldb::SBTarget::eBroadcastBitModulesUnloaded |
lldb::SBTarget::eBroadcastBitSymbolsLoaded |
lldb::SBTarget::eBroadcastBitSymbolsChanged);
listener.StartListeningForEvents(this->broadcaster,
eBroadcastBitStopEventThread);
}
}
void DAP::SetTarget(const lldb::SBTarget target) { this->target = target; }

bool DAP::HandleObject(const Message &M) {
TelemetryDispatcher dispatcher(&debugger);
Expand Down Expand Up @@ -1489,17 +1474,31 @@ void DAP::ProgressEventThread() {
}

// All events from the debugger, target, process, thread and frames are
// received in this function that runs in its own thread. We are using a
// "FILE *" to output packets back to VS Code and they have mutexes in them
// them prevent multiple threads from writing simultaneously so no locking
// is required.
// received in this function that runs in its own thread.
void DAP::EventThread() {
llvm::set_thread_name(transport.GetClientName() + ".event_handler");
lldb::SBEvent event;

// Configure the debugger listener for all events lldb-dap is interested in.
lldb::SBListener listener = debugger.GetListener();
broadcaster.AddListener(listener, eBroadcastBitStopEventThread);
debugger.GetBroadcaster().AddListener(
listener, lldb::eBroadcastBitError | lldb::eBroadcastBitWarning);
listener.StartListeningForEventClass(
debugger, lldb::SBTarget::GetBroadcasterClassName(),
lldb::SBTarget::eBroadcastBitBreakpointChanged |
lldb::SBTarget::eBroadcastBitModulesLoaded |
lldb::SBTarget::eBroadcastBitModulesUnloaded |
lldb::SBTarget::eBroadcastBitSymbolsLoaded |
lldb::SBTarget::eBroadcastBitSymbolsChanged);
listener.StartListeningForEventClass(
debugger, lldb::SBProcess::GetBroadcasterClassName(),
lldb::SBProcess::eBroadcastBitStateChanged |
lldb::SBProcess::eBroadcastBitSTDOUT |
lldb::SBProcess::eBroadcastBitSTDERR);
listener.StartListeningForEvents(debugger.GetBroadcaster(),
lldb::SBDebugger::eBroadcastBitError |
lldb::SBDebugger::eBroadcastBitWarning);
// Listen for the lldb-dap stop event.
listener.StartListeningForEvents(broadcaster, eBroadcastBitStopEventThread);

lldb::SBEvent event;
bool done = false;
while (!done) {
if (listener.WaitForEvent(1, event)) {
Expand Down Expand Up @@ -1633,8 +1632,8 @@ void DAP::EventThread() {
SendJSON(llvm::json::Value(std::move(bp_event)));
}
}
} else if (event_mask & lldb::eBroadcastBitError ||
event_mask & lldb::eBroadcastBitWarning) {
} else if (event_mask & lldb::SBDebugger::eBroadcastBitError ||
event_mask & lldb::SBDebugger::eBroadcastBitWarning) {
lldb::SBStructuredData data =
lldb::SBDebugger::GetDiagnosticFromEvent(event);
if (!data.IsValid())
Expand Down
3 changes: 1 addition & 2 deletions lldb/tools/lldb-dap/DAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,7 @@ struct DAP {
/// An SBTarget object.
lldb::SBTarget CreateTarget(lldb::SBError &error);

/// Set given target object as a current target for lldb-dap and start
/// listeing for its breakpoint events.
/// Set given target object as a current target for lldb-dap.
void SetTarget(const lldb::SBTarget target);

bool HandleObject(const protocol::Message &M);
Expand Down
4 changes: 3 additions & 1 deletion lldb/tools/lldb-dap/Handler/AttachRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ Error AttachRequestHandler::Run(const AttachRequestArguments &args) const {
if (llvm::Error err = dap.RunAttachCommands(args.attachCommands))
return err;

dap.target = dap.debugger.GetSelectedTarget();
// The custom commands might have created a new target so we should use
// the selected target after these commands are run.
dap.SetTarget(dap.debugger.GetSelectedTarget());

// Validate the attachCommand results.
if (!dap.target.GetProcess().IsValid())
Expand Down
2 changes: 1 addition & 1 deletion lldb/tools/lldb-dap/Handler/RequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ llvm::Error BaseRequestHandler::LaunchProcess(

// The custom commands might have created a new target so we should use
// the selected target after these commands are run.
dap.target = dap.debugger.GetSelectedTarget();
dap.SetTarget(dap.debugger.GetSelectedTarget());
}
}

Expand Down
Loading