1""" 2Test lldb data formatter subsystem. 3""" 4 5 6import lldb 7from lldbsuite.test.decorators import * 8from lldbsuite.test.lldbtest import * 9from lldbsuite.test import lldbutil 10 11USE_LIBSTDCPP = "USE_LIBSTDCPP" 12USE_LIBCPP = "USE_LIBCPP" 13 14 15class GenericSetDataFormatterTestCase(TestBase): 16 def setUp(self): 17 TestBase.setUp(self) 18 self.namespace = "std" 19 20 def findVariable(self, name): 21 var = self.frame().FindVariable(name) 22 self.assertTrue(var.IsValid()) 23 return var 24 25 def getVariableType(self, name): 26 var = self.findVariable(name) 27 return var.GetType().GetDisplayTypeName() 28 29 def check(self, var_name, size): 30 var = self.findVariable(var_name) 31 self.assertEqual(var.GetNumChildren(), size) 32 children = [] 33 for i in range(size): 34 child = var.GetChildAtIndex(i) 35 children.append(ValueCheck(value=child.GetValue())) 36 self.expect_var_path( 37 var_name, type=self.getVariableType(var_name), children=children 38 ) 39 40 def do_test_with_run_command(self, stdlib_type): 41 """Test that that file and class static variables display correctly.""" 42 self.build(dictionary={stdlib_type: "1"}) 43 (self.target, process, _, bkpt) = lldbutil.run_to_source_breakpoint( 44 self, "Set break point at this line.", lldb.SBFileSpec("main.cpp", False) 45 ) 46 47 # This is the function to remove the custom formats in order to have a 48 # clean slate for the next test case. 49 def cleanup(): 50 self.runCmd("type format clear", check=False) 51 self.runCmd("type summary clear", check=False) 52 self.runCmd("type filter clear", check=False) 53 self.runCmd("type synth clear", check=False) 54 self.runCmd("settings set target.max-children-count 256", check=False) 55 56 # Execute the cleanup function during test case tear down. 57 self.addTearDownHook(cleanup) 58 59 ii_type = self.getVariableType("ii") 60 self.assertTrue( 61 ii_type.startswith(self.namespace + "::set"), "Type: " + ii_type 62 ) 63 64 self.expect("frame variable ii", substrs=["size=0", "{}"]) 65 lldbutil.continue_to_breakpoint(process, bkpt) 66 self.expect( 67 "frame variable ii", 68 substrs=[ 69 "size=6", 70 "[0] = 0", 71 "[1] = 1", 72 "[2] = 2", 73 "[3] = 3", 74 "[4] = 4", 75 "[5] = 5", 76 ], 77 ) 78 lldbutil.continue_to_breakpoint(process, bkpt) 79 self.check("ii", 7) 80 81 lldbutil.continue_to_breakpoint(process, bkpt) 82 self.expect("frame variable ii", substrs=["size=0", "{}"]) 83 lldbutil.continue_to_breakpoint(process, bkpt) 84 self.expect("frame variable ii", substrs=["size=0", "{}"]) 85 86 ss_type = self.getVariableType("ss") 87 self.assertTrue( 88 ii_type.startswith(self.namespace + "::set"), "Type: " + ss_type 89 ) 90 91 self.expect("frame variable ss", substrs=["size=0", "{}"]) 92 lldbutil.continue_to_breakpoint(process, bkpt) 93 self.expect( 94 "frame variable ss", 95 substrs=["size=2", '[0] = "a"', '[1] = "a very long string is right here"'], 96 ) 97 lldbutil.continue_to_breakpoint(process, bkpt) 98 self.expect( 99 "frame variable ss", 100 substrs=[ 101 "size=4", 102 '[0] = "a"', 103 '[1] = "a very long string is right here"', 104 '[2] = "b"', 105 '[3] = "c"', 106 ], 107 ) 108 self.expect( 109 "expression ss", 110 substrs=[ 111 "size=4", 112 '[0] = "a"', 113 '[1] = "a very long string is right here"', 114 '[2] = "b"', 115 '[3] = "c"', 116 ], 117 ) 118 self.expect("frame variable ss[2]", substrs=[' = "b"']) 119 lldbutil.continue_to_breakpoint(process, bkpt) 120 self.expect( 121 "frame variable ss", 122 substrs=[ 123 "size=3", 124 '[0] = "a"', 125 '[1] = "a very long string is right here"', 126 '[2] = "c"', 127 ], 128 ) 129 self.check("ss", 3) 130 131 @add_test_categories(["libstdcxx"]) 132 def test_with_run_command_libstdcpp(self): 133 self.do_test_with_run_command(USE_LIBSTDCPP) 134 135 @add_test_categories(["libc++"]) 136 def test_with_run_command_libcpp(self): 137 self.do_test_with_run_command(USE_LIBCPP) 138 139 def do_test_ref_and_ptr(self, stdlib_type): 140 """Test that the data formatters work on ref and ptr.""" 141 self.build() 142 (self.target, process, _, bkpt) = lldbutil.run_to_source_breakpoint( 143 self, 144 "Stop here to check by ref and ptr.", 145 lldb.SBFileSpec("main.cpp", False), 146 ) 147 # The reference should print just like the value: 148 self.check("ref", 7) 149 self.check("ptr", 7) 150 151 self.expect("frame variable ptr", substrs=["ptr =", "size=7"]) 152 self.expect("expr ptr", substrs=["size=7"]) 153 154 @add_test_categories(["libstdcxx"]) 155 def test_ref_and_ptr_libstdcpp(self): 156 self.do_test_ref_and_ptr(USE_LIBSTDCPP) 157 158 @add_test_categories(["libc++"]) 159 def test_ref_and_ptr_libcpp(self): 160 self.do_test_ref_and_ptr(USE_LIBCPP) 161