-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeninfo
executable file
·3072 lines (2682 loc) · 67.6 KB
/
geninfo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
#
# Copyright (c) International Business Machines Corp., 2002,2010
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# geninfo
#
# This script generates .info files from data files as created by code
# instrumented with gcc's built-in profiling mechanism. Call it with
# --help and refer to the geninfo man page to get information on usage
# and available options.
#
#
# Authors:
# 2002-08-23 created by Peter Oberparleiter <[email protected]>
# IBM Lab Boeblingen
# based on code by Manoj Iyer <[email protected]> and
# Megan Bock <[email protected]>
# IBM Austin
# 2002-09-05 / Peter Oberparleiter: implemented option that allows file list
# 2003-04-16 / Peter Oberparleiter: modified read_gcov so that it can also
# parse the new gcov format which is to be introduced in gcc 3.3
# 2003-04-30 / Peter Oberparleiter: made info write to STDERR, not STDOUT
# 2003-07-03 / Peter Oberparleiter: added line checksum support, added
# --no-checksum
# 2003-09-18 / Nigel Hinds: capture branch coverage data from GCOV
# 2003-12-11 / Laurent Deniel: added --follow option
# workaround gcov (<= 3.2.x) bug with empty .da files
# 2004-01-03 / Laurent Deniel: Ignore empty .bb files
# 2004-02-16 / Andreas Krebbel: Added support for .gcno/.gcda files and
# gcov versioning
# 2004-08-09 / Peter Oberparleiter: added configuration file support
# 2008-07-14 / Tom Zoerner: added --function-coverage command line option
# 2008-08-13 / Peter Oberparleiter: modified function coverage
# implementation (now enabled per default)
#
use strict;
use File::Basename;
use File::Spec::Functions qw /abs2rel catdir file_name_is_absolute splitdir
splitpath/;
use Getopt::Long;
use Digest::MD5 qw(md5_base64);
# Constants
our $lcov_version = 'LCOV version 1.9';
our $lcov_url = "http://ltp.sourceforge.net/coverage/lcov.php";
our $gcov_tool = "gcov";
our $tool_name = basename($0);
our $GCOV_VERSION_3_4_0 = 0x30400;
our $GCOV_VERSION_3_3_0 = 0x30300;
our $GCNO_FUNCTION_TAG = 0x01000000;
our $GCNO_LINES_TAG = 0x01450000;
our $GCNO_FILE_MAGIC = 0x67636e6f;
our $BBG_FILE_MAGIC = 0x67626267;
our $COMPAT_HAMMER = "hammer";
our $ERROR_GCOV = 0;
our $ERROR_SOURCE = 1;
our $ERROR_GRAPH = 2;
our $EXCL_START = "LCOV_EXCL_START";
our $EXCL_STOP = "LCOV_EXCL_STOP";
our $EXCL_LINE = "LCOV_EXCL_LINE";
our $BR_LINE = 0;
our $BR_BLOCK = 1;
our $BR_BRANCH = 2;
our $BR_TAKEN = 3;
our $BR_VEC_ENTRIES = 4;
our $BR_VEC_WIDTH = 32;
our $UNNAMED_BLOCK = 9999;
# Prototypes
sub print_usage(*);
sub gen_info($);
sub process_dafile($$);
sub match_filename($@);
sub solve_ambiguous_match($$$);
sub split_filename($);
sub solve_relative_path($$);
sub read_gcov_header($);
sub read_gcov_file($);
sub info(@);
sub get_gcov_version();
sub system_no_output($@);
sub read_config($);
sub apply_config($);
sub get_exclusion_data($);
sub apply_exclusion_data($$);
sub process_graphfile($$);
sub filter_fn_name($);
sub warn_handler($);
sub die_handler($);
sub graph_error($$);
sub graph_expect($);
sub graph_read(*$;$);
sub graph_skip(*$;$);
sub sort_uniq(@);
sub sort_uniq_lex(@);
sub graph_cleanup($);
sub graph_find_base($);
sub graph_from_bb($$$);
sub graph_add_order($$$);
sub read_bb_word(*;$);
sub read_bb_value(*;$);
sub read_bb_string(*$);
sub read_bb($$);
sub read_bbg_word(*;$);
sub read_bbg_value(*;$);
sub read_bbg_string(*);
sub read_bbg_lines_record(*$$$$$$);
sub read_bbg($$);
sub read_gcno_word(*;$);
sub read_gcno_value(*$;$);
sub read_gcno_string(*$);
sub read_gcno_lines_record(*$$$$$$$);
sub read_gcno_function_record(*$$$$);
sub read_gcno($$);
sub get_gcov_capabilities();
sub get_overall_line($$$$);
sub print_overall_rate($$$$$$$$$);
sub br_gvec_len($);
sub br_gvec_get($$);
sub debug($);
sub int_handler();
# Global variables
our $gcov_version;
our $graph_file_extension;
our $data_file_extension;
our @data_directory;
our $test_name = "";
our $quiet;
our $help;
our $output_filename;
our $base_directory;
our $version;
our $follow;
our $checksum;
our $no_checksum;
our $compat_libtool;
our $no_compat_libtool;
our $adjust_testname;
our $config; # Configuration file contents
our $compatibility; # Compatibility version flag - used to indicate
# non-standard GCOV data format versions
our @ignore_errors; # List of errors to ignore (parameter)
our @ignore; # List of errors to ignore (array)
our $initial;
our $no_recursion = 0;
our $maxdepth;
our $no_markers = 0;
our $opt_derive_func_data = 0;
our $debug = 0;
our $gcov_caps;
our @gcov_options;
our $cwd = `pwd`;
chomp($cwd);
#
# Code entry point
#
# Register handler routine to be called when interrupted
$SIG{"INT"} = \&int_handler;
$SIG{__WARN__} = \&warn_handler;
$SIG{__DIE__} = \&die_handler;
# Prettify version string
$lcov_version =~ s/\$\s*Revision\s*:?\s*(\S+)\s*\$/$1/;
# Set LANG so that gcov output will be in a unified format
$ENV{"LANG"} = "C";
# Read configuration file if available
if (defined($ENV{"HOME"}) && (-r $ENV{"HOME"}."/.lcovrc"))
{
$config = read_config($ENV{"HOME"}."/.lcovrc");
}
elsif (-r "/etc/lcovrc")
{
$config = read_config("/etc/lcovrc");
}
if ($config)
{
# Copy configuration file values to variables
apply_config({
"geninfo_gcov_tool" => \$gcov_tool,
"geninfo_adjust_testname" => \$adjust_testname,
"geninfo_checksum" => \$checksum,
"geninfo_no_checksum" => \$no_checksum, # deprecated
"geninfo_compat_libtool" => \$compat_libtool});
# Merge options
if (defined($no_checksum))
{
$checksum = ($no_checksum ? 0 : 1);
$no_checksum = undef;
}
}
# Parse command line options
if (!GetOptions("test-name|t=s" => \$test_name,
"output-filename|o=s" => \$output_filename,
"checksum" => \$checksum,
"no-checksum" => \$no_checksum,
"base-directory|b=s" => \$base_directory,
"version|v" =>\$version,
"quiet|q" => \$quiet,
"help|h|?" => \$help,
"follow|f" => \$follow,
"compat-libtool" => \$compat_libtool,
"no-compat-libtool" => \$no_compat_libtool,
"gcov-tool=s" => \$gcov_tool,
"ignore-errors=s" => \@ignore_errors,
"initial|i" => \$initial,
"no-recursion" => \$no_recursion,
"no-markers" => \$no_markers,
"derive-func-data" => \$opt_derive_func_data,
"debug" => \$debug,
))
{
print(STDERR "Use $tool_name --help to get usage information\n");
exit(1);
}
else
{
# Merge options
if (defined($no_checksum))
{
$checksum = ($no_checksum ? 0 : 1);
$no_checksum = undef;
}
if (defined($no_compat_libtool))
{
$compat_libtool = ($no_compat_libtool ? 0 : 1);
$no_compat_libtool = undef;
}
}
@data_directory = @ARGV;
# Check for help option
if ($help)
{
print_usage(*STDOUT);
exit(0);
}
# Check for version option
if ($version)
{
print("$tool_name: $lcov_version\n");
exit(0);
}
# Make sure test names only contain valid characters
if ($test_name =~ s/\W/_/g)
{
warn("WARNING: invalid characters removed from testname!\n");
}
# Adjust test name to include uname output if requested
if ($adjust_testname)
{
$test_name .= "__".`uname -a`;
$test_name =~ s/\W/_/g;
}
# Make sure base_directory contains an absolute path specification
if ($base_directory)
{
$base_directory = solve_relative_path($cwd, $base_directory);
}
# Check for follow option
if ($follow)
{
$follow = "-follow"
}
else
{
$follow = "";
}
# Determine checksum mode
if (defined($checksum))
{
# Normalize to boolean
$checksum = ($checksum ? 1 : 0);
}
else
{
# Default is off
$checksum = 0;
}
# Determine libtool compatibility mode
if (defined($compat_libtool))
{
$compat_libtool = ($compat_libtool? 1 : 0);
}
else
{
# Default is on
$compat_libtool = 1;
}
# Determine max depth for recursion
if ($no_recursion)
{
$maxdepth = "-maxdepth 1";
}
else
{
$maxdepth = "";
}
# Check for directory name
if (!@data_directory)
{
die("No directory specified\n".
"Use $tool_name --help to get usage information\n");
}
else
{
foreach (@data_directory)
{
stat($_);
if (!-r _)
{
die("ERROR: cannot read $_!\n");
}
}
}
if (@ignore_errors)
{
my @expanded;
my $error;
# Expand comma-separated entries
foreach (@ignore_errors) {
if (/,/)
{
push(@expanded, split(",", $_));
}
else
{
push(@expanded, $_);
}
}
foreach (@expanded)
{
/^gcov$/ && do { $ignore[$ERROR_GCOV] = 1; next; } ;
/^source$/ && do { $ignore[$ERROR_SOURCE] = 1; next; };
/^graph$/ && do { $ignore[$ERROR_GRAPH] = 1; next; };
die("ERROR: unknown argument for --ignore-errors: $_\n");
}
}
if (system_no_output(3, $gcov_tool, "--help") == -1)
{
die("ERROR: need tool $gcov_tool!\n");
}
$gcov_version = get_gcov_version();
if ($gcov_version < $GCOV_VERSION_3_4_0)
{
if (defined($compatibility) && $compatibility eq $COMPAT_HAMMER)
{
$data_file_extension = ".da";
$graph_file_extension = ".bbg";
}
else
{
$data_file_extension = ".da";
$graph_file_extension = ".bb";
}
}
else
{
$data_file_extension = ".gcda";
$graph_file_extension = ".gcno";
}
# Determine gcov options
$gcov_caps = get_gcov_capabilities();
push(@gcov_options, "-b") if ($gcov_caps->{'branch-probabilities'});
push(@gcov_options, "-c") if ($gcov_caps->{'branch-counts'});
# koomie disabled on 9/17/10; gcov -a causing hangs for certain projects (ablation, marco)
### push(@gcov_options, "-a") if ($gcov_caps->{'all-blocks'});
# end koomie mod
push(@gcov_options, "-p") if ($gcov_caps->{'preserve-paths'});
# Check output filename
if (defined($output_filename) && ($output_filename ne "-"))
{
# Initially create output filename, data is appended
# for each data file processed
local *DUMMY_HANDLE;
open(DUMMY_HANDLE, ">$output_filename")
or die("ERROR: cannot create $output_filename!\n");
close(DUMMY_HANDLE);
# Make $output_filename an absolute path because we're going
# to change directories while processing files
if (!($output_filename =~ /^\/(.*)$/))
{
$output_filename = $cwd."/".$output_filename;
}
}
# Do something
foreach my $entry (@data_directory) {
gen_info($entry);
}
if ($initial) {
warn("Note: --initial does not generate branch coverage ".
"data\n");
}
info("Finished .info-file creation\n");
exit(0);
#
# print_usage(handle)
#
# Print usage information.
#
sub print_usage(*)
{
local *HANDLE = $_[0];
print(HANDLE <<END_OF_USAGE);
Usage: $tool_name [OPTIONS] DIRECTORY
Traverse DIRECTORY and create a .info file for each data file found. Note
that you may specify more than one directory, all of which are then processed
sequentially.
-h, --help Print this help, then exit
-v, --version Print version number, then exit
-q, --quiet Do not print progress messages
-i, --initial Capture initial zero coverage data
-t, --test-name NAME Use test case name NAME for resulting data
-o, --output-filename OUTFILE Write data only to OUTFILE
-f, --follow Follow links when searching .da/.gcda files
-b, --base-directory DIR Use DIR as base directory for relative paths
--(no-)checksum Enable (disable) line checksumming
--(no-)compat-libtool Enable (disable) libtool compatibility mode
--gcov-tool TOOL Specify gcov tool location
--ignore-errors ERROR Continue after ERROR (gcov, source, graph)
--no-recursion Exclude subdirectories from processing
--function-coverage Capture function call counts
--no-markers Ignore exclusion markers in source code
--derive-func-data Generate function data from line data
For more information see: $lcov_url
END_OF_USAGE
;
}
#
# get_common_prefix(min_dir, filenames)
#
# Return the longest path prefix shared by all filenames. MIN_DIR specifies
# the minimum number of directories that a filename may have after removing
# the prefix.
#
sub get_common_prefix($@)
{
my ($min_dir, @files) = @_;
my $file;
my @prefix;
my $i;
foreach $file (@files) {
my ($v, $d, $f) = splitpath($file);
my @comp = splitdir($d);
if (!@prefix) {
@prefix = @comp;
next;
}
for ($i = 0; $i < scalar(@comp) && $i < scalar(@prefix); $i++) {
if ($comp[$i] ne $prefix[$i] ||
((scalar(@comp) - ($i + 1)) <= $min_dir)) {
delete(@prefix[$i..scalar(@prefix)]);
last;
}
}
}
return catdir(@prefix);
}
#
# gen_info(directory)
#
# Traverse DIRECTORY and create a .info file for each data file found.
# The .info file contains TEST_NAME in the following format:
#
# TN:<test name>
#
# For each source file name referenced in the data file, there is a section
# containing source code and coverage data:
#
# SF:<absolute path to the source file>
# FN:<line number of function start>,<function name> for each function
# DA:<line number>,<execution count> for each instrumented line
# LH:<number of lines with an execution count> greater than 0
# LF:<number of instrumented lines>
#
# Sections are separated by:
#
# end_of_record
#
# In addition to the main source code file there are sections for each
# #included file containing executable code. Note that the absolute path
# of a source file is generated by interpreting the contents of the respective
# graph file. Relative filenames are prefixed with the directory in which the
# graph file is found. Note also that symbolic links to the graph file will be
# resolved so that the actual file path is used instead of the path to a link.
# This approach is necessary for the mechanism to work with the /proc/gcov
# files.
#
# Die on error.
#
sub gen_info($)
{
my $directory = $_[0];
my @file_list;
my $file;
my $prefix;
my $type;
my $ext;
if ($initial) {
$type = "graph";
$ext = $graph_file_extension;
} else {
$type = "data";
$ext = $data_file_extension;
}
if (-d $directory)
{
info("Scanning $directory for $ext files ...\n");
@file_list = `find "$directory" $maxdepth $follow -name \\*$ext -type f 2>/dev/null`;
chomp(@file_list);
@file_list or
die("ERROR: no $ext files found in $directory!\n");
$prefix = get_common_prefix(1, @file_list);
info("Found %d %s files in %s\n", $#file_list+1, $type,
$directory);
}
else
{
@file_list = ($directory);
$prefix = "";
}
# Process all files in list
foreach $file (@file_list) {
# Process file
if ($initial) {
process_graphfile($file, $prefix);
} else {
process_dafile($file, $prefix);
}
}
}
sub derive_data($$$)
{
my ($contentdata, $funcdata, $bbdata) = @_;
my @gcov_content = @{$contentdata};
my @gcov_functions = @{$funcdata};
my %fn_count;
my %ln_fn;
my $line;
my $maxline;
my %fn_name;
my $fn;
my $count;
if (!defined($bbdata)) {
return @gcov_functions;
}
# First add existing function data
while (@gcov_functions) {
$count = shift(@gcov_functions);
$fn = shift(@gcov_functions);
$fn_count{$fn} = $count;
}
# Convert line coverage data to function data
foreach $fn (keys(%{$bbdata})) {
my $line_data = $bbdata->{$fn};
my $line;
if ($fn eq "") {
next;
}
# Find the lowest line count for this function
$count = 0;
foreach $line (@$line_data) {
my $lcount = $gcov_content[ ( $line - 1 ) * 3 + 1 ];
if (($lcount > 0) &&
(($count == 0) || ($lcount < $count))) {
$count = $lcount;
}
}
$fn_count{$fn} = $count;
}
# Check if we got data for all functions
foreach $fn (keys(%fn_name)) {
if ($fn eq "") {
next;
}
if (defined($fn_count{$fn})) {
next;
}
warn("WARNING: no derived data found for function $fn\n");
}
# Convert hash to list in @gcov_functions format
foreach $fn (sort(keys(%fn_count))) {
push(@gcov_functions, $fn_count{$fn}, $fn);
}
return @gcov_functions;
}
#
# get_filenames(directory, pattern)
#
# Return a list of filenames found in directory which match the specified
# pattern.
#
# Die on error.
#
sub get_filenames($$)
{
my ($dirname, $pattern) = @_;
my @result;
my $directory;
local *DIR;
opendir(DIR, $dirname) or
die("ERROR: cannot read directory $dirname\n");
while ($directory = readdir(DIR)) {
push(@result, $directory) if ($directory =~ /$pattern/);
}
closedir(DIR);
return @result;
}
#
# process_dafile(da_filename, dir)
#
# Create a .info file for a single data file.
#
# Die on error.
#
sub process_dafile($$)
{
my ($file, $dir) = @_;
my $da_filename; # Name of data file to process
my $da_dir; # Directory of data file
my $source_dir; # Directory of source file
my $da_basename; # data filename without ".da/.gcda" extension
my $bb_filename; # Name of respective graph file
my $bb_basename; # Basename of the original graph file
my $graph; # Contents of graph file
my $instr; # Contents of graph file part 2
my $gcov_error; # Error code of gcov tool
my $object_dir; # Directory containing all object files
my $source_filename; # Name of a source code file
my $gcov_file; # Name of a .gcov file
my @gcov_content; # Content of a .gcov file
my $gcov_branches; # Branch content of a .gcov file
my @gcov_functions; # Function calls of a .gcov file
my @gcov_list; # List of generated .gcov files
my $line_number; # Line number count
my $lines_hit; # Number of instrumented lines hit
my $lines_found; # Number of instrumented lines found
my $funcs_hit; # Number of instrumented functions hit
my $funcs_found; # Number of instrumented functions found
my $br_hit;
my $br_found;
my $source; # gcov source header information
my $object; # gcov object header information
my @matches; # List of absolute paths matching filename
my @unprocessed; # List of unprocessed source code files
my $base_dir; # Base directory for current file
my @tmp_links; # Temporary links to be cleaned up
my @result;
my $index;
my $da_renamed; # If data file is to be renamed
local *INFO_HANDLE;
info("Processing %s\n", abs2rel($file, $dir));
# Get path to data file in absolute and normalized form (begins with /,
# contains no more ../ or ./)
$da_filename = solve_relative_path($cwd, $file);
# Get directory and basename of data file
($da_dir, $da_basename) = split_filename($da_filename);
# avoid files from .libs dirs
if ($compat_libtool && $da_dir =~ m/(.*)\/\.libs$/) {
$source_dir = $1;
} else {
$source_dir = $da_dir;
}
if (-z $da_filename)
{
$da_renamed = 1;
}
else
{
$da_renamed = 0;
}
# Construct base_dir for current file
if ($base_directory)
{
$base_dir = $base_directory;
}
else
{
$base_dir = $source_dir;
}
# Check for writable $base_dir (gcov will try to write files there)
stat($base_dir);
if (!-w _)
{
die("ERROR: cannot write to directory $base_dir!\n");
}
# Construct name of graph file
$bb_basename = $da_basename.$graph_file_extension;
$bb_filename = "$da_dir/$bb_basename";
# Find out the real location of graph file in case we're just looking at
# a link
while (readlink($bb_filename))
{
my $last_dir = dirname($bb_filename);
$bb_filename = readlink($bb_filename);
$bb_filename = solve_relative_path($last_dir, $bb_filename);
}
# Ignore empty graph file (e.g. source file with no statement)
if (-z $bb_filename)
{
warn("WARNING: empty $bb_filename (skipped)\n");
return;
}
# Read contents of graph file into hash. We need it later to find out
# the absolute path to each .gcov file created as well as for
# information about functions and their source code positions.
if ($gcov_version < $GCOV_VERSION_3_4_0)
{
if (defined($compatibility) && $compatibility eq $COMPAT_HAMMER)
{
($instr, $graph) = read_bbg($bb_filename, $base_dir);
}
else
{
($instr, $graph) = read_bb($bb_filename, $base_dir);
}
}
else
{
($instr, $graph) = read_gcno($bb_filename, $base_dir);
}
# Set $object_dir to real location of object files. This may differ
# from $da_dir if the graph file is just a link to the "real" object
# file location.
$object_dir = dirname($bb_filename);
# Is the data file in a different directory? (this happens e.g. with
# the gcov-kernel patch)
if ($object_dir ne $da_dir)
{
# Need to create link to data file in $object_dir
system("ln", "-s", $da_filename,
"$object_dir/$da_basename$data_file_extension")
and die ("ERROR: cannot create link $object_dir/".
"$da_basename$data_file_extension!\n");
push(@tmp_links,
"$object_dir/$da_basename$data_file_extension");
# Need to create link to graph file if basename of link
# and file are different (CONFIG_MODVERSION compat)
if ((basename($bb_filename) ne $bb_basename) &&
(! -e "$object_dir/$bb_basename")) {
symlink($bb_filename, "$object_dir/$bb_basename") or
warn("WARNING: cannot create link ".
"$object_dir/$bb_basename\n");
push(@tmp_links, "$object_dir/$bb_basename");
}
}
# Change to directory containing data files and apply GCOV
chdir($base_dir);
if ($da_renamed)
{
# Need to rename empty data file to workaround
# gcov <= 3.2.x bug (Abort)
system_no_output(3, "mv", "$da_filename", "$da_filename.ori")
and die ("ERROR: cannot rename $da_filename\n");
}
# Execute gcov command and suppress standard output
$gcov_error = system_no_output(1, $gcov_tool, $da_filename,
"-o", $object_dir, @gcov_options);
if ($da_renamed)
{
system_no_output(3, "mv", "$da_filename.ori", "$da_filename")
and die ("ERROR: cannot rename $da_filename.ori");
}
# Clean up temporary links
foreach (@tmp_links) {
unlink($_);
}
if ($gcov_error)
{
if ($ignore[$ERROR_GCOV])
{
warn("WARNING: GCOV failed for $da_filename!\n");
return;
}
die("ERROR: GCOV failed for $da_filename!\n");
}
# Collect data from resulting .gcov files and create .info file
@gcov_list = get_filenames('.', '\.gcov$');
# Check for files
if (!@gcov_list)
{
warn("WARNING: gcov did not create any files for ".
"$da_filename!\n");
}
# Check whether we're writing to a single file
if ($output_filename)
{
if ($output_filename eq "-")
{
*INFO_HANDLE = *STDOUT;
}
else
{
# Append to output file
open(INFO_HANDLE, ">>$output_filename")
or die("ERROR: cannot write to ".
"$output_filename!\n");
}
}
else
{
# Open .info file for output
open(INFO_HANDLE, ">$da_filename.info")
or die("ERROR: cannot create $da_filename.info!\n");
}
# Write test name
printf(INFO_HANDLE "TN:%s\n", $test_name);
# Traverse the list of generated .gcov files and combine them into a
# single .info file
@unprocessed = keys(%{$instr});
foreach $gcov_file (sort(@gcov_list))
{
my $i;
my $num;
($source, $object) = read_gcov_header($gcov_file);
if (defined($source))
{
$source = solve_relative_path($base_dir, $source);
}
# gcov will happily create output even if there's no source code
# available - this interferes with checksum creation so we need
# to pull the emergency brake here.
if (defined($source) && ! -r $source && $checksum)
{
if ($ignore[$ERROR_SOURCE])
{
warn("WARNING: could not read source file ".
"$source\n");
next;
}
die("ERROR: could not read source file $source\n");
}
@matches = match_filename(defined($source) ? $source :
$gcov_file, keys(%{$instr}));
# Skip files that are not mentioned in the graph file
if (!@matches)
{
warn("WARNING: cannot find an entry for ".$gcov_file.
" in $graph_file_extension file, skipping ".
"file!\n");
unlink($gcov_file);
next;
}
# Read in contents of gcov file
@result = read_gcov_file($gcov_file);
if (!defined($result[0])) {
warn("WARNING: skipping unreadable file ".
$gcov_file."\n");
unlink($gcov_file);
next;
}
@gcov_content = @{$result[0]};
$gcov_branches = $result[1];
@gcov_functions = @{$result[2]};
# Skip empty files
if (!@gcov_content)
{
warn("WARNING: skipping empty file ".$gcov_file."\n");
unlink($gcov_file);
next;
}
if (scalar(@matches) == 1)
{
# Just one match
$source_filename = $matches[0];
}
else
{
# Try to solve the ambiguity
$source_filename = solve_ambiguous_match($gcov_file,
\@matches, \@gcov_content);
}