1import lldb 2import json 3from lldbsuite.test.decorators import * 4from lldbsuite.test.lldbtest import * 5from lldbsuite.test import lldbutil 6 7class TestCase(TestBase): 8 9 mydir = TestBase.compute_mydir(__file__) 10 11 def setUp(self): 12 TestBase.setUp(self) 13 self.build() 14 15 NO_DEBUG_INFO_TESTCASE = True 16 17 def test_enable_disable(self): 18 """ 19 Test "statistics disable" and "statistics enable". These don't do 20 anything anymore for cheap to gather statistics. In the future if 21 statistics are expensive to gather, we can enable the feature inside 22 of LLDB and test that enabling and disabling stops expesive information 23 from being gathered. 24 """ 25 target = self.createTestTarget() 26 27 self.expect("statistics disable", substrs=['need to enable statistics before disabling'], error=True) 28 self.expect("statistics enable") 29 self.expect("statistics enable", substrs=['already enabled'], error=True) 30 self.expect("statistics disable") 31 self.expect("statistics disable", substrs=['need to enable statistics before disabling'], error=True) 32 33 def verify_key_in_dict(self, key, d, description): 34 self.assertEqual(key in d, True, 35 'make sure key "%s" is in dictionary %s' % (key, description)) 36 37 def verify_key_not_in_dict(self, key, d, description): 38 self.assertEqual(key in d, False, 39 'make sure key "%s" is in dictionary %s' % (key, description)) 40 41 def verify_keys(self, dict, description, keys_exist, keys_missing=None): 42 """ 43 Verify that all keys in "keys_exist" list are top level items in 44 "dict", and that all keys in "keys_missing" do not exist as top 45 level items in "dict". 46 """ 47 if keys_exist: 48 for key in keys_exist: 49 self.verify_key_in_dict(key, dict, description) 50 if keys_missing: 51 for key in keys_missing: 52 self.verify_key_not_in_dict(key, dict, description) 53 54 def verify_success_fail_count(self, stats, key, num_successes, num_fails): 55 self.verify_key_in_dict(key, stats, 'stats["%s"]' % (key)) 56 success_fail_dict = stats[key] 57 self.assertEqual(success_fail_dict['successes'], num_successes, 58 'make sure success count') 59 self.assertEqual(success_fail_dict['failures'], num_fails, 60 'make sure success count') 61 62 def get_stats(self, options=None, log_path=None): 63 """ 64 Get the output of the "statistics dump" with optional extra options 65 and return the JSON as a python dictionary. 66 """ 67 # If log_path is set, open the path and emit the output of the command 68 # for debugging purposes. 69 if log_path is not None: 70 f = open(log_path, 'w') 71 else: 72 f = None 73 return_obj = lldb.SBCommandReturnObject() 74 command = "statistics dump " 75 if options is not None: 76 command += options 77 if f: 78 f.write('(lldb) %s\n' % (command)) 79 self.ci.HandleCommand(command, return_obj, False) 80 metrics_json = return_obj.GetOutput() 81 if f: 82 f.write(metrics_json) 83 return json.loads(metrics_json) 84 85 86 def get_target_stats(self, debug_stats): 87 if "targets" in debug_stats: 88 return debug_stats["targets"][0] 89 return None 90 91 def test_expressions_frame_var_counts(self): 92 lldbutil.run_to_source_breakpoint(self, "// break here", 93 lldb.SBFileSpec("main.c")) 94 95 self.expect("expr patatino", substrs=['27']) 96 stats = self.get_target_stats(self.get_stats()) 97 self.verify_success_fail_count(stats, 'expressionEvaluation', 1, 0) 98 self.expect("expr doesnt_exist", error=True, 99 substrs=["undeclared identifier 'doesnt_exist'"]) 100 # Doesn't successfully execute. 101 self.expect("expr int *i = nullptr; *i", error=True) 102 # Interpret an integer as an array with 3 elements is a failure for 103 # the "expr" command, but the expression evaluation will succeed and 104 # be counted as a success even though the "expr" options will for the 105 # command to fail. It is more important to track expression evaluation 106 # from all sources instead of just through the command, so this was 107 # changed. If we want to track command success and fails, we can do 108 # so using another metric. 109 self.expect("expr -Z 3 -- 1", error=True, 110 substrs=["expression cannot be used with --element-count"]) 111 # We should have gotten 3 new failures and the previous success. 112 stats = self.get_target_stats(self.get_stats()) 113 self.verify_success_fail_count(stats, 'expressionEvaluation', 2, 2) 114 115 self.expect("statistics enable") 116 # 'frame var' with enabled statistics will change stats. 117 self.expect("frame var", substrs=['27']) 118 stats = self.get_target_stats(self.get_stats()) 119 self.verify_success_fail_count(stats, 'frameVariable', 1, 0) 120 121 # Test that "stopCount" is available when the process has run 122 self.assertEqual('stopCount' in stats, True, 123 'ensure "stopCount" is in target JSON') 124 self.assertGreater(stats['stopCount'], 0, 125 'make sure "stopCount" is greater than zero') 126 127 def test_default_no_run(self): 128 """Test "statistics dump" without running the target. 129 130 When we don't run the target, we expect to not see any 'firstStopTime' 131 or 'launchOrAttachTime' top level keys that measure the launch or 132 attach of the target. 133 134 Output expected to be something like: 135 136 (lldb) statistics dump 137 { 138 "modules" : [...], 139 "targets" : [ 140 { 141 "targetCreateTime": 0.26566899599999999, 142 "expressionEvaluation": { 143 "failures": 0, 144 "successes": 0 145 }, 146 "frameVariable": { 147 "failures": 0, 148 "successes": 0 149 }, 150 "moduleIdentifiers": [...], 151 } 152 ], 153 "totalDebugInfoByteSize": 182522234, 154 "totalDebugInfoIndexTime": 2.33343, 155 "totalDebugInfoParseTime": 8.2121400240000071, 156 "totalSymbolTableParseTime": 0.123, 157 "totalSymbolTableIndexTime": 0.234, 158 } 159 """ 160 target = self.createTestTarget() 161 debug_stats = self.get_stats() 162 debug_stat_keys = [ 163 'modules', 164 'targets', 165 'totalSymbolTableParseTime', 166 'totalSymbolTableIndexTime', 167 'totalDebugInfoByteSize', 168 'totalDebugInfoIndexTime', 169 'totalDebugInfoParseTime', 170 ] 171 self.verify_keys(debug_stats, '"debug_stats"', debug_stat_keys, None) 172 stats = debug_stats['targets'][0] 173 keys_exist = [ 174 'expressionEvaluation', 175 'frameVariable', 176 'moduleIdentifiers', 177 'targetCreateTime', 178 ] 179 keys_missing = [ 180 'firstStopTime', 181 'launchOrAttachTime' 182 ] 183 self.verify_keys(stats, '"stats"', keys_exist, keys_missing) 184 self.assertGreater(stats['targetCreateTime'], 0.0) 185 186 def test_default_with_run(self): 187 """Test "statistics dump" when running the target to a breakpoint. 188 189 When we run the target, we expect to see 'launchOrAttachTime' and 190 'firstStopTime' top level keys. 191 192 Output expected to be something like: 193 194 (lldb) statistics dump 195 { 196 "modules" : [...], 197 "targets" : [ 198 { 199 "firstStopTime": 0.34164492800000001, 200 "launchOrAttachTime": 0.31969605400000001, 201 "moduleIdentifiers": [...], 202 "targetCreateTime": 0.0040863039999999998 203 "expressionEvaluation": { 204 "failures": 0, 205 "successes": 0 206 }, 207 "frameVariable": { 208 "failures": 0, 209 "successes": 0 210 }, 211 } 212 ], 213 "totalDebugInfoByteSize": 182522234, 214 "totalDebugInfoIndexTime": 2.33343, 215 "totalDebugInfoParseTime": 8.2121400240000071, 216 "totalSymbolTableParseTime": 0.123, 217 "totalSymbolTableIndexTime": 0.234, 218 } 219 220 """ 221 target = self.createTestTarget() 222 lldbutil.run_to_source_breakpoint(self, "// break here", 223 lldb.SBFileSpec("main.c")) 224 debug_stats = self.get_stats() 225 debug_stat_keys = [ 226 'modules', 227 'targets', 228 'totalSymbolTableParseTime', 229 'totalSymbolTableIndexTime', 230 'totalDebugInfoByteSize', 231 'totalDebugInfoIndexTime', 232 'totalDebugInfoParseTime', 233 ] 234 self.verify_keys(debug_stats, '"debug_stats"', debug_stat_keys, None) 235 stats = debug_stats['targets'][0] 236 keys_exist = [ 237 'expressionEvaluation', 238 'firstStopTime', 239 'frameVariable', 240 'launchOrAttachTime', 241 'moduleIdentifiers', 242 'targetCreateTime', 243 ] 244 self.verify_keys(stats, '"stats"', keys_exist, None) 245 self.assertGreater(stats['firstStopTime'], 0.0) 246 self.assertGreater(stats['launchOrAttachTime'], 0.0) 247 self.assertGreater(stats['targetCreateTime'], 0.0) 248 249 def find_module_in_metrics(self, path, stats): 250 modules = stats['modules'] 251 for module in modules: 252 if module['path'] == path: 253 return module 254 return None 255 256 def test_modules(self): 257 """ 258 Test "statistics dump" and the module information. 259 """ 260 exe = self.getBuildArtifact("a.out") 261 target = self.createTestTarget(file_path=exe) 262 debug_stats = self.get_stats() 263 debug_stat_keys = [ 264 'modules', 265 'targets', 266 'totalSymbolTableParseTime', 267 'totalSymbolTableIndexTime', 268 'totalDebugInfoParseTime', 269 'totalDebugInfoIndexTime', 270 'totalDebugInfoByteSize' 271 ] 272 self.verify_keys(debug_stats, '"debug_stats"', debug_stat_keys, None) 273 stats = debug_stats['targets'][0] 274 keys_exist = [ 275 'moduleIdentifiers', 276 ] 277 self.verify_keys(stats, '"stats"', keys_exist, None) 278 exe_module = self.find_module_in_metrics(exe, debug_stats) 279 module_keys = [ 280 'debugInfoByteSize', 281 'debugInfoIndexTime', 282 'debugInfoParseTime', 283 'identifier', 284 'path', 285 'symbolTableIndexTime', 286 'symbolTableParseTime', 287 'triple', 288 'uuid', 289 ] 290 self.assertNotEqual(exe_module, None) 291 self.verify_keys(exe_module, 'module dict for "%s"' % (exe), module_keys) 292 293 def test_breakpoints(self): 294 """Test "statistics dump" 295 296 Output expected to be something like: 297 298 { 299 "modules" : [...], 300 "targets" : [ 301 { 302 "firstStopTime": 0.34164492800000001, 303 "launchOrAttachTime": 0.31969605400000001, 304 "moduleIdentifiers": [...], 305 "targetCreateTime": 0.0040863039999999998 306 "expressionEvaluation": { 307 "failures": 0, 308 "successes": 0 309 }, 310 "frameVariable": { 311 "failures": 0, 312 "successes": 0 313 }, 314 "breakpoints": [ 315 { 316 "details": {...}, 317 "id": 1, 318 "resolveTime": 2.65438675 319 }, 320 { 321 "details": {...}, 322 "id": 2, 323 "resolveTime": 4.3632581669999997 324 } 325 ] 326 } 327 ], 328 "totalDebugInfoByteSize": 182522234, 329 "totalDebugInfoIndexTime": 2.33343, 330 "totalDebugInfoParseTime": 8.2121400240000071, 331 "totalSymbolTableParseTime": 0.123, 332 "totalSymbolTableIndexTime": 0.234, 333 "totalBreakpointResolveTime": 7.0176449170000001 334 } 335 336 """ 337 target = self.createTestTarget() 338 self.runCmd("b main.cpp:7") 339 self.runCmd("b a_function") 340 debug_stats = self.get_stats() 341 debug_stat_keys = [ 342 'modules', 343 'targets', 344 'totalSymbolTableParseTime', 345 'totalSymbolTableIndexTime', 346 'totalDebugInfoParseTime', 347 'totalDebugInfoIndexTime', 348 'totalDebugInfoByteSize', 349 ] 350 self.verify_keys(debug_stats, '"debug_stats"', debug_stat_keys, None) 351 target_stats = debug_stats['targets'][0] 352 keys_exist = [ 353 'breakpoints', 354 'expressionEvaluation', 355 'frameVariable', 356 'targetCreateTime', 357 'moduleIdentifiers', 358 'totalBreakpointResolveTime', 359 ] 360 self.verify_keys(target_stats, '"stats"', keys_exist, None) 361 self.assertGreater(target_stats['totalBreakpointResolveTime'], 0.0) 362 breakpoints = target_stats['breakpoints'] 363 bp_keys_exist = [ 364 'details', 365 'id', 366 'internal', 367 'numLocations', 368 'numResolvedLocations', 369 'resolveTime' 370 ] 371 for breakpoint in breakpoints: 372 self.verify_keys(breakpoint, 'target_stats["breakpoints"]', 373 bp_keys_exist, None) 374