]> git.ipfire.org Git - people/arne_f/kernel.git/blame - scripts/export_report.pl
export_report: do collectcfiles work in perl itself
[people/arne_f/kernel.git] / scripts / export_report.pl
CommitLineData
c5e30033
RP
1#!/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";
28 while (my ($mod, $list) = each %$href) {
29 print "\t$mod:\n";
30 foreach my $sym (sort numerically @{$list}) {
31 my ($symbol, $no) = split /\s+/, $sym;
32 printf("\t\t%-25s\t%-25d\n", $symbol, $no);
33 }
34 print "\n";
35 }
36 print "\n";
37 print "~"x80 , "\n";
38}
39
40sub usage {
41 print "Usage: @_ -h -k Module.symvers [ -o outputfile ] \n",
42 "\t-f: treat all the non-option argument as .mod.c files. ",
43 "Recommend using this as the last option\n",
44 "\t-h: print detailed help\n",
45 "\t-k: the path to Module.symvers file. By default uses ",
46 "the file from the current directory\n",
47 "\t-o outputfile: output the report to outputfile\n";
48 exit 0;
49}
50
51sub collectcfiles {
de7b0b41
JC
52 my @file;
53 while (<.tmp_versions/*.mod>) {
54 open my $fh, '<', $_ or die "cannot open $_: $!\n";
55 push (@file,
56 grep s/\.ko/.mod.c/, # change the suffix
57 grep m/.+\.ko/, # find the .ko path
58 <$fh>); # lines in opened file
59 }
91416cfd
SH
60 chomp @file;
61 return @file;
c5e30033
RP
62}
63
64my (%SYMBOL, %MODULE, %opt, @allcfiles);
65
66if (not getopts('hk:o:f',\%opt) or defined $opt{'h'}) {
67 usage($0);
68}
69
70if (defined $opt{'f'}) {
71 @allcfiles = @ARGV;
72} else {
73 @allcfiles = collectcfiles();
74}
75
76if (not defined $opt{'k'}) {
77 $opt{'k'} = "Module.symvers";
78}
79
91416cfd
SH
80open (my $module_symvers, '<', $opt{'k'})
81 or die "Sorry, cannot open $opt{'k'}: $!\n";
c5e30033
RP
82
83if (defined $opt{'o'}) {
91416cfd
SH
84 open (my $out, '>', $opt{'o'})
85 or die "Sorry, cannot open $opt{'o'} $!\n";
86
87 select $out;
c5e30033 88}
91416cfd 89
c5e30033
RP
90#
91# collect all the symbols and their attributes from the
92# Module.symvers file
93#
91416cfd 94while ( <$module_symvers> ) {
c5e30033
RP
95 chomp;
96 my (undef, $symbol, $module, $gpl) = split;
97 $SYMBOL { $symbol } = [ $module , "0" , $symbol, $gpl];
98}
91416cfd 99close($module_symvers);
c5e30033
RP
100
101#
102# collect the usage count of each symbol.
103#
104foreach my $thismod (@allcfiles) {
91416cfd
SH
105 my $module;
106
107 unless (open ($module, '<', $thismod)) {
108 warn "Sorry, cannot open $thismod: $!\n";
c5e30033
RP
109 next;
110 }
91416cfd 111
c5e30033 112 my $state=0;
91416cfd 113 while ( <$module> ) {
c5e30033 114 chomp;
88f567f3 115 if ($state == 0) {
c5e30033
RP
116 $state = 1 if ($_ =~ /static const struct modversion_info/);
117 next;
118 }
88f567f3 119 if ($state == 1) {
c5e30033
RP
120 $state = 2 if ($_ =~ /__attribute__\(\(section\("__versions"\)\)\)/);
121 next;
122 }
88f567f3 123 if ($state == 2) {
cf9a6ade 124 if ( $_ !~ /0x[0-9a-f]+,/ ) {
c5e30033
RP
125 next;
126 }
127 my $sym = (split /([,"])/,)[4];
128 my ($module, $value, $symbol, $gpl) = @{$SYMBOL{$sym}};
129 $SYMBOL{ $sym } = [ $module, $value+1, $symbol, $gpl];
130 push(@{$MODULE{$thismod}} , $sym);
131 }
132 }
88f567f3 133 if ($state != 2) {
c5e30033
RP
134 print "WARNING:$thismod is not built with CONFIG_MODVERSION enabled\n";
135 }
91416cfd 136 close($module);
c5e30033
RP
137}
138
139print "\tThis file reports the exported symbols usage patterns by in-tree\n",
140 "\t\t\t\tmodules\n";
141printf("%s\n\n\n","x"x80);
142printf("\t\t\t\tINDEX\n\n\n");
143printf("SECTION 1: Usage counts of all exported symbols\n");
144printf("SECTION 2: List of modules and the exported symbols they use\n");
145printf("%s\n\n\n","x"x80);
146printf("SECTION 1:\tThe exported symbols and their usage count\n\n");
147printf("%-25s\t%-25s\t%-5s\t%-25s\n", "Symbol", "Module", "Usage count",
148 "export type");
149
150#
151# print the list of unused exported symbols
152#
153foreach my $list (sort alphabetically values(%SYMBOL)) {
154 my ($module, $value, $symbol, $gpl) = @{$list};
155 printf("%-25s\t%-25s\t%-10s\t", $symbol, $module, $value);
156 if (defined $gpl) {
157 printf("%-25s\n",$gpl);
158 } else {
159 printf("\n");
160 }
161}
162printf("%s\n\n\n","x"x80);
163
164printf("SECTION 2:\n\tThis section reports export-symbol-usage of in-kernel
165modules. Each module lists the modules, and the symbols from that module that
166it uses. Each listed symbol reports the number of modules using it\n");
167
168print "~"x80 , "\n";
169while (my ($thismod, $list) = each %MODULE) {
170 my %depends;
171 $thismod =~ s/\.mod\.c/.ko/;
172 print "\t\t\t$thismod\n";
173 foreach my $symbol (@{$list}) {
174 my ($module, $value, undef, $gpl) = @{$SYMBOL{$symbol}};
175 push (@{$depends{"$module"}}, "$symbol $value");
176 }
177 print_depends_on(\%depends);
178}