SYN SYN
[p5sagit/p5-mst-13.2.git] / ext / Devel / Peek / Peek.pm
CommitLineData
3967c732 1# Devel::Peek - A data debugging tool for the XS programmer
2# The documentation is after the __END__
3
4package Devel::Peek;
5
cb50131a 6# Underscore to allow older Perls to access older version from CPAN
7$VERSION = '1.00_01';
3967c732 8
9require Exporter;
146174a9 10use XSLoader ();
3967c732 11
146174a9 12@ISA = qw(Exporter);
22d4bb9c 13@EXPORT = qw(Dump mstat DeadCode DumpArray DumpWithOP DumpProg
14 fill_mstats mstats_fillhash mstats2hash);
cb50131a 15@EXPORT_OK = qw(SvREFCNT SvREFCNT_inc SvREFCNT_dec CvGV);
3967c732 16%EXPORT_TAGS = ('ALL' => [@EXPORT, @EXPORT_OK]);
17
146174a9 18XSLoader::load 'Devel::Peek';
3967c732 19
20sub DumpWithOP ($;$) {
21 local($Devel::Peek::dump_ops)=1;
22 my $depth = @_ > 1 ? $_[1] : 4 ;
23 Dump($_[0],$depth);
24}
25
261;
27__END__
28
29=head1 NAME
30
31Devel::Peek - A data debugging tool for the XS programmer
32
33=head1 SYNOPSIS
34
35 use Devel::Peek;
36 Dump( $a );
37 Dump( $a, 5 );
38 DumpArray( 5, $a, $b, ... );
39 mstat "Point 5";
40
41=head1 DESCRIPTION
42
43Devel::Peek contains functions which allows raw Perl datatypes to be
44manipulated from a Perl script. This is used by those who do XS programming
45to check that the data they are sending from C to Perl looks as they think
46it should look. The trick, then, is to know what the raw datatype is
47supposed to look like when it gets to Perl. This document offers some tips
48and hints to describe good and bad raw data.
49
50It is very possible that this document will fall far short of being useful
51to the casual reader. The reader is expected to understand the material in
52the first few sections of L<perlguts>.
53
54Devel::Peek supplies a C<Dump()> function which can dump a raw Perl
55datatype, and C<mstat("marker")> function to report on memory usage
56(if perl is compiled with corresponding option). The function
57DeadCode() provides statistics on the data "frozen" into inactive
58C<CV>. Devel::Peek also supplies C<SvREFCNT()>, C<SvREFCNT_inc()>, and
59C<SvREFCNT_dec()> which can query, increment, and decrement reference
60counts on SVs. This document will take a passive, and safe, approach
61to data debugging and for that it will describe only the C<Dump()>
22d4bb9c 62function.
3967c732 63
64Function C<DumpArray()> allows dumping of multiple values (useful when you
4b19af01 65need to analyze returns of functions).
3967c732 66
67The global variable $Devel::Peek::pv_limit can be set to limit the
68number of character printed in various string values. Setting it to 0
69means no limit.
70
22d4bb9c 71=head2 Memory footprint debugging
72
73When perl is compiled with support for memory footprint debugging
74(default with Perl's malloc()), Devel::Peek provides an access to this API.
75
76Use mstat() function to emit a memory state statistic to the terminal.
77For more information on the format of output of mstat() see
78L<perldebug/Using C<$ENV{PERL_DEBUG_MSTATS}>>.
79
80Three additional functions allow access to this statistic from Perl.
81First, use C<mstats_fillhash(%hash)> to get the information contained
82in the output of mstat() into %hash. The field of this hash are
83
84 minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks start_slack
85 topbucket topbucket_ev topbucket_odd total total_chain total_sbrk totfree
86
87Two additional fields C<free>, C<used> contain array references which
88provide per-bucket count of free and used chunks. Two other fields
89C<mem_size>, C<available_size> contain array references which provide
90the information about the allocated size and usable size of chunks in
91each bucket. Again, see L<perldebug/Using C<$ENV{PERL_DEBUG_MSTATS}>>
92for details.
93
94Keep in mind that only the first several "odd-numbered" buckets are
95used, so the information on size of the "odd-numbered" buckets which are
96not used is probably meaningless.
97
98The information in
99
100 mem_size available_size minbucket nbuckets
101
102is the property of a particular build of perl, and does not depend on
103the current process. If you do not provide the optional argument to
104the functions mstats_fillhash(), fill_mstats(), mstats2hash(), then
105the information in fields C<mem_size>, C<available_size> is not
106updated.
107
108C<fill_mstats($buf)> is a much cheaper call (both speedwise and
109memory-wise) which collects the statistic into $buf in
110machine-readable form. At a later moment you may need to call
111C<mstats2hash($buf, %hash)> to use this information to fill %hash.
112
113All three APIs C<fill_mstats($buf)>, C<mstats_fillhash(%hash)>, and
114C<mstats2hash($buf, %hash)> are designed to allocate no memory if used
115I<the second time> on the same $buf and/or %hash.
116
117So, if you want to collect memory info in a cycle, you may call
118
119 $#buf = 999;
120 fill_mstats($_) for @buf;
121 mstats_fillhash(%report, 1); # Static info too
122
123 foreach (@buf) {
124 # Do something...
125 fill_mstats $_; # Collect statistic
126 }
127 foreach (@buf) {
128 mstats2hash($_, %report); # Preserve static info
129 # Do something with %report
130 }
131
3967c732 132=head1 EXAMPLES
133
134The following examples don't attempt to show everything as that would be a
135monumental task, and, frankly, we don't want this manpage to be an internals
136document for Perl. The examples do demonstrate some basics of the raw Perl
137datatypes, and should suffice to get most determined people on their way.
138There are no guidewires or safety nets, nor blazed trails, so be prepared to
139travel alone from this point and on and, if at all possible, don't fall into
140the quicksand (it's bad for business).
141
142Oh, one final bit of advice: take L<perlguts> with you. When you return we
143expect to see it well-thumbed.
144
145=head2 A simple scalar string
146
147Let's begin by looking a simple scalar which is holding a string.
148
a423dfdd 149 use Devel::Peek;
3967c732 150 $a = "hello";
151 Dump $a;
152
153The output:
154
155 SV = PVIV(0xbc288)
156 REFCNT = 1
157 FLAGS = (POK,pPOK)
158 IV = 0
159 PV = 0xb2048 "hello"\0
160 CUR = 5
161 LEN = 6
162
163This says C<$a> is an SV, a scalar. The scalar is a PVIV, a string.
164Its reference count is 1. It has the C<POK> flag set, meaning its
165current PV field is valid. Because POK is set we look at the PV item
166to see what is in the scalar. The \0 at the end indicate that this
167PV is properly NUL-terminated.
168If the FLAGS had been IOK we would look
169at the IV item. CUR indicates the number of characters in the PV.
170LEN indicates the number of bytes requested for the PV (one more than
171CUR, in this case, because LEN includes an extra byte for the
172end-of-string marker).
173
174=head2 A simple scalar number
175
176If the scalar contains a number the raw SV will be leaner.
177
a423dfdd 178 use Devel::Peek;
3967c732 179 $a = 42;
180 Dump $a;
181
182The output:
183
184 SV = IV(0xbc818)
185 REFCNT = 1
186 FLAGS = (IOK,pIOK)
187 IV = 42
188
189This says C<$a> is an SV, a scalar. The scalar is an IV, a number. Its
190reference count is 1. It has the C<IOK> flag set, meaning it is currently
191being evaluated as a number. Because IOK is set we look at the IV item to
192see what is in the scalar.
193
194=head2 A simple scalar with an extra reference
195
196If the scalar from the previous example had an extra reference:
197
a423dfdd 198 use Devel::Peek;
3967c732 199 $a = 42;
200 $b = \$a;
201 Dump $a;
202
203The output:
204
205 SV = IV(0xbe860)
206 REFCNT = 2
207 FLAGS = (IOK,pIOK)
208 IV = 42
209
210Notice that this example differs from the previous example only in its
211reference count. Compare this to the next example, where we dump C<$b>
212instead of C<$a>.
213
214=head2 A reference to a simple scalar
215
216This shows what a reference looks like when it references a simple scalar.
217
a423dfdd 218 use Devel::Peek;
3967c732 219 $a = 42;
220 $b = \$a;
221 Dump $b;
222
223The output:
224
225 SV = RV(0xf041c)
226 REFCNT = 1
227 FLAGS = (ROK)
228 RV = 0xbab08
229 SV = IV(0xbe860)
230 REFCNT = 2
231 FLAGS = (IOK,pIOK)
232 IV = 42
233
234Starting from the top, this says C<$b> is an SV. The scalar is an RV, a
235reference. It has the C<ROK> flag set, meaning it is a reference. Because
236ROK is set we have an RV item rather than an IV or PV. Notice that Dump
237follows the reference and shows us what C<$b> was referencing. We see the
238same C<$a> that we found in the previous example.
239
240Note that the value of C<RV> coincides with the numbers we see when we
241stringify $b. The addresses inside RV() and IV() are addresses of
242C<X***> structure which holds the current state of an C<SV>. This
243address may change during lifetime of an SV.
244
245=head2 A reference to an array
246
247This shows what a reference to an array looks like.
248
a423dfdd 249 use Devel::Peek;
3967c732 250 $a = [42];
251 Dump $a;
252
253The output:
254
255 SV = RV(0xf041c)
256 REFCNT = 1
257 FLAGS = (ROK)
258 RV = 0xb2850
259 SV = PVAV(0xbd448)
260 REFCNT = 1
261 FLAGS = ()
262 IV = 0
263 NV = 0
264 ARRAY = 0xb2048
265 ALLOC = 0xb2048
266 FILL = 0
267 MAX = 0
268 ARYLEN = 0x0
269 FLAGS = (REAL)
270 Elt No. 0 0xb5658
271 SV = IV(0xbe860)
272 REFCNT = 1
273 FLAGS = (IOK,pIOK)
274 IV = 42
275
276This says C<$a> is an SV and that it is an RV. That RV points to
277another SV which is a PVAV, an array. The array has one element,
278element zero, which is another SV. The field C<FILL> above indicates
279the last element in the array, similar to C<$#$a>.
280
281If C<$a> pointed to an array of two elements then we would see the
282following.
283
284 use Devel::Peek 'Dump';
285 $a = [42,24];
286 Dump $a;
287
288The output:
289
290 SV = RV(0xf041c)
291 REFCNT = 1
292 FLAGS = (ROK)
293 RV = 0xb2850
294 SV = PVAV(0xbd448)
295 REFCNT = 1
296 FLAGS = ()
297 IV = 0
298 NV = 0
299 ARRAY = 0xb2048
300 ALLOC = 0xb2048
301 FILL = 0
302 MAX = 0
303 ARYLEN = 0x0
304 FLAGS = (REAL)
305 Elt No. 0 0xb5658
306 SV = IV(0xbe860)
307 REFCNT = 1
308 FLAGS = (IOK,pIOK)
309 IV = 42
310 Elt No. 1 0xb5680
311 SV = IV(0xbe818)
312 REFCNT = 1
313 FLAGS = (IOK,pIOK)
314 IV = 24
315
316Note that C<Dump> will not report I<all> the elements in the array,
317only several first (depending on how deep it already went into the
318report tree).
319
320=head2 A reference to a hash
321
322The following shows the raw form of a reference to a hash.
323
a423dfdd 324 use Devel::Peek;
3967c732 325 $a = {hello=>42};
326 Dump $a;
327
328The output:
329
330 SV = RV(0xf041c)
331 REFCNT = 1
332 FLAGS = (ROK)
333 RV = 0xb2850
334 SV = PVHV(0xbd448)
335 REFCNT = 1
336 FLAGS = ()
337 NV = 0
338 ARRAY = 0xbd748
339 KEYS = 1
340 FILL = 1
341 MAX = 7
342 RITER = -1
343 EITER = 0x0
344 Elt "hello" => 0xbaaf0
345 SV = IV(0xbe860)
346 REFCNT = 1
347 FLAGS = (IOK,pIOK)
348 IV = 42
349
350This shows C<$a> is a reference pointing to an SV. That SV is a PVHV, a
351hash. Fields RITER and EITER are used by C<L<each>>.
352
353=head2 Dumping a large array or hash
354
355The C<Dump()> function, by default, dumps up to 4 elements from a
356toplevel array or hash. This number can be increased by supplying a
357second argument to the function.
358
a423dfdd 359 use Devel::Peek;
3967c732 360 $a = [10,11,12,13,14];
361 Dump $a;
362
363Notice that C<Dump()> prints only elements 10 through 13 in the above code.
364The following code will print all of the elements.
365
366 use Devel::Peek 'Dump';
367 $a = [10,11,12,13,14];
368 Dump $a, 5;
369
370=head2 A reference to an SV which holds a C pointer
371
372This is what you really need to know as an XS programmer, of course. When
373an XSUB returns a pointer to a C structure that pointer is stored in an SV
374and a reference to that SV is placed on the XSUB stack. So the output from
375an XSUB which uses something like the T_PTROBJ map might look something like
376this:
377
378 SV = RV(0xf381c)
379 REFCNT = 1
380 FLAGS = (ROK)
381 RV = 0xb8ad8
382 SV = PVMG(0xbb3c8)
383 REFCNT = 1
384 FLAGS = (OBJECT,IOK,pIOK)
385 IV = 729160
386 NV = 0
387 PV = 0
388 STASH = 0xc1d10 "CookBookB::Opaque"
389
390This shows that we have an SV which is an RV. That RV points at another
391SV. In this case that second SV is a PVMG, a blessed scalar. Because it is
392blessed it has the C<OBJECT> flag set. Note that an SV which holds a C
393pointer also has the C<IOK> flag set. The C<STASH> is set to the package
394name which this SV was blessed into.
395
396The output from an XSUB which uses something like the T_PTRREF map, which
397doesn't bless the object, might look something like this:
398
399 SV = RV(0xf381c)
400 REFCNT = 1
401 FLAGS = (ROK)
402 RV = 0xb8ad8
403 SV = PVMG(0xbb3c8)
404 REFCNT = 1
405 FLAGS = (IOK,pIOK)
406 IV = 729160
407 NV = 0
408 PV = 0
409
410=head2 A reference to a subroutine
411
412Looks like this:
413
414 SV = RV(0x798ec)
415 REFCNT = 1
416 FLAGS = (TEMP,ROK)
417 RV = 0x1d453c
418 SV = PVCV(0x1c768c)
419 REFCNT = 2
420 FLAGS = ()
421 IV = 0
422 NV = 0
423 COMP_STASH = 0x31068 "main"
424 START = 0xb20e0
425 ROOT = 0xbece0
426 XSUB = 0x0
427 XSUBANY = 0
428 GVGV::GV = 0x1d44e8 "MY" :: "top_targets"
146174a9 429 FILE = "(eval 5)"
3967c732 430 DEPTH = 0
431 PADLIST = 0x1c9338
432
433This shows that
434
435=over
436
cb50131a 437=item *
3967c732 438
439the subroutine is not an XSUB (since C<START> and C<ROOT> are
440non-zero, and C<XSUB> is zero);
441
cb50131a 442=item *
3967c732 443
444that it was compiled in the package C<main>;
445
cb50131a 446=item *
3967c732 447
448under the name C<MY::top_targets>;
449
cb50131a 450=item *
3967c732 451
452inside a 5th eval in the program;
453
cb50131a 454=item *
3967c732 455
456it is not currently executed (see C<DEPTH>);
457
cb50131a 458=item *
3967c732 459
460it has no prototype (C<PROTOTYPE> field is missing).
461
cb50131a 462=back
3967c732 463
464=head1 EXPORTS
465
466C<Dump>, C<mstat>, C<DeadCode>, C<DumpArray>, C<DumpWithOP> and
22d4bb9c 467C<DumpProg>, C<fill_mstats>, C<mstats_fillhash>, C<mstats2hash> by
468default. Additionally available C<SvREFCNT>, C<SvREFCNT_inc> and
469C<SvREFCNT_dec>.
3967c732 470
471=head1 BUGS
472
473Readers have been known to skip important parts of L<perlguts>, causing much
474frustration for all.
475
476=head1 AUTHOR
477
478Ilya Zakharevich ilya@math.ohio-state.edu
479
480Copyright (c) 1995-98 Ilya Zakharevich. All rights reserved.
481This program is free software; you can redistribute it and/or
482modify it under the same terms as Perl itself.
483
484Author of this software makes no claim whatsoever about suitability,
485reliability, edability, editability or usability of this product, and
486should not be kept liable for any damage resulting from the use of
487it. If you can use it, you are in luck, if not, I should not be kept
488responsible. Keep a handy copy of your backup tape at hand.
489
490=head1 SEE ALSO
491
492L<perlguts>, and L<perlguts>, again.
493
494=cut