xref: /llvm-project/lldb/test/API/lang/c/bitfields/TestBitfields.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1025f6ca7SRaphael Isemann"""Test C bitfields."""
299451b44SJordan Rupprecht
399451b44SJordan Rupprecht
499451b44SJordan Rupprechtimport lldb
599451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
699451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
799451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
899451b44SJordan Rupprecht
999451b44SJordan Rupprecht
10025f6ca7SRaphael Isemannclass TestCase(TestBase):
11025f6ca7SRaphael Isemann    def run_to_main(self):
12025f6ca7SRaphael Isemann        self.build()
13*2238dcc3SJonas Devlieghere        lldbutil.run_to_source_breakpoint(
14*2238dcc3SJonas Devlieghere            self, "// break here", lldb.SBFileSpec("main.c")
15*2238dcc3SJonas Devlieghere        )
1699451b44SJordan Rupprecht
1799451b44SJordan Rupprecht    # BitFields exhibit crashes in record layout on Windows
1899451b44SJordan Rupprecht    # (http://llvm.org/pr21800)
1999451b44SJordan Rupprecht    @skipIfWindows
20025f6ca7SRaphael Isemann    def test_bits(self):
21025f6ca7SRaphael Isemann        self.run_to_main()
2299451b44SJordan Rupprecht
23025f6ca7SRaphael Isemann        # Check each field of Bits.
24025f6ca7SRaphael Isemann        bits_children = [
25025f6ca7SRaphael Isemann            ValueCheck(type="int:1"),  # Unnamed and uninitialized
26025f6ca7SRaphael Isemann            ValueCheck(type="uint32_t:1", name="b1", value="1"),
27025f6ca7SRaphael Isemann            ValueCheck(type="uint32_t:2", name="b2", value="3"),
28025f6ca7SRaphael Isemann            ValueCheck(type="int:2"),  # Unnamed and uninitialized
29025f6ca7SRaphael Isemann            ValueCheck(type="uint32_t:3", name="b3", value="7"),
30025f6ca7SRaphael Isemann            ValueCheck(type="uint32_t", name="b4", value="15"),
31025f6ca7SRaphael Isemann            ValueCheck(type="uint32_t:5", name="b5", value="31"),
32025f6ca7SRaphael Isemann            ValueCheck(type="uint32_t:6", name="b6", value="63"),
33025f6ca7SRaphael Isemann            ValueCheck(type="uint32_t:7", name="b7", value="127"),
34*2238dcc3SJonas Devlieghere            ValueCheck(type="uint32_t:4", name="four", value="15"),
35025f6ca7SRaphael Isemann        ]
36025f6ca7SRaphael Isemann        self.expect_var_path("bits", type="Bits", children=bits_children)
37025f6ca7SRaphael Isemann        self.expect_expr("bits", result_children=bits_children)
3899451b44SJordan Rupprecht
39025f6ca7SRaphael Isemann        # Try accessing the different fields using the expression evaluator.
40025f6ca7SRaphael Isemann        self.expect_expr("bits.b1", result_type="uint32_t", result_value="1")
41025f6ca7SRaphael Isemann        self.expect_expr("bits.b2", result_type="uint32_t", result_value="3")
42025f6ca7SRaphael Isemann        self.expect_expr("bits.b3", result_type="uint32_t", result_value="7")
43025f6ca7SRaphael Isemann        self.expect_expr("bits.b4", result_type="uint32_t", result_value="15")
44025f6ca7SRaphael Isemann        self.expect_expr("bits.b5", result_type="uint32_t", result_value="31")
45025f6ca7SRaphael Isemann        self.expect_expr("bits.b6", result_type="uint32_t", result_value="63")
46025f6ca7SRaphael Isemann        self.expect_expr("bits.b7", result_type="uint32_t", result_value="127")
47025f6ca7SRaphael Isemann        self.expect_expr("bits.four", result_type="uint32_t", result_value="15")
4899451b44SJordan Rupprecht
49025f6ca7SRaphael Isemann        # Try accessing the different fields using variable paths.
50025f6ca7SRaphael Isemann        self.expect_var_path("bits.b1", type="uint32_t:1", value="1")
51025f6ca7SRaphael Isemann        self.expect_var_path("bits.b2", type="uint32_t:2", value="3")
52025f6ca7SRaphael Isemann        self.expect_var_path("bits.b4", type="uint32_t", value="15")
53025f6ca7SRaphael Isemann        self.expect_var_path("bits.b5", type="uint32_t:5", value="31")
54025f6ca7SRaphael Isemann        self.expect_var_path("bits.b7", type="uint32_t:7", value="127")
5599451b44SJordan Rupprecht
56025f6ca7SRaphael Isemann        # Check each field of MoreBits.
57025f6ca7SRaphael Isemann        more_bits_children = [
58025f6ca7SRaphael Isemann            ValueCheck(type="uint32_t:3", name="a", value="3"),
59025f6ca7SRaphael Isemann            ValueCheck(type="int:1", value="0"),
60025f6ca7SRaphael Isemann            ValueCheck(type="uint8_t:1", name="b", value="'\\0'"),
61025f6ca7SRaphael Isemann            ValueCheck(type="uint8_t:1", name="c", value="'\\x01'"),
62025f6ca7SRaphael Isemann            ValueCheck(type="uint8_t:1", name="d", value="'\\0'"),
63025f6ca7SRaphael Isemann        ]
64025f6ca7SRaphael Isemann        self.expect_var_path("more_bits", type="MoreBits", children=more_bits_children)
65025f6ca7SRaphael Isemann        self.expect_expr("more_bits", result_children=more_bits_children)
6699451b44SJordan Rupprecht
67025f6ca7SRaphael Isemann        self.expect_expr("more_bits.a", result_type="uint32_t", result_value="3")
68025f6ca7SRaphael Isemann        self.expect_expr("more_bits.b", result_type="uint8_t", result_value="'\\0'")
69025f6ca7SRaphael Isemann        self.expect_expr("more_bits.c", result_type="uint8_t", result_value="'\\x01'")
70025f6ca7SRaphael Isemann        self.expect_expr("more_bits.d", result_type="uint8_t", result_value="'\\0'")
7199451b44SJordan Rupprecht
72025f6ca7SRaphael Isemann        # Test a struct with several single bit fields.
73025f6ca7SRaphael Isemann        many_single_bits_children = [
74025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b1", value="1"),
75025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b2", value="0"),
76025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b3", value="0"),
77025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b4", value="0"),
78025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b5", value="1"),
79025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b6", value="0"),
80025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b7", value="1"),
81025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b8", value="0"),
82025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b9", value="0"),
83025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b10", value="0"),
84025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b11", value="0"),
85025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b12", value="0"),
86025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b13", value="1"),
87025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b14", value="0"),
88025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b15", value="0"),
89025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b16", value="0"),
90025f6ca7SRaphael Isemann            ValueCheck(type="uint16_t:1", name="b17", value="0"),
91025f6ca7SRaphael Isemann        ]
92*2238dcc3SJonas Devlieghere        self.expect_var_path(
93*2238dcc3SJonas Devlieghere            "many_single_bits",
94*2238dcc3SJonas Devlieghere            type="ManySingleBits",
95*2238dcc3SJonas Devlieghere            children=many_single_bits_children,
96*2238dcc3SJonas Devlieghere        )
97*2238dcc3SJonas Devlieghere        self.expect_expr(
98*2238dcc3SJonas Devlieghere            "many_single_bits",
99*2238dcc3SJonas Devlieghere            result_type="ManySingleBits",
100*2238dcc3SJonas Devlieghere            result_children=many_single_bits_children,
101*2238dcc3SJonas Devlieghere        )
10299451b44SJordan Rupprecht
103025f6ca7SRaphael Isemann        # Check a packed struct.
104025f6ca7SRaphael Isemann        self.expect_expr("packed.a", result_type="char", result_value="'a'")
105025f6ca7SRaphael Isemann        self.expect_expr("packed.b", result_type="uint32_t", result_value="10")
106*2238dcc3SJonas Devlieghere        self.expect_expr(
107*2238dcc3SJonas Devlieghere            "packed.c", result_type="uint32_t", result_value=str(int("7112233", 16))
108*2238dcc3SJonas Devlieghere        )
10999451b44SJordan Rupprecht
110025f6ca7SRaphael Isemann        # A packed struct with bitfield size > 32.
111*2238dcc3SJonas Devlieghere        self.expect(
112*2238dcc3SJonas Devlieghere            "v/x large_packed",
113*2238dcc3SJonas Devlieghere            VARIABLES_DISPLAYED_CORRECTLY,
114*2238dcc3SJonas Devlieghere            substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"],
115*2238dcc3SJonas Devlieghere        )
11699451b44SJordan Rupprecht
11797ca9ca1SPavel Labath        # Check reading a bitfield through a pointer in various ways (PR47743)
118*2238dcc3SJonas Devlieghere        self.expect(
119*2238dcc3SJonas Devlieghere            "v/x large_packed_ptr->b",
120*2238dcc3SJonas Devlieghere            substrs=["large_packed_ptr->b = 0x0000000dffffeeee"],
121*2238dcc3SJonas Devlieghere        )
122*2238dcc3SJonas Devlieghere        self.expect(
123*2238dcc3SJonas Devlieghere            "v/x large_packed_ptr[0].b",
124*2238dcc3SJonas Devlieghere            substrs=["large_packed_ptr[0].b = 0x0000000dffffeeee"],
125*2238dcc3SJonas Devlieghere        )
12697ca9ca1SPavel Labath
12719d64138SPavel Labath    # BitFields exhibit crashes in record layout on Windows
12819d64138SPavel Labath    # (http://llvm.org/pr21800)
12919d64138SPavel Labath    @skipIfWindows
13019d64138SPavel Labath    def test_expression_bug(self):
13119d64138SPavel Labath        # Ensure evaluating (emulating) an expression does not break bitfield
13219d64138SPavel Labath        # values for already parsed variables. The expression is run twice
13319d64138SPavel Labath        # because the very first expression can resume a target (to allocate
13419d64138SPavel Labath        # memory, etc.) even if it is not being jitted.
135025f6ca7SRaphael Isemann        self.run_to_main()
136025f6ca7SRaphael Isemann
137*2238dcc3SJonas Devlieghere        self.expect(
138*2238dcc3SJonas Devlieghere            "v/x large_packed",
139*2238dcc3SJonas Devlieghere            VARIABLES_DISPLAYED_CORRECTLY,
140*2238dcc3SJonas Devlieghere            substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"],
141*2238dcc3SJonas Devlieghere        )
142*2238dcc3SJonas Devlieghere        self.expect(
143*2238dcc3SJonas Devlieghere            "expr --allow-jit false  -- more_bits.a",
144*2238dcc3SJonas Devlieghere            VARIABLES_DISPLAYED_CORRECTLY,
145*2238dcc3SJonas Devlieghere            substrs=["uint32_t", "3"],
146*2238dcc3SJonas Devlieghere        )
147*2238dcc3SJonas Devlieghere        self.expect(
148*2238dcc3SJonas Devlieghere            "v/x large_packed",
149*2238dcc3SJonas Devlieghere            VARIABLES_DISPLAYED_CORRECTLY,
150*2238dcc3SJonas Devlieghere            substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"],
151*2238dcc3SJonas Devlieghere        )
152*2238dcc3SJonas Devlieghere        self.expect(
153*2238dcc3SJonas Devlieghere            "expr --allow-jit false  -- more_bits.a",
154*2238dcc3SJonas Devlieghere            VARIABLES_DISPLAYED_CORRECTLY,
155*2238dcc3SJonas Devlieghere            substrs=["uint32_t", "3"],
156*2238dcc3SJonas Devlieghere        )
157*2238dcc3SJonas Devlieghere        self.expect(
158*2238dcc3SJonas Devlieghere            "v/x large_packed",
159*2238dcc3SJonas Devlieghere            VARIABLES_DISPLAYED_CORRECTLY,
160*2238dcc3SJonas Devlieghere            substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"],
161*2238dcc3SJonas Devlieghere        )
16299451b44SJordan Rupprecht
163*2238dcc3SJonas Devlieghere    @add_test_categories(["pyapi"])
16499451b44SJordan Rupprecht    # BitFields exhibit crashes in record layout on Windows
16599451b44SJordan Rupprecht    # (http://llvm.org/pr21800)
16699451b44SJordan Rupprecht    @skipIfWindows
16799451b44SJordan Rupprecht    def test_and_python_api(self):
16899451b44SJordan Rupprecht        """Use Python APIs to inspect a bitfields variable."""
169025f6ca7SRaphael Isemann        self.run_to_main()
17099451b44SJordan Rupprecht
17199451b44SJordan Rupprecht        # Lookup the "bits" variable which contains 8 bitfields.
172025f6ca7SRaphael Isemann        bits = self.frame().FindVariable("bits")
17399451b44SJordan Rupprecht        self.DebugSBValue(bits)
174*2238dcc3SJonas Devlieghere        self.assertEqual(bits.GetTypeName(), "Bits")
175025f6ca7SRaphael Isemann        self.assertEqual(bits.GetNumChildren(), 10)
176025f6ca7SRaphael Isemann        self.assertEqual(bits.GetByteSize(), 32)
17799451b44SJordan Rupprecht
17899451b44SJordan Rupprecht        # Notice the pattern of int(b1.GetValue(), 0).  We pass a base of 0
17999451b44SJordan Rupprecht        # so that the proper radix is determined based on the contents of the
18099451b44SJordan Rupprecht        # string.
18199451b44SJordan Rupprecht        b1 = bits.GetChildMemberWithName("b1")
18299451b44SJordan Rupprecht        self.DebugSBValue(b1)
183025f6ca7SRaphael Isemann        self.assertEqual(b1.GetName(), "b1")
184025f6ca7SRaphael Isemann        self.assertEqual(b1.GetTypeName(), "uint32_t:1")
185025f6ca7SRaphael Isemann        self.assertTrue(b1.IsInScope())
186025f6ca7SRaphael Isemann        self.assertEqual(int(b1.GetValue(), 0), 1)
18799451b44SJordan Rupprecht
18899451b44SJordan Rupprecht        b7 = bits.GetChildMemberWithName("b7")
189025f6ca7SRaphael Isemann        self.assertEqual(b7.GetName(), "b7")
190025f6ca7SRaphael Isemann        self.assertEqual(b7.GetTypeName(), "uint32_t:7")
191025f6ca7SRaphael Isemann        self.assertTrue(b7.IsInScope())
192025f6ca7SRaphael Isemann        self.assertEqual(int(b7.GetValue(), 0), 127)
19399451b44SJordan Rupprecht
19499451b44SJordan Rupprecht        four = bits.GetChildMemberWithName("four")
195025f6ca7SRaphael Isemann        self.assertEqual(four.GetName(), "four")
196025f6ca7SRaphael Isemann        self.assertEqual(four.GetTypeName(), "uint32_t:4")
197025f6ca7SRaphael Isemann        self.assertTrue(four.IsInScope())
198025f6ca7SRaphael Isemann        self.assertEqual(int(four.GetValue(), 0), 15)
199