1 """Show bitfields and check that they display correctly."""
3 from __future__ import print_function
9 from lldbsuite.test.decorators import *
10 from lldbsuite.test.lldbtest import *
11 from lldbsuite.test import lldbutil
13 class BitfieldsTestCase(TestBase):
15 mydir = TestBase.compute_mydir(__file__)
18 # Call super's setUp().
20 # Find the line number to break inside main().
21 self.line = line_number('main.c', '// Set break point at this line.')
23 @skipIfWindows # BitFields exhibit crashes in record layout on Windows (http://llvm.org/pr21800)
24 def test_and_run_command(self):
25 """Test 'frame variable ...' on a variable with bitfields."""
27 exe = os.path.join(os.getcwd(), "a.out")
28 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
30 # Break inside the main.
31 lldbutil.run_break_set_by_file_and_line (self, "main.c", self.line, num_expected_locations=1, loc_exact=True)
33 self.runCmd("run", RUN_SUCCEEDED)
35 # The stop reason of the thread should be breakpoint.
36 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
38 'stop reason = breakpoint'])
40 # The breakpoint should have a hit count of 1.
41 self.expect("breakpoint list -f", BREAKPOINT_HIT_ONCE,
42 substrs = [' resolved, hit count = 1'])
44 # This should display correctly.
45 self.expect("frame variable --show-types bits", VARIABLES_DISPLAYED_CORRECTLY,
46 substrs = ['(uint32_t:1) b1 = 1',
47 '(uint32_t:2) b2 = 3',
48 '(uint32_t:3) b3 = 7',
50 '(uint32_t:5) b5 = 31',
51 '(uint32_t:6) b6 = 63',
52 '(uint32_t:7) b7 = 127',
53 '(uint32_t:4) four = 15'])
56 # rdar://problem/8348251
57 self.expect("frame variable --show-types", VARIABLES_DISPLAYED_CORRECTLY,
58 substrs = ['(uint32_t:1) b1 = 1',
59 '(uint32_t:2) b2 = 3',
60 '(uint32_t:3) b3 = 7',
62 '(uint32_t:5) b5 = 31',
63 '(uint32_t:6) b6 = 63',
64 '(uint32_t:7) b7 = 127',
65 '(uint32_t:4) four = 15'])
67 self.expect("expr (bits.b1)", VARIABLES_DISPLAYED_CORRECTLY,
68 substrs = ['uint32_t', '1'])
69 self.expect("expr (bits.b2)", VARIABLES_DISPLAYED_CORRECTLY,
70 substrs = ['uint32_t', '3'])
71 self.expect("expr (bits.b3)", VARIABLES_DISPLAYED_CORRECTLY,
72 substrs = ['uint32_t', '7'])
73 self.expect("expr (bits.b4)", VARIABLES_DISPLAYED_CORRECTLY,
74 substrs = ['uint32_t', '15'])
75 self.expect("expr (bits.b5)", VARIABLES_DISPLAYED_CORRECTLY,
76 substrs = ['uint32_t', '31'])
77 self.expect("expr (bits.b6)", VARIABLES_DISPLAYED_CORRECTLY,
78 substrs = ['uint32_t', '63'])
79 self.expect("expr (bits.b7)", VARIABLES_DISPLAYED_CORRECTLY,
80 substrs = ['uint32_t', '127'])
81 self.expect("expr (bits.four)", VARIABLES_DISPLAYED_CORRECTLY,
82 substrs = ['uint32_t', '15'])
84 self.expect("frame variable --show-types more_bits", VARIABLES_DISPLAYED_CORRECTLY,
85 substrs = ['(uint32_t:3) a = 3',
86 '(uint8_t:1) b = \'\\0\'',
87 '(uint8_t:1) c = \'\\x01\'',
88 '(uint8_t:1) d = \'\\0\''])
90 self.expect("expr (more_bits.a)", VARIABLES_DISPLAYED_CORRECTLY,
91 substrs = ['uint32_t', '3'])
92 self.expect("expr (more_bits.b)", VARIABLES_DISPLAYED_CORRECTLY,
93 substrs = ['uint8_t', '\\0'])
94 self.expect("expr (more_bits.c)", VARIABLES_DISPLAYED_CORRECTLY,
95 substrs = ['uint8_t', '\\x01'])
96 self.expect("expr (more_bits.d)", VARIABLES_DISPLAYED_CORRECTLY,
97 substrs = ['uint8_t', '\\0'])
99 self.expect("expr (packed.a)", VARIABLES_DISPLAYED_CORRECTLY,
100 substrs = ['char', "'a'"])
101 self.expect("expr (packed.b)", VARIABLES_DISPLAYED_CORRECTLY,
102 substrs = ['uint32_t', "10"])
103 self.expect("expr/x (packed.c)", VARIABLES_DISPLAYED_CORRECTLY,
104 substrs = ['uint32_t', "7112233"])
107 @add_test_categories(['pyapi'])
108 @skipIfWindows # BitFields exhibit crashes in record layout on Windows (http://llvm.org/pr21800)
109 @expectedFailureAll("llvm.org/pr27510", oslist=["linux"], compiler="clang", compiler_version=[">=", "3.9"])
110 def test_and_python_api(self):
111 """Use Python APIs to inspect a bitfields variable."""
113 exe = os.path.join(os.getcwd(), "a.out")
115 target = self.dbg.CreateTarget(exe)
116 self.assertTrue(target, VALID_TARGET)
118 breakpoint = target.BreakpointCreateByLocation("main.c", self.line)
119 self.assertTrue(breakpoint, VALID_BREAKPOINT)
121 process = target.LaunchSimple (None, None, self.get_process_working_directory())
122 self.assertTrue(process, PROCESS_IS_VALID)
124 # The stop reason of the thread should be breakpoint.
125 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
126 self.assertIsNotNone(thread)
128 # The breakpoint should have a hit count of 1.
129 self.assertEqual(breakpoint.GetHitCount(), 1, BREAKPOINT_HIT_ONCE)
131 # Lookup the "bits" variable which contains 8 bitfields.
132 frame = thread.GetFrameAtIndex(0)
133 bits = frame.FindVariable("bits")
134 self.DebugSBValue(bits)
135 self.assertTrue(bits.GetTypeName() == 'Bits', "bits.GetTypeName() == 'Bits'");
136 self.assertTrue(bits.GetNumChildren() == 10, "bits.GetNumChildren() == 10");
137 test_compiler = self.getCompiler()
138 self.assertTrue(bits.GetByteSize() == 32, "bits.GetByteSize() == 32");
140 # Notice the pattern of int(b1.GetValue(), 0). We pass a base of 0
141 # so that the proper radix is determined based on the contents of the
143 b1 = bits.GetChildMemberWithName("b1")
144 self.DebugSBValue(b1)
145 self.assertTrue(b1.GetName() == "b1" and
146 b1.GetTypeName() == "uint32_t:1" and
148 int(b1.GetValue(), 0) == 1,
149 'bits.b1 has type uint32_t:1, is in scope, and == 1')
151 b7 = bits.GetChildMemberWithName("b7")
152 self.DebugSBValue(b7)
153 self.assertTrue(b7.GetName() == "b7" and
154 b7.GetTypeName() == "uint32_t:7" and
156 int(b7.GetValue(), 0) == 127,
157 'bits.b7 has type uint32_t:7, is in scope, and == 127')
159 four = bits.GetChildMemberWithName("four")
160 self.DebugSBValue(four)
161 self.assertTrue(four.GetName() == "four" and
162 four.GetTypeName() == "uint32_t:4" and
164 int(four.GetValue(), 0) == 15,
165 'bits.four has type uint32_t:4, is in scope, and == 15')
167 # Now kill the process, and we are done.
168 rc = target.GetProcess().Kill()
169 self.assertTrue(rc.Success())