1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
|
|
5 | #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 | #if !PERL_VERSION_ATLEAST (5,10,0) |
|
|
16 | # define SvPAD_OUR(dummy) 0 |
|
|
17 | #endif |
|
|
18 | |
5 | #define res_pair(text) \ |
19 | #define res_pair(text) \ |
6 | { \ |
20 | do { \ |
7 | AV *av = newAV (); \ |
21 | AV *av = newAV (); \ |
8 | av_push (av, newSVpv (text, 0)); \ |
22 | av_push (av, newSVpv (text, 0)); \ |
|
|
23 | if (rmagical) SvRMAGICAL_on (sv); \ |
9 | av_push (av, newRV_inc (sv)); \ |
24 | av_push (av, sv_rvweaken (newRV_inc (sv))); \ |
|
|
25 | if (rmagical) SvRMAGICAL_off (sv); \ |
10 | av_push (about, newRV_noinc ((SV *)av)); \ |
26 | av_push (about, newRV_noinc ((SV *)av)); \ |
11 | } |
27 | } while (0) |
12 | |
28 | |
13 | #define res_gv(sigil) \ |
29 | #define res_text(text) \ |
14 | { \ |
30 | do { \ |
15 | AV *av = newAV (); \ |
31 | AV *av = newAV (); \ |
16 | av_push (av, newSVpv (form ("in the global %c%s::%.*s", sigil, \ |
32 | av_push (av, newSVpv (text, 0)); \ |
17 | HvNAME (GvSTASH (sv)), \ |
|
|
18 | GvNAMELEN (sv), GvNAME (sv) ? GvNAME (sv) : "<anonymous>"), \ |
|
|
19 | 0)); \ |
|
|
20 | av_push (about, newRV_noinc ((SV *)av)); \ |
33 | av_push (about, newRV_noinc ((SV *)av)); \ |
21 | } |
34 | } while (0) |
|
|
35 | |
|
|
36 | #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>")) |
22 | |
41 | |
23 | MODULE = Devel::FindRef PACKAGE = Devel::FindRef |
42 | MODULE = Devel::FindRef PACKAGE = Devel::FindRef |
24 | |
43 | |
25 | PROTOTYPES: ENABLE |
44 | PROTOTYPES: ENABLE |
26 | |
45 | |
… | |
… | |
34 | void |
53 | void |
35 | find_ (SV *target) |
54 | find_ (SV *target) |
36 | PPCODE: |
55 | PPCODE: |
37 | { |
56 | { |
38 | SV *arena, *targ; |
57 | SV *arena, *targ; |
39 | int rmagical, i; |
58 | U32 rmagical; |
|
|
59 | int i; |
40 | AV *about = newAV (); |
60 | AV *about = newAV (); |
41 | AV *excl = newAV (); |
61 | AV *excl = newAV (); |
42 | |
62 | |
43 | if (!SvROK (target)) |
63 | if (!SvROK (target)) |
44 | croak ("about expects a reference to a perl value"); |
64 | croak ("find expects a reference to a perl value"); |
45 | |
65 | |
46 | targ = SvRV (target); |
66 | targ = SvRV (target); |
47 | |
67 | |
48 | for (arena = PL_sv_arenaroot; arena; arena = SvANY (arena)) |
68 | for (arena = PL_sv_arenaroot; arena; arena = SvANY (arena)) |
49 | { |
69 | { |
… | |
… | |
52 | /* Remember that the zeroth slot is used as the pointer onwards, so don't |
72 | /* Remember that the zeroth slot is used as the pointer onwards, so don't |
53 | include it. */ |
73 | include it. */ |
54 | while (--idx > 0) |
74 | while (--idx > 0) |
55 | { |
75 | { |
56 | SV *sv = &arena [idx]; |
76 | SV *sv = &arena [idx]; |
57 | |
77 | |
|
|
78 | if (SvTYPE (sv) >= SVt_LAST) |
|
|
79 | continue; |
|
|
80 | |
|
|
81 | /* temporarily disable RMAGICAL, it can easily interfere with us */ |
58 | if ((rmagical = SvRMAGICAL (sv))) |
82 | if ((rmagical = SvRMAGICAL (sv))) |
59 | SvRMAGICAL_off (sv); |
83 | SvRMAGICAL_off (sv); |
60 | |
84 | |
61 | //TODO: magic |
|
|
62 | |
|
|
63 | switch (SvTYPE (sv)) |
85 | if (SvTYPE (sv) >= SVt_PVMG) |
64 | { |
86 | { |
65 | case SVt_RV: |
87 | if (SvTYPE (sv) == SVt_PVMG && SvPAD_OUR (sv)) |
66 | if (sv != target && SvRV (sv) == targ) |
|
|
67 | res_pair ("referenced by"); |
|
|
68 | break; |
|
|
69 | |
|
|
70 | case SVt_PVAV: |
|
|
71 | if (AvREAL (sv)) |
|
|
72 | for (i = AvFILLp (sv) + 1; i--; ) |
|
|
73 | if (AvARRAY (sv)[i] == targ) |
|
|
74 | res_pair (form ("in array element %d of", i)); |
|
|
75 | break; |
|
|
76 | |
|
|
77 | case SVt_PVHV: |
|
|
78 | if (hv_iterinit ((HV *)sv)) |
|
|
79 | { |
|
|
80 | HE *he; |
|
|
81 | while ((he = hv_iternext ((HV *)sv))) |
|
|
82 | if (HeVAL (he) == targ) |
|
|
83 | res_pair (form ("in the member '%.*s' of", HeKLEN (he), HeKEY (he))); |
|
|
84 | } |
|
|
85 | break; |
|
|
86 | |
|
|
87 | case SVt_PVGV: |
|
|
88 | if (GvGP (sv)) |
|
|
89 | { |
|
|
90 | if (GvSV (sv) == targ) |
|
|
91 | res_gv ('$'); |
|
|
92 | if (GvAV (sv) == (AV *)targ) |
|
|
93 | res_gv ('@'); |
|
|
94 | if (GvHV (sv) == (HV *)targ) |
|
|
95 | res_gv ('%'); |
|
|
96 | if (GvCV (sv) == (CV *)targ) |
|
|
97 | res_gv ('&'); |
|
|
98 | } |
|
|
99 | break; |
|
|
100 | |
|
|
101 | case SVt_PVCV: |
|
|
102 | { |
88 | { |
103 | int depth = CvDEPTH (sv); |
89 | /* I have no clue what this is */ |
|
|
90 | /* maybe some placeholder for our variables for eval? */ |
|
|
91 | /* it doesn't seem to reference anything, so we should be able to ignore it */ |
|
|
92 | } |
|
|
93 | else |
|
|
94 | { |
|
|
95 | MAGIC *mg = SvMAGIC (sv); |
|
|
96 | |
104 | if (depth) |
97 | while (mg) |
105 | { |
98 | { |
106 | AV *padlist = CvPADLIST (sv); |
99 | if (mg->mg_obj == targ) |
107 | while (depth) |
100 | res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type)); |
108 | { |
|
|
109 | AV *pad = (AV *)AvARRAY (padlist)[depth]; |
|
|
110 | av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads from being found */ |
|
|
111 | for (i = AvFILLp (pad); i--; ) |
|
|
112 | if (AvARRAY (pad)[i] == targ) |
|
|
113 | res_pair (form ("in the lexical '%s' in", SvPVX (AvARRAY (AvARRAY (padlist)[0])[i]))); |
|
|
114 | |
101 | |
115 | --depth; |
102 | if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED) |
116 | } |
103 | res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type)); |
|
|
104 | |
|
|
105 | mg = mg->mg_moremagic; |
117 | } |
106 | } |
118 | } |
107 | } |
119 | break; |
|
|
120 | } |
108 | } |
121 | |
109 | |
|
|
110 | if (SvROK (sv)) |
|
|
111 | { |
|
|
112 | if (sv != target && SvRV (sv) == targ && !SvWEAKREF (sv)) |
|
|
113 | res_pair ("referenced by"); |
|
|
114 | } |
|
|
115 | else |
|
|
116 | switch (SvTYPE (sv)) |
|
|
117 | { |
|
|
118 | case SVt_PVAV: |
|
|
119 | if (AvREAL (sv)) |
|
|
120 | for (i = AvFILLp (sv) + 1; i--; ) |
|
|
121 | if (AvARRAY (sv)[i] == targ) |
|
|
122 | res_pair (form ("in array element %d of", i)); |
|
|
123 | |
|
|
124 | break; |
|
|
125 | |
|
|
126 | case SVt_PVHV: |
|
|
127 | if (hv_iterinit ((HV *)sv)) |
|
|
128 | { |
|
|
129 | HE *he; |
|
|
130 | |
|
|
131 | while ((he = hv_iternext ((HV *)sv))) |
|
|
132 | if (HeVAL (he) == targ) |
|
|
133 | res_pair (form ("in the member '%.*s' of", HeKLEN (he), HeKEY (he))); |
|
|
134 | } |
|
|
135 | |
|
|
136 | break; |
|
|
137 | |
|
|
138 | case SVt_PVCV: |
|
|
139 | { |
|
|
140 | int depth = CvDEPTH (sv); |
|
|
141 | |
|
|
142 | /* Anonymous subs have a padlist but zero depth */ |
|
|
143 | if (CvANON (sv) && !depth && CvPADLIST (sv)) |
|
|
144 | depth = 1; |
|
|
145 | |
|
|
146 | if (depth) |
|
|
147 | { |
|
|
148 | AV *padlist = CvPADLIST (sv); |
|
|
149 | |
|
|
150 | while (depth) |
|
|
151 | { |
|
|
152 | AV *pad = (AV *)AvARRAY (padlist)[depth]; |
|
|
153 | |
|
|
154 | av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads themselves from being found */ |
|
|
155 | |
|
|
156 | for (i = AvFILLp (pad) + 1; i--; ) |
|
|
157 | if (AvARRAY (pad)[i] == targ) |
|
|
158 | { |
|
|
159 | /* Values from constant functions are stored in the pad without any name */ |
|
|
160 | SV *name_sv = AvARRAY (AvARRAY (padlist)[0])[i]; |
|
|
161 | |
|
|
162 | if (name_sv && SvPOK (name_sv)) |
|
|
163 | res_pair (form ("in the lexical '%s' in", SvPVX (name_sv))); |
|
|
164 | else |
|
|
165 | res_pair ("in an unnamed lexical in"); |
|
|
166 | } |
|
|
167 | |
|
|
168 | --depth; |
|
|
169 | } |
|
|
170 | } |
|
|
171 | |
|
|
172 | if (CvCONST (sv) && (SV*)CvXSUBANY (sv).any_ptr == targ) |
|
|
173 | res_pair ("the constant value of"); |
|
|
174 | |
|
|
175 | if (!CvWEAKOUTSIDE (sv) && (SV*)CvOUTSIDE (sv) == targ) |
|
|
176 | res_pair ("the containing scope for"); |
|
|
177 | |
|
|
178 | if (sv == targ && CvANON (sv)) |
|
|
179 | if (CvSTART (sv) |
|
|
180 | && CvSTART (sv)->op_type == OP_NEXTSTATE |
|
|
181 | && CopLINE ((COP *)CvSTART (sv))) |
|
|
182 | res_text (form ("the closure created at %s:%d", |
|
|
183 | CopFILE ((COP *)CvSTART (sv)) ? CopFILE ((COP *)CvSTART (sv)) : "<unknown>", |
|
|
184 | CopLINE ((COP *)CvSTART (sv)))); |
|
|
185 | else |
|
|
186 | res_text (form ("the closure created somewhere in file %s (PLEASE REPORT!)", |
|
|
187 | CvFILE (sv) ? CvFILE (sv) : "<unknown>")); |
|
|
188 | } |
|
|
189 | |
|
|
190 | break; |
|
|
191 | |
|
|
192 | case SVt_PVGV: |
|
|
193 | if (GvGP (sv)) |
|
|
194 | { |
|
|
195 | if (GvSV (sv) == (SV *)targ) res_gv ('$'); |
|
|
196 | if (GvAV (sv) == (AV *)targ) res_gv ('@'); |
|
|
197 | if (GvHV (sv) == (HV *)targ) res_gv ('%'); |
|
|
198 | if (GvCV (sv) == (CV *)targ) res_gv ('&'); |
|
|
199 | } |
|
|
200 | |
|
|
201 | break; |
|
|
202 | } |
|
|
203 | |
122 | if (rmagical) |
204 | if (rmagical) |
123 | SvRMAGICAL_off (sv); |
205 | SvRMAGICAL_on (sv); |
124 | } |
206 | } |
125 | } |
207 | } |
126 | |
208 | |
127 | EXTEND (SP, 2); |
209 | EXTEND (SP, 2); |
128 | PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); |
210 | PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); |