Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 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"; |
| 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 | |
| 40 | sub 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 | |
| 51 | sub collectcfiles { |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame^] | 52 | my @file |
| 53 | = `cat .tmp_versions/*.mod | grep '.*\.ko\$' | sed s/\.ko$/.mod.c/`; |
| 54 | chomp @file; |
| 55 | return @file; |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | my (%SYMBOL, %MODULE, %opt, @allcfiles); |
| 59 | |
| 60 | if (not getopts('hk:o:f',\%opt) or defined $opt{'h'}) { |
| 61 | usage($0); |
| 62 | } |
| 63 | |
| 64 | if (defined $opt{'f'}) { |
| 65 | @allcfiles = @ARGV; |
| 66 | } else { |
| 67 | @allcfiles = collectcfiles(); |
| 68 | } |
| 69 | |
| 70 | if (not defined $opt{'k'}) { |
| 71 | $opt{'k'} = "Module.symvers"; |
| 72 | } |
| 73 | |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame^] | 74 | open (my $module_symvers, '<', $opt{'k'}) |
| 75 | or die "Sorry, cannot open $opt{'k'}: $!\n"; |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 76 | |
| 77 | if (defined $opt{'o'}) { |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame^] | 78 | open (my $out, '>', $opt{'o'}) |
| 79 | or die "Sorry, cannot open $opt{'o'} $!\n"; |
| 80 | |
| 81 | select $out; |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 82 | } |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame^] | 83 | |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 84 | # |
| 85 | # collect all the symbols and their attributes from the |
| 86 | # Module.symvers file |
| 87 | # |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame^] | 88 | while ( <$module_symvers> ) { |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 89 | chomp; |
| 90 | my (undef, $symbol, $module, $gpl) = split; |
| 91 | $SYMBOL { $symbol } = [ $module , "0" , $symbol, $gpl]; |
| 92 | } |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame^] | 93 | close($module_symvers); |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 94 | |
| 95 | # |
| 96 | # collect the usage count of each symbol. |
| 97 | # |
| 98 | foreach my $thismod (@allcfiles) { |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame^] | 99 | my $module; |
| 100 | |
| 101 | unless (open ($module, '<', $thismod)) { |
| 102 | warn "Sorry, cannot open $thismod: $!\n"; |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 103 | next; |
| 104 | } |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame^] | 105 | |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 106 | my $state=0; |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame^] | 107 | while ( <$module> ) { |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 108 | chomp; |
Ram Pai | 88f567f | 2007-08-24 15:32:22 -0700 | [diff] [blame] | 109 | if ($state == 0) { |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 110 | $state = 1 if ($_ =~ /static const struct modversion_info/); |
| 111 | next; |
| 112 | } |
Ram Pai | 88f567f | 2007-08-24 15:32:22 -0700 | [diff] [blame] | 113 | if ($state == 1) { |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 114 | $state = 2 if ($_ =~ /__attribute__\(\(section\("__versions"\)\)\)/); |
| 115 | next; |
| 116 | } |
Ram Pai | 88f567f | 2007-08-24 15:32:22 -0700 | [diff] [blame] | 117 | if ($state == 2) { |
Adrian Bunk | cf9a6ad | 2007-08-24 23:04:51 +0200 | [diff] [blame] | 118 | if ( $_ !~ /0x[0-9a-f]+,/ ) { |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 119 | next; |
| 120 | } |
| 121 | my $sym = (split /([,"])/,)[4]; |
| 122 | my ($module, $value, $symbol, $gpl) = @{$SYMBOL{$sym}}; |
| 123 | $SYMBOL{ $sym } = [ $module, $value+1, $symbol, $gpl]; |
| 124 | push(@{$MODULE{$thismod}} , $sym); |
| 125 | } |
| 126 | } |
Ram Pai | 88f567f | 2007-08-24 15:32:22 -0700 | [diff] [blame] | 127 | if ($state != 2) { |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 128 | print "WARNING:$thismod is not built with CONFIG_MODVERSION enabled\n"; |
| 129 | } |
Stephen Hemminger | 91416cf | 2010-02-22 15:17:22 -0800 | [diff] [blame^] | 130 | close($module); |
Ram Pai | c5e3003 | 2006-06-23 16:44:38 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | print "\tThis file reports the exported symbols usage patterns by in-tree\n", |
| 134 | "\t\t\t\tmodules\n"; |
| 135 | printf("%s\n\n\n","x"x80); |
| 136 | printf("\t\t\t\tINDEX\n\n\n"); |
| 137 | printf("SECTION 1: Usage counts of all exported symbols\n"); |
| 138 | printf("SECTION 2: List of modules and the exported symbols they use\n"); |
| 139 | printf("%s\n\n\n","x"x80); |
| 140 | printf("SECTION 1:\tThe exported symbols and their usage count\n\n"); |
| 141 | printf("%-25s\t%-25s\t%-5s\t%-25s\n", "Symbol", "Module", "Usage count", |
| 142 | "export type"); |
| 143 | |
| 144 | # |
| 145 | # print the list of unused exported symbols |
| 146 | # |
| 147 | foreach my $list (sort alphabetically values(%SYMBOL)) { |
| 148 | my ($module, $value, $symbol, $gpl) = @{$list}; |
| 149 | printf("%-25s\t%-25s\t%-10s\t", $symbol, $module, $value); |
| 150 | if (defined $gpl) { |
| 151 | printf("%-25s\n",$gpl); |
| 152 | } else { |
| 153 | printf("\n"); |
| 154 | } |
| 155 | } |
| 156 | printf("%s\n\n\n","x"x80); |
| 157 | |
| 158 | printf("SECTION 2:\n\tThis section reports export-symbol-usage of in-kernel |
| 159 | modules. Each module lists the modules, and the symbols from that module that |
| 160 | it uses. Each listed symbol reports the number of modules using it\n"); |
| 161 | |
| 162 | print "~"x80 , "\n"; |
| 163 | while (my ($thismod, $list) = each %MODULE) { |
| 164 | my %depends; |
| 165 | $thismod =~ s/\.mod\.c/.ko/; |
| 166 | print "\t\t\t$thismod\n"; |
| 167 | foreach my $symbol (@{$list}) { |
| 168 | my ($module, $value, undef, $gpl) = @{$SYMBOL{$symbol}}; |
| 169 | push (@{$depends{"$module"}}, "$symbol $value"); |
| 170 | } |
| 171 | print_depends_on(\%depends); |
| 172 | } |