3 # Copyright (c) International Business Machines Corp., 2002,2010
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.9';
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
($);
143 # Global variables & initialization
144 our @directory; # Specifies where to get coverage data from
145 our @kernel_directory; # If set, captures only from specified kernel subdirs
146 our @add_tracefile; # If set, reads in and combines all files in list
147 our $list; # If set, list contents of tracefile
148 our $extract; # If set, extracts parts of tracefile
149 our $remove; # If set, removes parts of tracefile
150 our $diff; # If set, modifies tracefile according to diff
151 our $reset; # If set, reset all coverage data to zero
152 our $capture; # If set, capture data
153 our $output_filename; # Name for file to write coverage data to
154 our $test_name = ""; # Test case name
155 our $quiet = ""; # If set, suppress information messages
156 our $help; # Help option flag
157 our $version; # Version option flag
158 our $convert_filenames; # If set, convert filenames when applying diff
159 our $strip; # If set, strip leading directories when applying diff
160 our $temp_dir_name; # Name of temporary directory
161 our $cwd = `pwd`; # Current working directory
162 our $to_file; # If set, indicates that output is written to a file
163 our $follow; # If set, indicates that find shall follow links
164 our $diff_path = ""; # Path removed from tracefile when applying diff
165 our $base_directory; # Base directory (cwd of gcc during compilation)
166 our $checksum; # If set, calculate a checksum for each line
167 our $no_checksum; # If set, don't calculate a checksum for each line
168 our $compat_libtool; # If set, indicates that libtool mode is to be enabled
169 our $no_compat_libtool; # If set, indicates that libtool mode is to be disabled
173 our $no_recursion = 0;
178 our $config; # Configuration file contents
180 our $tool_dir = dirname
($0); # Directory where genhtml tool is installed
182 our $gcov_gkv; # gcov kernel support version found on machine
183 our $opt_derive_func_data;
185 our $opt_list_full_path;
186 our $opt_no_list_full_path;
187 our $opt_list_width = 80;
188 our $opt_list_truncate_max = 20;
189 our $ln_overall_found;
191 our $fn_overall_found;
193 our $br_overall_found;
201 $SIG{__WARN__
} = \
&warn_handler
;
202 $SIG{__DIE__
} = \
&die_handler
;
203 $SIG{'INT'} = \
&abort_handler
;
204 $SIG{'QUIT'} = \
&abort_handler
;
206 # Prettify version string
207 $lcov_version =~ s/\$\s*Revision\s*:?\s*(\S+)\s*\$/$1/;
209 # Add current working directory if $tool_dir is not already an absolute path
210 if (! ($tool_dir =~ /^\/(.*)$/))
212 $tool_dir = "$cwd/$tool_dir";
215 # Read configuration file if available
216 if (defined($ENV{"HOME"}) && (-r
$ENV{"HOME"}."/.lcovrc"))
218 $config = read_config
($ENV{"HOME"}."/.lcovrc");
220 elsif (-r
"/etc/lcovrc")
222 $config = read_config
("/etc/lcovrc");
227 # Copy configuration file values to variables
229 "lcov_gcov_dir" => \
$gcov_dir,
230 "lcov_tmp_dir" => \
$tmp_dir,
231 "lcov_list_full_path" => \
$opt_list_full_path,
232 "lcov_list_width" => \
$opt_list_width,
233 "lcov_list_truncate_max"=> \
$opt_list_truncate_max,
237 # Parse command line options
238 if (!GetOptions
("directory|d|di=s" => \
@directory,
239 "add-tracefile|a=s" => \
@add_tracefile,
240 "list|l=s" => \
$list,
241 "kernel-directory|k=s" => \
@kernel_directory,
242 "extract|e=s" => \
$extract,
243 "remove|r=s" => \
$remove,
245 "convert-filenames" => \
$convert_filenames,
246 "strip=i" => \
$strip,
247 "capture|c" => \
$capture,
248 "output-file|o=s" => \
$output_filename,
249 "test-name|t=s" => \
$test_name,
250 "zerocounters|z" => \
$reset,
251 "quiet|q" => \
$quiet,
252 "help|h|?" => \
$help,
253 "version|v" => \
$version,
254 "follow|f" => \
$follow,
255 "path=s" => \
$diff_path,
256 "base-directory|b=s" => \
$base_directory,
257 "checksum" => \
$checksum,
258 "no-checksum" => \
$no_checksum,
259 "compat-libtool" => \
$compat_libtool,
260 "no-compat-libtool" => \
$no_compat_libtool,
261 "gcov-tool=s" => \
$gcov_tool,
262 "ignore-errors=s" => \
$ignore_errors,
263 "initial|i" => \
$initial,
264 "no-recursion" => \
$no_recursion,
265 "to-package=s" => \
$to_package,
266 "from-package=s" => \
$from_package,
267 "no-markers" => \
$no_markers,
268 "derive-func-data" => \
$opt_derive_func_data,
269 "debug" => \
$opt_debug,
270 "list-full-path" => \
$opt_list_full_path,
271 "no-list-full-path" => \
$opt_no_list_full_path,
274 print(STDERR
"Use $tool_name --help to get usage information\n");
280 if (defined($no_checksum))
282 $checksum = ($no_checksum ?
0 : 1);
283 $no_checksum = undef;
286 if (defined($no_compat_libtool))
288 $compat_libtool = ($no_compat_libtool ?
0 : 1);
289 $no_compat_libtool = undef;
292 if (defined($opt_no_list_full_path))
294 $opt_list_full_path = ($opt_no_list_full_path ?
0 : 1);
295 $opt_no_list_full_path = undef;
299 # Check for help option
302 print_usage
(*STDOUT
);
306 # Check for version option
309 print("$tool_name: $lcov_version\n");
313 # Check list width option
314 if ($opt_list_width <= 40) {
315 die("ERROR: lcov_list_width parameter out of range (needs to be ".
316 "larger than 40)\n");
319 # Normalize --path text
320 $diff_path =~ s/\/$//;
333 $maxdepth = "-maxdepth 1";
340 # Check for valid options
343 # Only --extract, --remove and --diff allow unnamed parameters
344 if (@ARGV && !($extract || $remove || $diff))
346 die("Extra parameter found: '".join(" ", @ARGV)."'\n".
347 "Use $tool_name --help to get usage information\n");
350 # Check for output filename
351 $to_file = ($output_filename && ($output_filename ne "-"));
357 # Option that tells geninfo to write to stdout
358 $output_filename = "-";
362 # Determine kernel directory for gcov data
363 if (!$from_package && !@directory && ($capture || $reset)) {
364 ($gcov_gkv, $gcov_dir) = setup_gkv
();
367 # Check for requested functionality
370 # Differentiate between user space and kernel reset
382 # Capture source can be user space, kernel or package
385 } elsif (@directory) {
389 if (defined($to_package)) {
390 die("ERROR: --initial cannot be used together ".
391 "with --to-package\n");
393 kernel_capture_initial
();
399 elsif (@add_tracefile)
401 ($ln_overall_found, $ln_overall_hit,
402 $fn_overall_found, $fn_overall_hit,
403 $br_overall_found, $br_overall_hit) = add_traces
();
407 ($ln_overall_found, $ln_overall_hit,
408 $fn_overall_found, $fn_overall_hit,
409 $br_overall_found, $br_overall_hit) = remove
();
413 ($ln_overall_found, $ln_overall_hit,
414 $fn_overall_found, $fn_overall_hit,
415 $br_overall_found, $br_overall_hit) = extract
();
423 if (scalar(@ARGV) != 1)
425 die("ERROR: option --diff requires one additional argument!\n".
426 "Use $tool_name --help to get usage information\n");
428 ($ln_overall_found, $ln_overall_hit,
429 $fn_overall_found, $fn_overall_hit,
430 $br_overall_found, $br_overall_hit) = diff
();
435 if (defined($ln_overall_found)) {
436 print_overall_rate
(1, $ln_overall_found, $ln_overall_hit,
437 1, $fn_overall_found, $fn_overall_hit,
438 1, $br_overall_found, $br_overall_hit);
440 info
("Done.\n") if (!$list && !$capture);
445 # print_usage(handle)
447 # Print usage information.
452 local *HANDLE
= $_[0];
454 print(HANDLE
<<END_OF_USAGE);
455 Usage: $tool_name [OPTIONS]
457 Use lcov to collect coverage data from either the currently running Linux
458 kernel or from a user space application. Specify the --directory option to
459 get coverage data for a user space program.
462 -h, --help Print this help, then exit
463 -v, --version Print version number, then exit
464 -q, --quiet Do not print progress messages
467 -z, --zerocounters Reset all execution counts to zero
468 -c, --capture Capture coverage data
469 -a, --add-tracefile FILE Add contents of tracefiles
470 -e, --extract FILE PATTERN Extract files matching PATTERN from FILE
471 -r, --remove FILE PATTERN Remove files matching PATTERN from FILE
472 -l, --list FILE List contents of tracefile FILE
473 --diff FILE DIFF Transform tracefile FILE according to DIFF
476 -i, --initial Capture initial zero coverage data
477 -t, --test-name NAME Specify test name to be stored with data
478 -o, --output-file FILENAME Write data to FILENAME instead of stdout
479 -d, --directory DIR Use .da files in DIR instead of kernel
480 -f, --follow Follow links when searching .da files
481 -k, --kernel-directory KDIR Capture kernel coverage data only from KDIR
482 -b, --base-directory DIR Use DIR as base directory for relative paths
483 --convert-filenames Convert filenames when applying diff
484 --strip DEPTH Strip initial DEPTH directory levels in diff
485 --path PATH Strip PATH from tracefile when applying diff
486 --(no-)checksum Enable (disable) line checksumming
487 --(no-)compat-libtool Enable (disable) libtool compatibility mode
488 --gcov-tool TOOL Specify gcov tool location
489 --ignore-errors ERRORS Continue after ERRORS (gcov, source, graph)
490 --no-recursion Exclude subdirectories from processing
491 --to-package FILENAME Store unprocessed coverage data in FILENAME
492 --from-package FILENAME Capture from unprocessed data in FILENAME
493 --no-markers Ignore exclusion markers in source code
494 --derive-func-data Generate function data from line data
495 --list-full-path Print full path during a list operation
497 For more information see: $lcov_url
506 # Check for valid combination of command line options. Die on error.
513 # Count occurrence of mutually exclusive options
516 @add_tracefile && $i++;
524 die("Need one of the options -z, -c, -a, -e, -r, -l or ".
526 "Use $tool_name --help to get usage information\n");
530 die("ERROR: only one of -z, -c, -a, -e, -r, -l or ".
532 "Use $tool_name --help to get usage information\n");
540 # Reset coverage data found in DIRECTORY by deleting all contained .da files.
545 sub userspace_reset()
550 foreach $current_dir (@directory)
552 info("Deleting all .da files in $current_dir".
553 ($no_recursion?"\n":" and subdirectories\n"));
554 @file_list = `find "$current_dir" $maxdepth $follow -name \\*\\.da -o -name \\*\\.gcda -type f 2>/dev/null`;
558 unlink($_) or die("ERROR: cannot remove file $_!\n");
565 # userspace_capture()
567 # Capture coverage data found in DIRECTORY and write it to a package (if
568 # TO_PACKAGE specified) or to OUTPUT_FILENAME or STDOUT.
573 sub userspace_capture()
578 if (!defined($to_package)) {
579 lcov_geninfo(@directory);
582 if (scalar(@directory) != 1) {
583 die("ERROR: -d may be specified only once with --to-package\n");
585 $dir = $directory[0];
586 if (defined($base_directory)) {
587 $build = $base_directory;
591 create_package($to_package, $dir, $build);
598 # Reset kernel coverage.
608 info("Resetting kernel execution counters\n");
609 if (-e "$gcov_dir/vmlinux") {
610 $reset_file = "$gcov_dir/vmlinux";
611 } elsif (-e "$gcov_dir/reset") {
612 $reset_file = "$gcov_dir/reset";
614 die("ERROR: no reset control found in $gcov_dir\n");
616 open(HANDLE, ">$reset_file") or
617 die("ERROR: cannot write to $reset_file!\n");
624 # lcov_copy_single(from, to)
626 # Copy single regular file FROM to TO without checking its size. This is
627 # required to work with special files generated by the kernel
628 # seq_file-interface.
631 sub lcov_copy_single($$)
633 my ($from, $to) = @_;
638 open(HANDLE, "<$from") or die("ERROR: cannot read $from: $!\n");
641 open(HANDLE, ">$to") or die("ERROR: cannot write $from: $!\n");
642 if (defined($content)) {
643 print(HANDLE $content);
649 # lcov_find(dir, function, data[, extension, ...)])
651 # Search DIR for files and directories whose name matches PATTERN and run
652 # FUNCTION for each match. If not pattern is specified, match all names.
654 # FUNCTION has the following prototype:
655 # function(dir, relative_name, data)
658 # dir: the base directory for this search
659 # relative_name: the name relative to the base directory of this entry
660 # data: the DATA variable passed to lcov_find
664 my ($dir, $fn, $data, @pattern) = @_;
667 my $filename = $File::Find::name;
669 if (defined($result)) {
672 $filename = abs2rel($filename, $dir);
674 if ($filename =~ /$_/) {
680 $result = &$fn($dir, $filename, $data);
682 if (scalar(@pattern) == 0) {
685 find( { wanted => $_fn, no_chdir => 1 }, $dir);
691 # lcov_copy_fn(from, rel, to)
693 # Copy directories, files and links from/rel to to/rel.
696 sub lcov_copy_fn($$$)
698 my ($from, $rel, $to) = @_;
699 my $absfrom = canonpath(catfile($from, $rel));
700 my $absto = canonpath(catfile($to, $rel));
705 die("ERROR: cannot create directory $absto\n");
710 my $link = readlink($absfrom);
712 if (!defined($link)) {
713 die("ERROR: cannot read link $absfrom: $!\n");
715 symlink($link, $absto) or
716 die("ERROR: cannot create link $absto: $!\n");
718 lcov_copy_single($absfrom, $absto);
725 # lcov_copy(from, to, subdirs)
727 # Copy all specified SUBDIRS and files from directory FROM to directory TO. For
728 # regular files, copy file contents without checking its size. This is required
729 # to work with seq_file-generated files.
734 my ($from, $to, @subdirs) = @_;
738 push(@pattern, "^$_");
740 lcov_find($from, \&lcov_copy_fn, $to, @pattern);
744 # lcov_geninfo(directory)
746 # Call geninfo for the specified directory and with the parameters specified
747 # at the command line.
756 info("Capturing coverage data from ".join(" ", @dir)."\n");
757 @param = ("$tool_dir/geninfo", @dir);
758 if ($output_filename)
760 @param = (@param, "--output-filename", $output_filename);
764 @param = (@param, "--test-name", $test_name);
768 @param = (@param, "--follow");
772 @param = (@param, "--quiet");
774 if (defined($checksum))
778 @param = (@param, "--checksum");
782 @param = (@param, "--no-checksum");
787 @param = (@param, "--base-directory", $base_directory);
789 if ($no_compat_libtool)
791 @param = (@param, "--no-compat-libtool");
793 elsif ($compat_libtool)
795 @param = (@param, "--compat-libtool");
799 @param = (@param, "--gcov-tool", $gcov_tool);
803 @param = (@param, "--ignore-errors", $ignore_errors);
807 @param = (@param, "--initial");
811 @param = (@param, "--no-markers");
813 if ($opt_derive_func_data)
815 @param = (@param, "--derive-func-data");
819 @param = (@param, "--debug");
821 system(@param) and exit($? >> 8);
825 # read_file(filename)
827 # Return the contents of the file defined by filename.
837 open(HANDLE, "<$filename") || return undef;
845 # get_package(package_file)
847 # Unpack unprocessed coverage data files from package_file to a temporary
848 # directory and return directory name, build directory and gcov kernel version
849 # as found in package.
855 my $dir = create_temp_dir();
862 info("Reading package $file:\n");
863 info(" data directory .......: $dir\n");
864 $file = abs_path($file);
866 open(HANDLE, "tar xvfz $file 2>/dev/null|")
867 or die("ERROR: could not process package $file\n");
869 if (/\.da$/ || /\.gcda$/) {
874 $build = read_file("$dir/$pkg_build_file");
875 if (defined($build)) {
876 info(" build directory ......: $build\n");
878 $gkv = read_file("$dir/$pkg_gkv_file");
881 if ($gkv != $GKV_PROC && $gkv != $GKV_SYS) {
882 die("ERROR: unsupported gcov kernel version found ".
885 info(" content type .........: kernel data\n");
886 info(" gcov kernel version ..: %s\n", $GKV_NAME[$gkv]);
888 info(" content type .........: application data\n");
890 info(" data files ...........: $count\n");
893 return ($dir, $build, $gkv);
897 # write_file(filename, $content)
899 # Create a file named filename and write the specified content to it.
904 my ($filename, $content) = @_;
907 open(HANDLE, ">$filename") || return 0;
908 print(HANDLE $content);
909 close(HANDLE) || return 0;
914 # count_package_data(filename)
916 # Count the number of coverage data files in the specified package file.
919 sub count_package_data($)
925 open(HANDLE, "tar tfz $filename|") or return undef;
927 if (/\.da$/ || /\.gcda$/) {
936 # create_package(package_file, source_directory, build_directory[,
937 # kernel_gcov_version])
939 # Store unprocessed coverage data files from source_directory to package_file.
942 sub create_package($$$;$)
944 my ($file, $dir, $build, $gkv) = @_;
947 # Print information about the package
948 info("Creating package $file:\n");
949 info(" data directory .......: $dir\n");
951 # Handle build directory
952 if (defined($build)) {
953 info(" build directory ......: $build\n");
954 write_file("$dir/$pkg_build_file", $build)
955 or die("ERROR: could not write to ".
956 "$dir/$pkg_build_file\n");
959 # Handle gcov kernel version data
961 info(" content type .........: kernel data\n");
962 info(" gcov kernel version ..: %s\n", $GKV_NAME[$gkv]);
963 write_file("$dir/$pkg_gkv_file", $gkv)
964 or die("ERROR: could not write to ".
965 "$dir/$pkg_gkv_file\n");
967 info(" content type .........: application data\n");
971 $file = abs_path($file);
973 system("tar cfz $file .")
974 and die("ERROR: could not create package $file\n");
976 # Remove temporary files
977 unlink("$dir/$pkg_build_file");
978 unlink("$dir/$pkg_gkv_file");
980 # Show number of data files
982 my $count = count_package_data($file);
984 if (defined($count)) {
985 info(" data files ...........: $count\n");
991 sub find_link_fn($$$)
993 my ($from, $rel, $filename) = @_;
994 my $absfile = catfile($from, $rel, $filename);
1005 # Return (BASE, OBJ), where
1006 # - BASE: is the path to the kernel base directory relative to dir
1007 # - OBJ: is the absolute path to the kernel build directory
1013 my $marker = "kernel/gcov/base.gcno";
1019 $markerfile = lcov_find($dir, \&find_link_fn, $marker);
1020 if (!defined($markerfile)) {
1021 return (undef, undef);
1024 # sys base is parent of parent of markerfile.
1025 $sys = abs2rel(dirname(dirname(dirname($markerfile))), $dir);
1027 # obj base is parent of parent of markerfile link target.
1028 $link = readlink($markerfile);
1029 if (!defined($link)) {
1030 die("ERROR: could not read $markerfile\n");
1032 $obj = dirname(dirname(dirname($link)));
1034 return ($sys, $obj);
1038 # apply_base_dir(data_dir, base_dir, build_dir, @directories)
1040 # Make entries in @directories relative to data_dir.
1043 sub apply_base_dir($$$@)
1045 my ($data, $base, $build, @dirs) = @_;
1049 foreach $dir (@dirs) {
1050 # Is directory path relative to data directory?
1051 if (-d catdir($data, $dir)) {
1052 push(@result, $dir);
1055 # Relative to the auto-detected base-directory?
1056 if (defined($base)) {
1057 if (-d catdir($data, $base, $dir)) {
1058 push(@result, catdir($base, $dir));
1062 # Relative to the specified base-directory?
1063 if (defined($base_directory)) {
1064 if (file_name_is_absolute($base_directory)) {
1065 $base = abs2rel($base_directory, rootdir());
1067 $base = $base_directory;
1069 if (-d catdir($data, $base, $dir)) {
1070 push(@result, catdir($base, $dir));
1074 # Relative to the build directory?
1075 if (defined($build)) {
1076 if (file_name_is_absolute($build)) {
1077 $base = abs2rel($build, rootdir());
1081 if (-d catdir($data, $base, $dir)) {
1082 push(@result, catdir($base, $dir));
1086 die("ERROR: subdirectory $dir not found\n".
1087 "Please use -b to specify the correct directory\n");
1093 # copy_gcov_dir(dir, [@subdirectories])
1095 # Create a temporary directory and copy all or, if specified, only some
1096 # subdirectories from dir to that directory. Return the name of the temporary
1100 sub copy_gcov_dir($;@)
1102 my ($data, @dirs) = @_;
1103 my $tempdir = create_temp_dir();
1105 info("Copying data to temporary directory $tempdir\n");
1106 lcov_copy($data, $tempdir, @dirs);
1112 # kernel_capture_initial
1114 # Capture initial kernel coverage data, i.e. create a coverage data file from
1115 # static graph files which contains zero coverage data for all instrumented
1119 sub kernel_capture_initial()
1125 if (defined($base_directory)) {
1126 $build = $base_directory;
1127 $source = "specified";
1129 (undef, $build) = get_base($gcov_dir);
1130 if (!defined($build)) {
1131 die("ERROR: could not auto-detect build directory.\n".
1132 "Please use -b to specify the build directory\n");
1134 $source = "auto-detected";
1136 info("Using $build as kernel build directory ($source)\n");
1137 # Build directory needs to be passed to geninfo
1138 $base_directory = $build;
1139 if (@kernel_directory) {
1140 foreach my $dir (@kernel_directory) {
1141 push(@params, "$build/$dir");
1144 push(@params, $build);
1146 lcov_geninfo(@params);
1150 # kernel_capture_from_dir(directory, gcov_kernel_version, build)
1152 # Perform the actual kernel coverage capturing from the specified directory
1153 # assuming that the data was copied from the specified gcov kernel version.
1156 sub kernel_capture_from_dir($$$)
1158 my ($dir, $gkv, $build) = @_;
1160 # Create package or coverage file
1161 if (defined($to_package)) {
1162 create_package($to_package, $dir, $build, $gkv);
1164 # Build directory needs to be passed to geninfo
1165 $base_directory = $build;
1171 # adjust_kernel_dir(dir, build)
1173 # Adjust directories specified with -k so that they point to the directory
1174 # relative to DIR. Return the build directory if specified or the auto-
1175 # detected build-directory.
1178 sub adjust_kernel_dir($$)
1180 my ($dir, $build) = @_;
1181 my ($sys_base, $build_auto) = get_base($dir);
1183 if (!defined($build)) {
1184 $build = $build_auto;
1186 if (!defined($build)) {
1187 die("ERROR: could not auto-detect build directory.\n".
1188 "Please use -b to specify the build directory\n");
1190 # Make @kernel_directory relative to sysfs base
1191 if (@kernel_directory) {
1192 @kernel_directory = apply_base_dir($dir, $sys_base, $build,
1198 sub kernel_capture()
1201 my $build = $base_directory;
1203 if ($gcov_gkv == $GKV_SYS) {
1204 $build = adjust_kernel_dir($gcov_dir, $build);
1206 $data_dir = copy_gcov_dir($gcov_dir, @kernel_directory);
1207 kernel_capture_from_dir($data_dir, $gcov_gkv, $build);
1213 # Capture coverage data from a package of unprocessed coverage data files
1214 # as generated by lcov --to-package.
1217 sub package_capture()
1223 ($dir, $build, $gkv) = get_package($from_package);
1225 # Check for build directory
1226 if (defined($base_directory)) {
1227 if (defined($build)) {
1228 info("Using build directory specified by -b.\n");
1230 $build = $base_directory;
1233 # Do the actual capture
1234 if (defined($gkv)) {
1235 if ($gkv == $GKV_SYS) {
1236 $build = adjust_kernel_dir($dir, $build);
1238 if (@kernel_directory) {
1239 $dir = copy_gcov_dir($dir, @kernel_directory);
1241 kernel_capture_from_dir($dir, $gkv, $build);
1243 # Build directory needs to be passed to geninfo
1244 $base_directory = $build;
1251 # info(printf_parameter)
1253 # Use printf to write PRINTF_PARAMETER to stdout only when the $quiet flag
1268 # Don't interfere with the .info output to STDOUT
1278 # Create a temporary directory and return its path.
1283 sub create_temp_dir()
1287 if (defined($tmp_dir)) {
1288 $dir = tempdir(DIR => $tmp_dir, CLEANUP => 1);
1290 $dir = tempdir(CLEANUP => 1);
1292 if (!defined($dir)) {
1293 die("ERROR: cannot create temporary directory\n");
1295 push(@temp_dirs, $dir);
1302 # br_taken_to_num(taken)
1304 # Convert a branch taken value .info format to number format.
1307 sub br_taken_to_num($)
1311 return 0 if ($taken eq '-');
1317 # br_num_to_taken(taken)
1319 # Convert a branch taken value in number format to .info format.
1322 sub br_num_to_taken($)
1326 return '-' if ($taken == 0);
1332 # br_taken_add(taken1, taken2)
1334 # Return the result of taken1 + taken2 for 'branch taken' values.
1337 sub br_taken_add($$)
1341 return $t1 if (!defined($t2));
1342 return $t2 if (!defined($t1));
1343 return $t1 if ($t2 eq '-');
1344 return $t2 if ($t1 eq '-');
1350 # br_taken_sub(taken1, taken2)
1352 # Return the result of taken1 - taken2 for 'branch taken' values. Return 0
1353 # if the result would become negative.
1356 sub br_taken_sub($$)
1360 return $t1 if (!defined($t2));
1361 return undef if (!defined($t1));
1362 return $t1 if ($t1 eq '-');
1363 return $t1 if ($t2 eq '-');
1364 return 0 if $t2 > $t1;
1371 # br_ivec_len(vector)
1373 # Return the number of entries in the branch coverage vector.
1380 return 0 if (!defined($vec));
1381 return (length($vec) * 8 / $BR_VEC_WIDTH) / $BR_VEC_ENTRIES;
1386 # br_ivec_push(vector, block, branch, taken)
1388 # Add an entry to the branch coverage vector. If an entry with the same
1389 # branch ID already exists, add the corresponding taken values.
1392 sub br_ivec_push($$$$)
1394 my ($vec, $block, $branch, $taken) = @_;
1396 my $num = br_ivec_len($vec);
1399 $vec = "" if (!defined($vec));
1401 # Check if branch already exists in vector
1402 for ($i = 0; $i < $num; $i++) {
1403 my ($v_block, $v_branch, $v_taken) = br_ivec_get($vec, $i);
1405 next if ($v_block != $block || $v_branch != $branch);
1408 $taken = br_taken_add($taken, $v_taken);
1412 $offset = $i * $BR_VEC_ENTRIES;
1413 $taken = br_taken_to_num($taken);
1416 vec($vec, $offset + $BR_BLOCK, $BR_VEC_WIDTH) = $block;
1417 vec($vec, $offset + $BR_BRANCH, $BR_VEC_WIDTH) = $branch;
1418 vec($vec, $offset + $BR_TAKEN, $BR_VEC_WIDTH) = $taken;
1425 # br_ivec_get(vector, number)
1427 # Return an entry from the branch coverage vector.
1432 my ($vec, $num) = @_;
1436 my $offset = $num * $BR_VEC_ENTRIES;
1438 # Retrieve data from vector
1439 $block = vec($vec, $offset + $BR_BLOCK, $BR_VEC_WIDTH);
1440 $branch = vec($vec, $offset + $BR_BRANCH, $BR_VEC_WIDTH);
1441 $taken = vec($vec, $offset + $BR_TAKEN, $BR_VEC_WIDTH);
1443 # Decode taken value from an integer
1444 $taken = br_num_to_taken($taken);
1446 return ($block, $branch, $taken);
1451 # get_br_found_and_hit(brcount)
1453 # Return (br_found, br_hit) for brcount
1456 sub get_br_found_and_hit($)
1463 foreach $line (keys(%{$brcount})) {
1464 my $brdata = $brcount->{$line};
1466 my $num = br_ivec_len($brdata);
1468 for ($i = 0; $i < $num; $i++) {
1471 (undef, undef, $taken) = br_ivec_get($brdata, $i);
1474 $br_hit++ if ($taken ne "-" && $taken > 0);
1478 return ($br_found, $br_hit);
1483 # read_info_file(info_filename)
1485 # Read in the contents of the .info file specified by INFO_FILENAME. Data will
1486 # be returned as a reference to a hash containing the following mappings:
1488 # %result: for each filename found in file -> \%data
1490 # %data: "test" -> \%testdata
1491 # "sum" -> \%sumcount
1492 # "func" -> \%funcdata
1493 # "found" -> $lines_found (number of instrumented lines found in file)
1494 # "hit" -> $lines_hit (number of executed lines in file)
1495 # "check" -> \%checkdata
1496 # "testfnc" -> \%testfncdata
1497 # "sumfnc" -> \%sumfnccount
1498 # "testbr" -> \%testbrdata
1499 # "sumbr" -> \%sumbrcount
1501 # %testdata : name of test affecting this file -> \%testcount
1502 # %testfncdata: name of test affecting this file -> \%testfnccount
1503 # %testbrdata: name of test affecting this file -> \%testbrcount
1505 # %testcount : line number -> execution count for a single test
1506 # %testfnccount: function name -> execution count for a single test
1507 # %testbrcount : line number -> branch coverage data for a single test
1508 # %sumcount : line number -> execution count for all tests
1509 # %sumfnccount : function name -> execution count for all tests
1510 # %sumbrcount : line number -> branch coverage data for all tests
1511 # %funcdata : function name -> line number
1512 # %checkdata : line number -> checksum of source code line
1513 # $brdata : vector of items: block, branch, taken
1515 # Note that .info file sections referring to the same file and test name
1516 # will automatically be combined by adding all execution counts.
1518 # Note that if INFO_FILENAME ends with ".gz", it is assumed that the file
1519 # is compressed using GZIP. If available, GUNZIP will be used to decompress
1525 sub read_info_file($)
1527 my $tracefile = $_[0]; # Name of tracefile
1528 my %result; # Resulting hash: file -> data
1529 my $data; # Data handle for current entry
1531 my $testcount; # " "
1534 my $checkdata; # " "
1541 my $line; # Current line read from .info file
1542 my $testname; # Current test name
1543 my $filename; # Current filename
1544 my $hitcount; # Count for lines hit
1545 my $count; # Execution count of current line
1546 my $negative; # If set, warn about negative counts
1547 my $changed_testname; # If set, warn about changed testname
1548 my $line_checksum; # Checksum of current line
1549 local *INFO_HANDLE; # Filehandle for .info file
1551 info("Reading tracefile $tracefile\n");
1553 # Check if file exists and is readable
1557 die("ERROR: cannot read file $_[0]!\n");
1560 # Check if this is really a plain file
1563 die("ERROR: not a plain file: $_[0]!\n");
1566 # Check for .gz extension
1567 if ($_[0] =~ /\.gz$/)
1569 # Check for availability of GZIP tool
1570 system_no_output(1, "gunzip" ,"-h")
1571 and die("ERROR: gunzip command not available!\n");
1573 # Check integrity of compressed file
1574 system_no_output(1, "gunzip", "-t", $_[0])
1575 and die("ERROR: integrity check failed for ".
1576 "compressed file $_[0]!\n");
1578 # Open compressed file
1579 open(INFO_HANDLE, "gunzip -c $_[0]|")
1580 or die("ERROR: cannot start gunzip to decompress ".
1585 # Open decompressed file
1586 open(INFO_HANDLE, $_[0])
1587 or die("ERROR: cannot read file $_[0]!\n");
1591 while (<INFO_HANDLE>)
1599 /^TN:([^,]*)(,diff)?/ && do
1601 # Test name information found
1602 $testname = defined($1) ? $1 : "";
1603 if ($testname =~ s/\W/_/g)
1605 $changed_testname = 1;
1607 $testname .= $2 if (defined($2));
1613 # Filename information found
1614 # Retrieve data for new entry
1617 $data = $result{$filename};
1618 ($testdata, $sumcount, $funcdata, $checkdata,
1619 $testfncdata, $sumfnccount, $testbrdata,
1621 get_info_entry($data);
1623 if (defined($testname))
1625 $testcount = $testdata->{$testname};
1626 $testfnccount = $testfncdata->{$testname};
1627 $testbrcount = $testbrdata->{$testname};
1638 /^DA:(\d+),(-?\d+)(,[^,\s]+)?/ && do
1640 # Fix negative counts
1641 $count = $2 < 0 ? 0 : $2;
1646 # Execution count found, add to structure
1647 # Add summary counts
1648 $sumcount->{$1} += $count;
1650 # Add test-specific counts
1651 if (defined($testname))
1653 $testcount->{$1} += $count;
1656 # Store line checksum if available
1659 $line_checksum = substr($3, 1);
1661 # Does it match a previous definition
1662 if (defined($checkdata->{$1}) &&
1663 ($checkdata->{$1} ne
1666 die("ERROR: checksum mismatch ".
1667 "at $filename:$1\n");
1670 $checkdata->{$1} = $line_checksum;
1675 /^FN:(\d+),([^,]+)/ && do
1677 # Function data found, add to structure
1678 $funcdata->{$2} = $1;
1680 # Also initialize function call data
1681 if (!defined($sumfnccount->{$2})) {
1682 $sumfnccount->{$2} = 0;
1684 if (defined($testname))
1686 if (!defined($testfnccount->{$2})) {
1687 $testfnccount->{$2} = 0;
1693 /^FNDA:(\d+),([^,]+)/ && do
1695 # Function call count found, add to structure
1696 # Add summary counts
1697 $sumfnccount->{$2} += $1;
1699 # Add test-specific counts
1700 if (defined($testname))
1702 $testfnccount->{$2} += $1;
1707 /^BRDA:(\d+),(\d+),(\d+),(\d+|-)/ && do {
1708 # Branch coverage data found
1709 my ($line, $block, $branch, $taken) =
1712 $sumbrcount->{$line} =
1713 br_ivec_push($sumbrcount->{$line},
1714 $block, $branch, $taken);
1716 # Add test-specific counts
1717 if (defined($testname)) {
1718 $testbrcount->{$line} =
1720 $testbrcount->{$line},
1727 /^end_of_record/ && do
1729 # Found end of section marker
1732 # Store current section data
1733 if (defined($testname))
1735 $testdata->{$testname} =
1737 $testfncdata->{$testname} =
1739 $testbrdata->{$testname} =
1743 set_info_entry($data, $testdata,
1744 $sumcount, $funcdata,
1745 $checkdata, $testfncdata,
1749 $result{$filename} = $data;
1760 # Calculate hit and found values for lines and functions of each file
1761 foreach $filename (keys(%result))
1763 $data = $result{$filename};
1765 ($testdata, $sumcount, undef, undef, $testfncdata,
1766 $sumfnccount, $testbrdata, $sumbrcount) =
1767 get_info_entry($data);
1769 # Filter out empty files
1770 if (scalar(keys(%{$sumcount})) == 0)
1772 delete($result{$filename});
1775 # Filter out empty test cases
1776 foreach $testname (keys(%{$testdata}))
1778 if (!defined($testdata->{$testname}) ||
1779 scalar(keys(%{$testdata->{$testname}})) == 0)
1781 delete($testdata->{$testname});
1782 delete($testfncdata->{$testname});
1786 $data->{"found"} = scalar(keys(%{$sumcount}));
1789 foreach (keys(%{$sumcount}))
1791 if ($sumcount->{$_} > 0) { $hitcount++; }
1794 $data->{"hit"} = $hitcount;
1796 # Get found/hit values for function call data
1797 $data->{"f_found"} = scalar(keys(%{$sumfnccount}));
1800 foreach (keys(%{$sumfnccount})) {
1801 if ($sumfnccount->{$_} > 0) {
1805 $data->{"f_hit"} = $hitcount;
1807 # Get found/hit values for branch data
1809 my ($br_found, $br_hit) = get_br_found_and_hit($sumbrcount);
1811 $data->{"b_found"} = $br_found;
1812 $data->{"b_hit"} = $br_hit;
1816 if (scalar(keys(%result)) == 0)
1818 die("ERROR: no valid records found in tracefile $tracefile\n");
1822 warn("WARNING: negative counts found in tracefile ".
1825 if ($changed_testname)
1827 warn("WARNING: invalid characters removed from testname in ".
1828 "tracefile $tracefile\n");
1836 # get_info_entry(hash_ref)
1838 # Retrieve data from an entry of the structure generated by read_info_file().
1839 # Return a list of references to hashes:
1840 # (test data hash ref, sum count hash ref, funcdata hash ref, checkdata hash
1841 # ref, testfncdata hash ref, sumfnccount hash ref, testbrdata hash ref,
1842 # sumbrcount hash ref, lines found, lines hit, functions found,
1843 # functions hit, branches found, branches hit)
1846 sub get_info_entry($)
1848 my $testdata_ref = $_[0]->{"test"};
1849 my $sumcount_ref = $_[0]->{"sum"};
1850 my $funcdata_ref = $_[0]->{"func"};
1851 my $checkdata_ref = $_[0]->{"check"};
1852 my $testfncdata = $_[0]->{"testfnc"};
1853 my $sumfnccount = $_[0]->{"sumfnc"};
1854 my $testbrdata = $_[0]->{"testbr"};
1855 my $sumbrcount = $_[0]->{"sumbr"};
1856 my $lines_found = $_[0]->{"found"};
1857 my $lines_hit = $_[0]->{"hit"};
1858 my $f_found = $_[0]->{"f_found"};
1859 my $f_hit = $_[0]->{"f_hit"};
1860 my $br_found = $_[0]->{"b_found"};
1861 my $br_hit = $_[0]->{"b_hit"};
1863 return ($testdata_ref, $sumcount_ref, $funcdata_ref, $checkdata_ref,
1864 $testfncdata, $sumfnccount, $testbrdata, $sumbrcount,
1865 $lines_found, $lines_hit, $f_found, $f_hit,
1866 $br_found, $br_hit);
1871 # set_info_entry(hash_ref, testdata_ref, sumcount_ref, funcdata_ref,
1872 # checkdata_ref, testfncdata_ref, sumfcncount_ref,
1873 # testbrdata_ref, sumbrcount_ref[,lines_found,
1874 # lines_hit, f_found, f_hit, $b_found, $b_hit])
1876 # Update the hash referenced by HASH_REF with the provided data references.
1879 sub set_info_entry($$$$$$$$$;$$$$$$)
1881 my $data_ref = $_[0];
1883 $data_ref->{"test"} = $_[1];
1884 $data_ref->{"sum"} = $_[2];
1885 $data_ref->{"func"} = $_[3];
1886 $data_ref->{"check"} = $_[4];
1887 $data_ref->{"testfnc"} = $_[5];
1888 $data_ref->{"sumfnc"} = $_[6];
1889 $data_ref->{"testbr"} = $_[7];
1890 $data_ref->{"sumbr"} = $_[8];
1892 if (defined($_[9])) { $data_ref->{"found"} = $_[9]; }
1893 if (defined($_[10])) { $data_ref->{"hit"} = $_[10]; }
1894 if (defined($_[11])) { $data_ref->{"f_found"} = $_[11]; }
1895 if (defined($_[12])) { $data_ref->{"f_hit"} = $_[12]; }
1896 if (defined($_[13])) { $data_ref->{"b_found"} = $_[13]; }
1897 if (defined($_[14])) { $data_ref->{"b_hit"} = $_[14]; }
1902 # add_counts(data1_ref, data2_ref)
1904 # DATA1_REF and DATA2_REF are references to hashes containing a mapping
1906 # line number -> execution count
1908 # Return a list (RESULT_REF, LINES_FOUND, LINES_HIT) where RESULT_REF
1909 # is a reference to a hash containing the combined mapping in which
1910 # execution counts are added.
1915 my %data1 = %{$_[0]}; # Hash 1
1916 my %data2 = %{$_[1]}; # Hash 2
1917 my %result; # Resulting hash
1918 my $line; # Current line iteration scalar
1919 my $data1_count; # Count of line in hash1
1920 my $data2_count; # Count of line in hash2
1921 my $found = 0; # Total number of lines found
1922 my $hit = 0; # Number of lines with a count > 0
1924 foreach $line (keys(%data1))
1926 $data1_count = $data1{$line};
1927 $data2_count = $data2{$line};
1929 # Add counts if present in both hashes
1930 if (defined($data2_count)) { $data1_count += $data2_count; }
1932 # Store sum in %result
1933 $result{$line} = $data1_count;
1936 if ($data1_count > 0) { $hit++; }
1939 # Add lines unique to data2
1940 foreach $line (keys(%data2))
1942 # Skip lines already in data1
1943 if (defined($data1{$line})) { next; }
1945 # Copy count from data2
1946 $result{$line} = $data2{$line};
1949 if ($result{$line} > 0) { $hit++; }
1952 return (\%result, $found, $hit);
1957 # merge_checksums(ref1, ref2, filename)
1959 # REF1 and REF2 are references to hashes containing a mapping
1961 # line number -> checksum
1963 # Merge checksum lists defined in REF1 and REF2 and return reference to
1964 # resulting hash. Die if a checksum for a line is defined in both hashes
1965 # but does not match.
1968 sub merge_checksums($$$)
1972 my $filename = $_[2];
1976 foreach $line (keys(%{$ref1}))
1978 if (defined($ref2->{$line}) &&
1979 ($ref1->{$line} ne $ref2->{$line}))
1981 die("ERROR: checksum mismatch at $filename:$line\n");
1983 $result{$line} = $ref1->{$line};
1986 foreach $line (keys(%{$ref2}))
1988 $result{$line} = $ref2->{$line};
1996 # merge_func_data(funcdata1, funcdata2, filename)
1999 sub merge_func_data($$$)
2001 my ($funcdata1, $funcdata2, $filename) = @_;
2005 if (defined($funcdata1)) {
2006 %result = %{$funcdata1};
2009 foreach $func (keys(%{$funcdata2})) {
2010 my $line1 = $result{$func};
2011 my $line2 = $funcdata2->{$func};
2013 if (defined($line1) && ($line1 != $line2)) {
2014 warn("WARNING: function data mismatch at ".
2015 "$filename:$line2\n");
2018 $result{$func} = $line2;
2026 # add_fnccount(fnccount1, fnccount2)
2028 # Add function call count data. Return list (fnccount_added, f_found, f_hit)
2031 sub add_fnccount($$)
2033 my ($fnccount1, $fnccount2) = @_;
2039 if (defined($fnccount1)) {
2040 %result = %{$fnccount1};
2042 foreach $function (keys(%{$fnccount2})) {
2043 $result{$function} += $fnccount2->{$function};
2045 $f_found = scalar(keys(%result));
2047 foreach $function (keys(%result)) {
2048 if ($result{$function} > 0) {
2053 return (\%result, $f_found, $f_hit);
2057 # add_testfncdata(testfncdata1, testfncdata2)
2059 # Add function call count data for several tests. Return reference to
2060 # added_testfncdata.
2063 sub add_testfncdata($$)
2065 my ($testfncdata1, $testfncdata2) = @_;
2069 foreach $testname (keys(%{$testfncdata1})) {
2070 if (defined($testfncdata2->{$testname})) {
2073 # Function call count data for this testname exists
2074 # in both data sets: merge
2075 ($fnccount) = add_fnccount(
2076 $testfncdata1->{$testname},
2077 $testfncdata2->{$testname});
2078 $result{$testname} = $fnccount;
2081 # Function call count data for this testname is unique to
2083 $result{$testname} = $testfncdata1->{$testname};
2086 # Add count data for testnames unique to data set 2
2087 foreach $testname (keys(%{$testfncdata2})) {
2088 if (!defined($result{$testname})) {
2089 $result{$testname} = $testfncdata2->{$testname};
2097 # brcount_to_db(brcount)
2099 # Convert brcount data to the following format:
2101 # db: line number -> block hash
2102 # block hash: block number -> branch hash
2103 # branch hash: branch number -> taken value
2106 sub brcount_to_db($)
2112 # Add branches from first count to database
2113 foreach $line (keys(%{$brcount})) {
2114 my $brdata = $brcount->{$line};
2116 my $num = br_ivec_len($brdata);
2118 for ($i = 0; $i < $num; $i++) {
2119 my ($block, $branch, $taken) = br_ivec_get($brdata, $i);
2121 $db->{$line}->{$block}->{$branch} = $taken;
2132 # Convert branch coverage data back to brcount format.
2135 sub db_to_brcount($)
2143 # Convert database back to brcount format
2144 foreach $line (sort({$a <=> $b} keys(%{$db}))) {
2145 my $ldata = $db->{$line};
2149 foreach $block (sort({$a <=> $b} keys(%{$ldata}))) {
2150 my $bdata = $ldata->{$block};
2153 foreach $branch (sort({$a <=> $b} keys(%{$bdata}))) {
2154 my $taken = $bdata->{$branch};
2157 $br_hit++ if ($taken ne "-" && $taken > 0);
2158 $brdata = br_ivec_push($brdata, $block,
2162 $brcount->{$line} = $brdata;
2165 return ($brcount, $br_found, $br_hit);
2169 # combine_brcount(brcount1, brcount2, type)
2171 # If add is BR_ADD, add branch coverage data and return list (brcount_added,
2172 # br_found, br_hit). If add is BR_SUB, subtract the taken values of brcount2
2173 # from brcount1 and return (brcount_sub, br_found, br_hit).
2176 sub combine_brcount($$$)
2178 my ($brcount1, $brcount2, $type) = @_;
2188 # Convert branches from first count to database
2189 $db = brcount_to_db($brcount1);
2190 # Combine values from database and second count
2191 foreach $line (keys(%{$brcount2})) {
2192 my $brdata = $brcount2->{$line};
2193 my $num = br_ivec_len($brdata);
2196 for ($i = 0; $i < $num; $i++) {
2197 ($block, $branch, $taken) = br_ivec_get($brdata, $i);
2198 my $new_taken = $db->{$line}->{$block}->{$branch};
2200 if ($type == $BR_ADD) {
2201 $new_taken = br_taken_add($new_taken, $taken);
2202 } elsif ($type == $BR_SUB) {
2203 $new_taken = br_taken_sub($new_taken, $taken);
2205 $db->{$line}->{$block}->{$branch} = $new_taken
2206 if (defined($new_taken));
2209 # Convert database back to brcount format
2210 ($result, $br_found, $br_hit) = db_to_brcount($db);
2212 return ($result, $br_found, $br_hit);
2217 # add_testbrdata(testbrdata1, testbrdata2)
2219 # Add branch coverage data for several tests. Return reference to
2223 sub add_testbrdata($$)
2225 my ($testbrdata1, $testbrdata2) = @_;
2229 foreach $testname (keys(%{$testbrdata1})) {
2230 if (defined($testbrdata2->{$testname})) {
2233 # Branch coverage data for this testname exists
2234 # in both data sets: add
2235 ($brcount) = combine_brcount(
2236 $testbrdata1->{$testname},
2237 $testbrdata2->{$testname}, $BR_ADD);
2238 $result{$testname} = $brcount;
2241 # Branch coverage data for this testname is unique to
2243 $result{$testname} = $testbrdata1->{$testname};
2246 # Add count data for testnames unique to data set 2
2247 foreach $testname (keys(%{$testbrdata2})) {
2248 if (!defined($result{$testname})) {
2249 $result{$testname} = $testbrdata2->{$testname};
2257 # combine_info_entries(entry_ref1, entry_ref2, filename)
2259 # Combine .info data entry hashes referenced by ENTRY_REF1 and ENTRY_REF2.
2260 # Return reference to resulting hash.
2263 sub combine_info_entries($$$)
2265 my $entry1 = $_[0]; # Reference to hash containing first entry
2275 my $entry2 = $_[1]; # Reference to hash containing second entry
2285 my %result; # Hash containing combined entry
2286 my %result_testdata;
2287 my $result_sumcount = {};
2288 my $result_funcdata;
2289 my $result_testfncdata;
2290 my $result_sumfnccount;
2291 my $result_testbrdata;
2292 my $result_sumbrcount;
2301 my $filename = $_[2];
2304 ($testdata1, $sumcount1, $funcdata1, $checkdata1, $testfncdata1,
2305 $sumfnccount1, $testbrdata1, $sumbrcount1) = get_info_entry($entry1);
2306 ($testdata2, $sumcount2, $funcdata2, $checkdata2, $testfncdata2,
2307 $sumfnccount2, $testbrdata2, $sumbrcount2) = get_info_entry($entry2);
2310 $checkdata1 = merge_checksums($checkdata1, $checkdata2, $filename);
2313 $result_funcdata = merge_func_data($funcdata1, $funcdata2, $filename);
2315 # Combine function call count data
2316 $result_testfncdata = add_testfncdata($testfncdata1, $testfncdata2);
2317 ($result_sumfnccount, $f_found, $f_hit) =
2318 add_fnccount($sumfnccount1, $sumfnccount2);
2320 # Combine branch coverage data
2321 $result_testbrdata = add_testbrdata($testbrdata1, $testbrdata2);
2322 ($result_sumbrcount, $br_found, $br_hit) =
2323 combine_brcount($sumbrcount1, $sumbrcount2, $BR_ADD);
2326 foreach $testname (keys(%{$testdata1}))
2328 if (defined($testdata2->{$testname}))
2330 # testname is present in both entries, requires
2332 ($result_testdata{$testname}) =
2333 add_counts($testdata1->{$testname},
2334 $testdata2->{$testname});
2338 # testname only present in entry1, add to result
2339 $result_testdata{$testname} = $testdata1->{$testname};
2342 # update sum count hash
2343 ($result_sumcount, $lines_found, $lines_hit) =
2344 add_counts($result_sumcount,
2345 $result_testdata{$testname});
2348 foreach $testname (keys(%{$testdata2}))
2350 # Skip testnames already covered by previous iteration
2351 if (defined($testdata1->{$testname})) { next; }
2353 # testname only present in entry2, add to result hash
2354 $result_testdata{$testname} = $testdata2->{$testname};
2356 # update sum count hash
2357 ($result_sumcount, $lines_found, $lines_hit) =
2358 add_counts($result_sumcount,
2359 $result_testdata{$testname});
2362 # Calculate resulting sumcount
2365 set_info_entry(\%result, \%result_testdata, $result_sumcount,
2366 $result_funcdata, $checkdata1, $result_testfncdata,
2367 $result_sumfnccount, $result_testbrdata,
2368 $result_sumbrcount, $lines_found, $lines_hit,
2369 $f_found, $f_hit, $br_found, $br_hit);
2376 # combine_info_files(info_ref1, info_ref2)
2378 # Combine .info data in hashes referenced by INFO_REF1 and INFO_REF2. Return
2379 # reference to resulting hash.
2382 sub combine_info_files($$)
2384 my %hash1 = %{$_[0]};
2385 my %hash2 = %{$_[1]};
2388 foreach $filename (keys(%hash2))
2390 if ($hash1{$filename})
2392 # Entry already exists in hash1, combine them
2394 combine_info_entries($hash1{$filename},
2400 # Entry is unique in both hashes, simply add to
2402 $hash1{$filename} = $hash2{$filename};
2422 info("Combining tracefiles.\n");
2424 foreach $tracefile (@add_tracefile)
2426 $current_trace = read_info_file($tracefile);
2429 $total_trace = combine_info_files($total_trace,
2434 $total_trace = $current_trace;
2438 # Write combined data
2441 info("Writing data to $output_filename\n");
2442 open(INFO_HANDLE, ">$output_filename")
2443 or die("ERROR: cannot write to $output_filename!\n");
2444 @result = write_info_file(*INFO_HANDLE, $total_trace);
2445 close(*INFO_HANDLE);
2449 @result = write_info_file(*STDOUT, $total_trace);
2457 # write_info_file(filehandle, data)
2460 sub write_info_file(*$)
2462 local *INFO_HANDLE = $_[0];
2463 my %data = %{$_[1]};
2486 my $ln_total_found = 0;
2487 my $ln_total_hit = 0;
2488 my $fn_total_found = 0;
2489 my $fn_total_hit = 0;
2490 my $br_total_found = 0;
2491 my $br_total_hit = 0;
2493 foreach $source_file (sort(keys(%data)))
2495 $entry = $data{$source_file};
2496 ($testdata, $sumcount, $funcdata, $checkdata, $testfncdata,
2497 $sumfnccount, $testbrdata, $sumbrcount, $found, $hit,
2498 $f_found, $f_hit, $br_found, $br_hit) =
2499 get_info_entry($entry);
2502 $ln_total_found += $found;
2503 $ln_total_hit += $hit;
2504 $fn_total_found += $f_found;
2505 $fn_total_hit += $f_hit;
2506 $br_total_found += $br_found;
2507 $br_total_hit += $br_hit;
2509 foreach $testname (sort(keys(%{$testdata})))
2511 $testcount = $testdata->{$testname};
2512 $testfnccount = $testfncdata->{$testname};
2513 $testbrcount = $testbrdata->{$testname};
2517 print(INFO_HANDLE "TN:$testname\n");
2518 print(INFO_HANDLE "SF:$source_file\n");
2520 # Write function related data
2522 sort({$funcdata->{$a} <=> $funcdata->{$b}}
2523 keys(%{$funcdata})))
2525 print(INFO_HANDLE "FN:".$funcdata->{$func}.
2528 foreach $func (keys(%{$testfnccount})) {
2529 print(INFO_HANDLE "FNDA:".
2530 $testfnccount->{$func}.
2533 ($f_found, $f_hit) =
2534 get_func_found_and_hit($testfnccount);
2535 print(INFO_HANDLE "FNF:$f_found\n");
2536 print(INFO_HANDLE "FNH:$f_hit\n");
2538 # Write branch related data
2541 foreach $line (sort({$a <=> $b}
2542 keys(%{$testbrcount}))) {
2543 my $brdata = $testbrcount->{$line};
2544 my $num = br_ivec_len($brdata);
2547 for ($i = 0; $i < $num; $i++) {
2548 my ($block, $branch, $taken) =
2549 br_ivec_get($brdata, $i);
2551 print(INFO_HANDLE "BRDA:$line,$block,".
2552 "$branch,$taken\n");
2554 $br_hit++ if ($taken ne '-' &&
2558 if ($br_found > 0) {
2559 print(INFO_HANDLE "BRF:$br_found\n");
2560 print(INFO_HANDLE "BRH:$br_hit\n");
2563 # Write line related data
2564 foreach $line (sort({$a <=> $b} keys(%{$testcount})))
2566 print(INFO_HANDLE "DA:$line,".
2567 $testcount->{$line}.
2568 (defined($checkdata->{$line}) &&
2570 ",".$checkdata->{$line} : "")."\n");
2572 if ($testcount->{$line} > 0)
2578 print(INFO_HANDLE "LF:$found\n");
2579 print(INFO_HANDLE "LH:$hit\n");
2580 print(INFO_HANDLE "end_of_record\n");
2584 return ($ln_total_found, $ln_total_hit, $fn_total_found, $fn_total_hit,
2585 $br_total_found, $br_total_hit);
2590 # transform_pattern(pattern)
2592 # Transform shell wildcard expression to equivalent PERL regular expression.
2593 # Return transformed pattern.
2596 sub transform_pattern($)
2598 my $pattern = $_[0];
2600 # Escape special chars
2602 $pattern =~ s/\\/\\\\/g;
2603 $pattern =~ s/\//\\\//g;
2604 $pattern =~ s/\^/\\\^/g;
2605 $pattern =~ s/\$/\\\$/g;
2606 $pattern =~ s/\(/\\\(/g;
2607 $pattern =~ s/\)/\\\)/g;
2608 $pattern =~ s/\[/\\\[/g;
2609 $pattern =~ s/\]/\\\]/g;
2610 $pattern =~ s/\{/\\\{/g;
2611 $pattern =~ s/\}/\\\}/g;
2612 $pattern =~ s/\./\\\./g;
2613 $pattern =~ s/\,/\\\,/g;
2614 $pattern =~ s/\|/\\\|/g;
2615 $pattern =~ s/\+/\\\+/g;
2616 $pattern =~ s/\!/\\\!/g;
2618 # Transform ? => (.) and * => (.*)
2620 $pattern =~ s/\*/\(\.\*\)/g;
2621 $pattern =~ s/\?/\(\.\)/g;
2633 my $data = read_info_file($extract);
2642 # Need perlreg expressions instead of shell pattern
2643 @pattern_list = map({ transform_pattern($_); } @ARGV);
2645 # Filter out files which do not match any pattern
2646 foreach $filename (sort(keys(%{$data})))
2650 foreach $pattern (@pattern_list)
2652 $keep ||= ($filename =~ (/^$pattern$/));
2658 delete($data->{$filename});
2662 info("Extracting $filename\n"),
2667 # Write extracted data
2670 info("Extracted $extracted files\n");
2671 info("Writing data to $output_filename\n");
2672 open(INFO_HANDLE, ">$output_filename")
2673 or die("ERROR: cannot write to $output_filename!\n");
2674 @result = write_info_file(*INFO_HANDLE, $data);
2675 close(*INFO_HANDLE);
2679 @result = write_info_file(*STDOUT, $data);
2692 my $data = read_info_file($remove);
2701 # Need perlreg expressions instead of shell pattern
2702 @pattern_list = map({ transform_pattern($_); } @ARGV);
2704 # Filter out files that match the pattern
2705 foreach $filename (sort(keys(%{$data})))
2709 foreach $pattern (@pattern_list)
2711 $match_found ||= ($filename =~ (/$pattern$/));
2717 delete($data->{$filename});
2718 info("Removing $filename\n"),
2726 info("Deleted $removed files\n");
2727 info("Writing data to $output_filename\n");
2728 open(INFO_HANDLE, ">$output_filename")
2729 or die("ERROR: cannot write to $output_filename!\n");
2730 @result = write_info_file(*INFO_HANDLE, $data);
2731 close(*INFO_HANDLE);
2735 @result = write_info_file(*STDOUT, $data);
2742 # get_prefix(max_width, max_percentage_too_long, path_list)
2744 # Return a path prefix that satisfies the following requirements:
2745 # - is shared by more paths in path_list than any other prefix
2746 # - the percentage of paths which would exceed the given max_width length
2747 # after applying the prefix does not exceed max_percentage_too_long
2749 # If multiple prefixes satisfy all requirements, the longest prefix is
2750 # returned. Return an empty string if no prefix could be found.
2754 my ($max_width, $max_long, @path_list) = @_;
2761 foreach $path (@path_list) {
2762 my ($v, $d, $f) = splitpath($path);
2763 my @dirs = splitdir($d);
2764 my $p_len = length($path);
2767 # Remove trailing '/'
2768 pop(@dirs) if ($dirs[scalar(@dirs) - 1] eq '');
2769 for ($i = 0; $i < scalar(@dirs); $i++) {
2770 my $subpath = catpath($v, catdir(@dirs[0..$i]), '');
2771 my $entry = $prefix{$subpath};
2773 $entry = [ 0, 0 ] if (!defined($entry));
2774 $entry->[$ENTRY_NUM]++;
2775 if (($p_len - length($subpath) - 1) > $max_width) {
2776 $entry->[$ENTRY_LONG]++;
2778 $prefix{$subpath} = $entry;
2781 # Find suitable prefix (sort descending by two keys: 1. number of
2782 # entries covered by a prefix, 2. length of prefix)
2783 foreach $path (sort {($prefix{$a}->[$ENTRY_NUM] ==
2784 $prefix{$b}->[$ENTRY_NUM]) ?
2785 length($b) <=> length($a) :
2786 $prefix{$b}->[$ENTRY_NUM] <=>
2787 $prefix{$a}->[$ENTRY_NUM]}
2789 my ($num, $long) = @{$prefix{$path}};
2791 # Check for additional requirement: number of filenames
2792 # that would be too long may not exceed a certain percentage
2793 if ($long <= $num * $max_long / 100) {
2803 # shorten_filename(filename, width)
2805 # Truncate filename if it is longer than width characters.
2808 sub shorten_filename($$)
2810 my ($filename, $width) = @_;
2811 my $l = length($filename);
2815 return $filename if ($l <= $width);
2816 $e = int(($width - 3) / 2);
2817 $s = $width - 3 - $e;
2819 return substr($filename, 0, $s).'...'.substr($filename, $l - $e);
2823 sub shorten_number($$)
2825 my ($number, $width) = @_;
2826 my $result = sprintf("%*d", $width, $number);
2828 return $result if (length($result) <= $width);
2829 $number = $number / 1000;
2830 return $result if (length($result) <= $width);
2831 $result = sprintf("%*dk", $width - 1, $number);
2832 return $result if (length($result) <= $width);
2833 $number = $number / 1000;
2834 $result = sprintf("%*dM", $width - 1, $number);
2835 return $result if (length($result) <= $width);
2839 sub shorten_rate($$)
2841 my ($rate, $width) = @_;
2842 my $result = sprintf("%*.1f%%", $width - 3, $rate);
2844 return $result if (length($result) <= $width);
2845 $result = sprintf("%*d%%", $width - 1, $rate);
2846 return $result if (length($result) <= $width);
2856 my $data = read_info_file($list);
2865 my $total_found = 0;
2867 my $fn_total_found = 0;
2868 my $fn_total_hit = 0;
2869 my $br_total_found = 0;
2870 my $br_total_hit = 0;
2872 my $strlen = length("Filename");
2888 my @fwidth_narrow = (5, 5, 3, 5, 4, 5);
2889 my @fwidth_wide = (6, 5, 5, 5, 6, 5);
2890 my @fwidth = @fwidth_wide;
2892 my $max_width = $opt_list_width;
2893 my $max_long = $opt_list_truncate_max;
2894 my $fwidth_narrow_length;
2895 my $fwidth_wide_length;
2897 my $root_prefix = 0;
2899 # Calculate total width of narrow fields
2900 $fwidth_narrow_length = 0;
2901 foreach $w (@fwidth_narrow) {
2902 $fwidth_narrow_length += $w + 1;
2904 # Calculate total width of wide fields
2905 $fwidth_wide_length = 0;
2906 foreach $w (@fwidth_wide) {
2907 $fwidth_wide_length += $w + 1;
2909 # Get common file path prefix
2910 $prefix = get_prefix($max_width - $fwidth_narrow_length, $max_long,
2912 $root_prefix = 1 if ($prefix eq rootdir());
2913 $got_prefix = 1 if (length($prefix) > 0);
2915 # Get longest filename length
2916 foreach $filename (keys(%{$data})) {
2917 if (!$opt_list_full_path) {
2918 if (!$got_prefix || !$root_prefix &&
2919 !($filename =~ s/^\Q$prefix\/\E//)) {
2920 my ($v, $d, $f) = splitpath($filename);
2925 # Determine maximum length of entries
2926 if (length($filename) > $strlen) {
2927 $strlen = length($filename)
2930 if (!$opt_list_full_path) {
2933 $w = $fwidth_wide_length;
2934 # Check if all columns fit into max_width characters
2935 if ($strlen + $fwidth_wide_length > $max_width) {
2937 @fwidth = @fwidth_narrow;
2938 $w = $fwidth_narrow_length;
2939 if (($strlen + $fwidth_narrow_length) > $max_width) {
2940 # Truncate filenames at max width
2941 $strlen = $max_width - $fwidth_narrow_length;
2944 # Add some blanks between filename and fields if possible
2945 $blanks = int($strlen * 0.5);
2946 $blanks = 4 if ($blanks < 4);
2947 $blanks = 8 if ($blanks > 8);
2948 if (($strlen + $w + $blanks) < $max_width) {
2951 $strlen = $max_width - $w;
2956 $format = "%-${w}s|";
2957 $heading1 = sprintf("%*s|", $w, "");
2958 $heading2 = sprintf("%-*s|", $w, "Filename");
2960 # Line coverage rate
2961 $w = $fwidth[$F_LN_RATE];
2962 $format .= "%${w}s ";
2963 $heading1 .= sprintf("%-*s |", $w + $fwidth[$F_LN_NUM],
2965 $heading2 .= sprintf("%-*s ", $w, "Rate");
2968 $w = $fwidth[$F_LN_NUM];
2969 $format .= "%${w}s|";
2970 $heading2 .= sprintf("%*s|", $w, "Num");
2972 # Function coverage rate
2973 $w = $fwidth[$F_FN_RATE];
2974 $format .= "%${w}s ";
2975 $heading1 .= sprintf("%-*s|", $w + $fwidth[$F_FN_NUM] + 1,
2977 $heading2 .= sprintf("%-*s ", $w, "Rate");
2979 # Number of functions
2980 $w = $fwidth[$F_FN_NUM];
2981 $format .= "%${w}s|";
2982 $heading2 .= sprintf("%*s|", $w, "Num");
2984 # Branch coverage rate
2985 $w = $fwidth[$F_BR_RATE];
2986 $format .= "%${w}s ";
2987 $heading1 .= sprintf("%-*s", $w + $fwidth[$F_BR_NUM] + 1,
2989 $heading2 .= sprintf("%-*s ", $w, "Rate");
2991 # Number of branches
2992 $w = $fwidth[$F_BR_NUM];
2993 $format .= "%${w}s";
2994 $heading2 .= sprintf("%*s", $w, "Num");
3004 print(("="x$barlen)."\n");
3006 # Print per file information
3007 foreach $filename (sort(keys(%{$data})))
3010 my $print_filename = $filename;
3012 $entry = $data->{$filename};
3013 if (!$opt_list_full_path) {
3016 $print_filename = $filename;
3017 if (!$got_prefix || !$root_prefix &&
3018 !($print_filename =~ s/^\Q$prefix\/\E//)) {
3019 my ($v, $d, $f) = splitpath($filename);
3021 $p = catpath($v, $d, "");
3023 $print_filename = $f;
3028 if (!defined($lastpath) || $lastpath ne $p) {
3029 print("\n") if (defined($lastpath));
3031 print("[$lastpath/]\n") if (!$root_prefix);
3033 $print_filename = shorten_filename($print_filename,
3037 (undef, undef, undef, undef, undef, undef, undef, undef,
3038 $found, $hit, $fn_found, $fn_hit, $br_found, $br_hit) =
3039 get_info_entry($entry);
3041 # Assume zero count if there is no function data for this file
3042 if (!defined($fn_found) || !defined($fn_hit)) {
3046 # Assume zero count if there is no branch data for this file
3047 if (!defined($br_found) || !defined($br_hit)) {
3052 # Add line coverage totals
3053 $total_found += $found;
3055 # Add function coverage totals
3056 $fn_total_found += $fn_found;
3057 $fn_total_hit += $fn_hit;
3058 # Add branch coverage totals
3059 $br_total_found += $br_found;
3060 $br_total_hit += $br_hit;
3062 # Determine line coverage rate for this file
3066 $rate = shorten_rate(100 * $hit / $found,
3067 $fwidth[$F_LN_RATE]);
3069 # Determine function coverage rate for this file
3070 if (!defined($fn_found) || $fn_found == 0) {
3073 $fnrate = shorten_rate(100 * $fn_hit / $fn_found,
3074 $fwidth[$F_FN_RATE]);
3076 # Determine branch coverage rate for this file
3077 if (!defined($br_found) || $br_found == 0) {
3080 $brrate = shorten_rate(100 * $br_hit / $br_found,
3081 $fwidth[$F_BR_RATE]);
3084 # Assemble line parameters
3085 push(@file_data, $print_filename);
3086 push(@file_data, $rate);
3087 push(@file_data, shorten_number($found, $fwidth[$F_LN_NUM]));
3088 push(@file_data, $fnrate);
3089 push(@file_data, shorten_number($fn_found, $fwidth[$F_FN_NUM]));
3090 push(@file_data, $brrate);
3091 push(@file_data, shorten_number($br_found, $fwidth[$F_BR_NUM]));
3093 # Print assembled line
3094 printf($format, @file_data);
3097 # Determine total line coverage rate
3098 if ($total_found == 0) {
3101 $rate = shorten_rate(100 * $total_hit / $total_found,
3102 $fwidth[$F_LN_RATE]);
3104 # Determine total function coverage rate
3105 if ($fn_total_found == 0) {
3108 $fnrate = shorten_rate(100 * $fn_total_hit / $fn_total_found,
3109 $fwidth[$F_FN_RATE]);
3111 # Determine total branch coverage rate
3112 if ($br_total_found == 0) {
3115 $brrate = shorten_rate(100 * $br_total_hit / $br_total_found,
3116 $fwidth[$F_BR_RATE]);
3120 print(("="x$barlen)."\n");
3122 # Assemble line parameters
3123 push(@footer, sprintf("%*s", $strlen, "Total:"));
3124 push(@footer, $rate);
3125 push(@footer, shorten_number($total_found, $fwidth[$F_LN_NUM]));
3126 push(@footer, $fnrate);
3127 push(@footer, shorten_number($fn_total_found, $fwidth[$F_FN_NUM]));
3128 push(@footer, $brrate);
3129 push(@footer, shorten_number($br_total_found, $fwidth[$F_BR_NUM]));
3131 # Print assembled line
3132 printf($format, @footer);
3137 # get_common_filename(filename1, filename2)
3139 # Check for filename components which are common to FILENAME1 and FILENAME2.
3140 # Upon success, return
3142 # (common, path1, path2)
3144 # or 'undef' in case there are no such parts.
3147 sub get_common_filename($$)
3149 my @list1 = split("/", $_[0]);
3150 my @list2 = split("/", $_[1]);
3153 # Work in reverse order, i.e. beginning with the filename itself
3154 while (@list1 && @list2 && ($list1[$#list1] eq $list2[$#list2]))
3156 unshift(@result, pop(@list1));
3160 # Did we find any similarities?
3161 if (scalar(@result) > 0)
3163 return (join("/", @result), join("/", @list1),
3174 # strip_directories($path, $depth)
3176 # Remove DEPTH leading directory levels from PATH.
3179 sub strip_directories($$)
3181 my $filename = $_[0];
3185 if (!defined($depth) || ($depth < 1))
3189 for ($i = 0; $i < $depth; $i++)
3191 $filename =~ s/^[^\/]*\/+(.*)$/$1/;
3198 # read_diff(filename)
3200 # Read diff output from FILENAME to memory. The diff file has to follow the
3201 # format generated by 'diff -u'. Returns a list of hash references:
3203 # (mapping, path mapping)
3205 # mapping: filename -> reference to line hash
3206 # line hash: line number in new file -> corresponding line number in old file
3208 # path mapping: filename -> old filename
3210 # Die in case of error.
3215 my $diff_file = $_[0]; # Name of diff file
3216 my %diff; # Resulting mapping filename -> line hash
3217 my %paths; # Resulting mapping old path -> new path
3218 my $mapping; # Reference to current line hash
3219 my $line; # Contents of current line
3220 my $num_old; # Current line number in old file
3221 my $num_new; # Current line number in new file
3222 my $file_old; # Name of old file in diff section
3223 my $file_new; # Name of new file in diff section
3224 my $filename; # Name of common filename of diff section
3225 my $in_block = 0; # Non-zero while we are inside a diff block
3226 local *HANDLE; # File handle for reading the diff file
3228 info("Reading diff $diff_file\n");
3230 # Check if file exists and is readable
3234 die("ERROR: cannot read file $diff_file!\n");
3237 # Check if this is really a plain file
3240 die("ERROR: not a plain file: $diff_file!\n");
3243 # Check for .gz extension
3244 if ($diff_file =~ /\.gz$/)
3246 # Check for availability of GZIP tool
3247 system_no_output(1, "gunzip", "-h")
3248 and die("ERROR: gunzip command not available!\n");
3250 # Check integrity of compressed file
3251 system_no_output(1, "gunzip", "-t", $diff_file)
3252 and die("ERROR: integrity check failed for ".
3253 "compressed file $diff_file!\n");
3255 # Open compressed file
3256 open(HANDLE, "gunzip -c $diff_file|")
3257 or die("ERROR: cannot start gunzip to decompress ".
3262 # Open decompressed file
3263 open(HANDLE, $diff_file)
3264 or die("ERROR: cannot read file $_[0]!\n");
3267 # Parse diff file line by line
3275 # Filename of old file:
3276 # --- <filename> <date>
3279 $file_old = strip_directories($1, $strip);
3282 # Filename of new file:
3283 # +++ <filename> <date>
3284 /^\+\+\+ (\S+)/ && do
3286 # Add last file to resulting hash
3290 $diff{$filename} = $mapping;
3291 $mapping = \%new_hash;
3293 $file_new = strip_directories($1, $strip);
3294 $filename = $file_old;
3295 $paths{$filename} = $file_new;
3300 # Start of diff block:
3301 # @@ -old_start,old_num, +new_start,new_num @@
3302 /^\@\@\s+-(\d+),(\d+)\s+\+(\d+),(\d+)\s+\@\@$/ && do
3305 while ($num_old < $1)
3307 $mapping->{$num_new} = $num_old;
3314 # <line starts with blank>
3321 $mapping->{$num_new} = $num_old;
3326 # Line as seen in old file
3327 # <line starts with '-'>
3337 # Line as seen in new file
3338 # <line starts with '+'>
3355 $mapping->{$num_new} = $num_old;
3365 # Add final diff file section to resulting hash
3368 $diff{$filename} = $mapping;
3373 die("ERROR: no valid diff data found in $diff_file!\n".
3374 "Make sure to use 'diff -u' when generating the diff ".
3377 return (\%diff, \%paths);
3382 # apply_diff($count_data, $line_hash)
3384 # Transform count data using a mapping of lines:
3386 # $count_data: reference to hash: line number -> data
3387 # $line_hash: reference to hash: line number new -> line number old
3389 # Return a reference to transformed count data.
3394 my $count_data = $_[0]; # Reference to data hash: line -> hash
3395 my $line_hash = $_[1]; # Reference to line hash: new line -> old line
3396 my %result; # Resulting hash
3397 my $last_new = 0; # Last new line number found in line hash
3398 my $last_old = 0; # Last old line number found in line hash
3400 # Iterate all new line numbers found in the diff
3401 foreach (sort({$a <=> $b} keys(%{$line_hash})))
3404 $last_old = $line_hash->{$last_new};
3406 # Is there data associated with the corresponding old line?
3407 if (defined($count_data->{$line_hash->{$_}}))
3409 # Copy data to new hash with a new line number
3410 $result{$_} = $count_data->{$line_hash->{$_}};
3413 # Transform all other lines which come after the last diff entry
3414 foreach (sort({$a <=> $b} keys(%{$count_data})))
3416 if ($_ <= $last_old)
3418 # Skip lines which were covered by line hash
3421 # Copy data to new hash with an offset
3422 $result{$_ + ($last_new - $last_old)} = $count_data->{$_};
3430 # apply_diff_to_brcount(brcount, linedata)
3432 # Adjust line numbers of branch coverage data according to linedata.
3435 sub apply_diff_to_brcount($$)
3437 my ($brcount, $linedata) = @_;
3440 # Convert brcount to db format
3441 $db = brcount_to_db($brcount);
3442 # Apply diff to db format
3443 $db = apply_diff($db, $linedata);
3444 # Convert db format back to brcount format
3445 ($brcount) = db_to_brcount($db);
3452 # get_hash_max(hash_ref)
3454 # Return the highest integer key from hash.
3462 foreach (keys(%{$hash})) {
3463 if (!defined($max)) {
3465 } elsif ($hash->{$_} > $max) {
3472 sub get_hash_reverse($)
3477 foreach (keys(%{$hash})) {
3478 $result{$hash->{$_}} = $_;
3485 # apply_diff_to_funcdata(funcdata, line_hash)
3488 sub apply_diff_to_funcdata($$)
3490 my ($funcdata, $linedata) = @_;
3491 my $last_new = get_hash_max($linedata);
3492 my $last_old = $linedata->{$last_new};
3495 my $line_diff = get_hash_reverse($linedata);
3497 foreach $func (keys(%{$funcdata})) {
3498 my $line = $funcdata->{$func};
3500 if (defined($line_diff->{$line})) {
3501 $result{$func} = $line_diff->{$line};
3502 } elsif ($line > $last_old) {
3503 $result{$func} = $line + $last_new - $last_old;
3512 # get_line_hash($filename, $diff_data, $path_data)
3514 # Find line hash in DIFF_DATA which matches FILENAME. On success, return list
3515 # line hash. or undef in case of no match. Die if more than one line hashes in
3519 sub get_line_hash($$$)
3521 my $filename = $_[0];
3522 my $diff_data = $_[1];
3523 my $path_data = $_[2];
3532 # Remove trailing slash from diff path
3533 $diff_path =~ s/\/$//;
3534 foreach (keys(%{$diff_data}))
3538 $sep = '/' if (!/^\//);
3540 # Try to match diff filename with filename
3541 if ($filename =~ /^\Q$diff_path$sep$_\E$/)
3545 # Two files match, choose the more specific one
3546 # (the one with more path components)
3547 $old_depth = ($diff_name =~ tr/\///);
3548 $new_depth = (tr/\///);
3549 if ($old_depth == $new_depth)
3551 die("ERROR: diff file contains ".
3552 "ambiguous entries for ".
3555 elsif ($new_depth > $old_depth)
3568 # Get converted path
3569 if ($filename =~ /^(.*)$diff_name$/)
3571 ($common, $old_path, $new_path) =
3572 get_common_filename($filename,
3573 $1.$path_data->{$diff_name});
3575 return ($diff_data->{$diff_name}, $old_path, $new_path);
3585 # convert_paths(trace_data, path_conversion_data)
3587 # Rename all paths in TRACE_DATA which show up in PATH_CONVERSION_DATA.
3590 sub convert_paths($$)
3592 my $trace_data = $_[0];
3593 my $path_conversion_data = $_[1];
3597 if (scalar(keys(%{$path_conversion_data})) == 0)
3599 info("No path conversion data available.\n");
3603 # Expand path conversion list
3604 foreach $filename (keys(%{$path_conversion_data}))
3606 $new_path = $path_conversion_data->{$filename};
3607 while (($filename =~ s/^(.*)\/[^\/]+$/$1/) &&
3608 ($new_path =~ s/^(.*)\/[^\/]+$/$1/) &&
3609 ($filename ne $new_path))
3611 $path_conversion_data->{$filename} = $new_path;
3616 FILENAME: foreach $filename (keys(%{$trace_data}))
3618 # Find a path in our conversion table that matches, starting
3619 # with the longest path
3620 foreach (sort({length($b) <=> length($a)}
3621 keys(%{$path_conversion_data})))
3623 # Is this path a prefix of our filename?
3624 if (!($filename =~ /^$_(.*)$/))
3628 $new_path = $path_conversion_data->{$_}.$1;
3630 # Make sure not to overwrite an existing entry under
3632 if ($trace_data->{$new_path})
3634 # Need to combine entries
3635 $trace_data->{$new_path} =
3636 combine_info_entries(
3637 $trace_data->{$filename},
3638 $trace_data->{$new_path},
3643 # Simply rename entry
3644 $trace_data->{$new_path} =
3645 $trace_data->{$filename};
3647 delete($trace_data->{$filename});
3650 info("No conversion available for filename $filename\n");
3655 # sub adjust_fncdata(funcdata, testfncdata, sumfnccount)
3657 # Remove function call count data from testfncdata and sumfnccount which
3658 # is no longer present in funcdata.
3661 sub adjust_fncdata($$$)
3663 my ($funcdata, $testfncdata, $sumfnccount) = @_;
3669 # Remove count data in testfncdata for functions which are no longer
3671 foreach $testname (%{$testfncdata}) {
3672 my $fnccount = $testfncdata->{$testname};
3674 foreach $func (%{$fnccount}) {
3675 if (!defined($funcdata->{$func})) {
3676 delete($fnccount->{$func});
3680 # Remove count data in sumfnccount for functions which are no longer
3682 foreach $func (%{$sumfnccount}) {
3683 if (!defined($funcdata->{$func})) {
3684 delete($sumfnccount->{$func});
3690 # get_func_found_and_hit(sumfnccount)
3692 # Return (f_found, f_hit) for sumfnccount
3695 sub get_func_found_and_hit($)
3697 my ($sumfnccount) = @_;
3702 $f_found = scalar(keys(%{$sumfnccount}));
3704 foreach $function (keys(%{$sumfnccount})) {
3705 if ($sumfnccount->{$function} > 0) {
3709 return ($f_found, $f_hit);
3718 my $trace_data = read_info_file($diff);
3723 my %path_conversion_data;
3748 ($diff_data, $path_data) = read_diff($ARGV[0]);
3750 foreach $filename (sort(keys(%{$trace_data})))
3752 # Find a diff section corresponding to this file
3753 ($line_hash, $old_path, $new_path) =
3754 get_line_hash($filename, $diff_data, $path_data);
3757 # There's no diff section for this file
3762 if ($old_path && $new_path && ($old_path ne $new_path))
3764 $path_conversion_data{$old_path} = $new_path;
3766 # Check for deleted files
3767 if (scalar(keys(%{$line_hash})) == 0)
3769 info("Removing $filename\n");
3770 delete($trace_data->{$filename});
3773 info("Converting $filename\n");
3774 $entry = $trace_data->{$filename};
3775 ($testdata, $sumcount, $funcdata, $checkdata, $testfncdata,
3776 $sumfnccount, $testbrdata, $sumbrcount) =
3777 get_info_entry($entry);
3779 foreach $testname (keys(%{$testdata}))
3781 # Adjust line numbers of line coverage data
3782 $testdata->{$testname} =
3783 apply_diff($testdata->{$testname}, $line_hash);
3784 # Adjust line numbers of branch coverage data
3785 $testbrdata->{$testname} =
3786 apply_diff_to_brcount($testbrdata->{$testname},
3788 # Remove empty sets of test data
3789 if (scalar(keys(%{$testdata->{$testname}})) == 0)
3791 delete($testdata->{$testname});
3792 delete($testfncdata->{$testname});
3793 delete($testbrdata->{$testname});
3796 # Rename test data to indicate conversion
3797 foreach $testname (keys(%{$testdata}))
3799 # Skip testnames which already contain an extension
3800 if ($testname =~ /,[^,]+$/)
3804 # Check for name conflict
3805 if (defined($testdata->{$testname.",diff"}))
3808 ($testdata->{$testname}) = add_counts(
3809 $testdata->{$testname},
3810 $testdata->{$testname.",diff"});
3811 delete($testdata->{$testname.",diff"});
3812 # Add function call counts
3813 ($testfncdata->{$testname}) = add_fnccount(
3814 $testfncdata->{$testname},
3815 $testfncdata->{$testname.",diff"});
3816 delete($testfncdata->{$testname.",diff"});
3818 ($testbrdata->{$testname}) = combine_brcount(
3819 $testbrdata->{$testname},
3820 $testbrdata->{$testname.",diff"},
3822 delete($testbrdata->{$testname.",diff"});
3824 # Move test data to new testname
3825 $testdata->{$testname.",diff"} = $testdata->{$testname};
3826 delete($testdata->{$testname});
3827 # Move function call count data to new testname
3828 $testfncdata->{$testname.",diff"} =
3829 $testfncdata->{$testname};
3830 delete($testfncdata->{$testname});
3831 # Move branch count data to new testname
3832 $testbrdata->{$testname.",diff"} =
3833 $testbrdata->{$testname};
3834 delete($testbrdata->{$testname});
3836 # Convert summary of test data
3837 $sumcount = apply_diff($sumcount, $line_hash);
3838 # Convert function data
3839 $funcdata = apply_diff_to_funcdata($funcdata, $line_hash);
3840 # Convert branch coverage data
3841 $sumbrcount = apply_diff_to_brcount($sumbrcount, $line_hash);
3842 # Update found/hit numbers
3843 # Convert checksum data
3844 $checkdata = apply_diff($checkdata, $line_hash);
3845 # Convert function call count data
3846 adjust_fncdata($funcdata, $testfncdata, $sumfnccount);
3847 ($f_found, $f_hit) = get_func_found_and_hit($sumfnccount);
3848 ($br_found, $br_hit) = get_br_found_and_hit($sumbrcount);
3849 # Update found/hit numbers
3852 foreach (keys(%{$sumcount}))
3855 if ($sumcount->{$_} > 0)
3862 # Store converted entry
3863 set_info_entry($entry, $testdata, $sumcount, $funcdata,
3864 $checkdata, $testfncdata, $sumfnccount,
3865 $testbrdata, $sumbrcount, $found, $hit,
3866 $f_found, $f_hit, $br_found, $br_hit);
3870 # Remove empty data set
3871 delete($trace_data->{$filename});
3875 # Convert filenames as well if requested
3876 if ($convert_filenames)
3878 convert_paths($trace_data, \%path_conversion_data);
3881 info("$converted entr".($converted != 1 ? "ies" : "y")." converted, ".
3882 "$unchanged entr".($unchanged != 1 ? "ies" : "y")." left ".
3888 info("Writing data to $output_filename\n");
3889 open(INFO_HANDLE, ">$output_filename")
3890 or die("ERROR: cannot write to $output_filename!\n");
3891 @result = write_info_file(*INFO_HANDLE, $trace_data);
3892 close(*INFO_HANDLE);
3896 @result = write_info_file(*STDOUT, $trace_data);
3904 # system_no_output(mode, parameters)
3906 # Call an external program using PARAMETERS while suppressing depending on
3907 # the value of MODE:
3909 # MODE & 1: suppress STDOUT
3910 # MODE & 2: suppress STDERR
3912 # Return 0 on success, non-zero otherwise.
3915 sub system_no_output($@)
3922 # Save old stdout and stderr handles
3923 ($mode & 1) && open(OLD_STDOUT, ">>&STDOUT");
3924 ($mode & 2) && open(OLD_STDERR, ">>&STDERR");
3926 # Redirect to /dev/null
3927 ($mode & 1) && open(STDOUT, ">/dev/null");
3928 ($mode & 2) && open(STDERR, ">/dev/null");
3933 # Close redirected handles
3934 ($mode & 1) && close(STDOUT);
3935 ($mode & 2) && close(STDERR);
3937 # Restore old handles
3938 ($mode & 1) && open(STDOUT, ">>&OLD_STDOUT");
3939 ($mode & 2) && open(STDERR, ">>&OLD_STDERR");
3946 # read_config(filename)
3948 # Read configuration file FILENAME and return a reference to a hash containing
3949 # all valid key=value pairs found.
3954 my $filename = $_[0];
3960 if (!open(HANDLE, "<$filename"))
3962 warn("WARNING: cannot read configuration file $filename\n");
3970 # Remove leading blanks
3972 # Remove trailing blanks
3975 ($key, $value) = split(/\s*=\s*/, $_, 2);
3976 if (defined($key) && defined($value))
3978 $result{$key} = $value;
3982 warn("WARNING: malformed statement in line $. ".
3983 "of configuration file $filename\n");
3994 # REF is a reference to a hash containing the following mapping:
3996 # key_string => var_ref
3998 # where KEY_STRING is a keyword and VAR_REF is a reference to an associated
3999 # variable. If the global configuration hash CONFIG contains a value for
4000 # keyword KEY_STRING, VAR_REF will be assigned the value for that keyword.
4007 foreach (keys(%{$ref}))
4009 if (defined($config->{$_}))
4011 ${$ref->{$_}} = $config->{$_};
4021 warn("$tool_name: $msg");
4029 die("$tool_name: $msg");
4032 sub abort_handler($)
4041 info("Removing temporary directories.\n");
4042 foreach (@temp_dirs) {
4051 system_no_output(3, "mount", "-t", "debugfs", "nodev",
4052 "/sys/kernel/debug");
4055 sub setup_gkv_proc()
4057 if (system_no_output(3, "modprobe", "gcov_proc")) {
4058 system_no_output(3, "modprobe", "gcov_prof");
4062 sub check_gkv_sys($)
4066 if (-e "$dir/reset") {
4072 sub check_gkv_proc($)
4076 if (-e "$dir/vmlinux") {
4085 my $sys_dir = "/sys/kernel/debug/gcov";
4086 my $proc_dir = "/proc/gcov";
4089 if (!defined($gcov_dir)) {
4090 info("Auto-detecting gcov kernel support.\n");
4091 @todo = ( "cs", "cp", "ss", "cs", "sp", "cp" );
4092 } elsif ($gcov_dir =~ /proc/) {
4093 info("Checking gcov kernel support at $gcov_dir ".
4094 "(user-specified).\n");
4095 @todo = ( "cp", "sp", "cp", "cs", "ss", "cs");
4097 info("Checking gcov kernel support at $gcov_dir ".
4098 "(user-specified).\n");
4099 @todo = ( "cs", "ss", "cs", "cp", "sp", "cp", );
4104 $dir = defined($gcov_dir) ? $gcov_dir : $sys_dir;
4105 if (check_gkv_sys($dir)) {
4106 info("Found ".$GKV_NAME[$GKV_SYS]." gcov ".
4107 "kernel support at $dir\n");
4108 return ($GKV_SYS, $dir);
4110 } elsif ($_ eq "cp") {
4112 $dir = defined($gcov_dir) ? $gcov_dir : $proc_dir;
4113 if (check_gkv_proc($dir)) {
4114 info("Found ".$GKV_NAME[$GKV_PROC]." gcov ".
4115 "kernel support at $dir\n");
4116 return ($GKV_PROC, $dir);
4118 } elsif ($_ eq "ss") {
4121 } elsif ($_ eq "sp") {
4126 if (defined($gcov_dir)) {
4127 die("ERROR: could not find gcov kernel data at $gcov_dir\n");
4129 die("ERROR: no gcov kernel data found\n");
4135 # get_overall_line(found, hit, name_singular, name_plural)
4137 # Return a string containing overall information for the specified
4141 sub get_overall_line($$$$)
4143 my ($found, $hit, $name_sn, $name_pl) = @_;
4146 return "no data found" if (!defined($found) || $found == 0);
4147 $name = ($found == 1) ? $name_sn : $name_pl;
4148 return sprintf("%.1f%% (%d of %d %s)", $hit * 100 / $found, $hit,
4154 # print_overall_rate(ln_do, ln_found, ln_hit, fn_do, fn_found, fn_hit, br_do
4157 # Print overall coverage rates for the specified coverage types.
4160 sub print_overall_rate($$$$$$$$$)
4162 my ($ln_do, $ln_found, $ln_hit, $fn_do, $fn_found, $fn_hit,
4163 $br_do, $br_found, $br_hit) = @_;
4165 info("Overall coverage rate:\n");
4166 info(" lines......: %s\n",
4167 get_overall_line($ln_found, $ln_hit, "line", "lines"))
4169 info(" functions..: %s\n",
4170 get_overall_line($fn_found, $fn_hit, "function", "functions"))
4172 info(" branches...: %s\n",
4173 get_overall_line($br_found, $br_hit, "branch", "branches"))