1"""Show bitfields and check that they display correctly."""
2
3
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class BitfieldsTestCase(TestBase):
12
13    mydir = TestBase.compute_mydir(__file__)
14
15    def setUp(self):
16        # Call super's setUp().
17        TestBase.setUp(self)
18        # Find the line number to break inside main().
19        self.line = line_number('main.c', '// Set break point at this line.')
20
21    # BitFields exhibit crashes in record layout on Windows
22    # (http://llvm.org/pr21800)
23    @skipIfWindows
24    def test_and_run_command(self):
25        """Test 'frame variable ...' on a variable with bitfields."""
26        self.build()
27        exe = self.getBuildArtifact("a.out")
28        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
29
30        # Break inside the main.
31        lldbutil.run_break_set_by_file_and_line(
32            self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
33
34        self.runCmd("run", RUN_SUCCEEDED)
35
36        # The stop reason of the thread should be breakpoint.
37        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
38                    substrs=['stopped',
39                             'stop reason = breakpoint'])
40
41        # The breakpoint should have a hit count of 1.
42        self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
43                    substrs=[' resolved, hit count = 1'])
44
45        # This should display correctly.
46        self.expect(
47            "frame variable --show-types bits",
48            VARIABLES_DISPLAYED_CORRECTLY,
49            substrs=[
50                '(uint32_t:1) b1 = 1',
51                '(uint32_t:2) b2 = 3',
52                '(uint32_t:3) b3 = 7',
53                '(uint32_t) b4 = 15',
54                '(uint32_t:5) b5 = 31',
55                '(uint32_t:6) b6 = 63',
56                '(uint32_t:7) b7 = 127',
57                '(uint32_t:4) four = 15'])
58
59        # And so should this.
60        # rdar://problem/8348251
61        self.expect(
62            "frame variable --show-types",
63            VARIABLES_DISPLAYED_CORRECTLY,
64            substrs=[
65                '(uint32_t:1) b1 = 1',
66                '(uint32_t:2) b2 = 3',
67                '(uint32_t:3) b3 = 7',
68                '(uint32_t) b4 = 15',
69                '(uint32_t:5) b5 = 31',
70                '(uint32_t:6) b6 = 63',
71                '(uint32_t:7) b7 = 127',
72                '(uint32_t:4) four = 15'])
73
74        self.expect("expr (bits.b1)", VARIABLES_DISPLAYED_CORRECTLY,
75                    substrs=['uint32_t', '1'])
76        self.expect("expr (bits.b2)", VARIABLES_DISPLAYED_CORRECTLY,
77                    substrs=['uint32_t', '3'])
78        self.expect("expr (bits.b3)", VARIABLES_DISPLAYED_CORRECTLY,
79                    substrs=['uint32_t', '7'])
80        self.expect("expr (bits.b4)", VARIABLES_DISPLAYED_CORRECTLY,
81                    substrs=['uint32_t', '15'])
82        self.expect("expr (bits.b5)", VARIABLES_DISPLAYED_CORRECTLY,
83                    substrs=['uint32_t', '31'])
84        self.expect("expr (bits.b6)", VARIABLES_DISPLAYED_CORRECTLY,
85                    substrs=['uint32_t', '63'])
86        self.expect("expr (bits.b7)", VARIABLES_DISPLAYED_CORRECTLY,
87                    substrs=['uint32_t', '127'])
88        self.expect("expr (bits.four)", VARIABLES_DISPLAYED_CORRECTLY,
89                    substrs=['uint32_t', '15'])
90
91        self.expect(
92            "frame variable --show-types more_bits",
93            VARIABLES_DISPLAYED_CORRECTLY,
94            substrs=[
95                '(uint32_t:3) a = 3',
96                '(uint8_t:1) b = \'\\0\'',
97                '(uint8_t:1) c = \'\\x01\'',
98                '(uint8_t:1) d = \'\\0\''])
99
100        self.expect("expr (more_bits.a)", VARIABLES_DISPLAYED_CORRECTLY,
101                    substrs=['uint32_t', '3'])
102        self.expect("expr (more_bits.b)", VARIABLES_DISPLAYED_CORRECTLY,
103                    substrs=['uint8_t', '\\0'])
104        self.expect("expr (more_bits.c)", VARIABLES_DISPLAYED_CORRECTLY,
105                    substrs=['uint8_t', '\\x01'])
106        self.expect("expr (more_bits.d)", VARIABLES_DISPLAYED_CORRECTLY,
107                    substrs=['uint8_t', '\\0'])
108
109        self.expect("expr (packed.a)", VARIABLES_DISPLAYED_CORRECTLY,
110                    substrs=['char', "'a'"])
111        self.expect("expr (packed.b)", VARIABLES_DISPLAYED_CORRECTLY,
112                    substrs=['uint32_t', "10"])
113        self.expect("expr/x (packed.c)", VARIABLES_DISPLAYED_CORRECTLY,
114                    substrs=['uint32_t', "7112233"])
115
116        for bit in range(1,18):
117            expected = "1" if bit in [1, 5, 7, 13] else "0"
118            self.expect("expr even_more_bits.b" + str(bit), VARIABLES_DISPLAYED_CORRECTLY,
119                    substrs=['uint8_t', expected])
120
121        for bit in [3, 10, 14]:
122            self.expect("expr even_more_bits.b" + str(bit) + " = 1", VARIABLES_DISPLAYED_CORRECTLY,
123                    substrs=['uint8_t', "1"])
124
125        self.expect(
126            "frame variable --show-types even_more_bits",
127            VARIABLES_DISPLAYED_CORRECTLY,
128            substrs=[
129                '(uint8_t:1) b1 = \'\\x01\'',
130                '(uint8_t:1) b2 = \'\\0\'',
131                '(uint8_t:1) b3 = \'\\x01\'',
132                '(uint8_t:1) b4 = \'\\0\'',
133                '(uint8_t:1) b5 = \'\\x01\'',
134                '(uint8_t:1) b6 = \'\\0\'',
135                '(uint8_t:1) b7 = \'\\x01\'',
136                '(uint8_t:1) b8 = \'\\0\'',
137                '(uint8_t:1) b9 = \'\\0\'',
138                '(uint8_t:1) b10 = \'\\x01\'',
139                '(uint8_t:1) b12 = \'\\0\'',
140                '(uint8_t:1) b13 = \'\\x01\'',
141                '(uint8_t:1) b14 = \'\\x01\'',
142                '(uint8_t:1) b15 = \'\\0\'',
143                '(uint8_t:1) b16 = \'\\0\'',
144                '(uint8_t:1) b17 = \'\\0\'',
145                ])
146
147        self.expect("v/x large_packed", VARIABLES_DISPLAYED_CORRECTLY,
148                    substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"])
149
150        # Check reading a bitfield through a pointer in various ways (PR47743)
151        self.expect("v/x large_packed_ptr->b",
152                substrs=["large_packed_ptr->b = 0x0000000dffffeeee"])
153        self.expect("v/x large_packed_ptr[0].b",
154                substrs=["large_packed_ptr[0].b = 0x0000000dffffeeee"])
155
156    # BitFields exhibit crashes in record layout on Windows
157    # (http://llvm.org/pr21800)
158    @skipIfWindows
159    def test_expression_bug(self):
160        # Ensure evaluating (emulating) an expression does not break bitfield
161        # values for already parsed variables. The expression is run twice
162        # because the very first expression can resume a target (to allocate
163        # memory, etc.) even if it is not being jitted.
164        self.build()
165        lldbutil.run_to_line_breakpoint(self, lldb.SBFileSpec("main.c"),
166                self.line)
167        self.expect("v/x large_packed", VARIABLES_DISPLAYED_CORRECTLY,
168                    substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"])
169        self.expect("expr --allow-jit false  -- more_bits.a", VARIABLES_DISPLAYED_CORRECTLY,
170                    substrs=['uint32_t', '3'])
171        self.expect("v/x large_packed", VARIABLES_DISPLAYED_CORRECTLY,
172                    substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"])
173        self.expect("expr --allow-jit false  -- more_bits.a", VARIABLES_DISPLAYED_CORRECTLY,
174                    substrs=['uint32_t', '3'])
175        self.expect("v/x large_packed", VARIABLES_DISPLAYED_CORRECTLY,
176                    substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"])
177
178    @add_test_categories(['pyapi'])
179    # BitFields exhibit crashes in record layout on Windows
180    # (http://llvm.org/pr21800)
181    @skipIfWindows
182    def test_and_python_api(self):
183        """Use Python APIs to inspect a bitfields variable."""
184        self.build()
185        exe = self.getBuildArtifact("a.out")
186
187        target = self.dbg.CreateTarget(exe)
188        self.assertTrue(target, VALID_TARGET)
189
190        breakpoint = target.BreakpointCreateByLocation("main.c", self.line)
191        self.assertTrue(breakpoint, VALID_BREAKPOINT)
192
193        process = target.LaunchSimple(
194            None, None, self.get_process_working_directory())
195        self.assertTrue(process, PROCESS_IS_VALID)
196
197        # The stop reason of the thread should be breakpoint.
198        thread = lldbutil.get_stopped_thread(
199            process, lldb.eStopReasonBreakpoint)
200        self.assertIsNotNone(thread)
201
202        # The breakpoint should have a hit count of 1.
203        self.assertEqual(breakpoint.GetHitCount(), 1, BREAKPOINT_HIT_ONCE)
204
205        # Lookup the "bits" variable which contains 8 bitfields.
206        frame = thread.GetFrameAtIndex(0)
207        bits = frame.FindVariable("bits")
208        self.DebugSBValue(bits)
209        self.assertTrue(
210            bits.GetTypeName() == 'Bits',
211            "bits.GetTypeName() == 'Bits'")
212        self.assertTrue(
213            bits.GetNumChildren() == 10,
214            "bits.GetNumChildren() == 10")
215        test_compiler = self.getCompiler()
216        self.assertTrue(bits.GetByteSize() == 32, "bits.GetByteSize() == 32")
217
218        # Notice the pattern of int(b1.GetValue(), 0).  We pass a base of 0
219        # so that the proper radix is determined based on the contents of the
220        # string.
221        b1 = bits.GetChildMemberWithName("b1")
222        self.DebugSBValue(b1)
223        self.assertTrue(b1.GetName() == "b1" and
224                        b1.GetTypeName() == "uint32_t:1" and
225                        b1.IsInScope() and
226                        int(b1.GetValue(), 0) == 1,
227                        'bits.b1 has type uint32_t:1, is in scope, and == 1')
228
229        b7 = bits.GetChildMemberWithName("b7")
230        self.DebugSBValue(b7)
231        self.assertTrue(b7.GetName() == "b7" and
232                        b7.GetTypeName() == "uint32_t:7" and
233                        b7.IsInScope() and
234                        int(b7.GetValue(), 0) == 127,
235                        'bits.b7 has type uint32_t:7, is in scope, and == 127')
236
237        four = bits.GetChildMemberWithName("four")
238        self.DebugSBValue(four)
239        self.assertTrue(four.GetName() == "four" and
240                        four.GetTypeName() == "uint32_t:4" and
241                        four.IsInScope() and
242                        int(four.GetValue(), 0) == 15,
243                        'bits.four has type uint32_t:4, is in scope, and == 15')
244
245        # Now kill the process, and we are done.
246        rc = target.GetProcess().Kill()
247        self.assertTrue(rc.Success())
248