Djordje Todorovic | 2ef18fb | 2019-10-02 07:00:01 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # This is a tool that works like debug location coverage calculator. |
| 4 | # It parses the llvm-dwarfdump --statistics output by reporting it |
| 5 | # in a more human readable way. |
| 6 | # |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | import argparse |
| 10 | import os |
| 11 | import sys |
| 12 | from json import loads |
| 13 | from math import ceil |
| 14 | from subprocess import Popen, PIPE |
| 15 | |
| 16 | def coverage_buckets(): |
| 17 | yield '0%' |
| 18 | yield '1-9%' |
| 19 | for start in range(10, 91, 10): |
| 20 | yield '{0}-{1}%'.format(start, start + 9) |
| 21 | yield '100%' |
| 22 | |
| 23 | def locstats_output( |
| 24 | variables_total, |
| 25 | variables_total_locstats, |
| 26 | variables_with_loc, |
| 27 | scope_bytes_covered, |
Kristina Bessonova | 68f464a | 2019-11-19 13:28:21 +0300 | [diff] [blame^] | 28 | scope_bytes, |
Djordje Todorovic | 2ef18fb | 2019-10-02 07:00:01 +0000 | [diff] [blame] | 29 | variables_coverage_map |
| 30 | ): |
| 31 | |
| 32 | pc_ranges_covered = int(ceil(scope_bytes_covered * 100.0) |
Kristina Bessonova | 68f464a | 2019-11-19 13:28:21 +0300 | [diff] [blame^] | 33 | / scope_bytes) |
Djordje Todorovic | 2ef18fb | 2019-10-02 07:00:01 +0000 | [diff] [blame] | 34 | variables_coverage_per_map = {} |
| 35 | for cov_bucket in coverage_buckets(): |
| 36 | variables_coverage_per_map[cov_bucket] = \ |
| 37 | int(ceil(variables_coverage_map[cov_bucket] * 100.0) \ |
| 38 | / variables_total_locstats) |
| 39 | |
| 40 | print (' =================================================') |
| 41 | print (' Debug Location Statistics ') |
| 42 | print (' =================================================') |
| 43 | print (' cov% samples percentage(~) ') |
| 44 | print (' -------------------------------------------------') |
| 45 | for cov_bucket in coverage_buckets(): |
| 46 | print (' {0:6} {1:8d} {2:3d}%'. \ |
| 47 | format(cov_bucket, variables_coverage_map[cov_bucket], \ |
| 48 | variables_coverage_per_map[cov_bucket])) |
| 49 | print (' =================================================') |
| 50 | print (' -the number of debug variables processed: ' \ |
| 51 | + str(variables_total_locstats)) |
| 52 | print (' -PC ranges covered: ' + str(pc_ranges_covered) + '%') |
| 53 | |
| 54 | # Only if we are processing all the variables output the total |
| 55 | # availability. |
| 56 | if variables_total and variables_with_loc: |
| 57 | total_availability = int(ceil(variables_with_loc * 100.0) \ |
| 58 | / variables_total) |
| 59 | print (' -------------------------------------------------') |
| 60 | print (' -total availability: ' + str(total_availability) + '%') |
| 61 | print (' =================================================') |
| 62 | |
| 63 | def parse_program_args(parser): |
| 64 | parser.add_argument('-only-variables', action='store_true', |
| 65 | default=False, |
| 66 | help='calculate the location statistics only for ' |
| 67 | 'local variables' |
| 68 | ) |
| 69 | parser.add_argument('-only-formal-parameters', action='store_true', |
| 70 | default=False, |
| 71 | help='calculate the location statistics only for ' |
| 72 | 'formal parameters' |
| 73 | ) |
| 74 | parser.add_argument('-ignore-debug-entry-values', action='store_true', |
| 75 | default=False, |
| 76 | help='ignore the location statistics on locations with ' |
| 77 | 'entry values' |
| 78 | ) |
| 79 | parser.add_argument('file_name', type=str, help='file to process') |
| 80 | return parser.parse_args() |
| 81 | |
| 82 | |
| 83 | def Main(): |
| 84 | parser = argparse.ArgumentParser() |
| 85 | results = parse_program_args(parser) |
| 86 | |
| 87 | if len(sys.argv) < 2: |
| 88 | print ('error: Too few arguments.') |
| 89 | parser.print_help() |
| 90 | sys.exit(1) |
| 91 | |
| 92 | if results.only_variables and results.only_formal_parameters: |
| 93 | print ('error: Please use just one only* option.') |
| 94 | parser.print_help() |
| 95 | sys.exit(1) |
| 96 | |
| 97 | # These will be different due to different options enabled. |
| 98 | variables_total = None |
| 99 | variables_total_locstats = None |
| 100 | variables_with_loc = None |
| 101 | variables_scope_bytes_covered = None |
Kristina Bessonova | 68f464a | 2019-11-19 13:28:21 +0300 | [diff] [blame^] | 102 | variables_scope_bytes = None |
Djordje Todorovic | 2ef18fb | 2019-10-02 07:00:01 +0000 | [diff] [blame] | 103 | variables_scope_bytes_entry_values = None |
| 104 | variables_coverage_map = {} |
| 105 | binary = results.file_name |
| 106 | |
| 107 | # Get the directory of the LLVM tools. |
| 108 | llvm_dwarfdump_cmd = os.path.join(os.path.dirname(__file__), \ |
| 109 | "llvm-dwarfdump") |
| 110 | # The statistics llvm-dwarfdump option. |
| 111 | llvm_dwarfdump_stats_opt = "--statistics" |
| 112 | |
| 113 | subproc = Popen([llvm_dwarfdump_cmd, llvm_dwarfdump_stats_opt, binary], \ |
| 114 | stdin=PIPE, stdout=PIPE, stderr=PIPE, \ |
| 115 | universal_newlines = True) |
| 116 | cmd_stdout, cmd_stderr = subproc.communicate() |
| 117 | |
| 118 | # Get the JSON and parse it. |
| 119 | json_parsed = None |
| 120 | |
| 121 | try: |
| 122 | json_parsed = loads(cmd_stdout) |
| 123 | except: |
| 124 | print ('error: No valid llvm-dwarfdump statistics found.') |
| 125 | sys.exit(1) |
| 126 | |
| 127 | if results.only_variables: |
| 128 | # Read the JSON only for local variables. |
| 129 | variables_total_locstats = \ |
| 130 | json_parsed['total vars procesed by location statistics'] |
| 131 | variables_scope_bytes_covered = \ |
| 132 | json_parsed['vars scope bytes covered'] |
Kristina Bessonova | 68f464a | 2019-11-19 13:28:21 +0300 | [diff] [blame^] | 133 | variables_scope_bytes = \ |
Djordje Todorovic | 2ef18fb | 2019-10-02 07:00:01 +0000 | [diff] [blame] | 134 | json_parsed['vars scope bytes total'] |
| 135 | if not results.ignore_debug_entry_values: |
| 136 | for cov_bucket in coverage_buckets(): |
| 137 | cov_category = "vars with {} of its scope covered".format(cov_bucket) |
| 138 | variables_coverage_map[cov_bucket] = json_parsed[cov_category] |
| 139 | else: |
| 140 | variables_scope_bytes_entry_values = \ |
| 141 | json_parsed['vars entry value scope bytes covered'] |
| 142 | variables_scope_bytes_covered = variables_scope_bytes_covered \ |
| 143 | - variables_scope_bytes_entry_values |
| 144 | for cov_bucket in coverage_buckets(): |
| 145 | cov_category = \ |
| 146 | "vars (excluding the debug entry values) " \ |
| 147 | "with {} of its scope covered".format(cov_bucket) |
| 148 | variables_coverage_map[cov_bucket] = json_parsed[cov_category] |
| 149 | elif results.only_formal_parameters: |
| 150 | # Read the JSON only for formal parameters. |
| 151 | variables_total_locstats = \ |
| 152 | json_parsed['total params procesed by location statistics'] |
| 153 | variables_scope_bytes_covered = \ |
| 154 | json_parsed['formal params scope bytes covered'] |
Kristina Bessonova | 68f464a | 2019-11-19 13:28:21 +0300 | [diff] [blame^] | 155 | variables_scope_bytes = \ |
Djordje Todorovic | 2ef18fb | 2019-10-02 07:00:01 +0000 | [diff] [blame] | 156 | json_parsed['formal params scope bytes total'] |
| 157 | if not results.ignore_debug_entry_values: |
| 158 | for cov_bucket in coverage_buckets(): |
| 159 | cov_category = "params with {} of its scope covered".format(cov_bucket) |
| 160 | variables_coverage_map[cov_bucket] = json_parsed[cov_category] |
| 161 | else: |
| 162 | variables_scope_bytes_entry_values = \ |
| 163 | json_parsed['formal params entry value scope bytes covered'] |
| 164 | variables_scope_bytes_covered = variables_scope_bytes_covered \ |
| 165 | - variables_scope_bytes_entry_values |
| 166 | for cov_bucket in coverage_buckets(): |
| 167 | cov_category = \ |
| 168 | "params (excluding the debug entry values) " \ |
| 169 | "with {} of its scope covered".format(cov_bucket) |
Djordje Todorovic | 095531e | 2019-10-15 10:12:14 +0000 | [diff] [blame] | 170 | variables_coverage_map[cov_bucket] = json_parsed[cov_category] |
Djordje Todorovic | 2ef18fb | 2019-10-02 07:00:01 +0000 | [diff] [blame] | 171 | else: |
| 172 | # Read the JSON for both local variables and formal parameters. |
| 173 | variables_total = \ |
| 174 | json_parsed['source variables'] |
| 175 | variables_with_loc = json_parsed['variables with location'] |
| 176 | variables_total_locstats = \ |
| 177 | json_parsed['total variables procesed by location statistics'] |
| 178 | variables_scope_bytes_covered = \ |
| 179 | json_parsed['scope bytes covered'] |
Kristina Bessonova | 68f464a | 2019-11-19 13:28:21 +0300 | [diff] [blame^] | 180 | variables_scope_bytes = \ |
Djordje Todorovic | 2ef18fb | 2019-10-02 07:00:01 +0000 | [diff] [blame] | 181 | json_parsed['scope bytes total'] |
| 182 | if not results.ignore_debug_entry_values: |
| 183 | for cov_bucket in coverage_buckets(): |
| 184 | cov_category = "variables with {} of its scope covered". \ |
| 185 | format(cov_bucket) |
| 186 | variables_coverage_map[cov_bucket] = json_parsed[cov_category] |
| 187 | else: |
| 188 | variables_scope_bytes_entry_values = \ |
| 189 | json_parsed['entry value scope bytes covered'] |
| 190 | variables_scope_bytes_covered = variables_scope_bytes_covered \ |
| 191 | - variables_scope_bytes_entry_values |
| 192 | for cov_bucket in coverage_buckets(): |
| 193 | cov_category = "variables (excluding the debug entry values) " \ |
| 194 | "with {} of its scope covered". format(cov_bucket) |
| 195 | variables_coverage_map[cov_bucket] = json_parsed[cov_category] |
| 196 | |
| 197 | # Pretty print collected info. |
| 198 | locstats_output( |
| 199 | variables_total, |
| 200 | variables_total_locstats, |
| 201 | variables_with_loc, |
| 202 | variables_scope_bytes_covered, |
Kristina Bessonova | 68f464a | 2019-11-19 13:28:21 +0300 | [diff] [blame^] | 203 | variables_scope_bytes, |
Djordje Todorovic | 2ef18fb | 2019-10-02 07:00:01 +0000 | [diff] [blame] | 204 | variables_coverage_map |
| 205 | ) |
| 206 | |
| 207 | if __name__ == '__main__': |
| 208 | Main() |
| 209 | sys.exit(0) |