export_report.pl 4.2 KB

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