blob: 68ff426b347c7f0e7bc1d15659b69a77d7b4727d [file] [log] [blame]
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +02001#!/usr/bin/env perl
Ram Paic5e30032006-06-23 16:44:38 -07002#
3# (C) Copyright IBM Corporation 2006.
4# Released under GPL v2.
5# Author : Ram Pai (linuxram@us.ibm.com)
6#
7# Usage: export_report.pl -k Module.symvers [-o report_file ] -f *.mod.c
8#
9
Kamil Rytarowskicb77f0d2017-05-07 23:25:26 +020010use warnings;
Ram Paic5e30032006-06-23 16:44:38 -070011use Getopt::Std;
12use strict;
13
14sub numerically {
15 my $no1 = (split /\s+/, $a)[1];
16 my $no2 = (split /\s+/, $b)[1];
17 return $no1 <=> $no2;
18}
19
20sub alphabetically {
21 my ($module1, $value1) = @{$a};
22 my ($module2, $value2) = @{$b};
23 return $value1 <=> $value2 || $module2 cmp $module1;
24}
25
26sub print_depends_on {
27 my ($href) = @_;
28 print "\n";
Jim Cromiebdabc7a2011-05-23 12:44:56 -060029 for my $mod (sort keys %$href) {
30 my $list = $href->{$mod};
Ram Paic5e30032006-06-23 16:44:38 -070031 print "\t$mod:\n";
32 foreach my $sym (sort numerically @{$list}) {
33 my ($symbol, $no) = split /\s+/, $sym;
Jim Cromiebdabc7a2011-05-23 12:44:56 -060034 printf("\t\t%-25s\n", $symbol);
Ram Paic5e30032006-06-23 16:44:38 -070035 }
36 print "\n";
37 }
38 print "\n";
39 print "~"x80 , "\n";
40}
41
42sub 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;
51}
52
53sub collectcfiles {
Jim Cromiede7b0b42011-05-23 12:44:55 -060054 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
61 }
Stephen Hemminger91416cf2010-02-22 15:17:22 -080062 chomp @file;
63 return @file;
Ram Paic5e30032006-06-23 16:44:38 -070064}
65
66my (%SYMBOL, %MODULE, %opt, @allcfiles);
67
68if (not getopts('hk:o:f',\%opt) or defined $opt{'h'}) {
69 usage($0);
70}
71
72if (defined $opt{'f'}) {
73 @allcfiles = @ARGV;
74} else {
75 @allcfiles = collectcfiles();
76}
77
78if (not defined $opt{'k'}) {
79 $opt{'k'} = "Module.symvers";
80}
81
Stephen Hemminger91416cf2010-02-22 15:17:22 -080082open (my $module_symvers, '<', $opt{'k'})
83 or die "Sorry, cannot open $opt{'k'}: $!\n";
Ram Paic5e30032006-06-23 16:44:38 -070084
85if (defined $opt{'o'}) {
Stephen Hemminger91416cf2010-02-22 15:17:22 -080086 open (my $out, '>', $opt{'o'})
87 or die "Sorry, cannot open $opt{'o'} $!\n";
88
89 select $out;
Ram Paic5e30032006-06-23 16:44:38 -070090}
Stephen Hemminger91416cf2010-02-22 15:17:22 -080091
Ram Paic5e30032006-06-23 16:44:38 -070092#
93# collect all the symbols and their attributes from the
94# Module.symvers file
95#
Stephen Hemminger91416cf2010-02-22 15:17:22 -080096while ( <$module_symvers> ) {
Ram Paic5e30032006-06-23 16:44:38 -070097 chomp;
98 my (undef, $symbol, $module, $gpl) = split;
99 $SYMBOL { $symbol } = [ $module , "0" , $symbol, $gpl];
100}
Stephen Hemminger91416cf2010-02-22 15:17:22 -0800101close($module_symvers);
Ram Paic5e30032006-06-23 16:44:38 -0700102
103#
104# collect the usage count of each symbol.
105#
Jim Cromieca995cb2011-05-23 12:44:57 -0600106my $modversion_warnings = 0;
107
Ram Paic5e30032006-06-23 16:44:38 -0700108foreach my $thismod (@allcfiles) {
Stephen Hemminger91416cf2010-02-22 15:17:22 -0800109 my $module;
110
111 unless (open ($module, '<', $thismod)) {
112 warn "Sorry, cannot open $thismod: $!\n";
Ram Paic5e30032006-06-23 16:44:38 -0700113 next;
114 }
Stephen Hemminger91416cf2010-02-22 15:17:22 -0800115
Ram Paic5e30032006-06-23 16:44:38 -0700116 my $state=0;
Stephen Hemminger91416cf2010-02-22 15:17:22 -0800117 while ( <$module> ) {
Ram Paic5e30032006-06-23 16:44:38 -0700118 chomp;
Ram Pai88f567f2007-08-24 15:32:22 -0700119 if ($state == 0) {
Ram Paic5e30032006-06-23 16:44:38 -0700120 $state = 1 if ($_ =~ /static const struct modversion_info/);
121 next;
122 }
Ram Pai88f567f2007-08-24 15:32:22 -0700123 if ($state == 1) {
Ram Paic5e30032006-06-23 16:44:38 -0700124 $state = 2 if ($_ =~ /__attribute__\(\(section\("__versions"\)\)\)/);
125 next;
126 }
Ram Pai88f567f2007-08-24 15:32:22 -0700127 if ($state == 2) {
Adrian Bunkcf9a6ad2007-08-24 23:04:51 +0200128 if ( $_ !~ /0x[0-9a-f]+,/ ) {
Ram Paic5e30032006-06-23 16:44:38 -0700129 next;
130 }
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);
135 }
136 }
Ram Pai88f567f2007-08-24 15:32:22 -0700137 if ($state != 2) {
Jim Cromieca995cb2011-05-23 12:44:57 -0600138 warn "WARNING:$thismod is not built with CONFIG_MODVERSIONS enabled\n";
139 $modversion_warnings++;
Ram Paic5e30032006-06-23 16:44:38 -0700140 }
Stephen Hemminger91416cf2010-02-22 15:17:22 -0800141 close($module);
Ram Paic5e30032006-06-23 16:44:38 -0700142}
143
144print "\tThis file reports the exported symbols usage patterns by in-tree\n",
145 "\t\t\t\tmodules\n";
146printf("%s\n\n\n","x"x80);
147printf("\t\t\t\tINDEX\n\n\n");
148printf("SECTION 1: Usage counts of all exported symbols\n");
149printf("SECTION 2: List of modules and the exported symbols they use\n");
150printf("%s\n\n\n","x"x80);
151printf("SECTION 1:\tThe exported symbols and their usage count\n\n");
152printf("%-25s\t%-25s\t%-5s\t%-25s\n", "Symbol", "Module", "Usage count",
153 "export type");
154
155#
156# print the list of unused exported symbols
157#
158foreach 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");
165 }
166}
167printf("%s\n\n\n","x"x80);
168
169printf("SECTION 2:\n\tThis section reports export-symbol-usage of in-kernel
170modules. Each module lists the modules, and the symbols from that module that
171it uses. Each listed symbol reports the number of modules using it\n");
172
Jim Cromieca995cb2011-05-23 12:44:57 -0600173print "\nNOTE: Got $modversion_warnings CONFIG_MODVERSIONS warnings\n\n"
174 if $modversion_warnings;
175
Ram Paic5e30032006-06-23 16:44:38 -0700176print "~"x80 , "\n";
Jim Cromiebdabc7a2011-05-23 12:44:56 -0600177for my $thismod (sort keys %MODULE) {
178 my $list = $MODULE{$thismod};
Ram Paic5e30032006-06-23 16:44:38 -0700179 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");
185 }
186 print_depends_on(\%depends);
187}