PadWalker.xs

filter: Statementstmt 91.9% 218 / 237 Branchbran 72.2% 156 / 216 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.5% 374 / 453 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/PadWalker.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #ifndef isGV_with_GP
7 #define isGV_with_GP(x) isGV(x)
8 #endif
9  
10 #ifndef CxOLD_OP_TYPE
11 # define CxOLD_OP_TYPE(cx) (0 + (cx)->blk_eval.old_op_type)
12 #endif
13  
14 #ifndef CvISXSUB
15 #define CvISXSUB(sv) CvXSUB(sv)
16 #endif
17  
18 #ifndef PadnameUTF8
19 # define PadnameUTF8(pn) FALSE
20 #endif
21  
22 /* For development testing */
23 #ifdef PADWALKER_DEBUGGING
24 # define debug_print(x) printf x
25 #else
26 # define debug_print(x)
27 #endif
28  
29 /* For debugging */
30 #ifdef PADWALKER_DEBUGGING
31 char *
32 cxtype_name(U32 cx_type)
33 {
34 switch(cx_type & CXTYPEMASK)
35 {
36 case CXt_NULL: return "null";
37 case CXt_SUB: return "sub";
38 case CXt_EVAL: return "eval";
39 case CXt_LOOP: return "loop";
40 case CXt_SUBST: return "subst";
41 case CXt_BLOCK: return "block";
42 case CXt_FORMAT: return "format";
43  
44 default: debug_print(("Unknown context type 0x%lx\n", cx_type));
45 return "(unknown)";
46 }
47 }
48  
49 void
50 show_cxstack(void)
51 {
52 I32 i;
53 for (i = cxstack_ix; i>=0; --i)
54 {
55 printf(" =%ld= %s (%lx)", (long)i,
56 cxtype_name(CxTYPE(&cxstack[i])), cxstack[i].blk_oldcop->cop_seq);
57 if (CxTYPE(&cxstack[i]) == CXt_SUB) {
58 CV *cv = cxstack[i].blk_sub.cv;
59 printf("\t%s", (cv && CvGV(cv)) ? GvNAME(CvGV(cv)) :"(null)");
60 }
61 printf("\n");
62 }
63 }
64 #else
65 # define show_cxstack()
66 #endif
67  
68 #ifndef SvOURSTASH
69 # ifdef OURSTASH
70 # define SvOURSTASH OURSTASH
71 # else
72 # define SvOURSTASH GvSTASH
73 # endif
74 #endif
75  
76 #ifndef COP_SEQ_RANGE_LOW
77 # define COP_SEQ_RANGE_LOW(sv) U_32(SvNVX(sv))
78 #endif
79 #ifndef COP_SEQ_RANGE_HIGH
80 # define COP_SEQ_RANGE_HIGH(sv) U_32(SvUVX(sv))
81 #endif
82  
83 #ifndef PadARRAY
84 typedef AV PADNAMELIST;
85 typedef SV PADNAME;
86 # if PERL_VERSION < 8 || (PERL_VERSION == 8 && !PERL_SUBVERSION)
87 typedef AV PADLIST;
88 typedef AV PAD;
89 # endif
90 # define PadlistARRAY(pl) ((PAD **)AvARRAY(pl))
91 # define PadlistMAX(pl) AvFILLp(pl)
92 # define PadlistNAMES(pl) (*PadlistARRAY(pl))
93 # define PadnamelistARRAY(pnl) ((PADNAME **)AvARRAY(pnl))
94 # define PadnamelistMAX(pnl) AvFILLp(pnl)
95 # define PadARRAY AvARRAY
96 # define PadnameIsOUR(pn) !!(SvFLAGS(pn) & SVpad_OUR)
97 # define PadnameOURSTASH(pn) SvOURSTASH(pn)
98 # define PadnameOUTER(pn) !!SvFAKE(pn)
99 # define PadnamePV(pn) (SvPOKp(pn) ? SvPVX(pn) : NULL)
100 #endif
101  
102  
103 /* Originally stolen from pp_ctl.c; now significantly different */
104  
105 I32
106 56 dopoptosub_at(pTHX_ PERL_CONTEXT *cxstk, I32 startingblock)
107 {
108 dTHR;
109 I32 i;
110 PERL_CONTEXT *cx;
111 105 ▶ for (i = startingblock; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = startingblock; i >= 0; i--) { 83 22 105
112 83 cx = &cxstk[i];
113 83 ▶ switch (CxTYPE(cx)) {
Branch: 1 branch
BranchTrueFalseTotal
switch (CxTYPE(cx)) { 49 34 83
114 49 default:
115 49 continue;
116 34 case CXt_SUB:
117 /* In Perl 5.005, formats just used CXt_SUB */
118 #ifdef CXt_FORMAT
119 case CXt_FORMAT:
120 #endif
121 debug_print(("**dopoptosub_at: found sub #%ld\n", (long)i));
122 34 return i;
123 }
124 }
125 debug_print(("**dopoptosub_at: not found #%ld\n", (long)i));
126 22 return i;
127 }
128  
129 I32
130 34 dopoptosub(pTHX_ I32 startingblock)
131 {
132 dTHR;
133 34 return dopoptosub_at(aTHX_ cxstack, startingblock);
134 }
135  
136 /* This function is based on the code of pp_caller */
137 PERL_CONTEXT*
138 34 upcontext(pTHX_ I32 count, COP **cop_p, PERL_CONTEXT **ccstack_p,
139 I32 *cxix_from_p, I32 *cxix_to_p)
140 {
141 34 PERL_SI *top_si = PL_curstackinfo;
142 34 I32 cxix = dopoptosub(aTHX_ cxstack_ix);
143 34 PERL_CONTEXT *ccstack = cxstack;
144  
145 34 ▶ if (cxix_from_p) *cxix_from_p = cxstack_ix+1;
Branch: 1 branch
BranchTrueFalseTotal
if (cxix_from_p) *cxix_from_p = cxstack_ix+1; 34 0 34
146 34 ▶ if (cxix_to_p) *cxix_to_p = cxix;
Branch: 1 branch
BranchTrueFalseTotal
if (cxix_to_p) *cxix_to_p = cxix; 34 0 34
147 for (;;) {
148 /* we may be in a higher stacklevel, so dig down deeper */
149 56 ▶ while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
Branch: 2 branches
BranchTrueFalseTotal
while (cxix < 0 && top_si->si_type != PERLSI_MAIN) { 22 34 56
while (cxix < 0 && top_si->si_type != PERLSI_MAIN) { 2 20 22
150 2 top_si = top_si->si_prev;
151 2 ccstack = top_si->si_cxstack;
152 2 cxix = dopoptosub_at(aTHX_ ccstack, top_si->si_cxix);
153 2 ▶ if (cxix_to_p && cxix_from_p) *cxix_from_p = *cxix_to_p;
Branch: 2 branches
BranchTrueFalseTotal
if (cxix_to_p && cxix_from_p) *cxix_from_p = *cxix_to_p; 2 0 2
if (cxix_to_p && cxix_from_p) *cxix_from_p = *cxix_to_p; 2 0 2
154 2 ▶ if (cxix_to_p) *cxix_to_p = cxix;
Branch: 1 branch
BranchTrueFalseTotal
if (cxix_to_p) *cxix_to_p = cxix; 2 0 2
155 }
156 54 ▶ if (cxix < 0 && count == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (cxix < 0 && count == 0) { 20 34 54
if (cxix < 0 && count == 0) { 19 1 20
157 19 ▶ if (ccstack_p) *ccstack_p = ccstack;
Branch: 1 branch
BranchTrueFalseTotal
if (ccstack_p) *ccstack_p = ccstack; 19 0 19
158 19 return (PERL_CONTEXT *)0;
159 }
160 35 ▶ else if (cxix < 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (cxix < 0) 1 34 35
161 1 return (PERL_CONTEXT *)-1;
162 34 ▶ if (PL_DBsub && cxix >= 0 &&
Branch: 2 branches
BranchTrueFalseTotal
if (PL_DBsub && cxix >= 0 && 34 0 34
if (PL_DBsub && cxix >= 0 && 34 0 34
163 34 ▶ ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
Branch: 1 branch
BranchTrueFalseTotal
ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub)) 0 34 34
164 0 count++;
165 34 ▶ if (!count--)
Branch: 1 branch
BranchTrueFalseTotal
if (!count--) 14 20 34
166 14 break;
167  
168 20 ▶ if (cop_p) *cop_p = ccstack[cxix].blk_oldcop;
Branch: 1 branch
BranchTrueFalseTotal
if (cop_p) *cop_p = ccstack[cxix].blk_oldcop; 19 1 20
169 20 cxix = dopoptosub_at(aTHX_ ccstack, cxix - 1);
170 20 ▶ if (cxix_to_p && cxix_from_p) *cxix_from_p = *cxix_to_p;
Branch: 2 branches
BranchTrueFalseTotal
if (cxix_to_p && cxix_from_p) *cxix_from_p = *cxix_to_p; 20 0 20
if (cxix_to_p && cxix_from_p) *cxix_from_p = *cxix_to_p; 20 0 20
171 20 ▶ if (cxix_to_p) *cxix_to_p = cxix;
Branch: 1 branch
BranchTrueFalseTotal
if (cxix_to_p) *cxix_to_p = cxix; 20 0 20
172 }
173 14 ▶ if (ccstack_p) *ccstack_p = ccstack;
Branch: 1 branch
BranchTrueFalseTotal
if (ccstack_p) *ccstack_p = ccstack; 14 0 14
174 14 return &ccstack[cxix];
175 }
176  
177 /* end thievery */
178  
179 SV*
180 17 fetch_from_stash(pTHX_ HV *stash, char *name_str, U32 name_len)
181 {
182 /* This isn't the most efficient approach, but it has
183 * the advantage that it uses documented API functions. */
184 17 ▶ char *package_name = HvNAME(stash);
Branch: 6 branches
BranchTrueFalseTotal
char *package_name = HvNAME(stash); 17 0 17
char *package_name = HvNAME(stash); 17 0 17
char *package_name = HvNAME(stash); 0 17 17
char *package_name = HvNAME(stash); 0 0 0
char *package_name = HvNAME(stash); 17 0 17
char *package_name = HvNAME(stash); 0 17 17
185 char *qualified_name;
186 17 SV *ret = 0; /* Initialise to silence spurious compiler warning */
187
188 17 New(0, qualified_name, strlen(package_name) + 2 + name_len, char);
189 17 strcpy(qualified_name, package_name);
190 17 strcat(qualified_name, "::");
191 17 strcat(qualified_name, name_str+1);
192  
193 debug_print(("fetch_from_stash: Looking for %c%s\n",
194 name_str[0], qualified_name));
195 17 switch (name_str[0]) {
196 15 case '$': ret = get_sv(qualified_name, FALSE); break;
197 0 case '@': ret = (SV*) get_av(qualified_name, FALSE); break;
198 2 case '%': ret = (SV*) get_hv(qualified_name, FALSE); break;
199 0 default: die("PadWalker: variable '%s' of unknown type", name_str);
200 }
201 if (ret)
202 debug_print(("%s\n", sv_peek(ret)));
203 else
204 /* I don't _think_ this should ever happen */
205 debug_print(("XXXX - Variable %c%s not found\n",
206 name_str[0], qualified_name));
207 17 Safefree(qualified_name);
208 17 return ret;
209 }
210  
211 void
212 68 pads_into_hash(pTHX_ PADNAMELIST* pad_namelist, PAD* pad_vallist, HV* my_hash,
213 HV* our_hash, U32 valid_at_seq)
214 {
215 I32 i;
216  
217 debug_print(("pads_into_hash(%p, %p, ..)\n",
218 (void*)pad_namelist, (void*) pad_vallist));
219  
220 738 ▶ for (i=PadnamelistMAX(pad_namelist); i>=0; --i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=PadnamelistMAX(pad_namelist); i>=0; --i) { 670 68 738
221 670 PADNAME* name_sv = PadnamelistARRAY(pad_namelist)[i];
222  
223 670 ▶ if (name_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (name_sv) { 536 134 670
224 536 char *name_str = PadnamePV(name_sv);
225 536 ▶ if (name_str) {
Branch: 1 branch
BranchTrueFalseTotal
if (name_str) { 343 193 536
226  
227 debug_print(("** %s (%lx,%lx) [%lx]%s\n", name_str,
228 COP_SEQ_RANGE_LOW(name_sv), COP_SEQ_RANGE_HIGH(name_sv), valid_at_seq,
229 PadnameOUTER(name_sv) ? " <fake>" : ""));
230
231 /* Check that this variable is valid at the cop_seq
232 * specified, by peeking into the NV and IV slots
233 * of the name sv. (This must be one of those "breathtaking
234 * optimisations" mentioned in the Panther book).
235  
236 * Anonymous subs are stored here with a name of "&",
237 * so also check that the name is longer than one char.
238 * (Note that the prefix letter is here as well, so a
239 * valid variable will _always_ be >1 char)
240 */
241  
242 343 ▶ if ((PadnameOUTER(name_sv) || 0 == valid_at_seq ||
Branch: 2 branches
BranchTrueFalseTotal
if ((PadnameOUTER(name_sv) || 0 == valid_at_seq || 334 9 343
if ((PadnameOUTER(name_sv) || 0 == valid_at_seq || 324 10 334
243 324 ▶ (valid_at_seq <= COP_SEQ_RANGE_HIGH(name_sv) &&
Branch: 1 branch
BranchTrueFalseTotal
(valid_at_seq <= COP_SEQ_RANGE_HIGH(name_sv) && 211 113 324
244 211 ▶ valid_at_seq > COP_SEQ_RANGE_LOW(name_sv))) &&
Branch: 1 branch
BranchTrueFalseTotal
valid_at_seq > COP_SEQ_RANGE_LOW(name_sv))) && 101 110 211
245 120 ▶ strlen(name_str) > 1 )
Branch: 1 branch
BranchTrueFalseTotal
strlen(name_str) > 1 ) 120 0 120
246  
247 {
248 SV *val_sv;
249 120 U32 name_len = strlen(name_str);
250 120 bool is_our = PadnameIsOUR(name_sv);
251  
252 debug_print(((is_our ? "** FOUND OUR %s\n"
253 : "** FOUND MY %s\n"), name_str));
254  
255 120 ▶ if ( hv_exists(my_hash, name_str, name_len)
Branch: 1 branch
BranchTrueFalseTotal
if ( hv_exists(my_hash, name_str, name_len) 113 7 120
256 113 ▶ || hv_exists(our_hash, name_str, name_len))
Branch: 1 branch
BranchTrueFalseTotal
|| hv_exists(our_hash, name_str, name_len)) 109 4 113
257 {
258 debug_print(("** key already exists - ignoring!\n"));
259 }
260 else {
261 109 ▶ if (is_our) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_our) { 17 92 109
262 17 val_sv = fetch_from_stash(aTHX_ PadnameOURSTASH(name_sv),
263 name_str, name_len);
264 17 ▶ if (!val_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!val_sv) { 0 17 17
265 debug_print(("Value of our variable is undefined\n"));
266 0 val_sv = &PL_sv_undef;
267 }
268 }
269 else
270 {
271 92 val_sv =
272 92 ▶ pad_vallist ? PadARRAY(pad_vallist)[i] : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
pad_vallist ? PadARRAY(pad_vallist)[i] : &PL_sv_undef; 92 0 92
273 92 ▶ if (!val_sv) val_sv = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (!val_sv) val_sv = &PL_sv_undef; 0 92 92
274 }
275  
276 109 ▶ hv_store((is_our ? our_hash : my_hash), name_str, PadnameUTF8(name_sv) ? -name_len : name_len,
Branch: 2 branches
BranchTrueFalseTotal
hv_store((is_our ? our_hash : my_hash), name_str, PadnameUTF8(name_sv) ? -name_len : name_len, 109 0 109
hv_store((is_our ? our_hash : my_hash), name_str, PadnameUTF8(name_sv) ? -name_len : name_len, 17 92 109
277 (val_sv ? newRV_inc(val_sv) : &PL_sv_undef), 0);
278 }
279 }
280 }
281 }
282 }
283 68 }
284  
285 void
286 68 padlist_into_hash(pTHX_ PADLIST* padlist, HV* my_hash, HV* our_hash,
287 U32 valid_at_seq, long depth)
288 {
289 PADNAMELIST *pad_namelist;
290 PAD *pad_vallist;
291
292 68 ▶ if (depth == 0) depth = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (depth == 0) depth = 1; 6 62 68
293  
294 68 ▶ if (!padlist) {
Branch: 1 branch
BranchTrueFalseTotal
if (!padlist) { 0 68 68
295 /* Probably an XSUB */
296 0 die("PadWalker: cv has no padlist");
297 }
298 68 pad_namelist = PadlistNAMES(padlist);
299 68 pad_vallist = PadlistARRAY(padlist)[depth];
300  
301 68 pads_into_hash(aTHX_ pad_namelist, pad_vallist, my_hash, our_hash, valid_at_seq);
302 68 }
303  
304 void
305 37 context_vars(pTHX_ PERL_CONTEXT *cx, HV* my_ret, HV* our_ret, U32 seq, CV *cv)
306 {
307 /* If cx is null, we take that to mean that we should look
308 * at the cv instead
309 */
310  
311 debug_print(("**context_vars(%p, %p, %p, 0x%lx)\n",
312 (void*)cx, (void*)my_ret, (void*)our_ret, (long)seq));
313 37 ▶ if (cx == (PERL_CONTEXT*)-1)
Branch: 1 branch
BranchTrueFalseTotal
if (cx == (PERL_CONTEXT*)-1) 1 36 37
314 1 croak("Not nested deeply enough");
315  
316 else {
317 36 ▶ CV* cur_cv = cx ? cx->blk_sub.cv : cv;
Branch: 1 branch
BranchTrueFalseTotal
CV* cur_cv = cx ? cx->blk_sub.cv : cv; 14 22 36
318 36 ▶ long depth = cx ? cx->blk_sub.olddepth + 1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
long depth = cx ? cx->blk_sub.olddepth + 1 : 1; 14 22 36
319  
320 36 ▶ if (!cur_cv)
Branch: 1 branch
BranchTrueFalseTotal
if (!cur_cv) 0 36 36
321 0 die("panic: Context has no CV!\n");
322
323 101 ▶ while (cur_cv) {
Branch: 1 branch
BranchTrueFalseTotal
while (cur_cv) { 65 36 101
324 debug_print(("\tcv name = %s; depth=%ld\n",
325 CvGV(cur_cv) ? GvNAME(CvGV(cur_cv)) :"(null)", depth));
326 65 ▶ if (CvPADLIST(cur_cv))
Branch: 1 branch
BranchTrueFalseTotal
if (CvPADLIST(cur_cv)) 64 1 65
327 64 padlist_into_hash(aTHX_ CvPADLIST(cur_cv), my_ret, our_ret, seq, depth);
328 65 cur_cv = CvOUTSIDE(cur_cv);
329 65 ▶ if (cur_cv) depth = CvDEPTH(cur_cv);
Branch: 1 branch
BranchTrueFalseTotal
if (cur_cv) depth = CvDEPTH(cur_cv); 29 36 65
330 }
331 }
332 36 }
333  
334 void
335 27 do_peek(pTHX_ I32 uplevel, HV* my_hash, HV* our_hash)
336 {
337 PERL_CONTEXT *cx, *ccstack;
338 27 COP *cop = 0;
339 I32 cxix_from, cxix_to, i;
340 27 bool first_eval = TRUE;
341  
342 show_cxstack();
343 27 if (PL_curstackinfo->si_type != PERLSI_MAIN)
344 debug_print(("!! We're in a higher stack level\n"));
345  
346 27 cx = upcontext(aTHX_ uplevel, &cop, &ccstack, &cxix_from, &cxix_to);
347 debug_print(("** cxix = (%ld,%ld)\n", cxix_from, cxix_to));
348 27 ▶ if (cop == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cop == 0) { 14 13 27
349 debug_print(("**Setting cop to PL_curcop\n"));
350 14 cop = PL_curcop;
351 }
352 debug_print(("**Cop file = %s\n", CopFILE(cop)));
353  
354 27 context_vars(aTHX_ cx, my_hash, our_hash, cop->cop_seq, PL_main_cv);
355  
356 49 ▶ for (i = cxix_from-1; i > cxix_to; --i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = cxix_from-1; i > cxix_to; --i) { 25 24 49
357 debug_print(("** CxTYPE = %s (cxix = %ld)\n",
358 cxtype_name(CxTYPE(&ccstack[i])), i));
359 25 switch (CxTYPE(&ccstack[i])) {
360 7 case CXt_EVAL:
361 debug_print(("\told_op_type = %ld\n", CxOLD_OP_TYPE(&ccstack[i])));
362 7 switch(CxOLD_OP_TYPE(&ccstack[i])) {
363 5 case OP_ENTEREVAL:
364 5 ▶ if (first_eval) {
Branch: 1 branch
BranchTrueFalseTotal
if (first_eval) { 3 2 5
365 3 context_vars(aTHX_ 0, my_hash, our_hash, cop->cop_seq, ccstack[i].blk_eval.cv);
366 3 first_eval = FALSE;
367 }
368 5 context_vars(aTHX_ 0, my_hash, our_hash, ccstack[i].blk_oldcop->cop_seq,
369 5 ccstack[i].blk_eval.cv);
370 5 break;
371 2 case OP_REQUIRE:
372 case OP_DOFILE:
373 debug_print(("blk_eval.cv = %p\n", (void*) ccstack[i].blk_eval.cv));
374 2 ▶ if (first_eval)
Branch: 1 branch
BranchTrueFalseTotal
if (first_eval) 2 0 2
375 2 context_vars(aTHX_ 0, my_hash, our_hash,
376 2 cop->cop_seq, ccstack[i].blk_eval.cv);
377 2 return;
378 /* If it's OP_ENTERTRY, we skip this altogether. */
379 }
380 5 break;
381  
382 0 case CXt_SUB:
383 #ifdef CXt_FORMAT
384 case CXt_FORMAT:
385 #endif
386 0 Perl_die(aTHX_ "PadWalker: internal error");
387 exit(EXIT_FAILURE);
388 }
389 }
390 }
391  
392 void
393 7 get_closed_over(pTHX_ CV *cv, HV *hash, HV *indices)
394 {
395 I32 i;
396 U32 val_depth;
397 PADNAMELIST *pad_namelist;
398 PAD *pad_vallist;
399  
400 7 ▶ if (CvISXSUB(cv) || !CvPADLIST(cv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CvISXSUB(cv) || !CvPADLIST(cv)) { 6 1 7
if (CvISXSUB(cv) || !CvPADLIST(cv)) { 0 6 6
401 1 return;
402 }
403  
404 6 ▶ val_depth = CvDEPTH(cv) ? CvDEPTH(cv) : 1;
Branch: 1 branch
BranchTrueFalseTotal
val_depth = CvDEPTH(cv) ? CvDEPTH(cv) : 1; 2 4 6
405 6 pad_namelist = PadlistNAMES(CvPADLIST(cv));
406 6 pad_vallist = PadlistARRAY(CvPADLIST(cv))[val_depth];
407  
408 debug_print(("PadlistMAX(CvPADLIST(cv)) = %ld\n",
409 PadlistMAX(CvPADLIST(cv)) ));
410
411 86 ▶ for (i=PadnamelistMAX(pad_namelist); i>=0; --i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=PadnamelistMAX(pad_namelist); i>=0; --i) { 80 6 86
412 80 PADNAME* name_sv = PadnamelistARRAY(pad_namelist)[i];
413  
414 80 ▶ if (name_sv && PadnamePV(name_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (name_sv && PadnamePV(name_sv)) { 80 0 80
if (name_sv && PadnamePV(name_sv)) { 15 65 80
415 15 char* name_str = PadnamePV(name_sv);
416 15 STRLEN name_len = strlen(name_str);
417
418 15 ▶ if (PadnameOUTER(name_sv) && !PadnameIsOUR(name_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (PadnameOUTER(name_sv) && !PadnameIsOUR(name_sv)) { 9 6 15
if (PadnameOUTER(name_sv) && !PadnameIsOUR(name_sv)) { 8 1 9
419 8 SV *val_sv = PadARRAY(pad_vallist)[i];
420 8 ▶ if (!val_sv) val_sv = &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (!val_sv) val_sv = &PL_sv_undef; 0 8 8
421 #ifdef PADWALKER_DEBUGGING
422 debug_print(("Found a fake slot: %s\n", name_str));
423 if (val == 0)
424 debug_print(("value is null\n"));
425 else
426 sv_dump(*val);
427 #endif
428 8 hv_store(hash, name_str, name_len, newRV_inc(val_sv), 0);
429 8 ▶ if (indices) {
Branch: 1 branch
BranchTrueFalseTotal
if (indices) { 0 8 8
430 /* Create a temporary SV as a way of getting perl to
431 * stringify 'i' for us. */
432 0 SV *i_sv = newSViv(i);
433 0 hv_store_ent(indices, i_sv, newRV_inc(val_sv), 0);
434 0 SvREFCNT_dec(i_sv);
435 }
436 }
437 }
438 }
439 }
440  
441 char *
442 8 get_var_name(CV *cv, SV *var)
443 {
444 I32 i;
445 8 ▶ U32 val_depth = CvDEPTH(cv) ? CvDEPTH(cv) : 1;
Branch: 1 branch
BranchTrueFalseTotal
U32 val_depth = CvDEPTH(cv) ? CvDEPTH(cv) : 1; 7 1 8
446 8 PADNAMELIST *pad_namelist = PadlistNAMES(CvPADLIST(cv));
447 8 PAD *pad_vallist = PadlistARRAY(CvPADLIST(cv))[val_depth];
448  
449 28 ▶ for (i=PadnamelistMAX(pad_namelist); i>=0; --i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=PadnamelistMAX(pad_namelist); i>=0; --i) { 28 0 28
450 28 PADNAME* name = PadnamelistARRAY(pad_namelist)[i];
451 char* name_str;
452  
453 28 ▶ if ( name && (name_str = PadnamePV(name))
Branch: 2 branches
BranchTrueFalseTotal
if ( name && (name_str = PadnamePV(name)) 19 9 28
if ( name && (name_str = PadnamePV(name)) 19 0 19
454 19 ▶ && PadARRAY(pad_vallist)[i] == var) {
Branch: 1 branch
BranchTrueFalseTotal
&& PadARRAY(pad_vallist)[i] == var) { 8 11 19
455 8 return name_str;
456 }
457 }
458 0 return 0;
459 }
460  
461 CV *
462 7 up_cv(pTHX_ I32 uplevel, const char * caller_name)
463 {
464 PERL_CONTEXT *cx, *ccstack;
465 I32 cxix_from, cxix_to, i;
466  
467 7 ▶ if (uplevel < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (uplevel < 0) 0 7 7
468 0 croak("%s: sub is < 0", caller_name);
469  
470 7 cx = upcontext(aTHX_ uplevel, 0, &ccstack, &cxix_from, &cxix_to);
471 7 ▶ if (cx == (PERL_CONTEXT *)-1) {
Branch: 1 branch
BranchTrueFalseTotal
if (cx == (PERL_CONTEXT *)-1) { 0 7 7
472 0 croak("%s: Not nested deeply enough", caller_name);
473 return 0; /* NOT REACHED, but stop picky compilers from whining */
474 }
475 7 ▶ else if (cx)
Branch: 1 branch
BranchTrueFalseTotal
else if (cx) 0 7 7
476 0 return cx->blk_sub.cv;
477
478 else {
479  
480 12 ▶ for (i = cxix_from-1; i > cxix_to; --i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = cxix_from-1; i > cxix_to; --i) 9 3 12
481 9 ▶ if (CxTYPE(&ccstack[i]) == CXt_EVAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (CxTYPE(&ccstack[i]) == CXt_EVAL) { 6 3 9
482 6 I32 old_op_type = CxOLD_OP_TYPE(&ccstack[i]);
483 6 ▶ if (old_op_type == OP_REQUIRE || old_op_type == OP_DOFILE)
Branch: 2 branches
BranchTrueFalseTotal
if (old_op_type == OP_REQUIRE || old_op_type == OP_DOFILE) 5 1 6
if (old_op_type == OP_REQUIRE || old_op_type == OP_DOFILE) 3 2 5
484 4 return ccstack[i].blk_eval.cv;
485 }
486  
487 3 return PL_main_cv;
488 }
489 }
490  
491 STATIC bool
492 3 is_scalar_type(SV *sv) {
493 3 return !(
494 3 ▶ SvTYPE(sv) == SVt_PVAV
Branch: 1 branch
BranchTrueFalseTotal
SvTYPE(sv) == SVt_PVAV 2 1 3
495 2 ▶ || SvTYPE(sv) == SVt_PVHV
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(sv) == SVt_PVHV 2 0 2
496 2 ▶ || SvTYPE(sv) == SVt_PVCV
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(sv) == SVt_PVCV 2 0 2
497 2 ▶ || isGV_with_GP(sv)
Branch: 3 branches
BranchTrueFalseTotal
|| isGV_with_GP(sv) 0 2 2
|| isGV_with_GP(sv) 0 0 0
|| isGV_with_GP(sv) 0 0 0
498 2 ▶ || SvTYPE(sv) == SVt_PVIO
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(sv) == SVt_PVIO 2 0 2
499 );
500 }
501  
502 STATIC bool
503 4 is_correct_type(SV *orig, SV *restore) {
504 return (
505 4 ( SvTYPE(orig) == SvTYPE(restore) )
506 7 ||
507 3 ▶ ( is_scalar_type(orig) && is_scalar_type(restore) )
Branch: 1 branch
BranchTrueFalseTotal
( is_scalar_type(orig) && is_scalar_type(restore) ) 1 0 1
508 );
509 }
510  
511  
512 MODULE = PadWalker PACKAGE = PadWalker
513 PROTOTYPES: DISABLE
514  
515 void
516 peek_my(uplevel)
517 I32 uplevel;
518 PREINIT:
519 26 HV* ret = newHV();
520 26 HV* ignore = newHV();
521 PPCODE:
522 26 do_peek(aTHX_ uplevel, ret, ignore);
523 25 SvREFCNT_dec((SV*) ignore);
524 25 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 25 25
525 25 PUSHs(sv_2mortal(newRV_noinc((SV*)ret)));
526  
527 void
528 peek_our(uplevel)
529 I32 uplevel;
530 PREINIT:
531 1 HV* ret = newHV();
532 1 HV* ignore = newHV();
533 PPCODE:
534 1 do_peek(aTHX_ uplevel, ignore, ret);
535 1 SvREFCNT_dec((SV*) ignore);
536 1 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 1 1
537 1 PUSHs(sv_2mortal(newRV_noinc((SV*)ret)));
538  
539  
540 void
541 peek_sub(cv)
542 CV* cv;
543 PREINIT:
544 6 HV* ret = newHV();
545 6 HV* ignore = newHV();
546 PPCODE:
547 5 ▶ if (CvISXSUB(cv))
Branch: 1 branch
BranchTrueFalseTotal
if (CvISXSUB(cv)) 1 4 5
548 1 die("PadWalker: cv has no padlist");
549 4 padlist_into_hash(aTHX_ CvPADLIST(cv), ret, ignore, 0, CvDEPTH(cv));
550 4 SvREFCNT_dec((SV*) ignore);
551 4 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 4 4
552 4 PUSHs(sv_2mortal(newRV_noinc((SV*)ret)));
553  
554 void
555 set_closed_over(sv, pad)
556 SV* sv;
557 HV* pad;
558 PREINIT:
559 I32 i;
560 4 CV *cv = (CV *)SvRV(sv);
561 4 ▶ U32 val_depth = CvDEPTH(cv) ? CvDEPTH(cv) : 1;
Branch: 1 branch
BranchTrueFalseTotal
U32 val_depth = CvDEPTH(cv) ? CvDEPTH(cv) : 1; 0 4 4
562 4 PADNAMELIST *pad_namelist = PadlistNAMES(CvPADLIST(cv));
563 4 PAD *pad_vallist = PadlistARRAY(CvPADLIST(cv))[val_depth];
564 CODE:
565 23 ▶ for (i=PadnamelistMAX(pad_namelist); i>=0; --i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i=PadnamelistMAX(pad_namelist); i>=0; --i) { 20 3 23
566 20 PADNAME* name = PadnamelistARRAY(pad_namelist)[i];
567 char* name_str;
568  
569 20 ▶ if (name && (name_str = PadnamePV(name))) {
Branch: 2 branches
BranchTrueFalseTotal
if (name && (name_str = PadnamePV(name))) { 20 0 20
if (name && (name_str = PadnamePV(name))) { 10 10 20
570 10 STRLEN name_len = strlen(name_str);
571  
572 10 ▶ if (PadnameOUTER(name) && !PadnameIsOUR(name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (PadnameOUTER(name) && !PadnameIsOUR(name)) { 7 3 10
if (PadnameOUTER(name) && !PadnameIsOUR(name)) { 7 0 7
573 7 SV **restore_ref = hv_fetch(pad, name_str, name_len, FALSE);
574 7 ▶ if ( restore_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( restore_ref ) { 4 3 7
575 4 ▶ if ( SvROK(*restore_ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvROK(*restore_ref) ) { 4 0 4
576 4 SV *restore = SvRV(*restore_ref);
577 4 SV *orig = PadARRAY(pad_vallist)[i];
578 4 int restore_type = SvTYPE(restore);
579  
580 4 ▶ if ( !orig || is_correct_type(orig, restore) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( !orig || is_correct_type(orig, restore) ) { 4 0 4
if ( !orig || is_correct_type(orig, restore) ) { 3 1 4
581 3 SvREFCNT_inc(restore);
582  
583 3 PadARRAY(pad_vallist)[i] = restore;
584 } else {
585 1 croak("Incorrect reftype for variable %s (got %s expected %s)", name_str, sv_reftype(restore, 0), sv_reftype(orig, 0));
586 }
587 } else {
588 0 croak("The variable for %s is not a reference", name_str);
589 }
590 }
591 }
592 }
593 }
594  
595  
596  
597 void
598 closed_over(cv)
599 CV* cv;
600 PREINIT:
601 7 HV* ret = newHV();
602 HV* targs;
603 PPCODE:
604 7 ▶ if (GIMME_V == G_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (GIMME_V == G_ARRAY) { 1 6 7
605 1 targs = newHV();
606 1 get_closed_over(aTHX_ cv, ret, targs);
607
608 1 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 1 1
609 1 PUSHs(sv_2mortal(newRV_noinc((SV*)ret)));
610 1 PUSHs(sv_2mortal(newRV_noinc((SV*)targs)));
611 }
612 else {
613 6 get_closed_over(aTHX_ cv, ret, 0);
614
615 6 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 6 6
616 6 PUSHs(sv_2mortal(newRV_noinc((SV*)ret)));
617 }
618  
619 char*
620 var_name(sub, var_ref)
621 SV* sub;
622 SV* var_ref;
623 PREINIT:
624 SV *cv;
625 CODE:
626 8 ▶ if (!SvROK(var_ref))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(var_ref)) 0 8 8
627 0 croak("Usage: PadWalker::var_name(sub, var_ref)");
628
629 8 ▶ if (SvROK(sub)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sub)) { 1 7 8
630 1 cv = SvRV(sub);
631 1 ▶ if (SvTYPE(cv) != SVt_PVCV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(cv) != SVt_PVCV) 0 1 1
632 0 croak("PadWalker::var_name: sub is neither a CODE reference nor a number");
633 } else
634 7 cv = (SV *) up_cv(aTHX_ SvIV(sub), "PadWalker::upcontext");
635
636 8 RETVAL = get_var_name((CV *) cv, SvRV(var_ref));
637 OUTPUT:
638 RETVAL
639  
640 void
641 _upcontext(uplevel)
642 I32 uplevel
643 PPCODE:
644 /* This is used by Devel::Caller. */
645 0 ▶ XPUSHs(sv_2mortal(newSViv((IV)upcontext(aTHX_ uplevel, 0, 0, 0, 0))));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv((IV)upcontext(aTHX_ uplevel, 0, 0, 0, 0)))); 0 0 0
↑ Summary blib/lib/PadWalker.pm »