export_report.pl 4.5 KB

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