Commit | Line | Data |
---|---|---|
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 | ||
10 | use Getopt::Std; | |
11 | use strict; | |
12 | ||
13 | sub numerically { | |
14 | my $no1 = (split /\s+/, $a)[1]; | |
15 | my $no2 = (split /\s+/, $b)[1]; | |
16 | return $no1 <=> $no2; | |
17 | } | |
18 | ||
19 | sub alphabetically { | |
20 | my ($module1, $value1) = @{$a}; | |
21 | my ($module2, $value2) = @{$b}; | |
22 | return $value1 <=> $value2 || $module2 cmp $module1; | |
23 | } | |
24 | ||
25 | sub print_depends_on { | |
26 | my ($href) = @_; | |
27 | print "\n"; | |
bdabc7a3 JC |
28 | for my $mod (sort keys %$href) { |
29 | my $list = $href->{$mod}; | |
c5e30033 RP |
30 | print "\t$mod:\n"; |
31 | foreach my $sym (sort numerically @{$list}) { | |
32 | my ($symbol, $no) = split /\s+/, $sym; | |
bdabc7a3 | 33 | printf("\t\t%-25s\n", $symbol); |
c5e30033 RP |
34 | } |
35 | print "\n"; | |
36 | } | |
37 | print "\n"; | |
38 | print "~"x80 , "\n"; | |
39 | } | |
40 | ||
41 | sub 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 | ||
52 | sub collectcfiles { | |
de7b0b41 JC |
53 | 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 | } | |
91416cfd SH |
61 | chomp @file; |
62 | return @file; | |
c5e30033 RP |
63 | } |
64 | ||
65 | my (%SYMBOL, %MODULE, %opt, @allcfiles); | |
66 | ||
67 | if (not getopts('hk:o:f',\%opt) or defined $opt{'h'}) { | |
68 | usage($0); | |
69 | } | |
70 | ||
71 | if (defined $opt{'f'}) { | |
72 | @allcfiles = @ARGV; | |
73 | } else { | |
74 | @allcfiles = collectcfiles(); | |
75 | } | |
76 | ||
77 | if (not defined $opt{'k'}) { | |
78 | $opt{'k'} = "Module.symvers"; | |
79 | } | |
80 | ||
91416cfd SH |
81 | open (my $module_symvers, '<', $opt{'k'}) |
82 | or die "Sorry, cannot open $opt{'k'}: $!\n"; | |
c5e30033 RP |
83 | |
84 | if (defined $opt{'o'}) { | |
91416cfd SH |
85 | open (my $out, '>', $opt{'o'}) |
86 | or die "Sorry, cannot open $opt{'o'} $!\n"; | |
87 | ||
88 | select $out; | |
c5e30033 | 89 | } |
91416cfd | 90 | |
c5e30033 RP |
91 | # |
92 | # collect all the symbols and their attributes from the | |
93 | # Module.symvers file | |
94 | # | |
91416cfd | 95 | while ( <$module_symvers> ) { |
c5e30033 RP |
96 | chomp; |
97 | my (undef, $symbol, $module, $gpl) = split; | |
98 | $SYMBOL { $symbol } = [ $module , "0" , $symbol, $gpl]; | |
99 | } | |
91416cfd | 100 | close($module_symvers); |
c5e30033 RP |
101 | |
102 | # | |
103 | # collect the usage count of each symbol. | |
104 | # | |
105 | foreach my $thismod (@allcfiles) { | |
91416cfd SH |
106 | my $module; |
107 | ||
108 | unless (open ($module, '<', $thismod)) { | |
109 | warn "Sorry, cannot open $thismod: $!\n"; | |
c5e30033 RP |
110 | next; |
111 | } | |
91416cfd | 112 | |
c5e30033 | 113 | my $state=0; |
91416cfd | 114 | while ( <$module> ) { |
c5e30033 | 115 | chomp; |
88f567f3 | 116 | if ($state == 0) { |
c5e30033 RP |
117 | $state = 1 if ($_ =~ /static const struct modversion_info/); |
118 | next; | |
119 | } | |
88f567f3 | 120 | if ($state == 1) { |
c5e30033 RP |
121 | $state = 2 if ($_ =~ /__attribute__\(\(section\("__versions"\)\)\)/); |
122 | next; | |
123 | } | |
88f567f3 | 124 | if ($state == 2) { |
cf9a6ade | 125 | if ( $_ !~ /0x[0-9a-f]+,/ ) { |
c5e30033 RP |
126 | next; |
127 | } | |
128 | my $sym = (split /([,"])/,)[4]; | |
129 | my ($module, $value, $symbol, $gpl) = @{$SYMBOL{$sym}}; | |
130 | $SYMBOL{ $sym } = [ $module, $value+1, $symbol, $gpl]; | |
131 | push(@{$MODULE{$thismod}} , $sym); | |
132 | } | |
133 | } | |
88f567f3 | 134 | if ($state != 2) { |
c5e30033 RP |
135 | print "WARNING:$thismod is not built with CONFIG_MODVERSION enabled\n"; |
136 | } | |
91416cfd | 137 | close($module); |
c5e30033 RP |
138 | } |
139 | ||
140 | print "\tThis file reports the exported symbols usage patterns by in-tree\n", | |
141 | "\t\t\t\tmodules\n"; | |
142 | printf("%s\n\n\n","x"x80); | |
143 | printf("\t\t\t\tINDEX\n\n\n"); | |
144 | printf("SECTION 1: Usage counts of all exported symbols\n"); | |
145 | printf("SECTION 2: List of modules and the exported symbols they use\n"); | |
146 | printf("%s\n\n\n","x"x80); | |
147 | printf("SECTION 1:\tThe exported symbols and their usage count\n\n"); | |
148 | printf("%-25s\t%-25s\t%-5s\t%-25s\n", "Symbol", "Module", "Usage count", | |
149 | "export type"); | |
150 | ||
151 | # | |
152 | # print the list of unused exported symbols | |
153 | # | |
154 | foreach my $list (sort alphabetically values(%SYMBOL)) { | |
155 | my ($module, $value, $symbol, $gpl) = @{$list}; | |
156 | printf("%-25s\t%-25s\t%-10s\t", $symbol, $module, $value); | |
157 | if (defined $gpl) { | |
158 | printf("%-25s\n",$gpl); | |
159 | } else { | |
160 | printf("\n"); | |
161 | } | |
162 | } | |
163 | printf("%s\n\n\n","x"x80); | |
164 | ||
165 | printf("SECTION 2:\n\tThis section reports export-symbol-usage of in-kernel | |
166 | modules. Each module lists the modules, and the symbols from that module that | |
167 | it uses. Each listed symbol reports the number of modules using it\n"); | |
168 | ||
169 | print "~"x80 , "\n"; | |
bdabc7a3 JC |
170 | for my $thismod (sort keys %MODULE) { |
171 | my $list = $MODULE{$thismod}; | |
c5e30033 RP |
172 | my %depends; |
173 | $thismod =~ s/\.mod\.c/.ko/; | |
174 | print "\t\t\t$thismod\n"; | |
175 | foreach my $symbol (@{$list}) { | |
176 | my ($module, $value, undef, $gpl) = @{$SYMBOL{$symbol}}; | |
177 | push (@{$depends{"$module"}}, "$symbol $value"); | |
178 | } | |
179 | print_depends_on(\%depends); | |
180 | } |