<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package B::Debug;

our $VERSION = '1.19';

use strict;
require 5.006;
use B qw(peekop class walkoptree walkoptree_exec
         main_start main_root cstring sv_undef SVf_NOK SVf_IOK);
use Config;
my (@optype, @specialsv_name);
require B;
if ($] &lt; 5.009) {
  require B::Asmdata;
  B::Asmdata-&gt;import (qw(@optype @specialsv_name));
} else {
  B-&gt;import (qw(@optype @specialsv_name));
}

if ($] &lt; 5.006002) {
  eval q|sub B::GV::SAFENAME {
    my $name = (shift())-&gt;NAME;
    # The regex below corresponds to the isCONTROLVAR macro from toke.c
    $name =~ s/^([\cA-\cZ\c\\c[\c]\c?\c_\c^])/"^".chr(64 ^ ord($1))/e;
    return $name;
  }|;
}

my ($have_B_Flags, $have_B_Flags_extra);
if (!$ENV{PERL_CORE}){ # avoid CORE test crashes
  eval { require B::Flags and $have_B_Flags++ };
  $have_B_Flags_extra++ if $have_B_Flags and $B::Flags::VERSION gt '0.03';
}
my %done_gv;

sub _printop {
  my $op = shift;
  my $addr = ${$op} ? $op-&gt;ppaddr : '';
  $addr =~ s/^PL_ppaddr// if $addr;
  return sprintf "0x%08x %6s %s", ${$op}, ${$op} ? class($op) : '', $addr;
}

sub B::OP::debug {
    my ($op) = @_;
    printf &lt;&lt;'EOT', class($op), $$op, _printop($op), _printop($op-&gt;next), _printop($op-&gt;sibling), $op-&gt;targ, $op-&gt;type, $op-&gt;name;
%s (0x%lx)
	op_ppaddr	%s
	op_next		%s
	op_sibling	%s
	op_targ		%d
	op_type		%d	%s
EOT
    if ($] &gt; 5.009) {
	printf &lt;&lt;'EOT', $op-&gt;opt;
	op_opt		%d
EOT
    } else {
	printf &lt;&lt;'EOT', $op-&gt;seq;
	op_seq		%d
EOT
    }
    if ($have_B_Flags) {
        printf &lt;&lt;'EOT', $op-&gt;flags, $op-&gt;flagspv, $op-&gt;private, $op-&gt;privatepv;
	op_flags	%d	%s
	op_private	%d	%s
EOT
    } else {
        printf &lt;&lt;'EOT', $op-&gt;flags, $op-&gt;private;
	op_flags	%d
	op_private	%d
EOT
    }
}

sub B::UNOP::debug {
    my ($op) = @_;
    $op-&gt;B::OP::debug();
    printf "\top_first\t%s\n", _printop($op-&gt;first);
}

sub B::BINOP::debug {
    my ($op) = @_;
    $op-&gt;B::UNOP::debug();
    printf "\top_last \t%s\n", _printop($op-&gt;last);
}

sub B::LOOP::debug {
    my ($op) = @_;
    $op-&gt;B::BINOP::debug();
    printf &lt;&lt;'EOT', _printop($op-&gt;redoop), _printop($op-&gt;nextop), _printop($op-&gt;lastop);
	op_redoop	%s
	op_nextop	%s
	op_lastop	%s
EOT
}

sub B::LOGOP::debug {
    my ($op) = @_;
    $op-&gt;B::UNOP::debug();
    printf "\top_other\t%s\n", _printop($op-&gt;other);
}

sub B::LISTOP::debug {
    my ($op) = @_;
    $op-&gt;B::BINOP::debug();
    printf "\top_children\t%d\n", $op-&gt;children;
}

