3 # Copyright (c) International Business Machines Corp., 2002,2012
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or (at
8 # your option) any later version.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 # This is a wrapper script which provides a single interface for accessing
27 # 2002-08-29 created by Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
29 # 2002-09-05 / Peter Oberparleiter: implemented --kernel-directory +
30 # multiple directories
31 # 2002-10-16 / Peter Oberparleiter: implemented --add-tracefile option
32 # 2002-10-17 / Peter Oberparleiter: implemented --extract option
33 # 2002-11-04 / Peter Oberparleiter: implemented --list option
34 # 2003-03-07 / Paul Larson: Changed to make it work with the latest gcov
35 # kernel patch. This will break it with older gcov-kernel
36 # patches unless you change the value of $gcovmod in this script
37 # 2003-04-07 / Peter Oberparleiter: fixed bug which resulted in an error
38 # when trying to combine .info files containing data without
40 # 2003-04-10 / Peter Oberparleiter: extended Paul's change so that LCOV
41 # works both with the new and the old gcov-kernel patch
42 # 2003-04-10 / Peter Oberparleiter: added $gcov_dir constant in anticipation
43 # of a possible move of the gcov kernel directory to another
44 # file system in a future version of the gcov-kernel patch
45 # 2003-04-15 / Paul Larson: make info write to STDERR, not STDOUT
46 # 2003-04-15 / Paul Larson: added --remove option
47 # 2003-04-30 / Peter Oberparleiter: renamed --reset to --zerocounters
48 # to remove naming ambiguity with --remove
49 # 2003-04-30 / Peter Oberparleiter: adjusted help text to include --remove
50 # 2003-06-27 / Peter Oberparleiter: implemented --diff
51 # 2003-07-03 / Peter Oberparleiter: added line checksum support, added
53 # 2003-12-11 / Laurent Deniel: added --follow option
54 # 2004-03-29 / Peter Oberparleiter: modified --diff option to better cope with
55 # ambiguous patch file entries, modified --capture option to use
56 # modprobe before insmod (needed for 2.6)
57 # 2004-03-30 / Peter Oberparleiter: added --path option
58 # 2004-08-09 / Peter Oberparleiter: added configuration file support
59 # 2008-08-13 / Peter Oberparleiter: added function coverage support
66 use File
::Temp qw
/tempdir/;
67 use File
::Spec
::Functions qw
/abs2rel canonpath catdir catfile catpath
68 file_name_is_absolute rootdir splitdir splitpath
/;
70 use Cwd qw
/abs_path getcwd/;
74 our $lcov_version = 'LCOV version 1.10';
75 our $lcov_url = "http://ltp.sourceforge.net/coverage/lcov.php";
76 our $tool_name = basename
($0);
78 # Directory containing gcov kernel files
81 # Where to create temporary directories
85 our $GKV_PROC = 0; # gcov-kernel data in /proc via external patch
86 our $GKV_SYS = 1; # gcov-kernel data in /sys via vanilla 2.6.31+
87 our @GKV_NAME = ( "external", "upstream" );
88 our $pkg_gkv_file = ".gcov_kernel_version";
89 our $pkg_build_file = ".build_directory";
94 our $BR_VEC_ENTRIES = 3;
95 our $BR_VEC_WIDTH = 32;
97 # Branch data combination types
104 sub userspace_reset
();
105 sub userspace_capture
();
107 sub kernel_capture
();
108 sub kernel_capture_initial
();
109 sub package_capture
();
111 sub read_info_file
($);
112 sub get_info_entry
($);
113 sub set_info_entry
($$$$$$$$$;$$$$$$);
115 sub merge_checksums
($$$);
116 sub combine_info_entries
($$$);
117 sub combine_info_files
($$);
118 sub write_info_file
(*$);
122 sub get_common_filename
($$);
125 sub system_no_output
($@
);
129 sub create_temp_dir
();
130 sub transform_pattern
($);
133 sub abort_handler
($);
136 sub get_overall_line
($$$$);
137 sub print_overall_rate
($$$$$$$$$);
139 sub create_package
($$$;$);
140 sub get_func_found_and_hit
($);
145 # Global variables & initialization
146 our @directory; # Specifies where to get coverage data from
147 our @kernel_directory; # If set, captures only from specified kernel subdirs
148 our @add_tracefile; # If set, reads in and combines all files in list
149 our $list; # If set, list contents of tracefile
150 our $extract; # If set, extracts parts of tracefile
151 our $remove; # If set, removes parts of tracefile
152 our $diff; # If set, modifies tracefile according to diff
153 our $reset; # If set, reset all coverage data to zero
154 our $capture; # If set, capture data
155 our $output_filename; # Name for file to write coverage data to
156 our $test_name = ""; # Test case name
157 our $quiet = ""; # If set, suppress information messages
158 our $help; # Help option flag
159 our $version; # Version option flag
160 our $convert_filenames; # If set, convert filenames when applying diff
161 our $strip; # If set, strip leading directories when applying diff
162 our $temp_dir_name; # Name of temporary directory
163 our $cwd = `pwd`; # Current working directory
164 our $to_file; # If set, indicates that output is written to a file
165 our $follow; # If set, indicates that find shall follow links
166 our $diff_path = ""; # Path removed from tracefile when applying diff
167 our $base_directory; # Base directory (cwd of gcc during compilation)
168 our $checksum; # If set, calculate a checksum for each line
169 our $no_checksum; # If set, don't calculate a checksum for each line
170 our $compat_libtool; # If set, indicates that libtool mode is to be enabled
171 our $no_compat_libtool; # If set, indicates that libtool mode is to be disabled
173 our @opt_ignore_errors;
175 our $no_recursion = 0;
180 our $config; # Configuration file contents
182 our $tool_dir = dirname
($0); # Directory where genhtml tool is installed
184 our $gcov_gkv; # gcov kernel support version found on machine
185 our $opt_derive_func_data;
187 our $opt_list_full_path;
188 our $opt_no_list_full_path;
189 our $opt_list_width = 80;
190 our $opt_list_truncate_max = 20;
192 our $opt_no_external;
193 our $opt_config_file;
197 our $ln_overall_found;
199 our $fn_overall_found;
201 our $br_overall_found;
203 our $func_coverage = 1;
204 our $br_coverage = 0;
211 $SIG{__WARN__
} = \
&warn_handler
;
212 $SIG{__DIE__
} = \
&die_handler
;
213 $SIG{'INT'} = \
&abort_handler
;
214 $SIG{'QUIT'} = \
&abort_handler
;
216 # Prettify version string
217 $lcov_version =~ s/\$\s*Revision\s*:?\s*(\S+)\s*\$/$1/;
219 # Add current working directory if $tool_dir is not already an absolute path
220 if (! ($tool_dir =~ /^\/(.*)$/))
222 $tool_dir = "$cwd/$tool_dir";
225 # Check command line for a configuration file name
226 Getopt
::Long
::Configure
("pass_through", "no_auto_abbrev");
227 GetOptions
("config-file=s" => \
$opt_config_file,
228 "rc=s%" => \
%opt_rc);
229 Getopt
::Long
::Configure
("default");
231 # Read configuration file if available
232 if (defined($opt_config_file)) {
233 $config = read_config
($opt_config_file);
234 } elsif (defined($ENV{"HOME"}) && (-r
$ENV{"HOME"}."/.lcovrc"))
236 $config = read_config
($ENV{"HOME"}."/.lcovrc");
238 elsif (-r
"/etc/lcovrc")
240 $config = read_config
("/etc/lcovrc");
243 if ($config || %opt_rc)
245 # Copy configuration file and --rc values to variables
247 "lcov_gcov_dir" => \
$gcov_dir,
248 "lcov_tmp_dir" => \
$tmp_dir,
249 "lcov_list_full_path" => \
$opt_list_full_path,
250 "lcov_list_width" => \
$opt_list_width,
251 "lcov_list_truncate_max"=> \
$opt_list_truncate_max,
252 "lcov_branch_coverage" => \
$br_coverage,
253 "lcov_function_coverage"=> \
$func_coverage,
257 # Parse command line options
258 if (!GetOptions
("directory|d|di=s" => \
@directory,
259 "add-tracefile|a=s" => \
@add_tracefile,
260 "list|l=s" => \
$list,
261 "kernel-directory|k=s" => \
@kernel_directory,
262 "extract|e=s" => \
$extract,
263 "remove|r=s" => \
$remove,
265 "convert-filenames" => \
$convert_filenames,
266 "strip=i" => \
$strip,
267 "capture|c" => \
$capture,
268 "output-file|o=s" => \
$output_filename,
269 "test-name|t=s" => \
$test_name,
270 "zerocounters|z" => \
$reset,
271 "quiet|q" => \
$quiet,
272 "help|h|?" => \
$help,
273 "version|v" => \
$version,
274 "follow|f" => \
$follow,
275 "path=s" => \
$diff_path,
276 "base-directory|b=s" => \
$base_directory,
277 "checksum" => \
$checksum,
278 "no-checksum" => \
$no_checksum,
279 "compat-libtool" => \
$compat_libtool,
280 "no-compat-libtool" => \
$no_compat_libtool,
281 "gcov-tool=s" => \
$gcov_tool,
282 "ignore-errors=s" => \
@opt_ignore_errors,
283 "initial|i" => \
$initial,
284 "no-recursion" => \
$no_recursion,
285 "to-package=s" => \
$to_package,
286 "from-package=s" => \
$from_package,
287 "no-markers" => \
$no_markers,
288 "derive-func-data" => \
$opt_derive_func_data,
289 "debug" => \
$opt_debug,
290 "list-full-path" => \
$opt_list_full_path,
291 "no-list-full-path" => \
$opt_no_list_full_path,
292 "external" => \
$opt_external,
293 "no-external" => \
$opt_no_external,
294 "summary=s" => \
@opt_summary,
295 "compat=s" => \
$opt_compat,
296 "config-file=s" => \
$opt_config_file,
300 print(STDERR
"Use $tool_name --help to get usage information\n");
306 if (defined($no_checksum))
308 $checksum = ($no_checksum ?
0 : 1);
309 $no_checksum = undef;
312 if (defined($no_compat_libtool))
314 $compat_libtool = ($no_compat_libtool ?
0 : 1);
315 $no_compat_libtool = undef;
318 if (defined($opt_no_list_full_path))
320 $opt_list_full_path = ($opt_no_list_full_path ?
0 : 1);
321 $opt_no_list_full_path = undef;
324 if (defined($opt_no_external)) {
326 $opt_no_external = undef;
330 # Check for help option
333 print_usage
(*STDOUT
);
337 # Check for version option
340 print("$tool_name: $lcov_version\n");
344 # Check list width option
345 if ($opt_list_width <= 40) {
346 die("ERROR: lcov_list_width parameter out of range (needs to be ".
347 "larger than 40)\n");
350 # Normalize --path text
351 $diff_path =~ s/\/$//;
364 $maxdepth = "-maxdepth 1";
371 # Check for valid options
374 # Only --extract, --remove and --diff allow unnamed parameters
375 if (@ARGV && !($extract || $remove || $diff || @opt_summary))
377 die("Extra parameter found: '".join(" ", @ARGV)."'\n".
378 "Use $tool_name --help to get usage information\n");
381 # Check for output filename
382 $to_file = ($output_filename && ($output_filename ne "-"));
388 # Option that tells geninfo to write to stdout
389 $output_filename = "-";
393 # Determine kernel directory for gcov data
394 if (!$from_package && !@directory && ($capture || $reset)) {
395 ($gcov_gkv, $gcov_dir) = setup_gkv
();
398 # Check for requested functionality
401 # Differentiate between user space and kernel reset
413 # Capture source can be user space, kernel or package
416 } elsif (@directory) {
420 if (defined($to_package)) {
421 die("ERROR: --initial cannot be used together ".
422 "with --to-package\n");
424 kernel_capture_initial
();
430 elsif (@add_tracefile)
432 ($ln_overall_found, $ln_overall_hit,
433 $fn_overall_found, $fn_overall_hit,
434 $br_overall_found, $br_overall_hit) = add_traces
();
438 ($ln_overall_found, $ln_overall_hit,
439 $fn_overall_found, $fn_overall_hit,
440 $br_overall_found, $br_overall_hit) = remove
();
444 ($ln_overall_found, $ln_overall_hit,
445 $fn_overall_found, $fn_overall_hit,
446 $br_overall_found, $br_overall_hit) = extract
();
454 if (scalar(@ARGV) != 1)
456 die("ERROR: option --diff requires one additional argument!\n".
457 "Use $tool_name --help to get usage information\n");
459 ($ln_overall_found, $ln_overall_hit,
460 $fn_overall_found, $fn_overall_hit,
461 $br_overall_found, $br_overall_hit) = diff
();
465 ($ln_overall_found, $ln_overall_hit,
466 $fn_overall_found, $fn_overall_hit,
467 $br_overall_found, $br_overall_hit) = summary
();
472 if (defined($ln_overall_found)) {
473 print_overall_rate
(1, $ln_overall_found, $ln_overall_hit,
474 1, $fn_overall_found, $fn_overall_hit,
475 1, $br_overall_found, $br_overall_hit);
477 info
("Done.\n") if (!$list && !$capture);
482 # print_usage(handle)
484 # Print usage information.
489 local *HANDLE
= $_[0];
491 print(HANDLE
<<END_OF_USAGE);
492 Usage: $tool_name [OPTIONS]
494 Use lcov to collect coverage data from either the currently running Linux
495 kernel or from a user space application. Specify the --directory option to
496 get coverage data for a user space program.
499 -h, --help Print this help, then exit
500 -v, --version Print version number, then exit
501 -q, --quiet Do not print progress messages
504 -z, --zerocounters Reset all execution counts to zero
505 -c, --capture Capture coverage data
506 -a, --add-tracefile FILE Add contents of tracefiles
507 -e, --extract FILE PATTERN Extract files matching PATTERN from FILE
508 -r, --remove FILE PATTERN Remove files matching PATTERN from FILE
509 -l, --list FILE List contents of tracefile FILE
510 --diff FILE DIFF Transform tracefile FILE according to DIFF
511 --summary FILE Show summary coverage data for tracefiles
514 -i, --initial Capture initial zero coverage data
515 -t, --test-name NAME Specify test name to be stored with data
516 -o, --output-file FILENAME Write data to FILENAME instead of stdout
517 -d, --directory DIR Use .da files in DIR instead of kernel
518 -f, --follow Follow links when searching .da files
519 -k, --kernel-directory KDIR Capture kernel coverage data only from KDIR
520 -b, --base-directory DIR Use DIR as base directory for relative paths
521 --convert-filenames Convert filenames when applying diff
522 --strip DEPTH Strip initial DEPTH directory levels in diff
523 --path PATH Strip PATH from tracefile when applying diff
524 --(no-)checksum Enable (disable) line checksumming
525 --(no-)compat-libtool Enable (disable) libtool compatibility mode
526 --gcov-tool TOOL Specify gcov tool location
527 --ignore-errors ERRORS Continue after ERRORS (gcov, source, graph)
528 --no-recursion Exclude subdirectories from processing
529 --to-package FILENAME Store unprocessed coverage data in FILENAME
530 --from-package FILENAME Capture from unprocessed data in FILENAME
531 --no-markers Ignore exclusion markers in source code
532 --derive-func-data Generate function data from line data
533 --list-full-path Print full path during a list operation
534 --(no-)external Include (ignore) data for external files
535 --config-file FILENAME Specify configuration file location
536 --rc SETTING=VALUE Override configuration file setting
537 --compat MODE=on|off|auto Set compat MODE (libtool, hammer, split_crc)
539 For more information see: $lcov_url
548 # Check for valid combination of command line options. Die on error.
555 # Count occurrence of mutually exclusive options
558 @add_tracefile && $i++;
563 @opt_summary && $i++;
567 die("Need one of options -z, -c, -a, -e, -r, -l, ".
568 "--diff or --summary\n".
569 "Use $tool_name --help to get usage information\n");
573 die("ERROR: only one of -z, -c, -a, -e, -r, -l, ".
574 "--diff or --summary allowed!\n".
575 "Use $tool_name --help to get usage information\n");
583 # Reset coverage data found in DIRECTORY by deleting all contained .da files.
588 sub userspace_reset()
593 foreach $current_dir (@directory)
595 info("Deleting all .da files in $current_dir".
596 ($no_recursion?"\n":" and subdirectories\n"));
597 @file_list = `find "$current_dir" $maxdepth $follow -name \\*\\.da -o -name \\*\\.gcda -type f 2>/dev/null`;
601 unlink($_) or die("ERROR: cannot remove file $_!\n");
608 # userspace_capture()
610 # Capture coverage data found in DIRECTORY and write it to a package (if
611 # TO_PACKAGE specified) or to OUTPUT_FILENAME or STDOUT.
616 sub userspace_capture()
621 if (!defined($to_package)) {
622 lcov_geninfo(@directory);
625 if (scalar(@directory) != 1) {
626 die("ERROR: -d may be specified only once with --to-package\n");
628 $dir = $directory[0];
629 if (defined($base_directory)) {
630 $build = $base_directory;
634 create_package($to_package, $dir, $build);
641 # Reset kernel coverage.
651 info("Resetting kernel execution counters\n");
652 if (-e "$gcov_dir/vmlinux") {
653 $reset_file = "$gcov_dir/vmlinux";
654 } elsif (-e "$gcov_dir/reset") {
655 $reset_file = "$gcov_dir/reset";
657 die("ERROR: no reset control found in $gcov_dir\n");
659 open(HANDLE, ">", $reset_file) or
660 die("ERROR: cannot write to $reset_file!\n");
667 # lcov_copy_single(from, to)
669 # Copy single regular file FROM to TO without checking its size. This is
670 # required to work with special files generated by the kernel
671 # seq_file-interface.
674 sub lcov_copy_single($$)
676 my ($from, $to) = @_;
681 open(HANDLE, "<", $from) or die("ERROR: cannot read $from: $!\n");
684 open(HANDLE, ">", $to) or die("ERROR: cannot write $from: $!\n");
685 if (defined($content)) {
686 print(HANDLE $content);
692 # lcov_find(dir, function, data[, extension, ...)])
694 # Search DIR for files and directories whose name matches PATTERN and run
695 # FUNCTION for each match. If not pattern is specified, match all names.
697 # FUNCTION has the following prototype:
698 # function(dir, relative_name, data)
701 # dir: the base directory for this search
702 # relative_name: the name relative to the base directory of this entry
703 # data: the DATA variable passed to lcov_find
707 my ($dir, $fn, $data, @pattern) = @_;
710 my $filename = $File::Find::name;
712 if (defined($result)) {
715 $filename = abs2rel($filename, $dir);
717 if ($filename =~ /$_/) {
723 $result = &$fn($dir, $filename, $data);
725 if (scalar(@pattern) == 0) {
728 find( { wanted => $_fn, no_chdir => 1 }, $dir);
734 # lcov_copy_fn(from, rel, to)
736 # Copy directories, files and links from/rel to to/rel.
739 sub lcov_copy_fn($$$)
741 my ($from, $rel, $to) = @_;
742 my $absfrom = canonpath(catfile($from, $rel));
743 my $absto = canonpath(catfile($to, $rel));
748 die("ERROR: cannot create directory $absto\n");
753 my $link = readlink($absfrom);
755 if (!defined($link)) {
756 die("ERROR: cannot read link $absfrom: $!\n");
758 symlink($link, $absto) or
759 die("ERROR: cannot create link $absto: $!\n");
761 lcov_copy_single($absfrom, $absto);
768 # lcov_copy(from, to, subdirs)
770 # Copy all specified SUBDIRS and files from directory FROM to directory TO. For
771 # regular files, copy file contents without checking its size. This is required
772 # to work with seq_file-generated files.
777 my ($from, $to, @subdirs) = @_;
781 push(@pattern, "^$_");
783 lcov_find($from, \&lcov_copy_fn, $to, @pattern);
787 # lcov_geninfo(directory)
789 # Call geninfo for the specified directory and with the parameters specified
790 # at the command line.
799 info("Capturing coverage data from ".join(" ", @dir)."\n");
800 @param = ("$tool_dir/geninfo", @dir);
801 if ($output_filename)
803 @param = (@param, "--output-filename", $output_filename);
807 @param = (@param, "--test-name", $test_name);
811 @param = (@param, "--follow");
815 @param = (@param, "--quiet");
817 if (defined($checksum))
821 @param = (@param, "--checksum");
825 @param = (@param, "--no-checksum");
830 @param = (@param, "--base-directory", $base_directory);
832 if ($no_compat_libtool)
834 @param = (@param, "--no-compat-libtool");
836 elsif ($compat_libtool)
838 @param = (@param, "--compat-libtool");
842 @param = (@param, "--gcov-tool", $gcov_tool);
844 foreach (@opt_ignore_errors) {
845 @param = (@param, "--ignore-errors", $_);
848 @param = (@param, "--no-recursion");
852 @param = (@param, "--initial");
856 @param = (@param, "--no-markers");
858 if ($opt_derive_func_data)
860 @param = (@param, "--derive-func-data");
864 @param = (@param, "--debug");
866 if (defined($opt_external) && $opt_external)
868 @param = (@param, "--external");
870 if (defined($opt_external) && !$opt_external)
872 @param = (@param, "--no-external");
874 if (defined($opt_compat)) {
875 @param = (@param, "--compat", $opt_compat);
878 foreach my $key (keys(%opt_rc)) {
879 @param = (@param, "--rc", "$key=".$opt_rc{$key});
883 system(@param) and exit($? >> 8);
887 # read_file(filename)
889 # Return the contents of the file defined by filename.
899 open(HANDLE, "<", $filename) || return undef;
907 # get_package(package_file)
909 # Unpack unprocessed coverage data files from package_file to a temporary
910 # directory and return directory name, build directory and gcov kernel version
911 # as found in package.
917 my $dir = create_temp_dir();
924 info("Reading package $file:\n");
925 info(" data directory .......: $dir\n");
926 $file = abs_path($file);
928 open(HANDLE, "-|", "tar xvfz '$file' 2>/dev/null")
929 or die("ERROR: could not process package $file\n");
931 if (/\.da$/ || /\.gcda$/) {
936 $build = read_file("$dir/$pkg_build_file");
937 if (defined($build)) {
938 info(" build directory ......: $build\n");
940 $gkv = read_file("$dir/$pkg_gkv_file");
943 if ($gkv != $GKV_PROC && $gkv != $GKV_SYS) {
944 die("ERROR: unsupported gcov kernel version found ".
947 info(" content type .........: kernel data\n");
948 info(" gcov kernel version ..: %s\n", $GKV_NAME[$gkv]);
950 info(" content type .........: application data\n");
952 info(" data files ...........: $count\n");
955 return ($dir, $build, $gkv);
959 # write_file(filename, $content)
961 # Create a file named filename and write the specified content to it.
966 my ($filename, $content) = @_;
969 open(HANDLE, ">", $filename) || return 0;
970 print(HANDLE $content);
971 close(HANDLE) || return 0;
976 # count_package_data(filename)
978 # Count the number of coverage data files in the specified package file.
981 sub count_package_data($)
987 open(HANDLE, "-|", "tar tfz '$filename'") or return undef;
989 if (/\.da$/ || /\.gcda$/) {
998 # create_package(package_file, source_directory, build_directory[,
999 # kernel_gcov_version])
1001 # Store unprocessed coverage data files from source_directory to package_file.
1004 sub create_package($$$;$)
1006 my ($file, $dir, $build, $gkv) = @_;
1009 # Print information about the package
1010 info("Creating package $file:\n");
1011 info(" data directory .......: $dir\n");
1013 # Handle build directory
1014 if (defined($build)) {
1015 info(" build directory ......: $build\n");
1016 write_file("$dir/$pkg_build_file", $build)
1017 or die("ERROR: could not write to ".
1018 "$dir/$pkg_build_file\n");
1021 # Handle gcov kernel version data
1022 if (defined($gkv)) {
1023 info(" content type .........: kernel data\n");
1024 info(" gcov kernel version ..: %s\n", $GKV_NAME[$gkv]);
1025 write_file("$dir/$pkg_gkv_file", $gkv)
1026 or die("ERROR: could not write to ".
1027 "$dir/$pkg_gkv_file\n");
1029 info(" content type .........: application data\n");
1033 $file = abs_path($file);
1035 system("tar cfz $file .")
1036 and die("ERROR: could not create package $file\n");
1038 # Remove temporary files
1039 unlink("$dir/$pkg_build_file");
1040 unlink("$dir/$pkg_gkv_file");
1042 # Show number of data files
1044 my $count = count_package_data($file);
1046 if (defined($count)) {
1047 info(" data files ...........: $count\n");
1053 sub find_link_fn($$$)
1055 my ($from, $rel, $filename) = @_;
1056 my $absfile = catfile($from, $rel, $filename);
1067 # Return (BASE, OBJ), where
1068 # - BASE: is the path to the kernel base directory relative to dir
1069 # - OBJ: is the absolute path to the kernel build directory
1075 my $marker = "kernel/gcov/base.gcno";
1081 $markerfile = lcov_find($dir, \&find_link_fn, $marker);
1082 if (!defined($markerfile)) {
1083 return (undef, undef);
1086 # sys base is parent of parent of markerfile.
1087 $sys = abs2rel(dirname(dirname(dirname($markerfile))), $dir);
1089 # obj base is parent of parent of markerfile link target.
1090 $link = readlink($markerfile);
1091 if (!defined($link)) {
1092 die("ERROR: could not read $markerfile\n");
1094 $obj = dirname(dirname(dirname($link)));
1096 return ($sys, $obj);
1100 # apply_base_dir(data_dir, base_dir, build_dir, @directories)
1102 # Make entries in @directories relative to data_dir.
1105 sub apply_base_dir($$$@)
1107 my ($data, $base, $build, @dirs) = @_;
1111 foreach $dir (@dirs) {
1112 # Is directory path relative to data directory?
1113 if (-d catdir($data, $dir)) {
1114 push(@result, $dir);
1117 # Relative to the auto-detected base-directory?
1118 if (defined($base)) {
1119 if (-d catdir($data, $base, $dir)) {
1120 push(@result, catdir($base, $dir));
1124 # Relative to the specified base-directory?
1125 if (defined($base_directory)) {
1126 if (file_name_is_absolute($base_directory)) {
1127 $base = abs2rel($base_directory, rootdir());
1129 $base = $base_directory;
1131 if (-d catdir($data, $base, $dir)) {
1132 push(@result, catdir($base, $dir));
1136 # Relative to the build directory?
1137 if (defined($build)) {
1138 if (file_name_is_absolute($build)) {
1139 $base = abs2rel($build, rootdir());
1143 if (-d catdir($data, $base, $dir)) {
1144 push(@result, catdir($base, $dir));
1148 die("ERROR: subdirectory $dir not found\n".
1149 "Please use -b to specify the correct directory\n");
1155 # copy_gcov_dir(dir, [@subdirectories])
1157 # Create a temporary directory and copy all or, if specified, only some
1158 # subdirectories from dir to that directory. Return the name of the temporary
1162 sub copy_gcov_dir($;@)
1164 my ($data, @dirs) = @_;
1165 my $tempdir = create_temp_dir();
1167 info("Copying data to temporary directory $tempdir\n");
1168 lcov_copy($data, $tempdir, @dirs);
1174 # kernel_capture_initial
1176 # Capture initial kernel coverage data, i.e. create a coverage data file from
1177 # static graph files which contains zero coverage data for all instrumented
1181 sub kernel_capture_initial()
1187 if (defined($base_directory)) {
1188 $build = $base_directory;
1189 $source = "specified";
1191 (undef, $build) = get_base($gcov_dir);
1192 if (!defined($build)) {
1193 die("ERROR: could not auto-detect build directory.\n".
1194 "Please use -b to specify the build directory\n");
1196 $source = "auto-detected";
1198 info("Using $build as kernel build directory ($source)\n");
1199 # Build directory needs to be passed to geninfo
1200 $base_directory = $build;
1201 if (@kernel_directory) {
1202 foreach my $dir (@kernel_directory) {
1203 push(@params, "$build/$dir");
1206 push(@params, $build);
1208 lcov_geninfo(@params);
1212 # kernel_capture_from_dir(directory, gcov_kernel_version, build)
1214 # Perform the actual kernel coverage capturing from the specified directory
1215 # assuming that the data was copied from the specified gcov kernel version.
1218 sub kernel_capture_from_dir($$$)
1220 my ($dir, $gkv, $build) = @_;
1222 # Create package or coverage file
1223 if (defined($to_package)) {
1224 create_package($to_package, $dir, $build, $gkv);
1226 # Build directory needs to be passed to geninfo
1227 $base_directory = $build;
1233 # adjust_kernel_dir(dir, build)
1235 # Adjust directories specified with -k so that they point to the directory
1236 # relative to DIR. Return the build directory if specified or the auto-
1237 # detected build-directory.
1240 sub adjust_kernel_dir($$)
1242 my ($dir, $build) = @_;
1243 my ($sys_base, $build_auto) = get_base($dir);
1245 if (!defined($build)) {
1246 $build = $build_auto;
1248 if (!defined($build)) {
1249 die("ERROR: could not auto-detect build directory.\n".
1250 "Please use -b to specify the build directory\n");
1252 # Make @kernel_directory relative to sysfs base
1253 if (@kernel_directory) {
1254 @kernel_directory = apply_base_dir($dir, $sys_base, $build,
1260 sub kernel_capture()
1263 my $build = $base_directory;
1265 if ($gcov_gkv == $GKV_SYS) {
1266 $build = adjust_kernel_dir($gcov_dir, $build);
1268 $data_dir = copy_gcov_dir($gcov_dir, @kernel_directory);
1269 kernel_capture_from_dir($data_dir, $gcov_gkv, $build);
1275 # Capture coverage data from a package of unprocessed coverage data files
1276 # as generated by lcov --to-package.
1279 sub package_capture()
1285 ($dir, $build, $gkv) = get_package($from_package);
1287 # Check for build directory
1288 if (defined($base_directory)) {
1289 if (defined($build)) {
1290 info("Using build directory specified by -b.\n");
1292 $build = $base_directory;
1295 # Do the actual capture
1296 if (defined($gkv)) {
1297 if ($gkv == $GKV_SYS) {
1298 $build = adjust_kernel_dir($dir, $build);
1300 if (@kernel_directory) {
1301 $dir = copy_gcov_dir($dir, @kernel_directory);
1303 kernel_capture_from_dir($dir, $gkv, $build);
1305 # Build directory needs to be passed to geninfo
1306 $base_directory = $build;
1313 # info(printf_parameter)
1315 # Use printf to write PRINTF_PARAMETER to stdout only when the $quiet flag
1330 # Don't interfere with the .info output to STDOUT
1340 # Create a temporary directory and return its path.
1345 sub create_temp_dir()
1349 if (defined($tmp_dir)) {
1350 $dir = tempdir(DIR => $tmp_dir, CLEANUP => 1);
1352 $dir = tempdir(CLEANUP => 1);
1354 if (!defined($dir)) {
1355 die("ERROR: cannot create temporary directory\n");
1357 push(@temp_dirs, $dir);
1364 # br_taken_to_num(taken)
1366 # Convert a branch taken value .info format to number format.
1369 sub br_taken_to_num($)
1373 return 0 if ($taken eq '-');
1379 # br_num_to_taken(taken)
1381 # Convert a branch taken value in number format to .info format.
1384 sub br_num_to_taken($)
1388 return '-' if ($taken == 0);
1394 # br_taken_add(taken1, taken2)
1396 # Return the result of taken1 + taken2 for 'branch taken' values.
1399 sub br_taken_add($$)
1403 return $t1 if (!defined($t2));
1404 return $t2 if (!defined($t1));
1405 return $t1 if ($t2 eq '-');
1406 return $t2 if ($t1 eq '-');
1412 # br_taken_sub(taken1, taken2)
1414 # Return the result of taken1 - taken2 for 'branch taken' values. Return 0
1415 # if the result would become negative.
1418 sub br_taken_sub($$)
1422 return $t1 if (!defined($t2));
1423 return undef if (!defined($t1));
1424 return $t1 if ($t1 eq '-');
1425 return $t1 if ($t2 eq '-');
1426 return 0 if $t2 > $t1;
1433 # br_ivec_len(vector)
1435 # Return the number of entries in the branch coverage vector.
1442 return 0 if (!defined($vec));
1443 return (length($vec) * 8 / $BR_VEC_WIDTH) / $BR_VEC_ENTRIES;
1448 # br_ivec_push(vector, block, branch, taken)
1450 # Add an entry to the branch coverage vector. If an entry with the same
1451 # branch ID already exists, add the corresponding taken values.
1454 sub br_ivec_push($$$$)
1456 my ($vec, $block, $branch, $taken) = @_;
1458 my $num = br_ivec_len($vec);
1461 $vec = "" if (!defined($vec));
1463 # Check if branch already exists in vector
1464 for ($i = 0; $i < $num; $i++) {
1465 my ($v_block, $v_branch, $v_taken) = br_ivec_get($vec, $i);
1467 next if ($v_block != $block || $v_branch != $branch);
1470 $taken = br_taken_add($taken, $v_taken);
1474 $offset = $i * $BR_VEC_ENTRIES;
1475 $taken = br_taken_to_num($taken);
1478 vec($vec, $offset + $BR_BLOCK, $BR_VEC_WIDTH) = $block;
1479 vec($vec, $offset + $BR_BRANCH, $BR_VEC_WIDTH) = $branch;
1480 vec($vec, $offset + $BR_TAKEN, $BR_VEC_WIDTH) = $taken;
1487 # br_ivec_get(vector, number)
1489 # Return an entry from the branch coverage vector.
1494 my ($vec, $num) = @_;
1498 my $offset = $num * $BR_VEC_ENTRIES;
1500 # Retrieve data from vector
1501 $block = vec($vec, $offset + $BR_BLOCK, $BR_VEC_WIDTH);
1502 $branch = vec($vec, $offset + $BR_BRANCH, $BR_VEC_WIDTH);
1503 $taken = vec($vec, $offset + $BR_TAKEN, $BR_VEC_WIDTH);
1505 # Decode taken value from an integer
1506 $taken = br_num_to_taken($taken);
1508 return ($block, $branch, $taken);
1513 # get_br_found_and_hit(brcount)
1515 # Return (br_found, br_hit) for brcount
1518 sub get_br_found_and_hit($)
1525 foreach $line (keys(%{$brcount})) {
1526 my $brdata = $brcount->{$line};
1528 my $num = br_ivec_len($brdata);
1530 for ($i = 0; $i < $num; $i++) {
1533 (undef, undef, $taken) = br_ivec_get($brdata, $i);
1536 $br_hit++ if ($taken ne "-" && $taken > 0);
1540 return ($br_found, $br_hit);
1545 # read_info_file(info_filename)
1547 # Read in the contents of the .info file specified by INFO_FILENAME. Data will
1548 # be returned as a reference to a hash containing the following mappings:
1550 # %result: for each filename found in file -> \%data
1552 # %data: "test" -> \%testdata
1553 # "sum" -> \%sumcount
1554 # "func" -> \%funcdata
1555 # "found" -> $lines_found (number of instrumented lines found in file)
1556 # "hit" -> $lines_hit (number of executed lines in file)
1557 # "check" -> \%checkdata
1558 # "testfnc" -> \%testfncdata
1559 # "sumfnc" -> \%sumfnccount
1560 # "testbr" -> \%testbrdata
1561 # "sumbr" -> \%sumbrcount
1563 # %testdata : name of test affecting this file -> \%testcount
1564 # %testfncdata: name of test affecting this file -> \%testfnccount
1565 # %testbrdata: name of test affecting this file -> \%testbrcount
1567 # %testcount : line number -> execution count for a single test
1568 # %testfnccount: function name -> execution count for a single test
1569 # %testbrcount : line number -> branch coverage data for a single test
1570 # %sumcount : line number -> execution count for all tests
1571 # %sumfnccount : function name -> execution count for all tests
1572 # %sumbrcount : line number -> branch coverage data for all tests
1573 # %funcdata : function name -> line number
1574 # %checkdata : line number -> checksum of source code line
1575 # $brdata : vector of items: block, branch, taken
1577 # Note that .info file sections referring to the same file and test name
1578 # will automatically be combined by adding all execution counts.
1580 # Note that if INFO_FILENAME ends with ".gz", it is assumed that the file
1581 # is compressed using GZIP. If available, GUNZIP will be used to decompress
1587 sub read_info_file($)
1589 my $tracefile = $_[0]; # Name of tracefile
1590 my %result; # Resulting hash: file -> data
1591 my $data; # Data handle for current entry
1593 my $testcount; # " "
1596 my $checkdata; # " "
1603 my $line; # Current line read from .info file
1604 my $testname; # Current test name
1605 my $filename; # Current filename
1606 my $hitcount; # Count for lines hit
1607 my $count; # Execution count of current line
1608 my $negative; # If set, warn about negative counts
1609 my $changed_testname; # If set, warn about changed testname
1610 my $line_checksum; # Checksum of current line
1611 local *INFO_HANDLE; # Filehandle for .info file
1613 info("Reading tracefile $tracefile\n");
1615 # Check if file exists and is readable
1619 die("ERROR: cannot read file $_[0]!\n");
1622 # Check if this is really a plain file
1625 die("ERROR: not a plain file: $_[0]!\n");
1628 # Check for .gz extension
1629 if ($_[0] =~ /\.gz$/)
1631 # Check for availability of GZIP tool
1632 system_no_output(1, "gunzip" ,"-h")
1633 and die("ERROR: gunzip command not available!\n");
1635 # Check integrity of compressed file
1636 system_no_output(1, "gunzip", "-t", $_[0])
1637 and die("ERROR: integrity check failed for ".
1638 "compressed file $_[0]!\n");
1640 # Open compressed file
1641 open(INFO_HANDLE, "-|", "gunzip -c '$_[0]'")
1642 or die("ERROR: cannot start gunzip to decompress ".
1647 # Open decompressed file
1648 open(INFO_HANDLE, "<", $_[0])
1649 or die("ERROR: cannot read file $_[0]!\n");
1653 while (<INFO_HANDLE>)
1661 /^TN:([^,]*)(,diff)?/ && do
1663 # Test name information found
1664 $testname = defined($1) ? $1 : "";
1665 if ($testname =~ s/\W/_/g)
1667 $changed_testname = 1;
1669 $testname .= $2 if (defined($2));
1675 # Filename information found
1676 # Retrieve data for new entry
1679 $data = $result{$filename};
1680 ($testdata, $sumcount, $funcdata, $checkdata,
1681 $testfncdata, $sumfnccount, $testbrdata,
1683 get_info_entry($data);
1685 if (defined($testname))
1687 $testcount = $testdata->{$testname};
1688 $testfnccount = $testfncdata->{$testname};
1689 $testbrcount = $testbrdata->{$testname};
1700 /^DA:(\d+),(-?\d+)(,[^,\s]+)?/ && do
1702 # Fix negative counts
1703 $count = $2 < 0 ? 0 : $2;
1708 # Execution count found, add to structure
1709 # Add summary counts
1710 $sumcount->{$1} += $count;
1712 # Add test-specific counts
1713 if (defined($testname))
1715 $testcount->{$1} += $count;
1718 # Store line checksum if available
1721 $line_checksum = substr($3, 1);
1723 # Does it match a previous definition
1724 if (defined($checkdata->{$1}) &&
1725 ($checkdata->{$1} ne
1728 die("ERROR: checksum mismatch ".
1729 "at $filename:$1\n");
1732 $checkdata->{$1} = $line_checksum;
1737 /^FN:(\d+),([^,]+)/ && do
1739 last if (!$func_coverage);
1741 # Function data found, add to structure
1742 $funcdata->{$2} = $1;
1744 # Also initialize function call data
1745 if (!defined($sumfnccount->{$2})) {
1746 $sumfnccount->{$2} = 0;
1748 if (defined($testname))
1750 if (!defined($testfnccount->{$2})) {
1751 $testfnccount->{$2} = 0;
1757 /^FNDA:(\d+),([^,]+)/ && do
1759 last if (!$func_coverage);
1761 # Function call count found, add to structure
1762 # Add summary counts
1763 $sumfnccount->{$2} += $1;
1765 # Add test-specific counts
1766 if (defined($testname))
1768 $testfnccount->{$2} += $1;
1773 /^BRDA:(\d+),(\d+),(\d+),(\d+|-)/ && do {
1774 # Branch coverage data found
1775 my ($line, $block, $branch, $taken) =
1778 last if (!$br_coverage);
1779 $sumbrcount->{$line} =
1780 br_ivec_push($sumbrcount->{$line},
1781 $block, $branch, $taken);
1783 # Add test-specific counts
1784 if (defined($testname)) {
1785 $testbrcount->{$line} =
1787 $testbrcount->{$line},
1794 /^end_of_record/ && do
1796 # Found end of section marker
1799 # Store current section data
1800 if (defined($testname))
1802 $testdata->{$testname} =
1804 $testfncdata->{$testname} =
1806 $testbrdata->{$testname} =
1810 set_info_entry($data, $testdata,
1811 $sumcount, $funcdata,
1812 $checkdata, $testfncdata,
1816 $result{$filename} = $data;
1827 # Calculate hit and found values for lines and functions of each file
1828 foreach $filename (keys(%result))
1830 $data = $result{$filename};
1832 ($testdata, $sumcount, undef, undef, $testfncdata,
1833 $sumfnccount, $testbrdata, $sumbrcount) =
1834 get_info_entry($data);
1836 # Filter out empty files
1837 if (scalar(keys(%{$sumcount})) == 0)
1839 delete($result{$filename});
1842 # Filter out empty test cases
1843 foreach $testname (keys(%{$testdata}))
1845 if (!defined($testdata->{$testname}) ||
1846 scalar(keys(%{$testdata->{$testname}})) == 0)
1848 delete($testdata->{$testname});
1849 delete($testfncdata->{$testname});
1853 $data->{"found"} = scalar(keys(%{$sumcount}));
1856 foreach (keys(%{$sumcount}))
1858 if ($sumcount->{$_} > 0) { $hitcount++; }
1861 $data->{"hit"} = $hitcount;
1863 # Get found/hit values for function call data
1864 $data->{"f_found"} = scalar(keys(%{$sumfnccount}));
1867 foreach (keys(%{$sumfnccount})) {
1868 if ($sumfnccount->{$_} > 0) {
1872 $data->{"f_hit"} = $hitcount;
1874 # Get found/hit values for branch data
1876 my ($br_found, $br_hit) = get_br_found_and_hit($sumbrcount);
1878 $data->{"b_found"} = $br_found;
1879 $data->{"b_hit"} = $br_hit;
1883 if (scalar(keys(%result)) == 0)
1885 die("ERROR: no valid records found in tracefile $tracefile\n");
1889 warn("WARNING: negative counts found in tracefile ".
1892 if ($changed_testname)
1894 warn("WARNING: invalid characters removed from testname in ".
1895 "tracefile $tracefile\n");
1903 # get_info_entry(hash_ref)
1905 # Retrieve data from an entry of the structure generated by read_info_file().
1906 # Return a list of references to hashes:
1907 # (test data hash ref, sum count hash ref, funcdata hash ref, checkdata hash
1908 # ref, testfncdata hash ref, sumfnccount hash ref, testbrdata hash ref,
1909 # sumbrcount hash ref, lines found, lines hit, functions found,
1910 # functions hit, branches found, branches hit)
1913 sub get_info_entry($)
1915 my $testdata_ref = $_[0]->{"test"};
1916 my $sumcount_ref = $_[0]->{"sum"};
1917 my $funcdata_ref = $_[0]->{"func"};
1918 my $checkdata_ref = $_[0]->{"check"};
1919 my $testfncdata = $_[0]->{"testfnc"};
1920 my $sumfnccount = $_[0]->{"sumfnc"};
1921 my $testbrdata = $_[0]->{"testbr"};
1922 my $sumbrcount = $_[0]->{"sumbr"};
1923 my $lines_found = $_[0]->{"found"};
1924 my $lines_hit = $_[0]->{"hit"};
1925 my $f_found = $_[0]->{"f_found"};
1926 my $f_hit = $_[0]->{"f_hit"};
1927 my $br_found = $_[0]->{"b_found"};
1928 my $br_hit = $_[0]->{"b_hit"};
1930 return ($testdata_ref, $sumcount_ref, $funcdata_ref, $checkdata_ref,
1931 $testfncdata, $sumfnccount, $testbrdata, $sumbrcount,
1932 $lines_found, $lines_hit, $f_found, $f_hit,
1933 $br_found, $br_hit);
1938 # set_info_entry(hash_ref, testdata_ref, sumcount_ref, funcdata_ref,
1939 # checkdata_ref, testfncdata_ref, sumfcncount_ref,
1940 # testbrdata_ref, sumbrcount_ref[,lines_found,
1941 # lines_hit, f_found, f_hit, $b_found, $b_hit])
1943 # Update the hash referenced by HASH_REF with the provided data references.
1946 sub set_info_entry($$$$$$$$$;$$$$$$)
1948 my $data_ref = $_[0];
1950 $data_ref->{"test"} = $_[1];
1951 $data_ref->{"sum"} = $_[2];
1952 $data_ref->{"func"} = $_[3];
1953 $data_ref->{"check"} = $_[4];
1954 $data_ref->{"testfnc"} = $_[5];
1955 $data_ref->{"sumfnc"} = $_[6];
1956 $data_ref->{"testbr"} = $_[7];
1957 $data_ref->{"sumbr"} = $_[8];
1959 if (defined($_[9])) { $data_ref->{"found"} = $_[9]; }
1960 if (defined($_[10])) { $data_ref->{"hit"} = $_[10]; }
1961 if (defined($_[11])) { $data_ref->{"f_found"} = $_[11]; }
1962 if (defined($_[12])) { $data_ref->{"f_hit"} = $_[12]; }
1963 if (defined($_[13])) { $data_ref->{"b_found"} = $_[13]; }
1964 if (defined($_[14])) { $data_ref->{"b_hit"} = $_[14]; }
1969 # add_counts(data1_ref, data2_ref)
1971 # DATA1_REF and DATA2_REF are references to hashes containing a mapping
1973 # line number -> execution count
1975 # Return a list (RESULT_REF, LINES_FOUND, LINES_HIT) where RESULT_REF
1976 # is a reference to a hash containing the combined mapping in which
1977 # execution counts are added.
1982 my %data1 = %{$_[0]}; # Hash 1
1983 my %data2 = %{$_[1]}; # Hash 2
1984 my %result; # Resulting hash
1985 my $line; # Current line iteration scalar
1986 my $data1_count; # Count of line in hash1
1987 my $data2_count; # Count of line in hash2
1988 my $found = 0; # Total number of lines found
1989 my $hit = 0; # Number of lines with a count > 0
1991 foreach $line (keys(%data1))
1993 $data1_count = $data1{$line};
1994 $data2_count = $data2{$line};
1996 # Add counts if present in both hashes
1997 if (defined($data2_count)) { $data1_count += $data2_count; }
1999 # Store sum in %result
2000 $result{$line} = $data1_count;
2003 if ($data1_count > 0) { $hit++; }
2006 # Add lines unique to data2
2007 foreach $line (keys(%data2))
2009 # Skip lines already in data1
2010 if (defined($data1{$line})) { next; }
2012 # Copy count from data2
2013 $result{$line} = $data2{$line};
2016 if ($result{$line} > 0) { $hit++; }
2019 return (\%result, $found, $hit);
2024 # merge_checksums(ref1, ref2, filename)
2026 # REF1 and REF2 are references to hashes containing a mapping
2028 # line number -> checksum
2030 # Merge checksum lists defined in REF1 and REF2 and return reference to
2031 # resulting hash. Die if a checksum for a line is defined in both hashes
2032 # but does not match.
2035 sub merge_checksums($$$)
2039 my $filename = $_[2];
2043 foreach $line (keys(%{$ref1}))
2045 if (defined($ref2->{$line}) &&
2046 ($ref1->{$line} ne $ref2->{$line}))
2048 die("ERROR: checksum mismatch at $filename:$line\n");
2050 $result{$line} = $ref1->{$line};
2053 foreach $line (keys(%{$ref2}))
2055 $result{$line} = $ref2->{$line};
2063 # merge_func_data(funcdata1, funcdata2, filename)
2066 sub merge_func_data($$$)
2068 my ($funcdata1, $funcdata2, $filename) = @_;
2072 if (defined($funcdata1)) {
2073 %result = %{$funcdata1};
2076 foreach $func (keys(%{$funcdata2})) {
2077 my $line1 = $result{$func};
2078 my $line2 = $funcdata2->{$func};
2080 if (defined($line1) && ($line1 != $line2)) {
2081 warn("WARNING: function data mismatch at ".
2082 "$filename:$line2\n");
2085 $result{$func} = $line2;
2093 # add_fnccount(fnccount1, fnccount2)
2095 # Add function call count data. Return list (fnccount_added, f_found, f_hit)
2098 sub add_fnccount($$)
2100 my ($fnccount1, $fnccount2) = @_;
2106 if (defined($fnccount1)) {
2107 %result = %{$fnccount1};
2109 foreach $function (keys(%{$fnccount2})) {
2110 $result{$function} += $fnccount2->{$function};
2112 $f_found = scalar(keys(%result));
2114 foreach $function (keys(%result)) {
2115 if ($result{$function} > 0) {
2120 return (\%result, $f_found, $f_hit);
2124 # add_testfncdata(testfncdata1, testfncdata2)
2126 # Add function call count data for several tests. Return reference to
2127 # added_testfncdata.
2130 sub add_testfncdata($$)
2132 my ($testfncdata1, $testfncdata2) = @_;
2136 foreach $testname (keys(%{$testfncdata1})) {
2137 if (defined($testfncdata2->{$testname})) {
2140 # Function call count data for this testname exists
2141 # in both data sets: merge
2142 ($fnccount) = add_fnccount(
2143 $testfncdata1->{$testname},
2144 $testfncdata2->{$testname});
2145 $result{$testname} = $fnccount;
2148 # Function call count data for this testname is unique to
2150 $result{$testname} = $testfncdata1->{$testname};
2153 # Add count data for testnames unique to data set 2
2154 foreach $testname (keys(%{$testfncdata2})) {
2155 if (!defined($result{$testname})) {
2156 $result{$testname} = $testfncdata2->{$testname};
2164 # brcount_to_db(brcount)
2166 # Convert brcount data to the following format:
2168 # db: line number -> block hash
2169 # block hash: block number -> branch hash
2170 # branch hash: branch number -> taken value
2173 sub brcount_to_db($)
2179 # Add branches from first count to database
2180 foreach $line (keys(%{$brcount})) {
2181 my $brdata = $brcount->{$line};
2183 my $num = br_ivec_len($brdata);
2185 for ($i = 0; $i < $num; $i++) {
2186 my ($block, $branch, $taken) = br_ivec_get($brdata, $i);
2188 $db->{$line}->{$block}->{$branch} = $taken;
2199 # Convert branch coverage data back to brcount format.
2202 sub db_to_brcount($)
2210 # Convert database back to brcount format
2211 foreach $line (sort({$a <=> $b} keys(%{$db}))) {
2212 my $ldata = $db->{$line};
2216 foreach $block (sort({$a <=> $b} keys(%{$ldata}))) {
2217 my $bdata = $ldata->{$block};
2220 foreach $branch (sort({$a <=> $b} keys(%{$bdata}))) {
2221 my $taken = $bdata->{$branch};
2224 $br_hit++ if ($taken ne "-" && $taken > 0);
2225 $brdata = br_ivec_push($brdata, $block,
2229 $brcount->{$line} = $brdata;
2232 return ($brcount, $br_found, $br_hit);
2236 # combine_brcount(brcount1, brcount2, type)
2238 # If add is BR_ADD, add branch coverage data and return list (brcount_added,
2239 # br_found, br_hit). If add is BR_SUB, subtract the taken values of brcount2
2240 # from brcount1 and return (brcount_sub, br_found, br_hit).
2243 sub combine_brcount($$$)
2245 my ($brcount1, $brcount2, $type) = @_;
2255 # Convert branches from first count to database
2256 $db = brcount_to_db($brcount1);
2257 # Combine values from database and second count
2258 foreach $line (keys(%{$brcount2})) {
2259 my $brdata = $brcount2->{$line};
2260 my $num = br_ivec_len($brdata);
2263 for ($i = 0; $i < $num; $i++) {
2264 ($block, $branch, $taken) = br_ivec_get($brdata, $i);
2265 my $new_taken = $db->{$line}->{$block}->{$branch};
2267 if ($type == $BR_ADD) {
2268 $new_taken = br_taken_add($new_taken, $taken);
2269 } elsif ($type == $BR_SUB) {
2270 $new_taken = br_taken_sub($new_taken, $taken);
2272 $db->{$line}->{$block}->{$branch} = $new_taken
2273 if (defined($new_taken));
2276 # Convert database back to brcount format
2277 ($result, $br_found, $br_hit) = db_to_brcount($db);
2279 return ($result, $br_found, $br_hit);
2284 # add_testbrdata(testbrdata1, testbrdata2)
2286 # Add branch coverage data for several tests. Return reference to
2290 sub add_testbrdata($$)
2292 my ($testbrdata1, $testbrdata2) = @_;
2296 foreach $testname (keys(%{$testbrdata1})) {
2297 if (defined($testbrdata2->{$testname})) {
2300 # Branch coverage data for this testname exists
2301 # in both data sets: add
2302 ($brcount) = combine_brcount(
2303 $testbrdata1->{$testname},
2304 $testbrdata2->{$testname}, $BR_ADD);
2305 $result{$testname} = $brcount;
2308 # Branch coverage data for this testname is unique to
2310 $result{$testname} = $testbrdata1->{$testname};
2313 # Add count data for testnames unique to data set 2
2314 foreach $testname (keys(%{$testbrdata2})) {
2315 if (!defined($result{$testname})) {
2316 $result{$testname} = $testbrdata2->{$testname};
2324 # combine_info_entries(entry_ref1, entry_ref2, filename)
2326 # Combine .info data entry hashes referenced by ENTRY_REF1 and ENTRY_REF2.
2327 # Return reference to resulting hash.
2330 sub combine_info_entries($$$)
2332 my $entry1 = $_[0]; # Reference to hash containing first entry
2342 my $entry2 = $_[1]; # Reference to hash containing second entry
2352 my %result; # Hash containing combined entry
2353 my %result_testdata;
2354 my $result_sumcount = {};
2355 my $result_funcdata;
2356 my $result_testfncdata;
2357 my $result_sumfnccount;
2358 my $result_testbrdata;
2359 my $result_sumbrcount;
2368 my $filename = $_[2];
2371 ($testdata1, $sumcount1, $funcdata1, $checkdata1, $testfncdata1,
2372 $sumfnccount1, $testbrdata1, $sumbrcount1) = get_info_entry($entry1);
2373 ($testdata2, $sumcount2, $funcdata2, $checkdata2, $testfncdata2,
2374 $sumfnccount2, $testbrdata2, $sumbrcount2) = get_info_entry($entry2);
2377 $checkdata1 = merge_checksums($checkdata1, $checkdata2, $filename);
2380 $result_funcdata = merge_func_data($funcdata1, $funcdata2, $filename);
2382 # Combine function call count data
2383 $result_testfncdata = add_testfncdata($testfncdata1, $testfncdata2);
2384 ($result_sumfnccount, $f_found, $f_hit) =
2385 add_fnccount($sumfnccount1, $sumfnccount2);
2387 # Combine branch coverage data
2388 $result_testbrdata = add_testbrdata($testbrdata1, $testbrdata2);
2389 ($result_sumbrcount, $br_found, $br_hit) =
2390 combine_brcount($sumbrcount1, $sumbrcount2, $BR_ADD);
2393 foreach $testname (keys(%{$testdata1}))
2395 if (defined($testdata2->{$testname}))
2397 # testname is present in both entries, requires
2399 ($result_testdata{$testname}) =
2400 add_counts($testdata1->{$testname},
2401 $testdata2->{$testname});
2405 # testname only present in entry1, add to result
2406 $result_testdata{$testname} = $testdata1->{$testname};
2409 # update sum count hash
2410 ($result_sumcount, $lines_found, $lines_hit) =
2411 add_counts($result_sumcount,
2412 $result_testdata{$testname});
2415 foreach $testname (keys(%{$testdata2}))
2417 # Skip testnames already covered by previous iteration
2418 if (defined($testdata1->{$testname})) { next; }
2420 # testname only present in entry2, add to result hash
2421 $result_testdata{$testname} = $testdata2->{$testname};
2423 # update sum count hash
2424 ($result_sumcount, $lines_found, $lines_hit) =
2425 add_counts($result_sumcount,
2426 $result_testdata{$testname});
2429 # Calculate resulting sumcount
2432 set_info_entry(\%result, \%result_testdata, $result_sumcount,
2433 $result_funcdata, $checkdata1, $result_testfncdata,
2434 $result_sumfnccount, $result_testbrdata,
2435 $result_sumbrcount, $lines_found, $lines_hit,
2436 $f_found, $f_hit, $br_found, $br_hit);
2443 # combine_info_files(info_ref1, info_ref2)
2445 # Combine .info data in hashes referenced by INFO_REF1 and INFO_REF2. Return
2446 # reference to resulting hash.
2449 sub combine_info_files($$)
2451 my %hash1 = %{$_[0]};
2452 my %hash2 = %{$_[1]};
2455 foreach $filename (keys(%hash2))
2457 if ($hash1{$filename})
2459 # Entry already exists in hash1, combine them
2461 combine_info_entries($hash1{$filename},
2467 # Entry is unique in both hashes, simply add to
2469 $hash1{$filename} = $hash2{$filename};
2489 info("Combining tracefiles.\n");
2491 foreach $tracefile (@add_tracefile)
2493 $current_trace = read_info_file($tracefile);
2496 $total_trace = combine_info_files($total_trace,
2501 $total_trace = $current_trace;
2505 # Write combined data
2508 info("Writing data to $output_filename\n");
2509 open(INFO_HANDLE, ">", $output_filename)
2510 or die("ERROR: cannot write to $output_filename!\n");
2511 @result = write_info_file(*INFO_HANDLE, $total_trace);
2512 close(*INFO_HANDLE);
2516 @result = write_info_file(*STDOUT, $total_trace);
2524 # write_info_file(filehandle, data)
2527 sub write_info_file(*$)
2529 local *INFO_HANDLE = $_[0];
2530 my %data = %{$_[1]};
2553 my $ln_total_found = 0;
2554 my $ln_total_hit = 0;
2555 my $fn_total_found = 0;
2556 my $fn_total_hit = 0;
2557 my $br_total_found = 0;
2558 my $br_total_hit = 0;
2560 foreach $source_file (sort(keys(%data)))
2562 $entry = $data{$source_file};
2563 ($testdata, $sumcount, $funcdata, $checkdata, $testfncdata,
2564 $sumfnccount, $testbrdata, $sumbrcount, $found, $hit,
2565 $f_found, $f_hit, $br_found, $br_hit) =
2566 get_info_entry($entry);
2569 $ln_total_found += $found;
2570 $ln_total_hit += $hit;
2571 $fn_total_found += $f_found;
2572 $fn_total_hit += $f_hit;
2573 $br_total_found += $br_found;
2574 $br_total_hit += $br_hit;
2576 foreach $testname (sort(keys(%{$testdata})))
2578 $testcount = $testdata->{$testname};
2579 $testfnccount = $testfncdata->{$testname};
2580 $testbrcount = $testbrdata->{$testname};
2584 print(INFO_HANDLE "TN:$testname\n");
2585 print(INFO_HANDLE "SF:$source_file\n");
2587 # Write function related data
2589 sort({$funcdata->{$a} <=> $funcdata->{$b}}
2590 keys(%{$funcdata})))
2592 print(INFO_HANDLE "FN:".$funcdata->{$func}.
2595 foreach $func (keys(%{$testfnccount})) {
2596 print(INFO_HANDLE "FNDA:".
2597 $testfnccount->{$func}.
2600 ($f_found, $f_hit) =
2601 get_func_found_and_hit($testfnccount);
2602 print(INFO_HANDLE "FNF:$f_found\n");
2603 print(INFO_HANDLE "FNH:$f_hit\n");
2605 # Write branch related data
2608 foreach $line (sort({$a <=> $b}
2609 keys(%{$testbrcount}))) {
2610 my $brdata = $testbrcount->{$line};
2611 my $num = br_ivec_len($brdata);
2614 for ($i = 0; $i < $num; $i++) {
2615 my ($block, $branch, $taken) =
2616 br_ivec_get($brdata, $i);
2618 print(INFO_HANDLE "BRDA:$line,$block,".
2619 "$branch,$taken\n");
2621 $br_hit++ if ($taken ne '-' &&
2625 if ($br_found > 0) {
2626 print(INFO_HANDLE "BRF:$br_found\n");
2627 print(INFO_HANDLE "BRH:$br_hit\n");
2630 # Write line related data
2631 foreach $line (sort({$a <=> $b} keys(%{$testcount})))
2633 print(INFO_HANDLE "DA:$line,".
2634 $testcount->{$line}.
2635 (defined($checkdata->{$line}) &&
2637 ",".$checkdata->{$line} : "")."\n");
2639 if ($testcount->{$line} > 0)
2645 print(INFO_HANDLE "LF:$found\n");
2646 print(INFO_HANDLE "LH:$hit\n");
2647 print(INFO_HANDLE "end_of_record\n");
2651 return ($ln_total_found, $ln_total_hit, $fn_total_found, $fn_total_hit,
2652 $br_total_found, $br_total_hit);
2657 # transform_pattern(pattern)
2659 # Transform shell wildcard expression to equivalent Perl regular expression.
2660 # Return transformed pattern.
2663 sub transform_pattern($)
2665 my $pattern = $_[0];
2667 # Escape special chars
2669 $pattern =~ s/\\/\\\\/g;
2670 $pattern =~ s/\//\\\//g;
2671 $pattern =~ s/\^/\\\^/g;
2672 $pattern =~ s/\$/\\\$/g;
2673 $pattern =~ s/\(/\\\(/g;
2674 $pattern =~ s/\)/\\\)/g;
2675 $pattern =~ s/\[/\\\[/g;
2676 $pattern =~ s/\]/\\\]/g;
2677 $pattern =~ s/\{/\\\{/g;
2678 $pattern =~ s/\}/\\\}/g;
2679 $pattern =~ s/\./\\\./g;
2680 $pattern =~ s/\,/\\\,/g;
2681 $pattern =~ s/\|/\\\|/g;
2682 $pattern =~ s/\+/\\\+/g;
2683 $pattern =~ s/\!/\\\!/g;
2685 # Transform ? => (.) and * => (.*)
2687 $pattern =~ s/\*/\(\.\*\)/g;
2688 $pattern =~ s/\?/\(\.\)/g;
2700 my $data = read_info_file($extract);
2709 # Need perlreg expressions instead of shell pattern
2710 @pattern_list = map({ transform_pattern($_); } @ARGV);
2712 # Filter out files which do not match any pattern
2713 foreach $filename (sort(keys(%{$data})))
2717 foreach $pattern (@pattern_list)
2719 $keep ||= ($filename =~ (/^$pattern$/));
2725 delete($data->{$filename});
2729 info("Extracting $filename\n"),
2734 # Write extracted data
2737 info("Extracted $extracted files\n");
2738 info("Writing data to $output_filename\n");
2739 open(INFO_HANDLE, ">", $output_filename)
2740 or die("ERROR: cannot write to $output_filename!\n");
2741 @result = write_info_file(*INFO_HANDLE, $data);
2742 close(*INFO_HANDLE);
2746 @result = write_info_file(*STDOUT, $data);
2759 my $data = read_info_file($remove);
2768 # Need perlreg expressions instead of shell pattern
2769 @pattern_list = map({ transform_pattern($_); } @ARGV);
2771 # Filter out files that match the pattern
2772 foreach $filename (sort(keys(%{$data})))
2776 foreach $pattern (@pattern_list)
2778 $match_found ||= ($filename =~ (/$pattern$/));
2784 delete($data->{$filename});
2785 info("Removing $filename\n"),
2793 info("Deleted $removed files\n");
2794 info("Writing data to $output_filename\n");
2795 open(INFO_HANDLE, ">", $output_filename)
2796 or die("ERROR: cannot write to $output_filename!\n");
2797 @result = write_info_file(*INFO_HANDLE, $data);
2798 close(*INFO_HANDLE);
2802 @result = write_info_file(*STDOUT, $data);
2809 # get_prefix(max_width, max_percentage_too_long, path_list)
2811 # Return a path prefix that satisfies the following requirements:
2812 # - is shared by more paths in path_list than any other prefix
2813 # - the percentage of paths which would exceed the given max_width length
2814 # after applying the prefix does not exceed max_percentage_too_long
2816 # If multiple prefixes satisfy all requirements, the longest prefix is
2817 # returned. Return an empty string if no prefix could be found.
2821 my ($max_width, $max_long, @path_list) = @_;
2828 foreach $path (@path_list) {
2829 my ($v, $d, $f) = splitpath($path);
2830 my @dirs = splitdir($d);
2831 my $p_len = length($path);
2834 # Remove trailing '/'
2835 pop(@dirs) if ($dirs[scalar(@dirs) - 1] eq '');
2836 for ($i = 0; $i < scalar(@dirs); $i++) {
2837 my $subpath = catpath($v, catdir(@dirs[0..$i]), '');
2838 my $entry = $prefix{$subpath};
2840 $entry = [ 0, 0 ] if (!defined($entry));
2841 $entry->[$ENTRY_NUM]++;
2842 if (($p_len - length($subpath) - 1) > $max_width) {
2843 $entry->[$ENTRY_LONG]++;
2845 $prefix{$subpath} = $entry;
2848 # Find suitable prefix (sort descending by two keys: 1. number of
2849 # entries covered by a prefix, 2. length of prefix)
2850 foreach $path (sort {($prefix{$a}->[$ENTRY_NUM] ==
2851 $prefix{$b}->[$ENTRY_NUM]) ?
2852 length($b) <=> length($a) :
2853 $prefix{$b}->[$ENTRY_NUM] <=>
2854 $prefix{$a}->[$ENTRY_NUM]}
2856 my ($num, $long) = @{$prefix{$path}};
2858 # Check for additional requirement: number of filenames
2859 # that would be too long may not exceed a certain percentage
2860 if ($long <= $num * $max_long / 100) {
2870 # shorten_filename(filename, width)
2872 # Truncate filename if it is longer than width characters.
2875 sub shorten_filename($$)
2877 my ($filename, $width) = @_;
2878 my $l = length($filename);
2882 return $filename if ($l <= $width);
2883 $e = int(($width - 3) / 2);
2884 $s = $width - 3 - $e;
2886 return substr($filename, 0, $s).'...'.substr($filename, $l - $e);
2890 sub shorten_number($$)
2892 my ($number, $width) = @_;
2893 my $result = sprintf("%*d", $width, $number);
2895 return $result if (length($result) <= $width);
2896 $number = $number / 1000;
2897 return $result if (length($result) <= $width);
2898 $result = sprintf("%*dk", $width - 1, $number);
2899 return $result if (length($result) <= $width);
2900 $number = $number / 1000;
2901 $result = sprintf("%*dM", $width - 1, $number);
2902 return $result if (length($result) <= $width);
2906 sub shorten_rate($$$)
2908 my ($hit, $found, $width) = @_;
2909 my $result = rate($hit, $found, "%", 1, $width);
2911 return $result if (length($result) <= $width);
2912 $result = rate($hit, $found, "%", 0, $width);
2913 return $result if (length($result) <= $width);
2923 my $data = read_info_file($list);
2932 my $total_found = 0;
2934 my $fn_total_found = 0;
2935 my $fn_total_hit = 0;
2936 my $br_total_found = 0;
2937 my $br_total_hit = 0;
2939 my $strlen = length("Filename");
2955 my @fwidth_narrow = (5, 5, 3, 5, 4, 5);
2956 my @fwidth_wide = (6, 5, 5, 5, 6, 5);
2957 my @fwidth = @fwidth_wide;
2959 my $max_width = $opt_list_width;
2960 my $max_long = $opt_list_truncate_max;
2961 my $fwidth_narrow_length;
2962 my $fwidth_wide_length;
2964 my $root_prefix = 0;
2966 # Calculate total width of narrow fields
2967 $fwidth_narrow_length = 0;
2968 foreach $w (@fwidth_narrow) {
2969 $fwidth_narrow_length += $w + 1;
2971 # Calculate total width of wide fields
2972 $fwidth_wide_length = 0;
2973 foreach $w (@fwidth_wide) {
2974 $fwidth_wide_length += $w + 1;
2976 # Get common file path prefix
2977 $prefix = get_prefix($max_width - $fwidth_narrow_length, $max_long,
2979 $root_prefix = 1 if ($prefix eq rootdir());
2980 $got_prefix = 1 if (length($prefix) > 0);
2982 # Get longest filename length
2983 foreach $filename (keys(%{$data})) {
2984 if (!$opt_list_full_path) {
2985 if (!$got_prefix || !$root_prefix &&
2986 !($filename =~ s/^\Q$prefix\/\E//)) {
2987 my ($v, $d, $f) = splitpath($filename);
2992 # Determine maximum length of entries
2993 if (length($filename) > $strlen) {
2994 $strlen = length($filename)
2997 if (!$opt_list_full_path) {
3000 $w = $fwidth_wide_length;
3001 # Check if all columns fit into max_width characters
3002 if ($strlen + $fwidth_wide_length > $max_width) {
3004 @fwidth = @fwidth_narrow;
3005 $w = $fwidth_narrow_length;
3006 if (($strlen + $fwidth_narrow_length) > $max_width) {
3007 # Truncate filenames at max width
3008 $strlen = $max_width - $fwidth_narrow_length;
3011 # Add some blanks between filename and fields if possible
3012 $blanks = int($strlen * 0.5);
3013 $blanks = 4 if ($blanks < 4);
3014 $blanks = 8 if ($blanks > 8);
3015 if (($strlen + $w + $blanks) < $max_width) {
3018 $strlen = $max_width - $w;
3023 $format = "%-${w}s|";
3024 $heading1 = sprintf("%*s|", $w, "");
3025 $heading2 = sprintf("%-*s|", $w, "Filename");
3027 # Line coverage rate
3028 $w = $fwidth[$F_LN_RATE];
3029 $format .= "%${w}s ";
3030 $heading1 .= sprintf("%-*s |", $w + $fwidth[$F_LN_NUM],
3032 $heading2 .= sprintf("%-*s ", $w, "Rate");
3035 $w = $fwidth[$F_LN_NUM];
3036 $format .= "%${w}s|";
3037 $heading2 .= sprintf("%*s|", $w, "Num");
3039 # Function coverage rate
3040 $w = $fwidth[$F_FN_RATE];
3041 $format .= "%${w}s ";
3042 $heading1 .= sprintf("%-*s|", $w + $fwidth[$F_FN_NUM] + 1,
3044 $heading2 .= sprintf("%-*s ", $w, "Rate");
3046 # Number of functions
3047 $w = $fwidth[$F_FN_NUM];
3048 $format .= "%${w}s|";
3049 $heading2 .= sprintf("%*s|", $w, "Num");
3051 # Branch coverage rate
3052 $w = $fwidth[$F_BR_RATE];
3053 $format .= "%${w}s ";
3054 $heading1 .= sprintf("%-*s", $w + $fwidth[$F_BR_NUM] + 1,
3056 $heading2 .= sprintf("%-*s ", $w, "Rate");
3058 # Number of branches
3059 $w = $fwidth[$F_BR_NUM];
3060 $format .= "%${w}s";
3061 $heading2 .= sprintf("%*s", $w, "Num");
3071 print(("="x$barlen)."\n");
3073 # Print per file information
3074 foreach $filename (sort(keys(%{$data})))
3077 my $print_filename = $filename;
3079 $entry = $data->{$filename};
3080 if (!$opt_list_full_path) {
3083 $print_filename = $filename;
3084 if (!$got_prefix || !$root_prefix &&
3085 !($print_filename =~ s/^\Q$prefix\/\E//)) {
3086 my ($v, $d, $f) = splitpath($filename);
3088 $p = catpath($v, $d, "");
3090 $print_filename = $f;
3095 if (!defined($lastpath) || $lastpath ne $p) {
3096 print("\n") if (defined($lastpath));
3098 print("[$lastpath/]\n") if (!$root_prefix);
3100 $print_filename = shorten_filename($print_filename,
3104 (undef, undef, undef, undef, undef, undef, undef, undef,
3105 $found, $hit, $fn_found, $fn_hit, $br_found, $br_hit) =
3106 get_info_entry($entry);
3108 # Assume zero count if there is no function data for this file
3109 if (!defined($fn_found) || !defined($fn_hit)) {
3113 # Assume zero count if there is no branch data for this file
3114 if (!defined($br_found) || !defined($br_hit)) {
3119 # Add line coverage totals
3120 $total_found += $found;
3122 # Add function coverage totals
3123 $fn_total_found += $fn_found;
3124 $fn_total_hit += $fn_hit;
3125 # Add branch coverage totals
3126 $br_total_found += $br_found;
3127 $br_total_hit += $br_hit;
3129 # Determine line coverage rate for this file
3130 $rate = shorten_rate($hit, $found, $fwidth[$F_LN_RATE]);
3131 # Determine function coverage rate for this file
3132 $fnrate = shorten_rate($fn_hit, $fn_found, $fwidth[$F_FN_RATE]);
3133 # Determine branch coverage rate for this file
3134 $brrate = shorten_rate($br_hit, $br_found, $fwidth[$F_BR_RATE]);
3136 # Assemble line parameters
3137 push(@file_data, $print_filename);
3138 push(@file_data, $rate);
3139 push(@file_data, shorten_number($found, $fwidth[$F_LN_NUM]));
3140 push(@file_data, $fnrate);
3141 push(@file_data, shorten_number($fn_found, $fwidth[$F_FN_NUM]));
3142 push(@file_data, $brrate);
3143 push(@file_data, shorten_number($br_found, $fwidth[$F_BR_NUM]));
3145 # Print assembled line
3146 printf($format, @file_data);
3149 # Determine total line coverage rate
3150 $rate = shorten_rate($total_hit, $total_found, $fwidth[$F_LN_RATE]);
3151 # Determine total function coverage rate
3152 $fnrate = shorten_rate($fn_total_hit, $fn_total_found,
3153 $fwidth[$F_FN_RATE]);
3154 # Determine total branch coverage rate
3155 $brrate = shorten_rate($br_total_hit, $br_total_found,
3156 $fwidth[$F_BR_RATE]);
3159 print(("="x$barlen)."\n");
3161 # Assemble line parameters
3162 push(@footer, sprintf("%*s", $strlen, "Total:"));
3163 push(@footer, $rate);
3164 push(@footer, shorten_number($total_found, $fwidth[$F_LN_NUM]));
3165 push(@footer, $fnrate);
3166 push(@footer, shorten_number($fn_total_found, $fwidth[$F_FN_NUM]));
3167 push(@footer, $brrate);
3168 push(@footer, shorten_number($br_total_found, $fwidth[$F_BR_NUM]));
3170 # Print assembled line
3171 printf($format, @footer);
3176 # get_common_filename(filename1, filename2)
3178 # Check for filename components which are common to FILENAME1 and FILENAME2.
3179 # Upon success, return
3181 # (common, path1, path2)
3183 # or 'undef' in case there are no such parts.
3186 sub get_common_filename($$)
3188 my @list1 = split("/", $_[0]);
3189 my @list2 = split("/", $_[1]);
3192 # Work in reverse order, i.e. beginning with the filename itself
3193 while (@list1 && @list2 && ($list1[$#list1] eq $list2[$#list2]))
3195 unshift(@result, pop(@list1));
3199 # Did we find any similarities?
3200 if (scalar(@result) > 0)
3202 return (join("/", @result), join("/", @list1),
3213 # strip_directories($path, $depth)
3215 # Remove DEPTH leading directory levels from PATH.
3218 sub strip_directories($$)
3220 my $filename = $_[0];
3224 if (!defined($depth) || ($depth < 1))
3228 for ($i = 0; $i < $depth; $i++)
3230 $filename =~ s/^[^\/]*\/+(.*)$/$1/;
3237 # read_diff(filename)
3239 # Read diff output from FILENAME to memory. The diff file has to follow the
3240 # format generated by 'diff -u'. Returns a list of hash references:
3242 # (mapping, path mapping)
3244 # mapping: filename -> reference to line hash
3245 # line hash: line number in new file -> corresponding line number in old file
3247 # path mapping: filename -> old filename
3249 # Die in case of error.
3254 my $diff_file = $_[0]; # Name of diff file
3255 my %diff; # Resulting mapping filename -> line hash
3256 my %paths; # Resulting mapping old path -> new path
3257 my $mapping; # Reference to current line hash
3258 my $line; # Contents of current line
3259 my $num_old; # Current line number in old file
3260 my $num_new; # Current line number in new file
3261 my $file_old; # Name of old file in diff section
3262 my $file_new; # Name of new file in diff section
3263 my $filename; # Name of common filename of diff section
3264 my $in_block = 0; # Non-zero while we are inside a diff block
3265 local *HANDLE; # File handle for reading the diff file
3267 info("Reading diff $diff_file\n");
3269 # Check if file exists and is readable
3273 die("ERROR: cannot read file $diff_file!\n");
3276 # Check if this is really a plain file
3279 die("ERROR: not a plain file: $diff_file!\n");
3282 # Check for .gz extension
3283 if ($diff_file =~ /\.gz$/)
3285 # Check for availability of GZIP tool
3286 system_no_output(1, "gunzip", "-h")
3287 and die("ERROR: gunzip command not available!\n");
3289 # Check integrity of compressed file
3290 system_no_output(1, "gunzip", "-t", $diff_file)
3291 and die("ERROR: integrity check failed for ".
3292 "compressed file $diff_file!\n");
3294 # Open compressed file
3295 open(HANDLE, "-|", "gunzip -c '$diff_file'")
3296 or die("ERROR: cannot start gunzip to decompress ".
3301 # Open decompressed file
3302 open(HANDLE, "<", $diff_file)
3303 or die("ERROR: cannot read file $_[0]!\n");
3306 # Parse diff file line by line
3314 # Filename of old file:
3315 # --- <filename> <date>
3318 $file_old = strip_directories($1, $strip);
3321 # Filename of new file:
3322 # +++ <filename> <date>
3323 /^\+\+\+ (\S+)/ && do
3325 # Add last file to resulting hash
3329 $diff{$filename} = $mapping;
3330 $mapping = \%new_hash;
3332 $file_new = strip_directories($1, $strip);
3333 $filename = $file_old;
3334 $paths{$filename} = $file_new;
3339 # Start of diff block:
3340 # @@ -old_start,old_num, +new_start,new_num @@
3341 /^\@\@\s+-(\d+),(\d+)\s+\+(\d+),(\d+)\s+\@\@$/ && do
3344 while ($num_old < $1)
3346 $mapping->{$num_new} = $num_old;
3353 # <line starts with blank>
3360 $mapping->{$num_new} = $num_old;
3365 # Line as seen in old file
3366 # <line starts with '-'>
3376 # Line as seen in new file
3377 # <line starts with '+'>
3394 $mapping->{$num_new} = $num_old;
3404 # Add final diff file section to resulting hash
3407 $diff{$filename} = $mapping;
3412 die("ERROR: no valid diff data found in $diff_file!\n".
3413 "Make sure to use 'diff -u' when generating the diff ".
3416 return (\%diff, \%paths);
3421 # apply_diff($count_data, $line_hash)
3423 # Transform count data using a mapping of lines:
3425 # $count_data: reference to hash: line number -> data
3426 # $line_hash: reference to hash: line number new -> line number old
3428 # Return a reference to transformed count data.
3433 my $count_data = $_[0]; # Reference to data hash: line -> hash
3434 my $line_hash = $_[1]; # Reference to line hash: new line -> old line
3435 my %result; # Resulting hash
3436 my $last_new = 0; # Last new line number found in line hash
3437 my $last_old = 0; # Last old line number found in line hash
3439 # Iterate all new line numbers found in the diff
3440 foreach (sort({$a <=> $b} keys(%{$line_hash})))
3443 $last_old = $line_hash->{$last_new};
3445 # Is there data associated with the corresponding old line?
3446 if (defined($count_data->{$line_hash->{$_}}))
3448 # Copy data to new hash with a new line number
3449 $result{$_} = $count_data->{$line_hash->{$_}};
3452 # Transform all other lines which come after the last diff entry
3453 foreach (sort({$a <=> $b} keys(%{$count_data})))
3455 if ($_ <= $last_old)
3457 # Skip lines which were covered by line hash
3460 # Copy data to new hash with an offset
3461 $result{$_ + ($last_new - $last_old)} = $count_data->{$_};
3469 # apply_diff_to_brcount(brcount, linedata)
3471 # Adjust line numbers of branch coverage data according to linedata.
3474 sub apply_diff_to_brcount($$)
3476 my ($brcount, $linedata) = @_;
3479 # Convert brcount to db format
3480 $db = brcount_to_db($brcount);
3481 # Apply diff to db format
3482 $db = apply_diff($db, $linedata);
3483 # Convert db format back to brcount format
3484 ($brcount) = db_to_brcount($db);
3491 # get_hash_max(hash_ref)
3493 # Return the highest integer key from hash.
3501 foreach (keys(%{$hash})) {
3502 if (!defined($max)) {
3504 } elsif ($hash->{$_} > $max) {
3511 sub get_hash_reverse($)
3516 foreach (keys(%{$hash})) {
3517 $result{$hash->{$_}} = $_;
3524 # apply_diff_to_funcdata(funcdata, line_hash)
3527 sub apply_diff_to_funcdata($$)
3529 my ($funcdata, $linedata) = @_;
3530 my $last_new = get_hash_max($linedata);
3531 my $last_old = $linedata->{$last_new};
3534 my $line_diff = get_hash_reverse($linedata);
3536 foreach $func (keys(%{$funcdata})) {
3537 my $line = $funcdata->{$func};
3539 if (defined($line_diff->{$line})) {
3540 $result{$func} = $line_diff->{$line};
3541 } elsif ($line > $last_old) {
3542 $result{$func} = $line + $last_new - $last_old;
3551 # get_line_hash($filename, $diff_data, $path_data)
3553 # Find line hash in DIFF_DATA which matches FILENAME. On success, return list
3554 # line hash. or undef in case of no match. Die if more than one line hashes in
3558 sub get_line_hash($$$)
3560 my $filename = $_[0];
3561 my $diff_data = $_[1];
3562 my $path_data = $_[2];
3571 # Remove trailing slash from diff path
3572 $diff_path =~ s/\/$//;
3573 foreach (keys(%{$diff_data}))
3577 $sep = '/' if (!/^\//);
3579 # Try to match diff filename with filename
3580 if ($filename =~ /^\Q$diff_path$sep$_\E$/)
3584 # Two files match, choose the more specific one
3585 # (the one with more path components)
3586 $old_depth = ($diff_name =~ tr/\///);
3587 $new_depth = (tr/\///);
3588 if ($old_depth == $new_depth)
3590 die("ERROR: diff file contains ".
3591 "ambiguous entries for ".
3594 elsif ($new_depth > $old_depth)
3607 # Get converted path
3608 if ($filename =~ /^(.*)$diff_name$/)
3610 ($common, $old_path, $new_path) =
3611 get_common_filename($filename,
3612 $1.$path_data->{$diff_name});
3614 return ($diff_data->{$diff_name}, $old_path, $new_path);
3624 # convert_paths(trace_data, path_conversion_data)
3626 # Rename all paths in TRACE_DATA which show up in PATH_CONVERSION_DATA.
3629 sub convert_paths($$)
3631 my $trace_data = $_[0];
3632 my $path_conversion_data = $_[1];
3636 if (scalar(keys(%{$path_conversion_data})) == 0)
3638 info("No path conversion data available.\n");
3642 # Expand path conversion list
3643 foreach $filename (keys(%{$path_conversion_data}))
3645 $new_path = $path_conversion_data->{$filename};
3646 while (($filename =~ s/^(.*)\/[^\/]+$/$1/) &&
3647 ($new_path =~ s/^(.*)\/[^\/]+$/$1/) &&
3648 ($filename ne $new_path))
3650 $path_conversion_data->{$filename} = $new_path;
3655 FILENAME: foreach $filename (keys(%{$trace_data}))
3657 # Find a path in our conversion table that matches, starting
3658 # with the longest path
3659 foreach (sort({length($b) <=> length($a)}
3660 keys(%{$path_conversion_data})))
3662 # Is this path a prefix of our filename?
3663 if (!($filename =~ /^$_(.*)$/))
3667 $new_path = $path_conversion_data->{$_}.$1;
3669 # Make sure not to overwrite an existing entry under
3671 if ($trace_data->{$new_path})
3673 # Need to combine entries
3674 $trace_data->{$new_path} =
3675 combine_info_entries(
3676 $trace_data->{$filename},
3677 $trace_data->{$new_path},
3682 # Simply rename entry
3683 $trace_data->{$new_path} =
3684 $trace_data->{$filename};
3686 delete($trace_data->{$filename});
3689 info("No conversion available for filename $filename\n");
3694 # sub adjust_fncdata(funcdata, testfncdata, sumfnccount)
3696 # Remove function call count data from testfncdata and sumfnccount which
3697 # is no longer present in funcdata.
3700 sub adjust_fncdata($$$)
3702 my ($funcdata, $testfncdata, $sumfnccount) = @_;
3708 # Remove count data in testfncdata for functions which are no longer
3710 foreach $testname (%{$testfncdata}) {
3711 my $fnccount = $testfncdata->{$testname};
3713 foreach $func (%{$fnccount}) {
3714 if (!defined($funcdata->{$func})) {
3715 delete($fnccount->{$func});
3719 # Remove count data in sumfnccount for functions which are no longer
3721 foreach $func (%{$sumfnccount}) {
3722 if (!defined($funcdata->{$func})) {
3723 delete($sumfnccount->{$func});
3729 # get_func_found_and_hit(sumfnccount)
3731 # Return (f_found, f_hit) for sumfnccount
3734 sub get_func_found_and_hit($)
3736 my ($sumfnccount) = @_;
3741 $f_found = scalar(keys(%{$sumfnccount}));
3743 foreach $function (keys(%{$sumfnccount})) {
3744 if ($sumfnccount->{$function} > 0) {
3748 return ($f_found, $f_hit);
3757 my $trace_data = read_info_file($diff);
3762 my %path_conversion_data;
3787 ($diff_data, $path_data) = read_diff($ARGV[0]);
3789 foreach $filename (sort(keys(%{$trace_data})))
3791 # Find a diff section corresponding to this file
3792 ($line_hash, $old_path, $new_path) =
3793 get_line_hash($filename, $diff_data, $path_data);
3796 # There's no diff section for this file
3801 if ($old_path && $new_path && ($old_path ne $new_path))
3803 $path_conversion_data{$old_path} = $new_path;
3805 # Check for deleted files
3806 if (scalar(keys(%{$line_hash})) == 0)
3808 info("Removing $filename\n");
3809 delete($trace_data->{$filename});
3812 info("Converting $filename\n");
3813 $entry = $trace_data->{$filename};
3814 ($testdata, $sumcount, $funcdata, $checkdata, $testfncdata,
3815 $sumfnccount, $testbrdata, $sumbrcount) =
3816 get_info_entry($entry);
3818 foreach $testname (keys(%{$testdata}))
3820 # Adjust line numbers of line coverage data
3821 $testdata->{$testname} =
3822 apply_diff($testdata->{$testname}, $line_hash);
3823 # Adjust line numbers of branch coverage data
3824 $testbrdata->{$testname} =
3825 apply_diff_to_brcount($testbrdata->{$testname},
3827 # Remove empty sets of test data
3828 if (scalar(keys(%{$testdata->{$testname}})) == 0)
3830 delete($testdata->{$testname});
3831 delete($testfncdata->{$testname});
3832 delete($testbrdata->{$testname});
3835 # Rename test data to indicate conversion
3836 foreach $testname (keys(%{$testdata}))
3838 # Skip testnames which already contain an extension
3839 if ($testname =~ /,[^,]+$/)
3843 # Check for name conflict
3844 if (defined($testdata->{$testname.",diff"}))
3847 ($testdata->{$testname}) = add_counts(
3848 $testdata->{$testname},
3849 $testdata->{$testname.",diff"});
3850 delete($testdata->{$testname.",diff"});
3851 # Add function call counts
3852 ($testfncdata->{$testname}) = add_fnccount(
3853 $testfncdata->{$testname},
3854 $testfncdata->{$testname.",diff"});
3855 delete($testfncdata->{$testname.",diff"});
3857 ($testbrdata->{$testname}) = combine_brcount(
3858 $testbrdata->{$testname},
3859 $testbrdata->{$testname.",diff"},
3861 delete($testbrdata->{$testname.",diff"});
3863 # Move test data to new testname
3864 $testdata->{$testname.",diff"} = $testdata->{$testname};
3865 delete($testdata->{$testname});
3866 # Move function call count data to new testname
3867 $testfncdata->{$testname.",diff"} =
3868 $testfncdata->{$testname};
3869 delete($testfncdata->{$testname});
3870 # Move branch count data to new testname
3871 $testbrdata->{$testname.",diff"} =
3872 $testbrdata->{$testname};
3873 delete($testbrdata->{$testname});
3875 # Convert summary of test data
3876 $sumcount = apply_diff($sumcount, $line_hash);
3877 # Convert function data
3878 $funcdata = apply_diff_to_funcdata($funcdata, $line_hash);
3879 # Convert branch coverage data
3880 $sumbrcount = apply_diff_to_brcount($sumbrcount, $line_hash);
3881 # Update found/hit numbers
3882 # Convert checksum data
3883 $checkdata = apply_diff($checkdata, $line_hash);
3884 # Convert function call count data
3885 adjust_fncdata($funcdata, $testfncdata, $sumfnccount);
3886 ($f_found, $f_hit) = get_func_found_and_hit($sumfnccount);
3887 ($br_found, $br_hit) = get_br_found_and_hit($sumbrcount);
3888 # Update found/hit numbers
3891 foreach (keys(%{$sumcount}))
3894 if ($sumcount->{$_} > 0)
3901 # Store converted entry
3902 set_info_entry($entry, $testdata, $sumcount, $funcdata,
3903 $checkdata, $testfncdata, $sumfnccount,
3904 $testbrdata, $sumbrcount, $found, $hit,
3905 $f_found, $f_hit, $br_found, $br_hit);
3909 # Remove empty data set
3910 delete($trace_data->{$filename});
3914 # Convert filenames as well if requested
3915 if ($convert_filenames)
3917 convert_paths($trace_data, \%path_conversion_data);
3920 info("$converted entr".($converted != 1 ? "ies" : "y")." converted, ".
3921 "$unchanged entr".($unchanged != 1 ? "ies" : "y")." left ".
3927 info("Writing data to $output_filename\n");
3928 open(INFO_HANDLE, ">", $output_filename)
3929 or die("ERROR: cannot write to $output_filename!\n");
3930 @result = write_info_file(*INFO_HANDLE, $trace_data);
3931 close(*INFO_HANDLE);
3935 @result = write_info_file(*STDOUT, $trace_data);
3957 # Read and combine trace files
3958 foreach $filename (@opt_summary) {
3959 $current = read_info_file($filename);
3960 if (!defined($total)) {
3963 $total = combine_info_files($total, $current);
3966 # Calculate coverage data
3967 foreach $filename (keys(%{$total}))
3969 my $entry = $total->{$filename};
3977 (undef, undef, undef, undef, undef, undef, undef, undef,
3978 $ln_found, $ln_hit, $fn_found, $fn_hit, $br_found,
3979 $br_hit) = get_info_entry($entry);
3982 $ln_total_found += $ln_found;
3983 $ln_total_hit += $ln_hit;
3984 $fn_total_found += $fn_found;
3985 $fn_total_hit += $fn_hit;
3986 $br_total_found += $br_found;
3987 $br_total_hit += $br_hit;
3991 return ($ln_total_found, $ln_total_hit, $fn_total_found, $fn_total_hit,
3992 $br_total_found, $br_total_hit);
3996 # system_no_output(mode, parameters)
3998 # Call an external program using PARAMETERS while suppressing depending on
3999 # the value of MODE:
4001 # MODE & 1: suppress STDOUT
4002 # MODE & 2: suppress STDERR
4004 # Return 0 on success, non-zero otherwise.
4007 sub system_no_output($@)
4014 # Save old stdout and stderr handles
4015 ($mode & 1) && open(OLD_STDOUT, ">>&", "STDOUT");
4016 ($mode & 2) && open(OLD_STDERR, ">>&", "STDERR");
4018 # Redirect to /dev/null
4019 ($mode & 1) && open(STDOUT, ">", "/dev/null");
4020 ($mode & 2) && open(STDERR, ">", "/dev/null");
4025 # Close redirected handles
4026 ($mode & 1) && close(STDOUT);
4027 ($mode & 2) && close(STDERR);
4029 # Restore old handles
4030 ($mode & 1) && open(STDOUT, ">>&", "OLD_STDOUT");
4031 ($mode & 2) && open(STDERR, ">>&", "OLD_STDERR");
4038 # read_config(filename)
4040 # Read configuration file FILENAME and return a reference to a hash containing
4041 # all valid key=value pairs found.
4046 my $filename = $_[0];
4052 if (!open(HANDLE, "<", $filename))
4054 warn("WARNING: cannot read configuration file $filename\n");
4062 # Remove leading blanks
4064 # Remove trailing blanks
4067 ($key, $value) = split(/\s*=\s*/, $_, 2);
4068 if (defined($key) && defined($value))
4070 $result{$key} = $value;
4074 warn("WARNING: malformed statement in line $. ".
4075 "of configuration file $filename\n");
4086 # REF is a reference to a hash containing the following mapping:
4088 # key_string => var_ref
4090 # where KEY_STRING is a keyword and VAR_REF is a reference to an associated
4091 # variable. If the global configuration hashes CONFIG or OPT_RC contain a value
4092 # for keyword KEY_STRING, VAR_REF will be assigned the value for that keyword.
4099 foreach (keys(%{$ref}))
4101 if (defined($opt_rc{$_})) {
4102 ${$ref->{$_}} = $opt_rc{$_};
4103 } elsif (defined($config->{$_})) {
4104 ${$ref->{$_}} = $config->{$_};
4114 warn("$tool_name: $msg");
4122 die("$tool_name: $msg");
4125 sub abort_handler($)
4134 info("Removing temporary directories.\n");
4135 foreach (@temp_dirs) {
4144 system_no_output(3, "mount", "-t", "debugfs", "nodev",
4145 "/sys/kernel/debug");
4148 sub setup_gkv_proc()
4150 if (system_no_output(3, "modprobe", "gcov_proc")) {
4151 system_no_output(3, "modprobe", "gcov_prof");
4155 sub check_gkv_sys($)
4159 if (-e "$dir/reset") {
4165 sub check_gkv_proc($)
4169 if (-e "$dir/vmlinux") {
4178 my $sys_dir = "/sys/kernel/debug/gcov";
4179 my $proc_dir = "/proc/gcov";
4182 if (!defined($gcov_dir)) {
4183 info("Auto-detecting gcov kernel support.\n");
4184 @todo = ( "cs", "cp", "ss", "cs", "sp", "cp" );
4185 } elsif ($gcov_dir =~ /proc/) {
4186 info("Checking gcov kernel support at $gcov_dir ".
4187 "(user-specified).\n");
4188 @todo = ( "cp", "sp", "cp", "cs", "ss", "cs");
4190 info("Checking gcov kernel support at $gcov_dir ".
4191 "(user-specified).\n");
4192 @todo = ( "cs", "ss", "cs", "cp", "sp", "cp", );
4197 $dir = defined($gcov_dir) ? $gcov_dir : $sys_dir;
4198 if (check_gkv_sys($dir)) {
4199 info("Found ".$GKV_NAME[$GKV_SYS]." gcov ".
4200 "kernel support at $dir\n");
4201 return ($GKV_SYS, $dir);
4203 } elsif ($_ eq "cp") {
4205 $dir = defined($gcov_dir) ? $gcov_dir : $proc_dir;
4206 if (check_gkv_proc($dir)) {
4207 info("Found ".$GKV_NAME[$GKV_PROC]." gcov ".
4208 "kernel support at $dir\n");
4209 return ($GKV_PROC, $dir);
4211 } elsif ($_ eq "ss") {
4214 } elsif ($_ eq "sp") {
4219 if (defined($gcov_dir)) {
4220 die("ERROR: could not find gcov kernel data at $gcov_dir\n");
4222 die("ERROR: no gcov kernel data found\n");
4228 # get_overall_line(found, hit, name_singular, name_plural)
4230 # Return a string containing overall information for the specified
4234 sub get_overall_line($$$$)
4236 my ($found, $hit, $name_sn, $name_pl) = @_;
4239 return "no data found" if (!defined($found) || $found == 0);
4240 $name = ($found == 1) ? $name_sn : $name_pl;
4242 return rate($hit, $found, "% ($hit of $found $name)");
4247 # print_overall_rate(ln_do, ln_found, ln_hit, fn_do, fn_found, fn_hit, br_do
4250 # Print overall coverage rates for the specified coverage types.
4253 sub print_overall_rate($$$$$$$$$)
4255 my ($ln_do, $ln_found, $ln_hit, $fn_do, $fn_found, $fn_hit,
4256 $br_do, $br_found, $br_hit) = @_;
4258 info("Summary coverage rate:\n");
4259 info(" lines......: %s\n",
4260 get_overall_line($ln_found, $ln_hit, "line", "lines"))
4262 info(" functions..: %s\n",
4263 get_overall_line($fn_found, $fn_hit, "function", "functions"))
4265 info(" branches...: %s\n",
4266 get_overall_line($br_found, $br_hit, "branch", "branches"))
4272 # rate(hit, found[, suffix, precision, width])
4274 # Return the coverage rate [0..100] for HIT and FOUND values. 0 is only
4275 # returned when HIT is 0. 100 is only returned when HIT equals FOUND.
4276 # PRECISION specifies the precision of the result. SUFFIX defines a
4277 # string that is appended to the result if FOUND is non-zero. Spaces
4278 # are added to the start of the resulting string until it is at least WIDTH
4284 my ($hit, $found, $suffix, $precision, $width) = @_;
4287 # Assign defaults if necessary
4288 $precision = 1 if (!defined($precision));
4289 $suffix = "" if (!defined($suffix));
4290 $width = 0 if (!defined($width));
4292 return sprintf("%*s", $width, "-") if (!defined($found) || $found == 0);
4293 $rate = sprintf("%.*f", $precision, $hit * 100 / $found);
4295 # Adjust rates if necessary
4296 if ($rate == 0 && $hit > 0) {
4297 $rate = sprintf("%.*f", $precision, 1 / 10 ** $precision);
4298 } elsif ($rate == 100 && $hit != $found) {
4299 $rate = sprintf("%.*f", $precision, 100 - 1 / 10 ** $precision);
4302 return sprintf("%*s", $width, $rate.$suffix);