Skip to content

[lldb-dap] Support the Module Event #137380

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 3 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def __init__(self, recv, send, init_commands, log_file=None):
self.breakpoint_events = []
self.progress_events = []
self.reverse_requests = []
self.module_events = []
self.sequence = 1
self.threads = None
self.recv_thread.start()
Expand Down Expand Up @@ -255,6 +256,11 @@ def handle_recv_packet(self, packet):
# and 'progressEnd' events. Keep these around in case test
# cases want to verify them.
self.progress_events.append(packet)
elif event == "module":
# Module events indicate that some information about a module has changed.
self.module_events.append(packet)
# no need to add 'module' event packets to our packets list
return keepGoing

elif packet_type == "response":
if packet["command"] == "disconnect":
Expand Down
21 changes: 21 additions & 0 deletions lldb/test/API/tools/lldb-dap/module/TestDAP_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ def checkSymbolsLoadedWithSize():
self.assertEqual(program, program_module["path"])
self.assertIn("addressRange", program_module)

# Collect all the module names we saw as events.
module_new_names = []
module_changed_names = []
for module_event in self.dap_server.module_events:
module_name = module_event["body"]["module"]["name"]
reason = module_event["body"]["reason"]
if reason == "new":
module_new_names.append(module_name)
elif reason == "changed":
module_changed_names.append(module_name)

# Make sure we got an event for every active module.
self.assertNotEqual(len(module_new_names), 0)
for module in active_modules:
self.assertIn(module, module_new_names)

# Make sure we got an update event for the program module when the
# symbols got added.
self.assertNotEqual(len(module_changed_names), 0)
self.assertIn(program_module["name"], module_changed_names)

@skipIfWindows
def test_modules(self):
"""
Expand Down
6 changes: 5 additions & 1 deletion lldb/tools/lldb-dap/DAP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,11 @@ void DAP::SetTarget(const lldb::SBTarget target) {
lldb::SBListener listener = this->debugger.GetListener();
listener.StartListeningForEvents(
this->target.GetBroadcaster(),
lldb::SBTarget::eBroadcastBitBreakpointChanged);
lldb::SBTarget::eBroadcastBitBreakpointChanged |
lldb::SBTarget::eBroadcastBitModulesLoaded |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should handle when the symbols in the module changes eBroadcastBitSymbolsLoaded and eBroadcastBitSymbolsChanged since symbol changes are reported when CreateModule is called

This corresponds to the module event reason changed

lldb::SBTarget::eBroadcastBitModulesUnloaded |
lldb::SBTarget::eBroadcastBitSymbolsLoaded |
lldb::SBTarget::eBroadcastBitSymbolsChanged);
listener.StartListeningForEvents(this->broadcaster,
eBroadcastBitStopEventThread);
}
Expand Down
33 changes: 33 additions & 0 deletions lldb/tools/lldb-dap/Handler/InitializeRequestHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "lldb/API/SBEvent.h"
#include "lldb/API/SBListener.h"
#include "lldb/API/SBStream.h"
#include "lldb/API/SBTarget.h"
#include <cstdint>

using namespace lldb;
using namespace lldb_dap::protocol;
Expand Down Expand Up @@ -108,6 +110,16 @@ void ProgressEventThreadFunction(DAP &dap) {
}
}

static llvm::StringRef GetModuleEventReason(uint32_t event_mask) {
if (event_mask & lldb::SBTarget::eBroadcastBitModulesLoaded)
return "new";
if (event_mask & lldb::SBTarget::eBroadcastBitModulesUnloaded)
return "removed";
assert(event_mask & lldb::SBTarget::eBroadcastBitSymbolsLoaded ||
event_mask & lldb::SBTarget::eBroadcastBitSymbolsChanged);
return "changed";
}

// 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
Expand Down Expand Up @@ -193,6 +205,27 @@ static void EventThreadFunction(DAP &dap) {
(event_mask & lldb::SBProcess::eBroadcastBitSTDERR)) {
SendStdOutStdErr(dap, process);
}
} else if (lldb::SBTarget::EventIsTargetEvent(event)) {
if (event_mask & lldb::SBTarget::eBroadcastBitModulesLoaded ||
event_mask & lldb::SBTarget::eBroadcastBitModulesUnloaded ||
event_mask & lldb::SBTarget::eBroadcastBitSymbolsLoaded ||
event_mask & lldb::SBTarget::eBroadcastBitSymbolsChanged) {
llvm::StringRef reason = GetModuleEventReason(event_mask);
const uint32_t num_modules = SBTarget::GetNumModulesFromEvent(event);
for (uint32_t i = 0; i < num_modules; ++i) {
lldb::SBModule module =
SBTarget::GetModuleAtIndexFromEvent(i, event);
if (!module.IsValid())
continue;

llvm::json::Object body;
body.try_emplace("reason", reason);
body.try_emplace("module", CreateModule(dap.target, module));
llvm::json::Object module_event = CreateEventObject("module");
module_event.try_emplace("body", std::move(body));
dap.SendJSON(llvm::json::Value(std::move(module_event)));
}
}
} else if (lldb::SBBreakpoint::EventIsBreakpointEvent(event)) {
if (event_mask & lldb::SBTarget::eBroadcastBitBreakpointChanged) {
auto event_type =
Expand Down
Loading