xref: /llvm-project/lldb/test/API/python_api/value/TestValueAPI.py (revision 4cc8f2a017c76af25234afc7c380550e9c93135c)
1"""
2Test some SBValue APIs.
3"""
4
5from __future__ import print_function
6
7
8import lldb
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class ValueAPITestCase(TestBase):
15
16    def setUp(self):
17        # Call super's setUp().
18        TestBase.setUp(self)
19        # We'll use the test method name as the exe_name.
20        self.exe_name = self.testMethodName
21        # Find the line number to of function 'c'.
22        self.line = line_number('main.c', '// Break at this line')
23
24    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24772")
25    def test(self):
26        """Exercise some SBValue APIs."""
27        d = {'EXE': self.exe_name}
28        self.build(dictionary=d)
29        self.setTearDownCleanup(dictionary=d)
30        exe = self.getBuildArtifact(self.exe_name)
31
32        # Create a target by the debugger.
33        target = self.dbg.CreateTarget(exe)
34        self.assertTrue(target, VALID_TARGET)
35
36        # Create the breakpoint inside function 'main'.
37        breakpoint = target.BreakpointCreateByLocation('main.c', self.line)
38        self.assertTrue(breakpoint, VALID_BREAKPOINT)
39
40        # Now launch the process, and do not stop at entry point.
41        process = target.LaunchSimple(
42            None, None, self.get_process_working_directory())
43        self.assertTrue(process, PROCESS_IS_VALID)
44
45        # Get Frame #0.
46        self.assertState(process.GetState(), lldb.eStateStopped)
47        thread = lldbutil.get_stopped_thread(
48            process, lldb.eStopReasonBreakpoint)
49        self.assertTrue(
50            thread.IsValid(),
51            "There should be a thread stopped due to breakpoint condition")
52        frame0 = thread.GetFrameAtIndex(0)
53
54        # Get global variable 'days_of_week'.
55        list = target.FindGlobalVariables('days_of_week', 1)
56        days_of_week = list.GetValueAtIndex(0)
57        self.assertTrue(days_of_week, VALID_VARIABLE)
58        self.assertEqual(days_of_week.GetNumChildren(), 7, VALID_VARIABLE)
59        self.DebugSBValue(days_of_week)
60
61        # Use this to test the "child" and "children" accessors:
62        children = days_of_week.children
63        self.assertEqual(len(children), 7, VALID_VARIABLE)
64        for i in range(0, len(children)):
65            day = days_of_week.child[i]
66            list_day = children[i]
67            self.assertNotEqual(day, None)
68            self.assertNotEqual(list_day, None)
69            self.assertEqual(day.GetSummary(), list_day.GetSummary(), VALID_VARIABLE)
70
71        # Spot check the actual value:
72        first_day = days_of_week.child[1]
73        self.assertEqual(first_day.GetSummary(), '"Monday"', VALID_VARIABLE)
74
75        # Get global variable 'weekdays'.
76        list = target.FindGlobalVariables('weekdays', 1)
77        weekdays = list.GetValueAtIndex(0)
78        self.assertTrue(weekdays, VALID_VARIABLE)
79        self.assertEqual(weekdays.GetNumChildren(), 5, VALID_VARIABLE)
80        self.DebugSBValue(weekdays)
81
82        # Get global variable 'g_table'.
83        list = target.FindGlobalVariables('g_table', 1)
84        g_table = list.GetValueAtIndex(0)
85        self.assertTrue(g_table, VALID_VARIABLE)
86        self.assertEqual(g_table.GetNumChildren(), 2, VALID_VARIABLE)
87        self.DebugSBValue(g_table)
88
89        fmt = lldbutil.BasicFormatter()
90        cvf = lldbutil.ChildVisitingFormatter(indent_child=2)
91        rdf = lldbutil.RecursiveDecentFormatter(indent_child=2)
92        if self.TraceOn():
93            print(fmt.format(days_of_week))
94            print(cvf.format(days_of_week))
95            print(cvf.format(weekdays))
96            print(rdf.format(g_table))
97
98        # Get variable 'my_int_ptr'.
99        value = frame0.FindVariable('my_int_ptr')
100        self.assertTrue(value, VALID_VARIABLE)
101        self.DebugSBValue(value)
102
103        # Get what 'my_int_ptr' points to.
104        pointed = value.GetChildAtIndex(0)
105        self.assertTrue(pointed, VALID_VARIABLE)
106        self.DebugSBValue(pointed)
107
108        # While we are at it, verify that 'my_int_ptr' points to 'g_my_int'.
109        symbol = target.ResolveLoadAddress(
110            int(pointed.GetLocation(), 0)).GetSymbol()
111        self.assertTrue(symbol)
112        self.expect(symbol.GetName(), exe=False,
113                    startstr='g_my_int')
114
115        # Get variable 'str_ptr'.
116        value = frame0.FindVariable('str_ptr')
117        self.assertTrue(value, VALID_VARIABLE)
118        self.DebugSBValue(value)
119
120        # SBValue::TypeIsPointerType() should return true.
121        self.assertTrue(value.TypeIsPointerType())
122
123        # Verify the SBValue::GetByteSize() API is working correctly.
124        arch = self.getArchitecture()
125        if arch == 'i386':
126            self.assertEqual(value.GetByteSize(), 4)
127        elif arch == 'x86_64':
128            self.assertEqual(value.GetByteSize(), 8)
129
130        # Get child at index 5 => 'Friday'.
131        child = value.GetChildAtIndex(5, lldb.eNoDynamicValues, True)
132        self.assertTrue(child, VALID_VARIABLE)
133        self.DebugSBValue(child)
134
135        self.expect(child.GetSummary(), exe=False,
136                    substrs=['Friday'])
137
138        # Now try to get at the same variable using GetValueForExpressionPath().
139        # These two SBValue objects should have the same value.
140        val2 = value.GetValueForExpressionPath('[5]')
141        self.assertTrue(val2, VALID_VARIABLE)
142        self.DebugSBValue(val2)
143        self.assertTrue(child.GetValue() == val2.GetValue() and
144                        child.GetSummary() == val2.GetSummary())
145
146        val_i = target.EvaluateExpression('i')
147        val_s = target.EvaluateExpression('s')
148        val_a = target.EvaluateExpression('a')
149        self.assertTrue(
150            val_s.GetChildMemberWithName('a').GetAddress().IsValid(),
151            VALID_VARIABLE)
152        self.assertTrue(
153            val_s.GetChildMemberWithName('a').AddressOf(),
154            VALID_VARIABLE)
155        self.assertTrue(
156            val_a.Cast(
157                val_i.GetType()).AddressOf(),
158            VALID_VARIABLE)
159
160        # Check that lldb.value implements truth testing.
161        self.assertFalse(lldb.value(frame0.FindVariable('bogus')))
162        self.assertTrue(lldb.value(frame0.FindVariable('uinthex')))
163
164        self.assertTrue(int(lldb.value(frame0.FindVariable('uinthex')))
165                        == 3768803088, 'uinthex == 3768803088')
166        self.assertTrue(int(lldb.value(frame0.FindVariable('sinthex')))
167                        == -526164208, 'sinthex == -526164208')
168
169        # Check value_iter works correctly.
170        for v in [
171                lldb.value(frame0.FindVariable('uinthex')),
172                lldb.value(frame0.FindVariable('sinthex'))
173        ]:
174            self.assertTrue(v)
175
176        self.assertEqual(
177            frame0.FindVariable('uinthex').GetValueAsUnsigned(), 3768803088,
178            'unsigned uinthex == 3768803088')
179        self.assertEqual(
180            frame0.FindVariable('sinthex').GetValueAsUnsigned(), 3768803088,
181            'unsigned sinthex == 3768803088')
182
183        self.assertTrue(
184            frame0.FindVariable('uinthex').GetValueAsSigned() == -
185            526164208,
186            'signed uinthex == -526164208')
187        self.assertTrue(
188            frame0.FindVariable('sinthex').GetValueAsSigned() == -
189            526164208,
190            'signed sinthex == -526164208')
191