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
151    @add_test_categories(['pyapi'])
152    # BitFields exhibit crashes in record layout on Windows
153    # (http://llvm.org/pr21800)
154    @skipIfWindows
155    def test_and_python_api(self):
156        """Use Python APIs to inspect a bitfields variable."""
157        self.build()
158        exe = self.getBuildArtifact("a.out")
159
160        target = self.dbg.CreateTarget(exe)
161        self.assertTrue(target, VALID_TARGET)
162
163        breakpoint = target.BreakpointCreateByLocation("main.c", self.line)
164        self.assertTrue(breakpoint, VALID_BREAKPOINT)
165
166        process = target.LaunchSimple(
167            None, None, self.get_process_working_directory())
168        self.assertTrue(process, PROCESS_IS_VALID)
169
170        # The stop reason of the thread should be breakpoint.
171        thread = lldbutil.get_stopped_thread(
172            process, lldb.eStopReasonBreakpoint)
173        self.assertIsNotNone(thread)
174
175        # The breakpoint should have a hit count of 1.
176        self.assertEqual(breakpoint.GetHitCount(), 1, BREAKPOINT_HIT_ONCE)
177
178        # Lookup the "bits" variable which contains 8 bitfields.
179        frame = thread.GetFrameAtIndex(0)
180        bits = frame.FindVariable("bits")
181        self.DebugSBValue(bits)
182        self.assertTrue(
183            bits.GetTypeName() == 'Bits',
184            "bits.GetTypeName() == 'Bits'")
185        self.assertTrue(
186            bits.GetNumChildren() == 10,
187            "bits.GetNumChildren() == 10")
188        test_compiler = self.getCompiler()
189        self.assertTrue(bits.GetByteSize() == 32, "bits.GetByteSize() == 32")
190
191        # Notice the pattern of int(b1.GetValue(), 0).  We pass a base of 0
192        # so that the proper radix is determined based on the contents of the
193        # string.
194        b1 = bits.GetChildMemberWithName("b1")
195        self.DebugSBValue(b1)
196        self.assertTrue(b1.GetName() == "b1" and
197                        b1.GetTypeName() == "uint32_t:1" and
198                        b1.IsInScope() and
199                        int(b1.GetValue(), 0) == 1,
200                        'bits.b1 has type uint32_t:1, is in scope, and == 1')
201
202        b7 = bits.GetChildMemberWithName("b7")
203        self.DebugSBValue(b7)
204        self.assertTrue(b7.GetName() == "b7" and
205                        b7.GetTypeName() == "uint32_t:7" and
206                        b7.IsInScope() and
207                        int(b7.GetValue(), 0) == 127,
208                        'bits.b7 has type uint32_t:7, is in scope, and == 127')
209
210        four = bits.GetChildMemberWithName("four")
211        self.DebugSBValue(four)
212        self.assertTrue(four.GetName() == "four" and
213                        four.GetTypeName() == "uint32_t:4" and
214                        four.IsInScope() and
215                        int(four.GetValue(), 0) == 15,
216                        'bits.four has type uint32_t:4, is in scope, and == 15')
217
218        # Now kill the process, and we are done.
219        rc = target.GetProcess().Kill()
220        self.assertTrue(rc.Success())
221