ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Devel-FindRef/FindRef.xs
(Generate patch)

Comparing Devel-FindRef/FindRef.xs (file contents):
Revision 1.14 by root, Mon Dec 1 13:22:43 2008 UTC vs.
Revision 1.26 by root, Fri Jun 23 14:40:34 2017 UTC

4 4
5#define PERL_VERSION_ATLEAST(a,b,c) \ 5#define PERL_VERSION_ATLEAST(a,b,c) \
6 (PERL_REVISION > (a) \ 6 (PERL_REVISION > (a) \
7 || (PERL_REVISION == (a) \ 7 || (PERL_REVISION == (a) \
8 && (PERL_VERSION > (b) \ 8 && (PERL_VERSION > (b) \
9 || (PERL_VERSION == (b) && PERLSUBVERSION >= (c))))) 9 || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c)))))
10 10
11#if !PERL_VERSION_ATLEAST (5,8,9) 11#if !PERL_VERSION_ATLEAST (5,8,9)
12# define SVt_LAST 16 12# define SVt_LAST 16
13#endif 13#endif
14 14
15#if !PERL_VERSION_ATLEAST (5,10,0) 15#ifndef SvPAD_OUR
16# define SvPAD_OUR(dummy) 0 16# define SvPAD_OUR(dummy) 0
17#endif
18
19/* pre-5.10 perls always succeed, with 5.10, we have to check first apparently */
20#ifndef GvNAME_HEK
21# define GvNAME_HEK(sv) 1
22#endif
23
24#ifndef PadARRAY
25typedef AV PADNAMELIST;
26typedef SV PADNAME;
27# define PadnamePV(sv) SvPVX (sv)
28# define PadnameLEN(sv) SvCUR (sv)
29# define PadARRAY(pad) AvARRAY (pad)
30# define PadlistARRAY(pl) ((PAD **)AvARRAY (pl))
31#endif
32
33#ifndef PadMAX
34# define PadMAX(pad) AvFILLp (pad)
35#endif
36
37#ifndef padnamelist_fetch
38# define padnamelist_fetch(a,b) *av_fetch (a, b, FALSE)
39#endif
40
41#ifndef PadlistNAMES
42# define PadlistNAMES(padlist) *PadlistARRAY (padlist)
17#endif 43#endif
18 44
19#define res_pair(text) \ 45#define res_pair(text) \
20 do { \ 46 do { \
21 AV *av = newAV (); \ 47 AV *av = newAV (); \
32 av_push (av, newSVpv (text, 0)); \ 58 av_push (av, newSVpv (text, 0)); \
33 av_push (about, newRV_noinc ((SV *)av)); \ 59 av_push (about, newRV_noinc ((SV *)av)); \
34 } while (0) 60 } while (0)
35 61
36#define res_gv(sigil) \ 62#define res_gv(sigil) \
37 res_text (form ("in the global %c%s::%.*s", sigil, \ 63 res_text (form ("the global %c%s::%.*s", sigil, \
38 HvNAME (GvSTASH (sv)), \ 64 HvNAME (GvSTASH (sv)), \
39 GvNAMELEN (sv), \ 65 GvNAME_HEK (sv) ? GvNAMELEN (sv) : 11, \
40 GvNAME (sv) ? GvNAME (sv) : "<anonymous>")) 66 GvNAME_HEK (sv) ? GvNAME (sv) : "<anonymous>"))
41 67
42MODULE = Devel::FindRef PACKAGE = Devel::FindRef 68MODULE = Devel::FindRef PACKAGE = Devel::FindRef
43 69
44PROTOTYPES: ENABLE 70PROTOTYPES: ENABLE
45 71
46void 72void
47find_ (SV *target) 73find_ (SV *target_ref)
48 PPCODE: 74 PPCODE:
49{ 75{
50 SV *arena, *targ; 76 SV *arena, *targ;
51 U32 rmagical; 77 U32 rmagical;
52 int i; 78 int i;
53 AV *about = newAV (); 79 AV *about = newAV ();
54 AV *excl = newAV (); 80 AV *excl = newAV ();
55 81
56 if (!SvROK (target)) 82 if (!SvROK (target_ref))
57 croak ("find expects a reference to a perl value"); 83 croak ("find expects a reference to a perl value");
58 84
59 targ = SvRV (target); 85 targ = SvRV (target_ref);
60 86
61 for (arena = PL_sv_arenaroot; arena; arena = SvANY (arena)) 87 if (SvIMMORTAL (targ))
62 { 88 {
63 UV idx = SvREFCNT (arena); 89 if (targ == &PL_sv_undef)
64 90 res_text ("the immortal 'undef' value");
65 /* Remember that the zeroth slot is used as the pointer onwards, so don't 91 else if (targ == &PL_sv_yes)
66 include it. */ 92 res_text ("the immortal 'yes' value");
67 while (--idx > 0) 93 else if (targ == &PL_sv_no)
94 res_text ("the immortal 'no' value");
95 else if (targ == &PL_sv_placeholder)
96 res_text ("the immortal placeholder value");
97 else
98 res_text ("some unknown immortal");
99 }
100 else
101 {
102 for (arena = PL_sv_arenaroot; arena; arena = SvANY (arena))
68 { 103 {
69 SV *sv = &arena [idx]; 104 UV idx = SvREFCNT (arena);
70 105
71 if (SvTYPE (sv) >= SVt_LAST) 106 /* Remember that the zeroth slot is used as the pointer onwards, so don't
72 continue; 107 include it. */
73 108 while (--idx > 0)
74 /* temporarily disable RMAGICAL, it can easily interfere with us */
75 if ((rmagical = SvRMAGICAL (sv)))
76 SvRMAGICAL_off (sv);
77
78 if (SvTYPE (sv) >= SVt_PVMG)
79 { 109 {
110 SV *sv = &arena [idx];
111
112 if (SvTYPE (sv) >= SVt_LAST)
113 continue;
114
115 /* temporarily disable RMAGICAL, it can easily interfere with us */
116 if ((rmagical = SvRMAGICAL (sv)))
117 SvRMAGICAL_off (sv);
118
80 if (SvTYPE (sv) == SVt_PVMG && SvPAD_OUR (sv)) 119 if (SvTYPE (sv) >= SVt_PVMG)
81 { 120 {
121#if !PERL_VERSION_ATLEAST (5,21,6)
122 if (SvTYPE (sv) == SVt_PVMG && SvPAD_OUR (sv))
123 {
82 /* I have no clue what this is */ 124 /* I have no clue what this is */
83 /* maybe some placeholder for our variables for eval? */ 125 /* maybe some placeholder for our variables for eval? */
84 /* it doesn't seem to reference anything, so we should be able to ignore it */ 126 /* it doesn't seem to reference anything, so we should be able to ignore it */
127 }
128 else
129#endif
130 if (SvMAGICAL (sv)) /* name-pads use SvMAGIC for other purposes */
131 {
132 MAGIC *mg = SvMAGIC (sv);
133
134 while (mg)
135 {
136 if (mg->mg_obj == targ && mg->mg_flags & MGf_REFCOUNTED)
137 res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type));
138
139 if ((SV *)mg->mg_ptr == targ)
140 res_pair (form ("%sreferenced (in mg_ptr) by '%c' type magic attached to",
141 mg->mg_len == HEf_SVKEY ? "" : "possibly ",
142 mg->mg_type));
143
144 mg = mg->mg_moremagic;
145 }
146 }
147 }
148
149 if (SvROK (sv))
150 {
151 if (SvRV (sv) == targ && !SvWEAKREF (sv) && sv != target_ref)
152 res_pair ("referenced by");
85 } 153 }
86 else 154 else
155 switch (SvTYPE (sv))
87 { 156 {
88 MAGIC *mg = SvMAGIC (sv); 157 case SVt_PVAV:
158 if (AvREAL (sv))
159 for (i = AvFILLp (sv) + 1; i--; )
160 if (AvARRAY (sv)[i] == targ)
161 res_pair (form ("the array element %d of", i));
89 162
90 while (mg) 163 break;
164
165 case SVt_PVHV:
166 if (hv_iterinit ((HV *)sv))
167 {
168 HE *he;
169
170 while ((he = hv_iternext ((HV *)sv)))
171 if (HeVAL (he) == targ)
172 res_pair (form ("the hash member '%.*s' of", HeKLEN (he), HeKEY (he)));
173 }
174
175 break;
176
177 case SVt_PVCV:
91 { 178 {
92 if (mg->mg_obj == targ) 179 PADLIST *padlist = CvPADLIST (sv);
93 res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type));
94 180
95 if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED) 181 if (padlist)
96 res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type)); 182 {
183 int depth = CvDEPTH (sv);
97 184
98 mg = mg->mg_moremagic; 185 /* Anonymous subs have a padlist but zero depth */
186 /* some hacks switch CvANON off, so we just blindly assume a minimum of 1 */
187 if (!depth && !PERL_VERSION_ATLEAST (5,21,6))
188 depth = 1;
189
190 while (depth)
191 {
192 PAD *pad = PadlistARRAY (padlist)[depth];
193
194 av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads themselves from being found */
195
196 /* The 0th pad slot is @_ */
197 if (PadARRAY (pad)[0] == targ)
198 res_pair ("the argument array for");
199
200 for (i = PadMAX (pad) + 1; --i; )
201 if (PadARRAY (pad)[i] == targ)
202 {
203 /* Values from constant functions are stored in the pad without any name */
204 PADNAME *name = padnamelist_fetch (PadlistNAMES (padlist), i);
205
206 if (name && PadnamePV (name) && *PadnamePV (name))
207 res_pair (form ("the lexical '%s' in", PadnamePV (name)));
208 else
209 res_pair ("an unnamed lexical in");
210 }
211
212 --depth;
213 }
214 }
215
216 if (CvCONST (sv) && (SV*)CvXSUBANY (sv).any_ptr == targ)
217 res_pair ("the constant value of");
218
219 if (!CvWEAKOUTSIDE (sv) && (SV*)CvOUTSIDE (sv) == targ)
220 res_pair ("the containing scope for");
221
222 if (sv == targ && CvANON (sv))
223 if (CvSTART (sv)
224 && CvSTART (sv)->op_type == OP_NEXTSTATE
225 && CopLINE ((COP *)CvSTART (sv)))
226 res_text (form ("the closure created at %s:%d",
227 CopFILE ((COP *)CvSTART (sv)) ? CopFILE ((COP *)CvSTART (sv)) : "<unknown>",
228 CopLINE ((COP *)CvSTART (sv))));
229 else
230 res_text (form ("the closure created somewhere in file %s (PLEASE REPORT!)",
231 CvFILE (sv) ? CvFILE (sv) : "<unknown>"));
99 } 232 }
233
234 break;
235
236 case SVt_PVGV:
237 if (GvGP (sv))
238 {
239 if (GvSV (sv) == (SV *)targ) res_gv ('$');
240 if (GvAV (sv) == (AV *)targ) res_gv ('@');
241 if (GvHV (sv) == (HV *)targ) res_gv ('%');
242 if (GvCV (sv) == (CV *)targ) res_gv ('&');
243 }
244
245 break;
246
247 case SVt_PVLV:
248 if (LvTARG (sv) == targ)
249 {
250 if (LvTYPE (sv) == 'y')
251 {
252 MAGIC *mg = mg_find (sv, PERL_MAGIC_defelem);
253
254 if (mg && mg->mg_obj)
255 res_pair (form ("the target for the lvalue hash element '%.*s',",
256 (int)SvCUR (mg->mg_obj), SvPV_nolen (mg->mg_obj)));
257 else
258 res_pair (form ("the target for the lvalue array element #%d,", LvTARGOFF (sv)));
259 }
260 else
261 res_pair (form ("an lvalue reference target (type '%c', ofs %d, len %d),",
262 LvTYPE (sv), LvTARGOFF (sv), LvTARGLEN (sv)));
263 }
264
265 break;
100 } 266 }
267
268 if (rmagical)
269 SvRMAGICAL_on (sv);
101 } 270 }
102
103 if (SvROK (sv) && !SvWEAKREF (sv))
104 {
105 if (sv != target && SvRV (sv) == targ && !SvWEAKREF (sv))
106 res_pair ("referenced by");
107 }
108 else
109 switch (SvTYPE (sv))
110 {
111 case SVt_PVAV:
112 if (AvREAL (sv))
113 for (i = AvFILLp (sv) + 1; i--; )
114 if (AvARRAY (sv)[i] == targ)
115 res_pair (form ("in array element %d of", i));
116
117 break;
118
119 case SVt_PVHV:
120 if (hv_iterinit ((HV *)sv))
121 {
122 HE *he;
123
124 while ((he = hv_iternext ((HV *)sv)))
125 if (HeVAL (he) == targ)
126 res_pair (form ("in the member '%.*s' of", HeKLEN (he), HeKEY (he)));
127 }
128
129 break;
130
131 case SVt_PVCV:
132 {
133 int depth = CvDEPTH (sv);
134
135 /* Anonymous subs have a padlist but zero depth */
136 if (CvANON (sv) && !depth && CvPADLIST (sv))
137 depth = 1;
138
139 if (depth)
140 {
141 AV *padlist = CvPADLIST (sv);
142
143 while (depth)
144 {
145 AV *pad = (AV *)AvARRAY (padlist)[depth];
146
147 av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads themselves from being found */
148
149 /* The 0th pad slot is @_ */
150 if (AvARRAY (pad)[0] == targ)
151 res_pair ("the argument array for");
152
153 for (i = AvFILLp (pad) + 1; --i; )
154 if (AvARRAY (pad)[i] == targ)
155 {
156 /* Values from constant functions are stored in the pad without any name */
157 SV *name_sv = AvARRAY (AvARRAY (padlist)[0])[i];
158
159 if (name_sv && SvPOK (name_sv))
160 res_pair (form ("in the lexical '%s' in", SvPVX (name_sv)));
161 else
162 res_pair ("in an unnamed lexical in");
163 }
164
165 --depth;
166 }
167 }
168
169 if (CvCONST (sv) && (SV*)CvXSUBANY (sv).any_ptr == targ)
170 res_pair ("the constant value of");
171
172 if (!CvWEAKOUTSIDE (sv) && (SV*)CvOUTSIDE (sv) == targ)
173 res_pair ("the containing scope for");
174
175 if (sv == targ && CvANON (sv))
176 if (CvSTART (sv)
177 && CvSTART (sv)->op_type == OP_NEXTSTATE
178 && CopLINE ((COP *)CvSTART (sv)))
179 res_text (form ("the closure created at %s:%d",
180 CopFILE ((COP *)CvSTART (sv)) ? CopFILE ((COP *)CvSTART (sv)) : "<unknown>",
181 CopLINE ((COP *)CvSTART (sv))));
182 else
183 res_text (form ("the closure created somewhere in file %s (PLEASE REPORT!)",
184 CvFILE (sv) ? CvFILE (sv) : "<unknown>"));
185 }
186
187 break;
188
189 case SVt_PVGV:
190 if (GvGP (sv))
191 {
192 if (GvSV (sv) == (SV *)targ) res_gv ('$');
193 if (GvAV (sv) == (AV *)targ) res_gv ('@');
194 if (GvHV (sv) == (HV *)targ) res_gv ('%');
195 if (GvCV (sv) == (CV *)targ) res_gv ('&');
196 }
197
198 break;
199 }
200
201 if (rmagical)
202 SvRMAGICAL_on (sv);
203 } 271 }
272
273 /* look at the mortalise stack of the current coroutine */
274 for (i = 0; i <= PL_tmps_ix; ++i)
275 if (PL_tmps_stack [i] == targ)
276 res_text ("a temporary on the stack");
277
278 if (targ == (SV*)PL_main_cv)
279 res_text ("the main body of the program");
204 } 280 }
205
206 if (targ == (SV*)PL_main_cv)
207 res_text ("the main body of the program");
208 281
209 EXTEND (SP, 2); 282 EXTEND (SP, 2);
210 PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); 283 PUSHs (sv_2mortal (newRV_noinc ((SV *)about)));
211 PUSHs (sv_2mortal (newRV_noinc ((SV *)excl))); 284 PUSHs (sv_2mortal (newRV_noinc ((SV *)excl)));
212} 285}
219 RETVAL 292 RETVAL
220 293
221UV 294UV
222ref2ptr (SV *rv) 295ref2ptr (SV *rv)
223 CODE: 296 CODE:
297 if (!SvROK (rv))
298 croak ("argument to Devel::FindRef::ref2ptr must be a reference");
224 RETVAL = PTR2UV (SvRV (rv)); 299 RETVAL = PTR2UV (SvRV (rv));
225 OUTPUT: 300 OUTPUT:
226 RETVAL 301 RETVAL
227 302
228U32 303U32
229_refcnt (SV *rv) 304_refcnt (SV *rv)
230 CODE: 305 CODE:
306 if (!SvROK (rv))
307 croak ("argument to Devel::FindRef::_refcnt must be a reference");
231 RETVAL = SvREFCNT (SvRV (rv)); 308 RETVAL = SvREFCNT (SvRV (rv));
232 OUTPUT: 309 OUTPUT:
233 RETVAL 310 RETVAL

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines