⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 peek.pm

📁 source of perl for linux application,
💻 PM
📖 第 1 页 / 共 2 页
字号:
# Devel::Peek - A data debugging tool for the XS programmer# The documentation is after the __END__package Devel::Peek;$VERSION = '1.03';$XS_VERSION = $VERSION;$VERSION = eval $VERSION;require Exporter;use XSLoader ();@ISA = qw(Exporter);@EXPORT = qw(Dump mstat DeadCode DumpArray DumpWithOP DumpProg	     fill_mstats mstats_fillhash mstats2hash runops_debug debug_flags);@EXPORT_OK = qw(SvREFCNT SvREFCNT_inc SvREFCNT_dec CvGV);%EXPORT_TAGS = ('ALL' => [@EXPORT, @EXPORT_OK]);XSLoader::load 'Devel::Peek';sub import {  my $c = shift;  my $ops_rx = qr/^:opd(=[stP]*)?\b/;  my @db = grep m/$ops_rx/, @_;  @_ = grep !m/$ops_rx/, @_;  if (@db) {    die "Too many :opd options" if @db > 1;    runops_debug(1);    my $flags = ($db[0] =~ m/$ops_rx/ and $1);    $flags = 'st' unless defined $flags;    my $f = 0;    $f |= 2  if $flags =~ /s/;    $f |= 8  if $flags =~ /t/;    $f |= 64 if $flags =~ /P/;    $^D |= $f if $f;  }  unshift @_, $c;  goto &Exporter::import;}sub DumpWithOP ($;$) {   local($Devel::Peek::dump_ops)=1;   my $depth = @_ > 1 ? $_[1] : 4 ;   Dump($_[0],$depth);}$D_flags = 'psltocPmfrxuLHXDSTR';sub debug_flags (;$) {  my $out = "";  for my $i (0 .. length($D_flags)-1) {    $out .= substr $D_flags, $i, 1 if $^D & (1<<$i);  }  my $arg = shift;  my $num = $arg;  if (defined $arg and $arg =~ /\D/) {    die "unknown flags in debug_flags()" if $arg =~ /[^-$D_flags]/;    my ($on,$off) = split /-/, "$arg-";    $num = $^D;    $num |=  (1<<index($D_flags, $_)) for split //, $on;    $num &= ~(1<<index($D_flags, $_)) for split //, $off;  }  $^D = $num if defined $arg;  $out}1;__END__=head1 NAMEDevel::Peek - A data debugging tool for the XS programmer=head1 SYNOPSIS        use Devel::Peek;        Dump( $a );        Dump( $a, 5 );        DumpArray( 5, $a, $b, ... );	mstat "Point 5";        use Devel::Peek ':opd=st';=head1 DESCRIPTIONDevel::Peek contains functions which allows raw Perl datatypes to bemanipulated from a Perl script.  This is used by those who do XS programmingto check that the data they are sending from C to Perl looks as they thinkit should look.  The trick, then, is to know what the raw datatype issupposed to look like when it gets to Perl.  This document offers some tipsand hints to describe good and bad raw data.It is very possible that this document will fall far short of being usefulto the casual reader.  The reader is expected to understand the material inthe first few sections of L<perlguts>.Devel::Peek supplies a C<Dump()> function which can dump a raw Perldatatype, and C<mstat("marker")> function to report on memory usage(if perl is compiled with corresponding option).  The functionDeadCode() provides statistics on the data "frozen" into inactiveC<CV>.  Devel::Peek also supplies C<SvREFCNT()>, C<SvREFCNT_inc()>, andC<SvREFCNT_dec()> which can query, increment, and decrement referencecounts on SVs.  This document will take a passive, and safe, approachto data debugging and for that it will describe only the C<Dump()>function.Function C<DumpArray()> allows dumping of multiple values (useful when youneed to analyze returns of functions).The global variable $Devel::Peek::pv_limit can be set to limit thenumber of character printed in various string values.  Setting it to 0means no limit.If C<use Devel::Peek> directive has a C<:opd=FLAGS> argument,this switches on debugging of opcode dispatch.  C<FLAGS> should be acombination of C<s>, C<t>, and C<P> (see B<-D> flags in L<perlrun>).C<:opd> is a shortcut for C<:opd=st>.=head2 Runtime debuggingC<CvGV($cv)> return one of the globs associated to a subroutine reference $cv.debug_flags() returns a string representation of C<$^D> (similar towhat is allowed for B<-D> flag).  When called with a numeric argument,sets $^D to the corresponding value.  When called with an argument ofthe form C<"flags-flags">, set on/off bits of C<$^D> corresponding toletters before/after C<->.  (The returned value is for C<$^D> beforethe modification.)runops_debug() returns true if the current I<opcode dispatcher> is thedebugging one.  When called with an argument, switches to debugging ornon-debugging dispatcher depending on the argument (active fornewly-entered subs/etc only).  (The returned value is for the dispatcher before the modification.)=head2 Memory footprint debuggingWhen perl is compiled with support for memory footprint debugging(default with Perl's malloc()), Devel::Peek provides an access to this API.Use mstat() function to emit a memory state statistic to the terminal.For more information on the format of output of mstat() seeL<perldebguts/Using C<$ENV{PERL_DEBUG_MSTATS}>>.Three additional functions allow access to this statistic from Perl.First, use C<mstats_fillhash(%hash)> to get the information containedin the output of mstat() into %hash. The field of this hash are  minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks start_slack  topbucket topbucket_ev topbucket_odd total total_chain total_sbrk totfreeTwo additional fields C<free>, C<used> contain array references whichprovide per-bucket count of free and used chunks.  Two other fieldsC<mem_size>, C<available_size> contain array references which providethe information about the allocated size and usable size of chunks ineach bucket.  Again, see L<perldebguts/Using C<$ENV{PERL_DEBUG_MSTATS}>>for details.Keep in mind that only the first several "odd-numbered" buckets areused, so the information on size of the "odd-numbered" buckets which arenot used is probably meaningless.The information in mem_size available_size minbucket nbucketsis the property of a particular build of perl, and does not depend onthe current process.  If you do not provide the optional argument tothe functions mstats_fillhash(), fill_mstats(), mstats2hash(), thenthe information in fields C<mem_size>, C<available_size> is notupdated.C<fill_mstats($buf)> is a much cheaper call (both speedwise andmemory-wise) which collects the statistic into $buf inmachine-readable form.  At a later moment you may need to callC<mstats2hash($buf, %hash)> to use this information to fill %hash.All three APIs C<fill_mstats($buf)>, C<mstats_fillhash(%hash)>, andC<mstats2hash($buf, %hash)> are designed to allocate no memory if usedI<the second time> on the same $buf and/or %hash.So, if you want to collect memory info in a cycle, you may call  $#buf = 999;  fill_mstats($_) for @buf;  mstats_fillhash(%report, 1);		# Static info too  foreach (@buf) {    # Do something...    fill_mstats $_;			# Collect statistic  }  foreach (@buf) {    mstats2hash($_, %report);		# Preserve static info    # Do something with %report  }=head1 EXAMPLESThe following examples don't attempt to show everything as that would be amonumental task, and, frankly, we don't want this manpage to be an internalsdocument for Perl.  The examples do demonstrate some basics of the raw Perldatatypes, and should suffice to get most determined people on their way.There are no guidewires or safety nets, nor blazed trails, so be prepared totravel alone from this point and on and, if at all possible, don't fall intothe quicksand (it's bad for business).Oh, one final bit of advice: take L<perlguts> with you.  When you return weexpect to see it well-thumbed.=head2 A simple scalar stringLet's begin by looking a simple scalar which is holding a string.        use Devel::Peek;        $a = "hello";        Dump $a;The output:        SV = PVIV(0xbc288)          REFCNT = 1          FLAGS = (POK,pPOK)          IV = 0          PV = 0xb2048 "hello"\0          CUR = 5          LEN = 6This says C<$a> is an SV, a scalar.  The scalar is a PVIV, a string.Its reference count is 1.  It has the C<POK> flag set, meaning itscurrent PV field is valid.  Because POK is set we look at the PV itemto see what is in the scalar.  The \0 at the end indicate that thisPV is properly NUL-terminated.If the FLAGS had been IOK we would lookat the IV item.  CUR indicates the number of characters in the PV.LEN indicates the number of bytes requested for the PV (one more thanCUR, in this case, because LEN includes an extra byte for theend-of-string marker).=head2 A simple scalar numberIf the scalar contains a number the raw SV will be leaner.        use Devel::Peek;        $a = 42;        Dump $a;The output:        SV = IV(0xbc818)          REFCNT = 1          FLAGS = (IOK,pIOK)          IV = 42This says C<$a> is an SV, a scalar.  The scalar is an IV, a number.  Itsreference count is 1.  It has the C<IOK> flag set, meaning it is currentlybeing evaluated as a number.  Because IOK is set we look at the IV item tosee what is in the scalar.=head2 A simple scalar with an extra referenceIf the scalar from the previous example had an extra reference:        use Devel::Peek;        $a = 42;        $b = \$a;        Dump $a;The output:        SV = IV(0xbe860)          REFCNT = 2          FLAGS = (IOK,pIOK)          IV = 42Notice that this example differs from the previous example only in itsreference count.  Compare this to the next example, where we dump C<$b>instead of C<$a>.=head2 A reference to a simple scalarThis shows what a reference looks like when it references a simple scalar.        use Devel::Peek;        $a = 42;        $b = \$a;        Dump $b;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -