export_report.pl 4.3 KB

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