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,
42       and an optional limit for recursion and array elements (default is 4).
43       The first argument is evaluted in rvalue scalar context, with
44       exceptions for @array and %hash, which dump the array or hash itself.
45       So "Dump @array" works, as does "Dump $foo".  And "Dump pos" will call
46       "pos" in rvalue context, whereas "Dump ${\pos}" will call it in lvalue
47       context.
48
49       Function "DumpArray()" allows dumping of multiple values (useful when
50       you need to analyze returns of functions).
51
52       The global variable $Devel::Peek::pv_limit can be set to limit the
53       number of character printed in various string values.  Setting it to 0
54       means no limit.
55
56       If "use Devel::Peek" directive has a ":opd=FLAGS" argument, this
57       switches on debugging of opcode dispatch.  "FLAGS" should be a
58       combination of "s", "t", and "P" (see -D flags in perlrun).
59
60       ":opd" is a shortcut for ":opd=st".
61
62   Runtime debugging
63       "CvGV($cv)" return one of the globs associated to a subroutine
64       reference $cv.
65
66       debug_flags() returns a string representation of $^D (similar to what
67       is allowed for -D flag).  When called with a numeric argument, sets $^D
68       to the corresponding value.  When called with an argument of the form
69       "flags-flags", set on/off bits of $^D corresponding to letters
70       before/after "-".  (The returned value is for $^D before the
71       modification.)
72
73       runops_debug() returns true if the current opcode dispatcher is the
74       debugging one.  When called with an argument, switches to debugging or
75       non-debugging dispatcher depending on the argument (active for newly-
76       entered subs/etc only).  (The returned value is for the dispatcher
77       before the modification.)
78
79   Memory footprint debugging
80       When perl is compiled with support for memory footprint debugging
81       (default with Perl's malloc()), Devel::Peek provides an access to this
82       API.
83
84       Use mstat() function to emit a memory state statistic to the terminal.
85       For more information on the format of output of mstat() see "Using
86       $ENV{PERL_DEBUG_MSTATS}" in perldebguts.
87
88       Three additional functions allow access to this statistic from Perl.
89       First, use "mstats_fillhash(%hash)" to get the information contained in
90       the output of mstat() into %hash. The field of this hash are
91
92         minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks
93         start_slack topbucket topbucket_ev topbucket_odd total total_chain
94         total_sbrk totfree
95
96       Two additional fields "free", "used" contain array references which
97       provide per-bucket count of free and used chunks.  Two other fields
98       "mem_size", "available_size" contain array references which provide the
99       information about the allocated size and usable size of chunks in each
100       bucket.  Again, see "Using $ENV{PERL_DEBUG_MSTATS}" in perldebguts for
101       details.
102
103       Keep in mind that only the first several "odd-numbered" buckets are
104       used, so the information on size of the "odd-numbered" buckets which
105       are not used is probably meaningless.
106
107       The information in
108
109        mem_size available_size minbucket nbuckets
110
111       is the property of a particular build of perl, and does not depend on
112       the current process.  If you do not provide the optional argument to
113       the functions mstats_fillhash(), fill_mstats(), mstats2hash(), then the
114       information in fields "mem_size", "available_size" is not updated.
115
116       "fill_mstats($buf)" is a much cheaper call (both speedwise and memory-
117       wise) which collects the statistic into $buf in machine-readable form.
118       At a later moment you may need to call "mstats2hash($buf, %hash)" to
119       use this information to fill %hash.
120
121       All three APIs "fill_mstats($buf)", "mstats_fillhash(%hash)", and
122       "mstats2hash($buf, %hash)" are designed to allocate no memory if used
123       the second time on the same $buf and/or %hash.
124
125       So, if you want to collect memory info in a cycle, you may call
126
127         $#buf = 999;
128         fill_mstats($_) for @buf;
129         mstats_fillhash(%report, 1);          # Static info too
130
131         foreach (@buf) {
132           # Do something...
133           fill_mstats $_;                     # Collect statistic
134         }
135         foreach (@buf) {
136           mstats2hash($_, %report);           # Preserve static info
137           # Do something with %report
138         }
139

EXAMPLES

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

EXPORTS

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

BUGS

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

AUTHOR

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

SEE ALSO

475       perlguts, and perlguts, again.
476
477
478
479perl v5.32.1                      2021-03-31                  Devel::Peek(3pm)
Impressum