[Lldb-commits] [lldb] 3ff6367 - [lldb] Accept negative indexes in __getitem__

Dave Lee via lldb-commits lldb-commits at lists.llvm.org
Wed Feb 8 10:46:44 PST 2023


Author: Dave Lee
Date: 2023-02-08T10:46:26-08:00
New Revision: 3ff636729d067801039b3a37618f6ce0dd1c3d24

URL: https://github.com/llvm/llvm-project/commit/3ff636729d067801039b3a37618f6ce0dd1c3d24
DIFF: https://github.com/llvm/llvm-project/commit/3ff636729d067801039b3a37618f6ce0dd1c3d24.diff

LOG: [lldb] Accept negative indexes in __getitem__

To the Python bindings, add support for Python-like negative indexes.

While was using `script`, I tried to access a thread's bottom frame with
`thread.frame[-1]`, but that failed. This change updates the `__getitem__`
implementations to support negative indexes as one would expect in Python.

Differential Revision: https://reviews.llvm.org/D143282

Added: 
    

Modified: 
    lldb/bindings/interface/SBBreakpoint.i
    lldb/bindings/interface/SBInstructionList.i
    lldb/bindings/interface/SBModule.i
    lldb/bindings/interface/SBProcess.i
    lldb/bindings/interface/SBSymbolContextList.i
    lldb/bindings/interface/SBTarget.i
    lldb/bindings/interface/SBThread.i
    lldb/bindings/interface/SBTypeCategory.i
    lldb/bindings/interface/SBTypeEnumMember.i
    lldb/bindings/interface/SBValue.i
    lldb/bindings/interface/SBValueList.i
    lldb/test/API/python_api/breakpoint/TestBreakpointAPI.py
    lldb/test/API/python_api/thread/TestThreadAPI.py

Removed: 
    


################################################################################
diff  --git a/lldb/bindings/interface/SBBreakpoint.i b/lldb/bindings/interface/SBBreakpoint.i
index a7048309edd9f..a61874d8ebd81 100644
--- a/lldb/bindings/interface/SBBreakpoint.i
+++ b/lldb/bindings/interface/SBBreakpoint.i
@@ -273,8 +273,11 @@ public:
                 return 0
 
             def __getitem__(self, key):
-                if type(key) is int and key < len(self):
-                    return self.sbbreakpoint.GetLocationAtIndex(key)
+                if isinstance(key, int):
+                    count = len(self)
+                    if -count <= key < count:
+                        key %= count
+                        return self.sbbreakpoint.GetLocationAtIndex(key)
                 return None
 
         def get_locations_access_object(self):

diff  --git a/lldb/bindings/interface/SBInstructionList.i b/lldb/bindings/interface/SBInstructionList.i
index b51c0374c3adb..e80452e3bed52 100644
--- a/lldb/bindings/interface/SBInstructionList.i
+++ b/lldb/bindings/interface/SBInstructionList.i
@@ -83,7 +83,9 @@ public:
             '''Access instructions by integer index for array access or by lldb.SBAddress to find an instruction that matches a section offset address object.'''
             if type(key) is int:
                 # Find an instruction by index
-                if key < len(self):
+                count = len(self)
+                if -count <= key < count:
+                    key %= count
                     return self.GetInstructionAtIndex(key)
             elif type(key) is SBAddress:
                 # Find an instruction using a lldb.SBAddress object

diff  --git a/lldb/bindings/interface/SBModule.i b/lldb/bindings/interface/SBModule.i
index de476f706261c..f181d96a55f99 100644
--- a/lldb/bindings/interface/SBModule.i
+++ b/lldb/bindings/interface/SBModule.i
@@ -415,7 +415,8 @@ public:
             def __getitem__(self, key):
                 count = len(self)
                 if type(key) is int:
-                    if key < count:
+                    if -count <= key < count:
+                        key %= count
                         return self.sbmodule.GetSymbolAtIndex(key)
                 elif type(key) is str:
                     matches = []
@@ -476,7 +477,8 @@ public:
             def __getitem__(self, key):
                 count = len(self)
                 if type(key) is int:
-                    if key < count:
+                    if -count <= key < count:
+                        key %= count
                         return self.sbmodule.GetSectionAtIndex(key)
                 elif type(key) is str:
                     for idx in range(count):
@@ -511,7 +513,8 @@ public:
             def __getitem__(self, key):
                 count = len(self)
                 if type(key) is int:
