Back to home page

OSCL-LXR

 
 

    


0001 #!/usr/bin/env perl
0002 # SPDX-License-Identifier: GPL-2.0-only
0003 #
0004 # (C) Copyright IBM Corporation 2006.
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 warnings;
0011 use Getopt::Std;
0012 use strict;
0013 
0014 sub numerically {
0015     my $no1 = (split /\s+/, $a)[1];
0016     my $no2 = (split /\s+/, $b)[1];
0017     return $no1 <=> $no2;
0018 }
0019 
0020 sub alphabetically {
0021     my ($module1, $value1) = @{$a};
0022     my ($module2, $value2) = @{$b};
0023     return $value1 <=> $value2 || $module2 cmp $module1;
0024 }
0025 
0026 sub print_depends_on {
0027     my ($href) = @_;
0028     print "\n";
0029     for my $mod (sort keys %$href) {
0030         my $list = $href->{$mod};
0031         print "\t$mod:\n";
0032         foreach my $sym (sort numerically @{$list}) {
0033             my ($symbol, $no) = split /\s+/, $sym;
0034             printf("\t\t%-25s\n", $symbol);
0035         }
0036         print "\n";
0037     }
0038     print "\n";
0039     print "~"x80 , "\n";
0040 }
0041 
0042 sub usage {
0043         print "Usage: @_ -h -k Module.symvers  [ -o outputfile ] \n",
0044           "\t-f: treat all the non-option argument as .mod.c files. ",
0045           "Recommend using this as the last option\n",
0046           "\t-h: print detailed help\n",
0047           "\t-k: the path to Module.symvers file. By default uses ",
0048           "the file from the current directory\n",
0049           "\t-o outputfile: output the report to outputfile\n";
0050     exit 0;
0051 }
0052 
0053 sub collectcfiles {
0054     my @file;
0055     open my $fh, '< modules.order' or die "cannot open modules.order: $!\n";
0056     while (<$fh>) {
0057     s/\.ko$/.mod.c/;
0058     push (@file, $_)
0059     }
0060     close($fh);
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, $namespace) = split('\t');
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 }