export_report.pl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/env perl
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. # (C) Copyright IBM Corporation 2006.
  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. open my $fh, '< modules.order' or die "cannot open modules.order: $!\n";
  50. while (<$fh>) {
  51. s/\.ko$/.mod.c/;
  52. push (@file, $_)
  53. }
  54. close($fh);
  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, $namespace) = split('\t');
  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. }