-                    if key < count:
+                    if -count <= key < count:
+                        key %= count
                         return self.sbmodule.GetCompileUnitAtIndex(key)
                 elif type(key) is str:
                     is_full_path = key[0] == '/'

diff  --git a/lldb/bindings/interface/SBProcess.i b/lldb/bindings/interface/SBProcess.i
index 0ef558459e84f..01da4274ed20a 100644
--- a/lldb/bindings/interface/SBProcess.i
+++ b/lldb/bindings/interface/SBProcess.i
@@ -487,8 +487,11 @@ public:
                 return 0
 
             def __getitem__(self, key):
-                if type(key) is int and key < len(self):
-                    return self.sbprocess.GetThreadAtIndex(key)
+                if isinstance(key, int):
+                    count = len(self)
+                    if -count <= key < count:
+                        key %= count
+                        return self.sbprocess.GetThreadAtIndex(key)
                 return None
 
         def get_threads_access_object(self):

diff  --git a/lldb/bindings/interface/SBSymbolContextList.i b/lldb/bindings/interface/SBSymbolContextList.i
index 14566b3e3720a..7bbeed72cf8d9 100644
--- a/lldb/bindings/interface/SBSymbolContextList.i
+++ b/lldb/bindings/interface/SBSymbolContextList.i
@@ -74,8 +74,9 @@ public:
 
         def __getitem__(self, key):
             count = len(self)
-            if type(key) is int:
-                if key < count:
+            if isinstance(key, int):
+                if -count <= key < count:
+                    key %= count
                     return self.GetContextAtIndex(key)
                 else:
                     raise IndexError

diff  --git a/lldb/bindings/interface/SBTarget.i b/lldb/bindings/interface/SBTarget.i
index e887762b83134..6529f8f6aa1d6 100644
--- a/lldb/bindings/interface/SBTarget.i
+++ b/lldb/bindings/interface/SBTarget.i
@@ -1001,7 +1001,8 @@ public:
             def __getitem__(self, key):
                 num_modules = self.sbtarget.GetNumModules()
                 if type(key) is int:
-                    if key < num_modules:
+                    if -num_modules <= key < num_modules:
+                        key %= num_modules
                         return self.sbtarget.GetModuleAtIndex(key)
                 elif type(key) is str:
                     if key.find('/') == -1:

diff  --git a/lldb/bindings/interface/SBThread.i b/lldb/bindings/interface/SBThread.i
index 1e46bd6e21f63..8317e17a299db 100644
--- a/lldb/bindings/interface/SBThread.i
+++ b/lldb/bindings/interface/SBThread.i
@@ -434,8 +434,11 @@ public:
                 return 0
 
             def __getitem__(self, key):
-                if type(key) is int and key < self.sbthread.GetNumFrames():
-                    return self.sbthread.GetFrameAtIndex(key)
+                if isinstance(key, int):
+                    count = len(self)
+                    if -count <= key < count:
+                        key %= count
+                        return self.sbthread.GetFrameAtIndex(key)
                 return None
 
         def get_frames_access_object(self):

