xref: /openbsd/gnu/usr.bin/perl/ext/Devel-Peek/Peek.pm (revision 9e6efb0a)
1# Devel::Peek - A data debugging tool for the XS programmer
2# The documentation is after the __END__
3
4package Devel::Peek;
5
6$VERSION = '1.33';
7$XS_VERSION = $VERSION;
8$VERSION = eval $VERSION;
9
10require Exporter;
11require XSLoader;
12
13@EXPORT = qw(Dump mstat DeadCode DumpArray DumpWithOP DumpProg
14	     fill_mstats mstats_fillhash mstats2hash runops_debug debug_flags);
15@EXPORT_OK = qw(SvREFCNT CvGV);
16%EXPORT_TAGS = ('ALL' => [@EXPORT, @EXPORT_OK]);
17
18XSLoader::load();
19
20sub import {
21  my $c = shift;
22  my $ops_rx = qr/^:opd(=[stP]*)?\b/;
23  my @db = grep m/$ops_rx/, @_;
24  @_ = grep !m/$ops_rx/, @_;
25  if (@db) {
26    die "Too many :opd options" if @db > 1;
27    runops_debug(1);
28    my $flags = ($db[0] =~ m/$ops_rx/ and $1);
29    $flags = 'st' unless defined $flags;
30    my $f = 0;
31    $f |= 2  if $flags =~ /s/;
32    $f |= 8  if $flags =~ /t/;
33    $f |= 64 if $flags =~ /P/;
34    $^D |= $f if $f;
35  }
36  unshift @_, $c;
37  goto &Exporter::import;
38}
39
40sub DumpWithOP ($;$) {
41   local($Devel::Peek::dump_ops)=1;
42   my $depth = @_ > 1 ? $_[1] : 4 ;
43   Dump($_[0],$depth);
44}
45
46$D_flags = 'psltocPmfrxuLHXDSTR';
47
48sub debug_flags (;$) {
49  my $out = "";
50  for my $i (0 .. length($D_flags)-1) {
51    $out .= substr $D_flags, $i, 1 if $^D & (1<<$i);
52  }
53  my $arg = shift;
54  my $num = $arg;
55  if (defined $arg and $arg =~ /\D/) {
56    die "unknown flags in debug_flags()" if $arg =~ /[^-$D_flags]/;
57    my ($on,$off) = split /-/, "$arg-";
58    $num = $^D;
59    $num |=  (1<<index($D_flags, $_)) for split //, $on;
60    $num &= ~(1<<index($D_flags, $_)) for split //, $off;
61  }
62  $^D = $num if defined $arg;
63  $out
64}
65
66sub B::Deparse::pp_Devel_Peek_Dump {
67  my ($deparse,$op,$cx) = @_;
68  my @kids = $deparse->deparse($op->first, 6);
69  my $sib = $op->first->sibling;
70  if (ref $sib ne 'B::NULL') {
71    push @kids, $deparse->deparse($sib, 6);
72  }
73  return "Devel::Peek::Dump(" . join(", ", @kids) . ")";
74}
75
761;
77__END__
78
79=head1 NAME
80
81Devel::Peek - A data debugging tool for the XS programmer
82
83=head1 SYNOPSIS
84
85        use Devel::Peek;
86        Dump( $a );
87        Dump( $a, 5 );
88        Dump( @a );
89        Dump( %h );
90        DumpArray( 5, $a, $b, ... );
91	mstat "Point 5";
92
93        use Devel::Peek ':opd=st';
94
95=head1 DESCRIPTION
96
97Devel::Peek contains functions which allows raw Perl datatypes to be
98manipulated from a Perl script.  This is used by those who do XS programming
99to check that the data they are sending from C to Perl looks as they think
100it should look.  The trick, then, is to know what the raw datatype is
101supposed to look like when it gets to Perl.  This document offers some tips
102and hints to describe good and bad raw data.
103
104It is very possible that this document will fall far short of being useful
105to the casual reader.  The reader is expected to understand the material in
106the first few sections of L<perlguts>.
107
108Devel::Peek supplies a C<Dump()> function which can dump a raw Perl
109datatype, and C<mstat("marker")> function to report on memory usage
110(if perl is compiled with corresponding option).  The function
111DeadCode() provides statistics on the data "frozen" into inactive
112C<CV>.  Devel::Peek also supplies C<SvREFCNT()> which can query reference
113counts on SVs.  This document will take a passive, and safe, approach
114to data debugging and for that it will describe only the C<Dump()>
115function.
116
117All output is to STDERR.
118
119The C<Dump()> function takes one or two arguments: something to dump, and
120an optional limit for recursion and array elements (default is 4).  The
121first argument is evaluated in rvalue scalar context, with exceptions for
122@array and %hash, which dump the array or hash itself.  So C<Dump @array>
123works, as does C<Dump $foo>.  And C<Dump pos> will call C<pos> in rvalue
124context, whereas C<Dump ${\pos}> will call it in lvalue context.
125
126Function C<DumpArray()> allows dumping of multiple values (useful when you
127need to analyze returns of functions).
128
129The global variable $Devel::Peek::pv_limit can be set to limit the
130number of character printed in various string values.  Setting it to 0
131means no limit.
132
133If C<use Devel::Peek> directive has a C<:opd=FLAGS> argument,
134this switches on debugging of opcode dispatch.  C<FLAGS> should be a
135combination of C<s>, C<t>, and C<P> (see
136L<< B<-D> flags in perlrun|perlrun/B<-D>I<letters> >>).
137
138C<:opd> is a shortcut for C<:opd=st>.
139
140=head2 Runtime debugging
141
142C<CvGV($cv)> return one of the globs associated to a subroutine reference $cv.
143
144debug_flags() returns a string representation of C<$^D> (similar to
145what is allowed for B<-D> flag).  When called with a numeric argument,
146sets $^D to the corresponding value.  When called with an argument of
147the form C<"flags-flags">, set on/off bits of C<$^D> corresponding to
148letters before/after C<->.  (The returned value is for C<$^D> before
149the modification.)
150
151runops_debug() returns true if the current I<opcode dispatcher> is the
152debugging one.  When called with an argument, switches to debugging or
153non-debugging dispatcher depending on the argument (active for
154newly-entered subs/etc only).  (The returned value is for the dispatcher before the modification.)
155
156=head2 Memory footprint debugging
157
158When perl is compiled with support for memory footprint debugging
159(default with Perl's malloc()), Devel::Peek provides an access to this API.
160
161Use mstat() function to emit a memory state statistic to the terminal.
162For more information on the format of output of mstat() see
163L<perldebguts/Using $ENV{PERL_DEBUG_MSTATS}>.
164
165Three additional functions allow access to this statistic from Perl.
166First, use C<mstats_fillhash(%hash)> to get the information contained
167in the output of mstat() into %hash. The field of this hash are
168
169  minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks
170  start_slack topbucket topbucket_ev topbucket_odd total total_chain
171  total_sbrk totfree
172
173Two additional fields C<free>, C<used> contain array references which
174provide per-bucket count of free and used chunks.  Two other fields
175C<mem_size>, C<available_size> contain array references which provide
176the information about the allocated size and usable size of chunks in
177each bucket.  Again, see L<perldebguts/Using $ENV{PERL_DEBUG_MSTATS}>
178for details.
179
180
181Keep in mind that only the first several "odd-numbered" buckets are
182used, so the information on size of the "odd-numbered" buckets which are
183not used is probably meaningless.
184
185The information in
186
187 mem_size available_size minbucket nbuckets
188
189is the property of a particular build of perl, and does not depend on
190the current process.  If you do not provide the optional argument to
191the functions mstats_fillhash(), fill_mstats(), mstats2hash(), then
192the information in fields C<mem_size>, C<available_size> is not
193updated.
194
195C<fill_mstats($buf)> is a much cheaper call (both speedwise and
196memory-wise) which collects the statistic into $buf in
197machine-readable form.  At a later moment you may need to call
198C<mstats2hash($buf, %hash)> to use this information to fill %hash.
199
200All three APIs C<fill_mstats($buf)>, C<mstats_fillhash(%hash)>, and
201C<mstats2hash($buf, %hash)> are designed to allocate no memory if used
202I<the second time> on the same $buf and/or %hash.
203
204So, if you want to collect memory info in a cycle, you may call
205
206  $#buf = 999;
207  fill_mstats($_) for @buf;
208  mstats_fillhash(%report, 1);		# Static info too
209
210  foreach (@buf) {
211    # Do something...
212    fill_mstats $_;			# Collect statistic
213  }
214  foreach (@buf) {
215    mstats2hash($_, %report);		# Preserve static info
216    # Do something with %report
217  }
218
219=head1 EXAMPLES
220
221The following examples don't attempt to show everything as that would be a
222monumental task, and, frankly, we don't want this manpage to be an internals
223document for Perl.  The examples do demonstrate some basics of the raw Perl
224datatypes, and should suffice to get most determined people on their way.
225There are no guidewires or safety nets, nor blazed trails, so be prepared to
226travel alone from this point and on and, if at all possible, don't fall into
227the quicksand (it's bad for business).
228
229Oh, one final bit of advice: take L<perlguts> with you.  When you return we
230expect to see it well-thumbed.
231
232=head2 A simple scalar string
233
234Let's begin by looking a simple scalar which is holding a string.
235
236        use Devel::Peek;
237        $a = 42; $a = "hello";
238        Dump $a;
239
240The output:
241
242        SV = PVIV(0xbc288) at 0xbe9a8
243          REFCNT = 1
244          FLAGS = (POK,pPOK)
245          IV = 42
246          PV = 0xb2048 "hello"\0
247          CUR = 5
248          LEN = 8
249
250This says C<$a> is an SV, a scalar.  The scalar type is a PVIV, which is
251capable of holding an integer (IV) and/or a string (PV) value. The scalar's
252head is allocated at address 0xbe9a8, while the body is at 0xbc288.
253Its reference count is 1.  It has the C<POK> flag set, meaning its
254current PV field is valid.  Because POK is set we look at the PV item
255to see what is in the scalar.  The \0 at the end indicate that this
256PV is properly NUL-terminated.
257Note that the IV field still contains its old numeric value, but because
258FLAGS doesn't have IOK set, we must ignore the IV item.
259CUR indicates the number of characters in the PV.  LEN indicates the
260number of bytes allocated for the PV (at least one more than CUR, because
261LEN includes an extra byte for the end-of-string marker, then usually
262rounded up to some efficient allocation unit).
263
264=head2 A simple scalar number
265
266If the scalar contains a number the raw SV will be leaner.
267
268        use Devel::Peek;
269        $a = 42;
270        Dump $a;
271
272The output:
273
274        SV = IV(0xbc818) at 0xbe9a8
275          REFCNT = 1
276          FLAGS = (IOK,pIOK)
277          IV = 42
278
279This says C<$a> is an SV, a scalar.  The scalar is an IV, a number.  Its
280reference count is 1.  It has the C<IOK> flag set, meaning it is currently
281being evaluated as a number.  Because IOK is set we look at the IV item to
282see what is in the scalar.
283
284=head2 A simple scalar with an extra reference
285
286If the scalar from the previous example had an extra reference:
287
288        use Devel::Peek;
289        $a = 42;
290        $b = \$a;
291        Dump $a;
292
293The output:
294
295        SV = IV(0xbe860) at 0xbe9a8
296          REFCNT = 2
297          FLAGS = (IOK,pIOK)
298          IV = 42
299
300Notice that this example differs from the previous example only in its
301reference count.  Compare this to the next example, where we dump C<$b>
302instead of C<$a>.
303
304=head2 A reference to a simple scalar
305
306This shows what a reference looks like when it references a simple scalar.
307
308        use Devel::Peek;
309        $a = 42;
310        $b = \$a;
311        Dump $b;
312
313The output:
314
315        SV = IV(0xf041c) at 0xbe9a0
316          REFCNT = 1
317          FLAGS = (ROK)
318          RV = 0xbab08
319          SV = IV(0xbe860) at 0xbe9a8
320            REFCNT = 2
321            FLAGS = (IOK,pIOK)
322            IV = 42
323
324Starting from the top, this says C<$b> is an SV.  The scalar is an IV,
325which is capable of holding an integer or reference value.
326It has the C<ROK> flag set, meaning it is a reference (rather than an
327integer or string).  Notice that Dump
328follows the reference and shows us what C<$b> was referencing.  We see the
329same C<$a> that we found in the previous example.
330
331Note that the value of C<RV> coincides with the numbers we see when we
332stringify $b. The addresses inside IV() are addresses of
333C<X***> structures which hold the current state of an C<SV>. This
334address may change during lifetime of an SV.
335
336=head2 A reference to an array
337
338This shows what a reference to an array looks like.
339
340        use Devel::Peek;
341        $a = [42];
342        Dump $a;
343
344The output:
345
346        SV = IV(0xc85998) at 0xc859a8
347          REFCNT = 1
348          FLAGS = (ROK)
349          RV = 0xc70de8
350          SV = PVAV(0xc71e10) at 0xc70de8
351            REFCNT = 1
352            FLAGS = ()
353            ARRAY = 0xc7e820
354            FILL = 0
355            MAX = 0
356            FLAGS = (REAL)
357            Elt No. 0
358            SV = IV(0xc70f88) at 0xc70f98
359              REFCNT = 1
360              FLAGS = (IOK,pIOK)
361              IV = 42
362
363This says C<$a> is a reference (ROK), which points to
364another SV which is a PVAV, an array.  The array has one element,
365element zero, which is another SV. The field C<FILL> above indicates
366the last element in the array, similar to C<$#$a>.
367
368If C<$a> pointed to an array of two elements then we would see the
369following.
370
371        use Devel::Peek 'Dump';
372        $a = [42,24];
373        Dump $a;
374
375The output:
376
377        SV = IV(0x158c998) at 0x158c9a8
378          REFCNT = 1
379          FLAGS = (ROK)
380          RV = 0x1577de8
381          SV = PVAV(0x1578e10) at 0x1577de8
382            REFCNT = 1
383            FLAGS = ()
384            ARRAY = 0x1585820
385            FILL = 1
386            MAX = 1
387            FLAGS = (REAL)
388            Elt No. 0
389            SV = IV(0x1577f88) at 0x1577f98
390              REFCNT = 1
391              FLAGS = (IOK,pIOK)
392              IV = 42
393            Elt No. 1
394            SV = IV(0x158be88) at 0x158be98
395              REFCNT = 1
396              FLAGS = (IOK,pIOK)
397              IV = 24
398
399Note that C<Dump> will not report I<all> the elements in the array,
400only several first (depending on how deep it already went into the
401report tree).
402
403=head2 A reference to a hash
404
405The following shows the raw form of a reference to a hash.
406
407        use Devel::Peek;
408        $a = {hello=>42};
409        Dump $a;
410
411The output:
412
413    SV = IV(0x55cb50b50fb0) at 0x55cb50b50fc0
414      REFCNT = 1
415      FLAGS = (ROK)
416      RV = 0x55cb50b2b758
417      SV = PVHV(0x55cb50b319c0) at 0x55cb50b2b758
418        REFCNT = 1
419        FLAGS = (SHAREKEYS)
420        ARRAY = 0x55cb50b941a0  (0:7, 1:1)
421        hash quality = 100.0%
422        KEYS = 1
423        FILL = 1
424        MAX = 7
425        Elt "hello" HASH = 0x3128ece4
426        SV = IV(0x55cb50b464f8) at 0x55cb50b46508
427          REFCNT = 1
428          FLAGS = (IOK,pIOK)
429          IV = 42
430
431This shows C<$a> is a reference pointing to an SV.  That SV is a PVHV, a hash.
432
433The "quality" of a hash is defined as the total number of comparisons needed
434to access every element once, relative to the expected number needed for a
435random hash. The value can go over 100%.
436
437The total number of comparisons is equal to the sum of the squares of the
438number of entries in each bucket.  For a random hash of C<<n>> keys into
439C<<k>> buckets, the expected value is:
440
441		n + n(n-1)/2k
442
443=head2 Dumping a large array or hash
444
445The C<Dump()> function, by default, dumps up to 4 elements from a
446toplevel array or hash.  This number can be increased by supplying a
447second argument to the function.
448
449        use Devel::Peek;
450        $a = [10,11,12,13,14];
451        Dump $a;
452
453Notice that C<Dump()> prints only elements 10 through 13 in the above code.
454The following code will print all of the elements.
455
456        use Devel::Peek 'Dump';
457        $a = [10,11,12,13,14];
458        Dump $a, 5;
459
460=head2 A reference to an SV which holds a C pointer
461
462This is what you really need to know as an XS programmer, of course.  When
463an XSUB returns a pointer to a C structure that pointer is stored in an SV
464and a reference to that SV is placed on the XSUB stack.  So the output from
465an XSUB which uses something like the T_PTROBJ map might look something like
466this:
467
468        SV = IV(0xf381c) at 0xc859a8
469          REFCNT = 1
470          FLAGS = (ROK)
471          RV = 0xb8ad8
472          SV = PVMG(0xbb3c8) at 0xc859a0
473            REFCNT = 1
474            FLAGS = (OBJECT,IOK,pIOK)
475            IV = 729160
476            NV = 0
477            PV = 0
478            STASH = 0xc1d10       "CookBookB::Opaque"
479
480This shows that we have an SV which is a reference, which points at another
481SV.  In this case that second SV is a PVMG, a blessed scalar.  Because it is
482blessed it has the C<OBJECT> flag set.  Note that an SV which holds a C
483pointer also has the C<IOK> flag set.  The C<STASH> is set to the package
484name which this SV was blessed into.
485
486The output from an XSUB which uses something like the T_PTRREF map, which
487doesn't bless the object, might look something like this:
488
489        SV = IV(0xf381c) at 0xc859a8
490          REFCNT = 1
491          FLAGS = (ROK)
492          RV = 0xb8ad8
493          SV = PVMG(0xbb3c8) at 0xc859a0
494            REFCNT = 1
495            FLAGS = (IOK,pIOK)
496            IV = 729160
497            NV = 0
498            PV = 0
499
500=head2 A reference to a subroutine
501
502Looks like this:
503
504	SV = IV(0x24d2dd8) at 0x24d2de8
505	  REFCNT = 1
506	  FLAGS = (TEMP,ROK)
507	  RV = 0x24e79d8
508	  SV = PVCV(0x24e5798) at 0x24e79d8
509	    REFCNT = 2
510	    FLAGS = ()
511	    COMP_STASH = 0x22c9c50	"main"
512	    START = 0x22eed60 ===> 0
513	    ROOT = 0x22ee490
514	    GVGV::GV = 0x22de9d8	"MY" :: "top_targets"
515	    FILE = "(eval 5)"
516	    DEPTH = 0
517	    FLAGS = 0x0
518	    OUTSIDE_SEQ = 93
519	    PADLIST = 0x22e9ed8
520	    PADNAME = 0x22e9ec0(0x22eed00) PAD = 0x22e9ea8(0x22eecd0)
521	    OUTSIDE = 0x22c9fb0 (MAIN)
522
523
524This shows that
525
526=over 4
527
528=item *
529
530the subroutine is not an XSUB (since C<START> and C<ROOT> are
531non-zero, and C<XSUB> is not listed, and is thus null);
532
533=item *
534
535that it was compiled in the package C<main>;
536
537=item *
538
539under the name C<MY::top_targets>;
540
541=item *
542
543inside a 5th eval in the program;
544
545=item *
546
547it is not currently executed (because C<DEPTH> is 0);
548
549=item *
550
551it has no prototype (C<PROTOTYPE> field is missing).
552
553=back
554
555=head1 EXPORTS
556
557C<Dump>, C<mstat>, C<DeadCode>, C<DumpArray>, C<DumpWithOP> and
558C<DumpProg>, C<fill_mstats>, C<mstats_fillhash>, C<mstats2hash> by
559default. Additionally available C<SvREFCNT>, C<SvREFCNT_inc> and
560C<SvREFCNT_dec>.
561
562=head1 BUGS
563
564Readers have been known to skip important parts of L<perlguts>, causing much
565frustration for all.
566
567=head1 AUTHOR
568
569Ilya Zakharevich	ilya@math.ohio-state.edu
570
571Copyright (c) 1995-98 Ilya Zakharevich. All rights reserved.
572This program is free software; you can redistribute it and/or
573modify it under the same terms as Perl itself.
574
575Author of this software makes no claim whatsoever about suitability,
576reliability, edability, editability or usability of this product, and
577should not be kept liable for any damage resulting from the use of
578it. If you can use it, you are in luck, if not, I should not be kept
579responsible. Keep a handy copy of your backup tape at hand.
580
581=head1 SEE ALSO
582
583L<perlguts>, and L<perlguts>, again.
584
585=cut
586