sub B::PMOP::debug {
    my ($op) = @_;
    $op-&gt;B::LISTOP::debug();
    printf "\top_pmreplroot\t0x%x\n", $] &lt; 5.008 ? ${$op-&gt;pmreplroot} : $op-&gt;pmreplroot;
    printf "\top_pmreplstart\t0x%x\n", ${$op-&gt;pmreplstart};
    printf "\top_pmnext\t0x%x\n", ${$op-&gt;pmnext} if $] &lt; 5.009005;
    if ($Config{'useithreads'}) {
      printf "\top_pmstashpv\t%s\n", cstring($op-&gt;pmstashpv);
      printf "\top_pmoffset\t%d\n", $op-&gt;pmoffset;
    } else {
      printf "\top_pmstash\t%s\n", cstring($op-&gt;pmstash);
    }
    printf "\top_precomp\t%s\n", cstring($op-&gt;precomp);
    printf "\top_pmflags\t0x%x\n", $op-&gt;pmflags;
    printf "\top_reflags\t0x%x\n", $op-&gt;reflags if $] &gt;= 5.009;
    printf "\top_pmpermflags\t0x%x\n", $op-&gt;pmpermflags if $] &lt; 5.009;
    printf "\top_pmdynflags\t0x%x\n", $op-&gt;pmdynflags if $] &lt; 5.009;
    $op-&gt;pmreplroot-&gt;debug if $] &lt; 5.008;
}

sub B::COP::debug {
    my ($op) = @_;
    $op-&gt;B::OP::debug();
    my $warnings = ref $op-&gt;warnings ? ${$op-&gt;warnings} : 0;
    printf &lt;&lt;'EOT', $op-&gt;label, $op-&gt;stashpv, $op-&gt;file, $op-&gt;cop_seq, $op-&gt;arybase, $op-&gt;line, $warnings;
	cop_label	"%s"
	cop_stashpv	"%s"
	cop_file	"%s"
	cop_seq		%d
	cop_arybase	%d
	cop_line	%d
	cop_warnings	0x%x
EOT
  if ($] &gt; 5.008 and $] &lt; 5.011) {
    my $cop_io = class($op-&gt;io) eq 'SPECIAL' ? '' : $op-&gt;io-&gt;as_string;
    printf("	cop_io		%s\n", cstring($cop_io));
  }
}

sub B::SVOP::debug {
    my ($op) = @_;
    $op-&gt;B::OP::debug();
    printf "\top_sv\t\t0x%x\n", ${$op-&gt;sv};
    $op-&gt;sv-&gt;debug;
}

sub B::PVOP::debug {
    my ($op) = @_;
    $op-&gt;B::OP::debug();
    printf "\top_pv\t\t%s\n", cstring($op-&gt;pv);
}

sub B::PADOP::debug {
    my ($op) = @_;
    $op-&gt;B::OP::debug();
    printf "\top_padix\t%ld\n", $op-&gt;padix;
}

sub B::NULL::debug {
    my ($sv) = @_;
    if ($$sv == ${sv_undef()}) {
	print "&amp;sv_undef\n";
    } else {
	printf "NULL (0x%x)\n", $$sv;
    }
}

sub B::SV::debug {
    my ($sv) = @_;
    if (!$$sv) {
	print class($sv), " = NULL\n";
	return;
    }
    printf &lt;&lt;'EOT', class($sv), $$sv, $sv-&gt;REFCNT;
%s (0x%x)
	REFCNT		%d
EOT
    printf "\tFLAGS\t\t0x%x", $sv-&gt;FLAGS;
    if ($have_B_Flags) {
      printf "\t%s", $have_B_Flags_extra ? $sv-&gt;flagspv(0) : $sv-&gt;flagspv;
    }
    print "\n";
}

sub B::RV::debug {
    my ($rv) = @_;
    B::SV::debug($rv);
    printf &lt;&lt;'EOT', ${$rv-&gt;RV};
	RV		0x%x
EOT
    $rv-&gt;RV-&gt;debug;
}

sub B::PV::debug {
    my ($sv) = @_;
    $sv-&gt;B::SV::debug();
    my $pv = $sv-&gt;PV();
    printf &lt;&lt;'EOT', cstring($pv), $sv-&gt;CUR, $sv-&gt;LEN;
	xpv_pv		%s
	xpv_cur		%d
	xpv_len		%d
EOT
}

sub B::IV::debug {
    my ($sv) = @_;
    $sv-&gt;B::SV::debug();
    printf "\txiv_iv\t\t%d\n", $sv-&gt;IV if $sv-&gt;FLAGS &amp; SVf_IOK;
}

sub B::NV::debug {
    my ($sv) = @_;
    $sv-&gt;B::IV::debug();
    printf "\txnv_nv\t\t%s\n", $sv-&gt;NV if $sv-&gt;FLAGS &amp; SVf_NOK;
}

