1""" 2Test display and Python APIs on file and class static variables. 3""" 4 5import lldb 6from lldbsuite.test.decorators import * 7from lldbsuite.test.lldbtest import * 8from lldbsuite.test import lldbutil 9 10 11class StaticVariableTestCase(TestBase): 12 def setUp(self): 13 # Call super's setUp(). 14 TestBase.setUp(self) 15 # Find the line number to break at. 16 self.line = line_number("main.cpp", "// Set break point at this line.") 17 18 def test_with_run_command(self): 19 """Test that file and class static variables display correctly.""" 20 self.build() 21 self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET) 22 23 lldbutil.run_break_set_by_file_and_line( 24 self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True 25 ) 26 27 self.runCmd("run", RUN_SUCCEEDED) 28 29 # The stop reason of the thread should be breakpoint. 30 self.expect( 31 "thread list", 32 STOPPED_DUE_TO_BREAKPOINT, 33 substrs=["stopped", "stop reason = breakpoint"], 34 ) 35 36 # Global variables are no longer displayed with the "frame variable" 37 # command. 38 self.expect( 39 "target variable A::g_points", 40 VARIABLES_DISPLAYED_CORRECTLY, 41 patterns=["\(PointType\[[1-9]*\]\) A::g_points = {"], 42 ) 43 self.expect( 44 "target variable g_points", 45 VARIABLES_DISPLAYED_CORRECTLY, 46 substrs=["(PointType[2]) g_points"], 47 ) 48 49 # On Mac OS X, gcc 4.2 emits the wrong debug info for A::g_points. 50 # A::g_points is an array of two elements. 51 if self.platformIsDarwin() or self.getPlatform() == "linux": 52 self.expect( 53 "target variable A::g_points[1].x", 54 VARIABLES_DISPLAYED_CORRECTLY, 55 startstr="(int) A::g_points[1].x = 11", 56 ) 57 58 @expectedFailureAll( 59 compiler=["gcc"], bugnumber="Compiler emits incomplete debug info" 60 ) 61 @expectedFailureAll( 62 compiler=["clang"], compiler_version=["<", "3.9"], bugnumber="llvm.org/pr20550" 63 ) 64 def test_with_run_command_complete(self): 65 """ 66 Test that file and class static variables display correctly with 67 complete debug information. 68 """ 69 self.build() 70 target = self.dbg.CreateTarget(self.getBuildArtifact("a.out")) 71 self.assertTrue(target, VALID_TARGET) 72 73 # Global variables are no longer displayed with the "frame variable" 74 # command. 75 self.expect( 76 "target variable A::g_points", 77 VARIABLES_DISPLAYED_CORRECTLY, 78 patterns=[ 79 "\(PointType\[[1-9]*\]\) A::g_points = {", 80 "(x = 1, y = 2)", 81 "(x = 11, y = 22)", 82 ], 83 ) 84 85 # Ensure that we take the context into account and only print 86 # A::g_points. 87 self.expect( 88 "target variable A::g_points", 89 VARIABLES_DISPLAYED_CORRECTLY, 90 matching=False, 91 patterns=["(x = 3, y = 4)", "(x = 33, y = 44)"], 92 ) 93 94 # Finally, ensure that we print both points when not specifying a 95 # context. 96 self.expect( 97 "target variable g_points", 98 VARIABLES_DISPLAYED_CORRECTLY, 99 substrs=[ 100 "(PointType[2]) g_points", 101 "(x = 1, y = 2)", 102 "(x = 11, y = 22)", 103 "(x = 3, y = 4)", 104 "(x = 33, y = 44)", 105 ], 106 ) 107 108 def build_value_check(self, var_name, values): 109 children_1 = [ 110 ValueCheck(name="x", value=values[0], type="int"), 111 ValueCheck(name="y", value=values[1], type="int"), 112 ] 113 children_2 = [ 114 ValueCheck(name="x", value=values[2], type="int"), 115 ValueCheck(name="y", value=values[3], type="int"), 116 ] 117 elem_0 = ValueCheck( 118 name="[0]", value=None, type="PointType", children=children_1 119 ) 120 elem_1 = ValueCheck( 121 name="[1]", value=None, type="PointType", children=children_2 122 ) 123 value_check = ValueCheck( 124 name=var_name, value=None, type="PointType[2]", children=[elem_0, elem_1] 125 ) 126 127 return value_check 128 129 @expectedFailureAll( 130 compiler=["gcc"], bugnumber="Compiler emits incomplete debug info" 131 ) 132 @expectedFailureAll( 133 compiler=["clang"], compiler_version=["<", "3.9"], bugnumber="llvm.org/pr20550" 134 ) 135 @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764") 136 @add_test_categories(["pyapi"]) 137 def test_with_python_FindValue(self): 138 """Test Python APIs on file and class static variables.""" 139 self.build() 140 exe = self.getBuildArtifact("a.out") 141 142 target = self.dbg.CreateTarget(exe) 143 self.assertTrue(target, VALID_TARGET) 144 145 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line) 146 self.assertTrue(breakpoint, VALID_BREAKPOINT) 147 148 # Now launch the process, and do not stop at entry point. 149 process = target.LaunchSimple(None, None, self.get_process_working_directory()) 150 self.assertTrue(process, PROCESS_IS_VALID) 151 152 # The stop reason of the thread should be breakpoint. 153 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) 154 self.assertIsNotNone(thread) 155 156 # Get the SBValue of 'A::g_points' and 'g_points'. 157 frame = thread.GetFrameAtIndex(0) 158 159 # arguments => False 160 # locals => False 161 # statics => True 162 # in_scope_only => False 163 valList = frame.GetVariables(False, False, True, False) 164 165 # Build ValueCheckers for the values we're going to find: 166 value_check_A = self.build_value_check("A::g_points", ["1", "2", "11", "22"]) 167 value_check_none = self.build_value_check("g_points", ["3", "4", "33", "44"]) 168 value_check_AA = self.build_value_check("AA::g_points", ["5", "6", "55", "66"]) 169 170 for val in valList: 171 self.DebugSBValue(val) 172 name = val.GetName() 173 self.assertIn(name, ["g_points", "A::g_points", "AA::g_points"]) 174 175 if name == "A::g_points": 176 self.assertEqual(val.GetValueType(), lldb.eValueTypeVariableGlobal) 177 value_check_A.check_value(self, val, "Got A::g_points right") 178 if name == "g_points": 179 self.assertEqual(val.GetValueType(), lldb.eValueTypeVariableStatic) 180 value_check_none.check_value(self, val, "Got g_points right") 181 if name == "AA::g_points": 182 self.assertEqual(val.GetValueType(), lldb.eValueTypeVariableGlobal) 183 value_check_AA.check_value(self, val, "Got AA::g_points right") 184 185 # SBFrame.FindValue() should also work. 186 val = frame.FindValue("A::g_points", lldb.eValueTypeVariableGlobal) 187 self.DebugSBValue(val) 188 value_check_A.check_value(self, val, "FindValue also works") 189 190 # Also exercise the "parameter" and "local" scopes while we are at it. 191 val = frame.FindValue("argc", lldb.eValueTypeVariableArgument) 192 self.DebugSBValue(val) 193 self.assertEqual(val.GetName(), "argc") 194 195 val = frame.FindValue("argv", lldb.eValueTypeVariableArgument) 196 self.DebugSBValue(val) 197 self.assertEqual(val.GetName(), "argv") 198 199 val = frame.FindValue("hello_world", lldb.eValueTypeVariableLocal) 200 self.DebugSBValue(val) 201 self.assertEqual(val.GetName(), "hello_world") 202 203 # This test tests behavior that's been broken for a very long time.. 204 # The fix for it is in the accelerator table part of the DWARF reader, 205 # and I fixed the version that the names accelerator uses, but I don't 206 # know how to fix it on systems that don't use that. There isn't a 207 # decorator for that - not sure how to construct that so I'm limiting the 208 # test do Darwin for now. 209 @expectedFailureAll( 210 compiler=["gcc"], bugnumber="Compiler emits incomplete debug info" 211 ) 212 @skipUnlessDarwin 213 @add_test_categories(["pyapi"]) 214 def test_with_python_FindGlobalVariables(self): 215 """Test Python APIs on file and class static variables.""" 216 self.build() 217 exe = self.getBuildArtifact("a.out") 218 219 target = self.dbg.CreateTarget(exe) 220 self.assertTrue(target, VALID_TARGET) 221 222 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line) 223 self.assertTrue(breakpoint, VALID_BREAKPOINT) 224 225 # Now launch the process, and do not stop at entry point. 226 process = target.LaunchSimple(None, None, self.get_process_working_directory()) 227 self.assertTrue(process, PROCESS_IS_VALID) 228 229 # The stop reason of the thread should be breakpoint. 230 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint) 231 self.assertIsNotNone(thread) 232 233 # Get the SBValue of 'A::g_points' and 'g_points'. 234 frame = thread.GetFrameAtIndex(0) 235 236 # Build ValueCheckers for the values we're going to find: 237 value_check_A = self.build_value_check("A::g_points", ["1", "2", "11", "22"]) 238 value_check_none = self.build_value_check("g_points", ["3", "4", "33", "44"]) 239 value_check_AA = self.build_value_check("AA::g_points", ["5", "6", "55", "66"]) 240 241 # We should also be able to get class statics from FindGlobalVariables. 242 # eMatchTypeStartsWith should only find A:: not AA:: 243 val_list = target.FindGlobalVariables("A::", 10, lldb.eMatchTypeStartsWith) 244 self.assertEqual(val_list.GetSize(), 1, "Found only one match") 245 val = val_list[0] 246 value_check_A.check_value(self, val, "FindGlobalVariables starts with") 247 248 # Regex should find both 249 val_list = target.FindGlobalVariables("A::", 10, lldb.eMatchTypeRegex) 250 self.assertEqual(val_list.GetSize(), 2, "Found A & AA") 251 found_a = False 252 found_aa = False 253 for val in val_list: 254 name = val.GetName() 255 if name == "A::g_points": 256 value_check_A.check_value(self, val, "AA found by regex") 257 found_a = True 258 elif name == "AA::g_points": 259 value_check_AA.check_value(self, val, "A found by regex") 260 found_aa = True 261 262 self.assertTrue(found_a, "Regex search found A::g_points") 263 self.assertTrue(found_aa, "Regex search found AA::g_points") 264 265 # Regex lowercase should find both as well. 266 val_list = target.FindGlobalVariables( 267 "a::g_points", 10, lldb.eMatchTypeRegexInsensitive 268 ) 269 self.assertEqual(val_list.GetSize(), 2, "Found A & AA") 270 271 # Normal search for full name should find one, but it looks like we don't match 272 # on identifier boundaries here yet: 273 val_list = target.FindGlobalVariables("A::g_points", 10, lldb.eMatchTypeNormal) 274 self.assertEqual( 275 val_list.GetSize(), 2, "We aren't matching on name boundaries yet" 276 ) 277 278 # Normal search for g_points should find 3 - FindGlobalVariables doesn't distinguish 279 # between file statics and globals: 280 val_list = target.FindGlobalVariables("g_points", 10, lldb.eMatchTypeNormal) 281 self.assertEqual(val_list.GetSize(), 3, "Found all three g_points") 282