Skip to content

Commit 33177f9

Browse files
committed
Send reverse request from parent DAP instance and report errors
1 parent 24dea05 commit 33177f9

File tree

5 files changed

+76
-24
lines changed

5 files changed

+76
-24
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
@@ -5204,6 +5204,11 @@ Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp,
52045204
const ModuleList &module_list)
52055205
: EventData(), m_target_sp(target_sp), m_module_list(module_list) {}
52065206

5207+
Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp,
5208+
const lldb::TargetSP &created_target_sp)
5209+
: EventData(), m_target_sp(target_sp), m_created_target_sp(created_target_sp),
5210+
m_module_list() {}
5211+
52075212
Target::TargetEventData::~TargetEventData() = default;
52085213

52095214
llvm::StringRef Target::TargetEventData::GetFlavorString() {
@@ -5238,6 +5243,15 @@ TargetSP Target::TargetEventData::GetTargetFromEvent(const Event *event_ptr) {
52385243
return target_sp;
52395244
}
52405245

5246+
TargetSP
5247+
Target::TargetEventData::GetCreatedTargetFromEvent(const Event *event_ptr) {
5248+
TargetSP created_target_sp;
5249+
const TargetEventData *event_data = GetEventDataFromEvent(event_ptr);
5250+
if (event_data)
5251+
created_target_sp = event_data->m_created_target_sp;
5252+
return created_target_sp;
5253+
}
5254+
52415255
ModuleList
52425256
Target::TargetEventData::GetModuleListFromEvent(const Event *event_ptr) {
52435257
ModuleList module_list;

lldb/tools/lldb-dap/EventHelper.cpp

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -468,35 +468,50 @@ void EventThread(lldb::SBDebugger debugger, lldb::SBBroadcaster broadcaster,
468468
}
469469
}
470470
} else if (event_mask & lldb::SBTarget::eBroadcastBitNewTargetCreated) {
471-
auto target = lldb::SBTarget::GetTargetFromEvent(event);
471+
// For NewTargetCreated events, GetTargetFromEvent returns the parent
472+
// target (the subject of the event), and GetCreatedTargetFromEvent
473+
// returns the newly created target.
474+
lldb::SBTarget target =
475+
lldb::SBTarget::GetTargetFromEvent(event);
476+
lldb::SBTarget created_target =
477+
lldb::SBTarget::GetCreatedTargetFromEvent(event);
478+
479+
if (!target.IsValid() || !created_target.IsValid()) {
480+
DAP_LOG(log,
481+
"Received NewTargetCreated event but parent or "
482+
"created target is invalid");
483+
continue;
484+
}
472485

473-
// Find the DAP instance that owns this target
486+
// Find the DAP instance that spawned this target by looking up the
487+
// parent target.
474488
DAP *dap_instance = DAPSessionManager::FindDAP(target);
475-
// If we don't have a dap_instance (target wasn't found), get any
476-
// active instance
477489
if (!dap_instance) {
478-
std::vector<DAP *> active_instances =
479-
DAPSessionManager::GetInstance().GetActiveSessions();
480-
if (!active_instances.empty())
481-
dap_instance = active_instances[0];
490+
DAP_LOG(log,
491+
"Received NewTargetCreated event but could not find "
492+
"DAP instance for target");
493+
continue;
482494
}
483495

484-
if (dap_instance) {
485-
// Send a startDebugging reverse request with the debugger and target
486-
// IDs. The new DAP instance will use these IDs to find the existing
487-
// debugger and target via FindDebuggerWithID and
488-
// FindTargetByGloballyUniqueID.
489-
llvm::json::Object configuration{
490-
{"type", "lldb"},
491-
{"debuggerId", target.GetDebugger().GetID()},
492-
{"targetId", target.GetGloballyUniqueID()},
493-
{"name", target.GetTargetSessionName()}};
494-
495-
dap_instance->SendReverseRequest<LogFailureResponseHandler>(
496-
"startDebugging", llvm::json::Object{{"request", "attach"},
497-
{"configuration",
498-
std::move(configuration)}});
499-
}
496+
// Send a startDebugging reverse request with the debugger and target
497+
// IDs. The new DAP instance will use these IDs to find the existing
498+
// debugger and target via FindDebuggerWithID and
499+
// FindTargetByGloballyUniqueID.
500+
llvm::json::Object configuration;
501+
configuration.try_emplace("type", "lldb");
502+
configuration.try_emplace("debuggerId",
503+
created_target.GetDebugger().GetID());
504+
configuration.try_emplace("targetId",
505+
created_target.GetGloballyUniqueID());
506+
configuration.try_emplace("name",
507+
created_target.GetTargetSessionName());
508+
509+
llvm::json::Object request;
510+
request.try_emplace("request", "attach");
511+
request.try_emplace("configuration", std::move(configuration));
512+
513+
dap_instance->SendReverseRequest<LogFailureResponseHandler>(
514+
"startDebugging", std::move(request));
500515
}
501516
} else if (lldb::SBBreakpoint::EventIsBreakpointEvent(event)) {
502517
lldb::SBBreakpoint bp =

0 commit comments

Comments
 (0)