Skip to content

Commit 4a9dd25

Browse files
committed
[lldb-dap] Create a function for target events.
1 parent 475a615 commit 4a9dd25

File tree

4 files changed

+51
-42
lines changed

4 files changed

+51
-42
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ def check_symbols_loaded_with_size():
8787
# symbols got added.
8888
self.assertNotEqual(len(module_changed_names), 0)
8989
self.assertIn(program_module["name"], module_changed_names)
90-
self.continue_to_exit()
9190

9291
# fetch modules from offset
9392
if len(active_modules.keys()) > 2:
@@ -101,6 +100,8 @@ def check_symbols_loaded_with_size():
101100
module_name = resp_modules[0]["name"]
102101
self.assertIn(module_name, active_modules.keys())
103102

103+
self.continue_to_exit()
104+
104105
@skipIfWindows
105106
def test_modules(self):
106107
"""

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#include "llvm/Support/ErrorHandling.h"
4545
#include "llvm/Support/FormatVariadic.h"
4646
#include "llvm/Support/raw_ostream.h"
47-
#include <Protocol/ProtocolEvents.h>
4847
#include <algorithm>
4948
#include <cassert>
5049
#include <chrono>
@@ -1348,46 +1347,7 @@ void DAP::EventThread() {
13481347
SendStdOutStdErr(*this, process);
13491348
}
13501349
} else if (lldb::SBTarget::EventIsTargetEvent(event)) {
1351-
if (event_mask & lldb::SBTarget::eBroadcastBitModulesLoaded ||
1352-
event_mask & lldb::SBTarget::eBroadcastBitModulesUnloaded ||
1353-
event_mask & lldb::SBTarget::eBroadcastBitSymbolsLoaded ||
1354-
event_mask & lldb::SBTarget::eBroadcastBitSymbolsChanged) {
1355-
const uint32_t num_modules =
1356-
lldb::SBTarget::GetNumModulesFromEvent(event);
1357-
std::lock_guard<std::mutex> guard(modules_mutex);
1358-
for (uint32_t i = 0; i < num_modules; ++i) {
1359-
lldb::SBModule module =
1360-
lldb::SBTarget::GetModuleAtIndexFromEvent(i, event);
1361-
1362-
const bool remove_module =
1363-
event_mask & lldb::SBTarget::eBroadcastBitModulesUnloaded;
1364-
auto p_module = CreateModule(target, module, remove_module);
1365-
if (!p_module)
1366-
continue;
1367-
1368-
llvm::StringRef module_id = p_module->id;
1369-
1370-
if (modules.contains(module_id)) {
1371-
if (event_mask & lldb::SBTarget::eBroadcastBitModulesUnloaded) {
1372-
modules.erase(module_id);
1373-
Send(protocol::Event{
1374-
"module",
1375-
ModuleEventBody{std::move(p_module).value(),
1376-
ModuleEventBody::eReasonRemoved}});
1377-
} else {
1378-
Send(protocol::Event{
1379-
"module",
1380-
ModuleEventBody{std::move(p_module).value(),
1381-
ModuleEventBody::eReasonChanged}});
1382-
}
1383-
} else if (!remove_module) {
1384-
modules.insert(module_id);
1385-
Send(protocol::Event{
1386-
"module", ModuleEventBody{std::move(p_module).value(),
1387-
ModuleEventBody::eReasonNew}});
1388-
}
1389-
}
1390-
}
1350+
HandleTargetEvent(*this, event);
13911351
} else if (lldb::SBBreakpoint::EventIsBreakpointEvent(event)) {
13921352
if (event_mask & lldb::SBTarget::eBroadcastBitBreakpointChanged) {
13931353
auto event_type =

lldb/tools/lldb-dap/EventHelper.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "LLDBUtils.h"
1414
#include "Protocol/ProtocolEvents.h"
1515
#include "Protocol/ProtocolTypes.h"
16+
#include "ProtocolUtils.h"
17+
#include "lldb/API/SBEvent.h"
1618
#include "lldb/API/SBFileSpec.h"
1719
#include "llvm/Support/Error.h"
1820

@@ -269,4 +271,48 @@ void SendProcessExitedEvent(DAP &dap, lldb::SBProcess &process) {
269271
dap.SendJSON(llvm::json::Value(std::move(event)));
270272
}
271273

274+
void HandleTargetEvent(DAP &dap, const lldb::SBEvent &event) {
275+
const lldb::SBTarget &target = dap.target;
276+
const uint32_t event_mask = event.GetType();
277+
if (!(event_mask & lldb::SBTarget::eBroadcastBitModulesLoaded) &&
278+
!(event_mask & lldb::SBTarget::eBroadcastBitModulesUnloaded) &&
279+
!(event_mask & lldb::SBTarget::eBroadcastBitSymbolsLoaded) &&
280+
!(event_mask & lldb::SBTarget::eBroadcastBitSymbolsChanged))
281+
return;
282+
283+
const uint32_t num_modules = lldb::SBTarget::GetNumModulesFromEvent(event);
284+
std::lock_guard<std::mutex> guard(dap.modules_mutex);
285+
286+
for (uint32_t i = 0; i < num_modules; ++i) {
287+
lldb::SBModule module = lldb::SBTarget::GetModuleAtIndexFromEvent(i, event);
288+
289+
const bool remove_module =
290+
event_mask & lldb::SBTarget::eBroadcastBitModulesUnloaded;
291+
auto p_module = lldb_dap::CreateModule(target, module, remove_module);
292+
if (!p_module)
293+
continue;
294+
295+
const llvm::StringRef module_id = p_module->id;
296+
if (dap.modules.contains(module_id)) {
297+
if (event_mask & lldb::SBTarget::eBroadcastBitModulesUnloaded) {
298+
dap.modules.erase(module_id);
299+
dap.Send(protocol::Event{
300+
"module", protocol::ModuleEventBody{
301+
std::move(p_module).value(),
302+
protocol::ModuleEventBody::eReasonRemoved}});
303+
} else {
304+
dap.Send(protocol::Event{
305+
"module", protocol::ModuleEventBody{
306+
std::move(p_module).value(),
307+
protocol::ModuleEventBody::eReasonChanged}});
308+
}
309+
} else if (!remove_module) {
310+
dap.modules.insert(module_id);
311+
dap.Send(protocol::Event{
312+
"module",
313+
protocol::ModuleEventBody{std::move(p_module).value(),
314+
protocol::ModuleEventBody::eReasonNew}});
315+
}
316+
}
317+
}
272318
} // namespace lldb_dap

lldb/tools/lldb-dap/EventHelper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ void SendContinuedEvent(DAP &dap);
3232

3333
void SendProcessExitedEvent(DAP &dap, lldb::SBProcess &process);
3434

35+
void HandleTargetEvent(DAP &dap, const lldb::SBEvent &event);
36+
3537
} // namespace lldb_dap
3638

3739
#endif

0 commit comments

Comments
 (0)