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 | |
27 | SV * |
|
|
28 | ptr2ref (IV ptr) |
|
|
29 | CODE: |
|
|
30 | RETVAL = newRV_inc (INT2PTR (SV *, ptr)); |
|
|
31 | OUTPUT: |
|
|
32 | RETVAL |
|
|
33 | |
|
|
34 | void |
46 | void |
35 | find_ (SV *target) |
47 | find_ (SV *target_ref) |
36 | PPCODE: |
48 | PPCODE: |
37 | { |
49 | { |
38 | SV *arena, *targ; |
50 | SV *arena, *targ; |
39 | int rmagical, i; |
51 | U32 rmagical; |
|
|
52 | int i; |
40 | AV *about = newAV (); |
53 | AV *about = newAV (); |
41 | AV *excl = newAV (); |
54 | AV *excl = newAV (); |
42 | |
55 | |
43 | if (!SvROK (target)) |
56 | if (!SvROK (target_ref)) |
44 | croak ("about expects a reference to a perl value"); |
57 | croak ("find expects a reference to a perl value"); |
45 | |
58 | |
46 | targ = SvRV (target); |
59 | targ = SvRV (target_ref); |
47 | |
60 | |
48 | for (arena = PL_sv_arenaroot; arena; arena = SvANY (arena)) |
61 | for (arena = PL_sv_arenaroot; arena; arena = SvANY (arena)) |
49 | { |
62 | { |
50 | UV idx = SvREFCNT (arena); |
63 | UV idx = SvREFCNT (arena); |
51 | |
64 | |
52 | /* Remember that the zeroth slot is used as the pointer onwards, so don't |
65 | /* Remember that the zeroth slot is used as the pointer onwards, so don't |
53 | include it. */ |
66 | include it. */ |
54 | while (--idx > 0) |
67 | while (--idx > 0) |
55 | { |
68 | { |
56 | SV *sv = &arena [idx]; |
69 | SV *sv = &arena [idx]; |
57 | |
70 | |
|
|
71 | if (SvTYPE (sv) >= SVt_LAST) |
|
|
72 | continue; |
|
|
73 | |
|
|
74 | /* temporarily disable RMAGICAL, it can easily interfere with us */ |
58 | if ((rmagical = SvRMAGICAL (sv))) |
75 | if ((rmagical = SvRMAGICAL (sv))) |
59 | SvRMAGICAL_off (sv); |
76 | SvRMAGICAL_off (sv); |
60 | |
77 | |
61 | //TODO: magic |
|
|
62 | |
|
|
63 | switch (SvTYPE (sv)) |
78 | if (SvTYPE (sv) >= SVt_PVMG) |
64 | { |
79 | { |
65 | case SVt_RV: |
80 | 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 | { |
81 | { |
103 | int depth = CvDEPTH (sv); |
82 | /* I have no clue what this is */ |
|
|
83 | /* maybe some placeholder for our variables for eval? */ |
|
|
84 | /* it doesn't seem to reference anything, so we should be able to ignore it */ |
|
|
85 | } |
|
|
86 | else |
|
|
87 | { |
|
|
88 | MAGIC *mg = SvMAGIC (sv); |
|
|
89 | |
104 | if (depth) |
90 | while (mg) |
105 | { |
91 | { |
106 | AV *padlist = CvPADLIST (sv); |
92 | if (mg->mg_obj == targ) |
107 | while (depth) |
93 | 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 | |
94 | |
115 | --depth; |
95 | if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED) |
116 | } |
96 | res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type)); |
|
|
97 | |
|
|
98 | mg = mg->mg_moremagic; |
117 | } |
99 | } |
118 | } |
100 | } |
119 | break; |
|
|
120 | } |
101 | } |
121 | |
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 | |
122 | if (rmagical) |
201 | if (rmagical) |
123 | SvRMAGICAL_off (sv); |
202 | SvRMAGICAL_on (sv); |
124 | } |
203 | } |
125 | } |
204 | } |
|
|
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"); |
126 | |
213 | |
127 | EXTEND (SP, 2); |
214 | EXTEND (SP, 2); |
128 | PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); |
215 | PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); |
129 | PUSHs (sv_2mortal (newRV_noinc ((SV *)excl))); |
216 | PUSHs (sv_2mortal (newRV_noinc ((SV *)excl))); |
130 | } |
217 | } |
131 | |
218 | |
|
|
219 | SV * |
|
|
220 | ptr2ref (UV ptr) |
|
|
221 | CODE: |
|
|
222 | RETVAL = newRV_inc (INT2PTR (SV *, ptr)); |
|
|
223 | OUTPUT: |
|
|
224 | RETVAL |
|
|
225 | |
|
|
226 | UV |
|
|
227 | ref2ptr (SV *rv) |
|
|
228 | CODE: |
|
|
229 | RETVAL = PTR2UV (SvRV (rv)); |
|
|
230 | OUTPUT: |
|
|
231 | RETVAL |
|
|
232 | |
|
|
233 | U32 |
|
|
234 | _refcnt (SV *rv) |
|
|
235 | CODE: |
|
|
236 | RETVAL = SvREFCNT (SvRV (rv)); |
|
|
237 | OUTPUT: |
|
|
238 | RETVAL |