ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Devel-FindRef/FindRef.xs
(Generate patch)

Comparing Devel-FindRef/FindRef.xs (file contents):
Revision 1.5 by root, Wed Nov 28 12:20:33 2007 UTC vs.
Revision 1.15 by root, Mon Dec 1 13:47:09 2008 UTC

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, 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)
22 28
23#define res_gv(sigil) \ 29#define res_text(text) \
24 { \ 30 do { \
25 AV *av = newAV (); \ 31 AV *av = newAV (); \
26 av_push (av, newSVpv (form ("in the global %c%s::%.*s", sigil, \ 32 av_push (av, newSVpv (text, 0)); \
27 HvNAME (GvSTASH (sv)), \
28 GvNAMELEN (sv), GvNAME (sv) ? GvNAME (sv) : "<anonymous>"), \
29 0)); \
30 av_push (about, newRV_noinc ((SV *)av)); \ 33 av_push (about, newRV_noinc ((SV *)av)); \
31 } 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>"))
32 41
33MODULE = Devel::FindRef PACKAGE = Devel::FindRef 42MODULE = Devel::FindRef PACKAGE = Devel::FindRef
34 43
35PROTOTYPES: ENABLE 44PROTOTYPES: ENABLE
36 45
37SV *
38ptr2ref (IV ptr)
39 CODE:
40 RETVAL = newRV_inc (INT2PTR (SV *, ptr));
41 OUTPUT:
42 RETVAL
43
44void 46void
45find_ (SV *target) 47find_ (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));
94
82 if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED) 95 if ((SV *)mg->mg_ptr == targ && mg->mg_flags & MGf_REFCOUNTED)
83 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));
84 97
85 mg = mg->mg_moremagic; 98 mg = mg->mg_moremagic;
99 }
86 } 100 }
87 } 101 }
88 102
89 switch (SvTYPE (sv)) 103 if (SvROK (sv))
90 { 104 {
91 case SVt_RV: 105 if (SvRV (sv) == targ && !SvWEAKREF (sv) && sv != target_ref)
92 if (sv != target && SvRV (sv) == targ)
93 res_pair ("referenced by"); 106 res_pair ("referenced by");
107 }
108 else
109 switch (SvTYPE (sv))
94 break; 110 {
95
96 case SVt_PVAV: 111 case SVt_PVAV:
97 if (AvREAL (sv)) 112 if (AvREAL (sv))
98 for (i = AvFILLp (sv) + 1; i--; ) 113 for (i = AvFILLp (sv) + 1; i--; )
99 if (AvARRAY (sv)[i] == targ) 114 if (AvARRAY (sv)[i] == targ)
100 res_pair (form ("in array element %d of", i)); 115 res_pair (form ("in array element %d of", i));
116
101 break; 117 break;
102 118
103 case SVt_PVHV: 119 case SVt_PVHV:
104 if (hv_iterinit ((HV *)sv)) 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:
105 { 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)
106 HE *he; 140 {
107 while ((he = hv_iternext ((HV *)sv))) 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 @_ */
108 if (HeVAL (he) == targ) 150 if (AvARRAY (pad)[0] == targ)
109 res_pair (form ("in the member '%.*s' of", HeKLEN (he), HeKEY (he))); 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>"));
110 } 185 }
186
111 break; 187 break;
112 188
113 case SVt_PVCV: 189 case SVt_PVGV:
114 {
115 int depth = CvDEPTH (sv);
116 if (depth) 190 if (GvGP (sv))
117 { 191 {
118 AV *padlist = CvPADLIST (sv); 192 if (GvSV (sv) == (SV *)targ) res_gv ('$');
119 while (depth) 193 if (GvAV (sv) == (AV *)targ) res_gv ('@');
120 { 194 if (GvHV (sv) == (HV *)targ) res_gv ('%');
121 AV *pad = (AV *)AvARRAY (padlist)[depth]; 195 if (GvCV (sv) == (CV *)targ) res_gv ('&');
122 av_push (excl, newSVuv (PTR2UV (pad))); /* exclude pads from being found */
123 for (i = AvFILLp (pad); i--; )
124 if (AvARRAY (pad)[i] == targ)
125 res_pair (form ("in the lexical '%s' in", SvPVX (AvARRAY (AvARRAY (padlist)[0])[i])));
126
127 --depth;
128 }
129 } 196 }
130 } 197
131 break; 198 break;
132
133 case SVt_PVGV:
134 if (GvGP (sv))
135 {
136 if (GvSV (sv) == targ)
137 res_gv ('$');
138 if (GvAV (sv) == (AV *)targ)
139 res_gv ('@');
140 if (GvHV (sv) == (HV *)targ)
141 res_gv ('%');
142 if (GvCV (sv) == (CV *)targ)
143 res_gv ('&');
144 }
145 break;
146 } 199 }
147 200
148 if (rmagical) 201 if (rmagical)
149 SvRMAGICAL_on (sv); 202 SvRMAGICAL_on (sv);
150 } 203 }
151 } 204 }
152 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
153 EXTEND (SP, 2); 214 EXTEND (SP, 2);
154 PUSHs (sv_2mortal (newRV_noinc ((SV *)about))); 215 PUSHs (sv_2mortal (newRV_noinc ((SV *)about)));
155 PUSHs (sv_2mortal (newRV_noinc ((SV *)excl))); 216 PUSHs (sv_2mortal (newRV_noinc ((SV *)excl)));
156} 217}
157 218
219SV *
220ptr2ref (UV ptr)
221 CODE:
222 RETVAL = newRV_inc (INT2PTR (SV *, ptr));
223 OUTPUT:
224 RETVAL
225
226UV
227ref2ptr (SV *rv)
228 CODE:
229 RETVAL = PTR2UV (SvRV (rv));
230 OUTPUT:
231 RETVAL
232
233U32
234_refcnt (SV *rv)
235 CODE:
236 RETVAL = SvREFCNT (SvRV (rv));
237 OUTPUT:
238 RETVAL

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines