xref: /llvm-project/lldb/test/API/python_api/value/TestValueAPI.py (revision 3707c540d23a5684a1c37b0f7e41c1d8ed7f1f8a)
1"""
2Test some SBValue APIs.
3"""
4
5import lldb
6from lldbsuite.test import lldbutil
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
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    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24772")
21    def test(self):
22        """Exercise some SBValue APIs."""
23        d = {"EXE": self.exe_name}
24        self.build(dictionary=d)
25        self.setTearDownCleanup(dictionary=d)
26        exe = self.getBuildArtifact(self.exe_name)
27
28        # Create a target by the debugger.
29        target = self.dbg.CreateTarget(exe)
30        self.assertTrue(target, VALID_TARGET)
31
32        # Create the breakpoint inside function 'main'.
33        breakpoint = target.BreakpointCreateByLocation("main.c", self.line)
34        self.assertTrue(breakpoint, VALID_BREAKPOINT)
35
36        # Now launch the process, and do not stop at entry point.
37        process = target.LaunchSimple(None, None, self.get_process_working_directory())
38        self.assertTrue(process, PROCESS_IS_VALID)
39
40        # Get Frame #0.
41        self.assertState(process.GetState(), lldb.eStateStopped)
42        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
43        self.assertTrue(
44            thread.IsValid(),
45            "There should be a thread stopped due to breakpoint condition",
46        )
47        frame0 = thread.GetFrameAtIndex(0)
48
49        # Get global variable 'days_of_week'.
50        list = target.FindGlobalVariables("days_of_week", 1)
51        days_of_week = list.GetValueAtIndex(0)
52        self.assertTrue(days_of_week, VALID_VARIABLE)
53        self.assertEqual(days_of_week.GetNumChildren(), 7, VALID_VARIABLE)
54        self.DebugSBValue(days_of_week)
55
56        # Use this to test the "child" and "children" accessors:
57        children = days_of_week.children
58        self.assertEqual(len(children), 7, VALID_VARIABLE)
59        for i in range(0, len(children)):
60            day = days_of_week.child[i]
61            list_day = children[i]
62            self.assertNotEqual(day, None)
63            self.assertNotEqual(list_day, None)
64            self.assertEqual(day.GetSummary(), list_day.GetSummary(), VALID_VARIABLE)
65
66        # Spot check the actual value:
67        first_day = days_of_week.child[1]
68        self.assertEqual(first_day.GetSummary(), '"Monday"', VALID_VARIABLE)
69
70        # Get global variable 'weekdays'.
71        list = target.FindGlobalVariables("weekdays", 1)
72        weekdays = list.GetValueAtIndex(0)
73        self.assertTrue(weekdays, VALID_VARIABLE)
74        self.assertEqual(weekdays.GetNumChildren(), 5, VALID_VARIABLE)
75        self.DebugSBValue(weekdays)
76
77        # Get global variable 'g_table'.
78        list = target.FindGlobalVariables("g_table", 1)
79        g_table = list.GetValueAtIndex(0)
80        self.assertTrue(g_table, VALID_VARIABLE)
81        self.assertEqual(g_table.GetNumChildren(), 2, VALID_VARIABLE)
82        self.DebugSBValue(g_table)
83
84        fmt = lldbutil.BasicFormatter()
85        cvf = lldbutil.ChildVisitingFormatter(indent_child=2)
86        rdf = lldbutil.RecursiveDecentFormatter(indent_child=2)
87        if self.TraceOn():
88            print(fmt.format(days_of_week))
89            print(cvf.format(days_of_week))
90            print(cvf.format(weekdays))
91            print(rdf.format(g_table))
92
93        # Get variable 'my_int_ptr'.
94        value = frame0.FindVariable("my_int_ptr")
95        self.assertTrue(value, VALID_VARIABLE)
96        self.DebugSBValue(value)
97
98        # Get what 'my_int_ptr' points to.
99        pointed = value.GetChildAtIndex(0)
100        self.assertTrue(pointed, VALID_VARIABLE)
101        self.DebugSBValue(pointed)
102
103        # While we are at it, verify that 'my_int_ptr' points to 'g_my_int'.
104        symbol = target.ResolveLoadAddress(int(pointed.GetLocation(), 0)).GetSymbol()
105        self.assertTrue(symbol)
106        self.expect(symbol.GetName(), exe=False, startstr="g_my_int")
107
108        # Get variable 'str_ptr'.
109        value = frame0.FindVariable("str_ptr")
110        self.assertTrue(value, VALID_VARIABLE)
111        self.DebugSBValue(value)
112
113        # SBValue::TypeIsPointerType() should return true.
114        self.assertTrue(value.TypeIsPointerType())
115
116        # Verify the SBValue::GetByteSize() API is working correctly.
117        arch = self.getArchitecture()
118        if arch == "i386":
119            self.assertEqual(value.GetByteSize(), 4)
120        elif arch == "x86_64":
121            self.assertEqual(value.GetByteSize(), 8)
122
123        # Get child at index 5 => 'Friday'.
124        child = value.GetChildAtIndex(5, lldb.eNoDynamicValues, True)
125        self.assertTrue(child, VALID_VARIABLE)
126        self.DebugSBValue(child)
127
128        self.expect(child.GetSummary(), exe=False, substrs=["Friday"])
129
130        # Now try to get at the same variable using GetValueForExpressionPath().
131        # These two SBValue objects should have the same value.
132        val2 = value.GetValueForExpressionPath("[5]")
133        self.assertTrue(val2, VALID_VARIABLE)
134        self.DebugSBValue(val2)
135        self.assertTrue(
136            child.GetValue() == val2.GetValue()
137            and child.GetSummary() == val2.GetSummary()
138        )
139
140        val_i = target.EvaluateExpression("i")
141        val_s = target.EvaluateExpression("s")
142        val_a = target.EvaluateExpression("a")
143        self.assertTrue(
144            val_s.GetChildMemberWithName("a").GetAddress().IsValid(), VALID_VARIABLE
145        )
146        self.assertTrue(val_s.GetChildMemberWithName("a").AddressOf(), VALID_VARIABLE)
147        self.assertTrue(val_a.Cast(val_i.GetType()).AddressOf(), VALID_VARIABLE)
148
149        # Test some other cases of the Cast API.  We allow casts from one struct type
150        # to another, which is a little weird, but we don't support casting from a
151        # smaller type to a larger when the underlying data is not in the inferior,
152        # since then we have no way to fetch the out-of-bounds values.
153        # For an expression that references a variable, or a FindVariable result,
154        # or an SBValue made from an address and a type, we can get back to the target,
155        # so those will work.  Make sure they do and get the right extra values as well.
156
157        # We're casting everything to the type of "f", so get that first:
158        f_var = frame0.FindVariable("f")
159        self.assertSuccess(f_var.error, "Got f")
160        bigger_type = f_var.GetType()
161
162        # First try a value that we got from FindVariable
163        container = frame0.FindVariable("my_container")
164        self.assertSuccess(container.error, "Found my_container")
165        fv_small = container.GetValueForExpressionPath(".data.small")
166        self.assertSuccess(fv_small.error, "Found small in my_container")
167        fv_cast = fv_small.Cast(bigger_type)
168        self.assertSuccess(fv_cast.error, "Can cast up from FindVariable")
169        child_checks = [
170            ValueCheck(name="a", value="33", type="int"),
171            ValueCheck(name="b", value="44", type="int"),
172            ValueCheck(name="c", value="55", type="int"),
173        ]
174        cast_check = ValueCheck(type=bigger_type.name, children=child_checks)
175
176        # Now try one we made with expr.  This one should fail, because expr
177        # stores the "canonical value" in host memory, and doesn't know how
178        # to augment that from the live address.
179        expr_cont = frame0.EvaluateExpression("my_container")
180        self.assertSuccess(expr_cont.error, "Got my_container by expr")
181        expr_small = expr_cont.GetValueForExpressionPath(".data.small")
182        self.assertSuccess(expr_small.error, "Got small by expr")
183        expr_cast = expr_small.Cast(bigger_type)
184        self.assertFailure(expr_cast.error, msg="Cannot cast expr result")
185
186        # Now try one we made with CreateValueFromAddress.  That will succeed
187        # because this directly tracks the inferior memory.
188        small_addr = fv_small.addr
189        self.assertTrue(small_addr.IsValid())
190        small_type = fv_small.GetType()
191        vfa_small = target.CreateValueFromAddress(
192            "small_from_addr", small_addr, small_type
193        )
194        self.assertSuccess(vfa_small.error, "Made small from address")
195        vfa_cast = vfa_small.Cast(bigger_type)
196        self.assertSuccess(vfa_cast.error, "Made a cast from vfa_small")
197        cast_check.check_value(self, vfa_cast, "Cast of ValueFromAddress succeeds")
198
199        # Next try ValueObject created from data.  They should fail as there's no
200        # way to grow the data:
201        data_small = target.CreateValueFromData(
202            "small_from_data", fv_small.data, fv_small.type
203        )
204        self.assertSuccess(data_small.error, "Made a valid object from data")
205        data_cast = data_small.Cast(bigger_type)
206        self.assertFailure(data_cast.error, msg="Cannot cast data backed SBValue")
207
208        # Now check casting from a larger type to a smaller, we can always do this,
209        # so just test one case:
210        weird_cast = f_var.Cast(val_s.GetType())
211        self.assertSuccess(weird_cast.GetError(), "Can cast from a larger to a smaller")
212        self.assertEqual(
213            weird_cast.GetChildMemberWithName("a").GetValueAsSigned(0),
214            33,
215            "Got the right value",
216        )
217
218        # Check that lldb.value implements truth testing.
219        self.assertFalse(lldb.value(frame0.FindVariable("bogus")))
220        self.assertTrue(lldb.value(frame0.FindVariable("uinthex")))
221
222        self.assertEqual(
223            int(lldb.value(frame0.FindVariable("uinthex"))),
224            3768803088,
225            "uinthex == 3768803088",
226        )
227        self.assertEqual(
228            int(lldb.value(frame0.FindVariable("sinthex"))),
229            -526164208,
230            "sinthex == -526164208",
231        )
232
233        # Check value_iter works correctly.
234        for v in [
235            lldb.value(frame0.FindVariable("uinthex")),
236            lldb.value(frame0.FindVariable("sinthex")),
237        ]:
238            self.assertTrue(v)
239
240        self.assertEqual(
241            frame0.FindVariable("uinthex").GetValueAsUnsigned(),
242            3768803088,
243            "unsigned uinthex == 3768803088",
244        )
245        self.assertEqual(
246            frame0.FindVariable("sinthex").GetValueAsUnsigned(),
247            3768803088,
248            "unsigned sinthex == 3768803088",
249        )
250
251        self.assertEqual(
252            frame0.FindVariable("uinthex").GetValueAsSigned(),
253            -526164208,
254            "signed uinthex == -526164208",
255        )
256        self.assertEqual(
257            frame0.FindVariable("sinthex").GetValueAsSigned(),
258            -526164208,
259            "signed sinthex == -526164208",
260        )
261
262        # Check that hex value printing works as expected.
263        self.assertEqual(
264            frame0.FindVariable("fixed_int_ptr").GetValue(),
265            "0x000000aa" if target.addr_size == 4 else "0x00000000000000aa",
266        )
267        self.runCmd(
268            "settings set target.show-hex-variable-values-with-leading-zeroes false"
269        )
270        self.assertEqual(
271            frame0.FindVariable("another_fixed_int_ptr").GetValue(),
272            "0xaa",
273        )
274        self.assertEqual(
275            frame0.FindVariable("a_null_int_ptr").GetValue(),
276            "0x0",
277        )
278