ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Devel-FindRef/FindRef.xs
Revision: 1.12
Committed: Sat Jul 19 01:17:12 2008 UTC (15 years, 10 months ago) by root
Branch: MAIN
Changes since 1.11: +8 -1 lines
Log Message:
*** empty log message ***

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.6 #define res_pair(text) \
20 root 1.8 do { \
21 root 1.6 AV *av = newAV (); \
22     av_push (av, newSVpv (text, 0)); \
23 root 1.7 if (rmagical) SvRMAGICAL_on (sv); \
24 root 1.6 av_push (av, sv_rvweaken (newRV_inc (sv))); \
25 root 1.7 if (rmagical) SvRMAGICAL_off (sv); \
26 root 1.6 av_push (about, newRV_noinc ((SV *)av)); \
27 root 1.8 } while (0)
28 root 1.1
29 root 1.11 #define res_text(text) \
30 root 1.8 do { \
31 root 1.6 AV *av = newAV (); \
32 root 1.11 av_push (av, newSVpv (text, 0)); \
33 root 1.6 av_push (about, newRV_noinc ((SV *)av)); \
34 root 1.8 } while (0)
35 root 1.1
36 root 1.11 #define res_gv(sigil) \
37     res_text (form ("in the global %c%s::%.*s", sigil, \
38     HvNAME (GvSTASH (sv)), \
39     GvNAMELEN (sv), \
40     GvNAME (sv) ? GvNAME (sv) : "<anonymous>"))
41    
42 root 1.1 MODULE = Devel::FindRef PACKAGE = Devel::FindRef
43    
44     PROTOTYPES: ENABLE
45    
46     SV *
47 root 1.12 ptr2ref (UV ptr)
48 root 1.1 CODE:
49     RETVAL = newRV_inc (INT2PTR (SV *, ptr));
50     OUTPUT:
51     RETVAL
52    
53 root 1.12 UV
54     ref2ptr (SV *rv)
55     CODE:
56     RETVAL = PTR2UV (SvRV (rv));
57     OUTPUT:
58     RETVAL
59    
60 root 1.1 void
61     find_ (SV *target)
62     PPCODE:
63     {
64     SV *arena, *targ;
65 root 1.9 U32 rmagical;
66     int i;
67 root 1.1 AV *about = newAV ();
68     AV *excl = newAV ();
69    
70     if (!SvROK (target))
71 root 1.3 croak ("find expects a reference to a perl value");
72 root 1.1
73     targ = SvRV (target);
74    
75     for (arena = PL_sv_arenaroot; arena; arena = SvANY (arena))
76     {
77     UV idx = SvREFCNT (arena);
78    
79     /* Remember that the zeroth slot is used as the pointer onwards, so don't
80     include it. */
81     while (--idx > 0)
82     {
83     SV *sv = &arena [idx];
84 root 1.2
85     if (SvTYPE (sv) >= SVt_LAST)
86     continue;
87    
88     /* temporarily disable RMAGICAL, it can easily interfere with us */
89 root 1.1 if ((rmagical = SvRMAGICAL (sv)))
90     SvRMAGICAL_off (sv);
91    
92 root 1.2 if (SvTYPE (sv) >= SVt_PVMG)
93     {
94 root 1.10 if (SvTYPE (sv) == SVt_PVMG && SvPAD_OUR (sv))
95     {
96     /* I have no clue what this is */
97     /* maybe some placeholder for our variables for eval? */
98 root 1.11 /* it doesn't seem to reference anything, so we should be able to ignore it */
99 root 1.10 }
100     else
101 root 1.2 {
102 root 1.9 MAGIC *mg = SvMAGIC (sv);
103 root 1.6
104 root 1.9 while (mg)
105     {
106     if (mg->mg_obj == targ)
107     res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type));
108 root 1.2
109 root 1.9 if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED)
110     res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type));
111    
112     mg = mg->mg_moremagic;
113     }
114 root 1.2 }
115     }
116 root 1.1
117 root 1.6 if (SvROK (sv))
118 root 1.1 {
119 root 1.6 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 root 1.1
131 root 1.6 break;
132 root 1.1
133 root 1.6 case SVt_PVHV:
134     if (hv_iterinit ((HV *)sv))
135 root 1.1 {
136 root 1.6 HE *he;
137 root 1.1
138 root 1.6 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 root 1.1 }
142 root 1.2
143 root 1.6 break;
144    
145     case SVt_PVCV:
146 root 1.2 {
147 root 1.6 int depth = CvDEPTH (sv);
148    
149 root 1.8 /* Anonymous subs have a padlist but zero depth */
150 root 1.11 if (CvANON (sv) && !depth && CvPADLIST (sv))
151 root 1.7 depth = 1;
152    
153 root 1.6 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 root 1.7 for (i = AvFILLp (pad) + 1; i--; )
164 root 1.6 if (AvARRAY (pad)[i] == targ)
165 root 1.8 {
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 root 1.6
175     --depth;
176     }
177     }
178 root 1.7
179 root 1.8 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 root 1.7 res_pair ("the containing scope for");
184 root 1.11
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 root 1.2 }
196 root 1.6
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 root 1.1
211     if (rmagical)
212 root 1.5 SvRMAGICAL_on (sv);
213 root 1.1 }
214     }
215    
216     EXTEND (SP, 2);
217     PUSHs (sv_2mortal (newRV_noinc ((SV *)about)));
218     PUSHs (sv_2mortal (newRV_noinc ((SV *)excl)));
219     }
220