Back to home page

LXR

 
 

    


0001 #!/usr/bin/perl -w
0002 #
0003 # (C) Copyright IBM Corporation 2006.
0004 #   Released under GPL v2.
0005 #   Author : Ram Pai (linuxram@us.ibm.com)
0006 #
0007 # Usage: export_report.pl -k Module.symvers [-o report_file ] -f *.mod.c
0008 #
0009 
0010 use Getopt::Std;
0011 use strict;
0012 
0013 sub numerically {
0014     my $no1 = (split /\s+/, $a)[1];
0015     my $no2 = (split /\s+/, $b)[1];
0016     return $no1 <=> $no2;
0017 }
0018 
0019 sub alphabetically {
0020     my ($module1, $value1) = @{$a};
0021     my ($module2, $value2) = @{$b};
0022     return $value1 <=> $value2 || $module2 cmp $module1;
0023 }
0024 
0025 sub print_depends_on {
0026     my ($href) = @_;
0027     print "\n";
0028     for my $mod (sort keys %$href) {
0029         my $list = $href->{$mod};
0030         print "\t$mod:\n";
0031         foreach my $sym (sort numerically @{$list}) {
0032             my ($symbol, $no) = split /\s+/, $sym;
0033             printf("\t\t%-25s\n", $symbol);
0034         }
0035         print "\n";
0036     }
0037     print "\n";
0038     print "~"x80 , "\n";
0039 }
0040 
0041 sub usage {
0042         print "Usage: @_ -h -k Module.symvers  [ -o outputfile ] \n",
0043           "\t-f: treat all the non-option argument as .mod.c files. ",
0044           "Recommend using this as the last option\n",
0045           "\t-h: print detailed help\n",
0046           "\t-k: the path to Module.symvers file. By default uses ",
0047           "the file from the current directory\n",
0048           "\t-o outputfile: output the report to outputfile\n";
0049     exit 0;
0050 }
0051 
0052 sub collectcfiles {
0053     my @file;
0054     while (<.tmp_versions/*.mod>) {
0055     open my $fh, '<', $_ or die "cannot open $_: $!\n";
0056     push (@file,
0057           grep s/\.ko/.mod.c/,  # change the suffix
0058           grep m/.+\.ko/,       # find the .ko path
0059           <$fh>);           # lines in opened file
0060     }
0061     chomp @file;
0062     return @file;
0063 }
0064 
0065 my (%SYMBOL, %MODULE, %opt, @allcfiles);
0066 
0067 if (not getopts('hk:o:f',\%opt) or defined $opt{'h'}) {
0068         usage($0);
0069 }
0070 
0071 if (defined $opt{'f'}) {
0072     @allcfiles = @ARGV;
0073 } else {
0074     @allcfiles = collectcfiles();
0075 }
0076 
0077 if (not defined $opt{'k'}) {
0078     $opt{'k'} = "Module.symvers";
0079 }
0080 
0081 open (my $module_symvers, '<', $opt{'k'})
0082     or die "Sorry, cannot open $opt{'k'}: $!\n";
0083 
0084 if (defined $opt{'o'}) {
0085     open (my $out, '>', $opt{'o'})
0086     or die "Sorry, cannot open $opt{'o'} $!\n";
0087 
0088     select $out;
0089 }
0090 
0091 #
0092 # collect all the symbols and their attributes from the
0093 # Module.symvers file
0094 #
0095 while ( <$module_symvers> ) {
0096     chomp;
0097     my (undef, $symbol, $module, $gpl) = split;
0098     $SYMBOL { $symbol } =  [ $module , "0" , $symbol, $gpl];
0099 }
0100 close($module_symvers);
0101 
0102 #
0103 # collect the usage count of each symbol.
0104 #
0105 my $modversion_warnings = 0;
0106 
0107 foreach my $thismod (@allcfiles) {
0108     my $module;
0109 
0110     unless (open ($module, '<', $thismod)) {
0111         warn "Sorry, cannot open $thismod: $!\n";
0112         next;
0113     }
0114 
0115     my $state=0;
0116     while ( <$module> ) {
0117         chomp;
0118         if ($state == 0) {
0119             $state = 1 if ($_ =~ /static const struct modversion_info/);
0120             next;
0121         }
0122         if ($state == 1) {
0123             $state = 2 if ($_ =~ /__attribute__\(\(section\("__versions"\)\)\)/);
0124             next;
0125         }
0126         if ($state == 2) {
0127             if ( $_ !~ /0x[0-9a-f]+,/ ) {
0128                 next;
0129             }
0130             my $sym = (split /([,"])/,)[4];
0131             my ($module, $value, $symbol, $gpl) = @{$SYMBOL{$sym}};
0132             $SYMBOL{ $sym } =  [ $module, $value+1, $symbol, $gpl];
0133             push(@{$MODULE{$thismod}} , $sym);
0134         }
0135     }
0136     if ($state != 2) {
0137         warn "WARNING:$thismod is not built with CONFIG_MODVERSIONS enabled\n";
0138         $modversion_warnings++;
0139     }
0140     close($module);
0141 }
0142 
0143 print "\tThis file reports the exported symbols usage patterns by in-tree\n",
0144     "\t\t\t\tmodules\n";
0145 printf("%s\n\n\n","x"x80);
0146 printf("\t\t\t\tINDEX\n\n\n");
0147 printf("SECTION 1: Usage counts of all exported symbols\n");
0148 printf("SECTION 2: List of modules and the exported symbols they use\n");
0149 printf("%s\n\n\n","x"x80);
0150 printf("SECTION 1:\tThe exported symbols and their usage count\n\n");
0151 printf("%-25s\t%-25s\t%-5s\t%-25s\n", "Symbol", "Module", "Usage count",
0152     "export type");
0153 
0154 #
0155 # print the list of unused exported symbols
0156 #
0157 foreach my $list (sort alphabetically values(%SYMBOL)) {
0158     my ($module, $value, $symbol, $gpl) = @{$list};
0159     printf("%-25s\t%-25s\t%-10s\t", $symbol, $module, $value);
0160     if (defined $gpl) {
0161         printf("%-25s\n",$gpl);
0162     } else {
0163         printf("\n");
0164     }
0165 }
0166 printf("%s\n\n\n","x"x80);
0167 
0168 printf("SECTION 2:\n\tThis section reports export-symbol-usage of in-kernel
0169 modules. Each module lists the modules, and the symbols from that module that
0170 it uses.  Each listed symbol reports the number of modules using it\n");
0171 
0172 print "\nNOTE: Got $modversion_warnings CONFIG_MODVERSIONS warnings\n\n"
0173     if $modversion_warnings;
0174 
0175 print "~"x80 , "\n";
0176 for my $thismod (sort keys %MODULE) {
0177     my $list = $MODULE{$thismod};
0178     my %depends;
0179     $thismod =~ s/\.mod\.c/.ko/;
0180     print "\t\t\t$thismod\n";
0181     foreach my $symbol (@{$list}) {
0182         my ($module, $value, undef, $gpl) = @{$SYMBOL{$symbol}};
0183         push (@{$depends{"$module"}}, "$symbol $value");
0184     }
0185     print_depends_on(\%depends);
0186 }