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.10 by root, Fri Jul 11 15:33:48 2008 UTC vs.
Revision 1.17 by root, Wed Jul 1 08:25:04 2009 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines