1Devel::Peek(3pm)       Perl Programmers Reference Guide       Devel::Peek(3pm)
2
3
4

NAME

6       Devel::Peek - A data debugging tool for the XS programmer
7

SYNOPSIS

9               use Devel::Peek;
10               Dump( $a );
11               Dump( $a, 5 );
12               Dump( @a );
13               Dump( %h );
14               DumpArray( 5, $a, $b, ... );
15               mstat "Point 5";
16
17               use Devel::Peek ':opd=st';
18

DESCRIPTION

20       Devel::Peek contains functions which allows raw Perl datatypes to be
21       manipulated from a Perl script.  This is used by those who do XS
22       programming to check that the data they are sending from C to Perl
23       looks as they think it should look.  The trick, then, is to know what
24       the raw datatype is supposed to look like when it gets to Perl.  This
25       document offers some tips and hints to describe good and bad raw data.
26
27       It is very possible that this document will fall far short of being
28       useful to the casual reader.  The reader is expected to understand the
29       material in the first few sections of perlguts.
30
31       Devel::Peek supplies a Dump() function which can dump a raw Perl
32       datatype, and mstat("marker") function to report on memory usage (if
33       perl is compiled with corresponding option).  The function DeadCode()
34       provides statistics on the data "frozen" into inactive "CV".
35       Devel::Peek also supplies SvREFCNT() which can query reference counts
36       on SVs.  This document will take a passive, and safe, approach to data
37       debugging and for that it will describe only the Dump() function.
38
39       All output is to STDERR.
40
41       The Dump() function takes one or two arguments: something to dump, and
42       an optional limit for recursion and array elements (default is 4).  The
43       first argument is evaluated in rvalue scalar context, with exceptions
44       for @array and %hash, which dump the array or hash itself.  So "Dump
45       @array" works, as does "Dump $foo".  And "Dump pos" will call "pos" in
46       rvalue context, whereas "Dump ${\pos}" will call it in lvalue context.
47
48       Function DumpArray() allows dumping of multiple values (useful when you
49       need to analyze returns of functions).
50
51       The global variable $Devel::Peek::pv_limit can be set to limit the
52       number of character printed in various string values.  Setting it to 0
53       means no limit.
54
55       If "use Devel::Peek" directive has a ":opd=FLAGS" argument, this
56       switches on debugging of opcode dispatch.  "FLAGS" should be a
57       combination of "s", "t", and "P" (see -D flags in perlrun).
58
59       ":opd" is a shortcut for ":opd=st".
60
61   Runtime debugging
62       CvGV($cv) return one of the globs associated to a subroutine reference
63       $cv.
64
65       debug_flags() returns a string representation of $^D (similar to what
66       is allowed for -D flag).  When called with a numeric argument, sets $^D
67       to the corresponding value.  When called with an argument of the form
68       "flags-flags", set on/off bits of $^D corresponding to letters
69       before/after "-".  (The returned value is for $^D before the
70       modification.)
71
72       runops_debug() returns true if the current opcode dispatcher is the
73       debugging one.  When called with an argument, switches to debugging or
74       non-debugging dispatcher depending on the argument (active for newly-
75       entered subs/etc only).  (The returned value is for the dispatcher
76       before the modification.)
77
78   Memory footprint debugging
79       When perl is compiled with support for memory footprint debugging
80       (default with Perl's malloc()), Devel::Peek provides an access to this
81       API.
82
83       Use mstat() function to emit a memory state statistic to the terminal.
84       For more information on the format of output of mstat() see "Using
85       $ENV{PERL_DEBUG_MSTATS}" in perldebguts.
86
87       Three additional functions allow access to this statistic from Perl.
88       First, use mstats_fillhash(%hash) to get the information contained in
89       the output of mstat() into %hash. The field of this hash are
90
91         minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks
92         start_slack topbucket topbucket_ev topbucket_odd total total_chain
93         total_sbrk totfree
94
95       Two additional fields "free", "used" contain array references which
96       provide per-bucket count of free and used chunks.  Two other fields
97       "mem_size", "available_size" contain array references which provide the
98       information about the allocated size and usable size of chunks in each
99       bucket.  Again, see "Using $ENV{PERL_DEBUG_MSTATS}" in perldebguts for
100       details.
101
102       Keep in mind that only the first several "odd-numbered" buckets are
103       used, so the information on size of the "odd-numbered" buckets which
104       are not used is probably meaningless.
105
106       The information in
107
108        mem_size available_size minbucket nbuckets
109
110       is the property of a particular build of perl, and does not depend on
111       the current process.  If you do not provide the optional argument to
112       the functions mstats_fillhash(), fill_mstats(), mstats2hash(), then the
113       information in fields "mem_size", "available_size" is not updated.
114
115       fill_mstats($buf) is a much cheaper call (both speedwise and memory-
116       wise) which collects the statistic into $buf in machine-readable form.
117       At a later moment you may need to call "mstats2hash($buf, %hash)" to
118       use this information to fill %hash.
119
120       All three APIs fill_mstats($buf), mstats_fillhash(%hash), and
121       "mstats2hash($buf, %hash)" are designed to allocate no memory if used
122       the second time on the same $buf and/or %hash.
123
124       So, if you want to collect memory info in a cycle, you may call
125
126         $#buf = 999;
127         fill_mstats($_) for @buf;
128         mstats_fillhash(%report, 1);          # Static info too
129
130         foreach (@buf) {
131           # Do something...
132           fill_mstats $_;                     # Collect statistic
133         }
134         foreach (@buf) {
135           mstats2hash($_, %report);           # Preserve static info
136           # Do something with %report
137         }
138

EXAMPLES

140       The following examples don't attempt to show everything as that would
141       be a monumental task, and, frankly, we don't want this manpage to be an
142       internals document for Perl.  The examples do demonstrate some basics
143       of the raw Perl datatypes, and should suffice to get most determined
144       people on their way.  There are no guidewires or safety nets, nor
145       blazed trails, so be prepared to travel alone from this point and on
146       and, if at all possible, don't fall into the quicksand (it's bad for
147       business).
148
149       Oh, one final bit of advice: take perlguts with you.  When you return
150       we expect to see it well-thumbed.
151
152   A simple scalar string
153       Let's begin by looking a simple scalar which is holding a string.
154
155               use Devel::Peek;
156               $a = 42; $a = "hello";
157               Dump $a;
158
159       The output:
160
161               SV = PVIV(0xbc288) at 0xbe9a8
162                 REFCNT = 1
163                 FLAGS = (POK,pPOK)
164                 IV = 42
165                 PV = 0xb2048 "hello"\0
166                 CUR = 5
167                 LEN = 8
168
169       This says $a is an SV, a scalar.  The scalar type is a PVIV, which is
170       capable of holding an integer (IV) and/or a string (PV) value. The
171       scalar's head is allocated at address 0xbe9a8, while the body is at
172       0xbc288.  Its reference count is 1.  It has the "POK" flag set, meaning
173       its current PV field is valid.  Because POK is set we look at the PV
174       item to see what is in the scalar.  The \0 at the end indicate that
175       this PV is properly NUL-terminated.  Note that the IV field still
176       contains its old numeric value, but because FLAGS doesn't have IOK set,
177       we must ignore the IV item.  CUR indicates the number of characters in
178       the PV.  LEN indicates the number of bytes allocated for the PV (at
179       least one more than CUR, because LEN includes an extra byte for the
180       end-of-string marker, then usually rounded up to some efficient
181       allocation unit).
182
183   A simple scalar number
184       If the scalar contains a number the raw SV will be leaner.
185
186               use Devel::Peek;
187               $a = 42;
188               Dump $a;
189
190       The output:
191
192               SV = IV(0xbc818) at 0xbe9a8
193                 REFCNT = 1
194                 FLAGS = (IOK,pIOK)
195                 IV = 42
196
197       This says $a is an SV, a scalar.  The scalar is an IV, a number.  Its
198       reference count is 1.  It has the "IOK" flag set, meaning it is
199       currently being evaluated as a number.  Because IOK is set we look at
200       the IV item to see what is in the scalar.
201
202   A simple scalar with an extra reference
203       If the scalar from the previous example had an extra reference:
204
205               use Devel::Peek;
206               $a = 42;
207               $b = \$a;
208               Dump $a;
209
210       The output:
211
212               SV = IV(0xbe860) at 0xbe9a8
213                 REFCNT = 2
214                 FLAGS = (IOK,pIOK)
215                 IV = 42
216
217       Notice that this example differs from the previous example only in its
218       reference count.  Compare this to the next example, where we dump $b
219       instead of $a.
220
221   A reference to a simple scalar
222       This shows what a reference looks like when it references a simple
223       scalar.
224
225               use Devel::Peek;
226               $a = 42;
227               $b = \$a;
228               Dump $b;
229
230       The output:
231
232               SV = IV(0xf041c) at 0xbe9a0
233                 REFCNT = 1
234                 FLAGS = (ROK)
235                 RV = 0xbab08
236                 SV = IV(0xbe860) at 0xbe9a8
237                   REFCNT = 2
238                   FLAGS = (IOK,pIOK)
239                   IV = 42
240
241       Starting from the top, this says $b is an SV.  The scalar is an IV,
242       which is capable of holding an integer or reference value.  It has the
243       "ROK" flag set, meaning it is a reference (rather than an integer or
244       string).  Notice that Dump follows the reference and shows us what $b
245       was referencing.  We see the same $a that we found in the previous
246       example.
247
248       Note that the value of "RV" coincides with the numbers we see when we
249       stringify $b. The addresses inside IV() are addresses of "X***"
250       structures which hold the current state of an "SV". This address may
251       change during lifetime of an SV.
252
253   A reference to an array
254       This shows what a reference to an array looks like.
255
256               use Devel::Peek;
257               $a = [42];
258               Dump $a;
259
260       The output:
261
262               SV = IV(0xc85998) at 0xc859a8
263                 REFCNT = 1
264                 FLAGS = (ROK)
265                 RV = 0xc70de8
266                 SV = PVAV(0xc71e10) at 0xc70de8
267                   REFCNT = 1
268                   FLAGS = ()
269                   ARRAY = 0xc7e820
270                   FILL = 0
271                   MAX = 0
272                   FLAGS = (REAL)
273                   Elt No. 0
274                   SV = IV(0xc70f88) at 0xc70f98
275                     REFCNT = 1
276                     FLAGS = (IOK,pIOK)
277                     IV = 42
278
279       This says $a is a reference (ROK), which points to another SV which is
280       a PVAV, an array.  The array has one element, element zero, which is
281       another SV. The field "FILL" above indicates the last element in the
282       array, similar to "$#$a".
283
284       If $a pointed to an array of two elements then we would see the
285       following.
286
287               use Devel::Peek 'Dump';
288               $a = [42,24];
289               Dump $a;
290
291       The output:
292
293               SV = IV(0x158c998) at 0x158c9a8
294                 REFCNT = 1
295                 FLAGS = (ROK)
296                 RV = 0x1577de8
297                 SV = PVAV(0x1578e10) at 0x1577de8
298                   REFCNT = 1
299                   FLAGS = ()
300                   ARRAY = 0x1585820
301                   FILL = 1
302                   MAX = 1
303                   FLAGS = (REAL)
304                   Elt No. 0
305                   SV = IV(0x1577f88) at 0x1577f98
306                     REFCNT = 1
307                     FLAGS = (IOK,pIOK)
308                     IV = 42
309                   Elt No. 1
310                   SV = IV(0x158be88) at 0x158be98
311                     REFCNT = 1
312                     FLAGS = (IOK,pIOK)
313                     IV = 24
314
315       Note that "Dump" will not report all the elements in the array, only
316       several first (depending on how deep it already went into the report
317       tree).
318
319   A reference to a hash
320       The following shows the raw form of a reference to a hash.
321
322               use Devel::Peek;
323               $a = {hello=>42};
324               Dump $a;
325
326       The output:
327
328           SV = IV(0x55cb50b50fb0) at 0x55cb50b50fc0
329             REFCNT = 1
330             FLAGS = (ROK)
331             RV = 0x55cb50b2b758
332             SV = PVHV(0x55cb50b319c0) at 0x55cb50b2b758
333               REFCNT = 1
334               FLAGS = (SHAREKEYS)
335               ARRAY = 0x55cb50b941a0  (0:7, 1:1)
336               hash quality = 100.0%
337               KEYS = 1
338               FILL = 1
339               MAX = 7
340               Elt "hello" HASH = 0x3128ece4
341               SV = IV(0x55cb50b464f8) at 0x55cb50b46508
342                 REFCNT = 1
343                 FLAGS = (IOK,pIOK)
344                 IV = 42
345
346       This shows $a is a reference pointing to an SV.  That SV is a PVHV, a
347       hash.
348
349       The "quality" of a hash is defined as the total number of comparisons
350       needed to access every element once, relative to the expected number
351       needed for a random hash. The value can go over 100%.
352
353       The total number of comparisons is equal to the sum of the squares of
354       the number of entries in each bucket.  For a random hash of "<n"> keys
355       into "<k"> buckets, the expected value is:
356
357                       n + n(n-1)/2k
358
359   Dumping a large array or hash
360       The Dump() function, by default, dumps up to 4 elements from a toplevel
361       array or hash.  This number can be increased by supplying a second
362       argument to the function.
363
364               use Devel::Peek;
365               $a = [10,11,12,13,14];
366               Dump $a;
367
368       Notice that Dump() prints only elements 10 through 13 in the above
369       code.  The following code will print all of the elements.
370
371               use Devel::Peek 'Dump';
372               $a = [10,11,12,13,14];
373               Dump $a, 5;
374
375   A reference to an SV which holds a C pointer
376       This is what you really need to know as an XS programmer, of course.
377       When an XSUB returns a pointer to a C structure that pointer is stored
378       in an SV and a reference to that SV is placed on the XSUB stack.  So
379       the output from an XSUB which uses something like the T_PTROBJ map
380       might look something like this:
381
382               SV = IV(0xf381c) at 0xc859a8
383                 REFCNT = 1
384                 FLAGS = (ROK)
385                 RV = 0xb8ad8
386                 SV = PVMG(0xbb3c8) at 0xc859a0
387                   REFCNT = 1
388                   FLAGS = (OBJECT,IOK,pIOK)
389                   IV = 729160
390                   NV = 0
391                   PV = 0
392                   STASH = 0xc1d10       "CookBookB::Opaque"
393
394       This shows that we have an SV which is a reference, which points at
395       another SV.  In this case that second SV is a PVMG, a blessed scalar.
396       Because it is blessed it has the "OBJECT" flag set.  Note that an SV
397       which holds a C pointer also has the "IOK" flag set.  The "STASH" is
398       set to the package name which this SV was blessed into.
399
400       The output from an XSUB which uses something like the T_PTRREF map,
401       which doesn't bless the object, might look something like this:
402
403               SV = IV(0xf381c) at 0xc859a8
404                 REFCNT = 1
405                 FLAGS = (ROK)
406                 RV = 0xb8ad8
407                 SV = PVMG(0xbb3c8) at 0xc859a0
408                   REFCNT = 1
409                   FLAGS = (IOK,pIOK)
410                   IV = 729160
411                   NV = 0
412                   PV = 0
413
414   A reference to a subroutine
415       Looks like this:
416
417               SV = IV(0x24d2dd8) at 0x24d2de8
418                 REFCNT = 1
419                 FLAGS = (TEMP,ROK)
420                 RV = 0x24e79d8
421                 SV = PVCV(0x24e5798) at 0x24e79d8
422                   REFCNT = 2
423                   FLAGS = ()
424                   COMP_STASH = 0x22c9c50      "main"
425                   START = 0x22eed60 ===> 0
426                   ROOT = 0x22ee490
427                   GVGV::GV = 0x22de9d8        "MY" :: "top_targets"
428                   FILE = "(eval 5)"
429                   DEPTH = 0
430                   FLAGS = 0x0
431                   OUTSIDE_SEQ = 93
432                   PADLIST = 0x22e9ed8
433                   PADNAME = 0x22e9ec0(0x22eed00) PAD = 0x22e9ea8(0x22eecd0)
434                   OUTSIDE = 0x22c9fb0 (MAIN)
435
436       This shows that
437
438       •   the subroutine is not an XSUB (since "START" and "ROOT" are non-
439           zero, and "XSUB" is not listed, and is thus null);
440
441       •   that it was compiled in the package "main";
442
443       •   under the name "MY::top_targets";
444
445       •   inside a 5th eval in the program;
446
447       •   it is not currently executed (because "DEPTH" is 0);
448
449       •   it has no prototype ("PROTOTYPE" field is missing).
450

EXPORTS

452       "Dump", "mstat", "DeadCode", "DumpArray", "DumpWithOP" and "DumpProg",
453       "fill_mstats", "mstats_fillhash", "mstats2hash" by default.
454       Additionally available "SvREFCNT", "SvREFCNT_inc" and "SvREFCNT_dec".
455

BUGS

457       Readers have been known to skip important parts of perlguts, causing
458       much frustration for all.
459

AUTHOR

461       Ilya Zakharevich    ilya@math.ohio-state.edu
462
463       Copyright (c) 1995-98 Ilya Zakharevich. All rights reserved.  This
464       program is free software; you can redistribute it and/or modify it
465       under the same terms as Perl itself.
466
467       Author of this software makes no claim whatsoever about suitability,
468       reliability, edability, editability or usability of this product, and
469       should not be kept liable for any damage resulting from the use of it.
470       If you can use it, you are in luck, if not, I should not be kept
471       responsible. Keep a handy copy of your backup tape at hand.
472

SEE ALSO

474       perlguts, and perlguts, again.
475
476
477
478perl v5.38.2                      2023-11-30                  Devel::Peek(3pm)
Impressum