Skip to content

Commit b7c449a

Browse files
authored
[lldb-dap] Don't emit a removed module event for unseen modules (llvm#139324)
1 parent 0b9c63d commit b7c449a

File tree

11 files changed

+158
-27
lines changed

11 files changed

+158
-27
lines changed

lldb/packages/Python/lldbsuite/test/tools/lldb-dap/dap_server.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ def __init__(self, recv, send, init_commands, log_file=None):
134134
self.thread_stop_reasons = {}
135135
self.progress_events = []
136136
self.reverse_requests = []
137-
self.module_events = []
138137
self.sequence = 1
139138
self.threads = None
140139
self.recv_thread.start()
@@ -248,11 +247,6 @@ def handle_recv_packet(self, packet):
248247
# and 'progressEnd' events. Keep these around in case test
249248
# cases want to verify them.
250249
self.progress_events.append(packet)
251-
elif event == "module":
252-
# Module events indicate that some information about a module has changed.
253-
self.module_events.append(packet)
254-
# no need to add 'module' event packets to our packets list
255-
return keepGoing
256250

257251
elif packet_type == "response":
258252
if packet["command"] == "disconnect":
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CXX_SOURCES := main.cpp
2+
LD_EXTRAS := -Wl,-rpath "-Wl,$(shell pwd)"
3+
USE_LIBDL :=1
4+
5+
a.out: libother
6+
7+
include Makefile.rules
8+
9+
# The following shared library will be used to test breakpoints under dynamic loading
10+
libother: other.c
11+
"$(MAKE)" -f $(MAKEFILE_RULES) \
12+
DYLIB_ONLY=YES DYLIB_C_SOURCES=other.c DYLIB_NAME=other
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import dap_server
2+
from lldbsuite.test.decorators import *
3+
from lldbsuite.test.lldbtest import *
4+
from lldbsuite.test import lldbutil
5+
import lldbdap_testcase
6+
import re
7+
8+
9+
class TestDAP_module_event(lldbdap_testcase.DAPTestCaseBase):
10+
def test_module_event(self):
11+
program = self.getBuildArtifact("a.out")
12+
self.build_and_launch(program, stopOnEntry=True)
13+
14+
source = "main.cpp"
15+
breakpoint1_line = line_number(source, "// breakpoint 1")
16+
breakpoint2_line = line_number(source, "// breakpoint 2")
17+
breakpoint3_line = line_number(source, "// breakpoint 3")
18+
19+
breakpoint_ids = self.set_source_breakpoints(
20+
source, [breakpoint1_line, breakpoint2_line, breakpoint3_line]
21+
)
22+
self.continue_to_breakpoints(breakpoint_ids)
23+
24+
# We're now stopped at breakpoint 1 before the dlopen. Flush all the module events.
25+
event = self.dap_server.wait_for_event("module", 0.25)
26+
while event is not None:
27+
event = self.dap_server.wait_for_event("module", 0.25)
28+
29+
# Continue to the second breakpoint, before the dlclose.
30+
self.continue_to_breakpoints(breakpoint_ids)
31+
32+
# Make sure we got a module event for libother.
33+
event = self.dap_server.wait_for_event("module", 5)
34+
self.assertTrue(event, "didn't get a module event")
35+
module_name = event["body"]["module"]["name"]
36+
module_id = event["body"]["module"]["id"]
37+
self.assertEqual(event["body"]["reason"], "new")
38+
self.assertIn("libother", module_name)
39+
40+
# Continue to the third breakpoint, after the dlclose.
41+
self.continue_to_breakpoints(breakpoint_ids)
42+
43+
# Make sure we got a module event for libother.
44+
event = self.dap_server.wait_for_event("module", 5)
45+
self.assertTrue(event, "didn't get a module event")
46+
reason = event["body"]["reason"]
47+
self.assertEqual(event["body"]["reason"], "removed")
48+
self.assertEqual(event["body"]["module"]["id"], module_id)
49+
50+
# The removed module event should omit everything but the module id.
51+
# Check that there's no module name in the event.
52+
self.assertNotIn("name", event["body"]["module"])
53+
54+
self.continue_to_exit()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <dlfcn.h>
2+
#include <stdio.h>
3+
4+
int main(int argc, char const *argv[]) {
5+
6+
#if defined(__APPLE__)
7+
const char *libother_name = "libother.dylib";
8+
#else
9+
const char *libother_name = "libother.so";
10+
#endif
11+
12+
printf("before dlopen\n"); // breakpoint 1
13+
void *handle = dlopen(libother_name, RTLD_NOW);
14+
int (*foo)(int) = (int (*)(int))dlsym(handle, "foo");
15+
foo(12);
16+
17+
printf("before dlclose\n"); // breakpoint 2
18+
dlclose(handle);
19+
printf("after dlclose\n"); // breakpoint 3
20+
21+
return 0; // breakpoint 1
22+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern int foo(int x) {
2+
int y = x + 42; // break other
3+
int z = y + 42;
4+
return z;
5+
}

lldb/test/API/tools/lldb-dap/module/TestDAP_module.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ def checkSymbolsLoadedWithSize():
6060
# Collect all the module names we saw as events.
6161
module_new_names = []
6262
module_changed_names = []
63-
for module_event in self.dap_server.module_events:
64-
module_name = module_event["body"]["module"]["name"]
63+
module_event = self.dap_server.wait_for_event("module", 1)
64+
while module_event is not None:
6565
reason = module_event["body"]["reason"]
6666
if reason == "new":
67-
module_new_names.append(module_name)
67+
module_new_names.append(module_event["body"]["module"]["name"])
6868
elif reason == "changed":
69-
module_changed_names.append(module_name)
69+
module_changed_names.append(module_event["body"]["module"]["name"])
70+
71+
module_event = self.dap_server.wait_for_event("module", 1)
7072

7173
# Make sure we got an event for every active module.
7274
self.assertNotEqual(len(module_new_names), 0)

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,6 @@ static uint64_t GetUintFromStructuredData(lldb::SBStructuredData &data,
106106
return keyValue.GetUnsignedIntegerValue();
107107
}
108108

109-
static llvm::StringRef GetModuleEventReason(uint32_t event_mask) {
110-
if (event_mask & lldb::SBTarget::eBroadcastBitModulesLoaded)
111-
return "new";
112-
if (event_mask & lldb::SBTarget::eBroadcastBitModulesUnloaded)
113-
return "removed";
114-
assert(event_mask & lldb::SBTarget::eBroadcastBitSymbolsLoaded ||
115-
event_mask & lldb::SBTarget::eBroadcastBitSymbolsChanged);
116-
return "changed";
117-
}
118-
119109
/// Return string with first character capitalized.
120110
static std::string capitalize(llvm::StringRef str) {
121111
if (str.empty())
@@ -1571,18 +1561,41 @@ void DAP::EventThread() {
15711561
event_mask & lldb::SBTarget::eBroadcastBitModulesUnloaded ||
15721562
event_mask & lldb::SBTarget::eBroadcastBitSymbolsLoaded ||
15731563
event_mask & lldb::SBTarget::eBroadcastBitSymbolsChanged) {
1574-
llvm::StringRef reason = GetModuleEventReason(event_mask);
15751564
const uint32_t num_modules =
15761565
lldb::SBTarget::GetNumModulesFromEvent(event);
1566+
std::lock_guard<std::mutex> guard(modules_mutex);
15771567
for (uint32_t i = 0; i < num_modules; ++i) {
15781568
lldb::SBModule module =
15791569
lldb::SBTarget::GetModuleAtIndexFromEvent(i, event);
15801570
if (!module.IsValid())
15811571
continue;
1572+
llvm::StringRef module_id = module.GetUUIDString();
1573+
if (module_id.empty())
1574+
continue;
1575+
1576+
llvm::StringRef reason;
1577+
bool id_only = false;
1578+
if (event_mask & lldb::SBTarget::eBroadcastBitModulesLoaded) {
1579+
modules.insert(module_id);
1580+
reason = "new";
1581+
} else {
1582+
// If this is a module we've never told the client about, don't
1583+
// send an event.
1584+
if (!modules.contains(module_id))
1585+
continue;
1586+
1587+
if (event_mask & lldb::SBTarget::eBroadcastBitModulesUnloaded) {
1588+
modules.erase(module_id);
1589+
reason = "removed";
1590+
id_only = true;
1591+
} else {
1592+
reason = "changed";
1593+
}
1594+
}
15821595

15831596
llvm::json::Object body;
15841597
body.try_emplace("reason", reason);
1585-
body.try_emplace("module", CreateModule(target, module));
1598+
body.try_emplace("module", CreateModule(target, module, id_only));
15861599
llvm::json::Object module_event = CreateEventObject("module");
15871600
module_event.try_emplace("body", std::move(body));
15881601
SendJSON(llvm::json::Value(std::move(module_event)));

lldb/tools/lldb-dap/DAP.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "llvm/ADT/SmallSet.h"
4040
#include "llvm/ADT/StringMap.h"
4141
#include "llvm/ADT/StringRef.h"
42+
#include "llvm/ADT/StringSet.h"
4243
#include "llvm/Support/Error.h"
4344
#include "llvm/Support/JSON.h"
4445
#include "llvm/Support/Threading.h"
@@ -212,6 +213,13 @@ struct DAP {
212213
/// The initial thread list upon attaching.
213214
std::optional<llvm::json::Array> initial_thread_list;
214215

216+
/// Keep track of all the modules our client knows about: either through the
217+
/// modules request or the module events.
218+
/// @{
219+
std::mutex modules_mutex;
220+
llvm::StringSet<> modules;
221+
/// @}
222+
215223
/// Creates a new DAP sessions.
216224
///
217225
/// \param[in] log

lldb/tools/lldb-dap/Handler/ModulesRequestHandler.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,20 @@ void ModulesRequestHandler::operator()(
4545
FillResponse(request, response);
4646

4747
llvm::json::Array modules;
48-
for (size_t i = 0; i < dap.target.GetNumModules(); i++) {
49-
lldb::SBModule module = dap.target.GetModuleAtIndex(i);
50-
modules.emplace_back(CreateModule(dap.target, module));
48+
49+
{
50+
std::lock_guard<std::mutex> guard(dap.modules_mutex);
51+
for (size_t i = 0; i < dap.target.GetNumModules(); i++) {
52+
lldb::SBModule module = dap.target.GetModuleAtIndex(i);
53+
if (!module.IsValid())
54+
continue;
55+
56+
llvm::StringRef module_id = module.GetUUIDString();
57+
if (!module_id.empty())
58+
dap.modules.insert(module_id);
59+
60+
modules.emplace_back(CreateModule(dap.target, module));
61+
}
5162
}
5263

5364
llvm::json::Object body;

lldb/tools/lldb-dap/JSONUtils.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,18 @@ static std::string ConvertDebugInfoSizeToString(uint64_t debug_info) {
395395
return oss.str();
396396
}
397397

398-
llvm::json::Value CreateModule(lldb::SBTarget &target, lldb::SBModule &module) {
398+
llvm::json::Value CreateModule(lldb::SBTarget &target, lldb::SBModule &module,
399+
bool id_only) {
399400
llvm::json::Object object;
400401
if (!target.IsValid() || !module.IsValid())
401402
return llvm::json::Value(std::move(object));
402403

403404
const char *uuid = module.GetUUIDString();
404405
object.try_emplace("id", uuid ? std::string(uuid) : std::string(""));
406+
407+
if (id_only)
408+
return llvm::json::Value(std::move(object));
409+
405410
object.try_emplace("name", std::string(module.GetFileSpec().GetFilename()));
406411
char module_path_arr[PATH_MAX];
407412
module.GetFileSpec().GetPath(module_path_arr, sizeof(module_path_arr));

lldb/tools/lldb-dap/JSONUtils.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,15 @@ void FillResponse(const llvm::json::Object &request,
206206
/// \param[in] module
207207
/// A LLDB module object to convert into a JSON value
208208
///
209+
/// \param[in] id_only
210+
/// Only include the module ID in the JSON value. This is used when sending
211+
/// a "removed" module event.
212+
///
209213
/// \return
210214
/// A "Module" JSON object with that follows the formal JSON
211215
/// definition outlined by Microsoft.
212-
llvm::json::Value CreateModule(lldb::SBTarget &target, lldb::SBModule &module);
216+
llvm::json::Value CreateModule(lldb::SBTarget &target, lldb::SBModule &module,
217+
bool id_only = false);
213218

214219
/// Create a "Event" JSON object using \a event_name as the event name
215220
///

0 commit comments

Comments
 (0)