export_report.pl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/env perl
  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. use warnings;
  10. use Getopt::Std;
  11. use strict;
  12. sub numerically {
  13. my $no1 = (split /\s+/, $a)[1];
  14. my $no2 = (split /\s+/, $b)[1];
  15. return $no1 <=> $no2;
  16. }
  17. sub alphabetically {
  18. my ($module1, $value1) = @{$a};
  19. my ($module2, $value2) = @{$b};
  20. return $value1 <=> $value2 || $module2 cmp $module1;
  21. }
  22. sub print_depends_on {
  23. my ($href) = @_;
  24. print "\n";
  25. for my $mod (sort keys %$href) {
  26. my $list = $href->{$mod};
  27. print "\t$mod:\n";
  28. foreach my $sym (sort numerically @{$list}) {
  29. my ($symbol, $no) = split /\s+/, $sym;
  30. printf("\t\t%-25s\n", $symbol);
  31. }
  32. print "\n";
  33. }
  34. print "\n";
  35. print "~"x80 , "\n";
  36. }
  37. sub usage {
  38. print "Usage: @_ -h -k Module.symvers [ -o outputfile ] \n",
  39. "\t-f: treat all the non-option argument as .mod.c files. ",
  40. "Recommend using this as the last option\n",
  41. "\t-h: print detailed help\n",
  42. "\t-k: the path to Module.symvers file. By default uses ",
  43. "the file from the current directory\n",
  44. "\t-o outputfile: output the report to outputfile\n";
  45. exit 0;
  46. }
  47. sub collectcfiles {
  48. my @file;
  49. while (<.tmp_versions/*.mod>) {
  50. open my $fh, '<', $_ or die "cannot open $_: $!\n";
  51. push (@file,
  52. grep s/\.ko/.mod.c/, # change the suffix
  53. grep m/.+\.ko/, # find the .ko path
  54. <$fh>); # lines in opened file
  55. }
  56. chomp @file;
  57. return @file;
  58. }
  59. my (%SYMBOL, %MODULE, %opt, @allcfiles);
  60. if (not getopts('hk:o:f',\%opt) or defined $opt{'h'}) {
  61. usage($0);
  62. }
  63. if (defined $opt{'f'}) {
  64. @allcfiles = @ARGV;
  65. } else {
  66. @allcfiles = collectcfiles();
  67. }
  68. if (not defined $opt{'k'}) {
  69. $opt{'k'} = "Module.symvers";
  70. }
  71. open (my $module_symvers, '<', $opt{'k'})
  72. or die "Sorry, cannot open $opt{'k'}: $!\n";
  73. if (defined $opt{'o'}) {
  74. open (my $out, '>', $opt{'o'})
  75. or die "Sorry, cannot open $opt{'o'} $!\n";
  76. select $out;
  77. }
  78. #
  79. # collect all the symbols and their attributes from the
  80. # Module.symvers file
  81. #
  82. while ( <$module_symvers> ) {
  83. chomp;
  84. my (undef, $symbol, $module, $gpl) = split;
  85. $SYMBOL { $symbol } = [ $module , "0" , $symbol, $gpl];
  86. }
  87. close($module_symvers);
  88. #
  89. # collect the usage count of each symbol.
  90. #
  91. my $modversion_warnings = 0;
  92. foreach my $thismod (@allcfiles) {
  93. my $module;
  94. unless (open ($module, '<', $thismod)) {
  95. warn "Sorry, cannot open $thismod: $!\n";
  96. next;
  97. }
  98. my $state=0;
  99. while ( <$module> ) {
  100. chomp;
  101. if ($state == 0) {
  102. $state = 1 if ($_ =~ /static const struct modversion_info/);
  103. next;
  104. }
  105. if ($state == 1) {
  106. $state = 2 if ($_ =~ /__attribute__\(\(section\("__versions"\)\)\)/);
  107. next;
  108. }
  109. if ($state == 2) {
  110. if ( $_ !~ /0x[0-9a-f]+,/ ) {
  111. next;
  112. }
  113. my $sym = (split /([,"])/,)[4];
  114. my ($module, $value, $symbol, $gpl) = @{$SYMBOL{$sym}};
  115. $SYMBOL{ $sym } = [ $module, $value+1, $symbol, $gpl];
  116. push(@{$MODULE{$thismod}} , $sym);
  117. }
  118. }
  119. if ($state != 2) {
  120. warn "WARNING:$thismod is not built with CONFIG_MODVERSIONS enabled\n";
  121. $modversion_warnings++;
  122. }
  123. close($module);
  124. }
  125. print "\tThis file reports the exported symbols usage patterns by in-tree\n",
  126. "\t\t\t\tmodules\n";
  127. printf("%s\n\n\n","x"x80);
  128. printf("\t\t\t\tINDEX\n\n\n");
  129. printf("SECTION 1: Usage counts of all exported symbols\n");
  130. printf("SECTION 2: List of modules and the exported symbols they use\n");
  131. printf("%s\n\n\n","x"x80);
  132. printf("SECTION 1:\tThe exported symbols and their usage count\n\n");
  133. printf("%-25s\t%-25s\t%-5s\t%-25s\n", "Symbol", "Module", "Usage count",
  134. "export type");
  135. #
  136. # print the list of unused exported symbols
  137. #
  138. foreach my $list (sort alphabetically values(%SYMBOL)) {
  139. my ($module, $value, $symbol, $gpl) = @{$list};
  140. printf("%-25s\t%-25s\t%-10s\t", $symbol, $module, $value);
  141. if (defined $gpl) {
  142. printf("%-25s\n",$gpl);
  143. } else {
  144. printf("\n");
  145. }
  146. }
  147. printf("%s\n\n\n","x"x80);
  148. printf("SECTION 2:\n\tThis section reports export-symbol-usage of in-kernel
  149. modules. Each module lists the modules, and the symbols from that module that
  150. it uses. Each listed symbol reports the number of modules using it\n");
  151. print "\nNOTE: Got $modversion_warnings CONFIG_MODVERSIONS warnings\n\n"
  152. if $modversion_warnings;
  153. print "~"x80 , "\n";
  154. for my $thismod (sort keys %MODULE) {
  155. my $list = $MODULE{$thismod};
  156. my %depends;
  157. $thismod =~ s/\.mod\.c/.ko/;
  158. print "\t\t\t$thismod\n";
  159. foreach my $symbol (@{$list}) {
  160. my ($module, $value, undef, $gpl) = @{$SYMBOL{$symbol}};
  161. push (@{$depends{"$module"}}, "$symbol $value");
  162. }
  163. print_depends_on(\%depends);
  164. }