blob: 8f79b701de876a807ae76abd89ad077d65ef3390 [file] [log] [blame]
Ram Paic5e30032006-06-23 16:44:38 -07001#!/usr/bin/perl -w
2#
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
10use Getopt::Std;
11use strict;
12
13sub numerically {
14 my $no1 = (split /\s+/, $a)[1];
15 my $no2 = (split /\s+/, $b)[1];
16 return $no1 <=> $no2;
17}
18
19sub alphabetically {
20 my ($module1, $value1) = @{$a};
21 my ($module2, $value2) = @{$b};
22 return $value1 <=> $value2 || $module2 cmp $module1;
23}
24
25sub print_depends_on {
26 my ($href) = @_;
27 print "\n";
Jim Cromiebdabc7a2011-05-23 12:44:56 -060028 for my $mod (sort keys %$href) {
29 my $list = $href->{$mod};
Ram Paic5e30032006-06-23 16:44:38 -070030 print "\t$mod:\n";
31 foreach my $sym (sort numerically @{$list}) {
32 my ($symbol, $no) = split /\s+/, $sym;
Jim Cromiebdabc7a2011-05-23 12:44:56 -060033 printf("\t\t%-25s\n", $symbol);
Ram Paic5e30032006-06-23 16:44:38 -070034 }
35 print "\n";
36 }
37 print "\n";
38 print "~"x80 , "\n";
39}
40
41sub usage {
42 print "Usage: @_ -h -k Module.symvers [ -o outputfile ] \n",
43 "\t-f: treat all the non-option argument as .mod.c files. ",
44 "Recommend using this as the last option\n",
45 "\t-h: print detailed help\n",
46 "\t-k: the path to Module.symvers file. By default uses ",
47 "the file from the current directory\n",
48 "\t-o outputfile: output the report to outputfile\n";
49 exit 0;
50}
51
52sub collectcfiles {
Jim Cromiede7b0b42011-05-23 12:44:55 -060053 my @file;
54 while (<.tmp_versions/*.mod>) {
55 open my $fh, '<', $_ or die "cannot open $_: $!\n";
56 push (@file,
57 grep s/\.ko/.mod.c/, # change the suffix
58 grep m/.+\.ko/, # find the .ko path
59 <$fh>); # lines in opened file
60 }
Stephen Hemminger91416cf2010-02-22 15:17:22 -080061 chomp @file;
62 return @file;
Ram Paic5e30032006-06-23 16:44:38 -070063}
64
65my (%SYMBOL, %MODULE, %opt, @allcfiles);
66
67if (not getopts('hk:o:f',\%opt) or defined $opt{'h'}) {
68 usage($0);
69}
70
71if (defined $opt{'f'}) {
72 @allcfiles = @ARGV;
73} else {
74 @allcfiles = collectcfiles();
75}
76
77if (not defined $opt{'k'}) {
78 $opt{'k'} = "Module.symvers";
79}
80
Stephen Hemminger91416cf2010-02-22 15:17:22 -080081open (my $module_symvers, '<', $opt{'k'})
82 or die "Sorry, cannot open $opt{'k'}: $!\n";
Ram Paic5e30032006-06-23 16:44:38 -070083
84if (defined $opt{'o'}) {
Stephen Hemminger91416cf2010-02-22 15:17:22 -080085 open (my $out, '>', $opt{'o'})
86 or die "Sorry, cannot open $opt{'o'} $!\n";
87
88 select $out;
Ram Paic5e30032006-06-23 16:44:38 -070089}
Stephen Hemminger91416cf2010-02-22 15:17:22 -080090
Ram Paic5e30032006-06-23 16:44:38 -070091#
92# collect all the symbols and their attributes from the
93# Module.symvers file
94#
Stephen Hemminger91416cf2010-02-22 15:17:22 -080095while ( <$module_symvers> ) {
Ram Paic5e30032006-06-23 16:44:38 -070096 chomp;
97 my (undef, $symbol, $module, $gpl) = split;
98 $SYMBOL { $symbol } = [ $module , "0" , $symbol, $gpl];
99}
Stephen Hemminger91416cf2010-02-22 15:17:22 -0800100close($module_symvers);
Ram Paic5e30032006-06-23 16:44:38 -0700101
102#
103# collect the usage count of each symbol.
104#
Jim Cromieca995cb2011-05-23 12:44:57 -0600105my $modversion_warnings = 0;
106
Ram Paic5e30032006-06-23 16:44:38 -0700107foreach my $thismod (@allcfiles) {
Stephen Hemminger91416cf2010-02-22 15:17:22 -0800108 my $module;
109
110 unless (open ($module, '<', $thismod)) {
111 warn "Sorry, cannot open $thismod: $!\n";
Ram Paic5e30032006-06-23 16:44:38 -0700112 next;
113 }
Stephen Hemminger91416cf2010-02-22 15:17:22 -0800114
Ram Paic5e30032006-06-23 16:44:38 -0700115 my $state=0;
Stephen Hemminger91416cf2010-02-22 15:17:22 -0800116 while ( <$module> ) {
Ram Paic5e30032006-06-23 16:44:38 -0700117 chomp;
Ram Pai88f567f2007-08-24 15:32:22 -0700118 if ($state == 0) {
Ram Paic5e30032006-06-23 16:44:38 -0700119 $state = 1 if ($_ =~ /static const struct modversion_info/);
120 next;
121 }
Ram Pai88f567f2007-08-24 15:32:22 -0700122 if ($state == 1) {
Ram Paic5e30032006-06-23 16:44:38 -0700123 $state = 2 if ($_ =~ /__attribute__\(\(section\("__versions"\)\)\)/);
124 next;
125 }
Ram Pai88f567f2007-08-24 15:32:22 -0700126 if ($state == 2) {
Adrian Bunkcf9a6ad2007-08-24 23:04:51 +0200127 if ( $_ !~ /0x[0-9a-f]+,/ ) {
Ram Paic5e30032006-06-23 16:44:38 -0700128 next;
129 }
130 my $sym = (split /([,"])/,)[4];
131 my ($module, $value, $symbol, $gpl) = @{$SYMBOL{$sym}};
132 $SYMBOL{ $sym } = [ $module, $value+1, $symbol, $gpl];
133 push(@{$MODULE{$thismod}} , $sym);
134 }
135 }
Ram Pai88f567f2007-08-24 15:32:22 -0700136 if ($state != 2) {
Jim Cromieca995cb2011-05-23 12:44:57 -0600137 warn "WARNING:$thismod is not built with CONFIG_MODVERSIONS enabled\n";
138 $modversion_warnings++;
Ram Paic5e30032006-06-23 16:44:38 -0700139 }
Stephen Hemminger91416cf2010-02-22 15:17:22 -0800140 close($module);
Ram Paic5e30032006-06-23 16:44:38 -0700141}
142
143print "\tThis file reports the exported symbols usage patterns by in-tree\n",
144 "\t\t\t\tmodules\n";
145printf("%s\n\n\n","x"x80);
146printf("\t\t\t\tINDEX\n\n\n");
147printf("SECTION 1: Usage counts of all exported symbols\n");
148printf("SECTION 2: List of modules and the exported symbols they use\n");
149printf("%s\n\n\n","x"x80);
150printf("SECTION 1:\tThe exported symbols and their usage count\n\n");
151printf("%-25s\t%-25s\t%-5s\t%-25s\n", "Symbol", "Module", "Usage count",
152 "export type");
153
154#
155# print the list of unused exported symbols
156#
157foreach my $list (sort alphabetically values(%SYMBOL)) {
158 my ($module, $value, $symbol, $gpl) = @{$list};
159 printf("%-25s\t%-25s\t%-10s\t", $symbol, $module, $value);
160 if (defined $gpl) {
161 printf("%-25s\n",$gpl);
162 } else {
163 printf("\n");
164 }
165}
166printf("%s\n\n\n","x"x80);
167
168printf("SECTION 2:\n\tThis section reports export-symbol-usage of in-kernel
169modules. Each module lists the modules, and the symbols from that module that
170it uses. Each listed symbol reports the number of modules using it\n");
171
Jim Cromieca995cb2011-05-23 12:44:57 -0600172print "\nNOTE: Got $modversion_warnings CONFIG_MODVERSIONS warnings\n\n"
173 if $modversion_warnings;
174
Ram Paic5e30032006-06-23 16:44:38 -0700175print "~"x80 , "\n";
Jim Cromiebdabc7a2011-05-23 12:44:56 -0600176for my $thismod (sort keys %MODULE) {
177 my $list = $MODULE{$thismod};
Ram Paic5e30032006-06-23 16:44:38 -0700178 my %depends;
179 $thismod =~ s/\.mod\.c/.ko/;
180 print "\t\t\t$thismod\n";
181 foreach my $symbol (@{$list}) {
182 my ($module, $value, undef, $gpl) = @{$SYMBOL{$symbol}};
183 push (@{$depends{"$module"}}, "$symbol $value");
184 }
185 print_depends_on(\%depends);
186}