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