|
| 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, |
| 28 | + scope_bytes_from_first_def, |
| 29 | + variables_coverage_map |
| 30 | + ): |
| 31 | + |
| 32 | + pc_ranges_covered = int(ceil(scope_bytes_covered * 100.0) |
| 33 | + / scope_bytes_from_first_def) |
| 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 |
| 102 | + variables_scope_bytes_from_first_def = None |
| 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'] |
| 133 | + variables_scope_bytes_from_first_def = \ |
| 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'] |
| 155 | + variables_scope_bytes_from_first_def = \ |
| 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) |
| 170 | + else: |
| 171 | + # Read the JSON for both local variables and formal parameters. |
| 172 | + variables_total = \ |
| 173 | + json_parsed['source variables'] |
| 174 | + variables_with_loc = json_parsed['variables with location'] |
| 175 | + variables_total_locstats = \ |
| 176 | + json_parsed['total variables procesed by location statistics'] |
| 177 | + variables_scope_bytes_covered = \ |
| 178 | + json_parsed['scope bytes covered'] |
| 179 | + variables_scope_bytes_from_first_def = \ |
| 180 | + json_parsed['scope bytes total'] |
| 181 | + if not results.ignore_debug_entry_values: |
| 182 | + for cov_bucket in coverage_buckets(): |
| 183 | + cov_category = "variables with {} of its scope covered". \ |
| 184 | + format(cov_bucket) |
| 185 | + variables_coverage_map[cov_bucket] = json_parsed[cov_category] |
| 186 | + else: |
| 187 | + variables_scope_bytes_entry_values = \ |
| 188 | + json_parsed['entry value scope bytes covered'] |
| 189 | + variables_scope_bytes_covered = variables_scope_bytes_covered \ |
| 190 | + - variables_scope_bytes_entry_values |
| 191 | + for cov_bucket in coverage_buckets(): |
| 192 | + cov_category = "variables (excluding the debug entry values) " \ |
| 193 | + "with {} of its scope covered". format(cov_bucket) |
| 194 | + variables_coverage_map[cov_bucket] = json_parsed[cov_category] |
| 195 | + |
| 196 | + # Pretty print collected info. |
| 197 | + locstats_output( |
| 198 | + variables_total, |
| 199 | + variables_total_locstats, |
| 200 | + variables_with_loc, |
| 201 | + variables_scope_bytes_covered, |
| 202 | + variables_scope_bytes_from_first_def, |
| 203 | + variables_coverage_map |
| 204 | + ) |
| 205 | + |
| 206 | +if __name__ == '__main__': |
| 207 | + Main() |
| 208 | + sys.exit(0) |
0 commit comments