ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Devel-FindRef/FindRef.xs
Revision: 1.24
Committed: Fri May 16 07:43:52 2014 UTC (10 years ago) by root
Branch: MAIN
CVS Tags: rel-1_43
Changes since 1.23: +8 -7 lines
Log Message:
1.43

File Contents

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