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