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 def test_default_no_run(self): 122 """Test "statistics dump" without running the target. 123 124 When we don't run the target, we expect to not see any 'firstStopTime' 125 or 'launchOrAttachTime' top level keys that measure the launch or 126 attach of the target. 127 128 Output expected to be something like: 129 130 (lldb) statistics dump 131 { 132 "modules" : [...], 133 "targets" : [ 134 { 135 "targetCreateTime": 0.26566899599999999, 136 "expressionEvaluation": { 137 "failures": 0, 138 "successes": 0 139 }, 140 "frameVariable": { 141 "failures": 0, 142 "successes": 0 143 }, 144 "moduleIdentifiers": [...], 145 } 146 ], 147 "totalSymbolTableParseTime": 0.123, 148 "totalSymbolTableIndexTime": 0.234, 149 } 150 """ 151 target = self.createTestTarget() 152 debug_stats = self.get_stats() 153 debug_stat_keys = [ 154 'modules', 155 'targets', 156 'totalSymbolTableParseTime', 157 'totalSymbolTableIndexTime', 158 ] 159 self.verify_keys(debug_stats, '"debug_stats"', debug_stat_keys, None) 160 stats = debug_stats['targets'][0] 161 keys_exist = [ 162 'expressionEvaluation', 163 'frameVariable', 164 'moduleIdentifiers', 165 'targetCreateTime', 166 ] 167 keys_missing = [ 168 'firstStopTime', 169 'launchOrAttachTime' 170 ] 171 self.verify_keys(stats, '"stats"', keys_exist, keys_missing) 172 self.assertGreater(stats['targetCreateTime'], 0.0) 173 174 def test_default_with_run(self): 175 """Test "statistics dump" when running the target to a breakpoint. 176 177 When we run the target, we expect to see 'launchOrAttachTime' and 178 'firstStopTime' top level keys. 179 180 Output expected to be something like: 181 182 (lldb) statistics dump 183 { 184 "modules" : [...], 185 "targets" : [ 186 { 187 "firstStopTime": 0.34164492800000001, 188 "launchOrAttachTime": 0.31969605400000001, 189 "moduleIdentifiers": [...], 190 "targetCreateTime": 0.0040863039999999998 191 "expressionEvaluation": { 192 "failures": 0, 193 "successes": 0 194 }, 195 "frameVariable": { 196 "failures": 0, 197 "successes": 0 198 }, 199 } 200 ], 201 "totalSymbolTableParseTime": 0.123, 202 "totalSymbolTableIndexTime": 0.234, 203 } 204 205 """ 206 target = self.createTestTarget() 207 lldbutil.run_to_source_breakpoint(self, "// break here", 208 lldb.SBFileSpec("main.c")) 209 debug_stats = self.get_stats() 210 debug_stat_keys = [ 211 'modules', 212 'targets', 213 'totalSymbolTableParseTime', 214 'totalSymbolTableIndexTime', 215 ] 216 self.verify_keys(debug_stats, '"debug_stats"', debug_stat_keys, None) 217 stats = debug_stats['targets'][0] 218 keys_exist = [ 219 'expressionEvaluation', 220 'firstStopTime', 221 'frameVariable', 222 'launchOrAttachTime', 223 'moduleIdentifiers', 224 'targetCreateTime', 225 ] 226 self.verify_keys(stats, '"stats"', keys_exist, None) 227 self.assertGreater(stats['firstStopTime'], 0.0) 228 self.assertGreater(stats['launchOrAttachTime'], 0.0) 229 self.assertGreater(stats['targetCreateTime'], 0.0) 230 231 def find_module_in_metrics(self, path, stats): 232 modules = stats['modules'] 233 for module in modules: 234 if module['path'] == path: 235 return module 236 return None 237 238 def test_modules(self): 239 """ 240 Test "statistics dump" and the module information. 241 """ 242 exe = self.getBuildArtifact("a.out") 243 target = self.createTestTarget(file_path=exe) 244 debug_stats = self.get_stats() 245 debug_stat_keys = [ 246 'modules', 247 'targets', 248 'totalSymbolTableParseTime', 249 'totalSymbolTableIndexTime', 250 ] 251 self.verify_keys(debug_stats, '"debug_stats"', debug_stat_keys, None) 252 stats = debug_stats['targets'][0] 253 keys_exist = [ 254 'moduleIdentifiers', 255 ] 256 self.verify_keys(stats, '"stats"', keys_exist, None) 257 exe_module = self.find_module_in_metrics(exe, debug_stats) 258 module_keys = [ 259 'identifier', 260 'path', 261 'symbolTableIndexTime', 262 'symbolTableParseTime', 263 'triple', 264 'uuid', 265 ] 266 self.assertNotEqual(exe_module, None) 267 self.verify_keys(exe_module, 'module dict for "%s"' % (exe), module_keys) 268