@@ -294,3 +294,150 @@ def test_version(self):
294294
295295 self .assertEqual (instance_str , class_str )
296296 self .assertEqual (class_str , property_str )
297+
298+ def test_find_target_with_unique_id (self ):
299+ """Test SBDebugger.FindTargetByGloballyUniqueID() functionality."""
300+
301+ # Test with invalid ID - should return invalid target
302+ invalid_target = self .dbg .FindTargetByGloballyUniqueID (999999 )
303+ self .assertFalse (invalid_target .IsValid ())
304+
305+ # Test with ID 0 - should return invalid target
306+ zero_target = self .dbg .FindTargetByGloballyUniqueID (0 )
307+ self .assertFalse (zero_target .IsValid ())
308+
309+ # Build a real executable and create target with it
310+ self .build ()
311+ exe = self .getBuildArtifact ("a.out" )
312+ target = self .dbg .CreateTarget (exe )
313+ self .assertTrue (target .IsValid ())
314+
315+ # Find the target using its unique ID
316+ unique_id = target .GetGloballyUniqueID ()
317+ self .assertNotEqual (unique_id , lldb .LLDB_INVALID_GLOBALLY_UNIQUE_TARGET_ID )
318+ found_target = self .dbg .FindTargetByGloballyUniqueID (unique_id )
319+ self .assertTrue (found_target .IsValid ())
320+ self .assertEqual (
321+ self .dbg .GetIndexOfTarget (target ), self .dbg .GetIndexOfTarget (found_target )
322+ )
323+ self .assertEqual (found_target .GetGloballyUniqueID (), unique_id )
324+
325+ def test_target_unique_id_uniqueness (self ):
326+ """Test that Target.GetGloballyUniqueID() returns unique values across multiple targets."""
327+
328+ # Create multiple targets and verify they all have unique IDs
329+ self .build ()
330+ exe = self .getBuildArtifact ("a.out" )
331+ targets = []
332+ unique_ids = set ()
333+
334+ for i in range (10 ):
335+ target = self .dbg .CreateTarget (exe )
336+ self .assertTrue (target .IsValid ())
337+
338+ unique_id = target .GetGloballyUniqueID ()
339+ self .assertNotEqual (unique_id , 0 )
340+
341+ # Verify this ID hasn't been used before
342+ self .assertNotIn (
343+ unique_id , unique_ids , f"Duplicate unique ID found: { unique_id } "
344+ )
345+
346+ unique_ids .add (unique_id )
347+ targets .append (target )
348+
349+ # Verify all targets can still be found by their IDs
350+ for target in targets :
351+ unique_id = target .GetGloballyUniqueID ()
352+ found = self .dbg .FindTargetByGloballyUniqueID (unique_id )
353+ self .assertTrue (found .IsValid ())
354+ self .assertEqual (found .GetGloballyUniqueID (), unique_id )
355+
356+ def test_target_unique_id_uniqueness_after_deletion (self ):
357+ """Test finding targets have unique ID after target deletion."""
358+ # Create two targets
359+ self .build ()
360+ exe = self .getBuildArtifact ("a.out" )
361+ target1 = self .dbg .CreateTarget (exe )
362+ target2 = self .dbg .CreateTarget (exe )
363+ self .assertTrue (target1 .IsValid ())
364+ self .assertTrue (target2 .IsValid ())
365+
366+ unique_id1 = target1 .GetGloballyUniqueID ()
367+ unique_id2 = target2 .GetGloballyUniqueID ()
368+ self .assertNotEqual (unique_id1 , 0 )
369+ self .assertNotEqual (unique_id2 , 0 )
370+ self .assertNotEqual (unique_id1 , unique_id2 )
371+
372+ # Verify we can find them initially
373+ found_target1 = self .dbg .FindTargetByGloballyUniqueID (unique_id1 )
374+ found_target2 = self .dbg .FindTargetByGloballyUniqueID (unique_id2 )
375+ self .assertTrue (found_target1 .IsValid ())
376+ self .assertTrue (found_target2 .IsValid ())
377+ target2_index = self .dbg .GetIndexOfTarget (target2 )
378+
379+ # Delete target 2
380+ deleted = self .dbg .DeleteTarget (target2 )
381+ self .assertTrue (deleted )
382+
383+ # Try to find the deleted target - should not be found
384+ not_found_target = self .dbg .FindTargetByGloballyUniqueID (unique_id2 )
385+ self .assertFalse (not_found_target .IsValid ())
386+
387+ # Create a new target
388+ target3 = self .dbg .CreateTarget (exe )
389+ self .assertTrue (target3 .IsValid ())
390+ # Target list index of target3 should be the same as target2's
391+ # since it was deleted, but it should have a distinct unique ID
392+ target3_index = self .dbg .GetIndexOfTarget (target3 )
393+ unique_id3 = target3 .GetGloballyUniqueID ()
394+ self .assertEqual (target3_index , target2_index )
395+ self .assertNotEqual (unique_id3 , unique_id2 )
396+ self .assertNotEqual (unique_id3 , unique_id1 )
397+ # Make sure we can find the new target
398+ found_target3 = self .dbg .FindTargetByGloballyUniqueID (
399+ target3 .GetGloballyUniqueID ()
400+ )
401+ self .assertTrue (found_target3 .IsValid ())
402+
403+ def test_target_globally_unique_id_across_debuggers (self ):
404+ """Test that target IDs are globally unique across multiple debuggers."""
405+ self .build ()
406+ exe = self .getBuildArtifact ("a.out" )
407+
408+ # Create two debuggers with targets each
409+ debugger1 = lldb .SBDebugger .Create ()
410+ debugger2 = lldb .SBDebugger .Create ()
411+ self .addTearDownHook (lambda : lldb .SBDebugger .Destroy (debugger1 ))
412+ self .addTearDownHook (lambda : lldb .SBDebugger .Destroy (debugger2 ))
413+
414+ # Create 2 targets per debugger
415+ targets_d1 = [debugger1 .CreateTarget (exe ), debugger1 .CreateTarget (exe )]
416+ targets_d2 = [debugger2 .CreateTarget (exe ), debugger2 .CreateTarget (exe )]
417+ targets = targets_d1 + targets_d2
418+
419+ # Get all IDs and verify they're unique
420+ ids = [target .GetGloballyUniqueID () for target in targets ]
421+ self .assertEqual (
422+ len (set (ids )), len (ids ), f"IDs should be globally unique: { ids } "
423+ )
424+ self .assertTrue (
425+ all (uid != lldb .LLDB_INVALID_GLOBALLY_UNIQUE_TARGET_ID for uid in ids ),
426+ "All IDs should be valid" ,
427+ )
428+
429+ # Verify targets can be found by their IDs in respective debuggers
430+ for debugger , target_pair in [
431+ (debugger1 , targets [:2 ]),
432+ (debugger2 , targets [2 :]),
433+ ]:
434+ for target in target_pair :
435+ found = debugger .FindTargetByGloballyUniqueID (
436+ target .GetGloballyUniqueID ()
437+ )
438+ self .assertTrue (
439+ found .IsValid (), "Target should be found by its unique ID"
440+ )
441+ self .assertEqual (
442+ found .GetGloballyUniqueID (), target .GetGloballyUniqueID ()
443+ )
0 commit comments