| 1 | package B::Module::Info; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | our $VERSION = '0.39'; | ||
| 6 | |||
| 7 | use B; | ||
| 8 | use B::Utils 0.27 qw(walkoptree_filtered walkoptree_simple | ||
| 9 | opgrep all_roots); | ||
| 10 | @B::Utils::bad_stashes = qw(); # give us everything. | ||
| 11 | our ($Start, $End, $File, $CurCV); | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | B::Module::Info - information about Perl modules | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | { | ||
| 20 | # From: Roland Walker <walker@ncbi.nlm.nih.gov> | ||
| 21 | # "Syntax OK" may land inside output and render it unusable | ||
| 22 | my $oldfh = select STDERR; $| = 1; # STDERR is unbuffered, but just in case | ||
| 23 | select STDOUT; $| = 1; | ||
| 24 | select $oldfh; | ||
| 25 | } | ||
| 26 | |||
| 27 | my $the_file = $0; # when walking all subroutines, you need to skip | ||
| 28 | # the ones in other modules | ||
| 29 | |||
| 30 | sub state_change { | ||
| 31 | return opgrep {name => [qw(nextstate dbstate setstate)]}, @_ | ||
| 32 | } | ||
| 33 | |||
| 34 | my $cur_pack; | ||
| 35 | sub state_call { | ||
| 36 | my($op) = shift; | ||
| 37 | my $pack = $op->stashpv; | ||
| 38 | print "$pack\n" if !defined($cur_pack) || $pack ne $cur_pack; | ||
| 39 | $cur_pack = $pack; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | sub filtered_roots { | ||
| 44 | my %roots = all_roots; | ||
| 45 | my %filtered_roots = (); | ||
| 46 | while( my($name, $op) = each %roots ) { | ||
| 47 | next if $name eq '__MAIN__'; | ||
| 48 | $filtered_roots{$name} = $op; | ||
| 49 | } | ||
| 50 | return %filtered_roots; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | =head2 roots_cv_pairs_recursive | ||
| 55 | |||
| 56 | Returns a list of pairs, each containing a root with the relative | ||
| 57 | B::CV object; this list includes B::main_root/cv and all anonymous | ||
| 58 | subroutines defined therein. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub roots_cv_pairs_recursive { | ||
| 63 | my @queue = roots_cv_pairs(); | ||
| 64 | my @roots; | ||
| 65 | |||
| 66 | my $anon_sub = sub { | ||
| 67 | B::class($_[0]) ne 'NULL' && $_[0]->name eq 'anoncode'; | ||
| 68 | }; | ||
| 69 | |||
| 70 | my $anon_check = sub { | ||
| 71 | my $cv = const_sv($_[0]); | ||
| 72 | push @queue, [ $cv->ROOT, $cv ]; | ||
| 73 | }; | ||
| 74 | |||
| 75 | while( @queue ) { | ||
| 76 | my $p = shift @queue; | ||
| 77 | push @roots, $p; | ||
| 78 | local $CurCV = $p->[1]; | ||
| 79 | walkoptree_filtered($p->[0], | ||
| 80 | $anon_sub, | ||
| 81 | $anon_check ); | ||
| 82 | } | ||
| 83 | |||
| 84 | return @roots; | ||
| 85 | } | ||
| 86 | |||
| 87 | =head2 roots_cv_pairs | ||
| 88 | |||
| 89 | Returns a list of pairs, each containing a root with the relative | ||
| 90 | B::CV object for named subroutines; this list includes B::main_root/cv. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub roots_cv_pairs { | ||
| 95 | my %roots = filtered_roots; | ||
| 96 | my @roots = ( [ B::main_root, B::main_cv ], | ||
| 97 | map { [ $roots{$_}, | ||
| 98 | B::svref_2object(\&{$_}) ] } | ||
| 99 | keys %roots ); | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | my %modes = ( | ||
| 104 | packages => sub { | ||
| 105 | walkoptree_filtered(B::main_root, | ||
| 106 | \&state_change, | ||
| 107 | \&state_call ); | ||
| 108 | }, | ||
| 109 | subroutines => sub { | ||
| 110 | my %roots = filtered_roots(); | ||
| 111 | while( my($name, $op) = each %roots ) { | ||
| 112 | local($File, $Start, $End); | ||
| 113 | walkoptree_simple($op, \&sub_info); | ||
| 114 | print "$name at \"$File\" from $Start to $End\n"; | ||
| 115 | } | ||
| 116 | }, | ||
| 117 | modules_used => sub { | ||
| 118 | # begin_av is an undocumented B function. | ||
| 119 | # note: if module hasn't any BEGIN block, | ||
| 120 | # begin_av will be a B::SPECIAL | ||
| 121 | my @arr = B::begin_av->isa('B::SPECIAL') ? | ||
| 122 | () : | ||
| 123 | B::begin_av->ARRAY; | ||
| 124 | foreach my $begin_cv (@arr) { | ||
| 125 | my $root = $begin_cv->ROOT; | ||
| 126 | local $CurCV = $begin_cv; | ||
| 127 | |||
| 128 | next unless $begin_cv->FILE eq $the_file; | ||
| 129 | # cheat otherwise show_require guard prevents display | ||
| 130 | local $B::Utils::file = $begin_cv->FILE; | ||
| 131 | local $B::Utils::line = $begin_cv->START->line; | ||
| 132 | |||
| 133 | # this is from $ENV{PERL5OPT}, skip it | ||
| 134 | next if $B::Utils::line == 0; | ||
| 135 | |||
| 136 | my $lineseq = $root->first; | ||
| 137 | next if $lineseq->name ne 'lineseq'; | ||
| 138 | |||
| 139 | my $req_op = $lineseq->first->sibling; | ||
| 140 | if( $req_op->name eq 'require' ) { | ||
| 141 | my $module; | ||
| 142 | if( $req_op->first->private & B::OPpCONST_BARE ) { | ||
| 143 | $module = const_sv($req_op->first)->PV; | ||
| 144 | $module =~ s[/][::]g; | ||
| 145 | $module =~ s/.pm$//; | ||
| 146 | } | ||
| 147 | else { | ||
| 148 | # if it is not bare it can't be an "use" | ||
| 149 | show_require($req_op); | ||
| 150 | next; | ||
| 151 | } | ||
| 152 | |||
| 153 | printf "use %s (%s) at \"%s\" line %s\n", | ||
| 154 | $module, | ||
| 155 | get_required_version($req_op, $module), | ||
| 156 | $begin_cv->FILE, | ||
| 157 | $begin_cv->START->line; | ||
| 158 | } | ||
| 159 | # it can't be an use, scan the optree | ||
| 160 | else { | ||
| 161 | walkoptree_filtered($root, | ||
| 162 | \&is_require, | ||
| 163 | \&show_require, | ||
| 164 | ); | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | { | ||
| 169 | foreach my $p ( roots_cv_pairs_recursive ) { | ||
| 170 | local $CurCV = $p->[1]; | ||
| 171 | walkoptree_filtered($p->[0], | ||
| 172 | \&is_require, | ||
| 173 | \&show_require, | ||
| 174 | ); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | }, | ||
| 178 | subs_called => sub { | ||
| 179 | foreach my $p ( roots_cv_pairs_recursive ) { | ||
| 180 | local $CurCV = $p->[1]; | ||
| 181 | walkoptree_filtered($p->[0], | ||
| 182 | \&sub_call, | ||
| 183 | \&sub_check ); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | ); | ||
| 187 | |||
| 188 | |||
| 189 | sub const_sv { | ||
| 190 | my $op = shift; | ||
| 191 | my $sv; | ||
| 192 | |||
| 193 | if ($op->name eq 'method_named' && $op->can('meth_sv')) { | ||
| 194 | $sv = $op->meth_sv; | ||
| 195 | } | ||
| 196 | elsif ($op->can('sv')) { | ||
| 197 | $sv = $op->sv; | ||
| 198 | } | ||
| 199 | # the constant could be in the pad (under useithreads) | ||
| 200 | $sv = padval($op->targ) unless ref($sv) && $$sv; | ||
| 201 | return $sv; | ||
| 202 | } | ||
| 203 | |||
| 204 | # Don't do this for regexes | ||
| 205 | sub unback { | ||
| 206 | my($str) = @_; | ||
| 207 | $str =~ s/\\/\\\\/g; | ||
| 208 | return $str; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub const { | ||
| 212 | my $sv = shift; | ||
| 213 | if (B::class($sv) eq "SPECIAL") { | ||
| 214 | return ('undef', '1', '0')[$$sv-1]; # sv_undef, sv_yes, sv_no | ||
| 215 | } elsif (B::class($sv) eq "NULL") { | ||
| 216 | return 'undef'; | ||
| 217 | } elsif ($sv->FLAGS & B::SVf_IOK) { | ||
| 218 | return $sv->int_value; | ||
| 219 | } elsif ($sv->FLAGS & B::SVf_NOK) { | ||
| 220 | # try the default stringification | ||
| 221 | my $r = "".$sv->NV; | ||
| 222 | if ($r =~ /e/) { | ||
| 223 | # If it's in scientific notation, we might have lost information | ||
| 224 | return sprintf("%.20e", $sv->NV); | ||
| 225 | } | ||
| 226 | return $r; | ||
| 227 | } elsif ($sv->FLAGS & B::SVf_ROK && $sv->can("RV")) { | ||
| 228 | return "\\(" . B::const($sv->RV) . ")"; # constant folded | ||
| 229 | } elsif ($sv->FLAGS & B::SVf_POK) { | ||
| 230 | my $str = $sv->PV; | ||
| 231 | if ($str =~ /[^ -~]/) { # ASCII for non-printing | ||
| 232 | return single_delim("qq", '"', uninterp escape_str unback $str); | ||
| 233 | } else { | ||
| 234 | return single_delim("q", "'", unback $str); | ||
| 235 | } | ||
| 236 | } else { | ||
| 237 | return "undef"; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | sub single_delim { | ||
| 243 | my($q, $default, $str) = @_; | ||
| 244 | return "$default$str$default" if $default and index($str, $default) == -1; | ||
| 245 | my($succeed, $delim); | ||
| 246 | ($succeed, $str) = balanced_delim($str); | ||
| 247 | return "$q$str" if $succeed; | ||
| 248 | for $delim ('/', '"', '#') { | ||
| 249 | return "$q$delim" . $str . $delim if index($str, $delim) == -1; | ||
| 250 | } | ||
| 251 | if ($default) { | ||
| 252 | $str =~ s/$default/\\$default/g; | ||
| 253 | return "$default$str$default"; | ||
| 254 | } else { | ||
| 255 | $str =~ s[/][\\/]g; | ||
| 256 | return "$q/$str/"; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | |||
| 261 | sub padval { | ||
| 262 | my $targ = shift; | ||
| 263 | return (($CurCV->PADLIST->ARRAY)[1]->ARRAY)[$targ]; | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | sub sub_info { | ||
| 268 | $File = undef if $File eq '__none__'; | ||
| 269 | $File ||= $B::Utils::file; | ||
| 270 | $Start = $B::Utils::line if !$Start || $B::Utils::line < $Start; | ||
| 271 | $End = $B::Utils::line if !$End || $B::Utils::line > $End; | ||
| 272 | } | ||
| 273 | |||
| 274 | sub is_begin { | ||
| 275 | my($op) = shift; | ||
| 276 | my $name = $op->GV; | ||
| 277 | print $name; | ||
| 278 | return $name eq 'BEGIN'; | ||
| 279 | } | ||
| 280 | |||
| 281 | sub begin_is_use { | ||
| 282 | my($op) = shift; | ||
| 283 | print "Saw begin\n"; | ||
| 284 | } | ||
| 285 | |||
| 286 | |||
| 287 | sub grep_magic { | ||
| 288 | my($pvmg, $type) = @_; | ||
| 289 | my $magic = $pvmg->MAGIC; | ||
| 290 | |||
| 291 | while ($$magic) { | ||
| 292 | return $magic if $magic->TYPE eq $type; | ||
| 293 | } | ||
| 294 | |||
| 295 | return $magic; # false | ||
| 296 | } | ||
| 297 | |||
| 298 | sub get_required_version { | ||
| 299 | my($req_op, $module) = (shift, shift); | ||
| 300 | |||
| 301 | my $version; | ||
| 302 | my $version_op = $req_op->sibling; | ||
| 303 | return if B::class($version_op) eq 'NULL'; | ||
| 304 | if ($version_op->name eq 'lineseq') { | ||
| 305 | # We have a version parameter; skip nextstate & | ||
| 306 | # pushmark | ||
| 307 | my $constop = $version_op->first->next->next; | ||
| 308 | |||
| 309 | return '' unless const_sv($constop)->PV eq $module; | ||
| 310 | $constop = $constop->sibling; | ||
| 311 | $version = const_sv($constop); | ||
| 312 | my $class = B::class($version); | ||
| 313 | my $magic; | ||
| 314 | $version = $class eq 'IV' ? $version->int_value : | ||
| 315 | $class eq 'NV' ? $version->NV : | ||
| 316 | ($class eq 'PVMG' && ($magic = grep_magic($version, 'V')) | ||
| 317 | && $$magic) ? 'v' . $magic->PTR : | ||
| 318 | ((($class eq 'PVNV' && $] < 5.009) || $class eq 'PVMG') | ||
| 319 | && length($version->PV)) ? | ||
| 320 | 'v' . join('.', map(ord, | ||
| 321 | split(//, | ||
| 322 | $version->PV) | ||
| 323 | )) : | ||
| 324 | $class eq 'PVIV' ? $version->int_value : | ||
| 325 | $version->NV; | ||
| 326 | |||
| 327 | $constop = $constop->sibling; | ||
| 328 | return '' if $constop->name ne "method_named"; | ||
| 329 | return '' if const_sv($constop)->PV ne "VERSION"; | ||
| 330 | } | ||
| 331 | |||
| 332 | return $version; | ||
| 333 | } | ||
| 334 | |||
| 335 | |||
| 336 | sub is_require { | ||
| 337 | B::class($_[0]) ne 'NULL' && $_[0]->name eq 'require'; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub show_require { | ||
| 341 | return unless $B::Utils::file eq $the_file; | ||
| 342 | my($op) = shift; | ||
| 343 | |||
| 344 | my($name, $bare); | ||
| 345 | if( B::class($op) eq "UNOP" and $op->first->name eq 'const' | ||
| 346 | and $op->first->private & B::OPpCONST_BARE ) { | ||
| 347 | $bare = 'bare'; | ||
| 348 | $name = const_sv($op->first)->PV; | ||
| 349 | } | ||
| 350 | else { | ||
| 351 | $bare = 'not bare'; | ||
| 352 | if ($op->flags & B::OPf_KIDS) { | ||
| 353 | my $kid = $op->first; | ||
| 354 | if (defined prototype("CORE::$name") | ||
| 355 | && prototype("CORE::$name") =~ /^;?\*/ | ||
| 356 | && $kid->name eq "rv2gv") { | ||
| 357 | $kid = $kid->first; | ||
| 358 | } | ||
| 359 | |||
| 360 | my $sv = const_sv($kid); | ||
| 361 | return unless defined $sv && !$sv->isa('B::NULL'); | ||
| 362 | $name = $sv->isa("B::NV") ? $sv->NV : 0; | ||
| 363 | $name ||= $sv->isa("B::PV") ? $sv->PV : ''; | ||
| 364 | $name ||= $sv->IV; | ||
| 365 | } | ||
| 366 | else { | ||
| 367 | $name = ""; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | printf "require %s %s at line %d\n", $bare, $name, $B::Utils::line; | ||
| 371 | } | ||
| 372 | |||
| 373 | |||
| 374 | sub compile { | ||
| 375 | my($mode) = shift; | ||
| 376 | |||
| 377 | return $modes{$mode}; | ||
| 378 | } | ||
| 379 | |||
| 380 | |||
| 381 | sub sub_call { | ||
| 382 | B::class($_[0]) ne 'NULL' && $_[0]->name eq 'entersub'; | ||
| 383 | } | ||
| 384 | |||
| 385 | sub sub_check { | ||
| 386 | my($op) = shift; | ||
| 387 | |||
| 388 | unless( $op->name eq 'entersub' ) { | ||
| 389 | warn "sub_check only works with entersub ops"; | ||
| 390 | return; | ||
| 391 | } | ||
| 392 | |||
| 393 | my @kids = $op->kids; | ||
| 394 | |||
| 395 | # static method call | ||
| 396 | if( my($kid) = grep $_->name eq 'method_named', @kids ) { | ||
| 397 | my $class = _class_or_object_method(@kids); | ||
| 398 | printf "%s method call to %s%s at \"%s\" line %d\n", | ||
| 399 | $class ? "class" : "object", | ||
| 400 | const_sv($kid)->PV, | ||
| 401 | $class ? " via $class" : '', | ||
| 402 | $B::Utils::file, $B::Utils::line; | ||
| 403 | } | ||
| 404 | # dynamic method call | ||
| 405 | elsif( my($kid) = grep $_->name eq 'method', @kids ) { | ||
| 406 | my $class = _class_or_object_method(@kids); | ||
| 407 | printf "dynamic %s method call%s at \"%s\" line %d\n", | ||
| 408 | $class ? "class" : "object", | ||
| 409 | $class ? " via $class" : '', | ||
| 410 | $B::Utils::file, $B::Utils::line; | ||
| 411 | } | ||
| 412 | # function call | ||
| 413 | else { | ||
| 414 | my $gv_op; | ||
| 415 | my ($filename, $line) = ($B::Utils::file, $B::Utils::line); | ||
| 416 | walkoptree_simple($op, | ||
| 417 | sub { my $op = shift; $gv_op = $op if $op->name eq 'gv'; } | ||
| 418 | ); | ||
| 419 | if ($gv_op) { | ||
| 420 | my $gv = gv_or_padgv($gv_op); | ||
| 421 | printf "function call to %s at \"%s\" line %d\n", | ||
| 422 | $gv->NAME, $filename, $line; | ||
| 423 | } | ||
| 424 | else { | ||
| 425 | printf "function call using symbolic ref at \"%s\" line %d\n", | ||
| 426 | $filename, $line; | ||
| 427 | } | ||
| 428 | } | ||
| 429 | } | ||
| 430 | |||
| 431 | |||
| 432 | sub gv_or_padgv { | ||
| 433 | # my $self = shift; | ||
| 434 | my $op = shift; | ||
| 435 | if ($op->isa("B::PADOP")) { | ||
| 436 | return padval($op->padix); | ||
| 437 | } | ||
| 438 | else { # class($op) eq "SVOP" | ||
| 439 | return $op->gv; | ||
| 440 | } | ||
| 441 | } | ||
| 442 | |||
| 443 | |||
| 444 | sub _class_or_object_method { | ||
| 445 | my @kids = @_; | ||
| 446 | |||
| 447 | my $class; | ||
| 448 | my($classop) = $kids[1]; | ||
| 449 | if( $classop->name eq 'const' ) { | ||
| 450 | $class = const_sv($classop)->PV; | ||
| 451 | } | ||
| 452 | |||
| 453 | return $class; | ||
| 454 | } | ||
| 455 | |||
| 456 | |||
| 457 | 1; |