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