Kamil Rytarowski | cb77f0d | 2017-05-07 23:25:26 +0200 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Thomas Gleixner | 59bd9de | 2019-05-28 10:10:12 -0700 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0-only |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 3 | # |
| 4 | # (C) Copyright IBM Corporation 2006. |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 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 Rytarowski | cb77f0d | 2017-05-07 23:25:26 +0200 | [diff] [blame] | 10 | use warnings; |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 11 | use Getopt::Std; |
| 12 | use strict; |
| 13 | |
| 14 | sub numerically { |
| 15 | my $no1 = (split /\s+/, $a)[1]; |
| 16 | my $no2 = (split /\s+/, $b)[1]; |
| 17 | return $no1 <=> $no2; |
| 18 | } |
| 19 | |
| 20 | sub alphabetically { |
| 21 | my ($module1, $value1) = @{$a}; |
| 22 | my ($module2, $value2) = @{$b}; |
| 23 | return $value1 <=> $value2 || $module2 cmp $module1; |
| 24 | } |
| 25 | |
| 26 | sub print_depends_on { |
| 27 | my ($href) = @_; |
| 28 | print "\n"; |
Jim Cromie | bdabc7a | 2011-05-23 12:44:56 -0600 | [diff] [blame] | 29 | for my $mod (sort keys %$href) { |
| 30 | my $list = $href->{$mod}; |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 31 | print "\t$mod:\n"; |
| 32 | foreach my $sym (sort numerically @{$list}) { |
| 33 | my ($symbol, $no) = split /\s+/, $sym; |
Jim Cromie | bdabc7a | 2011-05-23 12:44:56 -0600 | [diff] [blame] | 34 | printf("\t\t%-25s\n", $symbol); |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 35 | } |
| 36 | print "\n"; |
| 37 | } |
| 38 | print "\n"; |
| 39 | print "~"x80 , "\n"; |
| 40 | } |
| 41 | |
| 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; |
| 51 | } |
| 52 | |
| 53 | sub collectcfiles { |
Jim Cromie | de7b0b4 | 2011-05-23 12:44:55 -0600 | [diff] [blame] | 54 | my @file; |
Masahiro Yamada | 7deb55f | 2019-07-17 15:17:56 +0900 | [diff] [blame] | 55 | open my $fh, '< modules.order' or die "cannot open modules.order: $!\n"; |
| 56 | while (<$fh>) { |
| 57 | s/\.ko$/.mod.c/; |
| 58 | push (@file, $_) |
Jim Cromie | de7b0b4 | 2011-05-23 12:44:55 -0600 | [diff] [blame] | 59 | } |
Masahiro Yamada | 7deb55f | 2019-07-17 15:17:56 +0900 | [diff] [blame] | 60 | close($fh); |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame] | 61 | chomp @file; |
| 62 | return @file; |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 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 | |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame] | 81 | open (my $module_symvers, '<', $opt{'k'}) |
| 82 | or die "Sorry, cannot open $opt{'k'}: $!\n"; |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 83 | |
| 84 | if (defined $opt{'o'}) { |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame] | 85 | open (my $out, '>', $opt{'o'}) |
| 86 | or die "Sorry, cannot open $opt{'o'} $!\n"; |
| 87 | |
| 88 | select $out; |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 89 | } |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame] | 90 | |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 91 | # |
| 92 | # collect all the symbols and their attributes from the |
| 93 | # Module.symvers file |
| 94 | # |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame] | 95 | while ( <$module_symvers> ) { |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 96 | chomp; |
Matthias Maennich | cb9b55d | 2019-09-06 11:32:28 +0100 | [diff] [blame] | 97 | my (undef, $symbol, $namespace, $module, $gpl) = split('\t'); |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 98 | $SYMBOL { $symbol } = [ $module , "0" , $symbol, $gpl]; |
| 99 | } |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame] | 100 | close($module_symvers); |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 101 | |
| 102 | # |
| 103 | # collect the usage count of each symbol. |
| 104 | # |
Jim Cromie | ca995cb | 2011-05-23 12:44:57 -0600 | [diff] [blame] | 105 | my $modversion_warnings = 0; |
| 106 | |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 107 | foreach my $thismod (@allcfiles) { |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame] | 108 | my $module; |
| 109 | |
| 110 | unless (open ($module, '<', $thismod)) { |
| 111 | warn "Sorry, cannot open $thismod: $!\n"; |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 112 | next; |
| 113 | } |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame] | 114 | |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 115 | my $state=0; |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame] | 116 | while ( <$module> ) { |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 117 | chomp; |
Ram Pai | 88f567f | 2007-08-24 15:32:22 -0700 | [diff] [blame] | 118 | if ($state == 0) { |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 119 | $state = 1 if ($_ =~ /static const struct modversion_info/); |
| 120 | next; |
| 121 | } |
Ram Pai | 88f567f | 2007-08-24 15:32:22 -0700 | [diff] [blame] | 122 | if ($state == 1) { |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 123 | $state = 2 if ($_ =~ /__attribute__\(\(section\("__versions"\)\)\)/); |
| 124 | next; |
| 125 | } |
Ram Pai | 88f567f | 2007-08-24 15:32:22 -0700 | [diff] [blame] | 126 | if ($state == 2) { |
Adrian Bunk | cf9a6ad | 2007-08-24 23:04:51 +0200 | [diff] [blame] | 127 | if ( $_ !~ /0x[0-9a-f]+,/ ) { |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 128 | 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 Pai | 88f567f | 2007-08-24 15:32:22 -0700 | [diff] [blame] | 136 | if ($state != 2) { |
Jim Cromie | ca995cb | 2011-05-23 12:44:57 -0600 | [diff] [blame] | 137 | warn "WARNING:$thismod is not built with CONFIG_MODVERSIONS enabled\n"; |
| 138 | $modversion_warnings++; |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 139 | } |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame] | 140 | close($module); |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | print "\tThis file reports the exported symbols usage patterns by in-tree\n", |
| 144 | "\t\t\t\tmodules\n"; |
| 145 | printf("%s\n\n\n","x"x80); |
| 146 | printf("\t\t\t\tINDEX\n\n\n"); |
| 147 | printf("SECTION 1: Usage counts of all exported symbols\n"); |
| 148 | printf("SECTION 2: List of modules and the exported symbols they use\n"); |
| 149 | printf("%s\n\n\n","x"x80); |
| 150 | printf("SECTION 1:\tThe exported symbols and their usage count\n\n"); |
| 151 | printf("%-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 | # |
| 157 | foreach 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 | } |
| 166 | printf("%s\n\n\n","x"x80); |
| 167 | |
| 168 | printf("SECTION 2:\n\tThis section reports export-symbol-usage of in-kernel |
| 169 | modules. Each module lists the modules, and the symbols from that module that |
| 170 | it uses. Each listed symbol reports the number of modules using it\n"); |
| 171 | |
Jim Cromie | ca995cb | 2011-05-23 12:44:57 -0600 | [diff] [blame] | 172 | print "\nNOTE: Got $modversion_warnings CONFIG_MODVERSIONS warnings\n\n" |
| 173 | if $modversion_warnings; |
| 174 | |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 175 | print "~"x80 , "\n"; |
Jim Cromie | bdabc7a | 2011-05-23 12:44:56 -0600 | [diff] [blame] | 176 | for my $thismod (sort keys %MODULE) { |
| 177 | my $list = $MODULE{$thismod}; |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 178 | 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 | } |