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 def test_expressions_frame_var_counts(self): 86 lldbutil.run_to_source_breakpoint(self, "// break here", 87 lldb.SBFileSpec("main.c")) 88 89 self.expect("expr patatino", substrs=['27']) 90 stats = self.get_stats() 91 self.verify_success_fail_count(stats, 'expressionEvaluation', 1, 0) 92 self.expect("expr doesnt_exist", error=True, 93 substrs=["undeclared identifier 'doesnt_exist'"]) 94 # Doesn't successfully execute. 95 self.expect("expr int *i = nullptr; *i", error=True) 96 # Interpret an integer as an array with 3 elements is also a failure. 97 self.expect("expr -Z 3 -- 1", error=True, 98 substrs=["expression cannot be used with --element-count"]) 99 # We should have gotten 3 new failures and the previous success. 100 stats = self.get_stats() 101 self.verify_success_fail_count(stats, 'expressionEvaluation', 1, 3) 102 103 self.expect("statistics enable") 104 # 'frame var' with enabled statistics will change stats. 105 self.expect("frame var", substrs=['27']) 106 stats = self.get_stats() 107 self.verify_success_fail_count(stats, 'frameVariable', 1, 0) 108 109 def test_default_no_run(self): 110 """Test "statistics dump" without running the target. 111 112 When we don't run the target, we expect to not see any 'firstStopTime' 113 or 'launchOrAttachTime' top level keys that measure the launch or 114 attach of the target. 115 116 Output expected to be something like: 117 118 (lldb) statistics dump 119 { 120 "targetCreateTime": 0.26566899599999999, 121 "expressionEvaluation": { 122 "failures": 0, 123 "successes": 0 124 }, 125 "frameVariable": { 126 "failures": 0, 127 "successes": 0 128 }, 129 } 130 """ 131 target = self.createTestTarget() 132 stats = self.get_stats() 133 keys_exist = [ 134 'expressionEvaluation', 135 'frameVariable', 136 'targetCreateTime', 137 ] 138 keys_missing = [ 139 'firstStopTime', 140 'launchOrAttachTime' 141 ] 142 self.verify_keys(stats, '"stats"', keys_exist, keys_missing) 143 self.assertGreater(stats['targetCreateTime'], 0.0) 144 145 def test_default_with_run(self): 146 """Test "statistics dump" when running the target to a breakpoint. 147 148 When we run the target, we expect to see 'launchOrAttachTime' and 149 'firstStopTime' top level keys. 150 151 Output expected to be something like: 152 153 (lldb) statistics dump 154 { 155 "firstStopTime": 0.34164492800000001, 156 "launchOrAttachTime": 0.31969605400000001, 157 "targetCreateTime": 0.0040863039999999998 158 "expressionEvaluation": { 159 "failures": 0, 160 "successes": 0 161 }, 162 "frameVariable": { 163 "failures": 0, 164 "successes": 0 165 }, 166 } 167 168 """ 169 target = self.createTestTarget() 170 lldbutil.run_to_source_breakpoint(self, "// break here", 171 lldb.SBFileSpec("main.c")) 172 stats = self.get_stats() 173 keys_exist = [ 174 'expressionEvaluation', 175 'firstStopTime', 176 'frameVariable', 177 'launchOrAttachTime', 178 'targetCreateTime', 179 ] 180 self.verify_keys(stats, '"stats"', keys_exist, None) 181 self.assertGreater(stats['firstStopTime'], 0.0) 182 self.assertGreater(stats['launchOrAttachTime'], 0.0) 183 self.assertGreater(stats['targetCreateTime'], 0.0) 184