Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

420 lines
11 KiB

  1. package B::Xref;
  2. =head1 NAME
  3. B::Xref - Generates cross reference reports for Perl programs
  4. =head1 SYNOPSIS
  5. perl -MO=Xref[,OPTIONS] foo.pl
  6. =head1 DESCRIPTION
  7. The B::Xref module is used to generate a cross reference listing of all
  8. definitions and uses of variables, subroutines and formats in a Perl program.
  9. It is implemented as a backend for the Perl compiler.
  10. The report generated is in the following format:
  11. File filename1
  12. Subroutine subname1
  13. Package package1
  14. object1 C<line numbers>
  15. object2 C<line numbers>
  16. ...
  17. Package package2
  18. ...
  19. Each B<File> section reports on a single file. Each B<Subroutine> section
  20. reports on a single subroutine apart from the special cases
  21. "(definitions)" and "(main)". These report, respectively, on subroutine
  22. definitions found by the initial symbol table walk and on the main part of
  23. the program or module external to all subroutines.
  24. The report is then grouped by the B<Package> of each variable,
  25. subroutine or format with the special case "(lexicals)" meaning
  26. lexical variables. Each B<object> name (implicitly qualified by its
  27. containing B<Package>) includes its type character(s) at the beginning
  28. where possible. Lexical variables are easier to track and even
  29. included dereferencing information where possible.
  30. The C<line numbers> are a comma separated list of line numbers (some
  31. preceded by code letters) where that object is used in some way.
  32. Simple uses aren't preceded by a code letter. Introductions (such as
  33. where a lexical is first defined with C<my>) are indicated with the
  34. letter "i". Subroutine and method calls are indicated by the character
  35. "&". Subroutine definitions are indicated by "s" and format
  36. definitions by "f".
  37. =head1 OPTIONS
  38. Option words are separated by commas (not whitespace) and follow the
  39. usual conventions of compiler backend options.
  40. =over 8
  41. =item C<-oFILENAME>
  42. Directs output to C<FILENAME> instead of standard output.
  43. =item C<-r>
  44. Raw output. Instead of producing a human-readable report, outputs a line
  45. in machine-readable form for each definition/use of a variable/sub/format.
  46. =item C<-D[tO]>
  47. (Internal) debug options, probably only useful if C<-r> included.
  48. The C<t> option prints the object on the top of the stack as it's
  49. being tracked. The C<O> option prints each operator as it's being
  50. processed in the execution order of the program.
  51. =back
  52. =head1 BUGS
  53. Non-lexical variables are quite difficult to track through a program.
  54. Sometimes the type of a non-lexical variable's use is impossible to
  55. determine. Introductions of non-lexical non-scalars don't seem to be
  56. reported properly.
  57. =head1 AUTHOR
  58. Malcolm Beattie, mbeattie@sable.ox.ac.uk.
  59. =cut
  60. use strict;
  61. use Config;
  62. use B qw(peekop class comppadlist main_start svref_2object walksymtable
  63. OPpLVAL_INTRO SVf_POK
  64. );
  65. sub UNKNOWN { ["?", "?", "?"] }
  66. my @pad; # lexicals in current pad
  67. # as ["(lexical)", type, name]
  68. my %done; # keyed by $$op: set when each $op is done
  69. my $top = UNKNOWN; # shadows top element of stack as
  70. # [pack, type, name] (pack can be "(lexical)")
  71. my $file; # shadows current filename
  72. my $line; # shadows current line number
  73. my $subname; # shadows current sub name
  74. my %table; # Multi-level hash to record all uses etc.
  75. my @todo = (); # List of CVs that need processing
  76. my %code = (intro => "i", used => "",
  77. subdef => "s", subused => "&",
  78. formdef => "f", meth => "->");
  79. # Options
  80. my ($debug_op, $debug_top, $nodefs, $raw);
  81. sub process {
  82. my ($var, $event) = @_;
  83. my ($pack, $type, $name) = @$var;
  84. if ($type eq "*") {
  85. if ($event eq "used") {
  86. return;
  87. } elsif ($event eq "subused") {
  88. $type = "&";
  89. }
  90. }
  91. $type =~ s/(.)\*$/$1/g;
  92. if ($raw) {
  93. printf "%-16s %-12s %5d %-12s %4s %-16s %s\n",
  94. $file, $subname, $line, $pack, $type, $name, $event;
  95. } else {
  96. # Wheee
  97. push(@{$table{$file}->{$subname}->{$pack}->{$type.$name}->{$event}},
  98. $line);
  99. }
  100. }
  101. sub load_pad {
  102. my $padlist = shift;
  103. my ($namelistav, $vallistav, @namelist, $ix);
  104. @pad = ();
  105. return if class($padlist) eq "SPECIAL";
  106. ($namelistav,$vallistav) = $padlist->ARRAY;
  107. @namelist = $namelistav->ARRAY;
  108. for ($ix = 1; $ix < @namelist; $ix++) {
  109. my $namesv = $namelist[$ix];
  110. next if class($namesv) eq "SPECIAL";
  111. my ($type, $name) = $namesv->PV =~ /^(.)([^\0]*)(\0.*)?$/;
  112. $pad[$ix] = ["(lexical)", $type, $name];
  113. }
  114. if ($Config{useithreads}) {
  115. my (@vallist);
  116. @vallist = $vallistav->ARRAY;
  117. for ($ix = 1; $ix < @vallist; $ix++) {
  118. my $valsv = $vallist[$ix];
  119. next unless class($valsv) eq "GV";
  120. # these pad GVs don't have corresponding names, so same @pad
  121. # array can be used without collisions
  122. $pad[$ix] = [$valsv->STASH->NAME, "*", $valsv->NAME];
  123. }
  124. }
  125. }
  126. sub xref {
  127. my $start = shift;
  128. my $op;
  129. for ($op = $start; $$op; $op = $op->next) {
  130. last if $done{$$op}++;
  131. warn sprintf("top = [%s, %s, %s]\n", @$top) if $debug_top;
  132. warn peekop($op), "\n" if $debug_op;
  133. my $opname = $op->name;
  134. if ($opname =~ /^(or|and|mapwhile|grepwhile|range|cond_expr)$/) {
  135. xref($op->other);
  136. } elsif ($opname eq "match" || $opname eq "subst") {
  137. xref($op->pmreplstart);
  138. } elsif ($opname eq "substcont") {
  139. xref($op->other->pmreplstart);
  140. $op = $op->other;
  141. redo;
  142. } elsif ($opname eq "enterloop") {
  143. xref($op->redoop);
  144. xref($op->nextop);
  145. xref($op->lastop);
  146. } elsif ($opname eq "subst") {
  147. xref($op->pmreplstart);
  148. } else {
  149. no strict 'refs';
  150. my $ppname = "pp_$opname";
  151. &$ppname($op) if defined(&$ppname);
  152. }
  153. }
  154. }
  155. sub xref_cv {
  156. my $cv = shift;
  157. my $pack = $cv->GV->STASH->NAME;
  158. $subname = ($pack eq "main" ? "" : "$pack\::") . $cv->GV->NAME;
  159. load_pad($cv->PADLIST);
  160. xref($cv->START);
  161. $subname = "(main)";
  162. }
  163. sub xref_object {
  164. my $cvref = shift;
  165. xref_cv(svref_2object($cvref));
  166. }
  167. sub xref_main {
  168. $subname = "(main)";
  169. load_pad(comppadlist);
  170. xref(main_start);
  171. while (@todo) {
  172. xref_cv(shift @todo);
  173. }
  174. }
  175. sub pp_nextstate {
  176. my $op = shift;
  177. $file = $op->file;
  178. $line = $op->line;
  179. $top = UNKNOWN;
  180. }
  181. sub pp_padsv {
  182. my $op = shift;
  183. $top = $pad[$op->targ];
  184. process($top, $op->private & OPpLVAL_INTRO ? "intro" : "used");
  185. }
  186. sub pp_padav { pp_padsv(@_) }
  187. sub pp_padhv { pp_padsv(@_) }
  188. sub deref {
  189. my ($var, $as) = @_;
  190. $var->[1] = $as . $var->[1];
  191. process($var, "used");
  192. }
  193. sub pp_rv2cv { deref($top, "&"); }
  194. sub pp_rv2hv { deref($top, "%"); }
  195. sub pp_rv2sv { deref($top, "\$"); }
  196. sub pp_rv2av { deref($top, "\@"); }
  197. sub pp_rv2gv { deref($top, "*"); }
  198. sub pp_gvsv {
  199. my $op = shift;
  200. my $gv;
  201. if ($Config{useithreads}) {
  202. $top = $pad[$op->padix];
  203. $top = UNKNOWN unless $top;
  204. $top->[1] = '$';
  205. }
  206. else {
  207. $gv = $op->gv;
  208. $top = [$gv->STASH->NAME, '$', $gv->NAME];
  209. }
  210. process($top, $op->private & OPpLVAL_INTRO ? "intro" : "used");
  211. }
  212. sub pp_gv {
  213. my $op = shift;
  214. my $gv;
  215. if ($Config{useithreads}) {
  216. $top = $pad[$op->padix];
  217. $top = UNKNOWN unless $top;
  218. $top->[1] = '*';
  219. }
  220. else {
  221. $gv = $op->gv;
  222. $top = [$gv->STASH->NAME, "*", $gv->NAME];
  223. }
  224. process($top, $op->private & OPpLVAL_INTRO ? "intro" : "used");
  225. }
  226. sub pp_const {
  227. my $op = shift;
  228. my $sv = $op->sv;
  229. # constant could be in the pad (under useithreads)
  230. if ($$sv) {
  231. $top = ["?", "",
  232. (class($sv) ne "SPECIAL" && $sv->FLAGS & SVf_POK) ? $sv->PV : "?"];
  233. }
  234. else {
  235. $top = $pad[$op->targ];
  236. }
  237. }
  238. sub pp_method {
  239. my $op = shift;
  240. $top = ["(method)", "->".$top->[1], $top->[2]];
  241. }
  242. sub pp_entersub {
  243. my $op = shift;
  244. if ($top->[1] eq "m") {
  245. process($top, "meth");
  246. } else {
  247. process($top, "subused");
  248. }
  249. $top = UNKNOWN;
  250. }
  251. #
  252. # Stuff for cross referencing definitions of variables and subs
  253. #
  254. sub B::GV::xref {
  255. my $gv = shift;
  256. my $cv = $gv->CV;
  257. if ($$cv) {
  258. #return if $done{$$cv}++;
  259. $file = $gv->FILE;
  260. $line = $gv->LINE;
  261. process([$gv->STASH->NAME, "&", $gv->NAME], "subdef");
  262. push(@todo, $cv);
  263. }
  264. my $form = $gv->FORM;
  265. if ($$form) {
  266. return if $done{$$form}++;
  267. $file = $gv->FILE;
  268. $line = $gv->LINE;
  269. process([$gv->STASH->NAME, "", $gv->NAME], "formdef");
  270. }
  271. }
  272. sub xref_definitions {
  273. my ($pack, %exclude);
  274. return if $nodefs;
  275. $subname = "(definitions)";
  276. foreach $pack (qw(B O AutoLoader DynaLoader XSLoader Config DB VMS
  277. strict vars FileHandle Exporter Carp)) {
  278. $exclude{$pack."::"} = 1;
  279. }
  280. no strict qw(vars refs);
  281. walksymtable(\%{"main::"}, "xref", sub { !defined($exclude{$_[0]}) });
  282. }
  283. sub output {
  284. return if $raw;
  285. my ($file, $subname, $pack, $name, $ev, $perfile, $persubname,
  286. $perpack, $pername, $perev);
  287. foreach $file (sort(keys(%table))) {
  288. $perfile = $table{$file};
  289. print "File $file\n";
  290. foreach $subname (sort(keys(%$perfile))) {
  291. $persubname = $perfile->{$subname};
  292. print " Subroutine $subname\n";
  293. foreach $pack (sort(keys(%$persubname))) {
  294. $perpack = $persubname->{$pack};
  295. print " Package $pack\n";
  296. foreach $name (sort(keys(%$perpack))) {
  297. $pername = $perpack->{$name};
  298. my @lines;
  299. foreach $ev (qw(intro formdef subdef meth subused used)) {
  300. $perev = $pername->{$ev};
  301. if (defined($perev) && @$perev) {
  302. my $code = $code{$ev};
  303. push(@lines, map("$code$_", @$perev));
  304. }
  305. }
  306. printf " %-16s %s\n", $name, join(", ", @lines);
  307. }
  308. }
  309. }
  310. }
  311. }
  312. sub compile {
  313. my @options = @_;
  314. my ($option, $opt, $arg);
  315. OPTION:
  316. while ($option = shift @options) {
  317. if ($option =~ /^-(.)(.*)/) {
  318. $opt = $1;
  319. $arg = $2;
  320. } else {
  321. unshift @options, $option;
  322. last OPTION;
  323. }
  324. if ($opt eq "-" && $arg eq "-") {
  325. shift @options;
  326. last OPTION;
  327. } elsif ($opt eq "o") {
  328. $arg ||= shift @options;
  329. open(STDOUT, ">$arg") or return "$arg: $!\n";
  330. } elsif ($opt eq "d") {
  331. $nodefs = 1;
  332. } elsif ($opt eq "r") {
  333. $raw = 1;
  334. } elsif ($opt eq "D") {
  335. $arg ||= shift @options;
  336. foreach $arg (split(//, $arg)) {
  337. if ($arg eq "o") {
  338. B->debug(1);
  339. } elsif ($arg eq "O") {
  340. $debug_op = 1;
  341. } elsif ($arg eq "t") {
  342. $debug_top = 1;
  343. }
  344. }
  345. }
  346. }
  347. if (@options) {
  348. return sub {
  349. my $objname;
  350. xref_definitions();
  351. foreach $objname (@options) {
  352. $objname = "main::$objname" unless $objname =~ /::/;
  353. eval "xref_object(\\&$objname)";
  354. die "xref_object(\\&$objname) failed: $@" if $@;
  355. }
  356. output();
  357. }
  358. } else {
  359. return sub {
  360. xref_definitions();
  361. xref_main();
  362. output();
  363. }
  364. }
  365. }
  366. 1;