sub B::PVIV::debug {
    my ($sv) = @_;
    $sv-&gt;B::PV::debug();
    printf "\txiv_iv\t\t%d\n", $sv-&gt;IV if $sv-&gt;FLAGS &amp; SVf_IOK;
}

sub B::PVNV::debug {
    my ($sv) = @_;
    $sv-&gt;B::PVIV::debug();
    printf "\txnv_nv\t\t%s\n", $sv-&gt;NV if $sv-&gt;FLAGS &amp; SVf_NOK;
}

sub B::PVLV::debug {
    my ($sv) = @_;
    $sv-&gt;B::PVNV::debug();
    printf "\txlv_targoff\t%d\n", $sv-&gt;TARGOFF;
    printf "\txlv_targlen\t%u\n", $sv-&gt;TARGLEN;
    printf "\txlv_type\t%s\n", cstring(chr($sv-&gt;TYPE));
}

sub B::BM::debug {
    my ($sv) = @_;
    $sv-&gt;B::PVNV::debug();
    printf "\txbm_useful\t%d\n", $sv-&gt;USEFUL;
    printf "\txbm_previous\t%u\n", $sv-&gt;PREVIOUS;
    printf "\txbm_rare\t%s\n", cstring(chr($sv-&gt;RARE));
}

sub B::CV::debug {
    my ($sv) = @_;
    $sv-&gt;B::PVNV::debug();
    my ($stash) = $sv-&gt;STASH;
    my ($start) = $sv-&gt;START;
    my ($root)  = $sv-&gt;ROOT;
    my ($padlist) = $sv-&gt;PADLIST;
    my ($file) = $sv-&gt;FILE;
    my ($gv) = $sv-&gt;GV;
    printf &lt;&lt;'EOT', $$stash, $$start, $$root;
	STASH		0x%x
	START		0x%x
	ROOT		0x%x
EOT
    if ( $]&gt;5.017 &amp;&amp; ($sv-&gt;FLAGS &amp; 0x40000)) { #lexsub
      printf("\tNAME\t%%s\n", $sv-&gt;NAME);
    } else {
      printf("\tGV\t%0x%x\t%s\n", $$gv, $gv-&gt;SAFENAME);
    }
    printf &lt;&lt;'EOT', $file, $sv-&gt;DEPTH, $padlist, ${$sv-&gt;OUTSIDE};
	FILE		%s
	DEPTH		%d
	PADLIST		0x%x
	OUTSIDE		0x%x
EOT
    printf("\tOUTSIDE_SEQ\t%d\n", $sv-&gt;OUTSIDE_SEQ) if $] &gt; 5.007;
    if ($have_B_Flags) {
      my $SVt_PVCV = $] &lt; 5.010 ? 12 : 13;
      printf("\tCvFLAGS\t0x%x\t%s\n", $sv-&gt;CvFLAGS,
	     $have_B_Flags_extra ? $sv-&gt;flagspv($SVt_PVCV) : $sv-&gt;flagspv);
    } else {
      printf("\tCvFLAGS\t0x%x\n", $sv-&gt;CvFLAGS);
    }
    $start-&gt;debug if $start;
    $root-&gt;debug if $root;
    $gv-&gt;debug if $gv;
    $padlist-&gt;debug if $padlist;
}

sub B::AV::debug {
    my ($av) = @_;
    $av-&gt;B::SV::debug;
    _array_debug($av);
}

sub _array_debug {
    my ($av) = @_;
    # tied arrays may leave out FETCHSIZE
    my (@array) = eval { $av-&gt;ARRAY; };
    print "\tARRAY\t\t(", join(", ", map("0x" . $$_, @array)), ")\n";
    my $fill = eval { scalar(@array) };
    if ($Config{'useithreads'} &amp;&amp; class($av) ne 'PADLIST') {
      printf &lt;&lt;'EOT', $fill, $av-&gt;MAX, $av-&gt;OFF;
	FILL		%d
	MAX		%d
	OFF		%d
EOT
    } else {
      printf &lt;&lt;'EOT', $fill, $av-&gt;MAX;
	FILL		%d
	MAX		%d
EOT
    }
    if ($] &lt; 5.009) {
      if ($have_B_Flags) {
	printf("\tAvFLAGS\t0x%x\t%s\n", $av-&gt;AvFLAGS,
	       $have_B_Flags_extra ? $av-&gt;flagspv(10) : $av-&gt;flagspv);
      } else {
	printf("\tAvFLAGS\t0x%x\n", $av-&gt;AvFLAGS);
      }
    }
}

