… | |
… | |
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 (); \ |
… | |
… | |
34 | } while (0) |
39 | } while (0) |
35 | |
40 | |
36 | #define res_gv(sigil) \ |
41 | #define res_gv(sigil) \ |
37 | res_text (form ("in the global %c%s::%.*s", sigil, \ |
42 | res_text (form ("in the global %c%s::%.*s", sigil, \ |
38 | HvNAME (GvSTASH (sv)), \ |
43 | HvNAME (GvSTASH (sv)), \ |
39 | GvNAMELEN (sv), \ |
44 | GvNAME_HEK (sv) ? GvNAMELEN (sv) : 11, \ |
40 | GvNAME (sv) ? GvNAME (sv) : "<anonymous>")) |
45 | GvNAME_HEK (sv) ? GvNAME (sv) : "<anonymous>")) |
41 | |
46 | |
42 | MODULE = Devel::FindRef PACKAGE = Devel::FindRef |
47 | MODULE = Devel::FindRef PACKAGE = Devel::FindRef |
43 | |
48 | |
44 | PROTOTYPES: ENABLE |
49 | PROTOTYPES: ENABLE |
45 | |
50 | |
… | |
… | |
56 | if (!SvROK (target_ref)) |
61 | if (!SvROK (target_ref)) |
57 | croak ("find expects a reference to a perl value"); |
62 | croak ("find expects a reference to a perl value"); |
58 | |
63 | |
59 | targ = SvRV (target_ref); |
64 | targ = SvRV (target_ref); |
60 | |
65 | |
61 | for (arena = PL_sv_arenaroot; arena; arena = SvANY (arena)) |
66 | if (SvIMMORTAL (targ)) |
62 | { |
67 | { |
63 | UV idx = SvREFCNT (arena); |
68 | if (targ == &PL_sv_undef) |
64 | |
69 | res_text ("the immortal 'undef' value"); |
65 | /* Remember that the zeroth slot is used as the pointer onwards, so don't |
70 | else if (targ == &PL_sv_yes) |
66 | include it. */ |
71 | res_text ("the immortal 'yes' value"); |
67 | 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)) |
68 | { |
82 | { |
69 | SV *sv = &arena [idx]; |
83 | UV idx = SvREFCNT (arena); |
70 | |
84 | |
71 | if (SvTYPE (sv) >= SVt_LAST) |
85 | /* Remember that the zeroth slot is used as the pointer onwards, so don't |
72 | continue; |
86 | include it. */ |
73 | |
87 | 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 | { |
88 | { |
|
|
89 | SV *sv = &arena [idx]; |
|
|
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 | |
80 | if (SvTYPE (sv) == SVt_PVMG && SvPAD_OUR (sv)) |
98 | if (SvTYPE (sv) >= SVt_PVMG) |
81 | { |
99 | { |
|
|
100 | if (SvTYPE (sv) == SVt_PVMG && SvPAD_OUR (sv)) |
|
|
101 | { |
82 | /* I have no clue what this is */ |
102 | /* I have no clue what this is */ |
83 | /* maybe some placeholder for our variables for eval? */ |
103 | /* maybe some placeholder for our variables for eval? */ |
84 | /* 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"); |
85 | } |
129 | } |
86 | else |
130 | else |
|
|
131 | switch (SvTYPE (sv)) |
87 | { |
132 | { |
88 | 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)); |
89 | |
138 | |
90 | 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: |
91 | { |
154 | { |
92 | if (mg->mg_obj == targ) |
155 | int depth = CvDEPTH (sv); |
93 | res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type)); |
|
|
94 | |
156 | |
95 | if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED) |
157 | /* Anonymous subs have a padlist but zero depth */ |
96 | 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; |
97 | |
160 | |
98 | 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>")); |
99 | } |
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; |
100 | } |
241 | } |
|
|
242 | |
|
|
243 | if (rmagical) |
|
|
244 | SvRMAGICAL_on (sv); |
101 | } |
245 | } |
102 | |
|
|
103 | if (SvROK (sv)) |
|
|
104 | { |
|
|
105 | if (SvRV (sv) == targ && !SvWEAKREF (sv) && sv != target_ref) |
|
|
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 | } |
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"); |
204 | } |
255 | } |
205 | |
|
|
206 | /* look at the mortalise stack of the current coroutine */ |
|
|
207 | for (i = 0; i <= PL_tmps_ix; ++i) |
|
|
208 | if (PL_tmps_stack [i] == targ) |
|
|
209 | res_text ("a temporary on the stack"); |
|
|
210 | |
|
|
211 | if (targ == (SV*)PL_main_cv) |
|
|
212 | res_text ("the main body of the program"); |
|
|
213 | |
256 | |
214 | EXTEND (SP, 2); |
257 | EXTEND (SP, 2); |
215 | PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); |
258 | PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); |
216 | PUSHs (sv_2mortal (newRV_noinc ((SV *)excl))); |
259 | PUSHs (sv_2mortal (newRV_noinc ((SV *)excl))); |
217 | } |
260 | } |
… | |
… | |
224 | RETVAL |
267 | RETVAL |
225 | |
268 | |
226 | UV |
269 | UV |
227 | ref2ptr (SV *rv) |
270 | ref2ptr (SV *rv) |
228 | CODE: |
271 | CODE: |
|
|
272 | if (!SvROK (rv)) |
|
|
273 | croak ("argument to Devel::FindRef::ref2ptr must be a reference"); |
229 | RETVAL = PTR2UV (SvRV (rv)); |
274 | RETVAL = PTR2UV (SvRV (rv)); |
230 | OUTPUT: |
275 | OUTPUT: |
231 | RETVAL |
276 | RETVAL |
232 | |
277 | |
233 | U32 |
278 | U32 |
234 | _refcnt (SV *rv) |
279 | _refcnt (SV *rv) |
235 | CODE: |
280 | CODE: |
|
|
281 | if (!SvROK (rv)) |
|
|
282 | croak ("argument to Devel::FindRef::_refcnt must be a reference"); |
236 | RETVAL = SvREFCNT (SvRV (rv)); |
283 | RETVAL = SvREFCNT (SvRV (rv)); |
237 | OUTPUT: |
284 | OUTPUT: |
238 | RETVAL |
285 | RETVAL |