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