1
package B::Module::Info;
6
use B::BUtils qw(walkoptree_filtered walkoptree_simple
8
@B::Utils::bad_stashes = qw(); # give us everything.
11
# From: Roland Walker <walker@ncbi.nlm.nih.gov>
12
# "Syntax OK" may land inside output and render it unusable
13
my $oldfh = select STDERR; $| = 1; # STDERR is unbuffered, but just in case
14
select STDOUT; $| = 1;
18
my $the_file = $0; # when walking all subroutines, you need to skip
19
# the ones in other modules
22
return opgrep {name => [qw(nextstate dbstate setstate)]}, @_
28
my $pack = $op->stashpv;
29
print "$pack\n" if !defined($cur_pack) || $pack ne $cur_pack;
35
my %roots = all_roots;
36
my %filtered_roots = ();
37
while( my($name, $op) = each %roots ) {
38
next if $name eq '__MAIN__';
39
$filtered_roots{$name} = $op;
41
return %filtered_roots;
45
=head2 roots_cv_pairs_recursive
47
Returns a list of pairs, each containing a root with the relative
48
B::CV object; this list includes B::main_root/cv and all anonymous
49
subroutines defined therein.
53
sub roots_cv_pairs_recursive {
54
my @queue = roots_cv_pairs();
58
B::class($_[0]) ne 'NULL' && $_[0]->name eq 'anoncode';
61
my $anon_check = sub {
62
my $cv = const_sv($_[0]);
63
push @queue, [ $cv->ROOT, $cv ];
69
local $CurCV = $p->[1];
70
walkoptree_filtered($p->[0],
80
Returns a list of pairs, each containing a root with the relative
81
B::CV object for named subroutines; this list includes B::main_root/cv.
86
my %roots = filtered_roots;
87
my @roots = ( [ B::main_root, B::main_cv ],
89
B::svref_2object(\&{$_}) ] }
96
walkoptree_filtered(B::main_root,
101
my %roots = filtered_roots();
102
while( my($name, $op) = each %roots ) {
103
local($File, $Start, $End);
104
walkoptree_simple($op, \&sub_info);
105
print "$name at \"$File\" from $Start to $End\n";
108
modules_used => sub {
109
# begin_av is an undocumented B function.
110
# note: if module hasn't any BEGIN block,
111
# begin_av will be a B::SPECIAL
112
my @arr = B::begin_av->isa('B::SPECIAL') ?
115
foreach my $begin_cv (@arr) {
116
my $root = $begin_cv->ROOT;
117
local $CurCV = $begin_cv;
119
next unless $begin_cv->FILE eq $the_file;
120
# cheat otherwise show_require guard prevents display
121
local $B::Utils::file = $begin_cv->FILE;
122
local $B::Utils::line = $begin_cv->START->line;
124
# this is from $ENV{PERL5OPT}, skip it
125
next if $B::Utils::line == 0;
127
my $lineseq = $root->first;
128
next if $lineseq->name ne 'lineseq';
130
my $req_op = $lineseq->first->sibling;
131
if( $req_op->name eq 'require' ) {
133
if( $req_op->first->private & B::OPpCONST_BARE ) {
134
$module = const_sv($req_op->first)->PV;
135
$module =~ s[/][::]g;
139
# if it is not bare it can't be an "use"
140
show_require($req_op);
144
printf "use %s (%s) at \"%s\" line %s\n",
146
get_required_version($req_op, $module),
148
$begin_cv->START->line;
150
# it can't be an use, scan the optree
152
walkoptree_filtered($root,
160
foreach my $p ( roots_cv_pairs_recursive ) {
161
local $CurCV = $p->[1];
162
walkoptree_filtered($p->[0],
170
foreach my $p ( roots_cv_pairs_recursive ) {
171
local $CurCV = $p->[1];
172
walkoptree_filtered($p->[0],
182
my $sv = $op->sv if $op->can('sv');
183
# the constant could be in the pad (under useithreads)
184
$sv = padval($op->targ) unless $$sv;
188
# Don't do this for regexes
197
if (B::class($sv) eq "SPECIAL") {
198
return ('undef', '1', '0')[$$sv-1]; # sv_undef, sv_yes, sv_no
199
} elsif (B::class($sv) eq "NULL") {
201
} elsif ($sv->FLAGS & B::SVf_IOK) {
202
return $sv->int_value;
203
} elsif ($sv->FLAGS & B::SVf_NOK) {
204
# try the default stringification
207
# If it's in scientific notation, we might have lost information
208
return sprintf("%.20e", $sv->NV);
211
} elsif ($sv->FLAGS & B::SVf_ROK && $sv->can("RV")) {
212
return "\\(" . B::const($sv->RV) . ")"; # constant folded
213
} elsif ($sv->FLAGS & B::SVf_POK) {
215
if ($str =~ /[^ -~]/) { # ASCII for non-printing
216
return single_delim("qq", '"', uninterp escape_str unback $str);
218
return single_delim("q", "'", unback $str);
227
my($q, $default, $str) = @_;
228
return "$default$str$default" if $default and index($str, $default) == -1;
229
my($succeed, $delim);
230
($succeed, $str) = balanced_delim($str);
231
return "$q$str" if $succeed;
232
for $delim ('/', '"', '#') {
233
return "$q$delim" . $str . $delim if index($str, $delim) == -1;
236
$str =~ s/$default/\\$default/g;
237
return "$default$str$default";
247
return (($CurCV->PADLIST->ARRAY)[1]->ARRAY)[$targ];
252
$File = undef if $File eq '__none__';
253
$File ||= $B::Utils::file;
254
$Start = $B::Utils::line if !$Start || $B::Utils::line < $Start;
255
$End = $B::Utils::line if !$End || $B::Utils::line > $End;
262
return $name eq 'BEGIN';
272
my($pvmg, $type) = @_;
273
my $magic = $pvmg->MAGIC;
276
return $magic if $magic->TYPE eq $type;
279
return $magic; # false
282
sub get_required_version {
283
my($req_op, $module) = (shift, shift);
286
my $version_op = $req_op->sibling;
287
return if B::class($version_op) eq 'NULL';
288
if ($version_op->name eq 'lineseq') {
289
# We have a version parameter; skip nextstate &
291
my $constop = $version_op->first->next->next;
293
return '' unless const_sv($constop)->PV eq $module;
294
$constop = $constop->sibling;
295
$version = const_sv($constop);
296
my $class = B::class($version);
298
$version = $class eq 'IV' ? $version->int_value :
299
$class eq 'NV' ? $version->NV :
300
($class eq 'PVMG' && ($magic = grep_magic($version, 'V'))
301
&& $$magic) ? 'v' . $magic->PTR :
302
((($class eq 'PVNV' && $] < 5.009) || $class eq 'PVMG')
303
&& length($version->PV)) ?
304
'v' . join('.', map(ord,
308
$class eq 'PVIV' ? $version->int_value :
311
$constop = $constop->sibling;
312
return '' if $constop->name ne "method_named";
313
return '' if const_sv($constop)->PV ne "VERSION";
321
B::class($_[0]) ne 'NULL' && $_[0]->name eq 'require';
325
return unless $B::Utils::file eq $the_file;
329
if( B::class($op) eq "UNOP" and $op->first->name eq 'const'
330
and $op->first->private & B::OPpCONST_BARE ) {
332
$name = const_sv($op->first)->PV;
336
if ($op->flags & B::OPf_KIDS) {
337
my $kid = $op->first;
338
if (defined prototype("CORE::$name")
339
&& prototype("CORE::$name") =~ /^;?\*/
340
&& $kid->name eq "rv2gv") {
344
my $sv = const_sv($kid);
345
return unless defined $sv && !$sv->isa('B::NULL');
346
$name = $sv->isa("B::NV") ? $sv->NV : 0;
347
$name ||= $sv->isa("B::PV") ? $sv->PV : '';
354
printf "require %s %s at line %d\n", $bare, $name, $B::Utils::line;
361
return $modes{$mode};
366
B::class($_[0]) ne 'NULL' && $_[0]->name eq 'entersub';
372
unless( $op->name eq 'entersub' ) {
373
warn "sub_check only works with entersub ops";
377
my @kids = $op->kids;
380
if( my($kid) = grep $_->name eq 'method_named', @kids ) {
381
my $class = _class_or_object_method(@kids);
382
printf "%s method call to %s%s at \"%s\" line %d\n",
383
$class ? "class" : "object",
385
$class ? " via $class" : '',
386
$B::Utils::file, $B::Utils::line;
388
# dynamic method call
389
elsif( my($kid) = grep $_->name eq 'method', @kids ) {
390
my $class = _class_or_object_method(@kids);
391
printf "dynamic %s method call%s at \"%s\" line %d\n",
392
$class ? "class" : "object",
393
$class ? " via $class" : '',
394
$B::Utils::file, $B::Utils::line;
398
my($name_op) = grep($_->name eq 'gv', @kids);
400
my $gv = gv_or_padgv($name_op);
401
printf "function call to %s at \"%s\" line %d\n",
402
$gv->NAME, $B::Utils::file, $B::Utils::line;
405
printf "function call using symbolic ref at \"%s\" line %d\n",
406
$B::Utils::file, $B::Utils::line;
415
if ($op->isa("B::PADOP")) {
416
return padval($op->padix);
418
else { # class($op) eq "SVOP"
424
sub _class_or_object_method {
428
my($classop) = $kids[1];
429
if( $classop->name eq 'const' ) {
430
$class = const_sv($classop)->PV;