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.
8 from __future__
import print_function
12 from json
import loads
14 from subprocess
import Popen
, PIPE
16 def coverage_buckets():
19 for start
in range(10, 91, 10):
20 yield '{0}-{1}%'.format(start
, start
+ 9)
25 variables_total_locstats
,
28 scope_bytes_from_first_def
,
29 variables_coverage_map
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
)
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
) + '%')
54 # Only if we are processing all the variables output the total
56 if variables_total
and variables_with_loc
:
57 total_availability
= int(ceil(variables_with_loc
* 100.0) \
59 print (' -------------------------------------------------')
60 print (' -total availability: ' + str(total_availability
) + '%')
61 print (' =================================================')
63 def parse_program_args(parser
):
64 parser
.add_argument('-only-variables', action
='store_true',
66 help='calculate the location statistics only for '
69 parser
.add_argument('-only-formal-parameters', action
='store_true',
71 help='calculate the location statistics only for '
74 parser
.add_argument('-ignore-debug-entry-values', action
='store_true',
76 help='ignore the location statistics on locations with '
79 parser
.add_argument('file_name', type=str, help='file to process')
80 return parser
.parse_args()
84 parser
= argparse
.ArgumentParser()
85 results
= parse_program_args(parser
)
88 print ('error: Too few arguments.')
92 if results
.only_variables
and results
.only_formal_parameters
:
93 print ('error: Please use just one only* option.')
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
107 # Get the directory of the LLVM tools.
108 llvm_dwarfdump_cmd
= os
.path
.join(os
.path
.dirname(__file__
), \
110 # The statistics llvm-dwarfdump option.
111 llvm_dwarfdump_stats_opt
= "--statistics"
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()
118 # Get the JSON and parse it.
122 json_parsed
= loads(cmd_stdout
)
124 print ('error: No valid llvm-dwarfdump statistics found.')
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
]
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():
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
]
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():
168 "params (excluding the debug entry values) " \
169 "with {} of its scope covered".format(cov_bucket
)
170 variables_coverage_map
[cov_bucket
] = json_parsed
[cov_category
]
172 # Read the JSON for both local variables and formal parameters.
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']
180 variables_scope_bytes_from_first_def
= \
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". \
186 variables_coverage_map
[cov_bucket
] = json_parsed
[cov_category
]
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
]
197 # Pretty print collected info.
200 variables_total_locstats
,
202 variables_scope_bytes_covered
,
203 variables_scope_bytes_from_first_def
,
204 variables_coverage_map
207 if __name__
== '__main__':