blib/lib/Devel/MAT/Dumper/Helper.pm

filter: Statementstmt 46.4% 13 / 28 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 3 / 6 Podpod 100% 3 / 3 totaltotal 46.3% 19 / 41 CC 3 SCAR 12.9
CC
3
Cov
41%
write_DMD_helper_h
6.0
extend_module_build
6.0
extra_compiler_flags
2.0
CRAP 3.6
« blib/lib/Devel/MAT/Dumper.pm ↑ Summary lib/Devel/MAT/Dumper.xs »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2019-2024 -- leonerd@leonerd.org.uk
5  
6 package Devel::MAT::Dumper::Helper;
7  
8 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.52';
13  
14 =head1 NAME
15  
16 C<Devel::MAT::Dumper::Helper> - give XS modules extensions for memory dumping
17  
18 =head1 SYNOPSIS
19  
20 =for highlighter language=perl
21  
22 In F<Build.PL>
23  
24 if( eval { require Devel::MAT::Dumper::Helper } ) {
25 Devel::MAT::Dumper::Helper->extend_module_build( $build );
26 }
27  
28 In your module's XS source:
29  
30 =for highlighter language=c
31  
32 #ifdef HAVE_DMD_HELPER
33 # define WANT_DMD_API_044
34 # include "DMD_helper.h"
35 #endif
36  
37 ...
38  
39 #ifdef HAVE_DMD_HELPER
40 static int dumpstruct(pTHX_ DMDContext *ctx, const SV *sv)
41 {
42 int ret = 0;
43  
44 ret += DMD_ANNOTATE_SV(sv, another_sv,
45 "the description of this field");
46 ...
47  
48 return ret;
49 }
50  
51 static int dumpmagic(pTHX_ DMDContext *ctx, const SV *sv, MAGIC *mg)
52 {
53 int ret = 0;
54  
55 ret += DMD_ANNOTATE_SV(sv, another_sv,
56 "the description of this field");
57 ...
58  
59 return ret;
60 }
61 #endif
62  
63 ...
64  
65 BOOT:
66 #ifdef HAVE_DMD_HELPER
67 DMD_SET_PACKAGE_HELPER("My::Package", dumpstruct);
68 DMD_SET_MAGIC_HELPER(&vtbl, dumpmagic);
69 #endif
70  
71 =head1 DESCRIPTION
72  
73 This module provides a build-time helper to assist in writing XS modules that
74 can provide extra information to a L<Devel::MAT> heap dump file when dumping
75 data structures relating to that module.
76  
77 Following the example in the L</SYNOPSIS> section above, the C<dumpstruct>
78 function is called whenever L<Devel::MAT::Dumper> finds an SV blessed into the
79 given package, and the C<dumpmagic> function is called whenever
80 L<Devel::MAT::Dumper> finds an SV with extension magic matching the given
81 magic virtual table pointer. These functions may then inspect the module's
82 state from the SV or MAGIC pointers, and invoke the C<DMD_ANNOTATE_SV> macro
83 to provide extra annotations into the heap dump file about how this SV is
84 related to another one.
85  
86 The C<WANT_DMD_API_044> macro is required before C<#include>ing the file, so
87 as to enable the API structure described here. Without that, an earlier
88 version of the module is provided instead, which will eventually be removed in
89 some later version.
90  
91 Under this code structure, a module will cleanly build, install and run just
92 fine if L<Devel::MAT::Dumper::Helper> is not available at build time, so it is
93 not necessary to list that as a C<configure_requires> or C<build_requires>
94 requirement.
95  
96 Additionally, the way the inserted code is structured does not cause the XS
97 module to load C<Devel::MAT::Dumper> itself, so there is no runtime dependency
98 either, even if the support was made available. The newly inserted code is
99 only invoked if both C<Devel::MAT::Dumper> and this XS module are actually
100 loaded.
101  
102 Note that this entire mechanism is currently experimental.
103  
104 =cut
105  
106 1 my $DMD_helper_h = do {
107 1 local $/;
108 1 readline DATA;
109 };
110  
111 =head1 FUNCTIONS
112  
113 =for highlighter language=perl
114  
115 =cut
116  
117 =head2 write_DMD_helper_h
118  
119 Devel::MAT::Dumper::Helper->write_DMD_helper_h;
120  
121 Writes the L<DMD_helper.h> file to the current working directory. To cause the
122 compiler to actually find this file, see L<extra_compiler_flags>.
123  
124 =cut
125  
126 sub write_DMD_helper_h
127 0 ▶{
Subroutine write_DMD_helper_h: not called CC=2 SCAR=17.9
Pod documented
128 0 shift;
129  
130 0 ▶ open my $out, ">", "DMD_helper.h" or
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", "DMD_helper.h" or die "Cannot open DMD_helper.h for writing - $!\n" 0 0 0
131 die "Cannot open DMD_helper.h for writing - $!\n";
132  
133 0 $out->print( $DMD_helper_h );
134 }
135  
136 =head2 extra_compiler_flags
137  
138 @flags = Devel::MAT::Dumper::Helper->extra_compiler_flags;
139  
140 Returns a list of extra flags that the build scripts should add to the
141 compiler invocation. This enables the C compiler to find the F<DMD_helper.h>
142 file, and also defines a symbol C<HAVE_DMD_HELPER> which the XS code can then
143 use in C<#ifdef> guards:
144  
145 #ifdef HAVE_DMD_HELPER
146 ...
147 #endif
148  
149 =cut
150  
151 sub extra_compiler_flags
152 0 ▶{
Subroutine extra_compiler_flags: not called CC=1 SCAR=6.9
Pod documented
153 0 shift;
154 0 return "-DHAVE_DMD_HELPER",
155 "-I.";
156 }
157  
158 =head2 extend_module_build
159  
160 Devel::MAT::Dumper::Helper->extend_module_build( $build );
161  
162 A convenient shortcut for performing all the tasks necessary to make a
163 L<Module::Build>-based distribution use the helper.
164  
165 =cut
166  
167 sub extend_module_build
168 0 ▶{
Subroutine extend_module_build: not called CC=2 SCAR=17.9
Pod documented
169 0 my $self = shift;
170 0 my ( $build ) = @_;
171  
172 0 ▶ eval { $self->write_DMD_helper_h } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->write_DMD_helper_h } } or do { warn $@; return } 0 0 0
173 0 warn $@;
174 0 return;
175 };
176  
177 # preserve existing flags
178 0 my @flags = @{ $build->extra_compiler_flags };
179 0 push @flags, $self->extra_compiler_flags;
180  
181 0 $build->extra_compiler_flags( @flags );
182 }
183  
184 =head1 XS MACROS
185  
186 =for highlighter language=c
187  
188 The header file provides the following macros, which may be used by the XS
189 module.
190  
191 =head2 DMD_SET_PACKAGE_HELPER
192  
193 typedef int DMD_Helper(pTHX_ DMDContext *ctx, const SV *sv);
194  
195 DMD_SET_PACKAGE_HELPER(char *packagename, DMD_Helper *helper);
196  
197 This macro should be called from the C<BOOT> section of the XS module to
198 associate a helper function with a named package. Whenever an instance of an
199 object blessed into that package is encountered by the dumper, the helper
200 function will be called to provide extra information about it.
201  
202 When invoked, the helper function is passed a pointer to the blessed SV
203 directly - remember this will be the underlying object storage and not the
204 C<RV> that the Perl code uses to refer to it. It should return an integer that
205 is the sum total of the return values of all the calls to C<DMD_ANNOTATE_SV>
206 that it made, or 0 if it did not make any.
207  
208 The I<ctx> pointer to the helper function points at an opaque structure
209 internal to the C<Devel::MAT::Dumper> module. Helper functions are not
210 expected to interact with it, except to pass it on any C<DMD_DUMP_STRUCT>
211 calls it may make.
212  
213 =head2 DMD_SET_MAGIC_HELPER
214  
215 typedef int DMD_MagicHelper(pTHX_ DMDContext *ctx, const SV *sv, MAGIC *mg);
216  
217 DMD_SET_MAGIC_HELPER(MGVTBL *vtbl, DMD_MagicHelper *helper);
218  
219 This macro should be called from the C<BOOT> section of the XS module to
220 associate a helper function with a given magic virtual method table. Whenever
221 an SV with that kind of magic is encountered by the dumper, the helper
222 function will be called to provide extra information about it.
223  
224 When invoked, the helper function is passed a pointer to the magical SV as
225 well as the specific C<MAGIC> instance responsible for this call. It should
226 return an integer that is the sum total of the return values of all the calls
227 to C<DMD_ANNOTATE_SV> that it made, or 0 if it did not make any.
228  
229 The I<ctx> pointer to the helper function points at an opaque structure
230 internal to the C<Devel::MAT::Dumper> module. Helper functions are not
231 expected to interact with it, except to pass it on any C<DMD_DUMP_STRUCT>
232 calls it may make.
233  
234 =head2 DMD_SET_MGv2_HELPER
235  
236 DMD_SET_MGv2_HELPER(void *funcs, DMD_MagicHelper *helper);
237  
238 I<Since version 0.52.>
239  
240 Similar to L</DMD_SET_MAGIC_HELPER>, this macro should be called from the
241 C<BOOT> section of the XS module to associate a helper function for Magic v2,
242 to provide extra annotations about the SV and its magic annotation. Likely
243 this will be in the form of calls to L</DMD_DUMP_STRUCT> on the C<MgPTR> area,
244 or L</DMD_DUMP_MGv2_USERSTRUCT> on the C<MgUSERSTRUCT> area.
245  
246 =head2 DMD_ADD_ROOT
247  
248 DMD_ADD_ROOT(SV *sv, const char *name);
249  
250 This macro should be called from the C<BOOT> section of the XS module to add
251 another root SV pointer to be added to the root SVs table. This is useful for
252 annotating static SV pointers or other storage that can refer to SVs or memory
253 structures within the module, but which would not be discovered by a normal
254 heap walk.
255  
256 The I<name> argument is also used as the description string within the
257 C<Devel::MAT> UI. It should begin with either a C<+> or C<-> character to
258 annotate that the root contains a strong or weak reference, respectively.
259  
260 =head2 DMD_ANNOTATE_SV
261  
262 DMD_ANNOTATE_SV(const SV *referrer, const SV *referrant, const char *label);
263  
264 This macro should be called by a helper function, in order to provide extra
265 information about the SV it has encountered. The macro notes that a pointer
266 exists from the SV given by I<referrer>, pointing at the SV given by
267 I<referrant>, described by the given string label.
268  
269 Each call to this macro returns an integer, which the helper function must
270 accumulate the total of, and return that number to the caller.
271  
272 Not that it is not necessary that either the referrer nor the referrant
273 actually are the SV that the helper function encountered. Arbitrary
274 annotations between SVs are permitted. Additionally, it is permitted that
275 the SV addresses do not in fact point at Perl SVs, but instead point to
276 arbitarary data structures, which should be written about using
277 C<DMD_DUMP_STRUCT>.
278  
279 =head2 DMD_DUMP_STRUCT
280  
281 typedef struct {
282 const char *name;
283 enum {
284 DMD_FIELD_PTR,
285 DMD_FIELD_BOOL,
286 DMD_FIELD_U8,
287 DMD_FIELD_U32,
288 DMD_FIELD_UINT,
289 } type;
290  
291 void *ptr; /* for type=PTR */
292 bool b; /* for type=BOOL */
293 long n; /* for the remaining numerical types */
294 } DMDNamedField;
295  
296 DMD_DUMP_STRUCT(DMDContext *ctx, const char *name, void *addr, size_t size,
297 size_t nfields, const DMDNamedField fields[]);
298  
299 This macro should be called by a helper function, in order to provide extra
300 information about a memory structure that is not a Perl SV. By using this
301 macro, the module can write information into the dumpfile about the memory
302 structure types and values that it operates on, allowing the C<Devel::MAT>
303 tooling to operate on it - such as by following pointers and finding or
304 identifying the contents.
305  
306 The code invoked by this macro at runtime actually does B<two> separate tasks,
307 which are closely related. The first time a call is made for any particular
308 string value in I<name>, the function will write metadata information into the
309 dumpfile which gives the name and type of each of the fields. Every call,
310 including this first one, will write the values of the fields associated with
311 a single instance of the structure, by reusing the information provided to the
312 first call.
313  
314 The I<ctx> argument must be the value given to the helper function. I<addr>
315 gives the pointer address of the structure itself. I<size> should give its
316 total size in bytes (often C<sizeof(*ptr)> is sufficient here).
317  
318 The I<name>, I<nfields>, and I<fields> parameters between them are used both
319 by the initial metadata call, and for every structure instance. I<name> gives
320 a unique name to this type of structure - it should be composed of the base
321 name of the XS module, and a local name within the module, separated by C</>.
322 I<nfields> gives the number of individual field instances given in the
323 I<fields> array, which itself provides a label name, a type, and an actual
324 value.
325  
326 The first two fields of the C<DMDNamedField> structure give its name and type,
327 and one subsequent field should be set to give the value for it. Which field
328 to use depends on the type.
329  
330 Note that it is very important, once a structure name has been seen the first
331 time, that every subsequent call for the same must have exactly the same count
332 of fields, and the types of each of them. The values of the fields, as well as
333 the size of the structure overall, are recorded for every call, but the typing
334 information is stored only once on that first call. It is best to ensure that
335 the module source contains only a single instance of this macro for a given
336 structure name, thus ensuring the type information will always be consistent.
337  
338 =head2 DMD_DUMP_MGv2_USERSTRUCT
339  
340 DMD_DUMP_MGv2_USERSTRUCT(DMDContext *ctx, const char *name, MAGIC *mg,
341 size_t nfields, const DMDNamedField fields[]);
342  
343 I<Since version 0.52.>
344  
345 When built on a Perl that contains Magic v2, this helper macro wraps a call to
346 the same helper function as L</DMD_DUMP_STRUCT>, but formats its arguments a
347 little better for handling the C<MgUSERSTRUCT> area of a Magic v2 annotation.
348 Rather than needing to give the actual structure address and its size, this
349 information can be recovered from the I<mg> pointer directly.
350  
351 =head1 HANDLING C-LEVEL STRUCTURES
352  
353 For example, given a C struct definition such as:
354  
355 struct MyData {
356 SV *buf;
357 int state;
358  
359 AV *more_stuff;
360 };
361  
362 A call to provide this to the dumpfile could look like:
363  
364 struct MyData *dat = ...;
365  
366 DMD_DUMP_STRUCT(ctx, "Module::Name/MyData", dat, sizeof(struct MyData),
367 3, ((const DMDNamedField []){
368 {"the buf SV", DMD_FIELD_PTR, .ptr = dat->buf},
369 {"the state", DMD_FIELD_UINT, .n = dat->state},
370 {"the more_stuff AV", DMD_FIELD_PTR, .ptr = dat->more_stuff},
371 })
372 );
373  
374 Conventionally, names of unique fields all begin C<"the ...">. Fields that
375 point to other Perl SVs should explain what kind of SV they point to, so any
376 discrepencies can be observed in the tooling later on.
377  
378 A call to this macro alone is likely not enough to fully link the information
379 in the dumpfile, however. It is unlikely that any pointer value that the
380 dumper itself will encounter would point to this data structure - if so, Perl
381 would not know how to deal with it. It's likely that the module would use some
382 technique such as storing a pointer in the UV field of a blessed SCALAR SV, as
383 a way to retain it. In that typical example, a helper function should be
384 attached to the package name that SV would be blessed into. When the dumper
385 encounters that blessed SV it will invoke the helper function, which can then
386 call C<DMD_DUMP_STRUCT> and also use C<DMD_ANNOTATE_SV> to provide a linkage
387 between the blessed SV containing the UV value, and this structure.
388  
389 static int dumppackage_mydata(pTHX_ DMDContext *ctx, const SV *sv)
390 {
391 int ret = 0;
392  
393 struct MyData *dat = NUM2PTR(struct MyData *, SvUV((SV *)sv));
394 DMD_DUMP_STRUCT(...);
395  
396 ret += DMD_ANNOTATE_SV(sv, (SV *)dat, "the MyData structure");
397  
398 return ret;
399 }
400  
401 BOOT:
402  
403 There is no ordering requirement between these two - the annotation linking
404 the pointers can be made before, or after, the structure itself has been
405 written. In fact, there are no ordering constraints at all; feel free to write
406 the data structures and annotations in whatever order is most natural to the
407 dumper code,
408  
409 =cut
410  
411 =head1 AUTHOR
412  
413 Paul Evans <leonerd@leonerd.org.uk>
414  
415 =cut
416  
417 1 0x55AA;
418  
419 __DATA__
« blib/lib/Devel/MAT/Dumper.pm ↑ Summary lib/Devel/MAT/Dumper.xs »