lib/Devel/MAT/Dumper.xs

filter: Statementstmt 63.4% 409 / 645 Branchbran 60.9% 229 / 376 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 62.4% 638 / 1021 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Devel/MAT/Dumper/Helper.pm ↑ Summary
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, 2013-2024 -- leonerd@leonerd.org.uk
5 */
6  
7 #include "EXTERN.h"
8 #include "perl.h"
9 #include "XSUB.h"
10  
11 #include <errno.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <stdio.h>
15  
16 #define FORMAT_VERSION_MAJOR 0
17 #define FORMAT_VERSION_MINOR 6
18  
19 #ifndef SvOOK_offset
20 # define SvOOK_offset(sv, len) STMT_START { len = SvIVX(sv); } STMT_END
21 #endif
22  
23 #ifndef CxHASARGS
24 # define CxHASARGS(cx) ((cx)->blk_sub.hasargs)
25 #endif
26  
27 #ifndef OpSIBLING
28 # define OpSIBLING(o) ((o)->op_sibling)
29 #endif
30  
31 #ifndef HvNAMELEN
32 # define HvNAMELEN(hv) (strlen(HvNAME(hv)))
33 #endif
34  
35 /* This technically applies all the way back to 5.6 if we need it... */
36 #if (PERL_REVISION == 5) && (PERL_VERSION == 10) && (PERL_SUBVERSION == 0)
37 # define CxOLD_OP_TYPE(cx) ((cx)->blk_eval.old_op_type)
38 #endif
39  
40 #ifdef ObjectFIELDS
41 # define HAVE_FEATURE_CLASS
42 #endif
43  
44 #if (PERL_REVISION == 5) && (PERL_VERSION < 38)
45 /* Perl v5.38 added HvHasAUX. Before that, just use SvOOK */
46 # define HvHasAUX(hv) SvOOK(hv)
47 #endif
48  
49 #if defined(MgIsV2)
50 # define HAVE_MAGICv2
51 #endif
52  
53 static int max_string;
54  
55 #if NVSIZE == 8
56 # define PMAT_NVSIZE 8
57 #else
58 # define PMAT_NVSIZE 10
59 #endif
60  
61 #if (PERL_REVISION == 5) && (PERL_VERSION >= 38)
62 # define SAVEt_ARG0_MAX SAVEt_REGCONTEXT
63 # define SAVEt_ARG1_MAX SAVEt_FREERCPV
64 # define SAVEt_ARG2_MAX SAVEt_RCPV
65 # define SAVEt_MAX SAVEt_HINTS_HH
66 #elif (PERL_REVISION == 5) && (PERL_VERSION >= 34)
67 # define SAVEt_ARG0_MAX SAVEt_REGCONTEXT
68 # define SAVEt_ARG1_MAX SAVEt_STRLEN_SMALL
69 # define SAVEt_ARG2_MAX SAVEt_APTR
70 # define SAVEt_MAX SAVEt_HINTS_HH
71 #elif (PERL_REVISION == 5) && (PERL_VERSION >= 26)
72 # define SAVEt_ARG0_MAX SAVEt_REGCONTEXT
73 # define SAVEt_ARG1_MAX SAVEt_FREEPADNAME
74 # define SAVEt_ARG2_MAX SAVEt_APTR
75 # define SAVEt_MAX SAVEt_DELETE
76 /* older perls already defined SAVEt_ARG<n>_MAX */
77 #elif (PERL_REVISION == 5) && (PERL_VERSION >= 22)
78 # define SAVEt_MAX SAVEt_DELETE
79 #elif (PERL_REVISION == 5) && (PERL_VERSION >= 20)
80 # define SAVEt_MAX SAVEt_AELEM
81 #elif (PERL_REVISION == 5) && (PERL_VERSION >= 18)
82 # define SAVEt_MAX SAVEt_GVSLOT
83 #endif
84  
85 static SV *tmpsv; /* A temporary SV for internal purposes. Will not get dumped */
86  
87 0 static SV *make_tmp_iv(IV iv)
88 {
89 0 ▶ if(!tmpsv)
Branch: 1 branch
BranchTrueFalseTotal
if(!tmpsv) 0 0 0
90 0 tmpsv = newSV(0);
91 0 sv_setiv(tmpsv, iv);
92 0 return tmpsv;
93 }
94  
95 static uint8_t sv_sizes[] = {
96 /* Header PTRs, STRs */
97 4 + PTRSIZE + UVSIZE, 1, 0, /* common SV */
98 UVSIZE + PTRSIZE, 8, 2, /* GLOB */
99 1 + 2*UVSIZE + PMAT_NVSIZE, 1, 1, /* SCALAR */
100 1, 2, 0, /* REF */
101 1 + UVSIZE, 0, 0, /* ARRAY + has body */
102 UVSIZE, 1, 0, /* HASH + has body */
103 UVSIZE + 0, 1 + 4, 0 + 1, /* STASH = extends HASH */
104 5 + UVSIZE + 2*PTRSIZE, 5, 2, /* CODE + has body */
105 2*UVSIZE, 3, 0, /* IO */
106 1 + 2*UVSIZE, 1, 0, /* LVALUE */
107 0, 0, 0, /* REGEXP */
108 0, 0, 0, /* FORMAT */
109 0, 0, 0, /* INVLIST */
110 0, 0, 0, /* UNDEF */
111 0, 0, 0, /* YES */
112 0, 0, 0, /* NO */
113 UVSIZE, 0, 0, /* OBJECT */
114 UVSIZE + 0, 1+4+1, 0+1, /* CLASS = extends STASH */
115 };
116  
117 static uint8_t svx_sizes[] = {
118 /* Header PTRs STRs */
119 2, 3, 0, /* magic */
120 0, 1, 0, /* saved SV */
121 0, 1, 0, /* saved AV */
122 0, 1, 0, /* saved HV */
123 UVSIZE, 1, 0, /* saved AELEM */
124 0, 2, 0, /* saved HELEM */
125 0, 1, 0, /* saved CV */
126 0, 1, 1, /* SV->SV annotation */
127 2*UVSIZE, 0, 1, /* SV leak report */
128 PTRSIZE, 0, 0, /* PV shared HEK */
129 4+2*UVSIZE, 5, 0, /* magic v2 */
130 };
131  
132 static uint8_t ctx_sizes[] = {
133 /* Header PTRs STRs */
134 1 + UVSIZE, 0, 1, /* common CTX */
135 4, 2, 0, /* SUB */
136 0, 0, 0, /* TRY */
137 0, 1, 0, /* EVAL */
138 };
139  
140 // These do NOT agree with perl's SVt_* constants!
141 enum PMAT_SVt {
142 PMAT_SVtGLOB = 1,
143 PMAT_SVtSCALAR,
144 PMAT_SVtREF,
145 PMAT_SVtARRAY,
146 PMAT_SVtHASH,
147 PMAT_SVtSTASH,
148 PMAT_SVtCODE,
149 PMAT_SVtIO,
150 PMAT_SVtLVALUE,
151 PMAT_SVtREGEXP,
152 PMAT_SVtFORMAT,
153 PMAT_SVtINVLIST,
154 PMAT_SVtUNDEF,
155 PMAT_SVtYES,
156 PMAT_SVtNO,
157 PMAT_SVtOBJ,
158 PMAT_SVtCLASS,
159  
160 PMAT_SVtSTRUCT = 0x7F, /* fields as described by corresponding META_STRUCT */
161  
162 /* TODO: emit these in DMD_helper.h */
163 PMAT_SVxMAGIC = 0x80,
164 PMAT_SVxSAVED_SV,
165 PMAT_SVxSAVED_AV,
166 PMAT_SVxSAVED_HV,
167 PMAT_SVxSAVED_AELEM,
168 PMAT_SVxSAVED_HELEM,
169 PMAT_SVxSAVED_CV,
170 PMAT_SVxSVSVnote,
171 PMAT_SVxDEBUGREPORT,
172 PMAT_SVxPV_SHARED_HEK,
173 PMAT_SVxMAGICv2,
174  
175 PMAT_SVtMETA_STRUCT = 0xF0,
176 };
177  
178 enum PMAT_CODEx {
179 PMAT_CODEx_CONSTSV = 1,
180 PMAT_CODEx_CONSTIX,
181 PMAT_CODEx_GVSV,
182 PMAT_CODEx_GVIX,
183 PMAT_CODEx_PADNAME,
184 /* PMAT_CODEx_PADSV was 6 */
185 PMAT_CODEx_PADNAMES = 7,
186 PMAT_CODEx_PAD,
187 PMAT_CODEx_PADNAME_FLAGS,
188 PMAT_CODEx_PADNAME_FIELD,
189 };
190  
191 enum PMAT_CLASSx {
192 PMAT_CLASSx_FIELD = 1,
193 };
194  
195 enum PMAT_CTXt {
196 PMAT_CTXtSUB = 1,
197 PMAT_CTXtTRY,
198 PMAT_CTXtEVAL,
199 };
200  
201 /* API v0.44 */
202 typedef struct {
203 FILE *fh;
204 int next_structid;
205 HV *structdefs;
206 } DMDContext;
207  
208 typedef int DMD_Helper(pTHX_ DMDContext *ctx, SV const *sv);
209 static HV *helper_per_package;
210  
211 typedef int DMD_MagicHelper(pTHX_ DMDContext *ctx, SV const *sv, MAGIC *mg);
212 static HV *helper_per_magic;
213  
214 243636 static void write_u8(FILE *fh, uint8_t v)
215 {
216 243636 fwrite(&v, 1, 1, fh);
217 243636 }
218  
219 /* We just write multi-byte integers in native endian, because we've declared
220 * in the file flags what the platform byte direction is anyway
221 */
222 0 static void write_u16(FILE *fh, uint16_t v)
223 {
224 0 fwrite(&v, 2, 1, fh);
225 0 }
226  
227 95717 static void write_u32(FILE *fh, uint32_t v)
228 {
229 95717 fwrite(&v, 4, 1, fh);
230 95717 }
231  
232 332778 static void write_u64(FILE *fh, uint64_t v)
233 {
234 332778 fwrite(&v, 8, 1, fh);
235 332778 }
236  
237 332778 static void write_uint(FILE *fh, UV v)
238 {
239 #if UVSIZE == 8
240 332778 write_u64(fh, v);
241 #elif UVSIZE == 4
242 write_u32(fh, v);
243 #else
244 # error "Expected UVSIZE to be either 4 or 8"
245 #endif
246 332778 }
247  
248 57361 static void write_ptr(FILE *fh, const void *ptr)
249 {
250 57361 fwrite(&ptr, sizeof ptr, 1, fh);
251 57361 }
252  
253 486625 static void write_svptr(FILE *fh, const SV *ptr)
254 {
255 486625 fwrite(&ptr, sizeof ptr, 1, fh);
256 486625 }
257  
258 32422 static void write_nv(FILE *fh, NV v)
259 {
260 #if NVSIZE == 8
261 32422 fwrite(&v, sizeof(NV), 1, fh);
262 #else
263 // long double is 10 bytes but sizeof() may be 16.
264 fwrite(&v, 10, 1, fh);
265 #endif
266 32422 }
267  
268 78916 static void write_strn(FILE *fh, const char *s, size_t len)
269 {
270 78916 write_uint(fh, len);
271 78916 fwrite(s, len, 1, fh);
272 78916 }
273  
274 67549 static void write_str(FILE *fh, const char *s)
275 {
276 67549 ▶ if(s)
Branch: 1 branch
BranchTrueFalseTotal
if(s) 30022 37527 67549
277 30022 write_strn(fh, s, strlen(s));
278 else
279 37527 write_uint(fh, -1);
280 67549 }
281  
282 #define PMOP_pmreplroot(o) o->op_pmreplrootu.op_pmreplroot
283  
284 #if (PERL_REVISION == 5) && (PERL_VERSION < 14)
285 # define OP_CLASS(o) (PL_opargs[o->op_type] & OA_CLASS_MASK)
286 #endif
287  
288 static void dump_optree(FILE *fh, const CV *cv, OP *o);
289 162008 static void dump_optree(FILE *fh, const CV *cv, OP *o)
290 {
291 OP *kid;
292  
293 162008 switch(o->op_type) {
294 14708 case OP_CONST:
295 case OP_METHOD_NAMED:
296 #ifdef USE_ITHREADS
297 if(o->op_targ) {
298 write_u8(fh, PMAT_CODEx_CONSTIX);
299 write_uint(fh, o->op_targ);
300 }
301 #else
302 14708 write_u8(fh, PMAT_CODEx_CONSTSV);
303 14708 write_svptr(fh, cSVOPx(o)->op_sv);
304 #endif
305 14708 break;
306  
307 6410 case OP_AELEMFAST:
308 case OP_GVSV:
309 case OP_GV:
310 #ifdef USE_ITHREADS
311 write_u8(fh, PMAT_CODEx_GVIX);
312 write_uint(fh, o->op_targ ? o->op_targ : cPADOPx(o)->op_padix);
313 #else
314 6410 write_u8(fh, PMAT_CODEx_GVSV);
315 6410 write_svptr(fh, cSVOPx(o)->op_sv);
316 #endif
317 6410 break;
318 }
319  
320 162008 ▶ if(o->op_flags & OPf_KIDS) {
Branch: 1 branch
BranchTrueFalseTotal
if(o->op_flags & OPf_KIDS) { 74997 87011 162008
321 233757 ▶ for (kid = ((UNOP*)o)->op_first; kid; kid = OpSIBLING(kid)) {
Branch: 2 branches
BranchTrueFalseTotal
for (kid = ((UNOP*)o)->op_first; kid; kid = OpSIBLING(kid)) { 83763 74997 158760
for (kid = ((UNOP*)o)->op_first; kid; kid = OpSIBLING(kid)) { 158760 74997 233757
322 158760 dump_optree(fh, cv, kid);
323 }
324 }
325  
326 162008 ▶ if(OP_CLASS(o) == OA_PMOP &&
Branch: 2 branches
BranchTrueFalseTotal
if(OP_CLASS(o) == OA_PMOP && 0 162008 162008
if(OP_CLASS(o) == OA_PMOP && 843 161165 162008
327 #if (PERL_REVISION == 5) && ((PERL_VERSION > 25) || ((PERL_VERSION == 25) && (PERL_SUBVERSION >= 6)))
328 /* The OP_PUSHRE behaviour was moved to OP_SPLIT in 5.25.6 */
329 843 ▶ o->op_type != OP_SPLIT &&
Branch: 1 branch
BranchTrueFalseTotal
o->op_type != OP_SPLIT && 807 36 843
330 #else
331 o->op_type != OP_PUSHRE &&
332 #endif
333 807 ▶ (kid = PMOP_pmreplroot(cPMOPx(o))))
Branch: 1 branch
BranchTrueFalseTotal
(kid = PMOP_pmreplroot(cPMOPx(o)))) 48 759 807
334 48 dump_optree(fh, cv, kid);
335 162008 }
336  
337 89412 static void write_common_sv(FILE *fh, const SV *sv, size_t size)
338 {
339 // Header
340 89412 write_svptr(fh, sv);
341 89412 write_u32(fh, SvREFCNT(sv));
342 89412 write_uint(fh, sizeof(SV) + size);
343  
344 // PTRs
345 89412 ▶ write_svptr(fh, SvOBJECT(sv) ? (SV*)SvSTASH(sv) : NULL);
Branch: 1 branch
BranchTrueFalseTotal
write_svptr(fh, SvOBJECT(sv) ? (SV*)SvSTASH(sv) : NULL); 170 89242 89412
346 89412 }
347  
348 7533 static void write_private_gv(FILE *fh, const GV *gv)
349 {
350 7533 write_common_sv(fh, (const SV *)gv,
351 7533 ▶ sizeof(XPVGV) + (isGV_with_GP(gv) ? sizeof(struct gp) : 0));
Branch: 3 branches
BranchTrueFalseTotal
sizeof(XPVGV) + (isGV_with_GP(gv) ? sizeof(struct gp) : 0)); 7533 0 7533
sizeof(XPVGV) + (isGV_with_GP(gv) ? sizeof(struct gp) : 0)); 0 7533 7533
sizeof(XPVGV) + (isGV_with_GP(gv) ? sizeof(struct gp) : 0)); 0 0 0
352  
353 7533 ▶ if(isGV_with_GP(gv)) {
Branch: 3 branches
BranchTrueFalseTotal
if(isGV_with_GP(gv)) { 7533 0 7533
if(isGV_with_GP(gv)) { 0 7533 7533
if(isGV_with_GP(gv)) { 0 0 0
354 // Header
355 7533 write_uint(fh, GvLINE(gv));
356 7533 write_ptr(fh, GvNAME_HEK(gv));
357  
358 // PTRs
359 7533 write_svptr(fh, (SV*)GvSTASH(gv));
360 7533 write_svptr(fh, GvSV(gv));
361 7533 write_svptr(fh, (SV*)GvAV(gv));
362 7533 write_svptr(fh, (SV*)GvHV(gv));
363 7533 write_svptr(fh, (SV*)GvCV(gv));
364 7533 write_svptr(fh, (SV*)GvEGV(gv));
365 7533 ▶ write_svptr(fh, (SV*)GvIO(gv));
Branch: 4 branches
BranchTrueFalseTotal
write_svptr(fh, (SV*)GvIO(gv)); 7533 0 7533
write_svptr(fh, (SV*)GvIO(gv)); 0 7533 7533
write_svptr(fh, (SV*)GvIO(gv)); 0 0 0
write_svptr(fh, (SV*)GvIO(gv)); 7533 0 7533
366 7533 write_svptr(fh, (SV*)GvFORM(gv));
367  
368 // STRs
369 7533 write_str(fh, GvNAME(gv));
370 7533 ▶ write_str(fh, GvFILE(gv));
Branch: 1 branch
BranchTrueFalseTotal
write_str(fh, GvFILE(gv)); 7533 0 7533
371 }
372 else {
373 // Header
374 0 write_uint(fh, 0);
375 0 write_ptr(fh, NULL);
376  
377 // PTRs
378 0 write_svptr(fh, (SV*)GvSTASH(gv));
379 0 write_svptr(fh, NULL);
380 0 write_svptr(fh, NULL);
381 0 write_svptr(fh, NULL);
382 0 write_svptr(fh, NULL);
383 0 write_svptr(fh, NULL);
384 0 write_svptr(fh, NULL);
385 0 write_svptr(fh, NULL);
386  
387 // STRs
388 0 write_str(fh, NULL);
389 0 write_str(fh, NULL);
390 }
391 7533 }
392  
393 32422 static void write_private_sv(FILE *fh, const SV *sv)
394 {
395 32422 size_t size = 0;
396 32422 switch(SvTYPE(sv)) {
397 501 case SVt_NV: size += sizeof(NV); break;
398 21474 case SVt_PV: size += sizeof(XPV) - STRUCT_OFFSET(XPV, xpv_cur); break;
399 18 case SVt_PVIV: size += sizeof(XPVIV) - STRUCT_OFFSET(XPV, xpv_cur); break;
400 27 case SVt_PVNV: size += sizeof(XPVNV) - STRUCT_OFFSET(XPV, xpv_cur); break;
401 1150 case SVt_PVMG: size += sizeof(XPVMG); break;
402 9252 default: break;
403 }
404  
405 32422 ▶ if(SvPOK(sv))
Branch: 1 branch
BranchTrueFalseTotal
if(SvPOK(sv)) 22558 9864 32422
406 22558 size += SvLEN(sv);
407 32422 ▶ if(SvOOK(sv)) {
Branch: 4 branches
BranchTrueFalseTotal
if(SvOOK(sv)) { 0 32422 32422
if(SvOOK(sv)) { 0 0 0
if(SvOOK(sv)) { 0 0 0
if(SvOOK(sv)) { 0 32422 32422
408 STRLEN offset;
409 0 ▶ SvOOK_offset(sv, offset);
Branch: 5 branches
BranchTrueFalseTotal
SvOOK_offset(sv, offset); 0 0 0
SvOOK_offset(sv, offset); 0 0 0
SvOOK_offset(sv, offset); 0 0 0
SvOOK_offset(sv, offset); 0 0 0
SvOOK_offset(sv, offset); 0 0 0
410 0 size += offset;
411 }
412  
413 32422 write_common_sv(fh, sv, size);
414  
415 // Header
416 32422 write_u8(fh, (SvIOK(sv) ? 0x01 : 0) |
417 32422 ▶ (SvUOK(sv) ? 0x02 : 0) |
Branch: 1 branch
BranchTrueFalseTotal
(SvUOK(sv) ? 0x02 : 0) | 8 32414 32422
418 32422 (SvNOK(sv) ? 0x04 : 0) |
419 32422 (SvPOK(sv) ? 0x08 : 0) |
420 32422 (SvUTF8(sv) ? 0x10 : 0));
421 32422 ▶ write_uint(fh, SvIOK(sv) ? SvUVX(sv) : 0);
Branch: 1 branch
BranchTrueFalseTotal
write_uint(fh, SvIOK(sv) ? SvUVX(sv) : 0); 9357 23065 32422
422 32422 ▶ write_nv(fh, SvNOK(sv) ? SvNVX(sv) : 0.0);
Branch: 1 branch
BranchTrueFalseTotal
write_nv(fh, SvNOK(sv) ? SvNVX(sv) : 0.0); 533 31889 32422
423 32422 ▶ write_uint(fh, SvPOK(sv) ? SvCUR(sv) : 0);
Branch: 1 branch
BranchTrueFalseTotal
write_uint(fh, SvPOK(sv) ? SvCUR(sv) : 0); 22558 9864 32422
424  
425 // PTRs
426 #if (PERL_REVISION == 5) && (PERL_VERSION <= 20)
427 write_svptr(fh, (SV *)SvOURSTASH(sv));
428 #else
429 32422 write_svptr(fh, NULL);
430 #endif
431  
432 // STRs
433 32422 ▶ if(SvPOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvPOK(sv)) { 22558 9864 32422
434 22558 STRLEN len = SvCUR(sv);
435 22558 ▶ if(max_string > -1 && len > max_string)
Branch: 2 branches
BranchTrueFalseTotal
if(max_string > -1 && len > max_string) 22558 0 22558
if(max_string > -1 && len > max_string) 9 22549 22558
436 9 len = max_string;
437 22558 write_strn(fh, SvPVX((SV *)sv), len);
438 }
439 else
440 9864 write_str(fh, NULL);
441  
442 // Extensions
443 32422 ▶ if(SvPOKp(sv) && SvIsCOW_shared_hash(sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if(SvPOKp(sv) && SvIsCOW_shared_hash(sv)) { 22576 9846 32422
if(SvPOKp(sv) && SvIsCOW_shared_hash(sv)) { 20811 1765 22576
if(SvPOKp(sv) && SvIsCOW_shared_hash(sv)) { 10882 9929 20811
444 10882 write_u8(fh, PMAT_SVxPV_SHARED_HEK);
445 10882 write_svptr(fh, sv);
446 10882 write_ptr(fh, SvSHARED_HEK_FROM_PV(SvPVX_const(sv)));
447 }
448 32422 }
449  
450 3151 static void write_private_rv(FILE *fh, const SV *rv)
451 {
452 3151 write_common_sv(fh, rv, 0);
453  
454 // Header
455 3151 write_u8(fh, (SvWEAKREF(rv) ? 0x01 : 0));
456  
457 // PTRs
458 3151 write_svptr(fh, SvRV((SV *)rv));
459 #if (PERL_REVISION == 5) && (PERL_VERSION <= 20)
460 write_svptr(fh, (SV *)SvOURSTASH(rv));
461 #else
462 3151 write_svptr(fh, NULL);
463 #endif
464 3151 }
465  
466 8149 static void write_private_av(FILE *fh, const AV *av)
467 {
468 /* Perl doesn't bother to keep AvFILL(PL_curstack) updated for efficiency
469 * reasons, so if we're looking at PL_curstack we'll use a different method
470 * to calculate this
471 */
472 8149 ▶ int len = (av == PL_curstack) ? (PL_stack_sp - PL_stack_base + 1) :
Branch: 1 branch
BranchTrueFalseTotal
int len = (av == PL_curstack) ? (PL_stack_sp - PL_stack_base + 1) : 1 8148 8149
473 8148 AvFILLp(av) + 1;
474  
475 8149 write_common_sv(fh, (const SV *)av,
476 8149 sizeof(XPVAV) + sizeof(SV *) * (AvMAX(av) + 1));
477  
478 // Header
479 8149 write_uint(fh, len);
480 8149 write_u8(fh, (!AvREAL(av) ? 0x01 : 0));
481  
482 // Body
483 int i;
484 62410 ▶ for(i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < len; i++) 54261 8149 62410
485 54261 write_svptr(fh, AvARRAY(av)[i]);
486 8149 }
487  
488 1743 static int write_hv_header(FILE *fh, const HV *hv, size_t size)
489 {
490 1743 size += sizeof(XPVHV);
491 1743 int nkeys = 0;
492  
493 1743 ▶ if(HvARRAY(hv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(HvARRAY(hv)) { 1471 272 1743
494 int bucket;
495 85793 ▶ for(bucket = 0; bucket <= HvMAX(hv); bucket++) {
Branch: 1 branch
BranchTrueFalseTotal
for(bucket = 0; bucket <= HvMAX(hv); bucket++) { 84322 1471 85793
496 HE *he;
497 84322 size += sizeof(HE *);
498  
499 110658 ▶ for(he = HvARRAY(hv)[bucket]; he; he = he->hent_next) {
Branch: 1 branch
BranchTrueFalseTotal
for(he = HvARRAY(hv)[bucket]; he; he = he->hent_next) { 26336 84322 110658
500 26336 size += sizeof(HE);
501 26336 nkeys++;
502  
503 26336 ▶ if(!HvSHAREKEYS(hv))
Branch: 1 branch
BranchTrueFalseTotal
if(!HvSHAREKEYS(hv)) 10800 15536 26336
504 10800 size += sizeof(HEK) + he->hent_hek->hek_len + 2;
505 }
506 }
507 }
508  
509 1743 write_common_sv(fh, (const SV *)hv, size);
510  
511 1743 return nkeys;
512 }
513  
514 1443 static void write_hv_body_elems(FILE *fh, const HV *hv)
515 {
516 // The shared string table (PL_strtab) has shared strings as keys but its
517 // values are not SV pointers; they are refcounts. Pretend these values are
518 // NULL.
519 1443 bool is_strtab = (hv == PL_strtab);
520  
521 int bucket;
522 79213 ▶ for(bucket = 0; bucket <= HvMAX(hv); bucket++) {
Branch: 1 branch
BranchTrueFalseTotal
for(bucket = 0; bucket <= HvMAX(hv); bucket++) { 77770 1443 79213
523 HE *he;
524 104106 ▶ for(he = HvARRAY(hv)[bucket]; he; he = he->hent_next) {
Branch: 1 branch
BranchTrueFalseTotal
for(he = HvARRAY(hv)[bucket]; he; he = he->hent_next) { 26336 77770 104106
525 STRLEN keylen;
526 26336 ▶ char *keypv = HePV(he, keylen);
Branch: 1 branch
BranchTrueFalseTotal
char *keypv = HePV(he, keylen); 0 26336 26336
527 26336 write_strn(fh, keypv, keylen);
528  
529 26336 HEK *hek = HeKEY_hek(he);
530 26336 bool hek_is_shared =
531 #ifdef HVhek_NOTSHARED
532 26336 !(HEK_FLAGS(hek) & HVhek_NOTSHARED);
533 #else
534 true;
535 #endif
536 26336 ▶ write_ptr(fh, hek_is_shared ? hek : NULL);
Branch: 1 branch
BranchTrueFalseTotal
write_ptr(fh, hek_is_shared ? hek : NULL); 22711 3625 26336
537  
538 26336 ▶ write_svptr(fh, is_strtab ? NULL : HeVAL(he));
Branch: 1 branch
BranchTrueFalseTotal
write_svptr(fh, is_strtab ? NULL : HeVAL(he)); 7175 19161 26336
539 }
540 }
541 1443 }
542  
543 1446 static void write_private_hv(FILE *fh, const HV *hv)
544 {
545 1446 int nkeys = write_hv_header(fh, hv, 0);
546  
547 // Header
548 1446 write_uint(fh, nkeys);
549  
550 // PTRs
551 1446 ▶ if(HvHasAUX(hv) && HvAUX(hv))
Branch: 2 branches
BranchTrueFalseTotal
if(HvHasAUX(hv) && HvAUX(hv)) 146 1300 1446
if(HvHasAUX(hv) && HvAUX(hv)) 146 0 146
552 146 write_svptr(fh, (SV*)HvAUX(hv)->xhv_backreferences);
553 else
554 1300 write_svptr(fh, NULL);
555  
556 // Body
557 1446 ▶ if(HvARRAY(hv) && nkeys)
Branch: 2 branches
BranchTrueFalseTotal
if(HvARRAY(hv) && nkeys) 1174 272 1446
if(HvARRAY(hv) && nkeys) 1146 28 1174
558 1146 write_hv_body_elems(fh, hv);
559 1446 }
560  
561 297 static void write_stash_ptrs(FILE *fh, const HV *stash)
562 {
563 297 struct mro_meta *mro_meta = HvAUX(stash)->xhv_mro_meta;
564  
565 297 ▶ if(HvHasAUX(stash) && HvAUX(stash))
Branch: 2 branches
BranchTrueFalseTotal
if(HvHasAUX(stash) && HvAUX(stash)) 297 0 297
if(HvHasAUX(stash) && HvAUX(stash)) 297 0 297
566 297 write_svptr(fh, (SV*)HvAUX(stash)->xhv_backreferences);
567 else
568 0 write_svptr(fh, NULL);
569 297 ▶ if(mro_meta) {
Branch: 1 branch
BranchTrueFalseTotal
if(mro_meta) { 221 76 297
570 #if (PERL_REVISION == 5) && (PERL_VERSION >= 12)
571 221 write_svptr(fh, (SV*)mro_meta->mro_linear_all);
572 221 write_svptr(fh, mro_meta->mro_linear_current);
573 #else
574 write_svptr(fh, NULL);
575 write_svptr(fh, NULL);
576 #endif
577 221 write_svptr(fh, (SV*)mro_meta->mro_nextmethod);
578 #if (PERL_REVISION == 5) && ((PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION > 0))
579 221 write_svptr(fh, (SV*)mro_meta->isa);
580 #else
581 write_svptr(fh, NULL);
582 #endif
583 }
584 else {
585 76 write_svptr(fh, NULL);
586 76 write_svptr(fh, NULL);
587 76 write_svptr(fh, NULL);
588 76 write_svptr(fh, NULL);
589 }
590 297 }
591  
592 297 static void write_private_stash(FILE *fh, const HV *stash)
593 {
594 297 struct mro_meta *mro_meta = HvAUX(stash)->xhv_mro_meta;
595  
596 297 ▶ int nkeys = write_hv_header(fh, stash,
Branch: 1 branch
BranchTrueFalseTotal
int nkeys = write_hv_header(fh, stash, 221 76 297
597 sizeof(struct xpvhv_aux) + (mro_meta ? sizeof(struct mro_meta) : 0));
598  
599 // Header
600 // HASH
601 297 write_uint(fh, nkeys);
602  
603 // PTRs
604 297 write_stash_ptrs(fh, stash);
605  
606 // STRs
607 297 ▶ write_str(fh, HvNAME(stash));
Branch: 6 branches
BranchTrueFalseTotal
write_str(fh, HvNAME(stash)); 297 0 297
write_str(fh, HvNAME(stash)); 297 0 297
write_str(fh, HvNAME(stash)); 0 297 297
write_str(fh, HvNAME(stash)); 0 0 0
write_str(fh, HvNAME(stash)); 297 0 297
write_str(fh, HvNAME(stash)); 0 297 297
608  
609 // Body
610 297 ▶ if(HvARRAY(stash))
Branch: 1 branch
BranchTrueFalseTotal
if(HvARRAY(stash)) 297 0 297
611 297 write_hv_body_elems(fh, stash);
612 297 }
613  
614 6303 static void write_private_cv(FILE *fh, const CV *cv)
615 {
616 6303 bool is_xsub = CvISXSUB(cv);
617 6303 ▶ PADLIST *pl = (is_xsub ? NULL : CvPADLIST(cv));
Branch: 1 branch
BranchTrueFalseTotal
PADLIST *pl = (is_xsub ? NULL : CvPADLIST(cv)); 3593 2710 6303
618  
619 /* If the optree contains custom ops, the OP_CLASS() macro will allocate
620 * a mortal SV. We'll need to FREETMPS it to ensure we don't dump it
621 * accidentally
622 */
623 6303 SAVETMPS;
624  
625 // TODO: accurate size information on CVs
626 6303 write_common_sv(fh, (const SV *)cv, sizeof(XPVCV));
627  
628 // Header
629 6303 int line = 0;
630 OP *start;
631 6303 ▶ if(!CvISXSUB(cv) && !CvCONST(cv) && (start = CvSTART(cv))) {
Branch: 3 branches
BranchTrueFalseTotal
if(!CvISXSUB(cv) && !CvCONST(cv) && (start = CvSTART(cv))) { 3593 2710 6303
if(!CvISXSUB(cv) && !CvCONST(cv) && (start = CvSTART(cv))) { 3588 5 3593
if(!CvISXSUB(cv) && !CvCONST(cv) && (start = CvSTART(cv))) { 3364 224 3588
632 3364 ▶ if(start->op_type == OP_NEXTSTATE)
Branch: 1 branch
BranchTrueFalseTotal
if(start->op_type == OP_NEXTSTATE) 3191 173 3364
633 3191 line = CopLINE((COP*)start);
634 }
635 6303 write_uint(fh, line);
636 6303 write_u8(fh, (CvCLONE(cv) ? 0x01 : 0) |
637 12606 ▶ (CvCLONED(cv) ? 0x02 : 0) |
Branch: 2 branches
BranchTrueFalseTotal
(CvCLONED(cv) ? 0x02 : 0) | 355 5948 6303
(CvCLONED(cv) ? 0x02 : 0) | 2710 3593 6303
638 6303 (is_xsub ? 0x04 : 0) |
639 12606 ▶ (CvWEAKOUTSIDE(cv) ? 0x08 : 0) |
Branch: 1 branch
BranchTrueFalseTotal
(CvWEAKOUTSIDE(cv) ? 0x08 : 0) | 163 6140 6303
640 #if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
641 6303 ▶ (CvCVGV_RC(cv) ? 0x10 : 0) |
Branch: 1 branch
BranchTrueFalseTotal
(CvCVGV_RC(cv) ? 0x10 : 0) | 699 5604 6303
642 #else
643 /* Prior to 5.14, CvANON() was used to indicate this */
644 (CvANON(cv) ? 0x10 : 0) |
645 #endif
646 #if (PERL_REVISION == 5) && (PERL_VERSION >= 22)
647 6303 ▶ (CvLEXICAL(cv) ? 0x20 : 0) |
Branch: 1 branch
BranchTrueFalseTotal
(CvLEXICAL(cv) ? 0x20 : 0) | 6 6297 6303
648 #endif
649 0);
650 6303 ▶ if(!is_xsub && !CvCONST(cv))
Branch: 2 branches
BranchTrueFalseTotal
if(!is_xsub && !CvCONST(cv)) 3593 2710 6303
if(!is_xsub && !CvCONST(cv)) 3588 5 3593
651 3588 write_ptr(fh, CvROOT(cv));
652 else
653 2715 write_ptr(fh, NULL);
654  
655 6303 write_u32(fh, CvDEPTH(cv));
656 // CvNAME_HEK doesn't take a const CV *, but we know it won't modify
657 #ifdef CvNAME_HEK
658 write_ptr(fh, CvNAME_HEK((CV *)cv));
659 #else
660 6303 write_ptr(fh, NULL);
661 #endif
662  
663 // PTRs
664 6303 write_svptr(fh, (SV*)CvSTASH(cv));
665 #if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
666 6303 ▶ if(CvNAMED(cv))
Branch: 1 branch
BranchTrueFalseTotal
if(CvNAMED(cv)) 6 6297 6303
667 6 write_svptr(fh, NULL);
668 else
669 #endif
670 6297 write_svptr(fh, (SV*)CvGV(cv));
671 6303 write_svptr(fh, (SV*)CvOUTSIDE(cv));
672 #if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
673 /* Padlists are no longer heap-allocated on 5.20+ */
674 6303 write_svptr(fh, NULL);
675 #else
676 write_svptr(fh, (SV*)(pl));
677 #endif
678 6303 ▶ if(CvCONST(cv))
Branch: 1 branch
BranchTrueFalseTotal
if(CvCONST(cv)) 1936 4367 6303
679 1936 write_svptr(fh, (SV*)CvXSUBANY(cv).any_ptr);
680 else
681 4367 write_svptr(fh, NULL);
682  
683 // STRs
684 6303 ▶ if(CvFILE(cv))
Branch: 1 branch
BranchTrueFalseTotal
if(CvFILE(cv)) 6135 168 6303
685 6135 write_str(fh, CvFILE(cv));
686 else
687 168 write_str(fh, "");
688  
689 #if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
690 6303 ▶ if(CvNAMED(cv))
Branch: 1 branch
BranchTrueFalseTotal
if(CvNAMED(cv)) 6 6297 6303
691 6 write_str(fh, HEK_KEY(CvNAME_HEK((CV*)cv)));
692 else
693 #endif
694 6297 write_str(fh, NULL);
695  
696 // Body
697 6303 ▶ if(cv == PL_main_cv && PL_main_root)
Branch: 2 branches
BranchTrueFalseTotal
if(cv == PL_main_cv && PL_main_root) 1 6302 6303
if(cv == PL_main_cv && PL_main_root) 1 0 1
698 /* The PL_main_cv does not have a CvROOT(); instead that is found in
699 * PL_main_root
700 */
701 1 dump_optree(fh, cv, PL_main_root);
702 6302 ▶ else if(!is_xsub && !CvCONST(cv) && CvROOT(cv))
Branch: 3 branches
BranchTrueFalseTotal
else if(!is_xsub && !CvCONST(cv) && CvROOT(cv)) 3592 2710 6302
else if(!is_xsub && !CvCONST(cv) && CvROOT(cv)) 3587 5 3592
else if(!is_xsub && !CvCONST(cv) && CvROOT(cv)) 3199 388 3587
703 3199 dump_optree(fh, cv, CvROOT(cv));
704  
705 #if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
706 6303 ▶ if(pl) {
Branch: 1 branch
BranchTrueFalseTotal
if(pl) { 3369 2934 6303
707 3369 PADNAME **names = PadlistNAMESARRAY(pl);
708 3369 PAD **pads = PadlistARRAY(pl);
709 int depth, i;
710  
711 3369 write_u8(fh, PMAT_CODEx_PADNAMES);
712 # if (PERL_VERSION > 20)
713 3369 write_svptr(fh, NULL);
714 {
715 3369 PADNAME **padnames = PadnamelistARRAY(PadlistNAMES(pl));
716 3369 int padix_max = PadnamelistMAX(PadlistNAMES(pl));
717  
718 int padix;
719 34117 ▶ for(padix = 1; padix <= padix_max; padix++) {
Branch: 1 branch
BranchTrueFalseTotal
for(padix = 1; padix <= padix_max; padix++) { 30748 3369 34117
720 30748 PADNAME *pn = padnames[padix];
721 30748 ▶ if(!pn)
Branch: 1 branch
BranchTrueFalseTotal
if(!pn) 1093 29655 30748
722 1093 continue;
723  
724 29655 write_u8(fh, PMAT_CODEx_PADNAME);
725 29655 write_uint(fh, padix);
726 29655 write_str(fh, PadnamePV(pn));
727 29655 write_svptr(fh, (SV*)PadnameOURSTASH(pn));
728  
729 29655 ▶ if(PadnameFLAGS(pn)) {
Branch: 1 branch
BranchTrueFalseTotal
if(PadnameFLAGS(pn)) { 5266 24389 29655
730 5266 uint8_t flags = 0;
731  
732 5266 ▶ if(PadnameOUTER(pn)) flags |= 0x01;
Branch: 1 branch
BranchTrueFalseTotal
if(PadnameOUTER(pn)) flags |= 0x01; 1313 3953 5266
733 5266 ▶ if(PadnameIsSTATE(pn)) flags |= 0x02;
Branch: 1 branch
BranchTrueFalseTotal
if(PadnameIsSTATE(pn)) flags |= 0x02; 142 5124 5266
734 5266 ▶ if(PadnameLVALUE(pn)) flags |= 0x04;
Branch: 1 branch
BranchTrueFalseTotal
if(PadnameLVALUE(pn)) flags |= 0x04; 3833 1433 5266
735 5266 ▶ if(PadnameFLAGS(pn) & PADNAMEt_TYPED) flags |= 0x08;
Branch: 1 branch
BranchTrueFalseTotal
if(PadnameFLAGS(pn) & PADNAMEt_TYPED) flags |= 0x08; 3 5263 5266
736 5266 ▶ if(PadnameFLAGS(pn) & PADNAMEt_OUR) flags |= 0x10;
Branch: 1 branch
BranchTrueFalseTotal
if(PadnameFLAGS(pn) & PADNAMEt_OUR) flags |= 0x10; 609 4657 5266
737  
738 5266 ▶ if(flags) {
Branch: 1 branch
BranchTrueFalseTotal
if(flags) { 5266 0 5266
739 5266 write_u8(fh, PMAT_CODEx_PADNAME_FLAGS);
740 5266 write_uint(fh, padix);
741 5266 write_u8(fh, flags);
742 }
743  
744 #ifdef HAVE_FEATURE_CLASS
745 5266 ▶ if(PadnameIsFIELD(pn)) {
Branch: 1 branch
BranchTrueFalseTotal
if(PadnameIsFIELD(pn)) { 0 5266 5266
746 0 write_u8(fh, PMAT_CODEx_PADNAME_FIELD);
747 0 write_uint(fh, padix);
748 0 write_uint(fh, PadnameFIELDINFO(pn)->fieldix);
749 0 write_svptr(fh, (SV *)PadnameFIELDINFO(pn)->fieldstash);
750 }
751 #endif
752 }
753 }
754 }
755 # else
756 write_svptr(fh, (SV*)PadlistNAMES(pl));
757 # endif
758  
759 6770 ▶ for(depth = 1; depth <= PadlistMAX(pl); depth++) {
Branch: 1 branch
BranchTrueFalseTotal
for(depth = 1; depth <= PadlistMAX(pl); depth++) { 3401 3369 6770
760 3401 PAD *pad = pads[depth];
761  
762 3401 write_u8(fh, PMAT_CODEx_PAD);
763 3401 write_uint(fh, depth);
764 3401 write_svptr(fh, (SV*)pad);
765 }
766 }
767 #endif
768  
769 6303 write_u8(fh, 0);
770  
771 6303 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 6303 6303
772 6303 }
773  
774 13 static void write_private_io(FILE *fh, const IO *io)
775 {
776 13 write_common_sv(fh, (const SV *)io, sizeof(XPVIO));
777  
778 13 write_uint(fh, PerlIO_fileno(IoIFP(io)));
779 13 write_uint(fh, PerlIO_fileno(IoOFP(io)));
780  
781 // PTRs
782 13 write_svptr(fh, (SV*)IoTOP_GV(io));
783 13 write_svptr(fh, (SV*)IoFMT_GV(io));
784 13 write_svptr(fh, (SV*)IoBOTTOM_GV(io));
785 13 }
786  
787 0 static void write_private_lv(FILE *fh, const SV *sv)
788 {
789 0 write_common_sv(fh, sv, sizeof(XPVLV));
790  
791 // Header
792 0 write_u8(fh, LvTYPE(sv));
793 0 write_uint(fh, LvTARGOFF(sv));
794 0 write_uint(fh, LvTARGLEN(sv));
795  
796 // PTRs
797 0 write_svptr(fh, LvTARG(sv));
798 0 }
799  
800 #ifdef HAVE_FEATURE_CLASS
801 0 static void write_private_obj(FILE *fh, const SV *obj)
802 {
803 0 int nfields = ObjectMAXFIELD(obj) + 1;
804  
805 0 write_common_sv(fh, obj, sizeof(XPVOBJ));
806  
807 // Header
808 0 write_uint(fh, nfields);
809  
810 0 SV **fields = ObjectFIELDS(obj);
811 int i;
812 0 ▶ for(i = 0; i < nfields; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < nfields; i++) 0 0 0
813 0 write_svptr(fh, fields[i]);
814 0 }
815  
816 0 static void write_private_class(FILE *fh, const HV *cls)
817 {
818 0 struct mro_meta *mro_meta = HvAUX(cls)->xhv_mro_meta;
819  
820 0 ▶ int nkeys = write_hv_header(fh, cls,
Branch: 1 branch
BranchTrueFalseTotal
int nkeys = write_hv_header(fh, cls, 0 0 0
821 sizeof(struct xpvhv_aux) + (mro_meta ? sizeof(struct mro_meta) : 0));
822  
823 // Header
824 // HASH
825 0 write_uint(fh, nkeys);
826  
827 // PTRs
828 0 write_stash_ptrs(fh, cls);
829 0 write_ptr(fh, HvAUX(cls)->xhv_class_adjust_blocks);
830  
831 // STRs
832 0 ▶ write_str(fh, HvNAME(cls));
Branch: 6 branches
BranchTrueFalseTotal
write_str(fh, HvNAME(cls)); 0 0 0
write_str(fh, HvNAME(cls)); 0 0 0
write_str(fh, HvNAME(cls)); 0 0 0
write_str(fh, HvNAME(cls)); 0 0 0
write_str(fh, HvNAME(cls)); 0 0 0
write_str(fh, HvNAME(cls)); 0 0 0
833  
834 // Body
835 0 ▶ if(HvARRAY(cls))
Branch: 1 branch
BranchTrueFalseTotal
if(HvARRAY(cls)) 0 0 0
836 0 write_hv_body_elems(fh, cls);
837  
838 {
839 0 PADNAMELIST *fields = HvAUX(cls)->xhv_class_fields;
840  
841 0 ▶ int nfields = fields ? PadnamelistMAX(fields)+1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
int nfields = fields ? PadnamelistMAX(fields)+1 : 0; 0 0 0
842 0 ▶ for(int i = 0; i < nfields; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(int i = 0; i < nfields; i++) { 0 0 0
843 0 PADNAME *pn = PadnamelistARRAY(fields)[i];
844  
845 0 write_u8(fh, PMAT_CLASSx_FIELD);
846 0 write_uint(fh, PadnameFIELDINFO(pn)->fieldix);
847 0 write_str(fh, PadnamePV(pn));
848 }
849 }
850  
851 0 write_u8(fh, 0);
852 0 }
853 #endif
854  
855 0 static void write_annotations_from_stack(FILE *fh, int n)
856 {
857 0 dSP;
858 0 SV **p = SP - n + 1;
859  
860 0 ▶ while(p <= SP) {
Branch: 1 branch
BranchTrueFalseTotal
while(p <= SP) { 0 0 0
861 0 unsigned char type = SvIV(p[0]);
862 0 ▶ switch(type) {
Branch: 1 branch
BranchTrueFalseTotal
switch(type) { 0 0 0
863 0 case PMAT_SVxSVSVnote:
864 0 write_u8(fh, type);
865 0 write_svptr(fh, p[1]); /* target */
866 0 write_svptr(fh, p[2]); /* value */
867 0 write_strn(fh, SvPV_nolen(p[3]), SvCUR(p[3])); /* annotation */
868 0 p += 4;
869 0 break;
870 0 default:
871 0 fprintf(stderr, "ARG: Unsure how to handle PMAT_SVn annotation type %02x\n", type);
872 0 p = SP + 1;
873 }
874 }
875 0 }
876  
877 260 static void run_package_helpers(DMDContext *ctx, const SV *sv, SV *classname)
878 {
879 260 FILE *fh = ctx->fh;
880 HE *he;
881  
882 260 DMD_Helper *helper = NULL;
883 260 ▶ if((he = hv_fetch_ent(helper_per_package, classname, 0, 0)))
Branch: 1 branch
BranchTrueFalseTotal
if((he = hv_fetch_ent(helper_per_package, classname, 0, 0))) 0 260 260
884 0 helper = (DMD_Helper *)SvUV(HeVAL(he));
885  
886 260 ▶ if(helper) {
Branch: 1 branch
BranchTrueFalseTotal
if(helper) { 0 260 260
887 0 ENTER;
888 0 SAVETMPS;
889  
890 0 int ret = (*helper)(aTHX_ ctx, sv);
891  
892 0 ▶ if(ret > 0)
Branch: 1 branch
BranchTrueFalseTotal
if(ret > 0) 0 0 0
893 0 write_annotations_from_stack(fh, ret);
894  
895 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
896 0 LEAVE;
897 }
898 260 }
899  
900 6310 static void write_magic(DMDContext *ctx, const SV *sv, MAGIC *mg)
901 {
902 6310 FILE *fh = ctx->fh;
903  
904 #ifdef HAVE_MAGICv2
905 if(MgIsV2(mg)) {
906 const struct MagicFunctions *funcs = MgFUNCS(mg);
907  
908 write_u8(fh, PMAT_SVxMAGICv2);
909 write_svptr(fh, sv);
910  
911 write_u8(fh, (U8)funcs->shape);
912 write_u8(fh, 0 |
913 (MgWEAK_AUXSV(mg) ? 0x01 : 0) |
914 (MgHasKEYIV(mg) ? 0x02 : 0));
915 write_u16(fh, MgPRIV(mg));
916 write_uint(fh, MgPTRLEN(mg));
917 if(MgHasKEYIV(mg))
918 write_uint(fh, MgKEYIV(mg));
919 else
920 write_uint(fh, 0);
921  
922 write_ptr(fh, funcs);
923 write_svptr(fh, MgAUXSV(mg));
924 write_ptr(fh, MgPTR(mg));
925 if(MgHasKEYSV(mg))
926 write_svptr(fh, MgKEYSV(mg));
927 else
928 write_svptr(fh, NULL);
929 write_ptr(fh, funcs->user_size > 0 ? MgUSERSTRUCT(mg, void *) : NULL);
930  
931 if(funcs->user_size > 0) {
932 SV *key = make_tmp_iv((IV)funcs);
933  
934 DMD_MagicHelper *helper = NULL;
935 HE *he = hv_fetch_ent(helper_per_magic, key, 0, 0);
936 if(he)
937 helper = (DMD_MagicHelper *)SvUV(HeVAL(he));
938  
939 if(helper) {
940 ENTER;
941 SAVETMPS;
942  
943 int ret = (helper)(aTHX_ ctx, sv, mg);
944  
945 if(ret > 0)
946 write_annotations_from_stack(fh, ret);
947  
948 FREETMPS;
949 LEAVE;
950 }
951 }
952 }
953 else
954 #endif
955 {
956 6310 write_u8(fh, PMAT_SVxMAGIC);
957 6310 write_svptr(fh, sv);
958 6310 write_u8(fh, mg->mg_type);
959 6310 write_u8(fh, (mg->mg_flags & MGf_REFCOUNTED ? 0x01 : 0));
960 6310 write_svptr(fh, mg->mg_obj);
961 6310 ▶ if(mg->mg_len == HEf_SVKEY)
Branch: 1 branch
BranchTrueFalseTotal
if(mg->mg_len == HEf_SVKEY) 44 6266 6310
962 44 write_svptr(fh, (SV*)mg->mg_ptr);
963 else
964 6266 write_svptr(fh, NULL);
965 6310 write_svptr(fh, (SV *)mg->mg_virtual); /* Not really an SV */
966  
967 6310 ▶ if(mg->mg_type == PERL_MAGIC_ext &&
Branch: 1 branch
BranchTrueFalseTotal
if(mg->mg_type == PERL_MAGIC_ext && 1 6309 6310
968 1 ▶ mg->mg_ptr && mg->mg_len != HEf_SVKEY) {
Branch: 2 branches
BranchTrueFalseTotal
mg->mg_ptr && mg->mg_len != HEf_SVKEY) { 0 1 1
mg->mg_ptr && mg->mg_len != HEf_SVKEY) { 0 0 0
969 0 SV *key = make_tmp_iv((IV)mg->mg_virtual);
970 HE *he;
971  
972 0 DMD_MagicHelper *helper = NULL;
973 0 he = hv_fetch_ent(helper_per_magic, key, 0, 0);
974 0 ▶ if(he)
Branch: 1 branch
BranchTrueFalseTotal
if(he) 0 0 0
975 0 helper = (DMD_MagicHelper *)SvUV(HeVAL(he));
976  
977 0 ▶ if(helper) {
Branch: 1 branch
BranchTrueFalseTotal
if(helper) { 0 0 0
978 0 ENTER;
979 0 SAVETMPS;
980  
981 0 int ret = (helper)(aTHX_ ctx, sv, mg);
982  
983 0 ▶ if(ret > 0)
Branch: 1 branch
BranchTrueFalseTotal
if(ret > 0) 0 0 0
984 0 write_annotations_from_stack(fh, ret);
985  
986 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
987 0 LEAVE;
988 }
989 }
990 }
991 6310 }
992  
993 89412 static void write_sv(DMDContext *ctx, const SV *sv)
994 {
995 89412 FILE *fh = ctx->fh;
996 89412 unsigned char type = -1;
997 89412 switch(SvTYPE(sv)) {
998 28094 case SVt_NULL:
999 28094 type = PMAT_SVtUNDEF; break;
1000 36571 case SVt_IV:
1001 case SVt_NV:
1002 case SVt_PV:
1003 case SVt_PVIV:
1004 case SVt_PVNV:
1005 case SVt_PVMG:
1006 36571 ▶ type = SvROK(sv) ? PMAT_SVtREF : PMAT_SVtSCALAR; break;
Branch: 1 branch
BranchTrueFalseTotal
type = SvROK(sv) ? PMAT_SVtREF : PMAT_SVtSCALAR; break; 3151 33420 36571
1007 #if (PERL_REVISION == 5) && (PERL_VERSION < 12)
1008 case SVt_RV: type = PMAT_SVtREF; break;
1009 #endif
1010 #if (PERL_REVISION == 5) && (PERL_VERSION >= 19)
1011 102 case SVt_INVLIST: type = PMAT_SVtINVLIST; break;
1012 #endif
1013 #if (PERL_REVISION == 5) && (PERL_VERSION >= 12)
1014 904 case SVt_REGEXP: type = PMAT_SVtREGEXP; break;
1015 #endif
1016 7533 case SVt_PVGV: type = PMAT_SVtGLOB; break;
1017 0 case SVt_PVLV: type = PMAT_SVtLVALUE; break;
1018 8149 case SVt_PVAV: type = PMAT_SVtARRAY; break;
1019 // HVs with names we call STASHes
1020 1743 case SVt_PVHV:
1021 #ifdef HAVE_FEATURE_CLASS
1022 1743 ▶ if(HvNAME(sv) && HvSTASH_IS_CLASS(sv))
Branch: 9 branches
BranchTrueFalseTotal
if(HvNAME(sv) && HvSTASH_IS_CLASS(sv)) 443 1300 1743
if(HvNAME(sv) && HvSTASH_IS_CLASS(sv)) 297 146 443
if(HvNAME(sv) && HvSTASH_IS_CLASS(sv)) 0 297 297
if(HvNAME(sv) && HvSTASH_IS_CLASS(sv)) 0 0 0
if(HvNAME(sv) && HvSTASH_IS_CLASS(sv)) 297 0 297
if(HvNAME(sv) && HvSTASH_IS_CLASS(sv)) 0 297 297
if(HvNAME(sv) && HvSTASH_IS_CLASS(sv)) 297 0 297
if(HvNAME(sv) && HvSTASH_IS_CLASS(sv)) 297 0 297
if(HvNAME(sv) && HvSTASH_IS_CLASS(sv)) 0 297 297
1023 0 type = PMAT_SVtCLASS;
1024 else
1025 #endif
1026 1743 ▶ if(HvNAME(sv))
Branch: 7 branches
BranchTrueFalseTotal
if(HvNAME(sv)) 443 1300 1743
if(HvNAME(sv)) 297 146 443
if(HvNAME(sv)) 0 297 297
if(HvNAME(sv)) 0 0 0
if(HvNAME(sv)) 297 0 297
if(HvNAME(sv)) 0 297 297
if(HvNAME(sv)) 297 0 297
1027 297 type = PMAT_SVtSTASH;
1028 else
1029 1446 type = PMAT_SVtHASH;
1030 1743 break;
1031 6303 case SVt_PVCV: type = PMAT_SVtCODE; break;
1032 0 case SVt_PVFM: type = PMAT_SVtFORMAT; break;
1033 13 case SVt_PVIO: type = PMAT_SVtIO; break;
1034 #ifdef HAVE_FEATURE_CLASS
1035 0 case SVt_PVOBJ: type = PMAT_SVtOBJ; break;
1036 #endif
1037 0 default:
1038 0 fprintf(stderr, "dumpsv %p has unknown SvTYPE %d\n", sv, SvTYPE(sv));
1039 0 break;
1040 }
1041  
1042 89412 ▶ if(type == PMAT_SVtSCALAR && !SvOK(sv))
Branch: 2 branches
BranchTrueFalseTotal
if(type == PMAT_SVtSCALAR && !SvOK(sv)) 33420 55992 89412
if(type == PMAT_SVtSCALAR && !SvOK(sv)) 980 32440 33420
1043 980 type = PMAT_SVtUNDEF;
1044 #if (PERL_REVISION == 5) && (PERL_VERSION >= 35)
1045 89412 ▶ if(type == PMAT_SVtSCALAR && SvIsBOOL(sv))
Branch: 2 branches
BranchTrueFalseTotal
if(type == PMAT_SVtSCALAR && SvIsBOOL(sv)) 32440 56972 89412
if(type == PMAT_SVtSCALAR && SvIsBOOL(sv)) 18 32422 32440
1046 /* SvTRUE() et al. might mutate; but it's OK we know this is one of the bools */
1047 18 ▶ type = (SvIVX(sv)) ? PMAT_SVtYES : PMAT_SVtNO;
Branch: 1 branch
BranchTrueFalseTotal
type = (SvIVX(sv)) ? PMAT_SVtYES : PMAT_SVtNO; 9 9 18
1048 #endif
1049  
1050 89412 write_u8(fh, type);
1051  
1052 89412 switch(type) {
1053 7533 case PMAT_SVtGLOB: write_private_gv (fh, (GV*)sv); break;
1054 32422 case PMAT_SVtSCALAR: write_private_sv (fh, sv); break;
1055 3151 case PMAT_SVtREF: write_private_rv (fh, sv); break;
1056 8149 case PMAT_SVtARRAY: write_private_av (fh, (AV*)sv); break;
1057 1446 case PMAT_SVtHASH: write_private_hv (fh, (HV*)sv); break;
1058 297 case PMAT_SVtSTASH: write_private_stash(fh, (HV*)sv); break;
1059 6303 case PMAT_SVtCODE: write_private_cv (fh, (CV*)sv); break;
1060 13 case PMAT_SVtIO: write_private_io (fh, (IO*)sv); break;
1061 0 case PMAT_SVtLVALUE: write_private_lv (fh, sv); break;
1062 #ifdef HAVE_FEATURE_CLASS
1063 0 case PMAT_SVtOBJ: write_private_obj(fh, sv); break;
1064 0 case PMAT_SVtCLASS: write_private_class(fh, (HV*)sv); break;
1065 #endif
1066  
1067 #if (PERL_REVISION == 5) && (PERL_VERSION >= 12)
1068 904 case PMAT_SVtREGEXP: write_common_sv(fh, sv, sizeof(regexp)); break;
1069 #endif
1070 0 case PMAT_SVtFORMAT: write_common_sv(fh, sv, sizeof(XPVFM)); break;
1071 102 case PMAT_SVtINVLIST: write_common_sv(fh, sv, sizeof(XPV) + SvLEN(sv)); break;
1072 29074 case PMAT_SVtUNDEF: write_common_sv(fh, sv, 0); break;
1073 9 case PMAT_SVtYES: write_common_sv(fh, sv, 0); break;
1074 9 case PMAT_SVtNO: write_common_sv(fh, sv, 0); break;
1075 }
1076  
1077 89412 ▶ if(SvMAGICAL(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvMAGICAL(sv)) { 6310 83102 89412
1078 MAGIC *mg;
1079 12620 ▶ for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
Branch: 1 branch
BranchTrueFalseTotal
for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) 6310 6310 12620
1080 6310 write_magic(ctx, sv, mg);
1081 }
1082  
1083 89412 ▶ if(SvOBJECT(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvOBJECT(sv)) { 170 89242 89412
1084 170 AV *linearized_mro = mro_get_linear_isa(SvSTASH(sv));
1085 430 ▶ for(SSize_t i = 0; i <= AvFILL(linearized_mro); i++)
Branch: 1 branch
BranchTrueFalseTotal
for(SSize_t i = 0; i <= AvFILL(linearized_mro); i++) 260 170 430
1086 260 run_package_helpers(ctx, sv, AvARRAY(linearized_mro)[i]);
1087 }
1088  
1089 #ifdef DEBUG_LEAKING_SCALARS
1090 {
1091 write_u8(fh, PMAT_SVxDEBUGREPORT);
1092 write_svptr(fh, sv);
1093 write_uint(fh, sv->sv_debug_serial);
1094 write_uint(fh, sv->sv_debug_line);
1095 /* TODO: this is going to make the file a lot larger, due to nonshared
1096 * strings. Consider if there's a way we can share these somehow
1097 */
1098 write_str(fh, sv->sv_debug_file);
1099 }
1100 #endif
1101 89412 }
1102  
1103 typedef struct
1104 {
1105 const char *name;
1106 enum {
1107 DMD_FIELD_PTR,
1108 DMD_FIELD_BOOL,
1109 DMD_FIELD_U8,
1110 DMD_FIELD_U32,
1111 DMD_FIELD_UINT,
1112 } type;
1113 struct {
1114 void *ptr;
1115 bool b;
1116 long n;
1117 };
1118 } DMDNamedField;
1119  
1120 typedef struct
1121 {
1122 const char *name;
1123 const char *str;
1124 size_t len;
1125 } DMDNamedString;
1126  
1127 0 static void writestruct(pTHX_ DMDContext *ctx, const char *name, void *addr, size_t size,
1128 size_t nfields, const DMDNamedField fields[])
1129 {
1130 0 FILE *fh = ctx->fh;
1131  
1132 0 ▶ if(!ctx->structdefs)
Branch: 1 branch
BranchTrueFalseTotal
if(!ctx->structdefs) 0 0 0
1133 0 ctx->structdefs = newHV();
1134  
1135 0 SV *idsv = *hv_fetch(ctx->structdefs, name, strlen(name), 1);
1136 0 ▶ if(!SvOK(idsv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(idsv)) { 0 0 0
1137 0 int structid = ctx->next_structid;
1138 0 ctx->next_structid++;
1139  
1140 0 sv_setiv(idsv, structid);
1141  
1142 0 write_u8(fh, PMAT_SVtMETA_STRUCT);
1143 0 write_uint(fh, structid);
1144 0 write_uint(fh, nfields);
1145 0 write_str(fh, name);
1146 0 ▶ for(size_t i = 0; i < nfields; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(size_t i = 0; i < nfields; i++) { 0 0 0
1147 0 write_str(fh, fields[i].name);
1148 0 write_u8(fh, fields[i].type);
1149 }
1150 }
1151  
1152 0 write_u8(fh, PMAT_SVtSTRUCT);
1153 /* Almost the same layout as write_common_sv() */
1154 // Header for common
1155 0 write_svptr(fh, addr);
1156 0 write_u32(fh, -1);
1157 0 write_uint(fh, size);
1158 // PTRs for common
1159 0 write_svptr(fh, NUM2PTR(SV *, SvIV(idsv))); /* abuse the stash pointer to store the descriptor ID */
1160  
1161 // Body
1162 0 ▶ for(size_t i = 0; i < nfields; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(size_t i = 0; i < nfields; i++) 0 0 0
1163 0 switch(fields[i].type) {
1164 0 case DMD_FIELD_PTR:
1165 0 write_ptr(fh, fields[i].ptr);
1166 0 break;
1167  
1168 0 case DMD_FIELD_BOOL:
1169 0 write_u8(fh, fields[i].b);
1170 0 break;
1171  
1172 0 case DMD_FIELD_U8:
1173 0 write_u8(fh, fields[i].n);
1174 0 break;
1175  
1176 0 case DMD_FIELD_U32:
1177 0 write_u32(fh, fields[i].n);
1178 0 break;
1179  
1180 0 case DMD_FIELD_UINT:
1181 0 write_uint(fh, fields[i].n);
1182 0 break;
1183 }
1184 0 }
1185  
1186 #if (PERL_REVISION == 5) && (PERL_VERSION < 14)
1187 /*
1188 * This won't be very good, but good enough for our needs
1189 */
1190 static I32 dopoptosub_at(const PERL_CONTEXT *cxstk, I32 startingblock)
1191 {
1192 dVAR;
1193 I32 i;
1194  
1195 for(i = startingblock; i >= 0; i--) {
1196 const PERL_CONTEXT * const cx = &cxstk[i];
1197 switch (CxTYPE(cx)) {
1198 case CXt_EVAL:
1199 case CXt_SUB:
1200 case CXt_FORMAT:
1201 return i;
1202 default:
1203 continue;
1204 }
1205 }
1206 return i;
1207 }
1208  
1209 static const PERL_CONTEXT *caller_cx(int count, void *ignore)
1210 {
1211 I32 cxix = dopoptosub_at(cxstack, cxstack_ix);
1212 const PERL_CONTEXT *ccstack = cxstack;
1213 const PERL_SI *top_si = PL_curstackinfo;
1214  
1215 while(1) {
1216 while(cxix < 0 && top_si->si_type != PERLSI_MAIN) {
1217 top_si = top_si->si_prev;
1218  
1219 ccstack = top_si->si_cxstack;
1220 cxix = dopoptosub_at(ccstack, top_si->si_cxix);
1221 }
1222  
1223 if(cxix < 0)
1224 return NULL;
1225  
1226 if (PL_DBsub && GvCV(PL_DBsub) && cxix >= 0 && ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
1227 count++;
1228  
1229 if(!count--)
1230 break;
1231  
1232 cxix = dopoptosub_at(ccstack, cxix - 1);
1233 }
1234  
1235 const PERL_CONTEXT *cx = &ccstack[cxix];
1236  
1237 if(CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
1238 const I32 dbcxix = dopoptosub_at(ccstack, cxix - 1);
1239 if (PL_DBsub && GvCV(PL_DBsub) && dbcxix >= 0 && ccstack[dbcxix].blk_sub.cv == GvCV(PL_DBsub))
1240 cx = &ccstack[dbcxix];
1241 }
1242  
1243 return cx;
1244 }
1245 #endif
1246  
1247 1 static void dumpfh(FILE *fh)
1248 {
1249 1 max_string = SvIV(get_sv("Devel::MAT::Dumper::MAX_STRING", GV_ADD));
1250  
1251 1 DMDContext ctx = {
1252 .fh = fh,
1253 .next_structid = 0,
1254 };
1255  
1256 // Header
1257 1 fwrite("PMAT", 4, 1, fh);
1258  
1259 1 int flags = 0;
1260 #if (BYTEORDER == 0x1234) || (BYTEORDER == 0x12345678)
1261 // little-endian
1262 #elif (BYTEORDER == 0x4321) || (BYTEORDER == 0x87654321)
1263 flags |= 0x01; // big-endian
1264 #else
1265 # error "Expected BYTEORDER to be big- or little-endian"
1266 #endif
1267  
1268 #if UVSIZE == 8
1269 1 flags |= 0x02; // 64-bit integers
1270 #elif UVSIZE == 4
1271 #else
1272 # error "Expected UVSIZE to be either 4 or 8"
1273 #endif
1274  
1275 #if PTRSIZE == 8
1276 1 flags |= 0x04; // 64-bit pointers
1277 #elif PTRSIZE == 4
1278 #else
1279 # error "Expected PTRSIZE to be either 4 or 8"
1280 #endif
1281  
1282 #if NVSIZE > 8
1283 flags |= 0x08; // long-double
1284 #endif
1285  
1286 #ifdef USE_ITHREADS
1287 flags |= 0x10; // ithreads
1288 #endif
1289  
1290 1 write_u8(fh, flags);
1291 1 write_u8(fh, 0);
1292 1 write_u8(fh, FORMAT_VERSION_MAJOR);
1293 1 write_u8(fh, FORMAT_VERSION_MINOR);
1294  
1295 1 write_u32(fh, PERL_REVISION<<24 | PERL_VERSION<<16 | PERL_SUBVERSION);
1296  
1297 1 write_u8(fh, sizeof(sv_sizes)/3);
1298 1 fwrite(sv_sizes, sizeof(sv_sizes), 1, fh);
1299  
1300 1 write_u8(fh, sizeof(svx_sizes)/3);
1301 1 fwrite(svx_sizes, sizeof(svx_sizes), 1, fh);
1302  
1303 1 write_u8(fh, sizeof(ctx_sizes)/3);
1304 1 fwrite(ctx_sizes, sizeof(ctx_sizes), 1, fh);
1305  
1306 // Roots
1307 1 write_svptr(fh, &PL_sv_undef);
1308 1 write_svptr(fh, &PL_sv_yes);
1309 1 write_svptr(fh, &PL_sv_no);
1310  
1311 1 struct root { char *name; SV *ptr; } roots[] = {
1312 { "main_cv", (SV*)PL_main_cv },
1313 { "defstash", (SV*)PL_defstash },
1314 { "mainstack", (SV*)PL_mainstack },
1315 { "beginav", (SV*)PL_beginav },
1316 { "checkav", (SV*)PL_checkav },
1317 { "unitcheckav", (SV*)PL_unitcheckav },
1318 { "initav", (SV*)PL_initav },
1319 { "endav", (SV*)PL_endav },
1320 { "strtab", (SV*)PL_strtab },
1321 { "envgv", (SV*)PL_envgv },
1322 { "incgv", (SV*)PL_incgv },
1323 { "statgv", (SV*)PL_statgv },
1324 { "statname", (SV*)PL_statname },
1325 { "tmpsv", (SV*)PL_Sv }, // renamed
1326 { "defgv", (SV*)PL_defgv },
1327 { "argvgv", (SV*)PL_argvgv },
1328 { "argvoutgv", (SV*)PL_argvoutgv },
1329 { "argvout_stack", (SV*)PL_argvout_stack },
1330 { "errgv", (SV*)PL_errgv },
1331 { "fdpid", (SV*)PL_fdpid },
1332 { "preambleav", (SV*)PL_preambleav },
1333 { "modglobalhv", (SV*)PL_modglobal },
1334 #ifdef USE_ITHREADS
1335 { "regex_padav", (SV*)PL_regex_padav },
1336 #endif
1337 { "sortstash", (SV*)PL_sortstash },
1338 { "firstgv", (SV*)PL_firstgv },
1339 { "secondgv", (SV*)PL_secondgv },
1340 { "debstash", (SV*)PL_debstash },
1341 { "stashcache", (SV*)PL_stashcache },
1342 { "isarev", (SV*)PL_isarev },
1343 #if (PERL_REVISION == 5) && ((PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION > 0))
1344 { "registered_mros", (SV*)PL_registered_mros },
1345 #endif
1346 { "rs", (SV*)PL_rs },
1347 { "last_in_gv", (SV*)PL_last_in_gv },
1348 { "defoutgv", (SV*)PL_defoutgv },
1349 { "hintgv", (SV*)PL_hintgv },
1350 { "patchlevel", (SV*)PL_patchlevel },
1351 { "e_script", (SV*)PL_e_script },
1352 { "mess_sv", (SV*)PL_mess_sv },
1353 { "ors_sv", (SV*)PL_ors_sv },
1354 { "encoding", (SV*)PL_encoding },
1355 #if (PERL_REVISION == 5) && (PERL_VERSION >= 12)
1356 { "ofsgv", (SV*)PL_ofsgv },
1357 #endif
1358 #if (PERL_REVISION == 5) && (PERL_VERSION >= 14) && (PERL_VERSION <= 20)
1359 { "apiversion", (SV*)PL_apiversion },
1360 #endif
1361 #if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
1362 { "blockhooks", (SV*)PL_blockhooks },
1363 #endif
1364 #if (PERL_REVISION == 5) && (PERL_VERSION >= 16)
1365 { "custom_ops", (SV*)PL_custom_ops },
1366 { "custom_op_names", (SV*)PL_custom_op_names },
1367 { "custom_op_descs", (SV*)PL_custom_op_descs },
1368 #endif
1369  
1370 // Unicode etc...
1371 { "utf8_mark", (SV*)PL_utf8_mark },
1372 { "utf8_toupper", (SV*)PL_utf8_toupper },
1373 { "utf8_totitle", (SV*)PL_utf8_totitle },
1374 { "utf8_tolower", (SV*)PL_utf8_tolower },
1375 { "utf8_tofold", (SV*)PL_utf8_tofold },
1376 { "utf8_idstart", (SV*)PL_utf8_idstart },
1377 { "utf8_idcont", (SV*)PL_utf8_idcont },
1378 #if (PERL_REVISION == 5) && (PERL_VERSION >= 12) && (PERL_VERSION <= 20)
1379 { "utf8_X_extend", (SV*)PL_utf8_X_extend },
1380 #endif
1381 #if (PERL_REVISION == 5) && (PERL_VERSION >= 14)
1382 { "utf8_xidstart", (SV*)PL_utf8_xidstart },
1383 { "utf8_xidcont", (SV*)PL_utf8_xidcont },
1384 { "utf8_foldclosures", (SV*)PL_utf8_foldclosures },
1385 #if (PERL_REVISION == 5) && ((PERL_VERSION < 29) || (PERL_VERSION == 29 && PERL_SUBVERSION < 7))
1386 { "utf8_foldable", (SV*)PL_utf8_foldable },
1387 #endif
1388 #endif
1389 #if (PERL_REVISION == 5) && (PERL_VERSION >= 16)
1390 { "Latin1", (SV*)PL_Latin1 },
1391 { "AboveLatin1", (SV*)PL_AboveLatin1 },
1392 { "utf8_perl_idstart", (SV*)PL_utf8_perl_idstart },
1393 #endif
1394 #if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
1395 #if (PERL_REVISION == 5) && ((PERL_VERSION < 29) || (PERL_VERSION == 29 && PERL_SUBVERSION < 7))
1396 { "NonL1NonFinalFold", (SV*)PL_NonL1NonFinalFold },
1397 #endif
1398 { "HasMultiCharFold", (SV*)PL_HasMultiCharFold },
1399 # if (PERL_VERSION <= 20)
1400 { "utf8_X_regular_begin", (SV*)PL_utf8_X_regular_begin },
1401 # endif
1402 { "utf8_charname_begin", (SV*)PL_utf8_charname_begin },
1403 { "utf8_charname_continue", (SV*)PL_utf8_charname_continue },
1404 { "utf8_perl_idcont", (SV*)PL_utf8_perl_idcont },
1405 #endif
1406 #if (PERL_REVISION == 5) && ((PERL_VERSION > 19) || (PERL_VERSION == 19 && PERL_SUBVERSION >= 4))
1407 { "UpperLatin1", (SV*)PL_UpperLatin1 },
1408 #endif
1409 };
1410  
1411 1 AV *moreroots = get_av("Devel::MAT::Dumper::MORE_ROOTS", 0);
1412  
1413 1 int nroots = sizeof(roots) / sizeof(roots[0]);
1414 1 ▶ if(moreroots)
Branch: 1 branch
BranchTrueFalseTotal
if(moreroots) 0 1 1
1415 0 nroots += (AvFILL(moreroots)+1) / 2;
1416  
1417 1 write_u32(fh, nroots);
1418  
1419 int i;
1420 62 ▶ for(i = 0; i < sizeof(roots) / sizeof(roots[0]); i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < sizeof(roots) / sizeof(roots[0]); i++) { 61 1 62
1421 61 write_str(fh, roots[i].name);
1422 61 write_svptr(fh, roots[i].ptr);
1423 }
1424 1 ▶ if(moreroots) {
Branch: 1 branch
BranchTrueFalseTotal
if(moreroots) { 0 1 1
1425 0 SV **svp = AvARRAY(moreroots);
1426 0 int max = AvFILL(moreroots);
1427  
1428 0 ▶ for(i = 0; i < max; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < max; i += 2) { 0 0 0
1429 0 write_str(fh, SvPV_nolen(svp[i]));
1430 0 write_svptr(fh, svp[i+1]);
1431 }
1432 }
1433  
1434 // Stack
1435 1 write_uint(fh, PL_stack_sp - PL_stack_base + 1);
1436 SV **sp;
1437 3 ▶ for(sp = PL_stack_base; sp <= PL_stack_sp; sp++)
Branch: 1 branch
BranchTrueFalseTotal
for(sp = PL_stack_base; sp <= PL_stack_sp; sp++) 2 1 3
1438 2 write_svptr(fh, *sp);
1439  
1440 1 bool seen_defstash = false;
1441  
1442 // Heap
1443 SV *arena;
1444 531 ▶ for(arena = PL_sv_arenaroot; arena; arena = (SV *)SvANY(arena)) {
Branch: 1 branch
BranchTrueFalseTotal
for(arena = PL_sv_arenaroot; arena; arena = (SV *)SvANY(arena)) { 530 1 531
1445 530 const SV *arenaend = &arena[SvREFCNT(arena)];
1446  
1447 SV *sv;
1448 90100 ▶ for(sv = arena + 1; sv < arenaend; sv++) {
Branch: 1 branch
BranchTrueFalseTotal
for(sv = arena + 1; sv < arenaend; sv++) { 89570 530 90100
1449 89570 ▶ if(sv == tmpsv)
Branch: 1 branch
BranchTrueFalseTotal
if(sv == tmpsv) 0 89570 89570
1450 0 continue;
1451  
1452 89570 ▶ if(SvTYPE(sv) == 0xff)
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(sv) == 0xff) 158 89412 89570
1453 158 continue;
1454  
1455 89412 write_sv(&ctx, sv);
1456  
1457 89412 ▶ if(sv == (const SV *)PL_defstash)
Branch: 1 branch
BranchTrueFalseTotal
if(sv == (const SV *)PL_defstash) 1 89411 89412
1458 1 seen_defstash = true;
1459 }
1460 }
1461  
1462 // and a few other things that don't actually appear in the arena
1463 1 ▶ if(!seen_defstash)
Branch: 1 branch
BranchTrueFalseTotal
if(!seen_defstash) 0 1 1
1464 0 write_sv(&ctx, (const SV *)PL_defstash);
1465  
1466 // Savestack
1467 #if (PERL_REVISION == 5) && (PERL_VERSION >= 18)
1468 /* The savestack only had a vaguely nicely predicable layout from perl 5.18 onwards
1469 * On earlier perls we'll just not bother. Sorry
1470 * No `local` detection for you
1471 */
1472  
1473 1 int saveix = PL_savestack_ix;
1474 6306 ▶ while(saveix) {
Branch: 1 branch
BranchTrueFalseTotal
while(saveix) { 6305 1 6306
1475 6305 UV uv = PL_savestack[saveix-1].any_uv;
1476 6305 U8 type = (U8)uv & SAVE_MASK;
1477  
1478 /* TODO: this seems fragile - does core perl not export a nice way to
1479 * do it?
1480 */
1481 char count;
1482 6305 ▶ if(type <= SAVEt_ARG0_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if(type <= SAVEt_ARG0_MAX) 1 6304 6305
1483 1 count = 0;
1484 6304 ▶ else if(type <= SAVEt_ARG1_MAX)
Branch: 1 branch
BranchTrueFalseTotal
else if(type <= SAVEt_ARG1_MAX) 6304 0 6304
1485 6304 count = 1;
1486 0 ▶ else if(type <= SAVEt_ARG2_MAX)
Branch: 1 branch
BranchTrueFalseTotal
else if(type <= SAVEt_ARG2_MAX) 0 0 0
1487 0 count = 2;
1488 0 ▶ else if(type <= SAVEt_MAX)
Branch: 1 branch
BranchTrueFalseTotal
else if(type <= SAVEt_MAX) 0 0 0
1489 0 count = 3;
1490 else
1491 /* Unrecognised type; just abort here */
1492 0 break;
1493  
1494 6305 saveix -= (count + 1);
1495 6305 ▶ ANY *a0 = count > 0 ? &PL_savestack[saveix ] : NULL,
Branch: 1 branch
BranchTrueFalseTotal
ANY *a0 = count > 0 ? &PL_savestack[saveix ] : NULL, 6304 1 6305
1496 6305 ▶ *a1 = count > 1 ? &PL_savestack[saveix+1] : NULL,
Branch: 1 branch
BranchTrueFalseTotal
*a1 = count > 1 ? &PL_savestack[saveix+1] : NULL, 0 6305 6305
1497 6305 ▶ *a2 = count > 2 ? &PL_savestack[saveix+2] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
*a2 = count > 2 ? &PL_savestack[saveix+2] : NULL; 0 6305 6305
1498  
1499 6305 switch(type) {
1500 /* Most savestack entries aren't very interesting to Devel::MAT, but
1501 * there's a few we find useful. A lot of them don't add any linkages
1502 * between SVs, so we can ignore the majority of them
1503 */
1504 6305 case SAVEt_CLEARSV:
1505 case SAVEt_CLEARPADRANGE:
1506  
1507 #if (PERL_REVISION == 5) && (PERL_VERSION >= 24)
1508 case SAVEt_TMPSFLOOR:
1509 #endif
1510 case SAVEt_BOOL:
1511 case SAVEt_COMPPAD:
1512 case SAVEt_FREEOP:
1513 case SAVEt_FREEPV:
1514 case SAVEt_FREESV:
1515 case SAVEt_I16:
1516 case SAVEt_I32_SMALL:
1517 case SAVEt_I8:
1518 case SAVEt_INT_SMALL:
1519 case SAVEt_MORTALIZESV:
1520 case SAVEt_OP:
1521 case SAVEt_PARSER:
1522 case SAVEt_SHARED_PVREF:
1523 case SAVEt_SPTR:
1524 #if (PERL_REVISION == 5) && (PERL_VERSION >= 38)
1525 case SAVEt_FREERCPV:
1526 #endif
1527  
1528 case SAVEt_DESTRUCTOR:
1529 case SAVEt_DESTRUCTOR_X:
1530 case SAVEt_GP:
1531 case SAVEt_I32:
1532 case SAVEt_INT:
1533 case SAVEt_IV:
1534 #if (PERL_REVISION == 5) && (PERL_VERSION <= 40)
1535 /* was removed in 5.41.3 */
1536 case SAVEt_LONG:
1537 #endif
1538 #if (PERL_REVISION == 5) && (PERL_VERSION >= 20)
1539 case SAVEt_STRLEN:
1540 #endif
1541 #if (PERL_REVISION == 5) && (PERL_VERSION >= 34)
1542 case SAVEt_STRLEN_SMALL:
1543 #endif
1544 case SAVEt_SAVESWITCHSTACK:
1545 case SAVEt_VPTR:
1546 case SAVEt_ADELETE:
1547 #if (PERL_REVISION == 5) && (PERL_VERSION >= 38)
1548 case SAVEt_RCPV:
1549 #endif
1550  
1551 case SAVEt_DELETE:
1552 /* ignore */
1553 6305 break;
1554  
1555 0 case SAVEt_AV:
1556 /* a local'ised @var */
1557 0 write_u8(fh, PMAT_SVxSAVED_AV);
1558 0 write_svptr(fh, a0->any_ptr); // GV
1559 0 write_svptr(fh, a1->any_ptr); // AV
1560 0 break;
1561  
1562 0 case SAVEt_HV:
1563 /* a local'ised %var */
1564 0 write_u8(fh, PMAT_SVxSAVED_HV);
1565 0 write_svptr(fh, a0->any_ptr); // GV
1566 0 write_svptr(fh, a1->any_ptr); // HV
1567 0 break;
1568  
1569 0 case SAVEt_SV:
1570 /* a local'ised $var */
1571 0 write_u8(fh, PMAT_SVxSAVED_SV);
1572 0 write_svptr(fh, a0->any_ptr); // GV
1573 0 write_svptr(fh, a1->any_ptr); // SV
1574 0 break;
1575  
1576 0 case SAVEt_HELEM:
1577 /* a local'ised $hash{key} */
1578 0 write_u8(fh, PMAT_SVxSAVED_HELEM);
1579 0 write_svptr(fh, a0->any_ptr); // HV
1580 0 write_svptr(fh, a1->any_ptr); // key SV
1581 0 write_svptr(fh, a2->any_ptr); // value SV
1582 0 break;
1583  
1584 0 case SAVEt_AELEM:
1585 /* a local'ised $array[idx] */
1586 0 write_u8(fh, PMAT_SVxSAVED_AELEM);
1587 0 write_svptr(fh, a0->any_ptr); // AV
1588 0 write_uint(fh, a1->any_iv); // index
1589 0 write_svptr(fh, a2->any_ptr); // value SV
1590 0 break;
1591  
1592 0 case SAVEt_GVSLOT:
1593 /* a local'ised glob slot
1594 * a0 points at the GV itself, a1 points at one of the slots within
1595 * the GP part
1596 * In practice this would only ever be the CODE slot, because other
1597 * slots have other localisation mechanisms
1598 */
1599 0 ▶ if(a1->any_ptr != (SV **) &(GvGP((GV *)a0->any_ptr)->gp_cv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(a1->any_ptr != (SV **) &(GvGP((GV *)a0->any_ptr)->gp_cv)) { 0 0 0
1600 0 fprintf(stderr, "TODO: SAVEt_GVSLOT of slot other than ->gp_cv\n");
1601 0 break;
1602 }
1603  
1604 0 write_u8(fh, PMAT_SVxSAVED_CV);
1605 0 write_svptr(fh, a0->any_ptr);
1606 0 write_svptr(fh, a2->any_ptr);
1607 0 break;
1608  
1609 0 case SAVEt_GENERIC_SVREF:
1610 /* Core perl uses this in a number of places, a few of which we can
1611 * identify
1612 */
1613 0 ▶ if(a0->any_ptr == &GvSV(PL_defgv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(a0->any_ptr == &GvSV(PL_defgv)) { 0 0 0
1614 /* local $_ = ... */
1615 0 write_u8(fh, PMAT_SVxSAVED_SV);
1616 0 write_svptr(fh, (SV *)PL_defgv);
1617 0 write_svptr(fh, a1->any_ptr);
1618 }
1619 else
1620 0 fprintf(stderr, "TODO: SAVEt_GENERIC_SVREF *a0=%p a1=%p\n",
1621 0 *((void **)a0->any_ptr), a1->any_ptr);
1622 0 break;
1623  
1624 0 default:
1625 0 fprintf(stderr, "TODO: savestack type=%d\n", type);
1626 0 break;
1627 }
1628 }
1629 #endif
1630  
1631 1 write_u8(fh, 0);
1632  
1633 // Caller context
1634 int cxix;
1635 1 for(cxix = 0; ; cxix++) {
1636 1 const PERL_CONTEXT *cx = caller_cx(cxix, NULL);
1637 1 ▶ if(!cx)
Branch: 1 branch
BranchTrueFalseTotal
if(!cx) 1 0 1
1638 1 break;
1639  
1640 0 switch(CxTYPE(cx)) {
1641 0 case CXt_SUB: {
1642 0 COP *oldcop = cx->blk_oldcop;
1643  
1644 0 write_u8(fh, PMAT_CTXtSUB);
1645 0 write_u8(fh, cx->blk_gimme);
1646 0 write_uint(fh, CopLINE(oldcop));
1647 0 ▶ write_str(fh, CopFILE(oldcop));
Branch: 1 branch
BranchTrueFalseTotal
write_str(fh, CopFILE(oldcop)); 0 0 0
1648  
1649 0 write_u32(fh, cx->blk_sub.olddepth);
1650 0 write_svptr(fh, (SV*)cx->blk_sub.cv);
1651 #if (PERL_REVISION == 5) && ((PERL_VERSION > 23) || (PERL_VERSION == 23 && PERL_SUBVERSION >= 8))
1652 0 write_svptr(fh, NULL);
1653 #else
1654 write_svptr(fh, CxHASARGS(cx) ? (SV*)cx->blk_sub.argarray : NULL);
1655 #endif
1656  
1657 0 break;
1658 }
1659 0 case CXt_EVAL: {
1660 0 COP *oldcop = cx->blk_oldcop;
1661  
1662  
1663 0 ▶ if(CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) {
Branch: 1 branch
BranchTrueFalseTotal
if(CxOLD_OP_TYPE(cx) == OP_ENTEREVAL) { 0 0 0
1664 /* eval() */
1665 0 write_u8(fh, PMAT_CTXtEVAL);
1666 0 write_u8(fh, cx->blk_gimme);
1667 0 write_uint(fh, CopLINE(oldcop));
1668 0 ▶ write_str(fh, CopFILE(oldcop));
Branch: 1 branch
BranchTrueFalseTotal
write_str(fh, CopFILE(oldcop)); 0 0 0
1669 0 write_svptr(fh, cx->blk_eval.cur_text);
1670 }
1671 0 ▶ else if(cx->blk_eval.old_namesv)
Branch: 1 branch
BranchTrueFalseTotal
else if(cx->blk_eval.old_namesv) 0 0 0
1672 // require
1673 ;
1674 else {
1675 /* eval BLOCK == TRY */
1676 0 write_u8(fh, PMAT_CTXtTRY);
1677 0 write_u8(fh, cx->blk_gimme);
1678 0 write_uint(fh, CopLINE(oldcop));
1679 0 ▶ write_str(fh, CopFILE(oldcop));
Branch: 1 branch
BranchTrueFalseTotal
write_str(fh, CopFILE(oldcop)); 0 0 0
1680 }
1681  
1682 0 break;
1683 }
1684 }
1685 }
1686  
1687 1 write_u8(fh, 0);
1688  
1689 // Mortals stack
1690 {
1691 // Mortal stack is a pre-inc stack
1692 1 write_uint(fh, PL_tmps_ix + 1);
1693 5 ▶ for(SSize_t i = 0; i <= PL_tmps_ix; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(SSize_t i = 0; i <= PL_tmps_ix; i++) { 4 1 5
1694 4 write_ptr(fh, PL_tmps_stack[i]);
1695 }
1696 1 write_uint(fh, PL_tmps_floor);
1697 }
1698  
1699 1 ▶ if(ctx.structdefs)
Branch: 1 branch
BranchTrueFalseTotal
if(ctx.structdefs) 0 1 1
1700 0 SvREFCNT_dec((SV *)ctx.structdefs);
1701 1 }
1702  
1703 MODULE = Devel::MAT::Dumper PACKAGE = Devel::MAT::Dumper
1704  
1705 void
1706 dump(char *file)
1707 CODE:
1708 {
1709 1 FILE *fh = fopen(file, "wb+");
1710 1 ▶ if(!fh)
Branch: 1 branch
BranchTrueFalseTotal
if(!fh) 0 1 1
1711 0 croak("Cannot open %s for writing - %s", file, strerror(errno));
1712  
1713 1 dumpfh(fh);
1714 1 fclose(fh);
1715 }
1716  
1717 void
1718 dumpfh(FILE *fh)
1719  
1720 BOOT:
1721 SV *sv, **svp;
1722  
1723 2 ▶ if((svp = hv_fetchs(PL_modglobal, "Devel::MAT::Dumper/%helper_per_package", 0)))
Branch: 1 branch
BranchTrueFalseTotal
if((svp = hv_fetchs(PL_modglobal, "Devel::MAT::Dumper/%helper_per_package", 0))) 0 2 2
1724 0 sv = *svp;
1725 else
1726 2 hv_stores(PL_modglobal, "Devel::MAT::Dumper/%helper_per_package",
1727 sv = newRV_noinc((SV *)(newHV())));
1728 2 helper_per_package = (HV *)SvRV(sv);
1729  
1730 2 ▶ if((svp = hv_fetchs(PL_modglobal, "Devel::MAT::Dumper/%helper_per_magic", 0)))
Branch: 1 branch
BranchTrueFalseTotal
if((svp = hv_fetchs(PL_modglobal, "Devel::MAT::Dumper/%helper_per_magic", 0))) 0 2 2
1731 0 sv = *svp;
1732 else
1733 2 hv_stores(PL_modglobal, "Devel::MAT::Dumper/%helper_per_magic",
1734 sv = newRV_noinc((SV *)(newHV())));
1735 2 helper_per_magic = (HV *)SvRV(sv);
1736  
1737 2 sv_setiv(*hv_fetchs(PL_modglobal, "Devel::MAT::Dumper/writestruct()", 1), PTR2UV(&writestruct));
« blib/lib/Devel/MAT/Dumper/Helper.pm ↑ Summary