… | |
… | |
10 | |
10 | |
11 | #if !PERL_VERSION_ATLEAST (5,8,9) |
11 | #if !PERL_VERSION_ATLEAST (5,8,9) |
12 | # define SVt_LAST 16 |
12 | # define SVt_LAST 16 |
13 | #endif |
13 | #endif |
14 | |
14 | |
|
|
15 | #if !PERL_VERSION_ATLEAST (5,10,0) |
|
|
16 | # define SvPAD_OUR(dummy) 0 |
|
|
17 | #endif |
|
|
18 | |
15 | #define res_pair(text) \ |
19 | #define res_pair(text) \ |
16 | { \ |
20 | do { \ |
17 | AV *av = newAV (); \ |
21 | AV *av = newAV (); \ |
18 | av_push (av, newSVpv (text, 0)); \ |
22 | av_push (av, newSVpv (text, 0)); \ |
|
|
23 | if (rmagical) SvRMAGICAL_on (sv); \ |
19 | av_push (av, sv_rvweaken (newRV_inc (sv))); \ |
24 | av_push (av, sv_rvweaken (newRV_inc (sv))); \ |
|
|
25 | if (rmagical) SvRMAGICAL_off (sv); \ |
20 | av_push (about, newRV_noinc ((SV *)av)); \ |
26 | av_push (about, newRV_noinc ((SV *)av)); \ |
21 | } |
27 | } while (0) |
|
|
28 | |
|
|
29 | #define res_text(text) \ |
|
|
30 | do { \ |
|
|
31 | AV *av = newAV (); \ |
|
|
32 | av_push (av, newSVpv (text, 0)); \ |
|
|
33 | av_push (about, newRV_noinc ((SV *)av)); \ |
|
|
34 | } while (0) |
22 | |
35 | |
23 | #define res_gv(sigil) \ |
36 | #define res_gv(sigil) \ |
24 | { \ |
|
|
25 | AV *av = newAV (); \ |
|
|
26 | av_push (av, newSVpv (form ("in the global %c%s::%.*s", sigil, \ |
37 | res_text (form ("in the global %c%s::%.*s", sigil, \ |
27 | HvNAME (GvSTASH (sv)), \ |
38 | HvNAME (GvSTASH (sv)), \ |
|
|
39 | GvNAMELEN (sv), \ |
28 | GvNAMELEN (sv), GvNAME (sv) ? GvNAME (sv) : "<anonymous>"), \ |
40 | GvNAME (sv) ? GvNAME (sv) : "<anonymous>")) |
29 | 0)); \ |
|
|
30 | av_push (about, newRV_noinc ((SV *)av)); \ |
|
|
31 | } |
|
|
32 | |
41 | |
33 | MODULE = Devel::FindRef PACKAGE = Devel::FindRef |
42 | MODULE = Devel::FindRef PACKAGE = Devel::FindRef |
34 | |
43 | |
35 | PROTOTYPES: ENABLE |
44 | PROTOTYPES: ENABLE |
36 | |
45 | |
37 | SV * |
|
|
38 | ptr2ref (IV ptr) |
|
|
39 | CODE: |
|
|
40 | RETVAL = newRV_inc (INT2PTR (SV *, ptr)); |
|
|
41 | OUTPUT: |
|
|
42 | RETVAL |
|
|
43 | |
|
|
44 | void |
46 | void |
45 | find_ (SV *target) |
47 | find_ (SV *target_ref) |
46 | PPCODE: |
48 | PPCODE: |
47 | { |
49 | { |
48 | SV *arena, *targ; |
50 | SV *arena, *targ; |
49 | int rmagical, i; |
51 | U32 rmagical; |
|
|
52 | int i; |
50 | AV *about = newAV (); |
53 | AV *about = newAV (); |
51 | AV *excl = newAV (); |
54 | AV *excl = newAV (); |
52 | |
55 | |
53 | if (!SvROK (target)) |
56 | if (!SvROK (target_ref)) |
54 | croak ("find expects a reference to a perl value"); |
57 | croak ("find expects a reference to a perl value"); |
55 | |
58 | |
56 | targ = SvRV (target); |
59 | targ = SvRV (target_ref); |
57 | |
60 | |
58 | for (arena = PL_sv_arenaroot; arena; arena = SvANY (arena)) |
61 | for (arena = PL_sv_arenaroot; arena; arena = SvANY (arena)) |
59 | { |
62 | { |
60 | UV idx = SvREFCNT (arena); |
63 | UV idx = SvREFCNT (arena); |
61 | |
64 | |
… | |
… | |
72 | if ((rmagical = SvRMAGICAL (sv))) |
75 | if ((rmagical = SvRMAGICAL (sv))) |
73 | SvRMAGICAL_off (sv); |
76 | SvRMAGICAL_off (sv); |
74 | |
77 | |
75 | if (SvTYPE (sv) >= SVt_PVMG) |
78 | if (SvTYPE (sv) >= SVt_PVMG) |
76 | { |
79 | { |
77 | MAGIC *mg = SvMAGIC (sv); |
80 | if (SvTYPE (sv) == SVt_PVMG && SvPAD_OUR (sv)) |
78 | while (mg) |
|
|
79 | { |
81 | { |
|
|
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 | |
|
|
90 | while (mg) |
|
|
91 | { |
80 | if (mg->mg_obj == targ) |
92 | if (mg->mg_obj == targ) |
81 | res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type)); |
93 | res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type)); |
82 | |
94 | |
83 | if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED) |
95 | if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED) |
84 | res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type)); |
96 | res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type)); |
85 | |
97 | |
86 | mg = mg->mg_moremagic; |
98 | mg = mg->mg_moremagic; |
|
|
99 | } |
87 | } |
100 | } |
88 | } |
101 | } |
89 | |
102 | |
90 | if (SvROK (sv)) |
103 | if (SvROK (sv)) |
91 | { |
104 | { |
92 | if (sv != target && SvRV (sv) == targ && !SvWEAKREF (sv)) |
105 | if (SvRV (sv) == targ && !SvWEAKREF (sv) && sv != target_ref) |
93 | res_pair ("referenced by"); |
106 | res_pair ("referenced by"); |
94 | } |
107 | } |
95 | else |
108 | else |
96 | switch (SvTYPE (sv)) |
109 | switch (SvTYPE (sv)) |
97 | { |
110 | { |
… | |
… | |
117 | |
130 | |
118 | case SVt_PVCV: |
131 | case SVt_PVCV: |
119 | { |
132 | { |
120 | int depth = CvDEPTH (sv); |
133 | int depth = CvDEPTH (sv); |
121 | |
134 | |
|
|
135 | /* Anonymous subs have a padlist but zero depth */ |
|
|
136 | if (CvANON (sv) && !depth && CvPADLIST (sv)) |
|
|
137 | depth = 1; |
|
|
138 | |
122 | if (depth) |
139 | if (depth) |
123 | { |
140 | { |
124 | AV *padlist = CvPADLIST (sv); |
141 | AV *padlist = CvPADLIST (sv); |
125 | |
142 | |
126 | while (depth) |
143 | while (depth) |
127 | { |
144 | { |
128 | AV *pad = (AV *)AvARRAY (padlist)[depth]; |
145 | AV *pad = (AV *)AvARRAY (padlist)[depth]; |
129 | |
146 | |
130 | av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads themselves from being found */ |
147 | av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads themselves from being found */ |
131 | |
148 | |
|
|
149 | /* The 0th pad slot is @_ */ |
|
|
150 | if (AvARRAY (pad)[0] == targ) |
|
|
151 | res_pair ("the argument array for"); |
|
|
152 | |
132 | for (i = AvFILLp (pad); i--; ) |
153 | for (i = AvFILLp (pad) + 1; --i; ) |
133 | if (AvARRAY (pad)[i] == targ) |
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)) |
134 | res_pair (form ("in the lexical '%s' in", SvPVX (AvARRAY (AvARRAY (padlist)[0])[i]))); |
160 | res_pair (form ("in the lexical '%s' in", SvPVX (name_sv))); |
|
|
161 | else |
|
|
162 | res_pair ("in an unnamed lexical in"); |
|
|
163 | } |
135 | |
164 | |
136 | --depth; |
165 | --depth; |
137 | } |
166 | } |
138 | } |
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>")); |
139 | } |
185 | } |
140 | |
186 | |
141 | break; |
187 | break; |
142 | |
188 | |
143 | case SVt_PVGV: |
189 | case SVt_PVGV: |
… | |
… | |
155 | if (rmagical) |
201 | if (rmagical) |
156 | SvRMAGICAL_on (sv); |
202 | SvRMAGICAL_on (sv); |
157 | } |
203 | } |
158 | } |
204 | } |
159 | |
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 | |
160 | EXTEND (SP, 2); |
214 | EXTEND (SP, 2); |
161 | PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); |
215 | PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); |
162 | PUSHs (sv_2mortal (newRV_noinc ((SV *)excl))); |
216 | PUSHs (sv_2mortal (newRV_noinc ((SV *)excl))); |
163 | } |
217 | } |
164 | |
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 |