… | |
… | |
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 | |
… | |
… | |
44 | void |
53 | void |
45 | find_ (SV *target) |
54 | find_ (SV *target) |
46 | PPCODE: |
55 | PPCODE: |
47 | { |
56 | { |
48 | SV *arena, *targ; |
57 | SV *arena, *targ; |
49 | int rmagical, i; |
58 | U32 rmagical; |
|
|
59 | int i; |
50 | AV *about = newAV (); |
60 | AV *about = newAV (); |
51 | AV *excl = newAV (); |
61 | AV *excl = newAV (); |
52 | |
62 | |
53 | if (!SvROK (target)) |
63 | if (!SvROK (target)) |
54 | croak ("find expects a reference to a perl value"); |
64 | croak ("find expects a reference to a perl value"); |
… | |
… | |
72 | if ((rmagical = SvRMAGICAL (sv))) |
82 | if ((rmagical = SvRMAGICAL (sv))) |
73 | SvRMAGICAL_off (sv); |
83 | SvRMAGICAL_off (sv); |
74 | |
84 | |
75 | if (SvTYPE (sv) >= SVt_PVMG) |
85 | if (SvTYPE (sv) >= SVt_PVMG) |
76 | { |
86 | { |
77 | MAGIC *mg = SvMAGIC (sv); |
87 | if (SvTYPE (sv) == SVt_PVMG && SvPAD_OUR (sv)) |
78 | while (mg) |
|
|
79 | { |
88 | { |
|
|
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 | |
|
|
97 | while (mg) |
|
|
98 | { |
80 | if (mg->mg_obj == targ) |
99 | if (mg->mg_obj == targ) |
81 | res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type)); |
100 | res_pair (form ("referenced (in mg_obj) by '%c' type magic attached to", mg->mg_type)); |
82 | |
101 | |
83 | if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED) |
102 | 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)); |
103 | res_pair (form ("referenced (in mg_ptr) by '%c' type magic attached to", mg->mg_type)); |
85 | |
104 | |
86 | mg = mg->mg_moremagic; |
105 | mg = mg->mg_moremagic; |
|
|
106 | } |
87 | } |
107 | } |
88 | } |
108 | } |
89 | |
109 | |
90 | if (SvROK (sv)) |
110 | if (SvROK (sv)) |
91 | { |
111 | { |
… | |
… | |
117 | |
137 | |
118 | case SVt_PVCV: |
138 | case SVt_PVCV: |
119 | { |
139 | { |
120 | int depth = CvDEPTH (sv); |
140 | int depth = CvDEPTH (sv); |
121 | |
141 | |
|
|
142 | /* Anonymous subs have a padlist but zero depth */ |
|
|
143 | if (CvANON (sv) && !depth && CvPADLIST (sv)) |
|
|
144 | depth = 1; |
|
|
145 | |
122 | if (depth) |
146 | if (depth) |
123 | { |
147 | { |
124 | AV *padlist = CvPADLIST (sv); |
148 | AV *padlist = CvPADLIST (sv); |
125 | |
149 | |
126 | while (depth) |
150 | while (depth) |
127 | { |
151 | { |
128 | AV *pad = (AV *)AvARRAY (padlist)[depth]; |
152 | AV *pad = (AV *)AvARRAY (padlist)[depth]; |
129 | |
153 | |
130 | av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads themselves from being found */ |
154 | av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads themselves from being found */ |
131 | |
155 | |
132 | for (i = AvFILLp (pad); i--; ) |
156 | for (i = AvFILLp (pad) + 1; i--; ) |
133 | if (AvARRAY (pad)[i] == targ) |
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)) |
134 | res_pair (form ("in the lexical '%s' in", SvPVX (AvARRAY (AvARRAY (padlist)[0])[i]))); |
163 | res_pair (form ("in the lexical '%s' in", SvPVX (name_sv))); |
|
|
164 | else |
|
|
165 | res_pair ("in an unnamed lexical in"); |
|
|
166 | } |
135 | |
167 | |
136 | --depth; |
168 | --depth; |
137 | } |
169 | } |
138 | } |
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>")); |
139 | } |
188 | } |
140 | |
189 | |
141 | break; |
190 | break; |
142 | |
191 | |
143 | case SVt_PVGV: |
192 | case SVt_PVGV: |