Skip to content

Commit 25356d5

Browse files
committed
Send reverse request from parent DAP instance and report errors
1 parent dd3e809 commit 25356d5

File tree

5 files changed

+63
-27
lines changed

5 files changed

+63
-27
lines changed

lldb/include/lldb/API/SBTarget.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ class LLDB_API SBTarget {
6565

6666
static lldb::SBTarget GetTargetFromEvent(const lldb::SBEvent &event);
6767

68+
/// For eBroadcastBitNewTargetCreated events, returns the newly created target.
69+
/// For other event types, returns an invalid SBTarget.
70+
static lldb::SBTarget GetCreatedTargetFromEvent(const lldb::SBEvent &event);
71+
6872
static uint32_t GetNumModulesFromEvent(const lldb::SBEvent &event);
6973

7074
static lldb::SBModule GetModuleAtIndexFromEvent(const uint32_t idx,

lldb/include/lldb/Target/Target.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,12 @@ class Target : public std::enable_shared_from_this<Target>,
557557
TargetEventData(const lldb::TargetSP &target_sp,
558558
const ModuleList &module_list);
559559

560+
// Constructor for eBroadcastBitNewTargetCreated events. For this event type:
561+
// - target_sp is the parent target (the subject/broadcaster of the event)
562+
// - created_target_sp is the newly created target
563+
TargetEventData(const lldb::TargetSP &target_sp,
564+
const lldb::TargetSP &created_target_sp);
565+
560566
~TargetEventData() override;
561567

562568
static llvm::StringRef GetFlavorString();
@@ -571,14 +577,21 @@ class Target : public std::enable_shared_from_this<Target>,
571577

572578
static lldb::TargetSP GetTargetFromEvent(const Event *event_ptr);
573579

580+
// For eBroadcastBitNewTargetCreated events, returns the newly created target.
581+
// For other event types, returns an invalid target.
582+
static lldb::TargetSP GetCreatedTargetFromEvent(const Event *event_ptr);
583+
574584
static ModuleList GetModuleListFromEvent(const Event *event_ptr);
575585

576586
const lldb::TargetSP &GetTarget() const { return m_target_sp; }
577587

588+
const lldb::TargetSP &GetCreatedTarget() const { return m_created_target_sp; }
589+
578590
const ModuleList &GetModuleList() const { return m_module_list; }
579591

580592
private:
581593
lldb::TargetSP m_target_sp;
594+
lldb::TargetSP m_created_target_sp;
582595
ModuleList m_module_list;
583596

584597
TargetEventData(const TargetEventData &) = delete;

lldb/source/API/SBTarget.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ SBTarget SBTarget::GetTargetFromEvent(const SBEvent &event) {
128128
return Target::TargetEventData::GetTargetFromEvent(event.get());
129129
}
130130

131+
SBTarget SBTarget::GetCreatedTargetFromEvent(const SBEvent &event) {
132+
LLDB_INSTRUMENT_VA(event);
133+
134+
return Target::TargetEventData::GetCreatedTargetFromEvent(event.get());
135+
}
136+
131137
uint32_t SBTarget::GetNumModulesFromEvent(const SBEvent &event) {
132138
LLDB_INSTRUMENT_VA(event);
133139

lldb/source/Target/Target.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5201,6 +5201,11 @@ Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp,
52015201
const ModuleList &module_list)
52025202
: EventData(), m_target_sp(target_sp), m_module_list(module_list) {}
52035203

5204+
Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp,
5205+
const lldb::TargetSP &created_target_sp)
5206+
: EventData(), m_target_sp(target_sp), m_created_target_sp(created_target_sp),
5207+
m_module_list() {}
5208+
52045209
Target::TargetEventData::~TargetEventData() = default;
52055210

52065211
llvm::StringRef Target::TargetEventData::GetFlavorString() {
@@ -5235,6 +5240,15 @@ TargetSP Target::TargetEventData::GetTargetFromEvent(const Event *event_ptr) {
52355240
return target_sp;
52365241
}
52375242

5243+
TargetSP
5244+
Target::TargetEventData::GetCreatedTargetFromEvent(const Event *event_ptr) {
5245+
TargetSP created_target_sp;
5246+
const TargetEventData *event_data = GetEventDataFromEvent(event_ptr);
5247+
if (event_data)
5248+
created_target_sp = event_data->m_created_target_sp;
5249+
return created_target_sp;
5250+
}
5251+
52385252
ModuleList
52395253
Target::TargetEventData::GetModuleListFromEvent(const Event *event_ptr) {
52405254
ModuleList module_list;

lldb/tools/lldb-dap/EventHelper.cpp

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -449,35 +449,34 @@ void HandleTargetEvent(const lldb::SBEvent &event, Log *log) {
449449
}
450450
}
451451
} else if (event_mask & lldb::SBTarget::eBroadcastBitNewTargetCreated) {
452-
auto target = lldb::SBTarget::GetTargetFromEvent(event);
453-
454-
// Find the DAP instance that owns this target
455-
DAP *dap_instance = DAPSessionManager::FindDAP(target);
456-
// If we don't have a dap_instance (target wasn't found), get any
457-
// active instance
458-
if (!dap_instance) {
459-
std::vector<DAP *> active_instances =
460-
DAPSessionManager::GetInstance().GetActiveSessions();
461-
if (!active_instances.empty())
462-
dap_instance = active_instances[0];
452+
// For NewTargetCreated events, GetTargetFromEvent returns the parent
453+
// target, and GetCreatedTargetFromEvent returns the newly created target.
454+
lldb::SBTarget created_target =
455+
lldb::SBTarget::GetCreatedTargetFromEvent(event);
456+
457+
if (!target.IsValid() || !created_target.IsValid()) {
458+
DAP_LOG(log, "Received NewTargetCreated event but parent or "
459+
"created target is invalid");
460+
return;
463461
}
464462

465-
if (dap_instance) {
466-
// Send a startDebugging reverse request with the debugger and target
467-
// IDs. The new DAP instance will use these IDs to find the existing
468-
// debugger and target via FindDebuggerWithID and
469-
// FindTargetByGloballyUniqueID.
470-
llvm::json::Object configuration{
471-
{"type", "lldb"},
472-
{"debuggerId", target.GetDebugger().GetID()},
473-
{"targetId", target.GetGloballyUniqueID()},
474-
{"name", target.GetTargetSessionName()}};
475-
476-
dap_instance->SendReverseRequest<LogFailureResponseHandler>(
477-
"startDebugging", llvm::json::Object{{"request", "attach"},
478-
{"configuration",
479-
std::move(configuration)}});
480-
}
463+
// Send a startDebugging reverse request with the debugger and target
464+
// IDs. The new DAP instance will use these IDs to find the existing
465+
// debugger and target via FindDebuggerWithID and
466+
// FindTargetByGloballyUniqueID.
467+
llvm::json::Object configuration;
468+
configuration.try_emplace("type", "lldb");
469+
configuration.try_emplace("debuggerId",
470+
created_target.GetDebugger().GetID());
471+
configuration.try_emplace("targetId", created_target.GetGloballyUniqueID());
472+
configuration.try_emplace("name", created_target.GetTargetSessionName());
473+
474+
llvm::json::Object request;
475+
request.try_emplace("request", "attach");
476+
request.try_emplace("configuration", std::move(configuration));
477+
478+
dap->SendReverseRequest<LogFailureResponseHandler>("startDebugging",
479+
std::move(request));
481480
}
482481
}
483482

0 commit comments

Comments
 (0)