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.12 by root, Sat Jul 19 01:17:12 2008 UTC vs.
Revision 1.23 by root, Fri May 16 06:41:37 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines