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