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