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