1"""Test C bitfields.""" 2 3 4 5import lldb 6from lldbsuite.test.decorators import * 7from lldbsuite.test.lldbtest import * 8from lldbsuite.test import lldbutil 9 10 11class TestCase(TestBase): 12 13 mydir = TestBase.compute_mydir(__file__) 14 15 def run_to_main(self): 16 self.build() 17 lldbutil.run_to_source_breakpoint(self, "// break here", lldb.SBFileSpec("main.c")) 18 19 # BitFields exhibit crashes in record layout on Windows 20 # (http://llvm.org/pr21800) 21 @skipIfWindows 22 def test_bits(self): 23 self.run_to_main() 24 25 # Check each field of Bits. 26 bits_children = [ 27 ValueCheck(type="int:1"), # Unnamed and uninitialized 28 ValueCheck(type="uint32_t:1", name="b1", value="1"), 29 ValueCheck(type="uint32_t:2", name="b2", value="3"), 30 ValueCheck(type="int:2"), # Unnamed and uninitialized 31 ValueCheck(type="uint32_t:3", name="b3", value="7"), 32 ValueCheck(type="uint32_t", name="b4", value="15"), 33 ValueCheck(type="uint32_t:5", name="b5", value="31"), 34 ValueCheck(type="uint32_t:6", name="b6", value="63"), 35 ValueCheck(type="uint32_t:7", name="b7", value="127"), 36 ValueCheck(type="uint32_t:4", name="four", value="15") 37 ] 38 self.expect_var_path("bits", type="Bits", children=bits_children) 39 self.expect_expr("bits", result_children=bits_children) 40 41 # Try accessing the different fields using the expression evaluator. 42 self.expect_expr("bits.b1", result_type="uint32_t", result_value="1") 43 self.expect_expr("bits.b2", result_type="uint32_t", result_value="3") 44 self.expect_expr("bits.b3", result_type="uint32_t", result_value="7") 45 self.expect_expr("bits.b4", result_type="uint32_t", result_value="15") 46 self.expect_expr("bits.b5", result_type="uint32_t", result_value="31") 47 self.expect_expr("bits.b6", result_type="uint32_t", result_value="63") 48 self.expect_expr("bits.b7", result_type="uint32_t", result_value="127") 49 self.expect_expr("bits.four", result_type="uint32_t", result_value="15") 50 51 # Try accessing the different fields using variable paths. 52 self.expect_var_path("bits.b1", type="uint32_t:1", value="1") 53 self.expect_var_path("bits.b2", type="uint32_t:2", value="3") 54 self.expect_var_path("bits.b4", type="uint32_t", value="15") 55 self.expect_var_path("bits.b5", type="uint32_t:5", value="31") 56 self.expect_var_path("bits.b7", type="uint32_t:7", value="127") 57 58 59 # Check each field of MoreBits. 60 more_bits_children = [ 61 ValueCheck(type="uint32_t:3", name="a", value="3"), 62 ValueCheck(type="int:1", value="0"), 63 ValueCheck(type="uint8_t:1", name="b", value="'\\0'"), 64 ValueCheck(type="uint8_t:1", name="c", value="'\\x01'"), 65 ValueCheck(type="uint8_t:1", name="d", value="'\\0'"), 66 ] 67 self.expect_var_path("more_bits", type="MoreBits", children=more_bits_children) 68 self.expect_expr("more_bits", result_children=more_bits_children) 69 70 self.expect_expr("more_bits.a", result_type="uint32_t", result_value="3") 71 self.expect_expr("more_bits.b", result_type="uint8_t", result_value="'\\0'") 72 self.expect_expr("more_bits.c", result_type="uint8_t", result_value="'\\x01'") 73 self.expect_expr("more_bits.d", result_type="uint8_t", result_value="'\\0'") 74 75 # Test a struct with several single bit fields. 76 many_single_bits_children = [ 77 ValueCheck(type="uint16_t:1", name="b1", value="1"), 78 ValueCheck(type="uint16_t:1", name="b2", value="0"), 79 ValueCheck(type="uint16_t:1", name="b3", value="0"), 80 ValueCheck(type="uint16_t:1", name="b4", value="0"), 81 ValueCheck(type="uint16_t:1", name="b5", value="1"), 82 ValueCheck(type="uint16_t:1", name="b6", value="0"), 83 ValueCheck(type="uint16_t:1", name="b7", value="1"), 84 ValueCheck(type="uint16_t:1", name="b8", value="0"), 85 ValueCheck(type="uint16_t:1", name="b9", value="0"), 86 ValueCheck(type="uint16_t:1", name="b10", value="0"), 87 ValueCheck(type="uint16_t:1", name="b11", value="0"), 88 ValueCheck(type="uint16_t:1", name="b12", value="0"), 89 ValueCheck(type="uint16_t:1", name="b13", value="1"), 90 ValueCheck(type="uint16_t:1", name="b14", value="0"), 91 ValueCheck(type="uint16_t:1", name="b15", value="0"), 92 ValueCheck(type="uint16_t:1", name="b16", value="0"), 93 ValueCheck(type="uint16_t:1", name="b17", value="0"), 94 ] 95 self.expect_var_path("many_single_bits", type="ManySingleBits", children=many_single_bits_children) 96 self.expect_expr("many_single_bits", result_type="ManySingleBits", 97 result_children=many_single_bits_children) 98 99 # Check a packed struct. 100 self.expect_expr("packed.a", result_type="char", result_value="'a'") 101 self.expect_expr("packed.b", result_type="uint32_t", result_value="10") 102 self.expect_expr("packed.c", result_type="uint32_t", result_value=str(int("7112233", 16))) 103 104 # A packed struct with bitfield size > 32. 105 self.expect("v/x large_packed", VARIABLES_DISPLAYED_CORRECTLY, 106 substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"]) 107 108 # Check reading a bitfield through a pointer in various ways (PR47743) 109 self.expect("v/x large_packed_ptr->b", 110 substrs=["large_packed_ptr->b = 0x0000000dffffeeee"]) 111 self.expect("v/x large_packed_ptr[0].b", 112 substrs=["large_packed_ptr[0].b = 0x0000000dffffeeee"]) 113 114 # BitFields exhibit crashes in record layout on Windows 115 # (http://llvm.org/pr21800) 116 @skipIfWindows 117 def test_expression_bug(self): 118 # Ensure evaluating (emulating) an expression does not break bitfield 119 # values for already parsed variables. The expression is run twice 120 # because the very first expression can resume a target (to allocate 121 # memory, etc.) even if it is not being jitted. 122 self.run_to_main() 123 124 self.expect("v/x large_packed", VARIABLES_DISPLAYED_CORRECTLY, 125 substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"]) 126 self.expect("expr --allow-jit false -- more_bits.a", VARIABLES_DISPLAYED_CORRECTLY, 127 substrs=['uint32_t', '3']) 128 self.expect("v/x large_packed", VARIABLES_DISPLAYED_CORRECTLY, 129 substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"]) 130 self.expect("expr --allow-jit false -- more_bits.a", VARIABLES_DISPLAYED_CORRECTLY, 131 substrs=['uint32_t', '3']) 132 self.expect("v/x large_packed", VARIABLES_DISPLAYED_CORRECTLY, 133 substrs=["a = 0x0000000cbbbbaaaa", "b = 0x0000000dffffeee"]) 134 135 @add_test_categories(['pyapi']) 136 # BitFields exhibit crashes in record layout on Windows 137 # (http://llvm.org/pr21800) 138 @skipIfWindows 139 def test_and_python_api(self): 140 """Use Python APIs to inspect a bitfields variable.""" 141 self.run_to_main() 142 143 # Lookup the "bits" variable which contains 8 bitfields. 144 bits = self.frame().FindVariable("bits") 145 self.DebugSBValue(bits) 146 self.assertEqual(bits.GetTypeName(), 'Bits') 147 self.assertEqual(bits.GetNumChildren(), 10) 148 self.assertEqual(bits.GetByteSize(), 32) 149 150 # Notice the pattern of int(b1.GetValue(), 0). We pass a base of 0 151 # so that the proper radix is determined based on the contents of the 152 # string. 153 b1 = bits.GetChildMemberWithName("b1") 154 self.DebugSBValue(b1) 155 self.assertEqual(b1.GetName(), "b1") 156 self.assertEqual(b1.GetTypeName(), "uint32_t:1") 157 self.assertTrue(b1.IsInScope()) 158 self.assertEqual(int(b1.GetValue(), 0), 1) 159 160 b7 = bits.GetChildMemberWithName("b7") 161 self.assertEqual(b7.GetName(), "b7") 162 self.assertEqual(b7.GetTypeName(), "uint32_t:7") 163 self.assertTrue(b7.IsInScope()) 164 self.assertEqual(int(b7.GetValue(), 0), 127) 165 166 four = bits.GetChildMemberWithName("four") 167 self.assertEqual(four.GetName(), "four") 168 self.assertEqual(four.GetTypeName(), "uint32_t:4") 169 self.assertTrue(four.IsInScope()) 170 self.assertEqual(int(four.GetValue(), 0), 15)