xref: /llvm-project/lldb/test/API/python_api/value/linked_list/TestValueAPILinkedList.py (revision e0053bc04e6b80ece8b334b268c2942e012009b9)
1"""
2Test SBValue API linked_list_iter which treats the SBValue as a linked list and
3supports iteration till the end of list is reached.
4"""
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12class ValueAsLinkedListTestCase(TestBase):
13    NO_DEBUG_INFO_TESTCASE = True
14
15    def setUp(self):
16        # Call super's setUp().
17        TestBase.setUp(self)
18        # We'll use the test method name as the exe_name.
19        self.exe_name = self.testMethodName
20        # Find the line number to break at.
21        self.line = line_number("main.cpp", "// Break at this line")
22
23    def test(self):
24        """Exercise SBValue API linked_list_iter."""
25        d = {"EXE": self.exe_name}
26        self.build(dictionary=d)
27        self.setTearDownCleanup(dictionary=d)
28        exe = self.getBuildArtifact(self.exe_name)
29
30        # Create a target by the debugger.
31        target = self.dbg.CreateTarget(exe)
32        self.assertTrue(target, VALID_TARGET)
33
34        # Create the breakpoint inside function 'main'.
35        breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
36        self.assertTrue(breakpoint, VALID_BREAKPOINT)
37
38        # Now launch the process, and do not stop at entry point.
39        process = target.LaunchSimple(None, None, self.get_process_working_directory())
40        self.assertTrue(process, PROCESS_IS_VALID)
41
42        # Get Frame #0.
43        self.assertState(process.GetState(), lldb.eStateStopped)
44        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
45        self.assertTrue(
46            thread.IsValid(),
47            "There should be a thread stopped due to breakpoint condition",
48        )
49        frame0 = thread.GetFrameAtIndex(0)
50
51        # Get variable 'task_head'.
52        task_head = frame0.FindVariable("task_head")
53        self.assertTrue(task_head, VALID_VARIABLE)
54        self.DebugSBValue(task_head)
55
56        # By design (see main.cpp), the visited id's are: [1, 2, 4, 5].
57        visitedIDs = [1, 2, 4, 5]
58        list = []
59
60        cvf = lldbutil.ChildVisitingFormatter(indent_child=2)
61        for t in task_head.linked_list_iter("next"):
62            self.assertTrue(t, VALID_VARIABLE)
63            # Make sure that 'next' corresponds to an SBValue with pointer
64            # type.
65            self.assertTrue(t.TypeIsPointerType())
66            if self.TraceOn():
67                print(cvf.format(t))
68            list.append(int(t.GetChildMemberWithName("id").GetValue()))
69
70        # Sanity checks that the we visited all the items (no more, no less).
71        if self.TraceOn():
72            print("visited IDs:", list)
73        self.assertEqual(visitedIDs, list)
74
75        # Let's exercise the linked_list_iter() API again, this time supplying
76        # our end of list test function.
77        def eol(val):
78            """Test function to determine end of list."""
79            # End of list is reached if either the value object is invalid
80            # or it corresponds to a null pointer.
81            if not val or int(val.GetValue(), 16) == 0:
82                return True
83            # Also check the "id" for correct semantics.  If id <= 0, the item
84            # is corrupted, let's return True to signify end of list.
85            if int(val.GetChildMemberWithName("id").GetValue(), 0) <= 0:
86                return True
87
88            # Otherwise, return False.
89            return False
90
91        list = []
92        for t in task_head.linked_list_iter("next", eol):
93            self.assertTrue(t, VALID_VARIABLE)
94            # Make sure that 'next' corresponds to an SBValue with pointer
95            # type.
96            self.assertTrue(t.TypeIsPointerType())
97            if self.TraceOn():
98                print(cvf.format(t))
99            list.append(int(t.GetChildMemberWithName("id").GetValue()))
100
101        # Sanity checks that the we visited all the items (no more, no less).
102        if self.TraceOn():
103            print("visited IDs:", list)
104        self.assertEqual(visitedIDs, list)
105
106        # Get variable 'empty_task_head'.
107        empty_task_head = frame0.FindVariable("empty_task_head")
108        self.assertTrue(empty_task_head, VALID_VARIABLE)
109        self.DebugSBValue(empty_task_head)
110
111        list = []
112        # There is no iterable item from empty_task_head.linked_list_iter().
113        for t in empty_task_head.linked_list_iter("next", eol):
114            if self.TraceOn():
115                print(cvf.format(t))
116            list.append(int(t.GetChildMemberWithName("id").GetValue()))
117
118        self.assertEqual(len(list), 0)
119
120        # Get variable 'task_evil'.
121        task_evil = frame0.FindVariable("task_evil")
122        self.assertTrue(task_evil, VALID_VARIABLE)
123        self.DebugSBValue(task_evil)
124
125        list = []
126        # There 3 iterable items from task_evil.linked_list_iter(). :-)
127        for t in task_evil.linked_list_iter("next"):
128            if self.TraceOn():
129                print(cvf.format(t))
130            list.append(int(t.GetChildMemberWithName("id").GetValue()))
131
132        self.assertEqual(len(list), 3)
133