sub B::GV::debug {
    my ($gv) = @_;
    if ($done_gv{$$gv}++) {
	printf "GV %s::%s\n", $gv-&gt;STASH-&gt;NAME, $gv-&gt;SAFENAME;
	return;
    }
    my $sv = $gv-&gt;SV;
    my $av = $gv-&gt;AV;
    my $cv = $gv-&gt;CV;
    $gv-&gt;B::SV::debug;
    printf &lt;&lt;'EOT', $gv-&gt;SAFENAME, $gv-&gt;STASH-&gt;NAME, $gv-&gt;STASH, $$sv, $gv-&gt;GvREFCNT, $gv-&gt;FORM, $$av, ${$gv-&gt;HV}, ${$gv-&gt;EGV}, $$cv, $gv-&gt;CVGEN, $gv-&gt;LINE, $gv-&gt;FILE, $gv-&gt;GvFLAGS;
	NAME		%s
	STASH		%s (0x%x)
	SV		0x%x
	GvREFCNT	%d
	FORM		0x%x
	AV		0x%x
	HV		0x%x
	EGV		0x%x
	CV		0x%x
	CVGEN		%d
	LINE		%d
	FILE		%s
EOT
    if ($have_B_Flags) {
      my $SVt_PVGV = $] &lt; 5.010 ? 13 : 9;
      printf("\tGvFLAGS\t0x%x\t%s\n", $gv-&gt;GvFLAGS,
	     $have_B_Flags_extra ? $gv-&gt;flagspv($SVt_PVGV) : $gv-&gt;flagspv);
    } else {
      printf("\tGvFLAGS\t0x%x\n", $gv-&gt;GvFLAGS);
    }
    $sv-&gt;debug if $sv;
    $av-&gt;debug if $av;
    $cv-&gt;debug if $cv;
}

sub B::SPECIAL::debug {
    my $sv = shift;
    my $i = ref $sv ? $$sv : 0;
    print exists $specialsv_name[$i] ? $specialsv_name[$i] : "", "\n";
}

sub B::PADLIST::debug {
    my ($padlist) = @_;
    printf &lt;&lt;'EOT', class($padlist), $$padlist, $padlist-&gt;REFCNT;
%s (0x%x)
	REFCNT		%d
EOT
    _array_debug($padlist);
}

sub compile {
    my $order = shift;
    B::clearsym();
    if ($order &amp;&amp; $order eq "exec") {
        return sub { walkoptree_exec(main_start, "debug") }
    } else {
        return sub { walkoptree(main_root, "debug") }
    }
}

1;

__END__

=head1 NAME

B::Debug - Walk Perl syntax tree, printing debug info about ops

=head1 SYNOPSIS

        perl -MO=Debug foo.pl
        perl -MO=Debug,-exec foo.pl

=head1 DESCRIPTION

See F&lt;ext/B/README&gt; and the newer L&lt;B::Concise&gt;, L&lt;B::Terse&gt;.

=head1 OPTIONS

With option -exec, walks tree in execute order,
otherwise in basic order.

=head1 AUTHOR

Malcolm Beattie, C&lt;mbeattie@sable.ox.ac.uk&gt;
Reini Urban C&lt;rurban@cpan.org&gt;

=head1 LICENSE

Copyright (c) 1996, 1997 Malcolm Beattie
Copyright (c) 2008, 2010, 2013 Reini Urban

	This program is free software; you can redistribute it and/or modify
	it under the terms of either:

	a) the GNU General Public License as published by the Free
	Software Foundation; either version 1, or (at your option) any
	later version, or

	b) the "Artistic License" which comes with this kit.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either
    the GNU General Public License or the Artistic License for more details.

    You should have received a copy of the Artistic License with this kit,
    in the file named "Artistic".  If not, you can get one from the Perl
    distribution. You should also have received a copy of the GNU General
    Public License, in the file named "Copying". If not, you can get one
    from the Perl distribution or else write to the Free Software Foundation,
    Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

=cut

</pre></body></html>