1""" 2Test lldb data formatter subsystem. 3""" 4 5import lldb 6from lldbsuite.test.decorators import * 7from lldbsuite.test.lldbtest import * 8from lldbsuite.test import lldbutil 9 10 11class PythonSynthDataFormatterTestCase(TestBase): 12 def test_with_run_command(self): 13 """Test data formatter commands.""" 14 self.build() 15 self.data_formatter_commands() 16 17 def test_rdar10960550_with_run_command(self): 18 """Test data formatter commands.""" 19 self.build() 20 self.rdar10960550_formatter_commands() 21 22 def setUp(self): 23 # Call super's setUp(). 24 TestBase.setUp(self) 25 # Find the line number to break at. 26 self.line = line_number("main.cpp", "// Set break point at this line.") 27 self.line2 = line_number("main.cpp", "// Set cast break point at this line.") 28 self.line3 = line_number( 29 "main.cpp", "// Set second cast break point at this line." 30 ) 31 32 def data_formatter_commands(self): 33 """Test using Python synthetic children provider.""" 34 35 _, process, thread, _ = lldbutil.run_to_line_breakpoint( 36 self, lldb.SBFileSpec("main.cpp"), self.line 37 ) 38 39 # This is the function to remove the custom formats in order to have a 40 # clean slate for the next test case. 41 def cleanup(): 42 self.runCmd("type format clear", check=False) 43 self.runCmd("type summary clear", check=False) 44 self.runCmd("type filter clear", check=False) 45 self.runCmd("type synth clear", check=False) 46 47 # Execute the cleanup function during test case tear down. 48 self.addTearDownHook(cleanup) 49 50 # print the f00_1 variable without a synth 51 self.expect("frame variable f00_1", substrs=["a = 1", "b = 2", "r = 34"]) 52 53 # now set up the synth 54 self.runCmd("script from fooSynthProvider import *") 55 self.runCmd("type synth add -l fooSynthProvider foo") 56 self.runCmd("type synth add -l wrapfooSynthProvider wrapfoo") 57 self.expect("type synthetic list foo", substrs=["fooSynthProvider"]) 58 59 # note that the value of fake_a depends on target byte order 60 if process.GetByteOrder() == lldb.eByteOrderLittle: 61 fake_a_val = 0x02000000 62 else: 63 fake_a_val = 0x00000100 64 65 # check that we get the two real vars and the fake_a variables 66 self.expect( 67 "frame variable f00_1", 68 substrs=[ 69 "a = 1", 70 "fake_a = %d" % fake_a_val, 71 "r = 34", 72 ], 73 ) 74 75 # check that we do not get the extra vars 76 self.expect("frame variable f00_1", matching=False, substrs=["b = 2"]) 77 78 # check access to members by name 79 self.expect("frame variable f00_1.fake_a", substrs=["%d" % fake_a_val]) 80 81 # check access to members by index 82 self.expect("frame variable f00_1[1]", substrs=["%d" % fake_a_val]) 83 84 # put synthetic children in summary in several combinations 85 self.runCmd('type summary add --summary-string "fake_a=${svar.fake_a}" foo') 86 self.expect("frame variable f00_1", substrs=["fake_a=%d" % fake_a_val]) 87 self.runCmd('type summary add --summary-string "fake_a=${svar[1]}" foo') 88 self.expect("frame variable f00_1", substrs=["fake_a=%d" % fake_a_val]) 89 90 # clear the summary 91 self.runCmd("type summary delete foo") 92 93 # check that the caching does not span beyond the stopoint 94 self.runCmd("n") 95 96 if process.GetByteOrder() == lldb.eByteOrderLittle: 97 fake_a_val = 0x02000000 98 else: 99 fake_a_val = 0x00000200 100 101 self.expect( 102 "frame variable f00_1", 103 substrs=[ 104 "a = 2", 105 "fake_a = %d" % fake_a_val, 106 "r = 34", 107 ], 108 ) 109 110 # check that altering the object also alters fake_a 111 self.runCmd("expr f00_1.a = 280") 112 113 if process.GetByteOrder() == lldb.eByteOrderLittle: 114 fake_a_val = 0x02000001 115 else: 116 fake_a_val = 0x00011800 117 118 self.expect( 119 "frame variable f00_1", 120 substrs=[ 121 "a = 280", 122 "fake_a = %d" % fake_a_val, 123 "r = 34", 124 ], 125 ) 126 127 # check that expanding a pointer does the right thing 128 if process.GetByteOrder() == lldb.eByteOrderLittle: 129 fake_a_val = 0x0D000000 130 else: 131 fake_a_val = 0x00000C00 132 133 self.expect( 134 "frame variable --ptr-depth 1 f00_ptr", 135 substrs=[ 136 "a = 12", 137 "fake_a = %d" % fake_a_val, 138 "r = 45", 139 ], 140 ) 141 self.expect( 142 "frame variable --ptr-depth 1 wrapper", 143 substrs=[ 144 "a = 12", 145 "fake_a = %d" % fake_a_val, 146 "r = 45", 147 ], 148 ) 149 150 # now add a filter.. it should fail 151 self.expect( 152 "type filter add foo --child b --child j", 153 error=True, 154 substrs=["cannot add"], 155 ) 156 157 # we get the synth again.. 158 self.expect("frame variable f00_1", matching=False, substrs=["b = 1", "j = 17"]) 159 self.expect( 160 "frame variable --ptr-depth 1 f00_ptr", 161 substrs=[ 162 "a = 12", 163 "fake_a = %d" % fake_a_val, 164 "r = 45", 165 ], 166 ) 167 self.expect( 168 "frame variable --ptr-depth 1 wrapper", 169 substrs=[ 170 "a = 12", 171 "fake_a = %d" % fake_a_val, 172 "r = 45", 173 ], 174 ) 175 176 # Test that the custom dereference operator for `wrapfoo` works through 177 # the Python API. The synthetic children provider gets queried at 178 # slightly different times in this case. 179 wrapper_var = thread.GetSelectedFrame().FindVariable("wrapper") 180 foo_var = wrapper_var.Dereference() 181 self.assertEqual(foo_var.GetNumChildren(), 3) 182 self.assertEqual(foo_var.GetChildAtIndex(0).GetName(), "a") 183 self.assertEqual(foo_var.GetChildAtIndex(1).GetName(), "fake_a") 184 self.assertEqual(foo_var.GetChildAtIndex(2).GetName(), "r") 185 186 # now delete the synth and add the filter 187 self.runCmd("type synth delete foo") 188 self.runCmd("type synth delete wrapfoo") 189 self.runCmd("type filter add foo --child b --child j") 190 191 self.expect("frame variable f00_1", substrs=["b = 2", "j = 18"]) 192 self.expect( 193 "frame variable --ptr-depth 1 f00_ptr", 194 matching=False, 195 substrs=["r = 45", "fake_a = %d" % fake_a_val, "a = 12"], 196 ) 197 self.expect( 198 "frame variable --ptr-depth 1 wrapper", 199 matching=False, 200 substrs=["r = 45", "fake_a = %d" % fake_a_val, "a = 12"], 201 ) 202 203 # now add the synth and it should fail 204 self.expect( 205 "type synth add -l fooSynthProvider foo", error=True, substrs=["cannot add"] 206 ) 207 208 # check the listing 209 self.expect( 210 "type synth list", 211 matching=False, 212 substrs=["foo", "Python class fooSynthProvider"], 213 ) 214 self.expect("type filter list", substrs=["foo", ".b", ".j"]) 215 216 # delete the filter, add the synth 217 self.runCmd("type filter delete foo") 218 self.runCmd("type synth add -l fooSynthProvider foo") 219 220 self.expect("frame variable f00_1", matching=False, substrs=["b = 2", "j = 18"]) 221 self.expect( 222 "frame variable --ptr-depth 1 f00_ptr", 223 substrs=[ 224 "a = 12", 225 "fake_a = %d" % fake_a_val, 226 "r = 45", 227 ], 228 ) 229 self.expect( 230 "frame variable --ptr-depth 1 wrapper", 231 substrs=[ 232 "a = 12", 233 "fake_a = %d" % fake_a_val, 234 "r = 45", 235 ], 236 ) 237 238 # check the listing 239 self.expect("type synth list", substrs=["foo", "Python class fooSynthProvider"]) 240 self.expect("type filter list", matching=False, substrs=["foo", ".b", ".j"]) 241 242 # delete the synth and check that we get good output 243 self.runCmd("type synth delete foo") 244 245 self.expect("frame variable f00_1", substrs=["a = 280", "b = 2", "j = 18"]) 246 247 self.expect("frame variable f00_1", matching=False, substrs=["fake_a = "]) 248 249 # check that we don't feed a regex into another regex when checking for 250 # existing conflicting synth/filters. The two following expressions 251 # accept different types: one will accept types that look like an array 252 # of MyType, the other will accept types that contain "MyType1" or 253 # "MyType2". But the second regex looks like an array of MyType, so 254 # lldb used to incorrectly reject it. 255 self.runCmd(r'type synth add -l fooSynthProvider -x "^MyType\[[0-9]+]$"') 256 self.runCmd(r'type filter add --child a -x "MyType[12]"') 257 258 # Same, but adding the filter first to verify the check when doing 259 # `type synth add`. We need to delete the synth from the previous test 260 # first. 261 self.runCmd(r'type synth delete "^MyType\[[0-9]+]$"') 262 self.runCmd(r'type filter add --child a -x "^MyType\[[0-9]+]$"') 263 self.runCmd(r'type synth add -l fooSynthProvider -x "MyType[12]"') 264 265 def rdar10960550_formatter_commands(self): 266 """Test that synthetic children persist stoppoints.""" 267 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET) 268 269 # The second breakpoint is on a multi-line expression, so the comment 270 # can't be on the right line... 271 lldbutil.run_break_set_by_file_and_line( 272 self, "main.cpp", self.line2, num_expected_locations=1, loc_exact=False 273 ) 274 lldbutil.run_break_set_by_file_and_line( 275 self, "main.cpp", self.line3, num_expected_locations=1, loc_exact=True 276 ) 277 278 self.runCmd("run", RUN_SUCCEEDED) 279 280 # The stop reason of the thread should be breakpoint. 281 self.expect( 282 "thread list", 283 STOPPED_DUE_TO_BREAKPOINT, 284 substrs=["stopped", "stop reason = breakpoint"], 285 ) 286 287 # This is the function to remove the custom formats in order to have a 288 # clean slate for the next test case. 289 def cleanup(): 290 self.runCmd("type format clear", check=False) 291 self.runCmd("type summary clear", check=False) 292 self.runCmd("type filter clear", check=False) 293 self.runCmd("type synth clear", check=False) 294 295 # Execute the cleanup function during test case tear down. 296 self.addTearDownHook(cleanup) 297 298 self.runCmd("command script import ./ftsp.py --allow-reload") 299 self.runCmd("type synth add -l ftsp.ftsp wrapint") 300 301 # we need to check that the VO is properly updated so that the same synthetic children are reused 302 # but their values change correctly across stop-points - in order to do this, self.runCmd("next") 303 # does not work because it forces a wipe of the stack frame - this is why we are using this more contrived 304 # mechanism to achieve our goal of preserving test_cast as a VO 305 test_cast = ( 306 self.dbg.GetSelectedTarget() 307 .GetProcess() 308 .GetSelectedThread() 309 .GetSelectedFrame() 310 .FindVariable("test_cast") 311 ) 312 313 str_cast = str(test_cast) 314 315 if self.TraceOn(): 316 print(str_cast) 317 318 self.assertNotEqual(str_cast.find("A"), -1, "could not find A in output") 319 self.assertNotEqual(str_cast.find("B"), -1, "could not find B in output") 320 self.assertNotEqual(str_cast.find("C"), -1, "could not find C in output") 321 self.assertNotEqual(str_cast.find("D"), -1, "could not find D in output") 322 self.assertNotEqual( 323 str_cast.find("4 = '\\0'"), -1, "could not find item 4 == 0" 324 ) 325 326 self.dbg.GetSelectedTarget().GetProcess().GetSelectedThread().StepOver() 327 328 str_cast = str(test_cast) 329 330 if self.TraceOn(): 331 print(str_cast) 332 333 # we detect that all the values of the child objects have changed - but the counter-generated item 334 # is still fixed at 0 because it is cached - this would fail if update(self): in ftsp returned False 335 # or if synthetic children were not being preserved 336 self.assertNotEqual(str_cast.find("Q"), -1, "could not find Q in output") 337 self.assertNotEqual(str_cast.find("X"), -1, "could not find X in output") 338 self.assertNotEqual(str_cast.find("T"), -1, "could not find T in output") 339 self.assertNotEqual(str_cast.find("F"), -1, "could not find F in output") 340 self.assertNotEqual( 341 str_cast.find("4 = '\\0'"), -1, "could not find item 4 == 0" 342 ) 343