Merge tag 'afs-fixes-20171201' of git://git.kernel.org/pub/scm/linux/kernel/git/dhowe...
[linux/fpc-iii.git] / scripts / export_report.pl
blob68ff426b347c7f0e7bc1d15659b69a77d7b4727d
1 #!/usr/bin/env perl
3 # (C) Copyright IBM Corporation 2006.
4 # Released under GPL v2.
5 # Author : Ram Pai (linuxram@us.ibm.com)
7 # Usage: export_report.pl -k Module.symvers [-o report_file ] -f *.mod.c
10 use warnings;
11 use Getopt::Std;
12 use strict;
14 sub numerically {
15 my $no1 = (split /\s+/, $a)[1];
16 my $no2 = (split /\s+/, $b)[1];
17 return $no1 <=> $no2;
20 sub alphabetically {
21 my ($module1, $value1) = @{$a};
22 my ($module2, $value2) = @{$b};
23 return $value1 <=> $value2 || $module2 cmp $module1;
26 sub print_depends_on {
27 my ($href) = @_;
28 print "\n";
29 for my $mod (sort keys %$href) {
30 my $list = $href->{$mod};
31 print "\t$mod:\n";
32 foreach my $sym (sort numerically @{$list}) {
33 my ($symbol, $no) = split /\s+/, $sym;
34 printf("\t\t%-25s\n", $symbol);
36 print "\n";
38 print "\n";
39 print "~"x80 , "\n";
42 sub usage {
43 print "Usage: @_ -h -k Module.symvers [ -o outputfile ] \n",
44 "\t-f: treat all the non-option argument as .mod.c files. ",
45 "Recommend using this as the last option\n",
46 "\t-h: print detailed help\n",
47 "\t-k: the path to Module.symvers file. By default uses ",
48 "the file from the current directory\n",
49 "\t-o outputfile: output the report to outputfile\n";
50 exit 0;
53 sub collectcfiles {
54 my @file;
55 while (<.tmp_versions/*.mod>) {
56 open my $fh, '<', $_ or die "cannot open $_: $!\n";
57 push (@file,
58 grep s/\.ko/.mod.c/, # change the suffix
59 grep m/.+\.ko/, # find the .ko path
60 <$fh>); # lines in opened file
62 chomp @file;
63 return @file;
66 my (%SYMBOL, %MODULE, %opt, @allcfiles);
68 if (not getopts('hk:o:f',\%opt) or defined $opt{'h'}) {
69 usage($0);
72 if (defined $opt{'f'}) {
73 @allcfiles = @ARGV;
74 } else {
75 @allcfiles = collectcfiles();
78 if (not defined $opt{'k'}) {
79 $opt{'k'} = "Module.symvers";
82 open (my $module_symvers, '<', $opt{'k'})
83 or die "Sorry, cannot open $opt{'k'}: $!\n";
85 if (defined $opt{'o'}) {
86 open (my $out, '>', $opt{'o'})
87 or die "Sorry, cannot open $opt{'o'} $!\n";
89 select $out;
93 # collect all the symbols and their attributes from the
94 # Module.symvers file
96 while ( <$module_symvers> ) {
97 chomp;
98 my (undef, $symbol, $module, $gpl) = split;
99 $SYMBOL { $symbol } = [ $module , "0" , $symbol, $gpl];
101 close($module_symvers);
104 # collect the usage count of each symbol.
106 my $modversion_warnings = 0;
108 foreach my $thismod (@allcfiles) {
109 my $module;
111 unless (open ($module, '<', $thismod)) {
112 warn "Sorry, cannot open $thismod: $!\n";
113 next;
116 my $state=0;
117 while ( <$module> ) {
118 chomp;
119 if ($state == 0) {
120 $state = 1 if ($_ =~ /static const struct modversion_info/);
121 next;
123 if ($state == 1) {
124 $state = 2 if ($_ =~ /__attribute__\(\(section\("__versions"\)\)\)/);
125 next;
127 if ($state == 2) {
128 if ( $_ !~ /0x[0-9a-f]+,/ ) {
129 next;
131 my $sym = (split /([,"])/,)[4];
132 my ($module, $value, $symbol, $gpl) = @{$SYMBOL{$sym}};
133 $SYMBOL{ $sym } = [ $module, $value+1, $symbol, $gpl];
134 push(@{$MODULE{$thismod}} , $sym);
137 if ($state != 2) {
138 warn "WARNING:$thismod is not built with CONFIG_MODVERSIONS enabled\n";
139 $modversion_warnings++;
141 close($module);
144 print "\tThis file reports the exported symbols usage patterns by in-tree\n",
145 "\t\t\t\tmodules\n";
146 printf("%s\n\n\n","x"x80);
147 printf("\t\t\t\tINDEX\n\n\n");
148 printf("SECTION 1: Usage counts of all exported symbols\n");
149 printf("SECTION 2: List of modules and the exported symbols they use\n");
150 printf("%s\n\n\n","x"x80);
151 printf("SECTION 1:\tThe exported symbols and their usage count\n\n");
152 printf("%-25s\t%-25s\t%-5s\t%-25s\n", "Symbol", "Module", "Usage count",
153 "export type");
156 # print the list of unused exported symbols
158 foreach my $list (sort alphabetically values(%SYMBOL)) {
159 my ($module, $value, $symbol, $gpl) = @{$list};
160 printf("%-25s\t%-25s\t%-10s\t", $symbol, $module, $value);
161 if (defined $gpl) {
162 printf("%-25s\n",$gpl);
163 } else {
164 printf("\n");
167 printf("%s\n\n\n","x"x80);
169 printf("SECTION 2:\n\tThis section reports export-symbol-usage of in-kernel
170 modules. Each module lists the modules, and the symbols from that module that
171 it uses. Each listed symbol reports the number of modules using it\n");
173 print "\nNOTE: Got $modversion_warnings CONFIG_MODVERSIONS warnings\n\n"
174 if $modversion_warnings;
176 print "~"x80 , "\n";
177 for my $thismod (sort keys %MODULE) {
178 my $list = $MODULE{$thismod};
179 my %depends;
180 $thismod =~ s/\.mod\.c/.ko/;
181 print "\t\t\t$thismod\n";
182 foreach my $symbol (@{$list}) {
183 my ($module, $value, undef, $gpl) = @{$SYMBOL{$symbol}};
184 push (@{$depends{"$module"}}, "$symbol $value");
186 print_depends_on(\%depends);