export_report.pl 4.2 KB

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