diff  --git a/lldb/bindings/interface/SBTypeCategory.i b/lldb/bindings/interface/SBTypeCategory.i
index b762bf8a95a36..f8af390c24be9 100644
--- a/lldb/bindings/interface/SBTypeCategory.i
+++ b/lldb/bindings/interface/SBTypeCategory.i
@@ -147,7 +147,8 @@ namespace lldb {
                 def __getitem__(self, key):
                     num_items = len(self)
                     if type(key) is int:
-                        if key < num_items:
+                        if -num_items <= key < num_items:
+                            key %= num_items
                             return self.get_at_index_function(self.sbcategory,key)
                     elif type(key) is str:
                         return self.get_by_name_function(self.sbcategory,SBTypeNameSpecifier(key))

diff  --git a/lldb/bindings/interface/SBTypeEnumMember.i b/lldb/bindings/interface/SBTypeEnumMember.i
index b419010272450..986cb87531c1d 100644
--- a/lldb/bindings/interface/SBTypeEnumMember.i
+++ b/lldb/bindings/interface/SBTypeEnumMember.i
@@ -121,7 +121,8 @@ public:
         def __getitem__(self, key):
           num_elements = self.GetSize()
           if type(key) is int:
-              if key < num_elements:
+              if -num_elements <= key < num_elements:
+                  key %= num_elements
                   return self.GetTypeEnumMemberAtIndex(key)
           elif type(key) is str:
               for idx in range(num_elements):

diff  --git a/lldb/bindings/interface/SBValue.i b/lldb/bindings/interface/SBValue.i
index bc66a4ae28f82..335667f97e9cf 100644
--- a/lldb/bindings/interface/SBValue.i
+++ b/lldb/bindings/interface/SBValue.i
@@ -459,8 +459,11 @@ public:
                 return 0
 
             def __getitem__(self, key):
-                if type(key) is int and key < len(self):
-                    return self.sbvalue.GetChildAtIndex(key)
+                if isinstance(key, int):
+                    count = len(self)
+                    if -count <= key < count:
+                        key %= count
+                        return self.sbvalue.GetChildAtIndex(key)
                 return None
 
         def get_child_access_object(self):

diff  --git a/lldb/bindings/interface/SBValueList.i b/lldb/bindings/interface/SBValueList.i
index e03b5c649db99..4488b5d6fa90a 100644
--- a/lldb/bindings/interface/SBValueList.i
+++ b/lldb/bindings/interface/SBValueList.i
@@ -146,7 +146,8 @@ public:
             # Access with "int" to get Nth item in the list
             #------------------------------------------------------------
             if type(key) is int:
-                if key < count:
+                if -count <= key < count:
+                    key %= count
                     return self.GetValueAtIndex(key)
             #------------------------------------------------------------
             # Access with "str" to get values by name

diff  --git a/lldb/test/API/python_api/breakpoint/TestBreakpointAPI.py b/lldb/test/API/python_api/breakpoint/TestBreakpointAPI.py
index 014065e51919b..da6c8959f34fa 100644
--- a/lldb/test/API/python_api/breakpoint/TestBreakpointAPI.py
+++ b/lldb/test/API/python_api/breakpoint/TestBreakpointAPI.py
@@ -62,6 +62,9 @@ def test_target_delete(self):
         location = breakpoint.GetLocationAtIndex(0)
         self.assertTrue(location.IsValid())
 
+        # Test negative index access.
+        self.assertTrue(breakpoint.location[-1].IsValid())
+
         # Make sure the breakpoint's target is right:
         self.assertEqual(target, breakpoint.GetTarget(), "Breakpoint reports its target correctly")
         

diff  --git a/lldb/test/API/python_api/thread/TestThreadAPI.py b/lldb/test/API/python_api/thread/TestThreadAPI.py
index 7bdcf36810b5a..b26d168c179bc 100644
--- a/lldb/test/API/python_api/thread/TestThreadAPI.py
+++ b/lldb/test/API/python_api/thread/TestThreadAPI.py
@@ -48,6 +48,11 @@ def test_step_over_3_times(self):
         self.setTearDownCleanup(dictionary=d)
         self.step_over_3_times(self.exe_name)
 
+    def test_negative_indexing(self):
+        """Test SBThread.frame with negative indexes."""
+        self.build()
+        self.validate_negative_indexing()
+
     def setUp(self):
         # Call super's setUp().
         TestBase.setUp(self)
@@ -269,3 +274,29 @@ def run_to_address(self, exe_name):
         thread.RunToAddress(start_addr)
         self.runCmd("process status")
         #self.runCmd("thread backtrace")
+
+    def validate_negative_indexing(self):
+        exe = self.getBuildArtifact("a.out")
+
+        target = self.dbg.CreateTarget(exe)
+        self.assertTrue(target, VALID_TARGET)
+
+        breakpoint = target.BreakpointCreateByLocation(
+            "main.cpp", self.break_line)
+        self.assertTrue(breakpoint, VALID_BREAKPOINT)
+        self.runCmd("breakpoint list")
+
+        # Launch the process, and do not stop at the entry point.
+        process = target.LaunchSimple(
+            None, None, self.get_process_working_directory())
+
+        thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
+        self.assertTrue(
+            thread.IsValid(),
+            "There should be a thread stopped due to breakpoint")
+        self.runCmd("process status")
+
+        pos_range = range(thread.num_frames)
+        neg_range = range(thread.num_frames, 0, -1)
+        for pos, neg in zip(pos_range, neg_range):
+            self.assertEqual(thread.frame[pos].idx, thread.frame[-neg].idx)


        


More information about the lldb-commits mailing list