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 # BitFields exhibit crashes in record layout on Windows 151 # (http://llvm.org/pr21800) 152 @skipIfWindows 153 def test_expression_bug(self): 154 # Ensure evaluating (emulating) an expression does not break bitfield 155 # values for already parsed variables. The expression is run twice 156 # because the very first expression can resume a target (to allocate 157 # memory, etc.) even if it is not being jitted. 158 self.build() 159 lldbutil.run_to_line_breakpoint(self, lldb.SBFileSpec("main.c"), 160 self.line) 161 self.expect("v/x large_packed", VARIABLES_DISPLAYED_CORRECTLY, 162 substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"]) 163 self.expect("expr --allow-jit false -- more_bits.a", VARIABLES_DISPLAYED_CORRECTLY, 164 substrs=['uint32_t', '3']) 165 self.expect("v/x large_packed", VARIABLES_DISPLAYED_CORRECTLY, 166 substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"]) 167 self.expect("expr --allow-jit false -- more_bits.a", VARIABLES_DISPLAYED_CORRECTLY, 168 substrs=['uint32_t', '3']) 169 self.expect("v/x large_packed", VARIABLES_DISPLAYED_CORRECTLY, 170 substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"]) 171 172 @add_test_categories(['pyapi']) 173 # BitFields exhibit crashes in record layout on Windows 174 # (http://llvm.org/pr21800) 175 @skipIfWindows 176 def test_and_python_api(self): 177 """Use Python APIs to inspect a bitfields variable.""" 178 self.build() 179 exe = self.getBuildArtifact("a.out") 180 181 target = self.dbg.CreateTarget(exe) 182 self.assertTrue(target, VALID_TARGET) 183 184 breakpoint = target.BreakpointCreateByLocation("main.c", self.line) 185 self.assertTrue(breakpoint, VALID_BREAKPOINT) 186 187 process = target.LaunchSimple( 188 None, None, self.get_process_working_directory()) 189 self.assertTrue(process, PROCESS_IS_VALID) 190 191 # The stop reason of the thread should be breakpoint. 192 thread = lldbutil.get_stopped_thread( 193 process, lldb.eStopReasonBreakpoint) 194 self.assertIsNotNone(thread) 195 196 # The breakpoint should have a hit count of 1. 197 self.assertEqual(breakpoint.GetHitCount(), 1, BREAKPOINT_HIT_ONCE) 198 199 # Lookup the "bits" variable which contains 8 bitfields. 200 frame = thread.GetFrameAtIndex(0) 201 bits = frame.FindVariable("bits") 202 self.DebugSBValue(bits) 203 self.assertTrue( 204 bits.GetTypeName() == 'Bits', 205 "bits.GetTypeName() == 'Bits'") 206 self.assertTrue( 207 bits.GetNumChildren() == 10, 208 "bits.GetNumChildren() == 10") 209 test_compiler = self.getCompiler() 210 self.assertTrue(bits.GetByteSize() == 32, "bits.GetByteSize() == 32") 211 212 # Notice the pattern of int(b1.GetValue(), 0). We pass a base of 0 213 # so that the proper radix is determined based on the contents of the 214 # string. 215 b1 = bits.GetChildMemberWithName("b1") 216 self.DebugSBValue(b1) 217 self.assertTrue(b1.GetName() == "b1" and 218 b1.GetTypeName() == "uint32_t:1" and 219 b1.IsInScope() and 220 int(b1.GetValue(), 0) == 1, 221 'bits.b1 has type uint32_t:1, is in scope, and == 1') 222 223 b7 = bits.GetChildMemberWithName("b7") 224 self.DebugSBValue(b7) 225 self.assertTrue(b7.GetName() == "b7" and 226 b7.GetTypeName() == "uint32_t:7" and 227 b7.IsInScope() and 228 int(b7.GetValue(), 0) == 127, 229 'bits.b7 has type uint32_t:7, is in scope, and == 127') 230 231 four = bits.GetChildMemberWithName("four") 232 self.DebugSBValue(four) 233 self.assertTrue(four.GetName() == "four" and 234 four.GetTypeName() == "uint32_t:4" and 235 four.IsInScope() and 236 int(four.GetValue(), 0) == 15, 237 'bits.four has type uint32_t:4, is in scope, and == 15') 238 239 # Now kill the process, and we are done. 240 rc = target.GetProcess().Kill() 241 self.assertTrue(rc.Success()) 242