@@ -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